(function () {
    "use strict";

    var KEY_CODES = {
        ENTER: 13,
        LEFT: 37,
        UP: 38,
        RIGHT: 39,
        DOWN: 40,
        BACK: 10009,
        RETURN: 461,
        PLAY: 415,
        PAUSE: 19,
        PLAY_PAUSE: 10252,
        MEDIA_PLAY_PAUSE: 179
    };

    var APP_SERVER_URL = "https://rivobu.com/backend";
    var IPTV_PLAYLIST_URL = "";
    var IPTV_MAX_ITEMS_PER_SECTION = 0;
    var VOD_RENDER_LIMIT = 0;
    var XTREAM_LIBRARY_TIMEOUT = 35000;

    var SECTIONS = [
        { id: "live", title: "مباشر", subtitle: "Live TV", icon: "TV" },
        { id: "movies", title: "أفلام", subtitle: "Movies", icon: "Play" },
        { id: "series", title: "مسلسلات", subtitle: "Series", icon: "List" }
    ];

    var state = {
        screen: "home",
        homePane: "sections",
        homeActionIndex: 0,
        latestIndex: 0,
        managementFocusIndex: 0,
        sectionIndex: 0,
        activeSection: "live",
        searchQuery: "",
        categories: [],
        categoryIndex: 0,
        itemIndex: 0,
        libraryPane: "categories",
        downloadActionIndex: 0,
        detailActionIndex: 0,
        detailPane: "actions",
        deviceMac: "",
        activation: null,
        language: "ar",
        playerMode: "auto",
        seasonIndex: 0,
        episodeIndex: 0,
        castIndex: 0,
        actorWorkIndex: 0,
        focusedControl: "play",
        playerToolOptionIndex: -1,
        subtitleSelectionLocked: false,
        subtitleCueTracks: {},
        activeSubtitleCueKey: "",
        activeSubtitleCues: [],
        lastSubtitleText: "",
        subtitleOverlayTimer: null,
        subtitleDelay: 0,
        overlayTimer: null,
        returnScreen: "library",
        detailReturnScreen: "library",
        detailItem: null,
        playingItem: null,
        pendingResume: null,
        lastProgressSaveAt: 0,
        splashVisible: true,
        splashStartedAt: Date.now(),
        splashHideTimer: null
    };

    var elements = {};
    var player;
    var allItems = [];
    var visibleItems = [];
    var renderedItems = [];
    var latestItems = [];
    var detailMetadataCache = {};
    var tmdbCastCache = {};
    var tmdbPersonImageCache = {};
    var tmdbResolveCache = {};
    var imdbResolveCache = {};
    var detailSeasons = [];
    var detailEpisodes = [];
    var actorWorks = [];
    var actorSearchToken = 0;
    var subtitleCueKeyCounter = 0;

    function init() {
        cacheElements();
        registerRemoteKeys();
        createPlayer();
        loadAppSettings();
        bindDomEvents();
        renderHome();
        updateHomeClock();
        setInterval(updateHomeClock, 30000);
        initializeActivation();
    }

    function cacheElements() {
        elements.homeScreen = document.getElementById("homeScreen");
        elements.splashScreen = document.getElementById("splashScreen");
        elements.splashStatus = document.getElementById("splashStatus");
        elements.playlistScreen = document.getElementById("playlistScreen");
        elements.accountScreen = document.getElementById("accountScreen");
        elements.historyScreen = document.getElementById("historyScreen");
        elements.settingsScreen = document.getElementById("settingsScreen");
        elements.libraryScreen = document.getElementById("libraryScreen");
        elements.detailScreen = document.getElementById("detailScreen");
        elements.playerScreen = document.getElementById("playerScreen");
        elements.playlistStatus = document.getElementById("playlistStatus");
        elements.sectionList = document.getElementById("sectionList");
        elements.homeActionList = document.getElementById("homeActionList");
        elements.latestList = document.getElementById("latestList");
        elements.latestCount = document.getElementById("latestCount");
        elements.activationSummary = document.getElementById("activationSummary");
        elements.playlistBackButton = document.getElementById("playlistBackButton");
        elements.playlistDeviceInfo = document.getElementById("playlistDeviceInfo");
        elements.playlistCurrentInfo = document.getElementById("playlistCurrentInfo");
        elements.playlistForm = document.getElementById("playlistForm");
        elements.playlistType = document.getElementById("playlistType");
        elements.playlistUrlInput = document.getElementById("playlistUrlInput");
        elements.playlistServerInput = document.getElementById("playlistServerInput");
        elements.playlistUsernameInput = document.getElementById("playlistUsernameInput");
        elements.playlistPasswordInput = document.getElementById("playlistPasswordInput");
        elements.playlistDeleteButton = document.getElementById("playlistDeleteButton");
        elements.playlistSaveButton = document.getElementById("playlistSaveButton");
        elements.playlistFormMessage = document.getElementById("playlistFormMessage");
        elements.accountBackButton = document.getElementById("accountBackButton");
        elements.accountInfo = document.getElementById("accountInfo");
        elements.accountPayButton = document.getElementById("accountPayButton");
        elements.accountDashboardButton = document.getElementById("accountDashboardButton");
        elements.historyBackButton = document.getElementById("historyBackButton");
        elements.historyHeading = document.getElementById("historyHeading");
        elements.recentHeading = document.getElementById("recentHeading");
        elements.favoriteHeading = document.getElementById("favoriteHeading");
        elements.recentList = document.getElementById("recentList");
        elements.favoriteList = document.getElementById("favoriteList");
        elements.settingsBackButton = document.getElementById("settingsBackButton");
        elements.settingsForm = document.getElementById("settingsForm");
        elements.appLanguageSelect = document.getElementById("appLanguageSelect");
        elements.playerModeSelect = document.getElementById("playerModeSelect");
        elements.settingsMessage = document.getElementById("settingsMessage");
        elements.settingsNav = document.querySelector(".settings-nav");
        elements.homeTime = document.getElementById("homeTime");
        elements.homeDate = document.getElementById("homeDate");
        elements.powerButton = document.getElementById("powerButton");
        elements.libraryBackButton = document.getElementById("libraryBackButton");
        elements.libraryEyebrow = document.getElementById("libraryEyebrow");
        elements.libraryHeading = document.getElementById("libraryHeading");
        elements.categoryHeading = document.getElementById("categoryHeading");
        elements.librarySearchInput = document.getElementById("librarySearchInput");
        elements.categoryList = document.getElementById("categoryList");
        elements.itemList = document.getElementById("itemList");
        elements.itemCount = document.getElementById("itemCount");
        elements.downloadSelectedButton = document.getElementById("downloadSelectedButton");
        elements.downloadCategoryButton = document.getElementById("downloadCategoryButton");
        elements.downloadMessage = document.getElementById("downloadMessage");
        elements.detailBackdrop = document.getElementById("detailBackdrop");
        elements.detailBackButton = document.getElementById("detailBackButton");
        elements.detailEyebrow = document.getElementById("detailEyebrow");
        elements.detailTitle = document.getElementById("detailTitle");
        elements.detailMeta = document.getElementById("detailMeta");
        elements.detailDescription = document.getElementById("detailDescription");
        elements.detailPlayButton = document.getElementById("detailPlayButton");
        elements.detailTrailerButton = document.getElementById("detailTrailerButton");
        elements.detailFavoriteButton = document.getElementById("detailFavoriteButton");
        elements.detailWatchlistButton = document.getElementById("detailWatchlistButton");
        elements.detailActionMessage = document.getElementById("detailActionMessage");
        elements.detailPoster = document.getElementById("detailPoster");
        elements.seasonSection = document.getElementById("seasonSection");
        elements.seasonList = document.getElementById("seasonList");
        elements.episodeList = document.getElementById("episodeList");
        elements.castList = document.getElementById("castList");
        elements.actorWorksSection = document.getElementById("actorWorksSection");
        elements.actorWorksTitle = document.getElementById("actorWorksTitle");
        elements.actorWorksList = document.getElementById("actorWorksList");
        elements.video = document.getElementById("videoPlayer");
        elements.subtitleOverlay = document.getElementById("subtitleOverlay");
        elements.videoTitle = document.getElementById("videoTitle");
        elements.loading = document.getElementById("loadingIndicator");
        elements.error = document.getElementById("errorMessage");
        elements.overlay = document.getElementById("controlsOverlay");
        elements.playPauseButton = document.getElementById("playPauseButton");
        elements.backButton = document.getElementById("backButton");
        elements.progressBar = document.getElementById("progressBar");
        elements.currentTime = document.getElementById("currentTime");
        elements.duration = document.getElementById("duration");
        elements.audioLanguageSelect = document.getElementById("audioLanguageSelect");
        elements.subtitleSelect = document.getElementById("subtitleSelect");
        elements.subtitleStyleSelect = document.getElementById("subtitleStyleSelect");
        elements.screenModeSelect = document.getElementById("screenModeSelect");
        elements.playerToolButtons = document.getElementById("playerToolButtons");
        elements.favoriteToolButton = document.getElementById("favoriteToolButton");
        elements.subtitleToolButton = document.getElementById("subtitleToolButton");
        elements.subtitleSettingsToolButton = document.getElementById("subtitleSettingsToolButton");
        elements.screenToolButton = document.getElementById("screenToolButton");
        elements.playerToolPanel = document.getElementById("playerToolPanel");
        elements.playerToolTitle = document.getElementById("playerToolTitle");
        elements.playerToolOptions = document.getElementById("playerToolOptions");
        elements.resumeDialog = document.getElementById("resumeDialog");
        elements.resumeDialogText = document.getElementById("resumeDialogText");
        elements.resumeContinueButton = document.getElementById("resumeContinueButton");
        elements.resumeRestartButton = document.getElementById("resumeRestartButton");
    }

    function registerRemoteKeys() {
        if (!window.tizen || !tizen.tvinputdevice) {
            return;
        }

        ["MediaPlay", "MediaPause", "MediaPlayPause"].forEach(function (keyName) {
            try {
                tizen.tvinputdevice.registerKey(keyName);
            } catch (error) {
                console.log("Could not register key:", keyName, error.message);
            }
        });
    }

    function createPlayer() {
        window.NEXO_APP_SERVER_URL = APP_SERVER_URL;
        player = new window.TvVideoPlayer({
            video: elements.video,
            titleElement: elements.videoTitle,
            loadingElement: elements.loading,
            errorElement: elements.error,
            playPauseButton: elements.playPauseButton,
            progressBar: elements.progressBar,
            currentTimeElement: elements.currentTime,
            durationElement: elements.duration,
            serverUrl: APP_SERVER_URL,
            onTracksChanged: refreshPlayerOptions
        });
    }

    function loadAppSettings() {
        try {
            state.language = window.localStorage.getItem("tizenPlayerLanguage") || "ar";
            state.playerMode = window.localStorage.getItem("tizenPlayerMode") || "auto";
        } catch (error) {
            state.language = "ar";
            state.playerMode = "auto";
        }

        player.setPlaybackMode(state.playerMode);
        if (elements.appLanguageSelect) {
            elements.appLanguageSelect.value = state.language;
        }
        if (elements.playerModeSelect) {
            elements.playerModeSelect.value = state.playerMode;
        }
        applyLanguage();
        applyScreenMode();
        applySubtitleStyle();
    }

    function saveSettings(event) {
        event.preventDefault();
        state.language = elements.appLanguageSelect.value;
        state.playerMode = elements.playerModeSelect.value;

        try {
            window.localStorage.setItem("tizenPlayerLanguage", state.language);
            window.localStorage.setItem("tizenPlayerMode", state.playerMode);
        } catch (error) {
            console.log("Could not save settings:", error.message);
        }

        player.setPlaybackMode(state.playerMode);
        applyLanguage();
        elements.settingsMessage.hidden = false;
        elements.settingsMessage.textContent = "تم حفظ الإعدادات";
        renderHome();
    }

    function handleSettingsClick(event) {
        var panelButton = event.target.closest("[data-settings-panel]");
        var languageButton = event.target.closest("[data-language]");
        var playerButton = event.target.closest("[data-player-mode]");

        if (panelButton && panelButton.classList.contains("settings-nav-button")) {
            setSettingsPanel(panelButton.getAttribute("data-settings-panel"));
            return;
        }

        if (languageButton) {
            elements.appLanguageSelect.value = languageButton.getAttribute("data-language");
            syncSettingsButtons();
            return;
        }

        if (playerButton) {
            elements.playerModeSelect.value = playerButton.getAttribute("data-player-mode");
            syncSettingsButtons();
        }
    }

    function setSettingsPanel(panelName) {
        Array.prototype.slice.call(elements.settingsForm.querySelectorAll(".settings-nav-button")).forEach(function (button) {
            button.classList.toggle("is-active", button.getAttribute("data-settings-panel") === panelName);
        });
        Array.prototype.slice.call(elements.settingsForm.querySelectorAll(".settings-panel")).forEach(function (panel) {
            panel.classList.toggle("is-active", panel.getAttribute("data-settings-panel") === panelName);
        });
    }

    function syncSettingsButtons() {
        Array.prototype.slice.call(elements.settingsForm.querySelectorAll("[data-language]")).forEach(function (button) {
            button.classList.toggle("is-selected", button.getAttribute("data-language") === elements.appLanguageSelect.value);
        });
        Array.prototype.slice.call(elements.settingsForm.querySelectorAll("[data-player-mode]")).forEach(function (button) {
            button.classList.toggle("is-selected", button.getAttribute("data-player-mode") === elements.playerModeSelect.value);
        });
    }

    function applyLanguage() {
        var labels = getLanguageLabels(state.language);
        var actionButtons = elements.homeActionList ? elements.homeActionList.querySelectorAll(".home-action-button") : [];

        SECTIONS[0].title = labels.live;
        SECTIONS[0].subtitle = "Live TV";
        SECTIONS[1].title = labels.movies;
        SECTIONS[1].subtitle = "Movies";
        SECTIONS[2].title = labels.series;
        SECTIONS[2].subtitle = "Series";

        if (actionButtons[0]) {
            actionButtons[0].textContent = labels.account;
        }
        if (actionButtons[1]) {
            actionButtons[1].textContent = labels.settings;
        }
        if (actionButtons[2]) {
            actionButtons[2].textContent = labels.historyFavorites;
        }
        if (actionButtons[3]) {
            actionButtons[3].textContent = labels.playlists;
        }
    }

    function getLanguageLabels(language) {
        var labels = {
            ar: { live: "مباشر", movies: "أفلام", series: "مسلسلات", account: "حسابي", settings: "الإعدادات", playlists: "قوائم التشغيل" },
            en: { live: "Live TV", movies: "Movies", series: "Series", account: "My Account", settings: "Settings", playlists: "List Playlists" },
            tr: { live: "Canli TV", movies: "Filmler", series: "Diziler", account: "Hesabim", settings: "Ayarlar", playlists: "Listeler" },
            de: { live: "Live-TV", movies: "Filme", series: "Serien", account: "Mein Konto", settings: "Einstellungen", playlists: "Playlists" },
            fr: { live: "TV en direct", movies: "Films", series: "Series", account: "Mon compte", settings: "Parametres", playlists: "Listes" },
            ru: { live: "Live TV", movies: "Filmy", series: "Serialy", account: "Akkount", settings: "Nastroiki", playlists: "Pleilisty" },
            zh: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            ja: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            es: { live: "TV en vivo", movies: "Peliculas", series: "Series", account: "Mi cuenta", settings: "Ajustes", playlists: "Listas" },
            it: { live: "TV live", movies: "Film", series: "Serie", account: "Account", settings: "Impostazioni", playlists: "Playlist" },
            hi: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            "pt-BR": { live: "TV ao vivo", movies: "Filmes", series: "Series", account: "Minha conta", settings: "Configuracoes", playlists: "Playlists" }
        };

        return labels[language] || labels.ar;
    }

    function applyLanguage() {
        var labels = getLanguageLabels(state.language);
        var actionButtons = elements.homeActionList ? elements.homeActionList.querySelectorAll(".home-action-button") : [];

        SECTIONS[0].title = labels.live;
        SECTIONS[0].subtitle = "Live TV";
        SECTIONS[1].title = labels.movies;
        SECTIONS[1].subtitle = "Movies";
        SECTIONS[2].title = labels.series;
        SECTIONS[2].subtitle = "Series";

        if (actionButtons[0]) {
            actionButtons[0].textContent = labels.account;
        }
        if (actionButtons[1]) {
            actionButtons[1].textContent = labels.settings;
        }
        if (actionButtons[2]) {
            actionButtons[2].textContent = labels.historyFavorites;
        }
        if (actionButtons[3]) {
            actionButtons[3].textContent = labels.playlists;
        }

        applyStaticLanguage(labels);
    }

    function getLanguageLabels(language) {
        var ar = {
            live: "\u0645\u0628\u0627\u0634\u0631",
            movies: "\u0623\u0641\u0644\u0627\u0645",
            series: "\u0645\u0633\u0644\u0633\u0644\u0627\u062a",
            account: "\u062d\u0633\u0627\u0628\u064a",
            settings: "\u0627\u0644\u0625\u0639\u062f\u0627\u062f\u0627\u062a",
            playlists: "\u0642\u0648\u0627\u0626\u0645 \u0627\u0644\u062a\u0634\u063a\u064a\u0644",
            historyFavorites: "\u0622\u062e\u0631 \u0627\u0644\u0645\u0634\u0627\u0647\u062f\u0627\u062a \u0648\u0627\u0644\u0645\u0641\u0636\u0644\u0629",
            recentlyWatched: "\u0622\u062e\u0631 \u0627\u0644\u0645\u0634\u0627\u0647\u062f\u0627\u062a",
            favorites: "\u0627\u0644\u0645\u0641\u0636\u0644\u0629",
            noRecent: "\u0644\u0627 \u062a\u0648\u062c\u062f \u0645\u0634\u0627\u0647\u062f\u0627\u062a \u062d\u062f\u064a\u062b\u0629.",
            noFavorites: "\u0644\u0627 \u062a\u0648\u062c\u062f \u0639\u0646\u0627\u0635\u0631 \u0645\u0641\u0636\u0644\u0629.",
            back: "\u0631\u062c\u0648\u0639",
            latest: "\u0622\u062e\u0631 \u0627\u0644\u0623\u0641\u0644\u0627\u0645 \u0648\u0627\u0644\u0645\u0633\u0644\u0633\u0644\u0627\u062a \u0627\u0644\u0645\u0636\u0627\u0641\u0629",
            categories: "\u0627\u0644\u062a\u0635\u0646\u064a\u0641\u0627\u062a",
            search: "\u0628\u062d\u062b",
            all: "\u0627\u0644\u0643\u0644",
            items: "\u0639\u0646\u0627\u0635\u0631",
            unavailable: "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631",
            unknown: "\u063a\u064a\u0631 \u0645\u0639\u0631\u0648\u0641",
            status: "\u0627\u0644\u062d\u0627\u0644\u0629",
            start: "\u0627\u0644\u0628\u062f\u0627\u064a\u0629",
            end: "\u0627\u0644\u0646\u0647\u0627\u064a\u0629",
            siteUrl: "\u0631\u0627\u0628\u0637 \u0627\u0644\u0645\u0648\u0642\u0639",
            playlistType: "\u0642\u0627\u0626\u0645\u0629 \u0627\u0644\u062a\u0634\u063a\u064a\u0644",
            notConfigured: "\u063a\u064a\u0631 \u0645\u0636\u0628\u0648\u0637",
            notAdded: "\u063a\u064a\u0631 \u0645\u0636\u0627\u0641\u0629",
            openDashboard: "\u0641\u062a\u062d \u0644\u0648\u062d\u0629 \u0627\u0644\u0645\u0648\u0642\u0639",
            playlistTypeLabel: "\u0646\u0648\u0639 \u0627\u0644\u0642\u0627\u0626\u0645\u0629",
            currentPlaylistSaved: "\u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0627\u0644\u062d\u0627\u0644\u064a\u0629 \u0645\u062d\u0641\u0648\u0638\u0629 \u0641\u064a \u0627\u0644\u0645\u0648\u0642\u0639",
            noSavedPlaylist: "\u0644\u0627 \u062a\u0648\u062c\u062f \u0642\u0627\u0626\u0645\u0629 \u0645\u062d\u0641\u0648\u0638\u0629 \u0644\u0647\u0630\u0627 \u0627\u0644\u062c\u0647\u0627\u0632 \u062d\u062a\u0649 \u0627\u0644\u0622\u0646.",
            saveOnSite: "\u062d\u0641\u0638 \u0639\u0644\u0649 \u0627\u0644\u0645\u0648\u0642\u0639",
            deleteFromSite: "\u062d\u0630\u0641 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0645\u0646 \u0627\u0644\u0645\u0648\u0642\u0639",
            enterM3u: "\u0623\u062f\u062e\u0644 \u0631\u0627\u0628\u0637 M3U.",
            enterXtream: "\u0623\u062f\u062e\u0644 \u0631\u0627\u0628\u0637 \u0627\u0644\u0633\u064a\u0631\u0641\u0631 \u0648\u0627\u0633\u0645 \u0627\u0644\u0645\u0633\u062a\u062e\u062f\u0645 \u0648\u0643\u0644\u0645\u0629 \u0627\u0644\u0645\u0631\u0648\u0631.",
            savingPlaylist: "\u062c\u0627\u0631\u064a \u0627\u0644\u062d\u0641\u0638 \u0641\u064a \u0627\u0644\u0645\u0648\u0642\u0639...",
            playlistSaved: "\u062a\u0645 \u062d\u0641\u0638 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0641\u064a \u0627\u0644\u0645\u0648\u0642\u0639 \u0644\u0647\u0630\u0627 \u0627\u0644\u062c\u0647\u0627\u0632.",
            playlistSaveFailed: "\u062a\u0639\u0630\u0631 \u062d\u0641\u0638 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0641\u064a \u0627\u0644\u0645\u0648\u0642\u0639.",
            deletingPlaylist: "\u062c\u0627\u0631\u064a \u062d\u0630\u0641 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0645\u0646 \u0627\u0644\u0645\u0648\u0642\u0639...",
            playlistDeleted: "\u062a\u0645 \u062d\u0630\u0641 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0645\u0646 \u0627\u0644\u0645\u0648\u0642\u0639.",
            playlistDeleteFailed: "\u062a\u0639\u0630\u0631 \u062d\u0630\u0641 \u0627\u0644\u0642\u0627\u0626\u0645\u0629 \u0645\u0646 \u0627\u0644\u0645\u0648\u0642\u0639.",
            serverRequired: "\u064a\u062c\u0628 \u0636\u0628\u0637 \u0631\u0627\u0628\u0637 \u0627\u0644\u0645\u0648\u0642\u0639 \u062f\u0627\u062e\u0644 \u0627\u0644\u062a\u0637\u0628\u064a\u0642 \u0623\u0648\u0644\u0627.",
            play: "\u062a\u0634\u063a\u064a\u0644",
            pause: "\u0625\u064a\u0642\u0627\u0641",
            trailer: "\u0645\u0634\u0627\u0647\u062f\u0629 \u0627\u0644\u0645\u0642\u0637\u0639 \u0627\u0644\u0625\u0639\u0644\u0627\u0646\u064a",
            removeWatchlist: "\u0625\u0632\u0627\u0644\u0629 \u0645\u0646 \u0642\u0627\u0626\u0645\u0629 \u0627\u0644\u0645\u0634\u0627\u0647\u062f\u0629",
            story: "\u0642\u0635\u0629 \u0627\u0644\u0641\u064a\u0644\u0645",
            seasons: "\u0645\u0648\u0627\u0633\u0645",
            season: "\u0645\u0648\u0633\u0645",
            cast: "\u0627\u0644\u0646\u062c\u0648\u0645",
            actorWorks: "\u0623\u0639\u0645\u0627\u0644 \u0627\u0644\u0645\u0645\u062b\u0644",
            actorWorksOf: "\u0623\u0639\u0645\u0627\u0644 ",
            noActorWorks: "\u0644\u0627 \u062a\u0648\u062c\u062f \u0623\u0639\u0645\u0627\u0644 \u0645\u0631\u062a\u0628\u0637\u0629 \u0628\u0647\u0630\u0627 \u0627\u0644\u0645\u0645\u062b\u0644 \u0641\u064a \u0627\u0644\u0645\u0643\u062a\u0628\u0629.",
            loadingActorWorks: "\u062c\u0627\u0631\u064a \u0627\u0644\u0628\u062d\u062b \u0639\u0646 \u0623\u0639\u0645\u0627\u0644 ",
            audio: "\u0627\u0644\u0635\u0648\u062a",
            subtitle: "\u0627\u0644\u062a\u0631\u062c\u0645\u0629",
            subtitleStyle: "\u0634\u0643\u0644 \u0627\u0644\u062a\u0631\u062c\u0645\u0629",
            screen: "\u0627\u0644\u0634\u0627\u0634\u0629",
            defaultAudio: "\u0627\u0644\u0635\u0648\u062a \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a",
            noSubtitle: "\u0628\u062f\u0648\u0646 \u062a\u0631\u062c\u0645\u0629",
            saved: "\u062a\u0645 \u062d\u0641\u0638 \u0627\u0644\u0625\u0639\u062f\u0627\u062f\u0627\u062a.",
            saveSettings: "\u062d\u0641\u0638 \u0627\u0644\u0625\u0639\u062f\u0627\u062f\u0627\u062a",
            accountTitle: "\u0644\u0648\u062d\u0629 \u062a\u062d\u0643\u0645 \u0627\u0644\u0639\u0645\u064a\u0644",
            playlistTitle: "\u0642\u0648\u0627\u0626\u0645 \u0627\u0644\u062a\u0634\u063a\u064a\u0644",
            settingsLanguages: "\u0627\u0644\u0644\u063a\u0627\u062a",
            settingsPlayer: "\u0627\u0644\u0645\u0634\u063a\u0644",
            settingsSubtitles: "\u0627\u0644\u062a\u0631\u062c\u0645\u0629",
            subtitleHelp: "\u062a\u0638\u0647\u0631 \u062e\u064a\u0627\u0631\u0627\u062a \u0627\u0644\u062a\u0631\u062c\u0645\u0629 \u0648\u0627\u0644\u0634\u0643\u0644 \u062f\u0627\u062e\u0644 \u0645\u0634\u063a\u0644 \u0627\u0644\u0641\u064a\u062f\u064a\u0648."
            ,
            subtitleTitle: "\u0627\u0644\u0639\u0646\u0648\u0627\u0646 \u0627\u0644\u0641\u0631\u0639\u064a",
            subtitleSettingsTitle: "\u0625\u0639\u062f\u0627\u062f\u0627\u062a \u0627\u0644\u062a\u0631\u062c\u0645\u0629",
            screenSettingsTitle: "\u0646\u0633\u0628\u0629 \u0627\u0644\u0639\u0631\u0636 \u0625\u0644\u0649 \u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639",
            subtitleSample: "\u0633\u064a\u0628\u062f\u0648 \u062a\u0631\u062c\u0645\u0627\u062a\u0643 \u0645\u062b\u0644 \u0647\u0630\u0627",
            small: "\u0635\u063a\u064a\u0631",
            normal: "\u0639\u0627\u062f\u064a",
            large: "\u0643\u0628\u064a\u0631",
            shadow: "\u0625\u0633\u0642\u0627\u0637 \u0627\u0644\u0638\u0644",
            boxed: "\u062e\u0644\u0641\u064a\u0629 \u062f\u0627\u0643\u0646\u0629",
            noBox: "\u0628\u062f\u0648\u0646 \u062e\u0644\u0641\u064a\u0629",
            delay: "\u062a\u0623\u062e\u064a\u0631",
            fit: "\u0627\u0641\u062a\u0631\u0627\u0636\u064a",
            fullscreen: "\u0645\u0644\u0621 \u0627\u0644\u0634\u0627\u0634\u0629"
        };
        var en = {
            live: "Live TV",
            movies: "Movies",
            series: "Series",
            account: "My Account",
            settings: "Settings",
            playlists: "List Playlists",
            historyFavorites: "Recent & Favorites",
            recentlyWatched: "Recently watched",
            favorites: "Favorites",
            noRecent: "No recently watched items.",
            noFavorites: "No favorite items.",
            back: "Back",
            latest: "Latest movies and series",
            categories: "Categories",
            search: "Search",
            all: "All",
            items: "items",
            unavailable: "Unavailable",
            unknown: "unknown",
            status: "Status",
            start: "Start",
            end: "End",
            siteUrl: "Site URL",
            playlistType: "Playlist",
            notConfigured: "Not configured",
            notAdded: "Not added",
            openDashboard: "Open dashboard",
            playlistTypeLabel: "Playlist type",
            currentPlaylistSaved: "Current playlist saved on the site",
            noSavedPlaylist: "No saved playlist exists for this device yet.",
            saveOnSite: "Save on site",
            deleteFromSite: "Delete playlist from site",
            enterM3u: "Enter an M3U URL.",
            enterXtream: "Enter server URL, username, and password.",
            savingPlaylist: "Saving playlist on site...",
            playlistSaved: "Playlist saved on site for this device.",
            playlistSaveFailed: "Could not save playlist on site.",
            deletingPlaylist: "Deleting playlist from site...",
            playlistDeleted: "Playlist deleted from site.",
            playlistDeleteFailed: "Could not delete playlist from site.",
            serverRequired: "Configure the site URL inside the app first.",
            play: "Play",
            pause: "Pause",
            trailer: "Watch trailer",
            removeWatchlist: "Remove from watchlist",
            story: "Story",
            seasons: "Seasons",
            season: "Season",
            cast: "Cast",
            actorWorks: "Actor works",
            actorWorksOf: "Works of ",
            noActorWorks: "No matching movies or series were found for this actor in your library.",
            loadingActorWorks: "Searching works for ",
            audio: "Audio",
            subtitle: "Subtitles",
            subtitleStyle: "Subtitle style",
            screen: "Screen",
            defaultAudio: "Default audio",
            noSubtitle: "No subtitles",
            saved: "Settings saved.",
            saveSettings: "Save settings",
            accountTitle: "Customer dashboard",
            playlistTitle: "Playlists",
            settingsLanguages: "Languages",
            settingsPlayer: "Player",
            settingsSubtitles: "Subtitles",
            subtitleHelp: "Subtitle track and style controls appear inside the video player."
            ,
            subtitleTitle: "Subtitles",
            subtitleSettingsTitle: "Subtitle settings",
            screenSettingsTitle: "Aspect ratio",
            subtitleSample: "Your subtitles will look like this",
            small: "Small",
            normal: "Normal",
            large: "Large",
            shadow: "Drop shadow",
            boxed: "Dark box",
            noBox: "No box",
            delay: "Delay",
            fit: "Fit",
            fullscreen: "Full screen"
        };
        var labels = {
            ar: ar,
            en: en,
            tr: { live: "Canli TV", movies: "Filmler", series: "Diziler", account: "Hesabim", settings: "Ayarlar", playlists: "Listeler" },
            de: { live: "Live-TV", movies: "Filme", series: "Serien", account: "Mein Konto", settings: "Einstellungen", playlists: "Playlists" },
            fr: { live: "TV en direct", movies: "Films", series: "Series", account: "Mon compte", settings: "Parametres", playlists: "Listes" },
            ru: { live: "Live TV", movies: "Filmy", series: "Serialy", account: "Akkount", settings: "Nastroiki", playlists: "Pleilisty" },
            zh: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            ja: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            es: { live: "TV en vivo", movies: "Peliculas", series: "Series", account: "Mi cuenta", settings: "Ajustes", playlists: "Listas" },
            it: { live: "TV live", movies: "Film", series: "Serie", account: "Account", settings: "Impostazioni", playlists: "Playlist" },
            hi: { live: "Live TV", movies: "Movies", series: "Series", account: "Account", settings: "Settings", playlists: "Playlists" },
            "pt-BR": { live: "TV ao vivo", movies: "Filmes", series: "Series", account: "Minha conta", settings: "Configuracoes", playlists: "Playlists" }
        };

        return Object.assign({}, en, labels[language] || ar);
    }

    function applyStaticLanguage(labels) {
        setText(elements.playlistBackButton, labels.back);
        setText(elements.accountBackButton, labels.back);
        setText(elements.settingsBackButton, labels.back);
        setText(elements.libraryBackButton, labels.back);
        setText(document.querySelector("#playlistScreen .playlist-header .eyebrow"), labels.playlists);
        setText(document.querySelector("#accountScreen .playlist-header .eyebrow"), labels.account);
        setText(document.querySelector("#playlistScreen h1"), labels.playlistTitle);
        setText(document.querySelector("#accountScreen h1"), labels.accountTitle);
        setText(document.querySelector("#settingsScreen h1"), labels.settings);
        setText(document.querySelector("#settingsSaveButton"), labels.saveSettings);
        setText(elements.accountDashboardButton, labels.openDashboard);
        setText(elements.historyBackButton, labels.back);
        setText(elements.historyHeading, labels.historyFavorites);
        setText(elements.recentHeading, labels.recentlyWatched);
        setText(elements.favoriteHeading, labels.favorites);
        updatePlaylistLanguage(labels);
        setText(document.querySelector(".latest-header h2"), labels.latest);
        setText(elements.libraryHeading, labels.categories);
        if (elements.categoryHeading && normalizeSearchText(elements.categoryHeading.textContent) === normalizeSearchText(labels.all === "All" ? "\u0627\u0644\u0643\u0644" : "All")) {
            elements.categoryHeading.textContent = labels.all;
        }
        setText(document.querySelector(".library-search span"), labels.search);
        if (elements.librarySearchInput) {
            elements.librarySearchInput.placeholder = labels.search;
        }
        setText(elements.detailPlayButton, labels.play);
        setText(elements.detailTrailerButton, labels.trailer);
        setText(elements.detailWatchlistButton, labels.removeWatchlist);
        setText(document.querySelector(".detail-story h2"), labels.story);
        setText(document.querySelector(".season-section h2"), labels.seasons);
        setText(document.querySelector(".cast-section h2"), labels.cast);
        if (elements.actorWorksSection && elements.actorWorksSection.hidden) {
            setText(elements.actorWorksTitle, labels.actorWorks);
        }
        setSelectLabel(elements.audioLanguageSelect, labels.audio);
        setSelectLabel(elements.subtitleSelect, labels.subtitle);
        setSelectLabel(elements.subtitleStyleSelect, labels.subtitleStyle);
        setSelectLabel(elements.screenModeSelect, labels.screen);
        updateSettingsLanguage(labels);
        populateAudioTracks();
        populateSubtitleTracks();
    }

    function setText(element, text) {
        if (element) {
            element.textContent = text;
        }
    }

    function setSelectLabel(selectElement, text) {
        var label = selectElement && selectElement.closest("label");
        if (!label || !label.firstChild) {
            return;
        }
        label.firstChild.nodeValue = text;
    }

    function updateSettingsLanguage(labels) {
        var nav = elements.settingsForm ? elements.settingsForm.querySelectorAll(".settings-nav-button") : [];
        var panels = elements.settingsForm ? elements.settingsForm.querySelectorAll(".settings-panel") : [];
        if (nav[0]) { nav[0].textContent = labels.settingsLanguages; }
        if (nav[1]) { nav[1].textContent = labels.settingsPlayer; }
        if (nav[2]) { nav[2].textContent = labels.settingsSubtitles; }
        if (panels[0]) { setText(panels[0].querySelector("h2"), labels.settingsLanguages); }
        if (panels[1]) { setText(panels[1].querySelector("h2"), labels.settingsPlayer); }
        if (panels[2]) {
            setText(panels[2].querySelector("h2"), labels.settingsSubtitles);
            setText(panels[2].querySelector(".settings-note"), labels.subtitleHelp);
        }
    }

    function updatePlaylistLanguage(labels) {
        var formLabels = elements.playlistForm ? elements.playlistForm.querySelectorAll("label > span") : [];
        setText(formLabels[0], labels.playlistTypeLabel);
        setText(formLabels[1], "M3U URL");
        setText(formLabels[2], "Server URL");
        setText(formLabels[3], "Username");
        setText(formLabels[4], "Password");
        setText(elements.playlistSaveButton, labels.saveOnSite);
        setText(elements.playlistDeleteButton, labels.deleteFromSite);
    }

    function saveSettings(event) {
        event.preventDefault();
        state.language = elements.appLanguageSelect.value;
        state.playerMode = elements.playerModeSelect.value;

        try {
            window.localStorage.setItem("tizenPlayerLanguage", state.language);
            window.localStorage.setItem("tizenPlayerMode", state.playerMode);
        } catch (error) {
            console.log("Could not save settings:", error.message);
        }

        player.setPlaybackMode(state.playerMode);
        applyLanguage();
        syncSettingsButtons();
        elements.settingsMessage.hidden = false;
        elements.settingsMessage.textContent = getLanguageLabels(state.language).saved;
        renderHome();
    }

    function bindDomEvents() {
        document.addEventListener("keydown", handleKeyDown);

        elements.sectionList.addEventListener("click", function (event) {
            var card = event.target.closest(".section-card");
            if (card) {
                openLibrary(card.getAttribute("data-section"));
            }
        });

        elements.homeActionList.addEventListener("click", function (event) {
            var action = event.target.closest(".home-action-button");
            if (action) {
                runHomeAction(action.getAttribute("data-action"));
            }
        });

        elements.latestList.addEventListener("click", function (event) {
            var item = event.target.closest(".latest-card");
            if (item) {
                openLatestItem(parseInt(item.getAttribute("data-index"), 10));
            }
        });

        elements.powerButton.addEventListener("click", exitApp);
        elements.playlistBackButton.addEventListener("click", goHome);
        elements.playlistType.addEventListener("change", renderPlaylistTypeFields);
        elements.playlistForm.addEventListener("submit", savePlaylistFromForm);
        elements.playlistDeleteButton.addEventListener("click", deletePlaylistFromServer);
        elements.accountBackButton.addEventListener("click", goHome);
        elements.accountPayButton.addEventListener("click", openAccountAction);
        elements.accountDashboardButton.addEventListener("click", openCustomerDashboard);
        elements.historyBackButton.addEventListener("click", goHome);
        elements.recentList.addEventListener("click", handleHistoryItemClick);
        elements.favoriteList.addEventListener("click", handleHistoryItemClick);
        elements.settingsBackButton.addEventListener("click", goHome);
        elements.settingsForm.addEventListener("submit", saveSettings);
        elements.settingsForm.addEventListener("click", handleSettingsClick);
        elements.audioLanguageSelect.addEventListener("change", applyAudioTrackSelection);
        elements.subtitleSelect.addEventListener("change", applySubtitleSelection);
        elements.subtitleStyleSelect.addEventListener("change", applySubtitleStyle);
        elements.screenModeSelect.addEventListener("change", applyScreenMode);
        elements.playerToolButtons.addEventListener("click", function (event) {
            var button = event.target.closest(".player-tool-button");
            if (button) {
                openPlayerTool(button.getAttribute("data-tool"));
            }
        });
        elements.playerToolOptions.addEventListener("click", function (event) {
            var option = event.target.closest(".player-tool-option");
            if (option) {
                applyPlayerToolOption(option);
            }
        });

        elements.categoryList.addEventListener("click", function (event) {
            var category = event.target.closest(".category-row");
            if (category) {
                setCategory(parseInt(category.getAttribute("data-index"), 10));
                setLibraryPane("items");
            }
        });

        elements.librarySearchInput.addEventListener("input", function () {
            state.searchQuery = elements.librarySearchInput.value;
            state.categoryIndex = 0;
            state.itemIndex = 0;
            renderLibrary();
        });

        elements.librarySearchInput.addEventListener("focus", function () {
            state.libraryPane = "search";
            syncLibraryFocus();
        });

        elements.itemList.addEventListener("click", function (event) {
            var item = event.target.closest(".item-row, .poster-card");
            if (item) {
                openItem(parseInt(item.getAttribute("data-index"), 10));
            }
        });

        elements.libraryBackButton.addEventListener("click", goHome);
        elements.downloadSelectedButton.addEventListener("click", downloadSelectedItem);
        elements.downloadCategoryButton.addEventListener("click", exportVisibleItemsAsM3u);
        elements.detailBackButton.addEventListener("click", goBackFromDetail);
        elements.detailPlayButton.addEventListener("click", playDetailItem);
        elements.detailTrailerButton.addEventListener("click", playTrailer);
        elements.detailFavoriteButton.addEventListener("click", addDetailFavorite);
        elements.detailWatchlistButton.addEventListener("click", removeFromWatchlist);
        elements.seasonList.addEventListener("click", function (event) {
            var season = event.target.closest(".season-button");
            if (season) {
                setDetailSeason(parseInt(season.getAttribute("data-index"), 10));
            }
        });
        elements.episodeList.addEventListener("click", function (event) {
            var episode = event.target.closest(".episode-card");
            if (episode) {
                playEpisode(parseInt(episode.getAttribute("data-index"), 10));
            }
        });
        elements.castList.addEventListener("click", function (event) {
            var cast = event.target.closest(".cast-card");
            if (cast) {
                openActorWorks(parseInt(cast.getAttribute("data-index"), 10));
            }
        });
        elements.actorWorksList.addEventListener("click", function (event) {
            var work = event.target.closest(".actor-work-card");
            if (work) {
                openActorWork(parseInt(work.getAttribute("data-index"), 10));
            }
        });
        elements.playPauseButton.addEventListener("click", function () {
            player.togglePlay();
            showOverlay();
        });
        elements.backButton.addEventListener("click", goBackFromPlayer);
        elements.video.addEventListener("timeupdate", savePlaybackProgress);
        elements.video.addEventListener("pause", savePlaybackProgress);
        elements.resumeContinueButton.addEventListener("click", function () {
            runPendingResume(true);
        });
        elements.resumeRestartButton.addEventListener("click", function () {
            runPendingResume(false);
        });
    }

    function renderHome() {
        elements.sectionList.innerHTML = "";

        SECTIONS.forEach(function (section, index) {
            var count = getItemsBySection(section.id).length;
            var card = document.createElement("button");
            card.className = "section-card";
            card.type = "button";
            card.setAttribute("data-section", section.id);
            card.setAttribute("data-index", index);
            card.innerHTML =
                '<span class="section-icon">' + getSectionIconHtml(section.id) + '</span>' +
                '<span class="section-title">' + escapeHtml(section.title) + '</span>' +
                '<span class="section-subtitle">' + escapeHtml(section.subtitle) + '</span>' +
                '<span class="section-count">' + count + '</span>';
            elements.sectionList.appendChild(card);
        });

        renderLatestItems();
        renderActivationSummary();
        syncHomeFocus();
    }

    function getSectionIconHtml(sectionId) {
        if (sectionId === "live") {
            return '<svg viewBox="0 0 96 96" aria-hidden="true" focusable="false">' +
                '<path d="M30 18 43 32M66 18 53 32" />' +
                '<rect x="16" y="32" width="64" height="42" rx="8" />' +
                '<path d="M36 80h24M48 74v6" />' +
                '<path d="M28 44h40v18H28z" />' +
            '</svg>';
        }

        if (sectionId === "movies") {
            return '<svg viewBox="0 0 96 96" aria-hidden="true" focusable="false">' +
                '<rect x="18" y="34" width="56" height="34" rx="7" />' +
                '<path d="M74 45 86 38v26l-12-7" />' +
                '<circle cx="31" cy="26" r="9" />' +
                '<circle cx="53" cy="24" r="11" />' +
                '<path d="M30 51h24" />' +
            '</svg>';
        }

        return '<svg viewBox="0 0 96 96" aria-hidden="true" focusable="false">' +
            '<g transform="rotate(-7 48 48)">' +
                '<path d="M12 28h72a7 7 0 0 1 7 7v26a7 7 0 0 1-7 7H12a7 7 0 0 1-7-7V35a7 7 0 0 1 7-7z" />' +
                '<path d="M25 31v34M48 31v34M71 31v34" />' +
                '<path d="M31 39h12v18H31zM54 39h12v18H54z" />' +
                '<path d="M12 36h6M12 48h6M12 60h6M78 36h6M78 48h6M78 60h6" />' +
                '<path d="M27 34h14M50 34h14M27 62h14M50 62h14" />' +
            '</g>' +
        '</svg>';
    }

    function renderActivationSummary() {
        var data = state.activation || {};
        var access = data.access || {};
        var status = access.reason || "offline";
        var planEnd = data.active_until || data.trial_ends_at || "";

        if (!elements.activationSummary) {
            return;
        }

        elements.activationSummary.innerHTML =
            '<span><strong>MAC</strong> ' + escapeHtml(state.deviceMac || data.mac || "غير متوفر") + '</span>' +
            '<span><strong>الحالة</strong> ' + escapeHtml(status) + '</span>' +
            '<span><strong>البداية</strong> ' + escapeHtml(formatServerDate(data.trial_started_at || "")) + '</span>' +
            '<span><strong>النهاية</strong> ' + escapeHtml(formatServerDate(planEnd || "")) + '</span>';
    }

    function renderActivationSummary() {
        var data = state.activation || {};
        var access = data.access || {};
        var status = access.reason || "offline";
        var planEnd = data.active_until || data.trial_ends_at || "";
        var labels = getLanguageLabels(state.language);

        if (!elements.activationSummary) {
            return;
        }

        elements.activationSummary.innerHTML =
            '<span><strong>MAC</strong> ' + escapeHtml(state.deviceMac || data.mac || labels.unavailable) + '</span>' +
            '<span><strong>' + escapeHtml(labels.status) + '</strong> ' + escapeHtml(formatStatusLabel(status)) + '</span>' +
            '<span><strong>' + escapeHtml(labels.start) + '</strong> ' + escapeHtml(formatOptionalDate(data.trial_started_at, labels)) + '</span>' +
            '<span><strong>' + escapeHtml(labels.end) + '</strong> ' + escapeHtml(formatOptionalDate(planEnd, labels)) + '</span>';
    }

    function formatStatusLabel(status) {
        var value = String(status || "offline").toLowerCase();

        if (state.language !== "ar") {
            return status || "offline";
        }

        if (value === "trial") {
            return "\u062a\u062c\u0631\u064a\u0628\u064a";
        }
        if (value === "active" || value === "allowed") {
            return "\u0645\u0641\u0639\u0644";
        }
        if (value === "expired") {
            return "\u0645\u0646\u062a\u0647\u064a";
        }
        if (value === "offline") {
            return "\u063a\u064a\u0631 \u0645\u062a\u0635\u0644";
        }

        return status;
    }

    function formatOptionalDate(value, labels) {
        if (!value) {
            return labels.unknown;
        }

        return formatServerDate(value);
    }

    function renderLatestItems() {
        latestItems = getLatestVodItems(18);

        elements.latestCount.textContent = latestItems.length + " " + getLanguageLabels(state.language).items;
        elements.latestList.innerHTML = latestItems.map(function (item, index) {
            return '<button class="latest-card home-control" type="button" data-index="' + index + '">' +
                '<span class="latest-poster" style="' + getPosterStyle(item) + '">' + getPosterFallbackHtml(item) + '</span>' +
                '<span class="latest-title">' + escapeHtml(item.title) + '</span>' +
                '<span class="latest-meta">' + escapeHtml(getSection(item.section).subtitle) + '</span>' +
            '</button>';
        }).join("");

        if (state.latestIndex >= latestItems.length) {
            state.latestIndex = 0;
        }
    }

    function getLatestVodItems(limit) {
        var map = {};
        var latest = [];
        var vodItems = allItems.filter(function (item) {
            return item.section === "movies" || item.section === "series";
        });

        vodItems.forEach(function (item, index) {
            var key = getLibraryWorkKey(item);
            var current = map[key];

            item.libraryOrder = typeof item.libraryOrder === "number" ? item.libraryOrder : index;
            if (!current || isBetterLatestRepresentative(item, current)) {
                map[key] = item;
            }
        });

        latest = Object.keys(map).map(function (key) {
            return map[key];
        }).sort(function (a, b) {
            return getItemSortTime(b) - getItemSortTime(a);
        });

        return latest.slice(0, limit);
    }

    function isBetterLatestRepresentative(candidate, current) {
        if (candidate.section === "series" && current.section === "series") {
            if (!candidate.episodeNumber && current.episodeNumber) {
                return true;
            }

            if (candidate.episodeNumber && current.episodeNumber) {
                if ((candidate.seasonNumber || 1) !== (current.seasonNumber || 1)) {
                    return (candidate.seasonNumber || 1) < (current.seasonNumber || 1);
                }

                return candidate.episodeNumber < current.episodeNumber;
            }
        }

        return getItemSortTime(candidate) > getItemSortTime(current);
    }

    function getItemSortTime(item) {
        var value = parseInt(item.addedAt || item.libraryOrder || 0, 10);

        return isFinite(value) ? value : 0;
    }

    function updateHomeClock() {
        var now = new Date();
        var time = now.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
        var date = now.toLocaleDateString([], { month: "short", day: "numeric", year: "numeric" });

        elements.homeTime.textContent = time;
        elements.homeDate.textContent = date;
    }

    function renderLibrary() {
        var section = getSection(state.activeSection);
        var sectionItems = getLibraryItemsBySection(state.activeSection);

        sectionItems = filterItemsBySearch(sectionItems, state.searchQuery);
        state.categories = buildCategories(sectionItems);
        if (state.categoryIndex >= state.categories.length) {
            state.categoryIndex = 0;
        }

        elements.libraryEyebrow.textContent = section.subtitle;
        elements.libraryHeading.textContent = section.title;
        if (elements.librarySearchInput && elements.librarySearchInput.value !== state.searchQuery) {
            elements.librarySearchInput.value = state.searchQuery;
        }
        renderCategories();
        renderItems();
        setLibraryPane(state.libraryPane);
    }

    function renderCategories() {
        elements.categoryList.innerHTML = "";

        state.categories.forEach(function (category, index) {
            var row = document.createElement("button");
            row.className = "category-row";
            row.type = "button";
            row.setAttribute("data-index", index);
            row.innerHTML =
                '<span>' + escapeHtml(category.name) + '</span>' +
                '<span class="row-count">' + category.items.length + '</span>';
            elements.categoryList.appendChild(row);
        });
    }

    function renderItems() {
        var labels = getLanguageLabels(state.language);
        var category = state.categories[state.categoryIndex];
        visibleItems = category ? category.items : [];
        renderedItems = isVodSection() && VOD_RENDER_LIMIT > 0 ? visibleItems.slice(0, VOD_RENDER_LIMIT) : visibleItems;
        elements.itemList.innerHTML = "";
        elements.categoryHeading.textContent = category ? category.name : labels.all;
        elements.itemCount.textContent = renderedItems.length < visibleItems.length ?
            renderedItems.length + " / " + visibleItems.length + " " + labels.items :
            visibleItems.length + " " + labels.items;
        elements.itemList.classList.toggle("poster-grid", isVodSection());
        elements.downloadSelectedButton.hidden = isVodSection();
        elements.downloadCategoryButton.hidden = isVodSection();

        if (state.itemIndex >= renderedItems.length) {
            state.itemIndex = 0;
        }

        renderedItems.forEach(function (item, index) {
            var row = document.createElement("button");
            row.className = "item-row";
            row.type = "button";
            row.setAttribute("data-index", index);

            if (isVodSection()) {
                row.className = "poster-card";
                row.innerHTML =
                    '<span class="poster-art" style="' + getPosterStyle(item) + '">' + getPosterFallbackHtml(item) + '</span>' +
                    '<span class="poster-title">' + escapeHtml(item.title) + '</span>' +
                    '<span class="poster-meta">' + escapeHtml(item.group || getSection(item.section).subtitle) + '</span>';
            } else {
                row.innerHTML =
                    '<span class="channel-number">' + (index + 1) + '</span>' +
                    '<span class="channel-logo" style="' + getChannelLogoStyle(item) + '">' + getChannelFallbackHtml(item) + '</span>' +
                    '<span class="channel-name">' + escapeHtml(item.title) + '</span>' +
                    '<span class="channel-type">' + escapeHtml(item.type.toUpperCase()) + '</span>';
            }

            elements.itemList.appendChild(row);
        });

        setLibraryPane(state.libraryPane);
    }

    function renderItemsLegacy() {
        var category = state.categories[state.categoryIndex];
        visibleItems = category ? category.items : [];
        renderedItems = isVodSection() && VOD_RENDER_LIMIT > 0 ? visibleItems.slice(0, VOD_RENDER_LIMIT) : visibleItems;
        elements.itemList.innerHTML = "";
        elements.categoryHeading.textContent = category ? category.name : "All";
        elements.itemCount.textContent = renderedItems.length < visibleItems.length ?
            renderedItems.length + " / " + visibleItems.length + " items" :
            visibleItems.length + " items";
        elements.itemList.classList.toggle("poster-grid", isVodSection());
        elements.downloadSelectedButton.hidden = isVodSection();
        elements.downloadCategoryButton.hidden = isVodSection();

        if (state.itemIndex >= renderedItems.length) {
            state.itemIndex = 0;
        }

        renderedItems.forEach(function (item, index) {
            var row = document.createElement("button");
            row.className = "item-row";
            row.type = "button";
            row.setAttribute("data-index", index);

            if (isVodSection()) {
                row.className = "poster-card";
                row.innerHTML =
                    '<span class="poster-art" style="' + getPosterStyle(item) + '">' +
                        getPosterFallbackHtml(item) +
                    '</span>' +
                    '<span class="poster-title">' + escapeHtml(item.title) + '</span>' +
                    '<span class="poster-meta">' + escapeHtml(item.group || getSection(item.section).subtitle) + '</span>';
            } else {
                row.innerHTML =
                    '<span class="item-number">' + (index + 1) + '</span>' +
                    '<span class="item-main">' +
                        '<span class="item-title">' + escapeHtml(item.title) + '</span>' +
                        '<span class="item-meta">' + escapeHtml(item.group || item.description || item.type.toUpperCase()) + '</span>' +
                    '</span>' +
                    '<span class="item-type">' + escapeHtml(item.type.toUpperCase()) + '</span>';
            }
            elements.itemList.appendChild(row);
        });

        syncLibraryFocus();
    }

    function initializeActivation() {
        setPlaylistLoading(true, "جاري التحقق من التفعيل وجلب بيانات الجهاز...");
        getDeviceMac(function (mac) {
            state.deviceMac = mac;

            if (!APP_SERVER_URL) {
                elements.playlistStatus.textContent = "MAC: " + mac + " - local playlist mode";
                renderActivationSummary();
                loadIptvPlaylist();
                return;
            }

            requestJsonWithTimeout(normalizeServerUrl(APP_SERVER_URL) + "/api/device.php?mac=" + encodeURIComponent(mac), 18000, function (error, data) {
                if (error || !data || !data.ok) {
                    console.log("Activation server unavailable:", error ? error.message : data);
                    setPlaylistLoading(false);
                    elements.playlistStatus.textContent = "Activation server unavailable";
                    renderActivationSummary();
                    return;
                }

                state.activation = data;
                renderActivationSummary();

                if (!data.access || !data.access.allowed) {
                    setPlaylistLoading(false);
                    elements.playlistStatus.textContent = "Trial expired. Open My Account to activate with Stripe. MAC: " + mac;
                    return;
                }

                if (data.playlist && data.playlist.generated_url) {
                    IPTV_PLAYLIST_URL = data.playlist.generated_url;
                } else {
                    IPTV_PLAYLIST_URL = "";
                }

                elements.playlistStatus.textContent = getActivationStatusText(data);
                loadIptvPlaylist();
            });
        });
    }

    function getDeviceMac(callback) {
        var fallback = getStoredBrowserDeviceId();

        try {
            if (window.webapis && window.webapis.network && typeof window.webapis.network.getMac === "function") {
                callback(normalizeMac(window.webapis.network.getMac()));
                return;
            }
        } catch (error) {
            console.log("Samsung MAC lookup failed:", error.message);
        }

        callback(fallback);
    }

    function getStoredBrowserDeviceId() {
        var key = "tizenPlayerDeviceId";
        var value;

        try {
            value = window.localStorage.getItem(key);
            if (!value) {
                value = "WEB-" + Math.random().toString(16).slice(2, 8).toUpperCase() + "-" + Math.random().toString(16).slice(2, 8).toUpperCase();
                window.localStorage.setItem(key, value);
            }
            return value;
        } catch (error) {
            return "WEB-DEMO";
        }
    }

    function normalizeMac(mac) {
        return String(mac || "").trim().toUpperCase();
    }

    function normalizeServerUrl(url) {
        return String(url || "").replace(/\/+$/, "");
    }

    function getActivationStatusText(data) {
        if (data.access && data.access.reason === "paid") {
            return "Activated until " + formatServerDate(data.active_until) + " - MAC: " + data.mac;
        }

        return "Trial active until " + formatServerDate(data.trial_ends_at) + " - MAC: " + data.mac;
    }

    function formatServerDate(value) {
        if (!value) {
            return "unknown";
        }

        return String(value).replace("T", " ").replace(/\+.*/, "");
    }

    function resetLibraryForReload() {
        allItems = [];
        visibleItems = [];
        renderedItems = [];
        latestItems = [];
        detailMetadataCache = {};
        imdbResolveCache = {};
        detailSeasons = [];
        detailEpisodes = [];
        actorWorks = [];
        renderHome();
    }

    function loadIptvPlaylist() {
        if (!IPTV_PLAYLIST_URL) {
            elements.playlistStatus.textContent = "Add playlist from List Playlists";
            hideStartupSplash();
            return;
        }

        setPlaylistLoading(true, "Loading channels, movies and series...");
        requestTextWithRetry(IPTV_PLAYLIST_URL, 2, function (error, playlistText) {
            var iptvItems;

            if (error) {
                setPlaylistLoading(false);
                elements.playlistStatus.textContent = "Playlist failed";
                console.log("Could not load IPTV playlist:", error.message);
                return;
            }

            iptvItems = limitItemsPerSection(parseM3uPlaylist(playlistText), IPTV_MAX_ITEMS_PER_SECTION);

            if (!iptvItems.length) {
                setPlaylistLoading(false);
                elements.playlistStatus.textContent = "No IPTV items found";
                return;
            }

            allItems = iptvItems;
            renderHome();
            if (getXtreamConfig()) {
                loadXtreamLibrary();
            } else {
                setPlaylistLoading(false);
                elements.playlistStatus.textContent = iptvItems.length + " IPTV items loaded";
            }

            if (state.screen === "library") {
                renderLibrary();
            }
        });
    }

    function setPlaylistLoading(isLoading, message) {
        if (isLoading && state.splashHideTimer) {
            clearTimeout(state.splashHideTimer);
            state.splashHideTimer = null;
        }

        elements.playlistStatus.classList.toggle("is-loading", !!isLoading && !state.splashVisible);
        if (message) {
            if (state.splashVisible && isLoading) {
                elements.splashStatus.textContent = "Loading ...";
                elements.playlistStatus.textContent = "";
            } else {
                elements.playlistStatus.textContent = message;
            }
        }

        if (!isLoading && state.splashVisible) {
            hideStartupSplash();
        }
    }

    function hideStartupSplash() {
        var elapsed;
        var delay;

        if (!state.splashVisible || state.splashHideTimer) {
            return;
        }

        elapsed = Date.now() - state.splashStartedAt;
        delay = Math.max(0, 1600 - elapsed);
        if (delay > 0) {
            state.splashHideTimer = setTimeout(hideStartupSplashNow, delay);
            return;
        }

        hideStartupSplashNow();
    }

    function hideStartupSplashNow() {
        state.splashVisible = false;
        state.splashHideTimer = null;
        if (elements.splashScreen) {
            elements.splashScreen.classList.add("is-hidden");
        }
    }

    function requestTextWithRetry(url, retries, callback) {
        requestText(url, function (error, text) {
            if (!error) {
                callback(null, text);
                return;
            }

            if (retries > 0) {
                console.log("Retrying playlist request:", error.message);
                setTimeout(function () {
                    requestTextWithRetry(url, retries - 1, callback);
                }, 1200);
                return;
            }

            callback(error);
        });
    }

    function loadXtreamLibrary() {
        var config = getXtreamConfig();
        var pending = 4;
        var rawMovieItems = [];
        var rawSeriesItems = [];
        var movieCategories = {};
        var seriesCategories = {};

        if (!config) {
            return;
        }

        setPlaylistLoading(true, "جاري تحديث مكتبة الأفلام والمسلسلات...");
        requestJsonWithTimeout(getXtreamActionUrl("get_vod_categories"), XTREAM_LIBRARY_TIMEOUT, function (error, data) {
            if (error) {
                console.log("Could not load Xtream movie categories:", error.message);
            } else {
                movieCategories = normalizeXtreamCategoryMap(data || []);
            }
            finish();
        });

        requestJsonWithTimeout(getXtreamActionUrl("get_series_categories"), XTREAM_LIBRARY_TIMEOUT, function (error, data) {
            if (error) {
                console.log("Could not load Xtream series categories:", error.message);
            } else {
                seriesCategories = normalizeXtreamCategoryMap(data || []);
            }
            finish();
        });

        requestJsonWithTimeout(getXtreamActionUrl("get_vod_streams"), XTREAM_LIBRARY_TIMEOUT, function (error, data) {
            if (error) {
                console.log("Could not load Xtream movies library:", error.message);
            } else {
                rawMovieItems = data || [];
            }
            finish();
        });

        requestJsonWithTimeout(getXtreamActionUrl("get_series"), XTREAM_LIBRARY_TIMEOUT, function (error, data) {
            if (error) {
                console.log("Could not load Xtream series library:", error.message);
            } else {
                rawSeriesItems = data || [];
            }
            finish();
        });

        function finish() {
            pending -= 1;
            if (pending > 0) {
                return;
            }

            mergeXtreamLibrary(
                normalizeXtreamMovieList(rawMovieItems, config, movieCategories).concat(
                    normalizeXtreamSeriesList(rawSeriesItems, seriesCategories)
                )
            );
            setPlaylistLoading(false);
            elements.playlistStatus.textContent = getItemsBySection("live").length + " Live / " +
                getItemsBySection("movies").length + " Movies / " +
                getItemsBySection("series").length + " Series";
            renderHome();

            if (state.screen === "library") {
                renderLibrary();
            } else if (state.screen === "detail" && state.detailItem) {
                renderDetail(state.detailItem);
            }
        }
    }

    function getXtreamActionUrl(action) {
        var config = getXtreamConfig();

        if (!config) {
            return "";
        }

        return config.baseUrl + "/player_api.php?username=" + encodeURIComponent(config.username) +
            "&password=" + encodeURIComponent(config.password) +
            "&action=" + encodeURIComponent(action);
    }

    function requestJsonWithTimeout(url, timeout, callback) {
        requestTextWithTimeout(url, timeout, function (error, text) {
            if (error) {
                callback(error);
                return;
            }

            try {
                callback(null, JSON.parse(text));
            } catch (parseError) {
                callback(parseError);
            }
        });
    }

    function requestTextWithTimeout(url, timeout, callback) {
        var request = new XMLHttpRequest();

        request.open("GET", url, true);
        request.timeout = timeout;

        request.onreadystatechange = function () {
            if (request.readyState !== 4) {
                return;
            }

            if (request.status >= 200 && request.status < 300) {
                callback(null, request.responseText);
                return;
            }

            callback(new Error("HTTP " + request.status));
        };

        request.onerror = function () {
            callback(new Error("Network error"));
        };

        request.ontimeout = function () {
            callback(new Error("Request timed out"));
        };

        request.send();
    }

    function requestExternalText(url, callback) {
        var proxyUrl = getIptvProxyUrl(url);

        requestTextWithTimeout(url, 12000, function (error, text) {
            var readerUrl;

            if (!error) {
                callback(null, text);
                return;
            }

            if (proxyUrl) {
                requestTextWithTimeout(proxyUrl, 22000, function (proxyError, proxyText) {
                    if (!proxyError) {
                        callback(null, proxyText);
                        return;
                    }
                    requestExternalTextViaReader(url, callback);
                });
                return;
            }

            requestExternalTextViaReader(url, callback);
        });
    }

    function requestExternalTextViaReader(url, callback) {
        var readerUrl = "https://r.jina.ai/http://" + String(url || "").replace(/^https?:\/\//i, "");

        requestTextWithTimeout(readerUrl, 18000, function (proxyError, proxyText) {
            if (!proxyError) {
                callback(null, proxyText);
                return;
            }

            requestTextWithTimeout(String(url || "").replace(/^https:\/\//i, "http://"), 18000, callback);
        });
    }

    function normalizeXtreamCategoryMap(items) {
        var map = {};

        items.forEach(function (category) {
            var id = category.category_id || category.id;
            var name = category.category_name || category.name || category.title;

            if (id && name) {
                map[String(id)] = String(name);
            }
        });

        return map;
    }

    function normalizeXtreamContainerExtension(value) {
        var extension = String(value || "").trim().toLowerCase().replace(/^\./, "");

        if (!extension || extension === "undefined" || extension === "null") {
            return "mp4";
        }

        return extension;
    }

    function getMediaTypeFromExtension(extension) {
        var normalized = normalizeXtreamContainerExtension(extension);

        if (normalized === "m3u8") {
            return "hls";
        }

        if (normalized === "ts") {
            return "mpegts";
        }

        return normalized;
    }

    function normalizeXtreamMovieList(items, config, categoryMap) {
        return items.map(function (movie) {
            var extension = normalizeXtreamContainerExtension(movie.container_extension);
            var titleParts = parseTitleDetails(movie.name || movie.title || "Movie");
            var categoryId = movie.category_id || movie.categoryId || "";
            var group = categoryMap[String(categoryId)] || movie.category_name || movie.genre || movie.category || "Movies";

            return {
                title: titleParts.title,
                group: group,
                section: "movies",
                description: "",
                logo: movie.stream_icon || "",
                year: titleParts.year,
                quality: titleParts.quality,
                duration: "",
                addedAt: movie.added || "",
                rating: movie.rating || movie.rating_5based || "",
                plot: "",
                cast: [],
                xtreamId: movie.stream_id,
                containerExtension: extension,
                type: getMediaTypeFromExtension(extension),
                url: config.baseUrl + "/movie/" + encodeURIComponent(config.username) + "/" + encodeURIComponent(config.password) + "/" + movie.stream_id + "." + extension
            };
        }).filter(function (item) {
            return item.xtreamId;
        });
    }

    function normalizeXtreamSeriesList(items, categoryMap) {
        return items.map(function (series) {
            var titleParts = parseTitleDetails(series.name || "Series");
            var castPeople = parseCastPeople(series.cast || "");
            var backdrop = Array.isArray(series.backdrop_path) ? series.backdrop_path[0] : series.backdrop_path;
            var categoryId = series.category_id || series.categoryId || "";
            var group = categoryMap[String(categoryId)] || series.category_name || series.genre || "Series";

            return {
                title: titleParts.title,
                group: group,
                section: "series",
                description: series.plot || "",
                logo: series.cover || "",
                backdrop: backdrop || "",
                year: series.releaseDate ? String(series.releaseDate).slice(0, 4) : titleParts.year,
                quality: titleParts.quality,
                duration: series.episode_run_time || "",
                addedAt: series.last_modified || "",
                rating: series.rating || series.rating_5based || "",
                plot: series.plot || "",
                cast: castPeople.map(function (person) {
                    return person.name;
                }),
                castPeople: castPeople,
                seriesTitle: titleParts.title,
                xtreamId: series.series_id,
                type: "mp4",
                url: ""
            };
        }).filter(function (item) {
            return item.xtreamId;
        });
    }

    function mergeXtreamLibrary(items) {
        var existing = {};
        var liveItems = [];
        var richSections = {};

        items.forEach(function (item) {
            richSections[item.section] = true;
        });

        allItems.forEach(function (item) {
            if (item.section === "live") {
                liveItems.push(item);
                return;
            }

            if (richSections[item.section]) {
                return;
            }

            existing[getXtreamMergeKey(item)] = item;
        });

        items.forEach(function (item) {
            existing[getXtreamMergeKey(item)] = mergeLibraryItem(existing[getXtreamMergeKey(item)], item);
        });

        allItems = liveItems.concat(Object.keys(existing).map(function (key) {
            return existing[key];
        }));
    }

    function getXtreamMergeKey(item) {
        if (item.xtreamId) {
            return item.section + ":id:" + item.xtreamId;
        }

        return getLibraryWorkKey(item);
    }

    function mergeLibraryItem(oldItem, newItem) {
        if (!oldItem) {
            return newItem;
        }

        Object.keys(newItem).forEach(function (key) {
            if (newItem[key] !== "" && newItem[key] !== null && typeof newItem[key] !== "undefined") {
                oldItem[key] = newItem[key];
            }
        });

        return oldItem;
    }

    function limitItemsPerSection(items, maxPerSection) {
        if (!maxPerSection || maxPerSection <= 0) {
            return items;
        }

        var counts = {
            live: 0,
            movies: 0,
            series: 0
        };

        return items.filter(function (item) {
            if (counts[item.section] >= maxPerSection) {
                return false;
            }

            counts[item.section] += 1;
            return true;
        });
    }

    function requestText(url, callback) {
        var request = new XMLHttpRequest();

        request.open("GET", url, true);
        request.timeout = 20000;

        request.onreadystatechange = function () {
            if (request.readyState !== 4) {
                return;
            }

            if (request.status >= 200 && request.status < 300) {
                callback(null, request.responseText);
                return;
            }

            callback(new Error("HTTP " + request.status));
        };

        request.onerror = function () {
            callback(new Error("Network error"));
        };

        request.ontimeout = function () {
            callback(new Error("Request timed out"));
        };

        request.send();
    }

    function postJson(url, payload, callback) {
        var request = new XMLHttpRequest();

        request.open("POST", url, true);
        request.timeout = 20000;
        request.setRequestHeader("Content-Type", "application/json");

        request.onreadystatechange = function () {
            if (request.readyState !== 4) {
                return;
            }

            if (request.status >= 200 && request.status < 300) {
                try {
                    callback(null, JSON.parse(request.responseText));
                } catch (error) {
                    callback(error);
                }
                return;
            }

            callback(new Error("HTTP " + request.status));
        };

        request.onerror = function () {
            callback(new Error("Network error"));
        };

        request.ontimeout = function () {
            callback(new Error("Request timed out"));
        };

        request.send(JSON.stringify(payload || {}));
    }

    function parseM3uPlaylist(text) {
        var lines = text.split(/\r?\n/);
        var items = [];
        var currentInfo = null;

        lines.forEach(function (line) {
            var trimmed = line.trim();

            if (!trimmed) {
                return;
            }

            if (trimmed.indexOf("#EXTINF") === 0) {
                currentInfo = parseExtInf(trimmed);
                return;
            }

            if (trimmed.charAt(0) === "#") {
                return;
            }

            if (currentInfo) {
                trimmed = normalizeIptvStreamUrl(trimmed);
                items.push({
                    title: currentInfo.title || "IPTV",
                    group: currentInfo.group || "Other",
                    section: detectSection(trimmed, currentInfo),
                    description: currentInfo.group ? "IPTV - " + currentInfo.group : "IPTV",
                    logo: currentInfo.logo,
                    year: currentInfo.year,
                    quality: currentInfo.quality,
                    duration: currentInfo.duration,
                    addedAt: currentInfo.addedAt,
                    rating: currentInfo.rating,
                    plot: currentInfo.plot,
                    cast: currentInfo.cast,
                    seriesTitle: currentInfo.seriesTitle,
                    seasonNumber: currentInfo.seasonNumber,
                    episodeNumber: currentInfo.episodeNumber,
                    xtreamId: getXtreamStreamId(trimmed),
                    type: getStreamType(trimmed),
                    url: trimmed
                });
                currentInfo = null;
            }
        });

        return items;
    }

    function parseExtInf(line) {
        var commaIndex = line.lastIndexOf(",");
        var title = commaIndex !== -1 ? line.slice(commaIndex + 1).trim() : "IPTV";
        var tvgName = getAttributeValue(line, "tvg-name");
        var group = getAttributeValue(line, "group-title");
        var logo = getAttributeValue(line, "tvg-logo");
        var titleParts = parseTitleDetails(tvgName || title);
        var seriesInfo = parseSeriesEpisodeInfo(titleParts.title);

        return {
            title: seriesInfo.title,
            group: group,
            logo: logo,
            year: titleParts.year,
            quality: titleParts.quality,
            duration: titleParts.duration,
            addedAt: getAttributeValue(line, "added") || getAttributeValue(line, "added-at") || getAttributeValue(line, "date") || "",
            rating: getAttributeValue(line, "rating") || "",
            plot: getAttributeValue(line, "plot") || getAttributeValue(line, "description") || "",
            cast: parseCast(getAttributeValue(line, "cast") || getAttributeValue(line, "actors")),
            seriesTitle: seriesInfo.seriesTitle,
            seasonNumber: seriesInfo.seasonNumber,
            episodeNumber: seriesInfo.episodeNumber
        };
    }

    function getAttributeValue(text, attributeName) {
        var pattern = new RegExp(attributeName + '="([^"]*)"', "i");
        var match = text.match(pattern);

        return match ? match[1] : "";
    }

    function parseTitleDetails(title) {
        var cleanTitle = String(title || "IPTV").replace(/\s+/g, " ").trim();
        var yearMatch = cleanTitle.match(/\b(19|20)\d{2}\b/);
        var durationMatch = cleanTitle.match(/\b(\d{1,2}:\d{2}(?::\d{2})?)\b/);
        var qualityMatch = cleanTitle.match(/\b(4K|UHD|FHD|HD|SD|WEB-DL|BLURAY|CAM|MP4|HEVC)\b/i);

        cleanTitle = cleanTitle
            .replace(/\b(19|20)\d{2}\b/g, "")
            .replace(/\b(\d{1,2}:\d{2}(?::\d{2})?)\b/g, "")
            .replace(/\b(4K|UHD|FHD|HD|SD|WEB-DL|BLURAY|CAM|MP4|HEVC)\b/ig, "")
            .replace(/\(\s*\)/g, "")
            .replace(/\[\s*\]/g, "")
            .replace(/\s*[-|]\s*$/g, "")
            .replace(/\s+/g, " ")
            .trim();

        return {
            title: cleanTitle || title || "IPTV",
            year: yearMatch ? yearMatch[0] : "",
            duration: durationMatch ? durationMatch[0] : "",
            quality: qualityMatch ? qualityMatch[0].toUpperCase() : ""
        };
    }

    function parseCast(value) {
        return parseCastPeople(value).map(function (person) {
            return person.name;
        });
    }

    function parseCastPeople(value) {
        var people;

        if (!value) {
            return [];
        }

        if (Array.isArray(value)) {
            people = value.map(function (person) {
                if (typeof person === "string") {
                    return { name: person.trim(), image: "" };
                }

                var name = String(person.name || person.original_name || person.title || "").trim();
                return {
                    name: name,
                    image: normalizePersonImageUrl(person.profile_path || person.image || person.photo || person.avatar || ""),
                    tmdbPersonId: person.id || person.tmdb_id || person.person_id || ""
                };
            });
        } else {
            people = String(value).split(/[,|،]/).map(function (name) {
                var cleanName = name.trim();
                return { name: cleanName, image: "" };
            });
        }

        return people.filter(function (person) {
            return person.name;
        }).slice(0, 12);
    }

    function parseSeriesEpisodeInfoLegacy(title) {
        var text = String(title || "IPTV").replace(/\s+/g, " ").trim();
        var match = text.match(/\bS(?:eason)?\s*(\d{1,2})\s*E(?:p(?:isode)?)?\s*(\d{1,3})\b/i) ||
            text.match(/\bS\s*(\d{1,2})\s*(?:Ep|Episode|E)\s*(\d{1,3})\b/i) ||
            text.match(/\b(\d{1,2})x(\d{1,3})\b/i) ||
            text.match(/موسم\s*(\d{1,2}).*حلقة\s*(\d{1,3})/i);
        var cleanTitle = match ? text.replace(match[0], "") : text;

        cleanTitle = cleanTitle
            .replace(/\bS(?:eason)?\s*\d{1,2}\b/ig, "")
            .replace(/\bE(?:p(?:isode)?)?\s*\d{1,3}\b/ig, "")
            .replace(/\b(?:Ep|Episode)\s*\d{1,3}\b/ig, "")
            .replace(/\bS\d{1,2}\b/ig, "")
            .replace(/\(\s*\)/g, "")
            .replace(/\[\s*\]/g, "")
            .replace(/\s*[-|]\s*$/g, "")
            .replace(/\s+/g, " ")
            .trim();

        return {
            title: text,
            seriesTitle: cleanTitle || text,
            seasonNumber: match ? parseInt(match[1], 10) : 1,
            episodeNumber: match ? parseInt(match[2], 10) : 1
        };
    }

    function parseSeriesEpisodeInfo(title) {
        var text = String(title || "IPTV").replace(/\s+/g, " ").trim();
        var match = text.match(/\bS(?:eason)?\s*(\d{1,2})\s*E(?:p(?:isode)?)?\s*(\d{1,3})\b/i) ||
            text.match(/\bS\s*(\d{1,2})\s*[._-]\s*E\s*(\d{1,3})\b/i) ||
            text.match(/\bS(\d{1,2})E(\d{1,3})\b/i) ||
            text.match(/\bS\s*(\d{1,2})\s*(?:Ep|Episode|E)\s*(\d{1,3})\b/i) ||
            text.match(/\b(\d{1,2})x(\d{1,3})\b/i) ||
            text.match(/(?:season|saison|temporada|sezon)\s*(\d{1,2}).*(?:episode|ep|episodio|bolum)\s*(\d{1,3})/i) ||
            text.match(/Ù…ÙˆØ³Ù…\s*(\d{1,2}).*Ø­Ù„Ù‚Ø©\s*(\d{1,3})/i);
        var cleanTitle = match ? text.replace(match[0], "") : text;

        cleanTitle = cleanTitle
            .replace(/\bS\s*\d{1,2}\s*[._-]\s*E\s*\d{1,3}\b/ig, "")
            .replace(/\bS\d{1,2}E\d{1,3}\b/ig, "")
            .replace(/\bS(?:eason)?\s*\d{1,2}\b/ig, "")
            .replace(/\bE(?:p(?:isode)?)?\s*\d{1,3}\b/ig, "")
            .replace(/(?:\b(?:Ep|Episode|Episodio|Bolum)|.pisode)\s*\d{1,3}\b/ig, "")
            .replace(/\bS\d{1,2}\b/ig, "")
            .replace(/\(\s*\)/g, "")
            .replace(/\[\s*\]/g, "")
            .replace(/\s*[-|]\s*[-|]?\s*/g, " - ")
            .replace(/\s*[-|]\s*$/g, "")
            .replace(/^\s*[-|]\s*/g, "")
            .replace(/\s+/g, " ")
            .trim();

        return {
            title: text,
            seriesTitle: cleanTitle || text,
            seasonNumber: match ? parseInt(match[1], 10) : 1,
            episodeNumber: match ? parseInt(match[2], 10) : 1
        };
    }

    function normalizeIptvStreamUrl(url) {
        return String(url || "").trim();
    }

    function getXtreamInfoUrl(item) {
        var config = getXtreamConfigForItem(item);
        var action;

        if (!config || !item.xtreamId || (item.section !== "movies" && item.section !== "series")) {
            return "";
        }

        action = item.section === "series" ? "get_series_info&series_id=" : "get_vod_info&vod_id=";
        return config.baseUrl + "/player_api.php?username=" + encodeURIComponent(config.username) +
            "&password=" + encodeURIComponent(config.password) +
            "&action=" + action + encodeURIComponent(item.seriesXtreamId || item.xtreamId);
    }

    function getXtreamConfigForItem(item) {
        var config = getXtreamConfig();
        var match;

        if (item && item.url) {
            match = String(item.url).match(/^(https?:\/\/[^/]+)\/(?:movie|series|live)\/([^/]+)\/([^/]+)\//i);
            if (match) {
                return {
                    baseUrl: normalizeIptvBaseUrl(match[1]),
                    username: decodeURIComponent(match[2]),
                    password: decodeURIComponent(match[3])
                };
            }
        }

        return config;
    }

    function normalizeIptvBaseUrl(url) {
        var normalized = String(url || "").replace(/\/+$/, "");
        var parts = normalized.match(/^(https?:\/\/[^/:]+)(?::80)?$/i);

        if (parts) {
            return parts[1];
        }

        return normalized.replace(/:80(\/|$)/i, "$1");
    }

    function getXtreamConfig() {
        var match = IPTV_PLAYLIST_URL.match(/^(https?:\/\/[^/]+)\/get\.php\?([^#]+)/i);
        var params;
        var playlist;

        if (match) {
            params = parseQueryString(match[2]);
            if (params.username && params.password) {
                return {
                    baseUrl: normalizeIptvBaseUrl(match[1]),
                    username: params.username,
                    password: params.password
                };
            }
        }

        playlist = state.activation && state.activation.playlist;
        if (playlist && playlist.type === "xtream" && playlist.server_url && playlist.username && playlist.password) {
            return {
                baseUrl: normalizeIptvBaseUrl(playlist.server_url),
                username: playlist.username,
                password: playlist.password
            };
        }

        return null;
    }

    function parseQueryString(query) {
        var params = {};

        query.split("&").forEach(function (part) {
            var pair = part.split("=");
            var key = decodeURIComponent(pair[0] || "");
            var value = decodeURIComponent(pair.slice(1).join("=") || "");

            if (key) {
                params[key] = value;
            }
        });

        return params;
    }

    function getXtreamStreamId(url) {
        var cleanUrl = url.split("?")[0];
        var fileName = cleanUrl.slice(cleanUrl.lastIndexOf("/") + 1);
        var id = fileName.replace(/\.[^.]+$/, "");

        return /^\d+$/.test(id) ? id : "";
    }

    function detectSection(url, info) {
        var text = String(info.group || "").toLowerCase();
        var cleanUrl = url.split("?")[0].toLowerCase();

        if (cleanUrl.indexOf("/live/") !== -1) {
            return "live";
        }

        if (cleanUrl.indexOf("/series/") !== -1) {
            return "series";
        }

        if (cleanUrl.indexOf("/movie/") !== -1) {
            return "movies";
        }

        if (text.indexOf("/series/") !== -1 || text.indexOf("series") !== -1 || text.indexOf("مسلسل") !== -1) {
            return "series";
        }

        if (text.indexOf("/movie/") !== -1 || text.indexOf("movie") !== -1 || text.indexOf("film") !== -1 || text.indexOf("vod") !== -1 || text.indexOf("افلام") !== -1 || text.indexOf("أفلام") !== -1) {
            return "movies";
        }

        return "live";
    }

    function getStreamType(url) {
        var cleanUrl = url.split("?")[0].toLowerCase();

        if (cleanUrl.indexOf(".m3u8") !== -1) {
            return "hls";
        }

        if (cleanUrl.indexOf(".mp4") !== -1) {
            return "mp4";
        }

        if (/\.(mkv|avi|mov|m4v|webm)(?:$|\?)/i.test(cleanUrl)) {
            return cleanUrl.slice(cleanUrl.lastIndexOf(".") + 1).split("?")[0];
        }

        if (cleanUrl.indexOf(".ts") !== -1) {
            return "mpegts";
        }

        return "iptv";
    }

    function handleKeyDown(event) {
        var keyCode = event.keyCode || event.which;

        if (state.pendingResume) {
            handleResumeKeys(keyCode, event);
            return;
        }

        if (isTextEditingTarget(event.target) && !isNavigationKey(keyCode)) {
            return;
        }

        if (state.screen === "home") {
            handleHomeKeys(keyCode, event);
        } else if (state.screen === "playlist" || state.screen === "account" || state.screen === "settings" || state.screen === "history") {
            handleManagementKeys(keyCode, event);
        } else if (state.screen === "library") {
            handleLibraryKeys(keyCode, event);
        } else if (state.screen === "detail") {
            handleDetailKeys(keyCode, event);
        } else {
            handlePlayerKeys(keyCode, event);
        }
    }

    function handleResumeKeys(keyCode, event) {
        switch (keyCode) {
            case KEY_CODES.LEFT:
            case KEY_CODES.RIGHT:
                event.preventDefault();
                if (document.activeElement === elements.resumeContinueButton) {
                    elements.resumeRestartButton.focus();
                } else {
                    elements.resumeContinueButton.focus();
                }
                break;
            case KEY_CODES.ENTER:
                event.preventDefault();
                runPendingResume(document.activeElement !== elements.resumeRestartButton);
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                elements.resumeDialog.hidden = true;
                state.pendingResume = null;
                break;
        }
    }

    function isTextEditingTarget(target) {
        return target && (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.tagName === "SELECT");
    }

    function isNavigationKey(keyCode) {
        return keyCode === KEY_CODES.LEFT ||
            keyCode === KEY_CODES.RIGHT ||
            keyCode === KEY_CODES.UP ||
            keyCode === KEY_CODES.DOWN ||
            keyCode === KEY_CODES.ENTER ||
            keyCode === KEY_CODES.BACK ||
            keyCode === KEY_CODES.RETURN;
    }

    function handleHomeKeys(keyCode, event) {
        switch (keyCode) {
            case KEY_CODES.LEFT:
                event.preventDefault();
                moveHomeFocus(-1, 0);
                break;
            case KEY_CODES.RIGHT:
                event.preventDefault();
                moveHomeFocus(1, 0);
                break;
            case KEY_CODES.UP:
                event.preventDefault();
                moveHomeFocus(0, -1);
                break;
            case KEY_CODES.DOWN:
                event.preventDefault();
                moveHomeFocus(0, 1);
                break;
            case KEY_CODES.ENTER:
                event.preventDefault();
                activateHomeFocus();
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                exitApp();
                break;
        }
    }

    function handleLibraryKeys(keyCode, event) {
        switch (keyCode) {
            case KEY_CODES.LEFT:
                event.preventDefault();
                if (isVodSection()) {
                    moveVodLibraryHorizontal(-1);
                    break;
                }
                if (state.libraryPane === "downloads") {
                    setLibraryPane("items");
                } else if (state.libraryPane === "search") {
                    setLibraryPane("categories");
                } else {
                    setLibraryPane("categories");
                }
                break;
            case KEY_CODES.RIGHT:
                event.preventDefault();
                if (isVodSection()) {
                    moveVodLibraryHorizontal(1);
                    break;
                }
                if (state.libraryPane === "search") {
                    setLibraryPane("items");
                } else if (state.libraryPane === "items" && !isVodSection()) {
                    setLibraryPane("downloads");
                } else if (visibleItems.length) {
                    setLibraryPane("items");
                }
                break;
            case KEY_CODES.UP:
                event.preventDefault();
                if (isVodSection() && state.libraryPane === "items") {
                    scrollVodLibraryItems(-1);
                    break;
                }
                moveLibraryFocus(-1);
                break;
            case KEY_CODES.DOWN:
                event.preventDefault();
                if (isVodSection() && state.libraryPane === "items") {
                    scrollVodLibraryItems(1);
                    break;
                }
                moveLibraryFocus(1);
                break;
            case KEY_CODES.ENTER:
                event.preventDefault();
                if (state.libraryPane === "search") {
                    elements.librarySearchInput.focus();
                } else if (state.libraryPane === "categories") {
                    setCategory(state.categoryIndex);
                    if (visibleItems.length) {
                        setLibraryPane("items");
                    }
                } else if (state.libraryPane === "downloads") {
                    runDownloadAction();
                } else {
                    openItem(state.itemIndex);
                }
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                goHome();
                break;
        }
    }

    function handleManagementKeys(keyCode, event) {
        if (state.screen === "settings" && handleSettingsKeys(keyCode, event)) {
            return;
        }

        if (state.screen === "playlist" && handlePlaylistManagementKeys(keyCode, event)) {
            return;
        }

        switch (keyCode) {
            case KEY_CODES.LEFT:
            case KEY_CODES.UP:
                event.preventDefault();
                moveManagementFocus(-1);
                break;
            case KEY_CODES.RIGHT:
            case KEY_CODES.DOWN:
                event.preventDefault();
                moveManagementFocus(1);
                break;
            case KEY_CODES.ENTER:
                if (activateManagementFocus()) {
                    event.preventDefault();
                }
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                goHome();
                break;
        }
    }

    function handleSettingsKeys(keyCode, event) {
        var active = document.activeElement;
        var navButtons = getSettingsNavButtons();
        var navIndex = navButtons.indexOf(active);

        if (navIndex >= 0) {
            switch (keyCode) {
                case KEY_CODES.UP:
                    event.preventDefault();
                    focusSettingsNavButton(navIndex - 1);
                    return true;
                case KEY_CODES.DOWN:
                    event.preventDefault();
                    focusSettingsNavButton(navIndex + 1);
                    return true;
                case KEY_CODES.RIGHT:
                    event.preventDefault();
                    focusFirstActiveSettingsOption();
                    return true;
                case KEY_CODES.ENTER:
                    event.preventDefault();
                    setSettingsPanel(active.getAttribute("data-settings-panel"));
                    syncSettingsButtons();
                    syncManagementFocus();
                    return true;
                case KEY_CODES.BACK:
                case KEY_CODES.RETURN:
                    event.preventDefault();
                    goHome();
                    return true;
            }
        }

        if (keyCode === KEY_CODES.LEFT) {
            event.preventDefault();
            focusActiveSettingsNavButton();
            return true;
        }

        return false;
    }

    function handlePlaylistManagementKeys(keyCode, event) {
        if (document.activeElement === elements.playlistType &&
                (keyCode === KEY_CODES.ENTER || keyCode === KEY_CODES.LEFT || keyCode === KEY_CODES.RIGHT)) {
            event.preventDefault();
            cyclePlaylistType(keyCode === KEY_CODES.LEFT ? -1 : 1);
            return true;
        }

        return false;
    }

    function getSettingsNavButtons() {
        return Array.prototype.slice.call(elements.settingsForm.querySelectorAll(".settings-nav-button"));
    }

    function focusSettingsNavButton(index) {
        var navButtons = getSettingsNavButtons();
        var controls = getManagementFocusableControls();
        var target;

        if (!navButtons.length) {
            return;
        }

        index = Math.max(0, Math.min(navButtons.length - 1, index));
        target = navButtons[index];
        focusManagementControl(controls.indexOf(target), controls);
    }

    function focusActiveSettingsNavButton() {
        var navButtons = getSettingsNavButtons();
        var activeIndex = 0;

        navButtons.forEach(function (button, index) {
            if (button.classList.contains("is-active")) {
                activeIndex = index;
            }
        });

        focusSettingsNavButton(activeIndex);
    }

    function focusFirstActiveSettingsOption() {
        var panel = elements.settingsForm.querySelector(".settings-panel.is-active");
        var target = panel && panel.querySelector("button");
        var controls = getManagementFocusableControls();

        if (!target) {
            target = elements.settingsForm.querySelector(".settings-save-button");
        }

        if (target) {
            focusManagementControl(controls.indexOf(target), controls);
        }
    }

    function cyclePlaylistType(direction) {
        var options = Array.prototype.slice.call(elements.playlistType.options);
        var index = Math.max(0, elements.playlistType.selectedIndex);

        if (!options.length) {
            return;
        }

        index = (index + direction + options.length) % options.length;
        elements.playlistType.selectedIndex = index;
        renderPlaylistTypeFields();
        focusFirstManagementControl("#playlistType");
    }

    function getManagementScreenElement() {
        if (state.screen === "playlist") {
            return elements.playlistScreen;
        }
        if (state.screen === "account") {
            return elements.accountScreen;
        }
        if (state.screen === "settings") {
            return elements.settingsScreen;
        }
        if (state.screen === "history") {
            return elements.historyScreen;
        }

        return null;
    }

    function getManagementFocusableControls() {
        var screen = getManagementScreenElement();
        var selector = "button, input, select, textarea, [tabindex]:not([tabindex='-1'])";

        if (!screen) {
            return [];
        }

        return Array.prototype.slice.call(screen.querySelectorAll(selector)).filter(function (control) {
            return !!(control &&
                !control.disabled &&
                !control.hidden &&
                control.getClientRects().length);
        });
    }

    function getManagementFocusIndex(controls) {
        var active = document.activeElement;
        var index = controls.indexOf(active);

        if (index >= 0) {
            return index;
        }

        return Math.max(0, Math.min(controls.length - 1, state.managementFocusIndex || 0));
    }

    function moveManagementFocus(delta) {
        var controls = getManagementFocusableControls();
        var index;

        if (!controls.length) {
            return;
        }

        index = getManagementFocusIndex(controls);
        focusManagementControl(index + delta, controls);
    }

    function focusFirstManagementControl(preferredSelector) {
        var controls = getManagementFocusableControls();
        var preferred = preferredSelector ? getManagementScreenElement().querySelector(preferredSelector) : null;
        var preferredIndex = preferred ? controls.indexOf(preferred) : -1;

        if (!controls.length) {
            return;
        }

        focusManagementControl(preferredIndex >= 0 ? preferredIndex : 0, controls);
    }

    function focusManagementControl(index, controls) {
        var target;

        controls = controls || getManagementFocusableControls();
        if (!controls.length) {
            return;
        }

        index = Math.max(0, Math.min(controls.length - 1, index));
        state.managementFocusIndex = index;
        target = controls[index];

        controls.forEach(function (control) {
            control.classList.remove("is-focused");
        });
        target.classList.add("is-focused");
        target.focus();
        target.scrollIntoView({ block: "nearest", inline: "nearest" });
    }

    function syncManagementFocus() {
        var controls = getManagementFocusableControls();

        if (!controls.length) {
            return;
        }

        focusManagementControl(getManagementFocusIndex(controls), controls);
    }

    function activateManagementFocus() {
        var controls = getManagementFocusableControls();
        var index = getManagementFocusIndex(controls);
        var control = controls[index];
        var tagName;

        if (!control) {
            return false;
        }

        tagName = control.tagName;
        if (tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT") {
            control.focus();
            return false;
        }

        if (typeof control.click === "function") {
            control.click();
            window.setTimeout(syncManagementFocus, 0);
            return true;
        }

        return false;
    }

    function handlePlayerKeys(keyCode, event) {
        showOverlay();

        switch (keyCode) {
            case KEY_CODES.LEFT:
                event.preventDefault();
                if (isPlayerToolPanelOpen()) {
                    movePlayerToolOptionByGrid(-1, 0);
                    break;
                }
                if (isPlayerToolFocused()) {
                    movePlayerToolFocus(-1);
                } else {
                    player.seekBy(-10);
                }
                break;
            case KEY_CODES.RIGHT:
                event.preventDefault();
                if (isPlayerToolPanelOpen()) {
                    movePlayerToolOptionByGrid(1, 0);
                    break;
                }
                if (isPlayerToolFocused()) {
                    movePlayerToolFocus(1);
                } else {
                    player.seekBy(10);
                }
                break;
            case KEY_CODES.UP:
                event.preventDefault();
                if (isPlayerToolPanelOpen()) {
                    movePlayerToolOptionByGrid(0, -1);
                    break;
                }
                movePlayerControlFocus(-1);
                break;
            case KEY_CODES.DOWN:
                event.preventDefault();
                if (isPlayerToolPanelOpen()) {
                    movePlayerToolOptionByGrid(0, 1);
                    break;
                }
                movePlayerControlFocus(1);
                break;
            case KEY_CODES.ENTER:
            case KEY_CODES.PLAY:
            case KEY_CODES.PAUSE:
            case KEY_CODES.PLAY_PAUSE:
            case KEY_CODES.MEDIA_PLAY_PAUSE:
                event.preventDefault();
                if (state.focusedControl === "back" && keyCode === KEY_CODES.ENTER) {
                    goBackFromPlayer();
                } else if (isPlayerToolPanelOpen() && keyCode === KEY_CODES.ENTER) {
                    applyFocusedPlayerToolOption();
                } else if (isPlayerToolFocused() && keyCode === KEY_CODES.ENTER) {
                    openPlayerTool(state.focusedControl);
                } else {
                    player.togglePlay();
                }
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                goBackFromPlayer();
                break;
        }
    }

    function handleDetailKeys(keyCode, event) {
        switch (keyCode) {
            case KEY_CODES.LEFT:
                event.preventDefault();
                moveDetailHorizontal(-1);
                break;
            case KEY_CODES.RIGHT:
                event.preventDefault();
                moveDetailHorizontal(1);
                break;
            case KEY_CODES.UP:
                event.preventDefault();
                moveDetailVertical(-1);
                break;
            case KEY_CODES.DOWN:
                event.preventDefault();
                moveDetailVertical(1);
                break;
            case KEY_CODES.ENTER:
                event.preventDefault();
                activateDetailFocus();
                break;
            case KEY_CODES.PLAY:
            case KEY_CODES.PLAY_PAUSE:
            case KEY_CODES.MEDIA_PLAY_PAUSE:
                event.preventDefault();
                playDetailItem();
                break;
            case KEY_CODES.BACK:
            case KEY_CODES.RETURN:
                event.preventDefault();
                goBackFromDetail();
                break;
        }
    }

    function openLibrary(sectionId) {
        state.screen = "library";
        state.activeSection = sectionId;
        state.categoryIndex = 0;
        state.itemIndex = 0;
        state.libraryPane = "categories";
        showScreen("library");
        state.searchQuery = "";
        if (elements.librarySearchInput) {
            elements.librarySearchInput.value = "";
        }
        renderLibrary();
    }

    function openItem(index) {
        if (!visibleItems[index]) {
            return;
        }

        if (isVodSection()) {
            openDetail(visibleItems[index], "library");
            return;
        }

        playItem(visibleItems[index], "library");
    }

    function openDetail(item, returnScreen) {
        state.screen = "detail";
        state.detailReturnScreen = returnScreen || "library";
        state.detailItem = item;
        state.detailPane = "actions";
        state.detailActionIndex = 1;
        state.seasonIndex = 0;
        state.episodeIndex = 0;
        state.castIndex = 0;
        state.actorWorkIndex = 0;
        showScreen("detail");
        elements.detailScreen.scrollTop = 0;
        renderDetail(item);
        setDetailAction(state.detailActionIndex);
    }

    function renderDetail(item) {
        var meta;
        var cast;

        if (!item) {
            return;
        }

        meta = getRichDetailMeta(item);
        cast = getCastPeople(item);

        elements.detailBackdrop.style.cssText = getBackdropStyle(item);
        elements.detailPoster.style.cssText = getPosterStyle(item);
        elements.detailPoster.innerHTML = getPosterFallbackHtml(item);
        elements.detailEyebrow.textContent = getSection(item.section).subtitle;
        elements.detailTitle.textContent = item.title;
        elements.detailDescription.textContent = getReadableStory(item);
        elements.detailMeta.innerHTML = meta.map(function (value) {
            return '<span class="' + escapeHtml(value.className || "") + '">' + escapeHtml(value.text) + '</span>';
        }).join("");
        elements.castList.innerHTML = cast.map(function (person, index) {
            var name = person.name || String(person || "");
            var initials = person.image ? "" : escapeHtml(getInitials(name));
            var selectedClass = state.detailPane === "actorWorks" && index === state.castIndex ? " is-selected" : "";
            return '<button class="cast-card' + selectedClass + '" type="button" data-index="' + index + '">' +
                '<div class="cast-photo cast-color-' + (index % 6) + '" style="' + getCastPhotoStyle(item, person, index) + '">' + initials + '</div>' +
                '<span>' + escapeHtml(name) + '</span>' +
            '</button>';
        }).join("");

        renderSeriesRows(item);
        actorSearchToken += 1;
        renderActorWorks([]);
        loadDetailMetadata(item);
    }

    function playDetailItem() {
        if (!state.detailItem) {
            return;
        }

        if (state.detailItem.section === "series") {
            if (detailEpisodes.length) {
                playEpisode(state.episodeIndex || 0);
            } else {
                showDetailActionMessage("جاري تحميل الحلقات. اختر الحلقة بعد ظهور المواسم.");
            }
            return;
        }

        playItem(state.detailItem, "detail");
    }

    function playTrailer() {
        showDetailActionMessage("المقطع الاعلاني غير متوفر لهذا العمل.");
    }

    function addDetailFavorite() {
        var key;

        if (!state.detailItem) {
            return;
        }

        key = getLibraryWorkKey(state.detailItem);
        try {
            window.localStorage.setItem("nexoWishlist:" + key, JSON.stringify({
                title: state.detailItem.title,
                section: state.detailItem.section,
                addedAt: Date.now()
            }));
        } catch (error) {
            console.log("Could not save wishlist item:", error.message);
        }

        showDetailActionMessage("\u062a\u0645\u062a \u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u0625\u0644\u0649 \u0642\u0627\u0626\u0645\u0629 \u0627\u0644\u0631\u063a\u0628\u0627\u062a \u2665");
    }

    function removeFromWatchlist() {
        showDetailActionMessage("تمت الإزالة من قائمة المشاهدة.");
    }

    function showDetailActionMessage(message) {
        elements.detailActionMessage.textContent = message;
        elements.detailActionMessage.hidden = false;
    }

    function renderSeriesRows(item) {
        detailSeasons = item && item.section === "series" ? getSeriesSeasons(item) : [];
        elements.seasonSection.hidden = !detailSeasons.length;

        if (!detailSeasons.length) {
            elements.seasonList.innerHTML = "";
            elements.episodeList.innerHTML = "";
            detailEpisodes = [];
            return;
        }

        if (state.seasonIndex >= detailSeasons.length) {
            state.seasonIndex = 0;
        }

        elements.seasonList.innerHTML = detailSeasons.map(function (season, index) {
            return '<button class="season-button' + (index === state.seasonIndex ? " is-focused" : "") + '" type="button" data-index="' + index + '">موسم ' + escapeHtml(season.number) + '</button>';
        }).join("");

        renderEpisodes();
    }

    function renderSeriesRows(item) {
        detailSeasons = item && item.section === "series" ? getSeriesSeasons(item) : [];
        elements.seasonSection.hidden = !detailSeasons.length;

        if (!detailSeasons.length) {
            elements.seasonList.innerHTML = "";
            elements.episodeList.innerHTML = "";
            detailEpisodes = [];
            return;
        }

        if (state.seasonIndex >= detailSeasons.length) {
            state.seasonIndex = 0;
        }

        elements.seasonList.innerHTML = detailSeasons.map(function (season, index) {
            return '<button class="season-button' + (index === state.seasonIndex ? " is-focused" : "") + '" type="button" data-index="' + index + '">' + escapeHtml(getLanguageLabels(state.language).season) + ' ' + escapeHtml(season.number) + '</button>';
        }).join("");

        renderEpisodes();
    }

    function renderEpisodes() {
        var season = detailSeasons[state.seasonIndex];

        detailEpisodes = season ? season.episodes : [];
        elements.episodeList.innerHTML = detailEpisodes.map(function (episode, index) {
            return '<button class="episode-card" type="button" data-index="' + index + '">' +
                '<span class="episode-thumb" style="' + getPosterStyle(episode) + '">' + getPosterFallbackHtml(episode) + '</span>' +
                '<span class="episode-title">S' + escapeHtml(episode.seasonNumber || season.number) + ' E' + escapeHtml(episode.episodeNumber || (index + 1)) + '</span>' +
            '</button>';
        }).join("");
    }

    function setDetailSeason(index) {
        state.seasonIndex = Math.max(0, Math.min(detailSeasons.length - 1, index));
        state.episodeIndex = 0;
        state.detailPane = "seasons";
        renderSeriesRows(state.detailItem);
        focusDetailPane();
    }

    function playEpisode(index) {
        var episode = detailEpisodes[index];

        if (episode) {
            state.episodeIndex = index;
            ensureSeriesPlaybackContext(episode, state.detailItem);
            playItem(episode, "detail");
        }
    }

    function ensureSeriesPlaybackContext(item, parentItem) {
        if (!item || item.section !== "series" || item.seriesXtreamId) {
            return;
        }

        if (parentItem && parentItem.xtreamId) {
            item.seriesXtreamId = parentItem.xtreamId;
        }
    }

    function getSeriesSeasons(item) {
        if (item.seriesSeasons && item.seriesSeasons.length) {
            return item.seriesSeasons;
        }

        return buildLocalSeriesSeasons(item);
    }

    function buildLocalSeriesSeasons(item) {
        var key = normalizeSeriesKey(item.seriesTitle || item.title);
        var map = {};

        allItems.forEach(function (candidate) {
            var candidateKey;
            var seasonNumber;
            var episodeNumber;

            if (candidate.section !== "series") {
                return;
            }

            candidateKey = normalizeSeriesKey(candidate.seriesTitle || candidate.title);
            if (candidateKey !== key) {
                return;
            }

            seasonNumber = candidate.seasonNumber || 1;
            episodeNumber = candidate.episodeNumber || 1;
            if (!map[seasonNumber]) {
                map[seasonNumber] = [];
            }
            candidate.seasonNumber = seasonNumber;
            candidate.episodeNumber = episodeNumber;
            if (item.xtreamId) {
                candidate.seriesXtreamId = item.xtreamId;
            }
            map[seasonNumber].push(candidate);
        });

        return Object.keys(map).sort(numberSort).map(function (seasonNumber) {
            return {
                number: seasonNumber,
                episodes: map[seasonNumber].sort(function (a, b) {
                    return (a.episodeNumber || 0) - (b.episodeNumber || 0);
                })
            };
        });
    }

    function normalizeSeriesKey(value) {
        return String(value || "").toLowerCase().replace(/\bS\d+\s*E\d+\b/ig, "").replace(/\s+/g, " ").trim();
    }

    function numberSort(a, b) {
        return parseInt(a, 10) - parseInt(b, 10);
    }

    function getUniqueVodItems() {
        var seen = {};
        var items = [];

        allItems.forEach(function (item) {
            var key;

            if (item.section !== "movies" && item.section !== "series") {
                return;
            }

            key = getLibraryWorkKey(item);
            if (!seen[key]) {
                seen[key] = true;
                items.push(item);
            }
        });

        return items;
    }

    function getLibraryWorkKey(item) {
        var title = item.section === "series" ? (item.seriesTitle || item.title) : item.title;
        return item.section + ":" + normalizeWorkTitle(title || item.title || item.url);
    }

    function normalizeWorkTitle(value) {
        return normalizeSearchText(value)
            .replace(/\bs\d{1,2}\s*e\d{1,3}\b/g, "")
            .replace(/\b\d{1,2}x\d{1,3}\b/g, "")
            .replace(/\b(19|20)\d{2}\b/g, "")
            .replace(/\([^)]*\)/g, "")
            .replace(/جوده اقل|جوده عاليه|مدبلج|مترجم/g, "")
            .replace(/\b(4k|uhd|fhd|hd|sd|mp4|hevc|web dl|bluray|cam)\b/g, "")
            .replace(/\s+/g, " ")
            .trim();
    }

    function itemHasActor(item, normalizedActor) {
        return getReadableCastItems(item).some(function (name) {
            var normalizedName = normalizeSearchText(name);
            return normalizedName === normalizedActor;
        });
    }

    function normalizeSearchText(value) {
        return String(value || "")
            .toLowerCase()
            .replace(/[\u064b-\u065f\u0670]/g, "")
            .replace(/[إأآا]/g, "ا")
            .replace(/[ىي]/g, "ي")
            .replace(/ة/g, "ه")
            .replace(/[^\w\u0600-\u06ff]+/g, " ")
            .replace(/\s+/g, " ")
            .trim();
    }

    function normalizeWorkTitle(value) {
        return normalizeSearchText(value)
            .replace(/\bs\d{1,2}\s*e\d{1,3}\b/g, "")
            .replace(/\b\d{1,2}x\d{1,3}\b/g, "")
            .replace(/\b(19|20)\d{2}\b/g, "")
            .replace(/\([^)]*\)/g, "")
            .replace(/جوده اقل|جوده عاليه|مدبلج|مترجم/g, "")
            .replace(/\b(4k|uhd|fhd|hd|sd|mp4|hevc|web dl|bluray|cam)\b/g, "")
            .replace(/\s+/g, " ")
            .trim();
    }

    function normalizeSearchText(value) {
        return String(value || "")
            .toLowerCase()
            .replace(/[\u064b-\u065f\u0670]/g, "")
            .replace(/[\u0625\u0623\u0622\u0627]/g, "\u0627")
            .replace(/[\u0649\u064a]/g, "\u064a")
            .replace(/\u0629/g, "\u0647")
            .replace(/[^\w\u0600-\u06ff]+/g, " ")
            .replace(/\s+/g, " ")
            .trim();
    }

    function renderActorWorks(items) {
        actorWorks = items || [];
        elements.actorWorksSection.hidden = !actorWorks.length;
        elements.actorWorksList.innerHTML = actorWorks.map(function (item, index) {
            return '<button class="actor-work-card" type="button" data-index="' + index + '">' +
                '<span class="actor-work-poster" style="' + getPosterStyle(item) + '">' + getPosterFallbackHtml(item) + '</span>' +
                '<span>' + escapeHtml(item.title) + '</span>' +
            '</button>';
        }).join("");
    }

    function renderActorWorksMessage(message) {
        actorWorks = [];
        elements.actorWorksSection.hidden = false;
        elements.actorWorksList.innerHTML = '<div class="actor-work-empty">' + escapeHtml(message) + '</div>';
    }

    function openActorWorks(index) {
        var cast = getCastPeople(state.detailItem);
        var actorPerson = cast[index];
        var actor = actorPerson && actorPerson.name;

        if (!actor || actor.indexOf("غير متوفر") !== -1) {
            showDetailActionMessage("لا توجد أعمال مرتبطة بهذا الممثل في المكتبة.");
            return;
        }

        elements.actorWorksTitle.textContent = "أعمال " + actor;
        state.castIndex = index;
        state.actorWorkIndex = 0;
        state.detailPane = "actorWorks";
        renderActorWorksMessage("جاري البحث عن أعمال " + actor + "...");
        focusDetailPane();
        findActorWorks(actor, actorPerson && actorPerson.tmdbPersonId);
    }

    function openActorWorks(index) {
        var cast = getCastPeople(state.detailItem);
        var actorPerson = cast[index];
        var actor = actorPerson && actorPerson.name;
        var labels = getLanguageLabels(state.language);

        if (!actor || actor.indexOf("\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631") !== -1) {
            showDetailActionMessage(labels.noActorWorks);
            return;
        }

        elements.actorWorksTitle.textContent = labels.actorWorksOf + actor;
        state.castIndex = index;
        state.actorWorkIndex = 0;
        state.detailPane = "actorWorks";
        renderActorWorksMessage(labels.loadingActorWorks + actor + "...");
        focusDetailPane();
        findActorWorks(actor, actorPerson && actorPerson.tmdbPersonId);
    }

    function findActorWorks(actor, tmdbPersonId) {
        var token = actorSearchToken + 1;
        var normalizedActor = normalizeSearchText(actor);
        var candidates = getUniqueVodItems();
        var matches = [];
        var seen = {};
        var cursor = 0;
        var active = 0;
        var maxActive = 5;

        actorSearchToken = token;

        function addMatch(item) {
            var key = getLibraryWorkKey(item);

            if (seen[key] || !itemHasActor(item, normalizedActor)) {
                return;
            }

            seen[key] = true;
            matches.push(item);
            renderActorWorks(matches.slice(0, 96));
        }

        candidates.forEach(addMatch);
        if (tmdbPersonId) {
            loadTmdbActorWorks(tmdbPersonId, token, function (titles) {
                if (token !== actorSearchToken) {
                    return;
                }
                addTmdbActorTitleMatches(titles, candidates, seen, matches);
                if (matches.length) {
                    renderActorWorks(matches.slice(0, 96));
                }
            });
        }

        function finishIfDone() {
            if (token !== actorSearchToken || active || cursor < candidates.length) {
                return;
            }

            if (!matches.length) {
                renderActorWorksMessage("لا توجد أعمال أخرى لهذا الممثل في مكتبة الأفلام أو المسلسلات.");
            }
        }

        function pump() {
            if (token !== actorSearchToken) {
                return;
            }

            while (active < maxActive && cursor < candidates.length) {
                var item = candidates[cursor];
                cursor += 1;

                if (item.metadataLoaded || !getXtreamInfoUrl(item)) {
                    continue;
                }

                fetchActorCandidate(item);
            }

            finishIfDone();
        }

        function fetchActorCandidate(candidate) {
            active += 1;
            fetchItemMetadata(candidate, function () {
                active -= 1;
                addMatch(candidate);
                pump();
                finishIfDone();
            });
        }

        pump();
    }

    function addTmdbActorTitleMatches(titles, candidates, seen, matches) {
        var normalizedTitles = (titles || []).map(function (title) {
            return normalizeWorkTitle(title);
        }).filter(Boolean);

        candidates.forEach(function (item) {
            var key = getLibraryWorkKey(item);
            var itemTitle = normalizeWorkTitle(item.seriesTitle || item.title);

            if (seen[key] || !itemTitle) {
                return;
            }

            normalizedTitles.some(function (title) {
                if (title === itemTitle) {
                    seen[key] = true;
                    matches.push(item);
                    return true;
                }
                return false;
            });
        });
    }

    function openActorWork(index) {
        if (actorWorks[index]) {
            openDetail(actorWorks[index]);
        }
    }

    function loadDetailMetadata(item) {
        var url = getXtreamInfoUrl(item);
        var cacheKey = item.section + ":" + (item.xtreamId || item.url);

        if (!url || item.metadataLoaded) {
            loadTmdbCastImages(item);
            return;
        }

        if (detailMetadataCache[cacheKey]) {
            applyDetailMetadata(item, detailMetadataCache[cacheKey]);
            return;
        }

        item.metadataLoading = true;
        requestIptvJson(url, function (error, data) {
            item.metadataLoading = false;
            if (error || state.detailItem !== item) {
                if (error) {
                    console.log("Could not load detail metadata:", error.message);
                    loadTmdbCastImages(item);
                }
                return;
            }

            detailMetadataCache[cacheKey] = data;
            applyDetailMetadata(item, data);
        });
    }

    function fetchItemMetadata(item, callback) {
        var url = getXtreamInfoUrl(item);
        var cacheKey = item.section + ":" + (item.xtreamId || item.url);
        var wantsSubtitles = !item.subtitles || !item.subtitles.length;

        if (!url) {
            callback();
            return;
        }

        if (item.metadataLoaded && !wantsSubtitles) {
            callback();
            return;
        }

        if (detailMetadataCache[cacheKey]) {
            mergeItemMetadata(item, detailMetadataCache[cacheKey]);
            if (!wantsSubtitles || (item.subtitles && item.subtitles.length)) {
                callback();
                return;
            }
        }

        requestIptvJson(url, function (error, data) {
            if (!error && data) {
                detailMetadataCache[cacheKey] = data;
                mergeItemMetadata(item, data);
            } else {
                if (error) {
                    console.log("Could not load playback metadata:", item.title, error.message);
                }
                item.metadataLoaded = true;
            }

            callback();
        });
    }

    function needsSubtitleMetadata(item) {
        return !!(getXtreamInfoUrl(item) && (!item.metadataLoaded || !item.subtitles || !item.subtitles.length));
    }

    function needsRemoteSubtitles(item) {
        return !!(item && (item.section === "movies" || item.section === "series") &&
            (!item.subtitles || !item.subtitles.length));
    }

    function ensurePlaybackSubtitles(item, callback) {
        function afterMetadata() {
            if (!needsRemoteSubtitles(item)) {
                callback();
                return;
            }

            fetchRemoteSubtitles(item, callback);
        }

        if (needsSubtitleMetadata(item)) {
            fetchItemMetadata(item, afterMetadata);
            return;
        }

        afterMetadata();
    }

    function pickPreferredSubtitle(subtitles) {
        var list = subtitles || [];
        var i;
        var subtitle;

        for (i = 0; i < list.length; i += 1) {
            subtitle = list[i];
            if (subtitle.language === "ar" || /arab/i.test(subtitle.label || "")) {
                return subtitle;
            }
        }

        return list[0] || null;
    }

    function applyLoadedSubtitlesToPlayer(item) {
        var preferred;

        if (!item || state.playingItem !== item || !item.subtitles || !item.subtitles.length) {
            return;
        }

        preferred = pickPreferredSubtitle(item.subtitles);
        console.log("[subtitles] Applying", item.subtitles.length, "track(s) to player for", item.title);

        if (player.loadMode === "avplay" && player.attachAvplayExternalSubtitle) {
            prepareSubtitleTracks(item);
            player.attachAvplayExternalSubtitle(preferred, item.subtitles, function (success) {
                if (!success) {
                    console.log("[subtitles] AVPlay subtitle attach failed; keeping AVPlay playback active:", item.title);
                }
                refreshPlayerOptions();
                autoSelectFirstSubtitleTrack();
            });
            return;
        }

        prepareSubtitleTracks(item);
        refreshPlayerOptions();
        autoSelectFirstSubtitleTrack();
    }

    function fetchRemoteSubtitles(item, callback) {
        var mediaType = item.section === "series" ? "tv" : "movie";
        var openSubtitlesType = item.section === "series" ? "series" : "movie";

        if (!needsRemoteSubtitles(item)) {
            callback();
            return;
        }

        function loadFromImdb(imdbId) {
            if (!imdbId) {
                searchOpenSubtitlesByTitle(item, openSubtitlesType, callback);
                return;
            }

            requestExternalText("https://opensubtitles-v3.strem.io/subtitles/" + openSubtitlesType + "/" + encodeURIComponent(buildOpenSubtitlesId(item, imdbId)) + ".json", function (error, text) {
                var parsed;
                var subtitles;

                if (error) {
                    console.log("[subtitles] OpenSubtitles lookup failed:", item.title, error.message);
                    searchOpenSubtitlesByTitle(item, openSubtitlesType, callback);
                    return;
                }

                try {
                    parsed = JSON.parse(text);
                } catch (parseError) {
                    searchOpenSubtitlesByTitle(item, openSubtitlesType, callback);
                    return;
                }

                subtitles = parseOpenSubtitlesResponse(parsed);
                if (subtitles.length) {
                    item.subtitles = mergeSubtitleLists(item.subtitles, subtitles);
                    console.log("[subtitles] OpenSubtitles tracks:", item.title, subtitles.length);
                    callback();
                    return;
                }

                searchOpenSubtitlesByTitle(item, openSubtitlesType, callback);
            });
        }

        if (!item.tmdbId) {
            resolveTmdbIdForItem(item, function (resolvedId) {
                if (resolvedId) {
                    item.tmdbId = resolvedId;
                }
                if (!item.tmdbId) {
                    searchOpenSubtitlesByTitle(item, openSubtitlesType, callback);
                    return;
                }
                resolveImdbIdForItem(item, mediaType, loadFromImdb);
            });
            return;
        }

        resolveImdbIdForItem(item, mediaType, loadFromImdb);
    }

    function buildOpenSubtitlesId(item, imdbId) {
        var season;
        var episode;

        if (!item || item.section !== "series") {
            return imdbId || "";
        }

        season = parseInt(item.seasonNumber || item.season || 1, 10);
        episode = parseInt(item.episodeNumber || item.episode || 1, 10);

        if (!season || season < 1) {
            season = 1;
        }
        if (!episode || episode < 1) {
            episode = 1;
        }

        return (imdbId || "") + ":" + season + ":" + episode;
    }

    function resolveImdbIdFromTitle(item, callback) {
        var query = normalizeDisplayTitle(item.seriesTitle || item.title).replace(/\(\d{4}\)\s*$/, "").trim();
        var yearMatch = String(item.title || "").match(/\((\d{4})\)/);
        var year = item.year || (yearMatch ? yearMatch[1] : "");
        var searchQuery;

        if (!query) {
            callback("");
            return;
        }

        resolveImdbIdFromCinemetaSearch(item, query, year, function (cinemetaImdbId) {
            if (cinemetaImdbId) {
                callback(cinemetaImdbId);
                return;
            }

            searchQuery = encodeURIComponent(query + (year ? " " + year : ""));
            requestExternalText("https://www.imdb.com/find?q=" + searchQuery, function (error, html) {
                var match = !error && html ? html.match(/\/title\/(tt\d+)/i) : null;
                callback(match ? match[1] : "");
            });
        });
    }

    function resolveImdbIdFromCinemetaSearch(item, query, year, callback) {
        var type = item && item.section === "series" ? "series" : "movie";
        var url = "https://v3-cinemeta.strem.io/catalog/" + type + "/top/search=" + encodeURIComponent(query + (year ? " " + year : "")) + ".json";
        var wantedTitle = normalizeWorkTitle(query);
        var wantedYear = String(year || "");

        requestExternalText(url, function (error, text) {
            var data;
            var metas;
            var best = null;
            var i;

            if (error || !text) {
                callback("");
                return;
            }

            try {
                data = JSON.parse(text);
            } catch (parseError) {
                callback("");
                return;
            }

            metas = data && data.metas ? data.metas : [];
            for (i = 0; i < metas.length; i += 1) {
                var meta = metas[i];
                var resultTitle = normalizeWorkTitle(meta.name || "");
                var resultYear = String(meta.year || meta.releaseInfo || "").match(/\b(19|20)\d{2}\b/);
                var titleMatches = resultTitle === wantedTitle ||
                    resultTitle.indexOf(wantedTitle) !== -1 ||
                    wantedTitle.indexOf(resultTitle) !== -1;
                var yearMatches = !wantedYear || (resultYear && resultYear[0] === wantedYear);

                if (meta && (meta.imdb_id || meta.id) && titleMatches && yearMatches) {
                    best = meta;
                    break;
                }
            }

            callback(best ? (best.imdb_id || best.id || "") : "");
        });
    }

    function searchOpenSubtitlesByTitle(item, openSubtitlesType, callback) {
        resolveImdbIdFromTitle(item, function (imdbId) {
            var parsed;
            var subtitles;

            if (!imdbId) {
                console.log("[subtitles] No IMDB id found for title search:", item.title);
                callback();
                return;
            }

            item.imdbId = imdbId;
            requestExternalText("https://opensubtitles-v3.strem.io/subtitles/" + openSubtitlesType + "/" + encodeURIComponent(buildOpenSubtitlesId(item, imdbId)) + ".json", function (error, text) {
                if (error) {
                    console.log("[subtitles] OpenSubtitles IMDB search failed:", item.title, error.message);
                    callback();
                    return;
                }

                try {
                    parsed = JSON.parse(text);
                } catch (parseError) {
                    callback();
                    return;
                }

                subtitles = parseOpenSubtitlesResponse(parsed);
                if (subtitles.length) {
                    item.subtitles = mergeSubtitleLists(item.subtitles, subtitles);
                    console.log("[subtitles] OpenSubtitles via IMDB title search:", item.title, subtitles.length, imdbId);
                }

                callback();
            });
        });
    }

    function resolveImdbIdForItem(item, mediaType, callback) {
        var cacheKey = mediaType + ":" + item.tmdbId;
        var cinemetaUrl = "https://cinemeta-live.strem.io/meta/" + (mediaType === "tv" ? "series" : "movie") + "/tmdb:" + encodeURIComponent(item.tmdbId) + ".json";

        if (item.imdbId) {
            callback(item.imdbId);
            return;
        }

        if (imdbResolveCache[cacheKey]) {
            item.imdbId = imdbResolveCache[cacheKey];
            callback(item.imdbId);
            return;
        }

        requestExternalText(cinemetaUrl, function (metaError, metaText) {
            var meta;

            if (!metaError && metaText) {
                try {
                    meta = JSON.parse(metaText);
                    if (meta && meta.meta && meta.meta.imdb_id) {
                        item.imdbId = meta.meta.imdb_id;
                        imdbResolveCache[cacheKey] = item.imdbId;
                        callback(item.imdbId);
                        return;
                    }
                } catch (parseError) {
                    // Fall through to TMDB page.
                }
            }

            requestExternalText("https://www.themoviedb.org/" + mediaType + "/" + encodeURIComponent(item.tmdbId), function (error, html) {
                var imdbMatch = !error && html ? html.match(/href="https?:\/\/(?:www\.)?imdb\.com\/title\/(tt\d+)/i) : null;

                if (imdbMatch) {
                    item.imdbId = imdbMatch[1];
                    imdbResolveCache[cacheKey] = item.imdbId;
                    callback(item.imdbId);
                    return;
                }

                resolveImdbIdFromTitle(item, function (imdbFromTitle) {
                    if (imdbFromTitle) {
                        item.imdbId = imdbFromTitle;
                        imdbResolveCache[cacheKey] = item.imdbId;
                    }
                    callback(item.imdbId || "");
                });
            });
        });
    }

    function parseOpenSubtitlesResponse(data) {
        var langPriority = { arb: 1, ara: 1, ar: 1, eng: 2, en: 2, fre: 3, fra: 3, fr: 3, spa: 4, es: 4, tur: 5, tr: 5, por: 6, pob: 6 };
        var langLabels = {
            arb: "Arabic",
            ara: "Arabic",
            ar: "Arabic",
            "ar-eg": "Arabic",
            "ar-sa": "Arabic",
            eng: "English",
            en: "English",
            fre: "French",
            fra: "French",
            fr: "French",
            spa: "Spanish",
            es: "Spanish",
            tur: "Turkish",
            tr: "Turkish",
            ger: "German",
            de: "German",
            ita: "Italian",
            it: "Italian",
            por: "Portuguese",
            pob: "Portuguese (BR)"
        };
        var seen = {};
        var list = [];

        (data && data.subtitles ? data.subtitles : []).forEach(function (entry) {
            var lang = String(entry.lang || entry.language || "").toLowerCase();
            var url = entry.url || "";

            if (!url || seen[url]) {
                return;
            }

            seen[url] = true;
            list.push({
                url: url,
                label: langLabels[lang] || lang.toUpperCase() || "Subtitle",
                language: guessSubtitleLanguage(lang),
                priority: langPriority[lang] || 99
            });
        });

        list.sort(function (a, b) {
            return (a.priority || 99) - (b.priority || 99);
        });

        return list.slice(0, 16).map(function (entry) {
            return {
                url: entry.url,
                label: entry.label,
                language: entry.language
            };
        });
    }

    function applyDetailMetadata(item, data) {
        if (!mergeItemMetadata(item, data) || state.detailItem !== item) {
            return;
        }

        renderDetail(item);
        loadTmdbCastImages(item);
    }

    function mergeItemMetadata(item, data) {
        var info = data && (data.info || data.movie_data || data);
        var movieProperties;
        var castPeople;
        var subtitleSources;

        if (!info) {
            return false;
        }

        item.metadataLoaded = true;
        item.plot = info.plot || info.description || item.plot;
        item.rating = info.rating || item.rating;
        item.year = info.releasedate ? String(info.releasedate).slice(0, 4) : (info.year || item.year);
        item.addedAt = info.added || info.added_at || item.addedAt;
        item.duration = info.duration || info.duration_secs || item.duration;
        item.quality = info.quality || item.quality;
        item.tmdbId = info.tmdb_id || getTmdbIdFromUrl(info.tmdb_url) || item.tmdbId;
        movieProperties = parseMovieProperties(info.movie_properties || info.movie_propeties || data.movie_properties || data.movie_propeties);
        subtitleSources = [
            info.subtitles,
            info.subtitle,
            info.subtitles_url,
            info.subtitles_urls,
            info.subtitle_url,
            info.subtitle_urls,
            info.external_subtitles,
            info.subtitles_all,
            info.subtitle_tracks,
            info.caption,
            info.caption_url,
            info.vtt,
            info.srt,
            info.cc,
            info.captions,
            movieProperties && movieProperties.subtitles,
            movieProperties && movieProperties.subtitle,
            movieProperties && movieProperties.subtitles_url,
            movieProperties && movieProperties.subtitle_url,
            data.subtitles,
            data.subtitle,
            data.subtitles_url,
            data.subtitles_urls,
            data.subtitle_url,
            data.subtitle_urls,
            data.external_subtitles,
            data.subtitles_all,
            data.vtt,
            data.srt,
            data.movie_data && data.movie_data.subtitles,
            data.movie_data && data.movie_data.subtitle,
            data.movie_data && data.movie_data.subtitles_url,
            data.movie_data && data.movie_data.subtitles_urls,
            data.movie_data && data.movie_data.subtitle_url,
            data.movie_data && data.movie_data.subtitle_urls,
            data.movie_data && data.movie_data.external_subtitles,
            data.movie_data && data.movie_data.vtt,
            data.movie_data && data.movie_data.srt
        ];
        if (data.info && data.movie_data) {
            collectSubtitleSources(data.movie_data, subtitleSources);
        }
        collectSubtitleSources(data, subtitleSources);
        item.subtitles = mergeSubtitleLists(item.subtitles, normalizeSubtitleList(subtitleSources, item));
        item.seriesSeasons = normalizeXtreamEpisodes(data.episodes, item);
        mergeEpisodeMetadataFromSeriesInfo(item);
        console.log("[subtitles] Metadata merged for", item.title, "tracks:", (item.subtitles || []).length);

        castPeople = parseCastPeople(
            info.cast ||
            info.actors ||
            info.cast_members ||
            info.people ||
            info.credits ||
            data.cast ||
            data.actors ||
            data.cast_members ||
            data.movie_data && (data.movie_data.cast || data.movie_data.actors || data.movie_data.cast_members) ||
            movieProperties && (movieProperties.cast || movieProperties.actors || movieProperties.cast_members) ||
            ""
        );
        if (castPeople.length) {
            item.castPeople = castPeople;
            item.cast = castPeople.map(function (person) {
                return person.name;
            });
        }

        return true;
    }

    function requestJson(url, callback) {
        requestText(url, function (error, text) {
            if (error) {
                callback(error);
                return;
            }

            try {
                callback(null, JSON.parse(text));
            } catch (parseError) {
                callback(parseError);
            }
        });
    }

    function getIptvProxyUrl(url) {
        if (!APP_SERVER_URL || !url || !/^https?:\/\//i.test(url)) {
            return "";
        }

        return normalizeServerUrl(APP_SERVER_URL) + "/api/iptv_proxy.php?url=" + encodeURIComponent(url);
    }

    function requestTextWithIptvFallback(url, timeout, callback) {
        var proxyUrl = getIptvProxyUrl(url);

        if (!proxyUrl) {
            requestTextWithTimeout(url, timeout, callback);
            return;
        }

        requestTextWithTimeout(proxyUrl, timeout, function (error, text) {
            if (!error) {
                callback(null, text);
                return;
            }

            requestTextWithTimeout(url, timeout, callback);
        });
    }

    function requestIptvJson(url, callback) {
        requestTextWithIptvFallback(url, 25000, function (error, text) {
            if (error) {
                callback(error);
                return;
            }

            try {
                callback(null, JSON.parse(text));
            } catch (parseError) {
                callback(parseError);
            }
        });
    }

    function loadTmdbCastImages(item) {
        var tmdbId = item && item.tmdbId;
        var mediaType = item && item.section === "series" ? "tv" : "movie";
        var cacheKey = mediaType + ":" + tmdbId;

        if (!item) {
            return;
        }

        if (!tmdbId) {
            resolveTmdbIdForItem(item, function (resolvedId) {
                if (resolvedId) {
                    item.tmdbId = resolvedId;
                    item.tmdbImagesLoaded = false;
                    loadTmdbCastImages(item);
                } else {
                    console.log("No TMDB id found for cast images:", item.title);
                    loadMissingCastPersonImages(item);
                }
            });
            return;
        }

        cacheKey = mediaType + ":" + tmdbId;

        if (item.tmdbImagesLoaded) {
            return;
        }

        if (tmdbCastCache[cacheKey]) {
            applyTmdbCastImages(item, tmdbCastCache[cacheKey]);
            return;
        }

        console.log("Loading TMDB cast images:", cacheKey, item.title);
        requestExternalText("https://www.themoviedb.org/" + mediaType + "/" + encodeURIComponent(tmdbId) + "/cast", function (error, html) {
            var people;

            if (error) {
                console.log("Could not load TMDB cast images:", error.message);
                loadCinemetaCastImages(item, mediaType, tmdbId, cacheKey);
                return;
            }

            people = parseTmdbCastHtml(html);
            console.log("TMDB cast images loaded:", cacheKey, people.length);
            tmdbCastCache[cacheKey] = people;
            applyTmdbCastImages(item, people);
            if (!people.length) {
                loadCinemetaCastImages(item, mediaType, tmdbId, cacheKey);
            }
        });
    }

    function loadCinemetaCastImages(item, mediaType, tmdbId, cacheKey) {
        var cinemetaType = mediaType === "tv" ? "series" : "movie";
        var url = "https://cinemeta-live.strem.io/meta/" + cinemetaType + "/tmdb:" + encodeURIComponent(tmdbId) + ".json";

        requestExternalText(url, function (error, text) {
            var parsed;
            var people;

            if (error) {
                console.log("Could not load Cinemeta cast:", error.message);
                loadMissingCastPersonImages(item);
                return;
            }

            try {
                parsed = JSON.parse(text);
            } catch (parseError) {
                loadMissingCastPersonImages(item);
                return;
            }

            people = parseCinemetaCastPeople(parsed);
            if (people.length) {
                tmdbCastCache[cacheKey] = people;
                applyTmdbCastImages(item, people);
                return;
            }

            loadMissingCastPersonImages(item);
        });
    }

    function parseCinemetaCastPeople(data) {
        var meta = data && (data.meta || data);
        var cast = meta && (meta.cast || meta.actors || meta.cast_members || []);

        if (typeof cast === "string") {
            cast = cast.split(/[,|،]/);
        }

        if (!Array.isArray(cast)) {
            return [];
        }

        return cast.map(function (person) {
            if (typeof person === "string") {
                return { name: person.trim(), image: "", tmdbPersonId: "" };
            }

            return {
                name: String(person.name || person.original_name || person.title || "").trim(),
                image: normalizePersonImageUrl(person.profile_path || person.image || person.photo || person.avatar || ""),
                tmdbPersonId: person.id || person.tmdb_id || person.person_id || ""
            };
        }).filter(function (person) {
            return person.name;
        }).slice(0, 24);
    }

    function parseTmdbCastHtml(html) {
        var people = [];
        var pattern = /<li[^>]*data-credit-id=[\s\S]*?<a href="\/person\/(\d+)[^"]*"[\s\S]*?<img[^>]+src="([^"]+)"[^>]+alt="([^"]*)"[\s\S]*?<\/li>/g;
        var match;

        while ((match = pattern.exec(html)) && people.length < 60) {
            people.push({
                name: decodeHtml(match[3]),
                image: normalizePersonImageUrl(match[2].replace(/w66_and_h66_face/g, "w300_and_h450_bestv2")),
                tmdbPersonId: match[1]
            });
        }

        if (!people.length) {
            people = parseTmdbCastHtmlFallback(html);
        }

        if (!people.length) {
            people = parseTmdbCastMarkdown(html);
        }

        return people;
    }

    function parseTmdbCastMarkdown(markdown) {
        var people = [];
        var pattern = /!\[Image\s+\d+:\s*([^\]]+)\]\((https:\/\/media\.themoviedb\.org\/t\/p\/w66_and_h66_face\/[^)]+)\)\]\(https:\/\/www\.themoviedb\.org\/person\/(\d+)[^)]*\)/g;
        var match;

        while ((match = pattern.exec(markdown)) && people.length < 60) {
            people.push({
                name: decodeHtml(match[1]),
                image: normalizePersonImageUrl(match[2].replace(/w66_and_h66_face/g, "w300_and_h450_bestv2")),
                tmdbPersonId: match[3]
            });
        }

        return people;
    }

    function resolveTmdbIdForItem(item, callback) {
        var mediaType = item.section === "series" ? "tv" : "movie";
        var cleanTitle = normalizeDisplayTitle(item.seriesTitle || item.title);
        var cacheKey = mediaType + ":" + cleanTitle;
        var searchUrl;

        if (!cleanTitle) {
            callback("");
            return;
        }

        if (tmdbResolveCache[cacheKey]) {
            callback(tmdbResolveCache[cacheKey]);
            return;
        }

        if (item.tmdbResolving) {
            callback("");
            return;
        }

        item.tmdbResolving = true;
        searchUrl = "https://www.themoviedb.org/search?query=" + encodeURIComponent(cleanTitle);
        console.log("Resolving TMDB id:", mediaType, cleanTitle);
        requestExternalText(searchUrl, function (error, markdown) {
            var tmdbId = "";

            item.tmdbResolving = false;
            if (error) {
                console.log("Could not resolve TMDB id:", item.title, error.message);
                callback("");
                return;
            }

            tmdbId = parseTmdbSearchId(markdown, mediaType, cleanTitle);
            if (tmdbId) {
                tmdbResolveCache[cacheKey] = tmdbId;
                console.log("Resolved TMDB id:", item.title, tmdbId);
            }

            callback(tmdbId);
        });
    }

    function parseTmdbSearchId(markdown, mediaType, cleanTitle) {
        var pattern = new RegExp("\\[\\!\\[Image\\s+\\d+:\\s*([^\\]]+)\\]\\([^)]*\\)\\]\\(https:\\/\\/www\\.themoviedb\\.org\\/" + mediaType + "\\/(\\d+)[^)]*\\)", "g");
        var normalizedTitle = normalizeWorkTitle(cleanTitle);
        var firstId = "";
        var match;

        while ((match = pattern.exec(markdown))) {
            var resultTitle = normalizeWorkTitle(match[1]);

            if (!firstId) {
                firstId = match[2];
            }

            if (resultTitle === normalizedTitle ||
                    resultTitle.indexOf(normalizedTitle) !== -1 ||
                    normalizedTitle.indexOf(resultTitle) !== -1) {
                return match[2];
            }
        }

        return firstId;
    }

    function normalizeDisplayTitle(value) {
        return String(value || "")
            .replace(/\([^)]*\)/g, "")
            .replace(/\[\s*\]/g, "")
            .replace(/\bS\s*\d{1,2}\s*[._-]\s*E\s*\d{1,3}\b/ig, "")
            .replace(/\bS\d{1,2}\s*E\d{1,3}\b/ig, "")
            .replace(/\bS\d{1,2}E\d{1,3}\b/ig, "")
            .replace(/\b\d{1,2}x\d{1,3}\b/ig, "")
            .replace(/(?:\b(?:Ep|Episode|Episodio|Bolum)|.pisode)\s*\d{1,3}\b/ig, "")
            .replace(/مدبلج|مترجم|جودة اقل|جودة عالية/g, "")
            .replace(/\s*[-|]\s*[-|]?\s*/g, " - ")
            .replace(/\s*[-|]\s*$/g, "")
            .replace(/^\s*[-|]\s*/g, "")
            .replace(/\s+/g, " ")
            .trim();
    }

    function parseTmdbCastHtmlFallback(html) {
        var people = [];
        var liPattern = /<li[^>]*data-credit-id[\s\S]*?<\/li>/g;
        var personPattern = /\/person\/(\d+)[^"]*/;
        var imagePattern = /<img[^>]+(?:src|data-src)="([^"]+)"/;
        var altPattern = /alt="([^"]*)"/;
        var match;

        while ((match = liPattern.exec(html)) && people.length < 60) {
            var block = match[0];
            var personMatch = block.match(personPattern);
            var imageMatch = block.match(imagePattern);
            var altMatch = block.match(altPattern);

            if (personMatch && imageMatch && altMatch) {
                people.push({
                    name: decodeHtml(altMatch[1]),
                    image: normalizePersonImageUrl(imageMatch[1].replace(/w66_and_h66_face/g, "w300_and_h450_bestv2")),
                    tmdbPersonId: personMatch[1]
                });
            }
        }

        return people;
    }

    function applyTmdbCastImages(item, tmdbPeople) {
        var sourceCast;
        var castPeople;

        if (!tmdbPeople || !tmdbPeople.length) {
            return;
        }

        item.tmdbImagesLoaded = true;
        sourceCast = item.cast && item.cast.length ? item.cast : tmdbPeople.map(function (person) {
            return person.name;
        });

        castPeople = sourceCast.map(function (name, index) {
            var current = item.castPeople && item.castPeople[index];
            var tmdbPerson = findTmdbPersonByName(name, tmdbPeople) || tmdbPeople[index];

            return {
                name: name || (tmdbPerson && tmdbPerson.name) || "",
                image: (current && current.image) || (tmdbPerson && tmdbPerson.image) || "",
                tmdbPersonId: (current && current.tmdbPersonId) || (tmdbPerson && tmdbPerson.tmdbPersonId) || ""
            };
        }).filter(function (person) {
            return person.name;
        });

        item.castPeople = castPeople;
        item.cast = castPeople.map(function (person) {
            return person.name;
        });
        loadMissingCastPersonImages(item);
        if (state.detailItem === item) {
            renderDetail(item);
        }
    }

    function loadMissingCastPersonImages(item) {
        var missing;
        var active = 0;
        var cursor = 0;
        var maxActive = 3;

        if (!item || !item.castPeople || !item.castPeople.length) {
            if (item && item.cast && item.cast.length) {
                item.castPeople = item.cast.map(function (name) {
                    return { name: name, image: "" };
                });
            }
        }

        if (!item || !item.castPeople || !item.castPeople.length) {
            return;
        }

        missing = item.castPeople.map(function (person, index) {
            return { person: person, index: index };
        }).filter(function (entry) {
            return entry.person && entry.person.name && !entry.person.image && entry.person.name.indexOf("\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631") === -1;
        }).slice(0, 18);

        function pump() {
            while (active < maxActive && cursor < missing.length) {
                (function (entry) {
                    active += 1;
                    cursor += 1;
                    resolveTmdbPersonImage(entry.person.name, function (personData) {
                        active -= 1;
                        if (personData && personData.image && item.castPeople && item.castPeople[entry.index]) {
                            item.castPeople[entry.index].image = personData.image;
                            item.castPeople[entry.index].tmdbPersonId = item.castPeople[entry.index].tmdbPersonId || personData.tmdbPersonId;
                            if (state.detailItem === item) {
                                renderDetail(item);
                            }
                        }
                        pump();
                    });
                }(missing[cursor]));
            }
        }

        pump();
    }

    function resolveTmdbPersonImage(name, callback) {
        var cleanName = String(name || "").trim();
        var cacheKey = normalizeSearchText(cleanName);

        if (!cacheKey) {
            callback(null);
            return;
        }

        if (Object.prototype.hasOwnProperty.call(tmdbPersonImageCache, cacheKey)) {
            callback(tmdbPersonImageCache[cacheKey]);
            return;
        }

        requestExternalText("https://www.themoviedb.org/search/person?query=" + encodeURIComponent(cleanName), function (error, html) {
            var personData = null;

            if (!error) {
                personData = parseTmdbPersonSearch(html, cleanName);
            }

            tmdbPersonImageCache[cacheKey] = personData;
            callback(personData);
        });
    }

    function parseTmdbPersonSearch(html, cleanName) {
        var patterns = [
            /!\[Image\s+\d+:\s*([^\]]+)\]\((https:\/\/media\.themoviedb\.org\/t\/p\/[^)]+)\)\]\(https:\/\/www\.themoviedb\.org\/person\/(\d+)[^)]*\)/g,
            /<a[^>]+href="\/person\/(\d+)[^"]*"[\s\S]{0,800}?<img[^>]+(?:src|data-src)="([^"]+)"[^>]+alt="([^"]*)"/g
        ];
        var normalizedName = normalizeSearchText(cleanName);
        var first = null;
        var i;

        for (i = 0; i < patterns.length; i += 1) {
            var match;
            while ((match = patterns[i].exec(html))) {
                var isMarkdown = i === 0;
                var name = decodeHtml(isMarkdown ? match[1] : match[3]);
                var image = normalizePersonImageUrl((isMarkdown ? match[2] : match[2]).replace(/w66_and_h66_face|w90_and_h90_face/g, "w300_and_h450_bestv2"));
                var id = isMarkdown ? match[3] : match[1];
                var data = { name: name, image: image, tmdbPersonId: id };

                if (!first) {
                    first = data;
                }
                if (normalizeSearchText(name) === normalizedName) {
                    return data;
                }
            }
        }

        return first;
    }

    function findTmdbPersonByName(name, people) {
        var normalizedName = normalizeSearchText(name);

        for (var i = 0; i < people.length; i += 1) {
            var normalizedPerson = normalizeSearchText(people[i].name);
            if (normalizedPerson === normalizedName ||
                    normalizedPerson.indexOf(normalizedName) !== -1 ||
                    normalizedName.indexOf(normalizedPerson) !== -1) {
                return people[i];
            }
        }

        return null;
    }

    function loadTmdbActorWorks(personId, token, callback) {
        var url = "https://www.themoviedb.org/person/" + encodeURIComponent(personId) + "?language=ar";

        requestExternalText(url, function (error, html) {
            if (token !== actorSearchToken) {
                return;
            }

            if (error) {
                console.log("Could not load TMDB actor works:", error.message);
                callback([]);
                return;
            }

            callback(parseTmdbActorTitles(html));
        });
    }

    function parseTmdbActorTitles(html) {
        var titles = [];
        var seen = {};
        var pattern = /<a\s+class="tooltip"[^>]+href="\/(?:movie|tv)\/\d+[^"]*"[^>]*>([\s\S]*?)<\/a>/g;
        var markdownPattern = /\]\(https:\/\/www\.themoviedb\.org\/(?:movie|tv)\/\d+[^)]*\)\s*\n+\s*([^\n]+)/g;
        var match;

        while ((match = pattern.exec(html)) && titles.length < 240) {
            var title = decodeHtml(match[1].replace(/<[^>]+>/g, "")).replace(/\s+/g, " ").trim();

            if (title && !seen[title]) {
                seen[title] = true;
                titles.push(title);
            }
        }

        while ((match = markdownPattern.exec(html)) && titles.length < 240) {
            var markdownTitle = decodeHtml(match[1].replace(/\s+/g, " ").trim());

            if (markdownTitle && !seen[markdownTitle] && markdownTitle.length < 120) {
                seen[markdownTitle] = true;
                titles.push(markdownTitle);
            }
        }

        return titles;
    }

    function normalizeXtreamEpisodes(episodes, parentItem) {
        var config = getXtreamConfig();
        var seasons = [];

        if (!episodes || !config) {
            return [];
        }

        Object.keys(episodes).sort(numberSort).forEach(function (seasonNumber) {
            var seasonEpisodes = episodes[seasonNumber] || [];

            seasons.push({
                number: seasonNumber,
                episodes: seasonEpisodes.map(function (episode, index) {
                    var episodeInfo = episode.info || {};
                    var extension = normalizeXtreamContainerExtension(
                        episode.container_extension ||
                        episode.containerExtension ||
                        episodeInfo.container_extension ||
                        episodeInfo.containerExtension ||
                        parentItem.containerExtension
                    );
                    return {
                        title: episode.title || parentItem.title + " S" + seasonNumber + " E" + (episode.episode_num || index + 1),
                        group: parentItem.group,
                        section: "series",
                        description: parentItem.description,
                        logo: episodeInfo.movie_image || parentItem.logo,
                        year: parentItem.year,
                        quality: parentItem.quality,
                        duration: episodeInfo.duration || parentItem.duration,
                        addedAt: parentItem.addedAt,
                        rating: parentItem.rating,
                        plot: episodeInfo.plot || parentItem.plot,
                        cast: parentItem.cast,
                        subtitles: normalizeSubtitleList([
                            episode.subtitles,
                            episode.subtitle,
                            episode.subtitle_url,
                            episode.subtitles_url,
                            episode.subtitles_urls,
                            episode.external_subtitles,
                            episode.vtt,
                            episode.srt,
                            episodeInfo.subtitles,
                            episodeInfo.subtitle,
                            episodeInfo.subtitle_url,
                            episodeInfo.subtitles_url,
                            episodeInfo.subtitles_urls,
                            episodeInfo.external_subtitles,
                            episodeInfo.vtt,
                            episodeInfo.srt
                        ], parentItem),
                        seriesTitle: parentItem.seriesTitle || parentItem.title,
                        seasonNumber: parseInt(seasonNumber, 10),
                        episodeNumber: parseInt(episode.episode_num || index + 1, 10),
                        seriesXtreamId: parentItem.xtreamId,
                        xtreamId: episode.id,
                        containerExtension: extension,
                        type: getMediaTypeFromExtension(extension),
                        url: config.baseUrl + "/series/" + encodeURIComponent(config.username) + "/" + encodeURIComponent(config.password) + "/" + episode.id + "." + extension
                    };
                })
            });
        });

        return seasons;
    }

    function mergeEpisodeMetadataFromSeriesInfo(item) {
        var matchedEpisode = null;

        if (!item || !item.seriesXtreamId || !item.seriesSeasons || !item.seriesSeasons.length) {
            return;
        }

        item.seriesSeasons.some(function (season) {
            return season.episodes.some(function (episode) {
                if (String(episode.xtreamId) === String(item.xtreamId)) {
                    matchedEpisode = episode;
                    return true;
                }
                return false;
            });
        });

        if (!matchedEpisode) {
            return;
        }

        item.subtitles = mergeSubtitleLists(item.subtitles, matchedEpisode.subtitles);
        item.plot = matchedEpisode.plot || item.plot;
        item.logo = matchedEpisode.logo || item.logo;
        item.duration = matchedEpisode.duration || item.duration;
    }

    function parseMovieProperties(value) {
        var parsed = value;

        if (!parsed) {
            return null;
        }

        if (typeof parsed === "string") {
            try {
                return JSON.parse(parsed);
            } catch (error) {
                return null;
            }
        }

        if (typeof parsed === "object") {
            return parsed;
        }

        return null;
    }

    function isSubtitleUrl(value) {
        var text = String(value || "").trim();

        if (!text) {
            return false;
        }

        return /^https?:\/\//i.test(text) ||
            /^\/[^\s]+$/.test(text) ||
            /\.(srt|vtt|ass|ssa|sub)(?:$|\?)/i.test(text);
    }

    function buildXtreamSubtitleProbeList(item) {
        var config = getXtreamConfigForItem(item) || getXtreamConfig();
        var streamId = item && (item.xtreamId || getXtreamStreamId(item.url || ""));
        var section = item && item.section;
        var user;
        var pass;
        var base;
        var probes = [];
        var langSuffixes = ["", "ar", "en"];

        if (!config || !streamId || (section !== "movies" && section !== "series")) {
            return [];
        }

        user = encodeURIComponent(config.username);
        pass = encodeURIComponent(config.password);
        base = String(config.baseUrl || "").replace(/\/+$/, "");

        function addProbe(folder, labelPrefix) {
            langSuffixes.forEach(function (suffix) {
                var nameSuffix = suffix ? "." + suffix : "";
                probes.push({
                    url: base + "/" + folder + "/" + user + "/" + pass + "/" + streamId + nameSuffix + ".srt",
                    label: (labelPrefix || "SRT") + (suffix ? " " + suffix.toUpperCase() : ""),
                    language: guessSubtitleLanguage(suffix)
                });
                probes.push({
                    url: base + "/" + folder + "/" + user + "/" + pass + "/" + streamId + nameSuffix + ".vtt",
                    label: (labelPrefix || "VTT") + (suffix ? " " + suffix.toUpperCase() : ""),
                    language: guessSubtitleLanguage(suffix)
                });
            });
            probes.push({
                url: base + "/subtitles/" + user + "/" + pass + "/" + streamId + ".srt",
                label: "Server SRT",
                language: ""
            });
            probes.push({
                url: base + "/subtitles/" + user + "/" + pass + "/" + streamId + ".vtt",
                label: "Server VTT",
                language: ""
            });
        }

        if (section === "movies") {
            addProbe("movie", "Movie");
        } else {
            addProbe("series", "Episode");
        }

        return probes;
    }

    function collectSubtitleSources(value, output, depth) {
        var keys;

        if (!value || !output || depth > 6) {
            return;
        }

        if (Array.isArray(value)) {
            value.forEach(function (entry) {
                collectSubtitleSources(entry, output, (depth || 0) + 1);
            });
            return;
        }

        if (typeof value !== "object") {
            return;
        }

        keys = Object.keys(value);
        keys.forEach(function (key) {
            var entry = value[key];
            var normalizedKey = String(key || "").toLowerCase();
            var isSubtitleKey = /sub|caption|vtt|srt|webvtt|track|cc/.test(normalizedKey);

            if (isSubtitleKey) {
                output.push(entry);
                return;
            }

            if (entry && typeof entry === "object") {
                collectSubtitleSources(entry, output, (depth || 0) + 1);
            }
        });
    }

    function normalizeSubtitleList(value, item) {
        var list = [];
        var parsed = [];

        if (!parsed) {
            return [];
        }

        collectSubtitleEntries(value, parsed);

        parsed.forEach(function (entry, index) {
            var url = typeof entry === "string" ? entry : (entry.url || entry.file || entry.src || entry.link || entry.path ||
                entry.subtitle || entry.subtitle_url || entry.subtitles_url || entry.subtitle_path || entry.file_path ||
                entry.download_url || entry.external_link || entry.stream || "");
            var label = typeof entry === "string" ? "" : (entry.label || entry.name || entry.title || entry.lang || entry.language || entry.code || "");

            if (typeof entry === "object" && entry && !url && entry.server && entry.path) {
                url = String(entry.server).replace(/\/+$/, "") + "/" + String(entry.path).replace(/^\/+/, "");
            }

            url = resolveSubtitleUrl(url, item);
            if (!url) {
                return;
            }

            list.push({
                url: url,
                label: label || guessSubtitleLabel(url, index),
                language: guessSubtitleLanguage(label || url, item)
            });
        });

        return mergeSubtitleLists([], list);
    }

    function collectSubtitleEntries(value, output) {
        var parsed = value;

        if (!parsed) {
            return;
        }

        if (Array.isArray(parsed)) {
            parsed.forEach(function (entry) {
                collectSubtitleEntries(entry, output);
            });
            return;
        }

        if (typeof parsed === "string") {
            parsed = parsed.trim();
            if (!parsed) {
                return;
            }
            if (parsed.charAt(0) === "[" || parsed.charAt(0) === "{") {
                try {
                    collectSubtitleEntries(JSON.parse(parsed), output);
                    return;
                } catch (error) {
                    // Treat malformed JSON as a plain URL list below.
                }
            }
            parsed.split(/[\n,|]+/).forEach(function (entry) {
                if (entry.trim()) {
                    output.push(entry.trim());
                }
            });
            return;
        }

        Object.keys(parsed).forEach(function (key) {
            var entry = parsed[key];
            if (typeof entry === "string") {
                if (isSubtitleUrl(entry)) {
                    output.push({ label: key, url: entry });
                }
                return;
            }
            if (entry && typeof entry === "object") {
                if (Array.isArray(entry) || (!entry.url && !entry.file && !entry.src && !entry.link && !entry.path &&
                        !entry.subtitle && !entry.subtitle_url)) {
                    collectSubtitleEntries(entry, output);
                    return;
                }
                entry.label = entry.label || entry.name || key;
                output.push(entry);
            }
        });
    }

    function resolveSubtitleUrl(url, item) {
        var cleanUrl = String(url || "").trim();
        var config;

        if (!cleanUrl) {
            return "";
        }

        cleanUrl = cleanUrl.replace(/\\\//g, "/").replace(/\s/g, "%20");
        if (/^https?:\/\//i.test(cleanUrl)) {
            return cleanUrl;
        }

        config = getXtreamConfigForItem(item) || getXtreamConfig();
        if (cleanUrl.charAt(0) === "/" && config) {
            return config.baseUrl + cleanUrl;
        }

        if (item && item.url && /^https?:\/\//i.test(item.url)) {
            return item.url.replace(/\/[^/?#]*(?:[?#].*)?$/, "/") + cleanUrl.replace(/^\/+/, "");
        }

        if (config && config.baseUrl) {
            return config.baseUrl + "/" + cleanUrl.replace(/^\/+/, "");
        }

        return "";
    }

    function mergeSubtitleLists(existing, incoming) {
        var seen = {};
        var merged = [];

        (existing || []).concat(incoming || []).forEach(function (subtitle) {
            var key = subtitle && subtitle.url;
            if (!key || seen[key]) {
                return;
            }
            seen[key] = true;
            merged.push(subtitle);
        });

        return merged;
    }

    function guessSubtitleLabel(url, index) {
        var file = decodeURIComponent(String(url).split("?")[0].split("/").pop() || "");
        return file || ("Subtitle " + (index + 1));
    }

    function guessSubtitleLanguage(value) {
        var text = String(value || "").toLowerCase();
        if (text.indexOf("arab") !== -1 || text.indexOf(".ar.") !== -1 || text.indexOf("_ar") !== -1) {
            return "ar";
        }
        if (text.indexOf("eng") !== -1 || text.indexOf(".en.") !== -1 || text.indexOf("_en") !== -1) {
            return "en";
        }
        if (text.indexOf("tur") !== -1 || text.indexOf(".tr.") !== -1 || text.indexOf("_tr") !== -1) {
            return "tr";
        }
        return "";
    }

    function prepareSubtitleTracks(item) {
        clearSubtitleTrackElements();

        if (!item) {
            return;
        }

        if (!item.subtitles || !item.subtitles.length) {
            console.log("No external subtitles found for item:", item.title);
            discoverSidecarSubtitles(item);
            discoverHlsManifestSubtitles(item);
            scheduleEmbeddedSubtitleScans();
            return;
        }

        console.log("External subtitles found:", item.title, item.subtitles.length);
        item.subtitles.forEach(function (subtitle, index) {
            addSubtitleTrack(subtitle, index);
        });
        discoverHlsManifestSubtitles(item);
        scheduleEmbeddedSubtitleScans();
    }

    function scheduleEmbeddedSubtitleScans() {
        var delays = [400, 1200, 2500, 5000, 8000];

        delays.forEach(function (delay) {
            setTimeout(refreshPlayerOptions, delay);
        });

        if (elements.video.textTracks) {
            elements.video.textTracks.addEventListener("addtrack", refreshPlayerOptions);
        }
    }

    function discoverHlsManifestSubtitles(item) {
        var url = item && item.url ? String(item.url) : "";

        if (!item || item.hlsSubtitleDiscoveryStarted || url.toLowerCase().split("?")[0].indexOf(".m3u8") === -1) {
            return;
        }

        item.hlsSubtitleDiscoveryStarted = true;
        requestExternalText(url, function (error, manifest) {
            var entries;
            var pending;
            var found = [];

            if (error || !manifest) {
                return;
            }

            entries = parseHlsSubtitleEntries(manifest, url);
            pending = entries.length;
            if (!pending) {
                return;
            }

            entries.forEach(function (entry, index) {
                loadHlsSubtitleEntry(entry, index, function (subtitle) {
                    pending -= 1;
                    if (subtitle) {
                        found.push(subtitle);
                        appendSubtitleTrack(subtitle.url, subtitle, found.length - 1, subtitle.blobUrl || "", subtitle.cueText || "");
                    }
                    if (!pending && found.length) {
                        item.subtitles = mergeSubtitleLists(item.subtitles, found);
                        refreshPlayerOptions();
                    }
                });
            });
        });
    }

    function parseHlsSubtitleEntries(manifest, baseUrl) {
        var entries = [];
        var lines = String(manifest || "").split(/\r?\n/);

        lines.forEach(function (line, index) {
            var attrs;
            var uri;

            if (line.indexOf("#EXT-X-MEDIA:") !== 0 || line.indexOf("TYPE=SUBTITLES") === -1) {
                return;
            }

            attrs = parseHlsAttributeList(line.slice(line.indexOf(":") + 1));
            uri = attrs.URI || attrs.Uri || attrs.uri;
            if (!uri) {
                return;
            }

            entries.push({
                url: resolveRelativeUrl(uri, baseUrl),
                label: attrs.NAME || attrs.LANGUAGE || ("Subtitle " + (index + 1)),
                language: attrs.LANGUAGE || guessSubtitleLanguage(attrs.NAME || uri)
            });
        });

        return entries;
    }

    function parseHlsAttributeList(value) {
        var attrs = {};
        var pattern = /([A-Z0-9-]+)=("(?:[^"\\]|\\.)*"|[^,]*)/gi;
        var match;

        while ((match = pattern.exec(value))) {
            attrs[match[1]] = String(match[2] || "").replace(/^"|"$/g, "");
        }

        return attrs;
    }

    function loadHlsSubtitleEntry(entry, index, callback) {
        var lowerUrl = entry.url.toLowerCase().split("?")[0];

        if (lowerUrl.lastIndexOf(".m3u8") === -1) {
            requestExternalText(entry.url, function (error, text) {
                var cueText;
                var blobUrl;

                if (error || !looksLikeSubtitleText(text)) {
                    callback(entry);
                    return;
                }

                cueText = lowerUrl.lastIndexOf(".srt") !== -1 ? convertSrtToVtt(text) : normalizeVttText(text);
                blobUrl = URL.createObjectURL(new Blob([cueText], { type: "text/vtt" }));
                callback({
                    url: blobUrl,
                    blobUrl: blobUrl,
                    cueText: cueText,
                    label: entry.label || ("Subtitle " + (index + 1)),
                    language: entry.language || guessSubtitleLanguage(entry.label || entry.url)
                });
            });
            return;
        }

        requestExternalText(entry.url, function (error, playlist) {
            var segments;

            if (error || !playlist) {
                callback(null);
                return;
            }

            segments = parseHlsSubtitleSegments(playlist, entry.url);
            if (!segments.length) {
                callback(null);
                return;
            }

            fetchSubtitleSegmentsAsVtt(segments, function (cueText, blobUrl) {
                if (!blobUrl) {
                    callback(null);
                    return;
                }

                callback({
                    url: blobUrl,
                    blobUrl: blobUrl,
                    cueText: cueText,
                    label: entry.label || ("Subtitle " + (index + 1)),
                    language: entry.language || guessSubtitleLanguage(entry.label || entry.url)
                });
            });
        });
    }

    function parseHlsSubtitleSegments(playlist, baseUrl) {
        var segments = [];

        String(playlist || "").split(/\r?\n/).forEach(function (line) {
            var clean = line.trim();
            if (!clean || clean.charAt(0) === "#") {
                return;
            }
            if (/\.(vtt|webvtt|srt)(?:\?|$)/i.test(clean)) {
                segments.push(resolveRelativeUrl(clean, baseUrl));
            }
        });

        return segments.slice(0, 400);
    }

    function fetchSubtitleSegmentsAsVtt(segments, callback) {
        var pending = segments.length;
        var parts = [];

        if (!pending) {
            callback("");
            return;
        }

        segments.forEach(function (url, index) {
            requestExternalText(url, function (error, text) {
                pending -= 1;
                if (!error && looksLikeSubtitleText(text)) {
                    parts[index] = normalizeSubtitleSegmentText(text);
                }
                if (!pending) {
                    parts = parts.filter(Boolean);
                    var cueText = parts.length ? "WEBVTT\n\n" + parts.join("\n\n") : "";
                    callback(cueText, cueText ? URL.createObjectURL(new Blob([cueText], { type: "text/vtt" })) : "");
                }
            });
        });
    }

    function normalizeSubtitleSegmentText(text) {
        return String(text || "")
            .replace(/^\uFEFF/, "")
            .replace(/^WEBVTT[^\n]*(?:\n|$)/i, "")
            .replace(/^X-TIMESTAMP-MAP=.*(?:\n|$)/gim, "")
            .replace(/\r+/g, "")
            .trim();
    }

    function resolveRelativeUrl(url, baseUrl) {
        try {
            return new URL(url, baseUrl).toString();
        } catch (error) {
            return url;
        }
    }

    function discoverSidecarSubtitles(item) {
        var candidates = getSidecarSubtitleCandidates(item).concat(buildXtreamSubtitleProbeList(item));
        var found = [];
        var pending = candidates.length;
        var seen = {};

        if (!item || !candidates.length) {
            return;
        }

        candidates = candidates.filter(function (subtitle) {
            var key = subtitle && subtitle.url;
            if (!key || seen[key]) {
                return false;
            }
            seen[key] = true;
            return true;
        });
        pending = candidates.length;

        candidates.forEach(function (subtitle) {
            requestExternalText(subtitle.url, function (error, text) {
                pending -= 1;
                if (!error && looksLikeSubtitleText(text)) {
                    var blobUrl;
                    var vttText;
                    found.push(subtitle);
                    if (subtitle.url.toLowerCase().split("?")[0].lastIndexOf(".srt") !== -1) {
                        vttText = convertSrtToVtt(text);
                    } else {
                        vttText = normalizeVttText(text);
                    }
                    blobUrl = URL.createObjectURL(new Blob([vttText], { type: "text/vtt" }));
                    appendSubtitleTrack(blobUrl, subtitle, found.length - 1, blobUrl, vttText);
                }
                if (!pending && found.length) {
                    item.subtitles = mergeSubtitleLists(item.subtitles, found);
                    refreshPlayerOptions();
                }
            });
        });
    }

    function getSidecarSubtitleCandidates(item) {
        var url = item && item.url ? String(item.url) : "";
        var clean;
        var base;

        if (!/^https?:\/\//i.test(url) || url.indexOf(".m3u8") !== -1) {
            return [];
        }

        clean = url.split("?")[0];
        base = clean.replace(/\.[^/.]+$/, "");
        if (base === clean) {
            return [];
        }

        return [
            { url: base + ".vtt", label: "VTT", language: "" },
            { url: base + ".srt", label: "SRT", language: "" },
            { url: base + ".ar.vtt", label: "Arabic VTT", language: "ar" },
            { url: base + ".ar.srt", label: "Arabic SRT", language: "ar" }
        ];
    }

    function looksLikeSubtitleText(text) {
        var value = String(text || "").slice(0, 2000);
        return value.indexOf("WEBVTT") !== -1 || /\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s+-->\s+\d{1,2}:\d{2}:\d{2}[,.]\d{3}/.test(value);
    }

    function parseSubtitleTime(value) {
        var parts = String(value || "").trim().replace(",", ".").split(":");
        var seconds;
        var minutes;
        var hours;

        if (parts.length < 2) {
            return NaN;
        }

        seconds = parseFloat(parts.pop());
        minutes = parseInt(parts.pop(), 10);
        hours = parts.length ? parseInt(parts.pop(), 10) : 0;

        if (!isFinite(seconds) || !isFinite(minutes) || !isFinite(hours)) {
            return NaN;
        }

        return (hours * 3600) + (minutes * 60) + seconds;
    }

    function cleanSubtitleCueText(text) {
        return String(text || "")
            .replace(/<br\s*\/?>/gi, "\n")
            .replace(/<\/?[^>]+>/g, "")
            .replace(/\{\\[^}]*\}/g, "")
            .replace(/\{[^}]*\}/g, "")
            .replace(/\n{3,}/g, "\n\n")
            .trim();
    }

    function parseSubtitleCues(text) {
        var blocks = String(text || "")
            .replace(/^\uFEFF/, "")
            .replace(/\r+/g, "")
            .split(/\n{2,}/);
        var cues = [];

        blocks.forEach(function (block) {
            var lines = block.split("\n").filter(function (line) {
                return line.trim();
            });
            var timingIndex = -1;
            var timing;
            var parts;
            var start;
            var end;
            var cueText;
            var i;

            if (!lines.length || /^WEBVTT/i.test(lines[0]) || /^NOTE\b/i.test(lines[0]) ||
                    /^STYLE\b/i.test(lines[0]) || /^REGION\b/i.test(lines[0])) {
                return;
            }

            for (i = 0; i < lines.length; i += 1) {
                if (lines[i].indexOf("-->") !== -1) {
                    timingIndex = i;
                    break;
                }
            }

            if (timingIndex === -1) {
                return;
            }

            timing = lines[timingIndex];
            parts = timing.split("-->");
            if (parts.length < 2) {
                return;
            }

            start = parseSubtitleTime(parts[0]);
            end = parseSubtitleTime(String(parts[1]).trim().split(/\s+/)[0]);
            cueText = cleanSubtitleCueText(lines.slice(timingIndex + 1).join("\n"));

            if (!isFinite(start) || !isFinite(end) || end <= start || !cueText) {
                return;
            }

            cues.push({
                start: start,
                end: end,
                text: cueText
            });
        });

        cues.sort(function (a, b) {
            return a.start - b.start;
        });

        return cues;
    }

    function createSubtitleCueTrack(subtitle, cueText) {
        var cueKey = "subtitle-" + (++subtitleCueKeyCounter);
        var cues = parseSubtitleCues(cueText);

        if (!cues.length) {
            return "";
        }

        state.subtitleCueTracks[cueKey] = {
            cues: cues,
            label: subtitle && subtitle.label ? subtitle.label : "",
            url: subtitle && subtitle.url ? subtitle.url : ""
        };

        return cueKey;
    }

    function clearManualSubtitleOverlay() {
        state.activeSubtitleCueKey = "";
        state.activeSubtitleCues = [];
        state.lastSubtitleText = "";
        if (elements.subtitleOverlay) {
            elements.subtitleOverlay.textContent = "";
            elements.subtitleOverlay.hidden = true;
        }
    }

    function getTrackElementForTextTrackIndex(index) {
        var textTrack = elements.video && elements.video.textTracks ? elements.video.textTracks[index] : null;
        var tracks;
        var i;

        if (!textTrack) {
            return null;
        }

        tracks = elements.video.querySelectorAll("track");
        for (i = 0; i < tracks.length; i += 1) {
            if (tracks[i].track === textTrack) {
                return tracks[i];
            }
        }

        return null;
    }

    function getCueKeyForSubtitleSelection(selected) {
        var tracks = player.getSubtitleTracks();
        var selectedText = String(selected);
        var i;
        var track;
        var trackElement;

        for (i = 0; i < tracks.length; i += 1) {
            track = tracks[i];
            if (String(track.id) !== selectedText) {
                continue;
            }

            if (track.cueKey) {
                return track.cueKey;
            }

            if (typeof track.textTrackIndex === "number") {
                trackElement = getTrackElementForTextTrackIndex(track.textTrackIndex);
                return trackElement && trackElement.dataset ? (trackElement.dataset.cueKey || "") : "";
            }
        }

        return "";
    }

    function activateManualSubtitleForSelection(selected) {
        var cueKey;
        var cueTrack;

        if (selected < 0 || !state.playingItem) {
            clearManualSubtitleOverlay();
            return;
        }

        cueKey = getCueKeyForSubtitleSelection(selected);
        cueTrack = cueKey ? state.subtitleCueTracks[cueKey] : null;

        if (!cueTrack || !cueTrack.cues || !cueTrack.cues.length) {
            clearManualSubtitleOverlay();
            return;
        }

        state.activeSubtitleCueKey = cueKey;
        state.activeSubtitleCues = cueTrack.cues;
        state.lastSubtitleText = "";
        updateSubtitleOverlay();
    }

    function findActiveSubtitleText(cues, time) {
        var text = [];
        var i;

        for (i = 0; i < cues.length; i += 1) {
            if (cues[i].end < time) {
                continue;
            }
            if (cues[i].start > time) {
                break;
            }
            text.push(cues[i].text);
        }

        return text.join("\n");
    }

    function updateSubtitleOverlay() {
        var cues = state.activeSubtitleCues || [];
        var time;
        var text;

        if (!elements.subtitleOverlay || state.screen !== "player" || !cues.length) {
            if (elements.subtitleOverlay) {
                elements.subtitleOverlay.hidden = true;
            }
            return;
        }

        time = player && player.getCurrentTime ? player.getCurrentTime() : (elements.video.currentTime || 0);
        time -= state.subtitleDelay || 0;
        text = findActiveSubtitleText(cues, Math.max(0, time));

        if (text === state.lastSubtitleText) {
            return;
        }

        state.lastSubtitleText = text;
        elements.subtitleOverlay.textContent = text;
        elements.subtitleOverlay.hidden = !text;
    }

    function startSubtitleOverlayTimer() {
        stopSubtitleOverlayTimer();
        state.subtitleOverlayTimer = setInterval(updateSubtitleOverlay, 250);
    }

    function stopSubtitleOverlayTimer() {
        if (state.subtitleOverlayTimer) {
            clearInterval(state.subtitleOverlayTimer);
            state.subtitleOverlayTimer = null;
        }
    }

    function clearSubtitleTrackElements() {
        Array.prototype.slice.call(elements.video.querySelectorAll("track[data-app-subtitle='1']")).forEach(function (track) {
            if (track.dataset.cueKey) {
                delete state.subtitleCueTracks[track.dataset.cueKey];
            }
            if (track.dataset.blobUrl) {
                URL.revokeObjectURL(track.dataset.blobUrl);
            }
            track.parentNode.removeChild(track);
        });
        clearManualSubtitleOverlay();
    }

    function addSubtitleTrack(subtitle, index) {
        var lowerUrl = subtitle.url.split("?")[0].toLowerCase();
        var isSrt = lowerUrl.indexOf(".srt") !== -1;

        function attachLoadedText(text) {
            var vttText = isSrt ? convertSrtToVtt(text) : normalizeVttText(text);
            var blobUrl = URL.createObjectURL(new Blob([vttText], { type: "text/vtt" }));
            appendSubtitleTrack(blobUrl, subtitle, index, blobUrl, vttText);
        }

        requestTextWithTimeout(subtitle.url, 12000, function (error, text) {
            if (!error && looksLikeSubtitleText(text)) {
                attachLoadedText(text);
                return;
            }

            requestExternalText(subtitle.url, function (proxyError, proxyText) {
                if (proxyError || !looksLikeSubtitleText(proxyText)) {
                    console.log("Could not load subtitle track:", subtitle.url, proxyError ? proxyError.message : "invalid subtitle text");
                    return;
                }
                attachLoadedText(proxyText);
            });
        });
    }

    function normalizeVttText(text) {
        var value = String(text || "").replace(/^\uFEFF/, "");
        if (value.indexOf("WEBVTT") === 0) {
            return value;
        }
        return "WEBVTT\n\n" + value;
    }

    function appendSubtitleTrack(src, subtitle, index, blobUrl, cueText) {
        var track = document.createElement("track");
        var cueKey = cueText ? createSubtitleCueTrack(subtitle, cueText) : "";

        track.kind = "subtitles";
        track.label = subtitle.label || ("Subtitle " + (index + 1));
        track.srclang = subtitle.language || "und";
        track.src = src;
        track.dataset.appSubtitle = "1";
        if (cueKey) {
            track.dataset.cueKey = cueKey;
        }
        if (blobUrl) {
            track.dataset.blobUrl = blobUrl;
        }
        track.addEventListener("load", refreshPlayerOptions);
        track.addEventListener("error", function () {
            console.log("Subtitle track failed to load:", src);
        });
        elements.video.appendChild(track);
        try {
            track.track.mode = "hidden";
        } catch (error) {}
        setTimeout(refreshPlayerOptions, 250);
    }

    function convertSrtToVtt(text) {
        return "WEBVTT\n\n" + String(text || "")
            .replace(/\r+/g, "")
            .replace(/^\uFEFF/, "")
            .replace(/(\d{2}:\d{2}:\d{2}),(\d{3})/g, "$1.$2");
    }

    function playItem(item, returnScreen, resumeChoice) {
        var progress = getSavedPlaybackProgress(item);

        if (resumeChoice !== "continue" && resumeChoice !== "restart" && progress && progress.time > 30) {
            showResumeDialog(item, returnScreen, progress);
            return;
        }

        state.screen = "player";
        state.returnScreen = returnScreen;
        state.playingItem = item;
        state.subtitleSelectionLocked = false;
        state.subtitleDelay = 0;
        showScreen("player");
        setFocusedControl("play");
        showOverlay();
        startSubtitleOverlayTimer();

        ensureSeriesPlaybackContext(item, state.detailItem);
        startPlaybackWithSubtitles(item, resumeChoice === "continue" ? progress.time : 0);
        loadPlaybackSubtitlesInBackground(item);
    }

    function loadPlaybackSubtitlesInBackground(item) {
        if (!needsSubtitleMetadata(item) && !needsRemoteSubtitles(item)) {
            return;
        }

        ensurePlaybackSubtitles(item, function () {
            applyLoadedSubtitlesToPlayer(item);
        });
    }

    function showResumeDialog(item, returnScreen, progress) {
        state.pendingResume = { item: item, returnScreen: returnScreen, progress: progress };
        elements.resumeDialogText.textContent = "\u062a\u0648\u0642\u0641\u062a \u0639\u0646\u062f " + formatTimeLabel(progress.time) + ". \u0647\u0644 \u062a\u0631\u064a\u062f \u0627\u0644\u0645\u062a\u0627\u0628\u0639\u0629\u061f";
        elements.resumeDialog.hidden = false;
        elements.resumeContinueButton.focus();
    }

    function runPendingResume(shouldContinue) {
        var pending = state.pendingResume;

        if (!pending) {
            return;
        }

        elements.resumeDialog.hidden = true;
        state.pendingResume = null;
        playItem(pending.item, pending.returnScreen, shouldContinue ? "continue" : "restart");
    }

    function startPlaybackWithSubtitles(item, resumeTime) {
        player.load(item);
        prepareSubtitleTracks(item);
        if (resumeTime > 0) {
            seekAfterMetadata(resumeTime);
        } else {
            clearPlaybackProgress(item);
        }
        setTimeout(refreshPlayerOptions, 250);
        setTimeout(refreshPlayerOptions, 900);
        setTimeout(refreshPlayerOptions, 1800);
        setTimeout(refreshPlayerOptions, 3500);
        setTimeout(refreshPlayerOptions, 6000);
    }

    function seekAfterMetadata(seconds) {
        var applied = false;
        function applySeek() {
            var duration = player.getDuration ? player.getDuration() : elements.video.duration;
            if (applied || !isFinite(duration) || duration <= 0) {
                return;
            }
            applied = true;
            if (player.seekTo) {
                player.seekTo(Math.min(seconds, Math.max(0, duration - 10)));
            } else {
                elements.video.currentTime = Math.min(seconds, Math.max(0, duration - 10));
            }
        }
        elements.video.addEventListener("loadedmetadata", applySeek, { once: true });
        setTimeout(applySeek, 1200);
        setTimeout(applySeek, 2500);
    }

    function getPlaybackProgressKey(item) {
        return "nexoProgress:" + getLibraryWorkKey(item);
    }

    function getSavedPlaybackProgress(item) {
        try {
            return JSON.parse(window.localStorage.getItem(getPlaybackProgressKey(item)) || "null");
        } catch (error) {
            return null;
        }
    }

    function clearPlaybackProgress(item) {
        try {
            window.localStorage.removeItem(getPlaybackProgressKey(item));
        } catch (error) {
            console.log("Could not clear playback progress:", error.message);
        }
    }

    function savePlaybackProgress() {
        var item = state.playingItem;
        var current = player.getCurrentTime ? player.getCurrentTime() : (elements.video.currentTime || 0);
        var duration = player.getDuration ? player.getDuration() : (elements.video.duration || 0);
        var now = Date.now();

        if (!item || now - state.lastProgressSaveAt < 4000) {
            return;
        }

        state.lastProgressSaveAt = now;
        if (duration && isFinite(duration) && current > duration - 20) {
            clearPlaybackProgress(item);
            return;
        }

        if (current < 15) {
            return;
        }

        try {
            window.localStorage.setItem(getPlaybackProgressKey(item), JSON.stringify({
                time: Math.floor(current),
                duration: isFinite(duration) ? Math.floor(duration) : 0,
                savedAt: now
            }));
        } catch (error) {
            console.log("Could not save playback progress:", error.message);
        }
    }

    function formatTimeLabel(seconds) {
        var total = Math.max(0, Math.floor(seconds || 0));
        var hours = Math.floor(total / 3600);
        var minutes = Math.floor((total % 3600) / 60);
        var secs = total % 60;

        if (hours) {
            return hours + ":" + pad2(minutes) + ":" + pad2(secs);
        }
        return minutes + ":" + pad2(secs);
    }

    function pad2(value) {
        return value < 10 ? "0" + value : String(value);
    }

    function refreshPlayerOptions() {
        populateAudioTracks();
        populateSubtitleTracks();
        refreshOpenPlayerTool();
        autoSelectFirstSubtitleTrack();
        applySubtitleStyle();
        applyScreenMode();
    }

    function populateAudioTracks() {
        var tracks = player.getAudioTracks();
        var options = '<option value="-1">الصوت الافتراضي</option>';

        for (var i = 0; i < tracks.length; i += 1) {
            options += '<option value="' + tracks[i].id + '"' + (tracks[i].selected ? " selected" : "") + '>' + escapeHtml(tracks[i].label) + '</option>';
        }

        elements.audioLanguageSelect.innerHTML = options;
    }

    function populateSubtitleTracks() {
        var tracks = player.getSubtitleTracks();
        var options = '<option value="-1">بدون ترجمة</option>';

        for (var i = 0; i < tracks.length; i += 1) {
            options += '<option value="' + tracks[i].id + '"' + (tracks[i].selected ? " selected" : "") + '>' + escapeHtml(tracks[i].label) + '</option>';
        }

        elements.subtitleSelect.innerHTML = options;
    }

    function populateAudioTracks() {
        var tracks = player.getAudioTracks();
        var labels = getLanguageLabels(state.language);
        var options = '<option value="-1">' + escapeHtml(labels.defaultAudio) + '</option>';

        for (var i = 0; i < tracks.length; i += 1) {
            options += '<option value="' + tracks[i].id + '"' + (tracks[i].selected ? " selected" : "") + '>' + escapeHtml(tracks[i].label) + '</option>';
        }

        elements.audioLanguageSelect.innerHTML = options;
    }

    function populateSubtitleTracks() {
        var tracks = player.getSubtitleTracks();
        var labels = getLanguageLabels(state.language);
        var options = '<option value="-1">' + escapeHtml(labels.noSubtitle) + '</option>';
        var hasTracks = tracks.length > 0;

        for (var i = 0; i < tracks.length; i += 1) {
            options += '<option value="' + tracks[i].id + '"' + (tracks[i].selected ? " selected" : "") + '>' + escapeHtml(tracks[i].label) + '</option>';
        }

        elements.subtitleSelect.innerHTML = options;
        elements.subtitleSelect.disabled = !hasTracks;
        elements.subtitleToolButton.hidden = !hasTracks;
        if (!hasTracks && state.activePlayerTool === "subtitles") {
            closePlayerToolPanel();
        }
    }

    function openPlayerTool(toolName) {
        if (toolName === "favorite") {
            togglePlayingFavorite();
            setFocusedControl("favorite");
            return;
        }
        state.playerToolOptionIndex = -1;
        state.activePlayerTool = state.activePlayerTool === toolName && !elements.playerToolPanel.hidden ? "" : toolName;
        renderPlayerToolPanel();
        setFocusedControl(toolName || "play");
    }

    function refreshOpenPlayerTool() {
        if (state.activePlayerTool && !elements.playerToolPanel.hidden) {
            renderPlayerToolPanel();
        }
    }

    function renderPlayerToolPanel() {
        var labels = getLanguageLabels(state.language);
        var tool = state.activePlayerTool;

        elements.playerToolPanel.hidden = !tool;
        elements.subtitleToolButton.classList.toggle("is-open", tool === "subtitles");
        elements.subtitleSettingsToolButton.classList.toggle("is-open", tool === "subtitle-settings");
        elements.screenToolButton.classList.toggle("is-open", tool === "screen");
        elements.favoriteToolButton.classList.remove("is-open");

        if (!tool) {
            elements.playerToolOptions.innerHTML = "";
            return;
        }

        if (tool === "subtitles") {
            renderSubtitleTool(labels);
        } else if (tool === "subtitle-settings") {
            renderSubtitleSettingsTool(labels);
        } else {
            renderScreenTool(labels);
        }
        syncPlayerToolOptionFocus();
    }

    function renderSubtitleTool(labels) {
        populateSubtitleTracks();
        var tracks = player.getSubtitleTracks();
        var current = elements.subtitleSelect.value || "-1";
        var options = [{ id: "-1", label: labels.noSubtitle, type: "subtitle" }].concat(tracks.map(function (track) {
            return { id: String(track.id), label: track.label, type: "subtitle" };
        }));

        elements.playerToolTitle.textContent = labels.subtitleTitle;
        elements.playerToolOptions.className = "player-tool-options";
        elements.playerToolOptions.innerHTML = options.map(function (option) {
            return '<button class="player-tool-option' + (String(option.id) === String(current) ? " is-selected" : "") + (option.type === "subtitle-missing" ? " is-muted" : "") + '" type="button" data-option-type="' + escapeHtml(option.type) + '" data-value="' + escapeHtml(option.id) + '">' + escapeHtml(option.label) + '</button>';
        }).join("");
    }

    function isPlayerToolPanelOpen() {
        return !!state.activePlayerTool && elements.playerToolPanel && !elements.playerToolPanel.hidden;
    }

    function getPlayerToolOptions() {
        return Array.prototype.slice.call(elements.playerToolOptions.querySelectorAll(".player-tool-option"));
    }

    function movePlayerToolOption(direction) {
        var options = getPlayerToolOptions();

        if (!options.length) {
            return;
        }

        if (state.playerToolOptionIndex < 0) {
            state.playerToolOptionIndex = 0;
        } else {
            state.playerToolOptionIndex = Math.max(0, Math.min(options.length - 1, state.playerToolOptionIndex + direction));
        }
        syncPlayerToolOptionFocus();
    }

    function getPlayerToolColumnCount() {
        if (elements.playerToolOptions.classList.contains("subtitle-settings")) {
            return 3;
        }

        return 2;
    }

    function movePlayerToolOptionByGrid(horizontal, vertical) {
        var options = getPlayerToolOptions();
        var columns = getPlayerToolColumnCount();
        var index = state.playerToolOptionIndex;
        var next;

        if (!options.length) {
            return;
        }

        if (index < 0) {
            index = 0;
        }

        if (horizontal) {
            if (horizontal < 0 && index % columns === 0) {
                closePlayerToolPanel();
                return;
            }
            next = index + horizontal;
            if (next >= 0 && next < options.length && Math.floor(next / columns) === Math.floor(index / columns)) {
                state.playerToolOptionIndex = next;
            }
        } else if (vertical) {
            next = index + (vertical * columns);
            if (next >= 0 && next < options.length) {
                state.playerToolOptionIndex = next;
            }
        }

        syncPlayerToolOptionFocus();
    }

    function closePlayerToolPanel() {
        var tool = state.activePlayerTool;

        state.activePlayerTool = null;
        state.playerToolOptionIndex = -1;
        renderPlayerToolPanel();
        setFocusedControl(tool || "play");
    }

    function syncPlayerToolOptionFocus() {
        var options = getPlayerToolOptions();
        var selectedIndex = -1;

        if (!options.length) {
            state.playerToolOptionIndex = -1;
            return;
        }

        options.forEach(function (option, index) {
            if (selectedIndex === -1 && option.classList.contains("is-selected")) {
                selectedIndex = index;
            }
        });

        if (state.playerToolOptionIndex < 0) {
            state.playerToolOptionIndex = selectedIndex >= 0 ? selectedIndex : 0;
        }
        if (state.playerToolOptionIndex >= options.length) {
            state.playerToolOptionIndex = options.length - 1;
        }

        options.forEach(function (option, index) {
            option.classList.toggle("is-focused", index === state.playerToolOptionIndex);
        });
        options[state.playerToolOptionIndex].focus();
    }

    function applyFocusedPlayerToolOption() {
        var options = getPlayerToolOptions();
        var option = options[state.playerToolOptionIndex];

        if (option) {
            applyPlayerToolOption(option);
        }
    }

    function getFallbackSubtitleLanguages(labels) {
        return [
            { id: "missing-ar", label: state.language === "ar" ? "\u0627\u0644\u0639\u0631\u0628\u064a\u0629" : "Arabic", type: "subtitle-missing" },
            { id: "missing-en", label: "English", type: "subtitle-missing" },
            { id: "missing-tr", label: state.language === "ar" ? "\u0627\u0644\u062a\u0631\u0643\u064a\u0629" : "Turkish", type: "subtitle-missing" },
            { id: "missing-fr", label: state.language === "ar" ? "\u0627\u0644\u0641\u0631\u0646\u0633\u064a\u0629" : "French", type: "subtitle-missing" },
            { id: "missing-de", label: state.language === "ar" ? "\u0627\u0644\u0623\u0644\u0645\u0627\u0646\u064a\u0629" : "German", type: "subtitle-missing" },
            { id: "missing-es", label: state.language === "ar" ? "\u0627\u0644\u0625\u0633\u0628\u0627\u0646\u064a\u0629" : "Spanish", type: "subtitle-missing" }
        ];
    }

    function renderSubtitleSettingsTool(labels) {
        var style = elements.subtitleStyleSelect.value || "normal";
        var size = style === "large" || style === "large-boxed" ? "large" : "normal";
        var box = style === "boxed" || style === "large-boxed";

        elements.playerToolTitle.textContent = labels.subtitleSettingsTitle;
        elements.playerToolOptions.className = "player-tool-options subtitle-settings";
        elements.playerToolOptions.innerHTML =
            '<div class="subtitle-preview">' + escapeHtml(labels.subtitleSample) + '</div>' +
            '<button class="player-tool-option' + (size === "normal" ? " is-selected" : "") + '" type="button" data-option-type="subtitle-size" data-value="normal">Aa ' + escapeHtml(labels.normal) + '</button>' +
            '<button class="player-tool-option' + (size === "large" ? " is-selected" : "") + '" type="button" data-option-type="subtitle-size" data-value="large">Aa ' + escapeHtml(labels.large) + '</button>' +
            '<button class="player-tool-option' + (!box ? " is-selected" : "") + '" type="button" data-option-type="subtitle-box" data-value="0">' + escapeHtml(labels.noBox) + '</button>' +
            '<button class="player-tool-option' + (box ? " is-selected" : "") + '" type="button" data-option-type="subtitle-box" data-value="1">' + escapeHtml(labels.boxed) + '</button>' +
            '<button class="player-tool-option" type="button" data-option-type="subtitle-delay" data-value="-1">-1s</button>' +
            '<button class="player-tool-option" type="button" data-option-type="subtitle-delay" data-value="1">+1s</button>';
    }

    function renderScreenTool(labels) {
        var current = elements.screenModeSelect.value || "contain";
        var options = [
            { id: "contain", label: labels.fit },
            { id: "cover", label: "Zoom" },
            { id: "fill", label: labels.fullscreen },
            { id: "wide", label: "Wide" }
        ];

        elements.playerToolTitle.textContent = labels.screenSettingsTitle;
        elements.playerToolOptions.className = "player-tool-options";
        elements.playerToolOptions.innerHTML = options.map(function (option) {
            return '<button class="player-tool-option' + (option.id === current ? " is-selected" : "") + '" type="button" data-option-type="screen" data-value="' + escapeHtml(option.id) + '">' + escapeHtml(option.label) + '</button>';
        }).join("");
    }

    function applyPlayerToolOption(option) {
        var type = option.getAttribute("data-option-type");
        var value = option.getAttribute("data-value");

        if (type === "subtitle") {
            elements.subtitleSelect.value = value;
            applySubtitleSelection();
        } else if (type === "subtitle-missing") {
            elements.subtitleSelect.value = "-1";
            applySubtitleSelection();
        } else if (type === "screen") {
            elements.screenModeSelect.value = value;
            applyScreenMode();
        } else if (type === "subtitle-size") {
            setSubtitleStyleValue(value, null);
        } else if (type === "subtitle-box") {
            setSubtitleStyleValue(null, value === "1");
        } else if (type === "subtitle-delay") {
            state.subtitleDelay = (state.subtitleDelay || 0) + parseInt(value, 10);
            updateSubtitleOverlay();
        }

        renderPlayerToolPanel();
    }

    function togglePlayingFavorite() {
        var item = state.playingItem;
        var key;

        if (!item) {
            return;
        }

        key = getLibraryWorkKey(item);
        try {
            window.localStorage.setItem("nexoWishlist:" + key, JSON.stringify({
                title: item.title,
                section: item.section,
                addedAt: Date.now()
            }));
        } catch (error) {
            console.log("Could not save favorite from player:", error.message);
        }
    }

    function setSubtitleStyleValue(size, boxed) {
        var current = elements.subtitleStyleSelect.value || "normal";
        var isLarge = size ? size === "large" : (current === "large" || current === "large-boxed");
        var hasBox = boxed === null ? (current === "boxed" || current === "large-boxed") : boxed;

        elements.subtitleStyleSelect.value = isLarge && hasBox ? "large-boxed" : (isLarge ? "large" : (hasBox ? "boxed" : "normal"));
        applySubtitleStyle();
    }

    function applyAudioTrackSelection() {
        var selected = parseInt(elements.audioLanguageSelect.value, 10);
        player.setAudioTrack(selected);
    }

    function applySubtitleSelection() {
        var selected = parseInt(elements.subtitleSelect.value, 10);
        state.subtitleSelectionLocked = true;
        player.setSubtitleTrack(selected);
        activateManualSubtitleForSelection(isNaN(selected) ? -1 : selected);
    }

    function autoSelectFirstSubtitleTrack() {
        var tracks;
        var selected;

        if (state.screen !== "player" || state.subtitleSelectionLocked) {
            return;
        }

        tracks = player.getSubtitleTracks();
        if (!tracks.length) {
            return;
        }

        selected = tracks[0].id;
        state.subtitleSelectionLocked = true;
        elements.subtitleSelect.value = String(selected);
        player.setSubtitleTrack(parseInt(selected, 10));
        activateManualSubtitleForSelection(parseInt(selected, 10));
        populateSubtitleTracks();
        refreshOpenPlayerTool();
        console.log("[subtitles] Auto-selected first subtitle:", tracks[0].label);
    }

    function applySubtitleStyle() {
        var style = elements.subtitleStyleSelect ? elements.subtitleStyleSelect.value : "normal";
        elements.video.classList.toggle("subtitle-large", style === "large" || style === "large-boxed");
        elements.video.classList.toggle("subtitle-boxed", style === "boxed" || style === "large-boxed");
    }

    function applyScreenMode() {
        var mode = elements.screenModeSelect ? elements.screenModeSelect.value : "contain";
        ["contain", "cover", "fill", "wide"].forEach(function (name) {
            elements.video.classList.toggle("screen-" + name, mode === name);
        });
    }

    function goHome() {
        state.screen = "home";
        showScreen("home");
        renderHome();
    }

    function goBackFromPlayer() {
        state.screen = state.returnScreen;
        clearTimeout(state.overlayTimer);
        stopSubtitleOverlayTimer();
        clearManualSubtitleOverlay();
        state.lastProgressSaveAt = 0;
        savePlaybackProgress();
        player.stop();
        state.playingItem = null;
        showScreen(state.returnScreen);

        if (state.returnScreen === "library") {
            renderLibrary();
        } else if (state.returnScreen === "detail") {
            renderDetail(state.detailItem);
            setDetailAction(state.detailActionIndex);
        } else {
            renderHome();
        }
    }

    function goBackFromDetail() {
        state.screen = state.detailReturnScreen;
        showScreen(state.detailReturnScreen);

        if (state.detailReturnScreen === "home") {
            renderHome();
        } else if (state.detailReturnScreen === "history") {
            renderHistoryScreen();
        } else {
            renderLibrary();
        }
    }

    function showScreen(screenName) {
        elements.homeScreen.classList.toggle("is-active", screenName === "home");
        elements.playlistScreen.classList.toggle("is-active", screenName === "playlist");
        elements.accountScreen.classList.toggle("is-active", screenName === "account");
        elements.historyScreen.classList.toggle("is-active", screenName === "history");
        elements.settingsScreen.classList.toggle("is-active", screenName === "settings");
        elements.libraryScreen.classList.toggle("is-active", screenName === "library");
        elements.detailScreen.classList.toggle("is-active", screenName === "detail");
        elements.playerScreen.classList.toggle("is-active", screenName === "player");
    }

    function setFocusedSection(index) {
        var cards = elements.sectionList.querySelectorAll(".section-card");

        if (!cards.length) {
            return;
        }

        state.sectionIndex = index;
        cards.forEach(function (card, cardIndex) {
            card.classList.toggle("is-focused", cardIndex === index);
        });
        cards[index].focus();
    }

    function moveHomeFocus(horizontal, vertical) {
        if (vertical < 0) {
            if (state.homePane === "latest") {
                state.homePane = "actions";
            } else if (state.homePane === "actions") {
                state.homePane = "sections";
            }
        } else if (vertical > 0) {
            if (state.homePane === "sections") {
                state.homePane = "actions";
            } else if (state.homePane === "actions") {
                state.homePane = "latest";
            }
        } else if (horizontal) {
            if (state.homePane === "sections") {
                state.sectionIndex = Math.max(0, Math.min(SECTIONS.length - 1, state.sectionIndex + horizontal));
            } else if (state.homePane === "actions") {
                state.homeActionIndex = Math.max(0, Math.min(elements.homeActionList.querySelectorAll(".home-action-button").length - 1, state.homeActionIndex + horizontal));
            } else if (state.homePane === "latest") {
                state.latestIndex = Math.max(0, Math.min(latestItems.length - 1, state.latestIndex + horizontal));
            }
        }

        syncHomeFocus();
    }

    function syncHomeFocus() {
        var cards = elements.sectionList.querySelectorAll(".section-card");
        var actions = elements.homeActionList.querySelectorAll(".home-action-button");
        var latest = elements.latestList.querySelectorAll(".latest-card");
        var target = null;

        cards.forEach(function (card, index) {
            card.classList.toggle("is-focused", state.homePane === "sections" && index === state.sectionIndex);
        });
        actions.forEach(function (button, index) {
            button.classList.toggle("is-focused", state.homePane === "actions" && index === state.homeActionIndex);
        });
        latest.forEach(function (button, index) {
            button.classList.toggle("is-focused", state.homePane === "latest" && index === state.latestIndex);
        });

        if (state.homePane === "sections") {
            target = cards[state.sectionIndex];
        } else if (state.homePane === "actions") {
            target = actions[state.homeActionIndex];
        } else {
            target = latest[state.latestIndex];
        }

        if (target) {
            target.focus();
            target.scrollIntoView({ block: "center", inline: "nearest" });
        }
    }

    function activateHomeFocus() {
        var actionButtons = elements.homeActionList.querySelectorAll(".home-action-button");

        if (state.homePane === "sections") {
            openLibrary(SECTIONS[state.sectionIndex].id);
        } else if (state.homePane === "actions" && actionButtons[state.homeActionIndex]) {
            runHomeAction(actionButtons[state.homeActionIndex].getAttribute("data-action"));
        } else if (state.homePane === "latest") {
            openLatestItem(state.latestIndex);
        }
    }

    function runHomeAction(action) {
        if (action === "account") {
            openAccountScreen();
        } else if (action === "settings") {
            openSettingsScreen();
        } else if (action === "history") {
            openHistoryScreen();
        } else {
            openPlaylistScreen();
        }
    }

    function openHistoryScreen() {
        state.screen = "history";
        showScreen("history");
        renderHistoryScreen();
        focusFirstManagementControl(".poster-card");
    }

    function renderHistoryScreen() {
        var labels = getLanguageLabels(state.language);
        setText(elements.historyHeading, labels.historyFavorites);
        setText(elements.recentHeading, labels.recentlyWatched);
        setText(elements.favoriteHeading, labels.favorites);
        renderHistoryList(elements.recentList, getRecentItems(), labels.noRecent);
        renderHistoryList(elements.favoriteList, getFavoriteItems(), labels.noFavorites);
    }

    function renderHistoryList(container, items, emptyText) {
        container.innerHTML = items.length ? items.map(function (item, index) {
            return '<button class="poster-card" type="button" data-index="' + index + '" data-key="' + escapeHtml(getLibraryWorkKey(item)) + '">' +
                '<span class="poster-art" style="' + getPosterStyle(item) + '">' + getPosterFallbackHtml(item) + '</span>' +
                '<span class="poster-title">' + escapeHtml(item.title) + '</span>' +
                '<span class="poster-meta">' + escapeHtml(getSection(item.section).subtitle) + '</span>' +
            '</button>';
        }).join("") : '<div class="history-empty">' + escapeHtml(emptyText) + '</div>';
    }

    function handleHistoryItemClick(event) {
        var card = event.target.closest(".poster-card");
        var item;

        if (!card) {
            return;
        }

        item = findItemByLibraryKey(card.getAttribute("data-key"));
        if (item) {
            state.activeSection = item.section;
            openDetail(item, "history");
        }
    }

    function getRecentItems() {
        return getStoredItemKeys("nexoProgress:").map(function (entry) {
            return {
                item: findItemByLibraryKey(entry.key.replace("nexoProgress:", "")),
                savedAt: entry.value && entry.value.savedAt ? entry.value.savedAt : 0
            };
        }).filter(function (entry) {
            return entry.item;
        }).sort(function (a, b) {
            return b.savedAt - a.savedAt;
        }).slice(0, 12).map(function (entry) {
            return entry.item;
        });
    }

    function getFavoriteItems() {
        return getStoredItemKeys("nexoWishlist:").map(function (entry) {
            return findItemByLibraryKey(entry.key.replace("nexoWishlist:", ""));
        }).filter(Boolean).slice(0, 12);
    }

    function getStoredItemKeys(prefix) {
        var entries = [];
        var i;

        try {
            for (i = 0; i < window.localStorage.length; i += 1) {
                var key = window.localStorage.key(i);
                if (key && key.indexOf(prefix) === 0) {
                    entries.push({ key: key, value: JSON.parse(window.localStorage.getItem(key) || "null") });
                }
            }
        } catch (error) {
            console.log("Could not read stored items:", error.message);
        }

        return entries;
    }

    function findItemByLibraryKey(key) {
        for (var i = 0; i < allItems.length; i += 1) {
            if (getLibraryWorkKey(allItems[i]) === key) {
                return allItems[i];
            }
        }
        return null;
    }

    function openAccountScreen() {
        state.screen = "account";
        showScreen("account");
        renderAccountScreen();
        focusFirstManagementControl("#accountDashboardButton");
    }

    function renderAccountScreen() {
        var data = state.activation || {};
        var access = data.access || {};
        var endDate = data.active_until || data.trial_ends_at || "";

        elements.accountInfo.innerHTML =
            '<span><strong>MAC</strong>' + escapeHtml(state.deviceMac || data.mac || "غير متوفر") + '</span>' +
            '<span><strong>الحالة</strong>' + escapeHtml(access.reason || "offline") + '</span>' +
            '<span><strong>بداية التجربة</strong>' + escapeHtml(formatServerDate(data.trial_started_at || "")) + '</span>' +
            '<span><strong>نهاية التجربة / الخطة</strong>' + escapeHtml(formatServerDate(endDate || "")) + '</span>' +
            '<span><strong>رابط الموقع</strong>' + escapeHtml(APP_SERVER_URL || "غير مضبوط") + '</span>' +
            '<span><strong>قائمة التشغيل</strong>' + escapeHtml(data.playlist && data.playlist.type ? data.playlist.type : "غير مضافة") + '</span>';
    }

    function renderAccountScreen() {
        var data = state.activation || {};
        var access = data.access || {};
        var endDate = data.active_until || data.trial_ends_at || "";
        var labels = getLanguageLabels(state.language);

        elements.accountInfo.innerHTML =
            '<span><strong>MAC</strong>' + escapeHtml(state.deviceMac || data.mac || labels.unavailable) + '</span>' +
            '<span><strong>' + escapeHtml(labels.status) + '</strong>' + escapeHtml(formatStatusLabel(access.reason || "offline")) + '</span>' +
            '<span><strong>' + escapeHtml(labels.start) + '</strong>' + escapeHtml(formatOptionalDate(data.trial_started_at, labels)) + '</span>' +
            '<span><strong>' + escapeHtml(labels.end) + '</strong>' + escapeHtml(formatOptionalDate(endDate, labels)) + '</span>' +
            '<span><strong>' + escapeHtml(labels.siteUrl) + '</strong>' + escapeHtml(APP_SERVER_URL || labels.notConfigured) + '</span>' +
            '<span><strong>' + escapeHtml(labels.playlistType) + '</strong>' + escapeHtml(data.playlist && data.playlist.type ? data.playlist.type : labels.notAdded) + '</span>';
    }

    function openSettingsScreen() {
        state.screen = "settings";
        showScreen("settings");
        elements.appLanguageSelect.value = state.language;
        elements.playerModeSelect.value = state.playerMode;
        elements.settingsMessage.hidden = true;
        setSettingsPanel("language");
        syncSettingsButtons();
        focusFirstManagementControl(".settings-nav-button");
    }

    function openCustomerDashboard() {
        if (!APP_SERVER_URL) {
            return;
        }

        window.location.href = normalizeServerUrl(APP_SERVER_URL) + "/dashboard.php?mac=" + encodeURIComponent(state.deviceMac || "");
    }

    function openAccountAction() {
        var checkoutUrl;

        if (!APP_SERVER_URL) {
            elements.playlistStatus.textContent = "MAC: " + (state.deviceMac || "unknown") + " - set APP_SERVER_URL to enable online activation";
            return;
        }

        checkoutUrl = normalizeServerUrl(APP_SERVER_URL) + "/api/create_checkout.php";
        setPlaylistLoading(true, "جاري تجهيز صفحة الدفع...");
        postJson(checkoutUrl, { mac: state.deviceMac }, function (error, data) {
            setPlaylistLoading(false);
            if (error || !data || !data.ok || !data.checkout_url) {
                elements.playlistStatus.textContent = "Could not create Stripe checkout. Check server config.";
                console.log("Stripe checkout failed:", error ? error.message : data);
                return;
            }

            elements.playlistStatus.textContent = "Opening Stripe checkout...";
            window.location.href = data.checkout_url;
        });
    }

    function openPlaylistSetupFlow() {
        openPlaylistScreen();
        return;
        var type = null;
        var payload = { mac: state.deviceMac || getStoredBrowserDeviceId() };
        var serverUrl;
        var username;
        var password;
        var playlistUrl;

        if (type === null) {
            return;
        }

        if (String(type).trim() === "2") {
            serverUrl = "";
            username = "";
            password = "";

            if (!serverUrl || !username || !password) {
                elements.playlistStatus.textContent = "Playlist was not saved";
                return;
            }

            payload.type = "xtream";
            payload.server_url = serverUrl;
            payload.username = username;
            payload.password = password;
            payload.output = "m3u8";
            playlistUrl = buildXtreamPlaylistUrl(serverUrl, username, password, payload.output);
        } else {
            playlistUrl = "";
            if (!playlistUrl) {
                elements.playlistStatus.textContent = "Playlist was not saved";
                return;
            }

            payload.type = "m3u";
            payload.playlist_url = playlistUrl;
        }

        IPTV_PLAYLIST_URL = playlistUrl;
        // Legacy prompt flow is disabled; playlists are saved only on the website.
        resetLibraryForReload();

        if (!APP_SERVER_URL) {
            elements.playlistStatus.textContent = "Playlist must be saved on the website";
            loadIptvPlaylist();
            return;
        }

        setPlaylistLoading(true, "جاري حفظ قائمة التشغيل على السيرفر...");
        postJson(normalizeServerUrl(APP_SERVER_URL) + "/api/playlist.php", payload, function (error, data) {
            setPlaylistLoading(false);
            if (error || !data || !data.ok) {
                console.log("Could not save playlist on server:", error ? error.message : data);
                elements.playlistStatus.textContent = "Playlist saved on this device only";
            } else {
                elements.playlistStatus.textContent = "Playlist saved for MAC: " + payload.mac;
            }
            loadIptvPlaylist();
        });
    }

    function buildXtreamPlaylistUrl(serverUrl, username, password, output) {
        return String(serverUrl || "").replace(/\/+$/, "") +
            "/get.php?username=" + encodeURIComponent(username || "") +
            "&password=" + encodeURIComponent(password || "") +
            "&type=m3u_plus&output=" + encodeURIComponent(output || "m3u8");
    }

    function normalizePlaylistFormUrl(value) {
        var url = String(value || "").trim();

        if (!url) {
            return "";
        }

        if (!/^https?:\/\//i.test(url)) {
            url = "http://" + url;
        }

        return url;
    }

    function openPlaylistScreen() {
        state.screen = "playlist";
        showScreen("playlist");
        renderPlaylistScreen();
        focusFirstManagementControl("#playlistType");
    }

    function renderPlaylistScreen() {
        var playlist = (state.activation && state.activation.playlist) || {};
        var type = playlist.type || "m3u";

        elements.playlistDeviceInfo.textContent = "MAC: " + (state.deviceMac || "unknown") + " | Server: " + (APP_SERVER_URL || "not configured");
        elements.playlistType.value = type;
        elements.playlistUrlInput.value = playlist.playlist_url || (type === "m3u" ? IPTV_PLAYLIST_URL : "");
        elements.playlistServerInput.value = playlist.server_url || "";
        elements.playlistUsernameInput.value = playlist.username || "";
        elements.playlistPasswordInput.value = playlist.password || "";
        elements.playlistFormMessage.hidden = true;
        elements.playlistCurrentInfo.innerHTML = playlist.generated_url ?
            '<strong>القائمة الحالية محفوظة في الموقع</strong><br><span class="ltr">' + escapeHtml(playlist.generated_url) + '</span>' :
            "لا توجد قائمة محفوظة لهذا الجهاز حتى الآن.";
        renderPlaylistTypeFields();
        elements.playlistType.focus();
    }

    function renderPlaylistTypeFields() {
        var isM3u = elements.playlistType.value === "m3u";
        elements.playlistUrlInput.closest("label").hidden = !isM3u;
        elements.playlistServerInput.closest("label").hidden = isM3u;
        elements.playlistUsernameInput.closest("label").hidden = isM3u;
        elements.playlistPasswordInput.closest("label").hidden = isM3u;
    }

    function savePlaylistFromForm(event) {
        var type = elements.playlistType.value;
        var payload = { mac: state.deviceMac || getStoredBrowserDeviceId(), type: type };

        event.preventDefault();

        if (!APP_SERVER_URL) {
            showPlaylistFormMessage("يجب ضبط رابط الموقع داخل التطبيق أولا.", true);
            return;
        }

        if (type === "m3u") {
            payload.playlist_url = elements.playlistUrlInput.value.trim();
            if (!payload.playlist_url) {
                showPlaylistFormMessage("أدخل رابط M3U.", true);
                return;
            }
        } else {
            payload.server_url = elements.playlistServerInput.value.trim();
            payload.username = elements.playlistUsernameInput.value.trim();
            payload.password = elements.playlistPasswordInput.value.trim();
            payload.output = "m3u8";
            if (!payload.server_url || !payload.username || !payload.password) {
                showPlaylistFormMessage("أدخل رابط السيرفر واسم المستخدم وكلمة المرور.", true);
                return;
            }
        }

        showPlaylistFormMessage("جاري الحفظ في الموقع...", false);
        postJson(normalizeServerUrl(APP_SERVER_URL) + "/api/playlist.php", payload, function (error, data) {
            if (error || !data || !data.ok) {
                console.log("Could not save playlist on server:", error ? error.message : data);
                showPlaylistFormMessage("تعذر حفظ القائمة في الموقع.", true);
                return;
            }

            state.activation = state.activation || {};
            state.activation.playlist = data.playlist;
            IPTV_PLAYLIST_URL = data.playlist && data.playlist.generated_url ? data.playlist.generated_url : "";
            resetLibraryForReload();
            renderPlaylistScreen();
            showPlaylistFormMessage("تم حفظ القائمة في الموقع لهذا الجهاز.", false);
            loadIptvPlaylist();
        });
    }

    function deletePlaylistFromServer() {
        if (!APP_SERVER_URL) {
            showPlaylistFormMessage("يجب ضبط رابط الموقع داخل التطبيق أولا.", true);
            return;
        }

        showPlaylistFormMessage("جاري حذف القائمة من الموقع...", false);
        postJson(normalizeServerUrl(APP_SERVER_URL) + "/api/playlist.php", {
            mac: state.deviceMac || getStoredBrowserDeviceId(),
            action: "delete"
        }, function (error, data) {
            if (error || !data || !data.ok) {
                console.log("Could not delete playlist on server:", error ? error.message : data);
                showPlaylistFormMessage("تعذر حذف القائمة من الموقع.", true);
                return;
            }

            state.activation = state.activation || {};
            state.activation.playlist = null;
            IPTV_PLAYLIST_URL = "";
            resetLibraryForReload();
            renderPlaylistScreen();
            showPlaylistFormMessage("تم حذف القائمة من الموقع.", false);
        });
    }

    function showPlaylistFormMessage(message, isError) {
        elements.playlistFormMessage.hidden = false;
        elements.playlistFormMessage.textContent = message;
        elements.playlistFormMessage.classList.toggle("is-error", !!isError);
    }

    function renderPlaylistScreen() {
        var playlist = (state.activation && state.activation.playlist) || {};
        var type = playlist.type || "m3u";
        var labels = getLanguageLabels(state.language);

        applyStaticLanguage(labels);
        elements.playlistDeviceInfo.textContent = "MAC: " + (state.deviceMac || labels.unknown) + " | Server: " + (APP_SERVER_URL || labels.notConfigured);
        elements.playlistType.value = type;
        elements.playlistUrlInput.value = playlist.playlist_url || (type === "m3u" ? IPTV_PLAYLIST_URL : "");
        elements.playlistServerInput.value = playlist.server_url || "";
        elements.playlistUsernameInput.value = playlist.username || "";
        elements.playlistPasswordInput.value = playlist.password || "";
        elements.playlistFormMessage.hidden = true;
        elements.playlistCurrentInfo.innerHTML = playlist.generated_url ?
            '<strong>' + escapeHtml(labels.currentPlaylistSaved) + '</strong><br><span class="ltr">' + escapeHtml(playlist.generated_url) + '</span>' :
            escapeHtml(labels.noSavedPlaylist);
        renderPlaylistTypeFields();
        syncManagementFocus();
    }

    function savePlaylistFromForm(event) {
        var type = elements.playlistType.value;
        var payload = { mac: state.deviceMac || getStoredBrowserDeviceId(), type: type };
        var labels = getLanguageLabels(state.language);

        event.preventDefault();

        if (!APP_SERVER_URL) {
            showPlaylistFormMessage(labels.serverRequired, true);
            return;
        }

        if (type === "m3u") {
            payload.playlist_url = normalizePlaylistFormUrl(elements.playlistUrlInput.value);
            elements.playlistUrlInput.value = payload.playlist_url;
            if (!payload.playlist_url) {
                showPlaylistFormMessage(labels.enterM3u, true);
                return;
            }
        } else {
            payload.server_url = normalizePlaylistFormUrl(elements.playlistServerInput.value);
            elements.playlistServerInput.value = payload.server_url;
            payload.username = elements.playlistUsernameInput.value.trim();
            payload.password = elements.playlistPasswordInput.value.trim();
            payload.output = "m3u8";
            if (!payload.server_url || !payload.username || !payload.password) {
                showPlaylistFormMessage(labels.enterXtream, true);
                return;
            }
        }

        showPlaylistFormMessage(labels.savingPlaylist, false);
        postJson(normalizeServerUrl(APP_SERVER_URL) + "/api/playlist.php", payload, function (error, data) {
            if (error || !data || !data.ok) {
                console.log("Could not save playlist on server:", error ? error.message : data);
                showPlaylistFormMessage(labels.playlistSaveFailed, true);
                return;
            }

            state.activation = state.activation || {};
            state.activation.playlist = data.playlist;
            IPTV_PLAYLIST_URL = data.playlist && data.playlist.generated_url ? data.playlist.generated_url : "";
            resetLibraryForReload();
            renderPlaylistScreen();
            showPlaylistFormMessage(labels.playlistSaved, false);
            loadIptvPlaylist();
        });
    }

    function deletePlaylistFromServer() {
        var labels = getLanguageLabels(state.language);

        if (!APP_SERVER_URL) {
            showPlaylistFormMessage(labels.serverRequired, true);
            return;
        }

        showPlaylistFormMessage(labels.deletingPlaylist, false);
        postJson(normalizeServerUrl(APP_SERVER_URL) + "/api/playlist.php", {
            mac: state.deviceMac || getStoredBrowserDeviceId(),
            action: "delete"
        }, function (error, data) {
            if (error || !data || !data.ok) {
                console.log("Could not delete playlist on server:", error ? error.message : data);
                showPlaylistFormMessage(labels.playlistDeleteFailed, true);
                return;
            }

            state.activation = state.activation || {};
            state.activation.playlist = null;
            IPTV_PLAYLIST_URL = "";
            resetLibraryForReload();
            renderPlaylistScreen();
            showPlaylistFormMessage(labels.playlistDeleted, false);
        });
    }

    function openLatestItem(index) {
        if (!latestItems[index]) {
            return;
        }

        state.activeSection = latestItems[index].section;
            openDetail(latestItems[index], "home");
    }

    function setCategory(index) {
        state.categoryIndex = Math.max(0, Math.min(state.categories.length - 1, index));
        state.itemIndex = 0;
        renderItems();
    }

    function setLibraryPane(pane) {
        state.libraryPane = pane;
        syncLibraryFocus();
    }

    function moveLibraryFocus(delta) {
        if (state.libraryPane === "search") {
            if (delta > 0) {
                setLibraryPane("categories");
            }
            return;
        }

        if (state.libraryPane === "categories") {
            if (delta < 0 && state.categoryIndex === 0) {
                setLibraryPane("search");
                return;
            }
            setCategory(Math.max(0, Math.min(state.categories.length - 1, state.categoryIndex + delta)));
            return;
        }

        if (state.libraryPane === "downloads" && isVodSection()) {
            state.libraryPane = "items";
        }

        if (state.libraryPane === "downloads") {
            state.downloadActionIndex = Math.max(0, Math.min(1, state.downloadActionIndex + delta));
            syncLibraryFocus();
            return;
        }

        state.itemIndex = Math.max(0, Math.min(renderedItems.length - 1, state.itemIndex + delta));
        syncLibraryFocus();
    }

    function moveVodLibraryHorizontal(delta) {
        if (!visibleItems.length) {
            return;
        }

        if (state.libraryPane === "search" || state.libraryPane === "categories") {
            setLibraryPane("items");
            return;
        }

        if (state.libraryPane !== "items") {
            setLibraryPane("items");
            return;
        }

        if (delta < 0 && state.itemIndex <= 0) {
            setLibraryPane("categories");
            return;
        }

        state.itemIndex = Math.max(0, Math.min(renderedItems.length - 1, state.itemIndex + delta));
        syncLibraryFocus();
    }

    function scrollVodLibraryItems(delta) {
        var scroller = elements.itemList;
        var amount;

        if (!scroller) {
            return;
        }

        amount = Math.max(280, Math.floor(scroller.clientHeight * 0.72));
        scroller.scrollTop += delta * amount;
        syncVodItemIndexToVisibleCard(delta);
        syncLibraryFocus(false);
    }

    function syncVodItemIndexToVisibleCard(direction) {
        var scroller = elements.itemList;
        var items = elements.itemList.querySelectorAll(".poster-card");
        var scrollerRect;
        var bestIndex = -1;
        var bestDistance = Infinity;
        var targetEdge;

        if (!scroller || !items.length) {
            return;
        }

        scrollerRect = scroller.getBoundingClientRect();
        targetEdge = direction > 0 ? scrollerRect.top + 24 : scrollerRect.bottom - 24;

        items.forEach(function (item, index) {
            var rect = item.getBoundingClientRect();
            var visible = rect.bottom > scrollerRect.top + 8 && rect.top < scrollerRect.bottom - 8;
            var distance;

            if (!visible) {
                return;
            }

            distance = Math.abs((direction > 0 ? rect.top : rect.bottom) - targetEdge);
            if (distance < bestDistance) {
                bestDistance = distance;
                bestIndex = index;
            }
        });

        if (bestIndex >= 0) {
            state.itemIndex = bestIndex;
        }
    }

    function syncLibraryFocus(shouldScroll) {
        var categories = elements.categoryList.querySelectorAll(".category-row");
        var items = elements.itemList.querySelectorAll(".item-row, .poster-card");
        var downloadButtons = [elements.downloadSelectedButton, elements.downloadCategoryButton];

        if (shouldScroll === undefined) {
            shouldScroll = true;
        }

        if (state.libraryPane === "downloads" && isVodSection()) {
            state.libraryPane = "items";
        }

        categories.forEach(function (row, index) {
            row.classList.toggle("is-selected", index === state.categoryIndex);
            row.classList.toggle("is-focused", state.libraryPane === "categories" && index === state.categoryIndex);
        });

        items.forEach(function (row, index) {
            row.classList.toggle("is-focused", state.libraryPane === "items" && index === state.itemIndex);
        });

        elements.librarySearchInput.classList.toggle("is-focused", state.libraryPane === "search");
        downloadButtons.forEach(function (button, index) {
            button.classList.toggle("is-focused", state.libraryPane === "downloads" && index === state.downloadActionIndex);
        });

        if (state.libraryPane === "search") {
            elements.librarySearchInput.focus();
            return;
        }

        if (state.libraryPane === "categories" && categories[state.categoryIndex]) {
            categories[state.categoryIndex].focus();
            categories[state.categoryIndex].scrollIntoView({ block: "nearest" });
        }

        if (state.libraryPane === "items" && items[state.itemIndex]) {
            items[state.itemIndex].focus();
            if (shouldScroll) {
                items[state.itemIndex].scrollIntoView({ block: "nearest" });
            }
        }

        if (state.libraryPane === "downloads") {
            downloadButtons[state.downloadActionIndex].focus();
        }
    }

    function setDetailAction(index) {
        var controls = getDetailControls();

        state.detailPane = "actions";
        state.detailActionIndex = index;
        focusDetailPane();
    }

    function getDetailControls() {
        return [
            elements.detailBackButton,
            elements.detailPlayButton,
            elements.detailTrailerButton,
            elements.detailFavoriteButton,
            elements.detailWatchlistButton
        ];
    }

    function moveDetailHorizontal(delta) {
        if (state.detailPane === "actions") {
            state.detailActionIndex = Math.max(0, Math.min(getDetailControls().length - 1, state.detailActionIndex + delta));
        } else if (state.detailPane === "seasons") {
            state.seasonIndex = Math.max(0, Math.min(detailSeasons.length - 1, state.seasonIndex + delta));
            state.episodeIndex = 0;
            renderSeriesRows(state.detailItem);
        } else if (state.detailPane === "episodes") {
            state.episodeIndex = Math.max(0, Math.min(detailEpisodes.length - 1, state.episodeIndex + delta));
        } else if (state.detailPane === "cast") {
            state.castIndex = Math.max(0, Math.min(getCastPeople(state.detailItem).length - 1, state.castIndex + delta));
        } else if (state.detailPane === "actorWorks") {
            state.actorWorkIndex = Math.max(0, Math.min(actorWorks.length - 1, state.actorWorkIndex + delta));
        }

        focusDetailPane();
    }

    function moveDetailVertical(delta) {
        if (delta < 0) {
            if (state.detailPane === "actorWorks") {
                state.detailPane = "cast";
            } else if (state.detailPane === "cast") {
                state.detailPane = detailEpisodes.length ? "episodes" : "actions";
            } else if (state.detailPane === "episodes") {
                state.detailPane = detailSeasons.length ? "seasons" : "actions";
            } else if (state.detailPane === "seasons") {
                state.detailPane = "actions";
            }
        } else {
            if (state.detailPane === "actions") {
                state.detailPane = detailSeasons.length ? "seasons" : "cast";
            } else if (state.detailPane === "seasons") {
                state.detailPane = "episodes";
            } else if (state.detailPane === "episodes") {
                state.detailPane = "cast";
            } else if (state.detailPane === "cast" && actorWorks.length) {
                state.detailPane = "actorWorks";
            }
        }

        focusDetailPane();
    }

    function activateDetailFocus() {
        if (state.detailPane === "actions") {
            if (state.detailActionIndex === 0) {
                goBackFromDetail();
            } else if (state.detailActionIndex === 1) {
                playDetailItem();
            } else if (state.detailActionIndex === 2) {
                playTrailer();
            } else if (state.detailActionIndex === 3) {
                addDetailFavorite();
            } else {
                removeFromWatchlist();
            }
        } else if (state.detailPane === "episodes") {
            playEpisode(state.episodeIndex);
        } else if (state.detailPane === "cast") {
            openActorWorks(state.castIndex);
        } else if (state.detailPane === "actorWorks") {
            openActorWork(state.actorWorkIndex);
        }
    }

    function focusDetailPane() {
        var controls = getDetailControls();
        var seasons = elements.seasonList.querySelectorAll(".season-button");
        var episodes = elements.episodeList.querySelectorAll(".episode-card");
        var castCards = elements.castList.querySelectorAll(".cast-card");
        var works = elements.actorWorksList.querySelectorAll(".actor-work-card");

        controls.forEach(function (control, index) {
            control.classList.toggle("is-focused", state.detailPane === "actions" && index === state.detailActionIndex);
        });
        seasons.forEach(function (control, index) {
            control.classList.toggle("is-focused", state.detailPane === "seasons" && index === state.seasonIndex);
        });
        episodes.forEach(function (control, index) {
            control.classList.toggle("is-focused", state.detailPane === "episodes" && index === state.episodeIndex);
        });
        castCards.forEach(function (control, index) {
            control.classList.toggle("is-focused", state.detailPane === "cast" && index === state.castIndex);
            control.classList.toggle("is-selected", state.detailPane === "actorWorks" && index === state.castIndex);
        });
        works.forEach(function (control, index) {
            control.classList.toggle("is-focused", state.detailPane === "actorWorks" && index === state.actorWorkIndex);
        });

        focusActiveDetailElement(controls, seasons, episodes, castCards, works);
    }

    function focusActiveDetailElement(controls, seasons, episodes, castCards, works) {
        var target = null;

        if (state.detailPane === "actions") {
            target = controls[state.detailActionIndex];
        } else if (state.detailPane === "seasons") {
            target = seasons[state.seasonIndex];
        } else if (state.detailPane === "episodes") {
            target = episodes[state.episodeIndex];
        } else if (state.detailPane === "cast") {
            target = castCards[state.castIndex];
        } else if (state.detailPane === "actorWorks") {
            target = works[state.actorWorkIndex];
        }

        if (target) {
            target.focus();
            target.scrollIntoView({ block: "nearest", inline: "nearest" });
        }
    }

    function runDownloadAction() {
        if (state.downloadActionIndex === 0) {
            downloadSelectedItem();
            return;
        }

        exportVisibleItemsAsM3u();
    }

    function downloadSelectedItem() {
        var item = visibleItems[state.itemIndex];

        if (!item) {
            showDownloadMessage("No item selected.");
            return;
        }

        triggerUrlDownload(item.url, makeSafeFileName(item.title) + getDownloadExtension(item));
        showDownloadMessage("Download started: " + item.title);
    }

    function downloadDetailItem() {
        if (!state.detailItem) {
            return;
        }

        triggerUrlDownload(state.detailItem.url, makeSafeFileName(state.detailItem.title) + getDownloadExtension(state.detailItem));
    }

    function exportVisibleItemsAsM3u() {
        var category = state.categories[state.categoryIndex];
        var fileName;
        var content;

        if (!visibleItems.length) {
            showDownloadMessage("No items in this category.");
            return;
        }

        fileName = makeSafeFileName(getSection(state.activeSection).subtitle + "-" + (category ? category.name : "category")) + ".m3u";
        content = "#EXTM3U\n" + visibleItems.map(function (item) {
            return '#EXTINF:-1 group-title="' + escapeM3uValue(item.group || "") + '",' + item.title + "\n" + item.url;
        }).join("\n");

        triggerBlobDownload(content, fileName, "audio/x-mpegurl");
        showDownloadMessage("M3U exported: " + visibleItems.length + " items");
    }

    function triggerUrlDownload(url, fileName) {
        var link = document.createElement("a");

        link.href = url;
        link.download = fileName;
        link.target = "_blank";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }

    function triggerBlobDownload(content, fileName, mimeType) {
        var blob = new Blob([content], { type: mimeType });
        var url = URL.createObjectURL(blob);

        triggerUrlDownload(url, fileName);
        setTimeout(function () {
            URL.revokeObjectURL(url);
        }, 1000);
    }

    function showDownloadMessage(message) {
        elements.downloadMessage.textContent = message;
        elements.downloadMessage.hidden = false;
    }

    function getDownloadExtension(item) {
        var cleanUrl = item.url.split("?")[0].toLowerCase();
        var match = cleanUrl.match(/\.(mp4|mkv|avi|mov|m3u8|ts)$/);

        if (match) {
            return "." + match[1];
        }

        if (item.type === "hls") {
            return ".m3u8";
        }

        return ".mp4";
    }

    function makeSafeFileName(value) {
        return String(value || "download")
            .replace(/[\\/:*?"<>|]+/g, "-")
            .replace(/\s+/g, " ")
            .trim()
            .slice(0, 120);
    }

    function escapeM3uValue(value) {
        return String(value).replace(/"/g, "'");
    }

    function buildCategories(items) {
        var map = {};
        var categories = [{ name: "الكل", items: items }];

        items.forEach(function (item) {
            var group = item.group || "Other";
            if (!map[group]) {
                map[group] = [];
            }
            map[group].push(item);
        });

        Object.keys(map).sort().forEach(function (group) {
            categories.push({ name: group, items: map[group] });
        });

        return categories;
    }

    function buildCategories(items) {
        var map = {};
        var categories = [{ name: getLanguageLabels(state.language).all, items: items }];

        items.forEach(function (item) {
            var group = item.group || "Other";
            if (!map[group]) {
                map[group] = [];
            }
            map[group].push(item);
        });

        Object.keys(map).sort().forEach(function (group) {
            categories.push({ name: group, items: map[group] });
        });

        return categories;
    }

    function isVodSection() {
        return state.activeSection === "movies" || state.activeSection === "series";
    }

    function getRichDetailMeta(item) {
        var values = [];
        var year = item.year || getYearFromTitle(item.title);

        values.push({ text: getReadableRating(item), className: "rating" });
        values.push({ text: year ? "تاريخ الإنتاج " + year : "تاريخ الإنتاج غير متوفر", className: "" });
        values.push({ text: item.addedAt ? "تاريخ الإضافة " + formatDateText(item.addedAt) : "تاريخ الإضافة غير متوفر", className: "" });
        values.push({ text: item.duration ? "المدة " + formatDurationText(item.duration) : "المدة غير متوفرة", className: "" });
        values.push({ text: item.group || "النوع غير متوفر", className: "" });
        values.push({ text: item.quality || item.type.toUpperCase(), className: "" });

        return values;
    }

    function getDetailMeta(item) {
        var values = [];
        var year = item.year || getYearFromTitle(item.title);

        values.push({ text: getRatingText(item), className: "rating" });
        values.push({ text: year ? "تاريخ الإنتاج " + year : "تاريخ الإنتاج غير متوفر", className: "" });
        values.push({ text: item.addedAt ? "تاريخ الإضافة " + item.addedAt : "تاريخ الإضافة غير متوفر", className: "" });
        values.push({ text: item.duration ? "المدة " + formatDurationText(item.duration) : "المدة غير متوفرة", className: "" });
        values.push({ text: item.group || "النوع غير متوفر", className: "" });
        values.push({ text: item.quality || item.type.toUpperCase(), className: "" });

        return values;
    }

    function getStoryText(item) {
        if (item.plot) {
            return item.plot;
        }

        if (item.description && item.description.indexOf("IPTV - ") !== 0) {
            return item.description;
        }

        return "قصة الفيلم غير متوفرة في بيانات الاشتراك. يمكن تشغيل العمل مباشرة من زر تشغيل.";
    }

    function getReadableStory(item) {
        if (item.plot) {
            return item.plot;
        }

        if (item.description && item.description.indexOf("IPTV - ") !== 0) {
            return item.description;
        }

        return "قصة الفيلم غير متوفرة في بيانات الاشتراك. يمكن تشغيل العمل مباشرة من زر تشغيل.";
    }

    function getCastItems(item) {
        if (item.cast && item.cast.length) {
            return item.cast;
        }

        return [
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر"
        ];
    }

    function getRatingText(item) {
        var rating = parseFloat(item.rating);

        if (isFinite(rating) && rating > 0) {
            return "★ " + rating.toFixed(1);
        }

        return "★ التقييم غير متوفر";
    }

    function getReadableRating(item) {
        var rating = parseFloat(item.rating);

        if (isFinite(rating) && rating > 0) {
            return "★ " + rating.toFixed(1);
        }

        return "★ التقييم غير متوفر";
    }

    function formatDurationText(value) {
        var text = String(value || "");
        var parts = text.split(":");
        var seconds = parseInt(text, 10);

        if (/^\d+$/.test(text) && seconds > 60) {
            return formatSeconds(seconds);
        }

        if (parts.length === 3) {
            return parts[0] + ":" + parts[1] + ":" + parts[2];
        }

        if (parts.length === 2) {
            return "00:" + parts[0] + ":" + parts[1];
        }

        return text;
    }

    function formatSeconds(totalSeconds) {
        var hours = Math.floor(totalSeconds / 3600);
        var minutes = Math.floor((totalSeconds % 3600) / 60);
        var seconds = totalSeconds % 60;

        function pad(value) {
            return value < 10 ? "0" + value : String(value);
        }

        return pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
    }

    function formatDateText(value) {
        var text = String(value || "");
        var timestamp = parseInt(text, 10);
        var date;

        if (/^\d{10}$/.test(text)) {
            date = new Date(timestamp * 1000);
            return date.getFullYear() + "-" + padDate(date.getMonth() + 1) + "-" + padDate(date.getDate());
        }

        return text;
    }

    function padDate(value) {
        return value < 10 ? "0" + value : String(value);
    }

    function getReadableCastItems(item) {
        if (item.cast && item.cast.length) {
            return item.cast;
        }

        return [
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر",
            "غير متوفر"
        ];
    }

    function getRichDetailMeta(item) {
        var labels = getLanguageLabels(state.language);
        var values = [];
        var year = item.year || getYearFromTitle(item.title);
        var isArabic = state.language === "ar";

        values.push({ text: getReadableRating(item), className: "rating" });
        values.push({ text: year ? (isArabic ? "\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0625\u0646\u062a\u0627\u062c " : "Release year ") + year : (isArabic ? "\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0625\u0646\u062a\u0627\u062c \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Release year unavailable"), className: "" });
        values.push({ text: item.addedAt ? (isArabic ? "\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0625\u0636\u0627\u0641\u0629 " : "Added ") + formatDateText(item.addedAt) : (isArabic ? "\u062a\u0627\u0631\u064a\u062e \u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Added date unavailable"), className: "" });
        values.push({ text: item.duration ? (isArabic ? "\u0627\u0644\u0645\u062f\u0629 " : "Duration ") + formatDurationText(item.duration) : (isArabic ? "\u0627\u0644\u0645\u062f\u0629 \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631\u0629" : "Duration unavailable"), className: "" });
        values.push({ text: item.group || (isArabic ? "\u0627\u0644\u0646\u0648\u0639 \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Category unavailable"), className: "" });
        values.push({ text: item.quality || item.type.toUpperCase(), className: "" });

        return values;
    }

    function getReadableStory(item) {
        if (item.plot) {
            return item.plot;
        }

        if (item.description && item.description.indexOf("IPTV - ") !== 0) {
            return item.description;
        }

        return state.language === "ar" ?
            "\u0642\u0635\u0629 \u0627\u0644\u0641\u064a\u0644\u0645 \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631\u0629 \u0641\u064a \u0628\u064a\u0627\u0646\u0627\u062a \u0627\u0644\u0627\u0634\u062a\u0631\u0627\u0643. \u064a\u0645\u0643\u0646 \u062a\u0634\u063a\u064a\u0644 \u0627\u0644\u0639\u0645\u0644 \u0645\u0628\u0627\u0634\u0631\u0629 \u0645\u0646 \u0632\u0631 \u062a\u0634\u063a\u064a\u0644." :
            "No story is available in the playlist data. You can play this item directly.";
    }

    function getReadableRating(item) {
        var rating = parseFloat(item.rating);

        if (isFinite(rating) && rating > 0) {
            return "\u2605 " + rating.toFixed(1);
        }

        return state.language === "ar" ? "\u2605 \u0627\u0644\u062a\u0642\u064a\u064a\u0645 \u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "\u2605 Rating unavailable";
    }

    function getReadableCastItems(item) {
        if (item.cast && item.cast.length) {
            return item.cast;
        }

        if (item.tmdbResolving || item.tmdbId || item.metadataLoading) {
            return [state.language === "ar" ? "\u062c\u0627\u0631\u064a \u062c\u0644\u0628 \u0627\u0644\u0646\u062c\u0648\u0645..." : "Loading cast..."];
        }

        if (item.tmdbImagesLoaded || item.metadataLoaded) {
            return [];
        }

        return [
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable",
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable",
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable",
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable",
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable",
            state.language === "ar" ? "\u063a\u064a\u0631 \u0645\u062a\u0648\u0641\u0631" : "Unavailable"
        ];
    }

    function getCastPeople(item) {
        var names;

        if (!item) {
            return [];
        }

        if (item.castPeople && item.castPeople.length) {
            return item.castPeople;
        }

        names = getReadableCastItems(item);
        return names.map(function (name) {
            return { name: name, image: "" };
        });
    }

    function getCastPhotoStyle(item, person, index) {
        var image = person && person.image;

        if (image) {
            return "background-image: linear-gradient(180deg, rgba(0,0,0,0.02), rgba(0,0,0,0.22)), url('" + escapeCssUrl(image) + "');";
        }

        return "background-image: " + getGeneratedPosterGradient((person && person.name) || item.title || ("Cast " + index)) + ";";
    }

    function getPosterStyle(item) {
        if (item.logo) {
            return "background-image: linear-gradient(180deg, rgba(0,0,0,0.02), rgba(0,0,0,0.18)), url('" + escapeCssUrl(item.logo) + "');";
        }

        return "background-image: " + getGeneratedPosterGradient(item.title) + ";";
    }

    function getPosterFallbackHtml(item) {
        if (item.logo || item.backdrop) {
            return "";
        }

        return '<span class="poster-fallback">' + escapeHtml(getInitials(item.title)) + '</span>';
    }

    function normalizePersonImageUrl(value) {
        var text = String(value || "").trim();

        if (!text) {
            return "";
        }

        if (text.indexOf("//") === 0) {
            return "https:" + text;
        }

        if (text.charAt(0) === "/") {
            return "https://image.tmdb.org/t/p/w300" + text;
        }

        return text;
    }

    function getTmdbIdFromUrl(value) {
        var match = String(value || "").match(/themoviedb\.org\/(?:movie|tv)\/(\d+)/i);

        return match ? match[1] : "";
    }

    function getBackdropStyle(item) {
        if (item.backdrop) {
            return "background-image: linear-gradient(90deg, rgba(5,8,12,0.94) 0%, rgba(5,8,12,0.66) 48%, rgba(5,8,12,0.78) 100%), url('" + escapeCssUrl(item.backdrop) + "');";
        }

        if (item.logo) {
            return "background-image: linear-gradient(90deg, rgba(5,8,12,0.94) 0%, rgba(5,8,12,0.66) 48%, rgba(5,8,12,0.78) 100%), url('" + escapeCssUrl(item.logo) + "');";
        }

        return "background-image: linear-gradient(90deg, rgba(5,8,12,0.96) 0%, rgba(5,8,12,0.70) 52%, rgba(5,8,12,0.86) 100%), " + getGeneratedPosterGradient(item.title) + ";";
    }

    function getGeneratedPosterGradient(value) {
        var hue = getStringHue(value);
        return "linear-gradient(145deg, hsl(" + hue + ", 72%, 38%) 0%, hsl(" + ((hue + 42) % 360) + ", 72%, 24%) 48%, hsl(" + ((hue + 92) % 360) + ", 70%, 18%) 100%)";
    }

    function getStringHue(value) {
        var text = String(value || "IPTV");
        var hash = 0;

        for (var i = 0; i < text.length; i += 1) {
            hash = ((hash << 5) - hash) + text.charCodeAt(i);
            hash |= 0;
        }

        return Math.abs(hash) % 360;
    }

    function getInitials(value) {
        var words = String(value || "IPTV").trim().split(/\s+/).filter(Boolean);

        if (!words.length) {
            return "IP";
        }

        return words.slice(0, 2).map(function (word) {
            return word.charAt(0).toUpperCase();
        }).join("");
    }

    function getYearFromTitle(title) {
        var match = String(title || "").match(/\b(19|20)\d{2}\b/);
        return match ? match[0] : "";
    }

    function escapeCssUrl(value) {
        return String(value || "").replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, "%22");
    }

    function getItemsBySection(sectionId) {
        return allItems.filter(function (item) {
            return item.section === sectionId;
        });
    }

    function getLibraryItemsBySection(sectionId) {
        var items = getItemsBySection(sectionId);

        if (sectionId === "series") {
            return getUniqueSeriesItems(items);
        }

        return items;
    }

    function getUniqueSeriesItems(items) {
        var map = {};

        items.forEach(function (item) {
            var key = getLibraryWorkKey(item);
            var current = map[key];

            if (!current || isBetterSeriesRepresentative(item, current)) {
                map[key] = item;
            }
        });

        return Object.keys(map).map(function (key) {
            return map[key];
        }).sort(function (a, b) {
            return getItemSortTime(b) - getItemSortTime(a);
        });
    }

    function isBetterSeriesRepresentative(candidate, current) {
        if (!candidate.episodeNumber && current.episodeNumber) {
            return true;
        }

        if (candidate.episodeNumber && current.episodeNumber) {
            if ((candidate.seasonNumber || 1) !== (current.seasonNumber || 1)) {
                return (candidate.seasonNumber || 1) < (current.seasonNumber || 1);
            }

            return candidate.episodeNumber < current.episodeNumber;
        }

        return getItemSortTime(candidate) > getItemSortTime(current);
    }

    function filterItemsBySearch(items, query) {
        var normalizedQuery = normalizeSearchText(query);

        if (!normalizedQuery) {
            return items;
        }

        return items.filter(function (item) {
            return normalizeSearchText([
                item.title,
                item.seriesTitle,
                item.group,
                item.description,
                item.plot
            ].join(" ")).indexOf(normalizedQuery) !== -1;
        });
    }

    function getSection(sectionId) {
        for (var i = 0; i < SECTIONS.length; i += 1) {
            if (SECTIONS[i].id === sectionId) {
                return SECTIONS[i];
            }
        }

        return SECTIONS[0];
    }

    function setFocusedControl(control) {
        state.focusedControl = control;
        elements.backButton.classList.toggle("is-focused", control === "back");
        elements.playPauseButton.classList.toggle("is-focused", control === "play");
        elements.favoriteToolButton.classList.toggle("is-focused", control === "favorite");
        elements.subtitleToolButton.classList.toggle("is-focused", control === "subtitles");
        elements.subtitleSettingsToolButton.classList.toggle("is-focused", control === "subtitle-settings");
        elements.screenToolButton.classList.toggle("is-focused", control === "screen");

        if (control === "back") {
            elements.backButton.focus();
        } else if (control === "favorite") {
            elements.favoriteToolButton.focus();
        } else if (control === "subtitles") {
            elements.subtitleToolButton.focus();
        } else if (control === "subtitle-settings") {
            elements.subtitleSettingsToolButton.focus();
        } else if (control === "screen") {
            elements.screenToolButton.focus();
        } else {
            elements.playPauseButton.focus();
        }
    }

    function isPlayerToolFocused() {
        return state.focusedControl === "subtitles" ||
            state.focusedControl === "favorite" ||
            state.focusedControl === "subtitle-settings" ||
            state.focusedControl === "screen";
    }

    function movePlayerControlFocus(direction) {
        var controls = ["back", "favorite", "subtitles", "subtitle-settings", "screen", "play"];
        var index = controls.indexOf(state.focusedControl);

        if (index === -1) {
            index = controls.indexOf("play");
        }

        index = Math.max(0, Math.min(controls.length - 1, index + direction));
        setFocusedControl(controls[index]);
    }

    function movePlayerToolFocus(direction) {
        var tools = ["favorite", "subtitles", "subtitle-settings", "screen"];
        var index = tools.indexOf(state.focusedControl);

        if (index === -1) {
            setFocusedControl("subtitles");
            return;
        }

        index = Math.max(0, Math.min(tools.length - 1, index + direction));
        setFocusedControl(tools[index]);
    }

    function showOverlay() {
        clearTimeout(state.overlayTimer);
        elements.overlay.classList.remove("is-hidden");

        state.overlayTimer = setTimeout(function () {
            if (state.screen === "player" && !(player.isPaused ? player.isPaused() : elements.video.paused)) {
                elements.overlay.classList.add("is-hidden");
            }
        }, 4200);
    }

    function exitApp() {
        if (window.tizen && tizen.application) {
            try {
                tizen.application.getCurrentApplication().exit();
            } catch (error) {
                console.log("Unable to exit app:", error.message);
            }
        }
    }

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

    function decodeHtml(value) {
        var textarea = document.createElement("textarea");

        textarea.innerHTML = String(value || "");
        return textarea.value;
    }

    document.addEventListener("DOMContentLoaded", init);
})();
