<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ config('sximo.cnf_appname') }}</title>

    <link rel="shortcut icon" href="{{ asset('favicon.ico') }}" type="image/x-icon">

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">

    <!-- Vendor CSS -->
    <link href="{{ asset('assets/plugins/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/plugins/perfect-scrollbar/dist/css/perfect-scrollbar.min.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/plugins/c3-master/c3.min.css') }}" rel="stylesheet">

    <!-- Application CSS -->
    <link href="{{ asset('assets/css/style.css') }}" rel="stylesheet">
    <link href="{{ asset('assets/css/legacy.css') }}" rel="stylesheet">

    <!-- Font Awesome -->
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">

    <!-- Application JS -->
    <script src="{{ asset('assets/plugins/moment/moment.js') }}"></script>
    <script src="{{ asset('assets/js/sximo.min.js') }}"></script>
    <script src="{{ asset('assets/js/sximo5.js') }}"></script>

    <!-- Google reCAPTCHA -->
    <script src="https://www.google.com/recaptcha/api.js"></script>

    <style>
        :root {
            --primary: #16a34a;
            --primary-dark: #15803d;
            --primary-light: #dcfce7;
            --white: #ffffff;
        }

        html,
        body {
            height: 100%;
            margin: 0;
        }

        body {
            font-family: 'Poppins', sans-serif;
            overflow: hidden;
        }

        .login-wrapper {
            min-height: 100vh;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .login-video {
            position: fixed;
            inset: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            z-index: 0;
        }

        .login-overlay {
            position: fixed;
            inset: 0;
            background: linear-gradient(
                135deg,
                rgba(4, 25, 12, 0.88),
                rgba(8, 60, 30, 0.65),
                rgba(0, 0, 0, 0.55)
            );
            z-index: -2;
        }

        .login-container {
            width: 100%;
            max-width: 1100px;
            min-height: 650px;
            display: grid;
            grid-template-columns: 1.1fr 0.9fr;
            overflow: hidden;
            border-radius: 32px;
            background: rgba(255, 255, 255, 0.08);
            backdrop-filter: blur(18px);
            -webkit-backdrop-filter: blur(18px);
            border: 1px solid rgba(255, 255, 255, 0.15);
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
            animation: loginAppear 0.8s ease;
        }

        .login-brand {
            position: relative;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 60px;
            color: var(--white);
        }

        .login-brand::after {
            content: '';
            position: absolute;
            left: -100px;
            bottom: -100px;
            width: 300px;
            height: 300px;
            border-radius: 50%;
            background: radial-gradient(
                circle,
                rgba(34, 197, 94, 0.35),
                transparent 70%
            );
        }

        .brand-logo {
            width: 95px;
            height: 95px;
            object-fit: contain;
            margin-bottom: 25px;
            padding: 15px;
            border-radius: 24px;
            background: rgb(255, 255, 255);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.25);
        }

        .brand-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            width: max-content;
            margin-bottom: 20px;
            padding: 8px 15px;
            border-radius: 50px;
            background: rgba(34, 197, 94, 0.18);
            border: 1px solid rgba(74, 222, 128, 0.3);
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 1px;
        }

        .brand-title {
            margin-bottom: 20px;
            font-size: 42px;
            font-weight: 800;
            line-height: 1.2;
        }

        .brand-title span {
            color: #4ade80;
        }

        .brand-description {
            max-width: 480px;
            font-size: 15px;
            line-height: 1.8;
            color: rgba(255, 255, 255, 0.75);
        }

        .brand-features {
            display: flex;
            flex-direction: column;
            gap: 15px;
            margin-top: 35px;
        }

        .brand-feature {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .feature-icon {
            width: 42px;
            height: 42px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 14px;
            background: rgba(255, 255, 255, 0.12);
            border: 1px solid rgba(255, 255, 255, 0.15);
            color: #86efac;
        }

        .brand-feature span {
            font-size: 13px;
            color: rgba(255, 255, 255, 0.8);
        }

        .login-panel {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 55px 50px;
            background: rgba(255, 255, 255, 0.95);
        }

        .login-panel-inner {
            width: 100%;
            max-width: 380px;
        }

        .login-header {
            margin-bottom: 35px;
        }

        .login-header h2 {
            margin-bottom: 8px;
            font-size: 30px;
            font-weight: 700;
            color: #1f2937;
        }

        .login-header p {
            margin: 0;
            font-size: 13px;
            color: #6b7280;
        }

        .mobile-brand {
            display: none;
            margin-bottom: 30px;
            text-align: center;
        }

        .mobile-logo {
            width: 75px;
            height: 75px;
            object-fit: contain;
            margin-bottom: 12px;
        }

        .form-group-modern {
            margin-bottom: 20px;
        }

        .form-label-modern {
            display: block;
            margin-bottom: 8px;
            font-size: 12px;
            font-weight: 600;
            color: #374151;
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper .input-icon {
            position: absolute;
            left: 17px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary);
            font-size: 15px;
        }

        .input-modern {
            width: 100%;
            height: 54px;
            padding: 0 50px;
            border: 1px solid #e5e7eb;
            border-radius: 14px;
            outline: none;
            background: #f9fafb;
            font-size: 13px;
            transition: 0.25s;
        }

        .input-modern:focus {
            border-color: #22c55e;
            background: #ffffff;
            box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.1);
        }

        .password-toggle {
            position: absolute;
            right: 17px;
            top: 50%;
            transform: translateY(-50%);
            color: #9ca3af;
            cursor: pointer;
        }

        .select-modern {
            appearance: none;
        }

        .btn-login-modern {
            width: 100%;
            height: 55px;
            border: none;
            border-radius: 14px;
            color: #ffffff;
            background: linear-gradient(135deg, #16a34a, #22c55e);
            box-shadow: 0 12px 25px rgba(34, 197, 94, 0.25);
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: 0.3s;
        }

        .btn-login-modern:hover {
            transform: translateY(-2px);
            box-shadow: 0 18px 30px rgba(34, 197, 94, 0.35);
        }

        .btn-login-modern:disabled {
            opacity: 0.7;
            cursor: not-allowed;
        }

        .login-footer {
            margin-top: 25px;
            text-align: center;
            font-size: 11px;
            color: #9ca3af;
        }

        .ajaxLoading {
            position: fixed;
            inset: 0;
            display: none;
            background: rgba(0, 0, 0, 0.45);
            z-index: 9999;
        }

        .ajaxLoading::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 48px;
            height: 48px;
            border: 4px solid rgba(255, 255, 255, 0.3);
            border-top-color: #4ade80;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        @keyframes loginAppear {
            from {
                opacity: 0;
                transform: translateY(30px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @media (max-width: 991px) {
            body {
                overflow: auto;
            }

            .login-wrapper {
                padding: 20px;
            }

            .login-container {
                max-width: 500px;
                grid-template-columns: 1fr;
            }

            .login-brand {
                display: none;
            }

            .login-panel {
                padding: 45px 30px;
            }

            .mobile-brand {
                display: block;
            }
        }

        @media (max-width: 480px) {
            .login-wrapper {
                padding: 12px;
            }

            .login-panel {
                padding: 35px 22px;
            }

            .login-container {
                border-radius: 25px;
            }

            .login-header h2 {
                font-size: 25px;
            }
        }

        /* =====================================================
   LEFT BRANDING - EMERALD DARK
===================================================== */

.login-brand {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 60px;

    /* Modern Emerald Gradient */
    background: linear-gradient(
        135deg,
        #064e3b 0%,
        #047857 45%,
        #16a34a 100%
    );

    color: #ffffff;
    overflow: hidden;
}


/* =====================================================
   DECORATION
===================================================== */

.login-brand::before {
    content: '';

    position: absolute;

    width: 450px;
    height: 450px;

    border-radius: 50%;

    background: rgba(255, 255, 255, 0.08);

    top: -200px;
    right: -200px;
}


.login-brand::after {
    content: '';

    position: absolute;

    width: 350px;
    height: 350px;

    border-radius: 50%;

    background: rgba(134, 239, 172, 0.15);

    bottom: -150px;
    left: -150px;
}


/* Content Above Decoration */

.login-brand > * {
    position: relative;
    z-index: 2;
}


/* =====================================================
   LOGO
===================================================== */

.brand-logo {
    width: 95px;
    height: 95px;

    object-fit: contain;

    margin-bottom: 25px;

    padding: 15px;

    border-radius: 24px;

    background: rgb(255, 255, 255);

    backdrop-filter: blur(10px);

    border: 1px solid rgba(255, 255, 255, 0.25);
}


/* =====================================================
   BADGE
===================================================== */

.brand-badge {
    display: inline-flex;
    align-items: center;

    gap: 8px;

    width: max-content;

    padding: 8px 16px;

    margin-bottom: 20px;

    border-radius: 50px;

    background: rgba(255, 255, 255, 0.12);

    border: 1px solid rgba(255, 255, 255, 0.2);

    color: #ffffff;

    font-size: 12px;

    font-weight: 600;

    letter-spacing: 1px;
}


/* =====================================================
   TITLE
===================================================== */

.brand-title {
    font-size: 42px;

    font-weight: 800;

    line-height: 1.2;

    margin-bottom: 20px;

    color: #ffffff;
}


.brand-title span {
    color: #fde047;
}


/* =====================================================
   DESCRIPTION
===================================================== */

.brand-description {
    max-width: 480px;

    font-size: 15px;

    line-height: 1.8;

    color: rgba(255, 255, 255, 0.8);
}


/* =====================================================
   FEATURES
===================================================== */

.brand-features {
    display: flex;
    flex-direction: column;

    gap: 15px;

    margin-top: 35px;
}


.brand-feature {
    display: flex;

    align-items: center;

    gap: 15px;
}


.feature-icon {
    width: 44px;
    height: 44px;

    display: flex;

    align-items: center;
    justify-content: center;

    border-radius: 14px;

    background: rgba(255, 255, 255, 0.12);

    border: 1px solid rgba(255, 255, 255, 0.15);

    color: #fde047;
}


.brand-feature span {
    font-size: 13px;

    color: rgba(255, 255, 255, 0.85);
}

/* =====================================================
   REMEMBER CHECKBOX
===================================================== */

.remember-wrapper {
    margin-bottom: 20px;
}


/* Label */

.remember-label {
    display: inline-flex !important;

    align-items: center;

    gap: 10px;

    cursor: pointer;

    margin: 0;

    user-select: none;
}


/* Hide Default Checkbox */

.remember-checkbox {
    position: absolute;

    opacity: 0;

    width: 0;

    height: 0;
}


/* Custom Checkbox */

.remember-checkmark {
    width: 20px;

    height: 20px;

    display: flex;

    align-items: center;

    justify-content: center;

    flex-shrink: 0;

    border-radius: 6px;

    border: 2px solid #d1d5db;

    background: #ffffff;

    transition: all 0.2s ease;
}


/* Icon */

.remember-checkmark i {
    display: none;

    font-size: 11px;

    color: #ffffff;
}


/* Checked */

.remember-checkbox:checked
+ .remember-checkmark {
    background: #16a34a;

    border-color: #16a34a;
}


/* Show Check */

.remember-checkbox:checked
+ .remember-checkmark i {
    display: block;
}


/* Hover */

.remember-label:hover
.remember-checkmark {
    border-color: #16a34a;
}


/* Text */

.remember-text {
    font-size: 13px;

    font-weight: 500;

    color: #6b7280;
}
    </style>
</head>

<body>

    <section class="login-wrapper">

        <video
            class="login-video"
            autoplay
            muted
            loop
            playsinline>

            <source
                src="{{ asset('assets/images/kebun4.mp4') }}"
                type="video/mp4">
        </video>

        <div class="login-overlay"></div>

        <div class="login-container">

            <!-- Branding Desktop -->
            <div class="login-brand">

                <a href="{{ url('') }}">

                    @if(
                        file_exists(
                            public_path() . '/uploads/images/' . config('sximo')['cnf_logo']
                        ) &&
                        config('sximo')['cnf_logo'] != ''
                    )

                        <img
                            src="{{ asset('uploads/images/' . config('sximo')['cnf_logo']) }}"
                            alt="{{ config('sximo')['cnf_appname'] }}"
                            class="brand-logo">

                    @else

                        <img
                            src="{{ asset('uploads/logo.png') }}"
                            alt="{{ config('sximo')['cnf_appname'] }}"
                            class="brand-logo">

                    @endif

                    <img
                        src="{{ asset('assets/images/difaai.png') }}"
                        alt="DiFAAI"
                        class="brand-logo" style="width: 169px;">

                </a>

                <div class="brand-badge">
                    <i class="fa-solid fa-leaf"></i>
                    DIGITAL AGRICULTURE
                </div>

                <h1 class="brand-title">
                    Digital Intelligence
                    <br>
                    for
                    <span>Farming</span>
                </h1>

                <p class="brand-description">
                    Platform digital untuk mendukung pengelolaan
                    perkebunan tebu secara modern, cepat,
                    terintegrasi, dan berbasis data.
                </p>

                <div class="brand-features">

                    <div class="brand-feature">
                        <div class="feature-icon">
                            <i class="fa-solid fa-chart-line"></i>
                        </div>

                        <span>
                            Monitoring Perkebunan Real-time
                        </span>
                    </div>

                    <div class="brand-feature">
                        <div class="feature-icon">
                            <i class="fa-solid fa-satellite"></i>
                        </div>

                        <span>
                            Smart Farming & Digital Mapping
                        </span>
                    </div>

                    <div class="brand-feature">
                        <div class="feature-icon">
                            <i class="fa-solid fa-seedling"></i>
                        </div>

                        <span>
                            Data Terintegrasi & Analitik
                        </span>
                    </div>

                </div>

            </div>


            <!-- Login Panel -->
            <div class="login-panel">

                <div class="login-panel-inner">

                    <!-- Branding Mobile -->
                    <div class="mobile-brand">

                        @if(
                            file_exists(
                                public_path() . '/uploads/images/' . config('sximo')['cnf_logo']
                            ) &&
                            config('sximo')['cnf_logo'] != ''
                        )

                            <img
                                src="{{ asset('uploads/images/' . config('sximo')['cnf_logo']) }}"
                                alt="{{ config('sximo')['cnf_appname'] }}"
                                class="mobile-logo">

                        @else

                            <img
                                src="{{ asset('uploads/logo.png') }}"
                                alt="{{ config('sximo')['cnf_appname'] }}"
                                class="mobile-logo">

                        @endif

                        <h5>DIGITAL FARMING</h5>

                    </div>


                    <div class="login-header">

                        <h2>
                            Selamat Datang 👋
                        </h2>

                        <p>
                            Silakan login untuk melanjutkan
                            ke sistem Digital Farming.
                        </p>

                    </div>


                    @yield('content')


                    <div class="login-footer">

                        © {{ date('Y') }}
                        {{ config('sximo.cnf_appname') }}

                        <br>

                        Digital Farming Platform

                    </div>

                </div>

            </div>

        </div>

    </section>

    
    <!-- IG Reels popup (inserted at placeholder) -->
    <style>
    /* Backdrop */
    #ig-popup-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.45);
        display: none;
        align-items: center;
        justify-content: center;
        z-index: 9999;
        backdrop-filter: blur(4px);
        padding: 12px;
    }

    /* Modal */
    #ig-popup {
        width: 90%;
        max-width: 980px;
        max-height: 90vh;
        background: linear-gradient(135deg, rgba(255,255,255,0.95), rgba(250,250,250,0.9));
        border-radius: 18px;
        box-shadow: 0 20px 50px rgba(0,0,0,0.35);
        overflow: hidden;
        display: flex;
        gap: 0;
        transform: translateY(20px) scale(.98);
        opacity: 0;
        transition: opacity .28s ease, transform .28s ease;
        position: relative;
    }

    /* Show */
    #ig-popup-backdrop.show { display: flex; }
    #ig-popup-backdrop.show #ig-popup { transform: translateY(0) scale(1); opacity: 1; }

    /* Layout: image/video left, text right */
    #ig-popup .ig-left,
    #ig-popup .ig-right {
        box-sizing: border-box;
        padding: 18px;
    }

    /* Left image/video */
    #ig-popup .ig-left {
        width: 46%;
        min-height: 160px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(180deg, rgba(0,0,0,0.03), rgba(0,0,0,0.02));
        overflow: hidden;
        position: relative;
    }
    #ig-popup .ig-left img,
    #ig-popup .ig-left video {
        width: 100%;
        height: 100%;
        max-height: calc(90vh - 36px);
        object-fit: cover;
        border-radius: 10px;
        box-shadow: 0 10px 30px rgba(0,0,0,0.18);
        transition: transform .35s ease;
        display: block;
    }
    #ig-popup .ig-left img:hover { transform: scale(1.02); }

    /* Right text */
    #ig-popup .ig-right {
        width: 54%;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        gap: 12px;
        color: #222;
        overflow: auto;
        max-height: calc(90vh - 36px);
        padding-bottom: 12px;
    }
    #ig-popup .ig-title {
        font-size: 20px;
        font-weight: 600;
        margin: 0;
        color: #0b2a48;
    }
    #ig-popup .ig-body {
        font-size: 14.5px;
        line-height: 1.5;
        color: #333;
        overflow: auto;
        padding-right: 6px;
    }

    /* Controls */
    #ig-popup .ig-actions {
        display: flex;
        gap: 8px;
        margin-top: 8px;
        flex-wrap: wrap;
        align-items: center;
        /* keep actions visible on small screens */
        position: sticky;
        bottom: 0;
        background: linear-gradient(180deg, rgba(255,255,255,0.0), rgba(255,255,255,0.95));
        padding-top: 8px;
        padding-bottom: 8px;
    }
    #ig-popup .btn-visit {
        background: linear-gradient(90deg,#ff7a18,#ffb11a);
        color: #fff;
        padding: 10px 14px;
        border-radius: 8px;
        text-decoration: none;
        font-weight: 600;
        display: inline-block;
    }
    #ig-popup .btn-close {
        background: transparent;
        border: none;
        font-size: 16px;
        color: #666;
        cursor: pointer;
        padding: 8px 12px;
        border-radius: 8px;
    }

    /* Close icon top-right */
    #ig-popup .close-top {
        position: absolute;
        right: 12px;
        top: 10px;
        z-index: 3;
        background: rgba(255,255,255,0.8);
        border: none;
        width: 40px;
        height: 40px;
        border-radius: 10px;
        font-size: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    }

    /* Ensure focusable and tappable */
    #ig-popup .close-top:focus,
    #ig-popup .btn-close:focus,
    #ig-popup .btn-visit:focus {
        outline: 3px solid rgba(21, 156, 228, 0.35);
        outline-offset: 2px;
    }

    /* Responsive: stack vertical and ensure buttons visible */
    @media (max-width: 820px) {
        #ig-popup { flex-direction: column; width: 95%; max-height: 94vh; }
        #ig-popup .ig-left, #ig-popup .ig-right { width: 100%; padding: 12px; }
        #ig-popup .ig-left { min-height: 200px; }
        #ig-popup .ig-left img, #ig-popup .ig-left video { max-height: 40vh; }
        #ig-popup .ig-right { max-height: calc(94vh - 220px); padding-bottom: 6px; }
        #ig-popup .ig-body { max-height: 34vh; }

        #ig-popup .ig-actions {
            position: sticky;
            bottom: 0;
            background: linear-gradient(180deg, rgba(255,255,255,0.0), rgba(255,255,255,0.98));
            padding: 12px;
            gap: 10px;
            flex-direction: column;
            align-items: stretch;
        }
        #ig-popup .btn-visit, #ig-popup .btn-close {
            width: 100%;
            text-align: center;
        }
        #ig-popup .close-top {
            right: 10px;
            top: 8px;
            width: 44px;
            height: 44px;
            border-radius: 12px;
        }
    }

                /* Rainbow animated gradient text */
                .rainbow-text {
                    background: linear-gradient(90deg, #1de982, #319197 40%, #b6ffb1 70%, #18ff7a, #0b8c3c, #1de982);
                    background-size: 400% 100%;
                    -webkit-background-clip: text;
                    -webkit-text-fill-color: transparent;
                    animation: rainbow-move 3.5s linear infinite;
                    font-weight: 700;
                    letter-spacing: 1px;
                }
                @keyframes rainbow-move {
                    0% { background-position: 0% 50%; }
                    100% { background-position: 100% 50%; }
                }

                /* Fade-in animation for description */
                .fade-in-desc {
                    opacity: 0;
                    animation: fadeInDesc 1.8s ease 0.5s forwards;
                }
                @keyframes fadeInDesc {
                    from { opacity: 0; transform: translateY(18px);}
                    to { opacity: 1; transform: translateY(0);}
                }

                /* Optional: glowing border for the header box */
                .animated-header {
                    border-radius: 18px;
                    background: rgba(0,0,0,0.18);
                    box-shadow: 0 0 18px 2px #ffb11a66, 0 2px 24px 0 #31919733;
                    padding: 18px 8px 12px 8px;
                    margin-bottom: 10px;
                    position: relative;
                    overflow: hidden;
                }
                .animated-header::before {
                    content: "";
                    position: absolute;
                    inset: 0;
                    border-radius: 18px;
                    background: linear-gradient(120deg, #ffb11a44 0%, #31919744 100%);
                    opacity: 0.5;
                    z-index: 0;
                    pointer-events: none;
                }
                .animated-header > * {
                    position: relative;
                    z-index: 1;
                }
                </style>

    <div id="ig-popup-backdrop" aria-hidden="true">
        <div id="ig-popup" role="dialog" aria-modal="true" aria-label="Instagram Reel">
            <button class="close-top" id="ig-popup-close" title="Tutup" aria-label="Tutup">&times;</button>

            <div class="ig-left">
                <img id="ig-popup-img" src="" alt="Reel image" style="display:none">
                <video id="ig-popup-video" src="" poster="" controls autoplay muted loop playsinline style="display:none"></video>
            </div>

            <div class="ig-right">
                <h3 class="ig-title" id="ig-popup-title"></h3>
                <div class="ig-body" id="ig-popup-body"></div>
                <div class="ig-actions">
                    <a id="ig-popup-openlink" class="btn-visit" target="_blank" rel="noopener">Lihat di Instagram</a>
                    <button id="ig-popup-dismiss" class="btn-close">Tutup</button>
                </div>
            </div>
        </div>
    </div>

    <script>
    (function(){
        const API = 'https://prodis.ksotebu.com/home/getigreels';
        const backdrop = document.getElementById('ig-popup-backdrop');
        const popupImg = document.getElementById('ig-popup-img');
        const popupVideo = document.getElementById('ig-popup-video');
        const popupBody = document.getElementById('ig-popup-body');
        const popupTitle = document.getElementById('ig-popup-title');
        const openLink = document.getElementById('ig-popup-openlink');
        const closeTop = document.getElementById('ig-popup-close');
        const dismissBtn = document.getElementById('ig-popup-dismiss');

        // Utility: safe set text, preserve line breaks
        function setText(el, txt) {
            if(!txt) { el.innerHTML = ''; return; }
            const paragraphs = String(txt).trim().split(/\n{2,}/).map(p => p.replace(/\n/g, '<br>'));
            el.innerHTML = paragraphs.map(p => '<p style="margin:0 0 10px 0;">' + p + '</p>').join('');
        }

        function showPopup() {
            backdrop.classList.add('show');
            backdrop.setAttribute('aria-hidden', 'false');
            dismissBtn.focus();
        }
        function hidePopupInternal() {
            backdrop.classList.remove('show');
            backdrop.setAttribute('aria-hidden', 'true');
            // pause video if playing
            try { if (popupVideo && !popupVideo.paused) popupVideo.pause(); } catch(e){}
        }
        // We'll allow wrapping/hooking later, expose a variable
        let hidePopup = hidePopupInternal;

        closeTop.addEventListener('click', () => hidePopup());
        dismissBtn.addEventListener('click', () => hidePopup());
        backdrop.addEventListener('click', function(e){
            if(e.target === backdrop) hidePopup();
        });
        document.addEventListener('keydown', function(e){
            if(e.key === 'Escape') hidePopup();
        });

        // Fetch and populate content
        function populateFromData(data) {
            if(!data) return;
            let mediaUrl = '';
            let imageUrl = '';
            let text = '';
            let external = '';
            let isVideo = false;

            // Accept either an array of objects or single object
            const items = Array.isArray(data) ? data : [data];

            // Try to find image/video/text entries
            for(const it of items) {
                if(it && typeof it === 'object') {
                    // detect media url candidates
                    if(!mediaUrl) {
                        if(it.url) mediaUrl = it.url;
                        else if(it.media_url) mediaUrl = it.media_url;
                        else if(it.video_url) mediaUrl = it.video_url;
                        else if(it.image_url) mediaUrl = it.image_url;
                    }
                    // detect image specifically
                    if(!imageUrl) {
                        if(it.image_url) imageUrl = it.image_url;
                        else if(it.media_url && !it.is_video) imageUrl = it.media_url;
                    }
                    // text
                    if(!text && (it.text || it.caption)) text = it.text || it.caption;

                    // is video flag (accept several variants)
                    if(typeof it.is_video !== 'undefined') isVideo = isVideo || !!it.is_video;
                    if(typeof it.isVideo !== 'undefined') isVideo = isVideo || !!it.isVideo;

                    // external links
                    if(!external) {
                        if(it.permalink) external = it.permalink;
                        else if(it.link) external = it.link;
                        else if(it.url_link) external = it.url_link;
                        else if(it.pk) external = 'https://www.instagram.com/p/' + encodeURIComponent(it.pk) + '/';
                    }
                }
            }

            // fallbacks
            if(!imageUrl && mediaUrl && !isVideo) imageUrl = mediaUrl;
            if(!mediaUrl && imageUrl) mediaUrl = imageUrl;

            // Decide whether to show video or image
            if(isVideo) {
                // prefer explicit video_url or media_url
                const vidSrc = mediaUrl;
                if(vidSrc) {
                    popupVideo.src = vidSrc;
                    // set poster if image available
                    if(imageUrl) popupVideo.poster = imageUrl;
                    popupVideo.style.display = '';
                    popupImg.style.display = 'none';
                    try { popupVideo.currentTime = 0; popupVideo.play().catch(()=>{}); } catch(e){}
                } else {
                    // no video source -> fallback to image if available
                    if(imageUrl) {
                        popupImg.src = imageUrl;
                        popupImg.style.display = '';
                        popupVideo.style.display = 'none';
                    } else {
                        popupImg.style.display = 'none';
                        popupVideo.style.display = 'none';
                    }
                    isVideo = false;
                }
            } else {
                if(imageUrl) {
                    popupImg.src = imageUrl;
                    popupImg.style.display = '';
                } else {
                    // try first item media_url
                    const first = items[0] || {};
                    if(first.media_url) {
                        popupImg.src = first.media_url;
                        popupImg.style.display = '';
                    } else {
                        popupImg.style.display = 'none';
                    }
                }
                // ensure video hidden and stopped
                if(popupVideo) {
                    popupVideo.style.display = 'none';
                    try { popupVideo.pause(); popupVideo.removeAttribute('src'); popupVideo.load(); } catch(e){}
                }
            }

            setText(popupBody, text || 'Lihat konten terbaru kami.');
            popupTitle.textContent = '';

            if(external) {
                openLink.href = external;
                openLink.style.display = '';
            } else {
                openLink.href = window.location.origin;
                openLink.style.display = '';
            }

            showPopup();
            setTimeout(hidePopup, 38000);
            // clear previous timers if any
            if (backdrop._igTimers) {
                clearTimeout(backdrop._igTimers.timeout);
                clearInterval(backdrop._igTimers.interval);
                backdrop._igTimers = null;
            }

            const AUTO_HIDE_MS = 38000;
            const originalDismissText = (dismissBtn.textContent || 'Tutup').trim();
            let remaining = Math.ceil(AUTO_HIDE_MS / 1000);

            // auto hide timer
            const autoHideTimer = setTimeout(() => {
                hidePopup();
            }, AUTO_HIDE_MS);

            // show initial countdown on button
            dismissBtn.textContent = `${originalDismissText} (${remaining})`;

            // countdown interval
            const countdownInterval = setInterval(() => {
                remaining--;
                if (remaining <= 0) {
                    clearInterval(countdownInterval);
                    dismissBtn.textContent = originalDismissText;
                    return;
                }
                dismissBtn.textContent = `${originalDismissText} (${remaining})`;
            }, 1000);

            // store timers so we can clear if popup is opened again
            backdrop._igTimers = { timeout: autoHideTimer, interval: countdownInterval };

            // wrap hidePopup once to ensure timers are cleaned up whenever popup is closed
            if (!backdrop._igHideWrapped) {
                const _origHide = hidePopup;
                hidePopup = function () {
                    if (backdrop._igTimers) {
                        clearTimeout(backdrop._igTimers.timeout);
                        clearInterval(backdrop._igTimers.interval);
                        backdrop._igTimers = null;
                    }
                    dismissBtn.textContent = originalDismissText;
                    // pause video when hiding
                    try { if (popupVideo && !popupVideo.paused) popupVideo.pause(); } catch(e){}
                    _origHide();
                };
                backdrop._igHideWrapped = true;
            }
        }

        function fetchWithTimeout(url, timeout = 8000) {
            return new Promise((resolve, reject) => {
                const timer = setTimeout(() => reject(new Error('timeout')), timeout);
                fetch(url, { credentials: 'omit' }).then(r => {
                    clearTimeout(timer);
                    if(!r.ok) return reject(new Error('Network response not ok'));
                    return r.json();
                }).then(json => resolve(json)).catch(err => reject(err));
            });
        }

        document.addEventListener('DOMContentLoaded', function(){
            fetchWithTimeout(API, 9000)
                .then(json => {
                    const payload = json && json.data ? json.data : json;
                    populateFromData(payload);
                })
                .catch(err => {
                    console.warn('IG popup load failed:', err);
                });
        });

    })();
    </script>
</body> 
</html>