// Mobile Menu Toggle
const hamburger = document.querySelector('.hamburger');
const navMenu = document.querySelector('.nav-menu');

hamburger.addEventListener('click', () => {
    hamburger.classList.toggle('active');
    navMenu.classList.toggle('active');
});

// Close mobile menu when clicking on a link
document.querySelectorAll('.nav-link').forEach(n => n.addEventListener('click', () => {
    hamburger.classList.remove('active');
    navMenu.classList.remove('active');
}));

// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        const target = document.querySelector(this.getAttribute('href'));
        if (target) {
            target.scrollIntoView({
                behavior: 'smooth',
                block: 'start'
            });
        }
    });
});

// Animate on scroll
const observerOptions = {
    threshold: 0.1,
    rootMargin: '0px 0px -50px 0px'
};

const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            entry.target.style.opacity = '1';
            entry.target.style.transform = 'translateY(0)';
        }
    });
}, observerOptions);

// Observe elements for animation
document.querySelectorAll('.service-card, .stat-item').forEach(el => {
    el.style.opacity = '0';
    el.style.transform = 'translateY(30px)';
    el.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
    observer.observe(el);
});

/* ============================================
   SUB INSTANSI - FULL JAVASCRIPT
   ============================================ */

// DATA INSTANSI (Mudah ditambah)
const instansiData = [
    {
        name: "Kejaksaan Negeri Medan",
        region: "sumatera",
        city: "Kota Medan, Sumatera Utara",
        desc: "Penegakan hukum wilayah Kota Medan dan sekitarnya secara profesional.",
        kasus: "120+",
        jaksa: "50+",
        berdiri: "2001",
        url: "#",
        telp: "+62611234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Aceh",
        region: "sumatera",
        city: "Banda Aceh, Aceh",
        desc: "Penegakan hukum wilayah Provinsi Aceh dengan memperhatikan kearifan lokal.",
        kasus: "95+",
        jaksa: "40+",
        berdiri: "1998",
        url: "#",
        telp: "+62651234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Jakarta",
        region: "jawa",
        city: "DKI Jakarta",
        desc: "Penegakan hukum di wilayah Ibu Kota Negara dengan standar tertinggi.",
        kasus: "200+",
        jaksa: "80+",
        berdiri: "1995",
        url: "#",
        telp: "+62211234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Surabaya",
        region: "jawa",
        city: "Kota Surabaya, Jawa Timur",
        desc: "Penegakan hukum wilayah Surabaya dan Jawa Timur secara profesional.",
        kasus: "150+",
        jaksa: "65+",
        berdiri: "1997",
        url: "#",
        telp: "+62311234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Makassar",
        region: "sulawesi",
        city: "Kota Makassar, Sulawesi Selatan",
        desc: "Penegakan hukum wilayah Sulawesi Selatan dan sekitarnya.",
        kasus: "110+",
        jaksa: "45+",
        berdiri: "2000",
        url: "#",
        telp: "+62411234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Jayapura",
        region: "papua",
        city: "Kota Jayapura, Papua",
        desc: "Penegakan hukum wilayah Papua dengan pendekatan keadilan restoratif.",
        kasus: "80+",
        jaksa: "35+",
        berdiri: "2003",
        url: "#",
        telp: "+62967234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Pontianak",
        region: "kalimantan",
        city: "Kota Pontianak, Kalimantan Barat",
        desc: "Penegakan hukum wilayah Kalimantan Barat secara adil dan profesional.",
        kasus: "90+",
        jaksa: "38+",
        berdiri: "1999",
        url: "#",
        telp: "+62561234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Bandung",
        region: "jawa",
        city: "Kota Bandung, Jawa Barat",
        desc: "Penegakan hukum wilayah Bandung dan Jawa Barat secara profesional.",
        kasus: "130+",
        jaksa: "55+",
        berdiri: "1996",
        url: "#",
        telp: "+62221234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Palembang",
        region: "sumatera",
        city: "Kota Palembang, Sumatera Selatan",
        desc: "Penegakan hukum wilayah Sumatera Selatan secara adil dan transparan.",
        kasus: "100+",
        jaksa: "42+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Pekanbaru",
        region: "sumatera",
        city: "Kota Pekanbaru, Riau",
        desc: "Penegakan hukum wilayah Kepulauan Riau secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Batam",
        region: "sumatera",
        city: "Kota Batam, Kepulauan Riau",
        desc: "Penegakan hukum wilayah Kepulauan Riau secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Padang",
        region: "sumatera",
        city: "Kota Padang, Sumatera Barat",
        desc: "Penegakan hukum wilayah Sumatera Barat secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Bukittinggi",
        region: "sumatera",
        city: "Kota Bukittinggi, Sumatera Barat",
        desc: "Penegakan hukum wilayah Sumatera Barat secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Jambi",
        region: "sumatera",
        city: "Kota Jambi, Sumatera",
        desc: "Penegakan hukum wilayah Sumatera dan Jambi secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Bengkulu",
        region: "sumatera",
        city: "Kota Bengkulu, pantai barat daya Sumatra",
        desc: "Penegakan hukum wilayah pantai barat daya dan Bengkulu secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Bandar Lampung",
        region: "sumatera",
        city: "Kota Bandar Lampung, pantai barat daya Sumatra",
        desc: "Penegakan hukum wilayah Lampung secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Lhokseumawe",
        region: "sumatera",
        city: "Kota Lhokseumawe, Aceh",
        desc: "Penegakan hukum wilayah Lhokseumawe secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri bekasi",
        region: "Jawa Barat",
        city: "kota bekasi, Jawa Barat",
        desc: "Penegakan hukum wilayah bekasi secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Bogor",
        region: "Jawa Barat",
        city: "kota Bogor, Jawa Barat",
        desc: "Penegakan hukum wilayah Bogor secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Depok",
        region: "Jawa Barat",
        city: "kota Depok, Jawa Barat",
        desc: "Penegakan hukum wilayah Depok secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Tanggerang",
        region: "Banten",
        city: "kota Tanggerang, Banten",
        desc: "Penegakan hukum wilayah Tanggerang secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Serang",
        region: "Banten",
        city: "kota Serang, Banten",
        desc: "Penegakan hukum wilayah Serang secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Solo",
        region: "Jawa Tengah",
        city: "kota Solo, Jawa Tengah",
        desc: "Penegakan hukum wilayah Solo secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Yogyakarta",
        region: "Daerah Istimewa Yogyakarta",
        city: "kota Yogyakarta, Daerah Istimewa Yogyakarta",
        desc: "Penegakan hukum wilayah Yogyakarta secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    {
        name: "Kejaksaan Negeri Malang",
        region: "Jawa Timur",
        city: "kota Malang, Jawa Timur",
        desc: "Penegakan hukum wilayah Malang secara adil dan transparan.",
        kasus: "50+",
        jaksa: "22+",
        berdiri: "2000",
        url: "#",
        telp: "+62711234567",
        status: "aktif"
    },
    // 🔥 TAMBAH INSTANSI BARU DI SINI
    // Copy format di atas dan tambahkan koma
];

