{% extends "base.html" %} {% block content %}
    <div id="preloader">
        <div class="demo">
            <!--        <div class="loader-inner">-->
            <div class="circle">
                <div class="inner"></div>
            </div>
            <div class="circle">
                <div class="inner"></div>
            </div>
            <div class="circle">
                <div class="inner"></div>
            </div>
            <div class="circle">
                <div class="inner"></div>
            </div>
            <div class="circle">
                <div class="inner"></div>
            </div>
            <!-- </div>-->
        </div>
    </div>
    <div id="yommi_wrapper" class="container-fluid mt-2 mt-md-4 mx-auto" style="max-width: 100%;">
        <form id="program_list">
            <div class="card p-2 p-md-4 mb-2 mb-md-4 border-light" style="background: rgba(30,30,40,0.6); backdrop-filter: blur(10px); box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
                <div class="form-group mb-0">
                    <label for="code" class="text-white font-weight-bold mb-2"><i class="fa fa-search mr-2"></i>작품 Code / 제목</label>
                    <div class="input-group input-group-lg">
                        <input type="text" id="code" name="code" class="form-control border-0" placeholder="URL 또는 제목을 입력하세요 (예: 녹을 먹는 비스코)" style="background: rgba(0,0,0,0.4); color: #fff; box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);">
                        <div class="input-group-append">
                            <button id="analysis_btn" class="btn btn-primary px-4 font-weight-bold" style="box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);">분석</button>
                            <button id="go_ohli24_btn" class="btn btn-outline-light px-3">Go OHLI24</button>
                        </div>
                    </div>
                    <div class="d-flex align-items-center mt-2 text-muted small">
                         <i class="fa fa-info-circle mr-1"></i> 
                         <span>예) "https://ohli24.net/c/녹을 먹는 비스코" 또는 "녹을 먹는 비스코"</span>
                    </div>
                </div>
            </div>
        </form>
        <form id="program_auto_form">
            <div id="episode_list"></div>
        </form>
    </div>
    <!--전체-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <script src="{{ url_for('.static', filename='js/sjva_ui14.js') }}"></script>

    <script type="text/javascript">
        const package_name = "{{arg['package_name'] }}";
        const sub = "{{arg['sub'] }}";
        const ohli24_url = "{{arg['ohli24_url']}}";
        {#let current_data = '';#}
        let accessibleCount = 1;


        const params = new Proxy(new URLSearchParams(window.location.search), {
            get: (searchParams, prop) => searchParams.get(prop),
        })

        const loader = document.getElementById("preloader");

        const dismissLoadingScreen = function () {
            loader.style.display = "none";
            $('.demo').css("display", "none")
        };

        const wait3seconds = function () {
            // Force parent container to be fluid to allow full width
            $("#main_container").removeClass("container").addClass("container-fluid");
            // REFERENCE:  https://www.w3schools.com/jsref/met_win_settimeout.asp
            const result = setTimeout(dismissLoadingScreen, 2000);
        };

        window.addEventListener("load", wait3seconds);


        function findGetParameter(parameterName) {
            let result = null,
                tmp = [];
            const items = location.search.substr(1).split("&");
            for (let index = 0; index < items.length; index++) {
                tmp = items[index].split("=");
                if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
            }
            return result;
        }

        function analyze(wr_id, bo_table) {
            {#e.preventDefault();#}

            const code = document.getElementById("code").value
            console.log(code)
            $.ajax({
                url: '/' + package_name + '/ajax/' + sub + '/analysis',
                type: "POST",
                cache: false,
                data: {code: code, wr_id: wr_id, bo_table: bo_table},
                dataType: "json",
                success: function (ret) {
                    if (ret.ret === 'success' && ret.data != null) {
                        // {#console.log(ret.code)#}
                        console.log(ret.data)
                        var order_text = (ret.data.list_order === 'desc') ? '최신화부터 (역순)' : '1화부터 (정순)';
                        if (ret.data.list_order === undefined) {
                            // 로직상 list_order가 없을 수 있으므로 체크
                            order_text = '';
                        }
                        $.notify('<strong>분석 성공</strong><br>' + order_text, {type: 'success'});
                        make_program(ret.data)
                    } else {
                        $.notify('<strong>분석 실패</strong><br>' + ret.log, {type: 'warning'});
                    }
                }
            });
        }

        function make_program(data) {
            current_data = data;
            console.log(data);
            
            let str = "";
            
            // 1. Info Card (Top)
            str += `<div class='card p-2 p-md-4 mb-3 mb-md-4 border-light' style="background: rgba(30,30,40,0.6); backdrop-filter: blur(10px);">`;
            str += `<div class="row">`;
            
            // Poster
            str += `<div class="col-md-3 text-center mb-3 mb-md-0">`;
            if (data.image) {
                str += `<img src="${data.image}" class="img-fluid rounded shadow-lg" style="max-height: 350px; width: auto;">`;
            }
            str += `</div>`;
            
            // Details
            str += `<div class="col-md-9">`;
            str += `<h2 class="font-weight-bold text-white mb-2">${data.title || ''}</h2>`;
            
            // Subtitle / Original Title
             const des = data.des || {};
            if (des._otit) {
                 str += `<h5 class="text-muted mb-3">${des._otit}</h5>`;
            }

            // Tags (Genre, etc.)
            str += `<div class="mb-4">`;
             if (des._tag) str += `<span class="badge badge-info mr-2" style="padding: 6px 12px; font-size:0.9em; white-space: normal; line-height: 1.4; text-align: left; display: inline-block;">${des._tag}</span>`;
             if (des._grade) str += `<span class="badge badge-secondary mr-2" style="padding: 6px 12px; font-size:0.9em;">${des._grade}</span>`;
             if (data.date || data.day) str += `<span class="badge badge-dark mr-2" style="padding: 6px 12px; font-size:0.9em;">${data.date || ''} (${data.day || ''})</span>`;
            str += `</div>`;
            
            // Table for details (Grid)
            str += `<div class="row mb-4" style="font-size: 0.95em; color: #ccc;">`;
             // Helper to add detail item
             const addDetail = (label, val) => {
                 if(val) str += `<div class="col-md-6 mb-2"><span class="font-weight-bold text-info mr-2">${label}:</span> <span class="text-light">${val}</span></div>`;
             };
             
             addDetail("감독", des._dir);
             addDetail("제작사", des._pub);
             addDetail("분류", des._classifi);
             addDetail("총화수", des._total_chapter);
             addDetail("상영시간", des._show_time);
             
             if (data.ser_description) {
                  str += `<div class="col-12 mt-3"><span class="font-weight-bold text-info mr-2">줄거리:</span> <p class="text-light mt-1" style="line-height:1.6;">${data.ser_description}</p></div>`;
             }
            str += `</div>`; // End detail row
            
            // Actions Toolbar
            str += `<div class="d-flex flex-wrap align-items-center mobile-action-buttons rounded" style="background: rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.05); padding: 10px; gap: 8px;">`;
            
            // Standard Actions
            str += `<button id="check_download_btn" class="btn btn-primary btn-sm"><i class="fa fa-download"></i> 선택 다운로드</button>`;
            str += `<button id="all_check_on_btn" class="btn btn-outline-light btn-sm">전체 선택</button>`;
            str += `<button id="all_check_off_btn" class="btn btn-outline-secondary btn-sm">해제</button>`;
            
            // Scheduling (Heart)
            str += `<button id="add_whitelist" class="btn btn-outline-danger btn-sm" style="min-width: 120px;"><i class="bi bi-heart"></i> 스케쥴링 추가</button>`;
            
            str += `</div>`; // End Action Toolbar
            
            str += `</div>`; // End Col-9
            str += `</div>`; // End Row
            str += `</div>`; // End Card
            
             // Episode List (Reuse existing logic or standard logic)
            str += '<div class="episode-list-container">';
            for (let i in data.episode) {
                let epThumbSrc = data.episode[i].thumbnail || '';
                let epTitle = data.episode[i].title || '';
                
                // 에피소드 번호 추출 (title에서 "N화" 패턴 찾기)
                let epNumMatch = epTitle.match(/(\d+)화/);
                let epNumText = epNumMatch ? epNumMatch[1] + '화' : (parseInt(i) + 1) + '화';
                
                str += '<div class="episode-card">';
                str += '<div class="episode-thumb">';
                if (epThumbSrc) {
                    str += '<img src="' + epThumbSrc + '" loading="lazy" onerror="this.style.display=\'none\'">';
                }
                str += '<span class="episode-num">' + epNumText + '</span>';
                str += '</div>';
                str += '<div class="episode-info">';
                str += '<div class="episode-info-row">';
                str += '<div class="episode-title">' + epTitle + '</div>';
                if (data.episode[i].date) {
                    str += '<div class="episode-date">' + data.episode[i].date + '</div>';
                }
                str += '</div>';
                str += '<div class="episode-actions">';
                str += '<input id="checkbox_' + i + '" name="checkbox_' + i + '" type="checkbox" checked data-toggle="toggle" data-on="선택" data-off="-" data-onstyle="success" data-offstyle="secondary" data-size="small">';
                str += m_button('add_queue_btn', '다운로드', [{'key': 'idx', 'value': i}]);
                str += '</div>';
                str += '</div>';
                str += '</div>';
            }
            str += '</div>';

            document.getElementById("episode_list").innerHTML = str;
            $('input[id^="checkbox_"]').bootstrapToggle()
        }

        $(function () {
            console.log(params.wr_id)
            console.log(findGetParameter('wr_id'))
            console.log(params.code)
            if (params.code === '') {

            } else {
                document.getElementById("code").value = params.code
                // {#document.getElementById("analysis_btn").click();#}
            }

            if ("{{arg['ohli24_current_code']}}" !== "") {
                if (params.code === null) {
                    console.log('params.code === null')
                    document.getElementById("code").value = "{{arg['ohli24_current_code']}}";

                } else if (params.code === '') {
                    document.getElementById("code").value = "{{arg['ohli24_current_code']}}";
                } else {

                    console.log('params code exist')
                    console.log(params.code)
                    document.getElementById("code").value = params.code

                    analyze(params.wr_id, params.bo_table)
                    // document.getElementById("analysis_btn").click();
                    // $('#analysis_btn').trigger('click')
                }
                // 값이 공백이 아니면 분석 버튼 계속 누름
                // {#document.getElementById("analysis_btn").click();#}
            } else {

            }

        })

        $(document).ready(function () {
            console.log("{{ arg['code'] }}")
            console.log('wr_id::', params.wr_id)
            if (document.getElementById("code").value !== "") {
                {#document.getElementById("analysis_btn").click()#}
                $('#analysis_btn').click();
            }


            console.log(accessibleCount)

        });


        $("#analysis_btn").unbind("click").bind('click', function (e) {
            e.preventDefault();
            e.stopPropagation();

            accessibleCount = accessibleCount - 1; //count부터 뺀다
            console.log(accessibleCount)
            if (accessibleCount < 0) {
                alert("이미 작업이 수행중 입니다.");
            } else {
                const code = document.getElementById("code").value
                console.log(code)
                $.ajax({
                    url: '/' + package_name + '/ajax/' + sub + '/analysis',
                    type: "POST",
                    cache: false,
                    data: {code: code},
                    dataType: "json",
                    success: function (ret) {
                        if (ret.ret === 'success' && ret.data != null) {
                            // {#console.log(ret.code)#}
                            console.log(ret.data)
                            make_program(ret.data)
                        } else {
                            $.notify('<strong>분석 실패</strong><br>' + ret.log, {type: 'warning'});
                        }
                    }
                });
                console.log(accessibleCount)
                accessibleCount++
                console.log(accessibleCount)

            }

        });


        $("body").on('click', '#add_whitelist', function (e) {
            e.preventDefault();
            const code = document.getElementById("code").value || params.code;
            const $btn = $(this);
            $.ajax({
                url: '/' + package_name + '/ajax/' + sub + '/add_whitelist',
                type: "POST",
                cache: false,
                data: JSON.stringify({data_code: code}),
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (ret) {
                    if (ret.ret) {
                        $.notify('<strong>스케쥴링 목록에 추가하였습니다.</strong>', {type: 'success'});
                        $btn.html('<i class="bi bi-heart-fill"></i> 추가됨').removeClass('btn-outline-danger').addClass('btn-danger');
                    } else {
                         if (ret.log == "이미 추가되어 있습니다.") {
                            $.notify('<strong>이미 추가되어 있습니다.</strong>', {type: 'warning'});
                            $btn.html('<i class="bi bi-heart-fill"></i> 추가됨').removeClass('btn-outline-danger').addClass('btn-danger');
                        } else {
                            $.notify('<strong>추가 실패</strong><br>' + ret.log, {type: 'warning'});
                        }
                    }
                }
            });
        });

        $("body").on('click', '#go_ohli24_btn', function (e) {
            e.preventDefault();
            window.open("{{arg['ohli24_url']}}", "_blank");
        });

        $("body").on('click', '#all_check_on_btn', function (e) {
            e.preventDefault();
            $('input[id^="checkbox_"]').bootstrapToggle('on')
        });

        $("body").on('click', '#all_check_off_btn', function (e) {
            e.preventDefault();
            $('input[id^="checkbox_"]').bootstrapToggle('off')
        });

        $("body").on('click', '#add_queue_btn', function (e) {
            e.preventDefault();
            data = current_data.episode[$(this).data('idx')];
            console.log('data:::>', data)
            $.ajax({
                url: '/' + package_name + '/ajax/' + sub + '/add_queue',
                type: "POST",
                cache: false,
                data: {data: JSON.stringify(data)},
                dataType: "json",
                success: function (data) {
                    console.log('#add_queue_btn::data >>', data)
                    if (data.ret == 'enqueue_db_append' || data.ret == 'enqueue_db_exist') {
                        $.notify('<strong>다운로드 작업을 추가 하였습니다.</strong>', {type: 'success'});
                    } else if (data.ret == 'queue_exist') {
                        $.notify('<strong>이미 큐에 있습니다. 삭제 후 추가하세요.</strong>', {type: 'warning'});
                    } else if (data.ret == 'db_completed') {
                        $.notify('<strong>DB에 완료 기록이 있습니다.</strong>', {type: 'warning'});
                    } else {
                        $.notify('<strong>추가 실패</strong><br>' + ret.log, {type: 'warning'});
                    }
                }
            });

        });

        $("body").on('click', '#check_download_btn', function (e) {
            e.preventDefault();
            all = $('input[id^="checkbox_"]');
            let data = [];
            let idx;
            for (let i in all) {
                if (all[i].checked) {
                    idx = parseInt(all[i].id.split('_')[1])
                    data.push(current_data.episode[idx]);
                }
            }
            if (data.length == 0) {
                $.notify('<strong>선택하세요.</strong>', {type: 'warning'});
                return;
            }
            $.ajax({
                url: '/' + package_name + '/ajax/' + sub + '/add_queue_checked_list',
                type: "POST",
                cache: false,
                data: {data: JSON.stringify(data)},
                dataType: "json",
                success: function (data) {
                    $.notify('<strong>백그라운드로 작업을 추가합니다.</strong>', {type: 'success'});
                }
            });
        });

        $('body').on('click', '#insert_download_btn', function (e) {
            e.preventDefault();
            let code = $(this).data('code');

            globalSendCommand('download_program', code, false);
        });

        $('body').on('click', '#force_insert_download_btn', function (e) {
            e.preventDefault();
            let code = $(this).data('code');
            globalSendCommand('download_program', code, true);
        });
    </script>
    <style>
        body {
            font-family: NanumSquareNeo, system-ui, -apple-system, Segoe UI, Roboto, Helvetica Neue,
            Noto Sans, Liberation Sans, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji,
            Segoe UI Symbol, Noto Color Emoji;
        }

        body {
            background-image: linear-gradient(90deg, #33242c, #263341, #17273a);
            color: #d6eaf8;
        }

        button.code-button {
            min-width: 82px !important;
        }

        .tooltip {
            position: relative;
            display: block;
        }

        [data-tooltip-text]:hover {
            position: relative;
        }

        [data-tooltip-text]:after {
            -webkit-transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;
            -moz-transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;
            transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;

            background-color: rgba(0, 0, 0, 0.8);

            -webkit-box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);
            -moz-box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);
            box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);

            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;

            color: #ffffff;
            font-size: 12px;
            margin-bottom: 10px;
            padding: 7px 12px;
            position: absolute;
            width: auto;
            min-width: 50px;
            max-width: 300px;
            word-wrap: break-word;

            z-index: 9999;

            opacity: 0;
            left: -9999px;
            top: 90%;

            content: attr(data-tooltip-text);
        }

        [data-tooltip-text]:hover:after {
            top: 230%;
            left: 0;
            opacity: 1;
        }

        [data-tooltip-text]:hover {
            position: relative;
        }

        [data-tooltip-text]:after {
            -webkit-transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;
            -moz-transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;
            transition: bottom 0.3s ease-in-out, opacity 0.3s ease-in-out;

            background-color: rgba(0, 0, 0, 0.8);

            -webkit-box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);
            -moz-box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);
            box-shadow: 0px 0px 3px 1px rgba(50, 50, 50, 0.4);

            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;

            color: #ffffff;
            font-size: 12px;
            margin-bottom: 10px;
            padding: 7px 12px;
            position: absolute;
            width: auto;
            min-width: 50px;
            max-width: 300px;
            word-wrap: break-word;

            z-index: 9999;

            opacity: 0;
            left: -9999px;
            top: -210% !important;

            content: attr(data-tooltip-text);
        }

        [data-tooltip-text]:hover:after {
            top: 130%;
            left: 0;
            opacity: 1;
        }

        .card {
            border: none;
            box-shadow: inset 1px 1px hsl(0deg 0% 100% / 20%), inset -1px -1px hsl(0deg 0% 100% / 10%),
            1px 3px 24px -1px rgb(0 0 0 / 15%);
            background-color: transparent;
            background-image: linear-gradient(125deg, hsla(0, 0%, 100%, 0.3), hsla(0, 0%, 100%, 0.2) 70%);
            backdrop-filter: blur(5px);
        }

        .card.border-light {
            border-radius: 30px 10px;
            --bs-border-opacity: 1;
            border-color: rgba(var(--bs-light-rgb), var(--bs-border-opacity)) !important;
        }

        /* 에피소드 목록 컨테이너 - 반응형 그리드 */
        .episode-list-container {
            margin-top: 20px;
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 10px;
            padding: 0 5px;
            width: 100%;
        }
        
        /* 에피소드 카드 */
        .episode-card {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            gap: 8px 12px;
            padding: 10px 12px;
            background: linear-gradient(135deg, rgba(30, 41, 59, 0.85) 0%, rgba(15, 23, 42, 0.85) 100%);
            border-radius: 8px;
            border: 1px solid rgba(148, 163, 184, 0.12);
            transition: all 0.2s ease;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        }
        
        .episode-card:hover {
            background: linear-gradient(135deg, rgba(51, 65, 85, 0.95) 0%, rgba(30, 41, 59, 0.95) 100%);
            border-color: rgba(96, 165, 250, 0.5);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(96, 165, 250, 0.2);
        }
        
        /* 에피소드 썸네일 */
        .episode-thumb {
            position: relative;
            width: 56px;
            min-width: 56px;
            height: 42px;
            border-radius: 5px;
            overflow: hidden;
            background: linear-gradient(135deg, rgba(0, 0, 0, 0.3) 0%, rgba(30, 41, 59, 0.5) 100%);
            flex-shrink: 0;
        }
        
        .episode-thumb img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        /* 에피소드 번호 배지 */
        .episode-num {
            position: absolute;
            bottom: 2px;
            left: 2px;
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
            color: white;
            font-size: 9px;
            font-weight: 700;
            padding: 1px 5px;
            border-radius: 3px;
        }
        
        /* 에피소드 정보 - 2줄 레이아웃 */
        .episode-info {
            flex: 1;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            gap: 4px 10px;
            min-width: 0;
        }
        
        /* 제목과 날짜를 담는 첫번째 줄 */
        .episode-info-row {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            gap: 10px;
        }
        
        .episode-title {
            color: #e2e8f0;
            font-weight: 500;
            font-size: 13px;
            line-height: 1.3;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            flex: 1;
            min-width: 0;
        }
        
        .episode-date {
            color: #64748b;
            font-size: 11px;
            white-space: nowrap;
            flex-shrink: 0;
        }
        
        /* 에피소드 액션 버튼 - 선택 좌측, 다운로드 우측 */
        .episode-actions {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 8px;
            flex-wrap: wrap;
            width: 100%;
            margin-top: 2px;
            padding-top: 6px;
            border-top: 1px solid rgba(148, 163, 184, 0.1);
        }
        
        .episode-actions .btn {
            font-size: 11px;
            padding: 3px 10px;
            border-radius: 4px;
        }
        
        .episode-actions .toggle {
            transform: scale(0.85);
        }
        
        /* 모바일 반응형 - Bootstrap 모든 레이아웃 강제 덮어쓰기 */
        @media (max-width: 768px) {
            /* 전체 페이지 기본 설정 */
            body {
                overflow-x: hidden !important;
                padding: 0 !important;
                margin: 0 !important;
            }
            
            /* 모든 컨테이너/row 폭 100% 강제 */
            .container, .container-fluid, .container-sm, .container-md, .container-lg,
            .row, form, #program_list, #program_auto_form, #episode_list {
                width: 100% !important;
                max-width: 100% !important;
                padding-left: 4px !important;
                padding-right: 4px !important;
                margin-left: 0 !important;
                margin-right: 0 !important;
                box-sizing: border-box !important;
            }
            
            /* form-group 및 모든 col 클래스 */
            .form-group, .form-inline,
            [class*="col-"] {
                flex: 0 0 100% !important;
                max-width: 100% !important;
                width: 100% !important;
                padding-left: 0 !important;
                padding-right: 0 !important;
            }
            
            /* row 마진 제거 */
            .row {
                margin-left: 0 !important;
                margin-right: 0 !important;
            }
            
            /* 입력 폼 스타일 */
            #program_list .form-group {
                display: flex !important;
                flex-direction: column !important;
                gap: 8px !important;
            }
            
            #program_list input.form-control {
                width: 100% !important;
                box-sizing: border-box !important;
            }
            
            #program_list .btn {
                width: auto !important;
                flex-shrink: 0;
            }
            
            /* 버튼 그룹 */
            .form-inline {
                display: flex !important;
                flex-wrap: wrap !important;
                gap: 6px !important;
                margin-bottom: 10px !important;
            }
            
            .form-inline .btn {
                font-size: 11px;
                padding: 6px 10px;
            }
            
            /* ===== 상단 정보 카드 ===== */
            .card.p-lg-5, .card.border-light {
                width: calc(100% - 10px) !important;
                max-width: 100% !important;
                padding: 6px !important; /* Reduced from 15px */
                margin: 5px !important;
                border-radius: 12px !important;
                box-sizing: border-box !important;
            }
            
            .card.p-lg-5 > .row {
                display: flex !important;
                flex-direction: column !important;
                width: 100% !important;
                margin: 0 !important;
            }
            
            /* 메인 썸네일 - 중앙 정렬 */
            .card.p-lg-5 > .row > [class*="col-"]:first-child {
                flex: 0 0 100% !important;
                max-width: 100% !important;
                text-align: center !important;
                margin-bottom: 15px !important;
                padding: 0 !important;
            }
            
            .card.p-lg-5 > .row > [class*="col-"]:first-child img {
                width: 70% !important;
                max-width: 220px !important;
                height: auto !important;
                border-radius: 10px !important;
                box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4) !important;
            }
            
            /* 정보 컬럼 - 전체 폭 */
            .card.p-lg-5 > .row > [class*="col-"]:last-child {
                flex: 0 0 100% !important;
                max-width: 100% !important;
                width: 100% !important;
                padding: 0 !important;
            }
            
            /* 정보 테이블 각 행 - 카드 스타일 */
            .card.p-lg-5 .row .row {
                display: flex !important;
                flex-direction: row !important;
                flex-wrap: nowrap !important;
                align-items: flex-start !important;
                width: 100% !important;
                max-width: 100% !important;
                margin: 0 0 6px 0 !important;
                padding: 10px 12px !important;
                background: rgba(255, 255, 255, 0.06) !important;
                border-radius: 8px !important;
                box-sizing: border-box !important;
            }

            /* Badge Wrap Fix for Mobile */
            .badge {
                white-space: normal !important;
                text-align: left !important;
                line-height: 1.4 !important;
                height: auto !important;
                display: inline-block !important;
            }

            /* Action Buttons 2x2 Grid for Mobile */
            .mobile-action-buttons {
                display: grid !important;
                grid-template-columns: 1fr 1fr !important;
                gap: 8px !important;
                width: 100% !important;
            }
            
            .mobile-action-buttons .btn {
                width: 100% !important;
                margin: 0 !important;
                min-width: auto !important; /* Override inline style */
                font-size: 11px !important;
                padding: 8px 4px !important;
            }
            
            /* 정보 라벨 (제목, 원제 등) */
            .card.p-lg-5 .row .row > [class*="col-"]:first-child {
                flex: 0 0 60px !important;
                max-width: 60px !important;
                min-width: 60px !important;
                font-size: 11px !important;
                font-weight: 600 !important;
                color: #94a3b8 !important;
                text-align: left !important;
                padding: 0 5px 0 0 !important;
            }
            
            /* 정보 값 */
            .card.p-lg-5 .row .row > [class*="col-"]:last-child {
                flex: 1 1 auto !important;
                max-width: none !important;
                font-size: 12px !important;
                color: #e2e8f0 !important;
                word-break: break-word !important;
                text-align: left !important;
                padding: 0 !important;
            }
            
            /* ===== 에피소드 목록 ===== */
            .episode-list-container {
                display: flex !important;
                flex-direction: column !important;
                gap: 8px !important;
                width: 100% !important;
                max-width: 100% !important;
                padding: 0 !important;
                margin: 15px 0 !important;
                box-sizing: border-box !important;
            }
            
            .episode-card {
                display: flex !important;
                align-items: center !important;
                width: 100% !important;
                max-width: 100% !important;
                padding: 10px 12px !important;
                gap: 10px !important;
                margin: 0 !important;
                box-sizing: border-box !important;
            }
            
            .episode-thumb {
                width: 55px !important;
                min-width: 55px !important;
                height: 42px !important;
                flex-shrink: 0 !important;
            }
            
            .episode-info {
                flex: 1 !important;
                min-width: 0 !important;
                flex-direction: row !important;
                align-items: center !important;
                justify-content: space-between !important;
            }
            
            .episode-title {
                font-size: 12px !important;
                white-space: nowrap !important;
                overflow: hidden !important;
                text-overflow: ellipsis !important;
                line-height: 1.3 !important;
                flex: 1 !important;
                min-width: 0 !important;
            }
            
            .episode-date {
                font-size: 10px !important;
                color: #64748b !important;
                flex-shrink: 0 !important;
                margin-left: 8px !important;
            }
            
            .episode-actions {
                display: flex !important;
                flex-wrap: wrap !important;
                gap: 6px !important;
                margin-top: 5px !important;
            }
            
            .episode-actions .btn {
                font-size: 10px !important;
                padding: 4px 10px !important;
            }
            
            .episode-actions .toggle {
                transform: scale(0.85) !important;
            }
        }
        
        /* 더 작은 화면 (400px 이하) */
        @media (max-width: 400px) {
            .card.p-lg-5 > .row > [class*="col-"]:first-child img {
                width: 60% !important;
                max-width: 180px !important;
            }
            
            .card.p-lg-5 .row .row > [class*="col-"]:first-child {
                flex: 0 0 50px !important;
                max-width: 50px !important;
                min-width: 50px !important;
                font-size: 10px !important;
            }
            
            .card.p-lg-5 .row .row > [class*="col-"]:last-child {
                font-size: 11px !important;
            }
            
            .episode-thumb {
                width: 45px !important;
                min-width: 45px !important;
                height: 34px !important;
            }
            
            .episode-num {
                font-size: 8px !important;
                padding: 1px 3px !important;
            }
            
            .episode-title {
                font-size: 12px !important;
            }
            
            .episode-actions .btn {
                font-size: 9px !important;
                padding: 3px 8px !important;
            }
        }

        #airing_list {
            display: none;
        }

        .cut-text {
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            width: 100%;
        }

        #screen_movie_list {
            margin-top: 10px;
        }

        /*@import url(https://fonts.googleapis.com/css?family=Lato);*/
        /*a {*/
        /*    position: fixed;*/
        /*    bottom: 2%;*/
        /*    display: block;*/
        /*    text-align: center;*/
        /*    color: #0fa;*/
        /*    font-family: "Lato", sans-serif;*/
        /*    text-decoration: none !important;*/
        /*    width: 100%;*/
        /*}*/

        /*body, html {*/
        /*    width: 100%;*/
        /*    height: 100%;*/
        /*    overflow: hidden;*/
        /*}*/

        /*body {*/
        /*    background: linear-gradient(90deg, #00b377, #00d68f);*/
        /*    box-shadow: inset 0px 0px 90px rgba(0, 0, 0, 0.5);*/
        /*    margin: 0px;*/
        /*    padding: 0px;*/
        /*}*/

        .demo {
            width: 100px;
            height: 102px;
            border-radius: 100%;
            position: absolute;
            top: 45%;
            left: calc(50% - 50px);
        }

        .circle {
            width: 100%;
            height: 100%;
            position: absolute;
        }

        .circle .inner {
            width: 100%;
            height: 100%;
            border-radius: 100%;
            border: 5px solid rgba(0, 255, 170, 0.7);
            border-right: none;
            border-top: none;
            background-clip: padding;
            box-shadow: inset 0px 0px 10px rgba(0, 255, 170, 0.15);
        }

        @-webkit-keyframes spin {
            from {
                transform: rotate(0deg);
            }
            to {
                transform: rotate(360deg);
            }
        }

        @keyframes spin {
            from {
                transform: rotate(0deg);
            }
            to {
                transform: rotate(360deg);
            }
        }

        .circle:nth-of-type(0) {
            transform: rotate(0deg);
        }

        .circle:nth-of-type(0) .inner {
            -webkit-animation: spin 2s infinite linear;
            animation: spin 2s infinite linear;
        }

        .circle:nth-of-type(1) {
            transform: rotate(70deg);
        }

        .circle:nth-of-type(1) .inner {
            -webkit-animation: spin 2s infinite linear;
            animation: spin 2s infinite linear;
        }

        .circle:nth-of-type(2) {
            transform: rotate(140deg);
        }

        .circle:nth-of-type(2) .inner {
            -webkit-animation: spin 2s infinite linear;
            animation: spin 2s infinite linear;
        }

        .demo {
            -webkit-animation: spin 5s infinite linear;
            animation: spin 5s infinite linear;
            background: rgba(0, 0, 0, 0.2);
            background: radial-gradient(#222, #000);
            bottom: 0;
            left: 0;
            overflow: hidden;
            /*position: fixed;*/
            right: 0;
            /*top: 0;*/
            z-index: 99999;
            opacity: 0.5;
            margin: 0 auto;
            transform: translate(-50%, -50%);
            position: absolute;
            top: 50%;
        }

        .loader-inner {
            bottom: 0;
            height: 60px;
            left: 0;
            margin: auto;
            position: absolute;
            right: 0;
            top: 0;
            width: 100px;
        }

        #preloader {
            /*background-color: green;*/
            /*color: white;*/
            /*height: 100vh;*/
            /*width: 100%;*/
            /*position: fixed;*/
            /*z-index: 100;*/
            background: rgba(0, 0, 0, 0.2);
            background: radial-gradient(#222, #000);
            bottom: 0;
            left: 0;
            overflow: hidden;
            position: fixed;
            right: 0;
            top: 0;
            z-index: 99999;
            opacity: 0.5;
        }

        .btn-outline-success {
            color: #fd7e14 !important;
            border-color: #fd7e14 !important;
        }

        .btn-outline-success:hover {
            color: #fff !important;
            background-color: #0dcaf0 !important;
            border-color: #0dcaf0 !important;
        }
        
        /* Navigation Menu Override */
        ul.nav.nav-pills.bg-light {
            background-color: rgba(30, 41, 59, 0.6) !important;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.08);
            border-radius: 50rem !important;
            padding: 6px !important;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2) !important;
            display: inline-flex !important;
            flex-wrap: wrap;
            justify-content: center;
            width: auto !important;
            margin-bottom: 20px;
        }

        ul.nav.nav-pills .nav-item {
            margin: 0 2px;
        }

        ul.nav.nav-pills .nav-link {
            border-radius: 50rem !important;
            padding: 8px 20px !important;
            color: #94a3b8 !important;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        ul.nav.nav-pills .nav-link:hover {
            background-color: rgba(255, 255, 255, 0.1);
            color: #fff !important;
            transform: translateY(-1px);
        }

        ul.nav.nav-pills .nav-link.active {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%) !important;
            color: #fff !important;
            box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
        }
        
        /* 로딩 인디케이터 오버라이드 */
        #loading {
            background: rgba(15, 23, 42, 0.85) !important;
            backdrop-filter: blur(8px) !important;
        }
        
        #loading img,
        #loading svg {
            display: none !important;
            visibility: hidden !important;
            width: 0 !important;
            height: 0 !important;
        }
        
        #loading::before {
            content: '';
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 50px;
            height: 50px;
            border: 4px solid rgba(96, 165, 250, 0.2);
            border-top-color: #60a5fa;
            border-radius: 50%;
            animation: loader-spin 0.8s linear infinite;
            z-index: 100001;
        }
        
        @keyframes loader-spin {
            to { transform: translate(-50%, -50%) rotate(360deg); }
        }
        
        /* 모달 로딩 인디케이터 오버라이드 */
        #modal_loading img,
        #modal_loading svg {
            display: none !important;
            visibility: hidden !important;
            width: 0 !important;
            height: 0 !important;
        }
        
        #modal_loading::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 40px;
            height: 40px;
            border: 3px solid rgba(96, 165, 250, 0.2);
            border-top-color: #60a5fa;
            border-radius: 50%;
            animation: loader-spin 0.8s linear infinite;
        }
    </style>