// BADGE COLOR MAP
const badgeColors = {
    sumatera: { bg: 'rgba(255,152,0,0.15)', color: '#ff9800', border: 'rgba(255,152,0,0.3)' },
    jawa: { bg: 'rgba(33,150,243,0.15)', color: '#2196f3', border: 'rgba(33,150,243,0.3)' },
    kalimantan: { bg: 'rgba(76,175,80,0.15)', color: '#4caf50', border: 'rgba(76,175,80,0.3)' },
    sulawesi: { bg: 'rgba(156,39,176,0.15)', color: '#9c27b0', border: 'rgba(156,39,176,0.3)' },
    papua: { bg: 'rgba(0,188,212,0.15)', color: '#00bcd4', border: 'rgba(0,188,212,0.3)' }
};

// RENDER CARDS
function renderCards(data) {
    const grid = document.getElementById('instansiGrid');
    if (!grid) return;

    grid.innerHTML = '';

    if (data.length === 0) { grid.innerHTML = `
            <div class="no-result">
                <i class="fas fa-search"></i>
                <h3>Instansi Tidak Ditemukan</h3>
                <p>Coba kata kunci lain atau pilih filter berbeda</p>
            </div>`;
        document.getElementById('visibleCount').textContent = '0';
        return;
    }

    data.forEach((item, index) => {
        const badge = badgeColors[item.region] || badgeColors.jawa;
        const regionLabel = item.region.charAt(0).toUpperCase() + item.region.slice(1);

        const card = `
        <div class="instansi-card" 
             data-region="${item.region}" 
             data-name="${item.name.toLowerCase()}"
             style="animation-delay: ${index * 0.1}s">
            <div class="card-ribbon">Aktif</div>
            <div class="card-header">
                <div class="card-logo">
                    <img src="https://annturnercook.com/Kejaksaan_Agung_Republik_Indonesia_new_logo.png" 
                         alt="Logo ${item.name}"
                         loading="lazy"
                         onerror="this.src='https://via.placeholder.com/80x80/d32f2f/fff?text=KN'">
                </div>
                <div class="card-badge" style="
                    background: ${badge.bg}; 
                    color: ${badge.color}; 
                    border: 1px solid ${badge.border};
                    padding: 6px 14px;
                    border-radius: 50px;
                    font-size: 0.78rem;
                    font-weight: 600;
                    display: flex;
                    align-items: center;
                    gap: 5px;">
                    <i class="fas fa-map-marker-alt"></i> ${regionLabel}
                </div>
            </div>
            <div class="card-body">
                <h3>${item.name}</h3>
                <p class="card-location">
                    <i class="fas fa-city"></i> ${item.city}
                </p>
                <p class="card-desc">${item.desc}</p>
                <div class="card-stats">
                    <div class="mini-stat">
                        <span class="mini-num">${item.kasus}</span>
                        <span class="mini-label">Kasus</span>
                    </div>
                    <div class="mini-stat">
                        <span class="mini-num">${item.jaksa}</span>
                        <span class="mini-label">Jaksa</span>
                    </div>
                    <div class="mini-stat">
                        <span class="mini-num">${item.berdiri}</span>
                        <span class="mini-label">Berdiri</span>
                    </div>
                </div>
            </div>
            <div class="card-footer">
                <a href="${item.url}" 
                   target="_blank" 
                   rel="noopener noreferrer" 
                   class="visit-btn">
                    <i class="fas fa-external-link-alt"></i> Kunjungi Website
                </a>
                <a href="tel:${item.telp}" class="call-btn" title="Hubungi via Telepon">
                    <i class="fas fa-phone"></i>
                </a>
            </div>
        </div>`;

        grid.innerHTML += card;
    });

    // UPDATE COUNTER
    document.getElementById('visibleCount').textContent = data.length;
    document.getElementById('totalCount').textContent = instansiData.length;
}