<style>
    /* Smooth Load Transition */
    .content-cloak, 
    #menu_module_div, 
    #menu_page_div {
        opacity: 0;
        transition: opacity 0.5s ease-out;
    }
    
    /* Staggered Delays for Natural Top-Down Flow */
    #menu_module_div.visible {
        opacity: 1;
        transition-delay: 0ms;
    }

    #menu_page_div.visible {
        opacity: 1;
        transition-delay: 150ms;
    }

    .content-cloak.visible {
        opacity: 1;
        transition-delay: 300ms;
    }
    
    /* Navigation Menu Override (Top Sub-menu) */
    ul.nav.nav-pills.bg-light {
        background-color: rgba(30, 41, 59, 0.6) !important;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.08);
        border-radius: 50rem !important;
        padding: 6px !important;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2) !important;
        display: inline-flex !important;
        flex-wrap: wrap;
        justify-content: center;
        width: auto !important;
        margin-bottom: 20px;
    }

    ul.nav.nav-pills .nav-item { margin: 0 2px; }
    ul.nav.nav-pills .nav-link {
        border-radius: 50rem !important;
        padding: 8px 20px !important;
        color: #94a3b8 !important;
        font-weight: 600;
        transition: all 0.3s ease;
    }
    ul.nav.nav-pills .nav-link:hover {
        background-color: rgba(255, 255, 255, 0.1);
        color: #fff !important;
        transform: translateY(-1px);
    }
    ul.nav.nav-pills .nav-link.active {
        background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%) !important;
        color: #fff !important;
        box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
    }
</style>

<script type="text/javascript">
$(document).ready(function(){
  // Smooth Load Trigger
  setTimeout(function() {
      $('.content-cloak, #menu_module_div, #menu_page_div').addClass('visible');
  }, 100);
});
</script>
{% endblock %}