// ============================================
// FILTER BY REGION
// ============================================
let currentRegion = 'all';
let currentSearch = '';

function filterByRegion(region, btn) {
    currentRegion = region;

    // Update active tab
    document.querySelectorAll('.filter-tab').forEach(t => t.classList.remove('active'));
    btn.classList.add('active');

    applyFilters();
}

// ============================================
// SEARCH FILTER
// ============================================
function filterInstansi() {
    currentSearch = document.getElementById('searchInstansi').value.toLowerCase();
    applyFilters();
}

// ============================================
// APPLY ALL FILTERS
// ============================================
function applyFilters() {
    let filtered = instansiData;

    // Filter by region
    if (currentRegion !== 'all') {
        filtered = filtered.filter(item => item.region === currentRegion);
    }

    // Filter by search
    if (currentSearch !== '') {
        filtered = filtered.filter(item =>
            item.name.toLowerCase().includes(currentSearch) ||
            item.city.toLowerCase().includes(currentSearch) ||
            item.region.toLowerCase().includes(currentSearch)
        );
    }

    renderCards(filtered);

    // Show/hide load more
    const loadMoreBtn = document.getElementById('loadMoreBtn');
    if (loadMoreBtn) {
        loadMoreBtn.style.display = filtered.length >= instansiData.length ? 'none' : 'inline-flex';
    }
}

// ============================================
// LOAD MORE
// ============================================
let visibleItems = 9;

function loadMore() {
    visibleItems += 6;
    const btn = document.getElementById('loadMoreBtn');

    // Animate button
    btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Memuat...';
    btn.disabled = true;

    setTimeout(() => {
        renderCards(instansiData.slice(0, visibleItems));
        btn.innerHTML = '<i class="fas fa-plus-circle"></i> Tampilkan Lebih Banyak';
        btn.disabled = false;

        if (visibleItems >= instansiData.length) {
            btn.style.display = 'none';
        }
    }, 800);
}

// ============================================
// INIT ON DOM READY
// ============================================
document.addEventListener('DOMContentLoaded', function () {
    renderCards(instansiData);

    // ANIMATE CARDS ON SCROLL
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.style.opacity = '1';
                entry.target.style.transform = 'translateY(0)';
            }
        });
    }, { threshold: 0.1 });

    document.querySelectorAll('.instansi-card').forEach(card => {
        card.style.opacity = '0';
        card.style.transform = 'translateY(30px)';
        card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        observer.observe(card);
    });

    // SEARCH DEBOUNCE
    const searchInput = document.getElementById('searchInstansi');
    if (searchInput) {
        let debounceTimer;
        searchInput.addEventListener('input', function () {
            clearTimeout(debounceTimer);
            debounceTimer = setTimeout(() => {
                filterInstansi();
            }, 300);
        });
    }
});

// ============================================
// TAMBAH INSTANSI BARU - CARA MUDAH
// ============================================
// Cukup tambahkan object baru ke array instansiData di atas
// Contoh:
// {
//     name: "Kejaksaan Negeri Semarang",
//     region: "jawa",
//     city: "Kota Semarang, Jawa Tengah",
//     desc: "Penegakan hukum wilayah Jawa Tengah.",
//     kasus: "115+",
//     jaksa: "48+",
//     berdiri: "1998",
//     url: "https://kejarisemarang.org",
//     telp: "+62241234567",
//     status: "aktif"
// }