<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Ketua RT') — Sistem Zakat Warga</title>
    <link rel="dns-prefetch" href="//fonts.googleapis.com">
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link rel="dns-prefetch" href="//cdn.jsdelivr.net">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    {{-- NProgress CSS (local) --}}
    <link rel="stylesheet" href="{{ asset('css/nprogress.css') }}">
    <link rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'" href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap">
    <noscript><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap"></noscript>
    <link rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
    <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css"></noscript>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        :root {
            --green-primary: #1a6b3c;
            --green-light:   #e8f5ee;
            --green-mid:     #2e8b57;
            --green-hover:   #f0faf4;
            --sidebar-w:     240px;
            --topbar-h:      70px;
            --text-dark:     #111827;
            --text-muted:    #6b7280;
            --border-color:  #e5e7eb;
            --bg-page:       #f9fafb;
            --white:         #ffffff;
            --shadow-sm:     0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
            --shadow-md:     0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
            --radius:        12px;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg-page);
            color: var(--text-dark);
            min-height: 100vh;
            display: flex;
        }

        /* ===== SIDEBAR OVERLAY (Mobile) ===== */
        .sidebar-overlay {
            display: none;
            position: fixed; inset: 0;
            background: rgba(0,0,0,0.45);
            z-index: 99; opacity: 0;
            transition: opacity .25s ease;
        }
        .sidebar-overlay.active { display: block; opacity: 1; }

        /* ===== SIDEBAR ===== */
        .sidebar {
            width: var(--sidebar-w);
            min-height: 100vh;
            background: var(--white);
            border-right: 1px solid var(--border-color);
            display: flex;
            flex-direction: column;
            position: fixed;
            top: 0; left: 0; bottom: 0;
            z-index: 100;
            transition: transform .25s ease;
        }

        .sidebar-logo {
            padding: 20px 20px 16px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-bottom: 1px solid var(--border-color);
        }
        .sidebar-logo-icon {
            width: 38px; height: 38px;
            background: var(--green-primary);
            border-radius: 10px;
            display: flex; align-items: center; justify-content: center;
            color: white; font-size: 18px; flex-shrink: 0;
        }
        .sidebar-logo-text { line-height: 1.2; }
        .sidebar-logo-title {
            font-size: 14px; font-weight: 700;
            color: var(--green-primary); letter-spacing: -0.3px;
        }
        .sidebar-logo-sub { font-size: 11px; color: var(--text-muted); }

        .sidebar-nav {
            flex: 1;
            padding: 20px 0;
            overflow-y: auto;
        }

        .nav-item {
            display: flex; align-items: center; gap: 14px;
            padding: 12px 24px;
            font-size: 13.5px; font-weight: 600;
            color: var(--text-dark);
            text-decoration: none;
            transition: all .15s ease;
            position: relative;
        }
        .nav-item i { font-size: 18px; color: var(--text-muted); transition: color .15s; }
        .nav-item:hover { background: var(--green-hover); }
        .nav-item:hover i { color: var(--green-primary); }
        .nav-item.active { background: var(--green-light); color: var(--green-primary); }
        .nav-item.active i { color: var(--green-primary); }
        .nav-item.active::before {
            content: '';
            position: absolute; left: 0; top: 0; bottom: 0;
            width: 4px; background: var(--green-primary);
        }

        .sidebar-footer {
            padding: 12px;
            border-top: 1px solid var(--border-color);
        }
        .sidebar-logout-form { margin: 0; }
        .sidebar-logout {
            display: flex; align-items: center; gap: 10px;
            width: 100%; padding: 9px 12px;
            border-radius: 8px;
            font-size: 13.5px; font-weight: 500;
            color: #ef4444;
            background: none; border: none;
            cursor: pointer; transition: background .15s ease;
            text-align: left;
        }
        .sidebar-logout:hover { background: #fef2f2; color: #dc2626; }

        /* ===== MAIN AREA ===== */
        .main-area {
            margin-left: var(--sidebar-w);
            flex: 1;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            min-width: 0;
        }

        /* ===== TOPBAR ===== */
        .topbar {
            height: var(--topbar-h);
            background: var(--white);
            border-bottom: 1px solid var(--border-color);
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 32px;
            position: sticky; top: 0; z-index: 50;
            gap: 12px;
        }

        .topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }

        .hamburger-btn {
            display: none;
            align-items: center; justify-content: center;
            width: 38px; height: 38px;
            border: 1px solid var(--border-color);
            background: var(--white);
            border-radius: 8px;
            cursor: pointer; font-size: 20px;
            color: var(--text-dark); flex-shrink: 0;
            transition: background .15s;
        }
        .hamburger-btn:hover { background: var(--bg-page); }

        .topbar-title {
            font-size: 18px; font-weight: 700;
            color: var(--text-dark);
            white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
        }

        .topbar-right {
            display: flex; align-items: center; gap: 20px; flex-shrink: 0;
        }

        .topbar-date {
            font-size: 13px; color: var(--text-muted); font-weight: 500;
            white-space: nowrap;
        }

        .topbar-icon-btn {
            color: var(--text-dark); font-size: 20px;
            cursor: pointer; transition: all .15s;
            text-decoration: none; position: relative;
        }
        .topbar-icon-btn:hover { color: var(--green-primary); }

        .notif-dot {
            position: absolute; top: 2px; right: 2px;
            width: 8px; height: 8px;
            background-color: #ef4444; border-radius: 50%;
            border: 2px solid white;
        }

        /* ===== PAGE CONTENT ===== */
        .page-content {
            padding: 32px;
            flex: 1;
        }

        .page-header {
            display: flex; align-items: flex-start; justify-content: space-between;
            margin-bottom: 24px; gap: 12px;
        }
        .page-header-left h1 {
            font-size: 24px; font-weight: 800; color: var(--text-dark);
            letter-spacing: -0.5px; margin-bottom: 4px;
        }
        .page-header-left p { font-size: 13.5px; color: var(--text-muted); margin: 0; }

        /* ===== CARD ===== */
        .card {
            background: var(--white);
            border: 1px solid var(--border-color);
            border-radius: var(--radius);
            box-shadow: var(--shadow-sm);
        }

        /* ===== BUTTONS ===== */
        .btn-primary-green {
            display: inline-flex; align-items: center; gap: 6px;
            background: var(--green-primary); color: white;
            border: none; border-radius: 8px;
            padding: 9px 16px;
            font-size: 13.5px; font-weight: 600;
            cursor: pointer; text-decoration: none;
            transition: background .15s; white-space: nowrap;
        }
        .btn-primary-green:hover { background: var(--green-mid); color: white; }

        .btn-outline {
            display: inline-flex; align-items: center; gap: 6px;
            background: white; color: var(--text-dark);
            border: 1px solid var(--border-color); border-radius: 8px;
            padding: 8px 14px;
            font-size: 13px; font-weight: 500;
            cursor: pointer; text-decoration: none;
            transition: all .15s; white-space: nowrap;
        }
        .btn-outline:hover { background: var(--bg-page); color: var(--text-dark); }

        /* ===== TABLE — Responsif ===== */
        .table-responsive {
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }
        .data-table {
            width: 100%; border-collapse: collapse;
            min-width: 580px;
        }
        .data-table thead th {
            font-size: 11px; font-weight: 700; text-transform: uppercase;
            letter-spacing: 0.6px; color: var(--text-muted);
            padding: 14px 20px;
            border-bottom: 1px solid var(--border-color);
            background: var(--white); text-align: left;
            white-space: nowrap;
        }
        .data-table tbody td {
            padding: 16px 20px;
            font-size: 13.5px; color: var(--text-dark);
            border-bottom: 1px solid var(--border-color);
            vertical-align: middle;
        }
        .data-table tbody tr:last-child td { border-bottom: none; }
        .data-table tbody tr:hover { background: var(--bg-page); }

        /* ===== BADGE ===== */
        .badge-pill {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 11px; font-weight: 600;
        }
        .badge-green   { background: var(--green-light); color: var(--green-primary); }
        .badge-orange  { background: #fff7ed; color: #ea580c; }
        .badge-red     { background: #fee2e2; color: #ef4444; }
        .badge-gray    { background: #f3f4f6; color: #374151; }
        .badge-blue    { background: #eff6ff; color: #2563eb; }

        /* ===== FORM CONTROLS ===== */
        .form-ctrl {
            width: 100%; padding: 10px 14px;
            border: 1px solid var(--border-color); border-radius: 8px;
            font-size: 13.5px; font-family: 'Inter', sans-serif;
            color: var(--text-dark); background: white;
            transition: border-color .15s; appearance: none;
        }
        .form-ctrl:focus {
            outline: none; border-color: var(--green-primary);
            box-shadow: 0 0 0 3px rgba(26,107,60,.1);
        }
        .form-label-styled {
            display: block; font-size: 12.5px; font-weight: 600;
            color: var(--text-dark); margin-bottom: 6px;
        }
        .select-wrap { position: relative; }
        .select-wrap::after {
            content: '\F282'; font-family: 'Bootstrap-icons';
            position: absolute; right: 12px; top: 50%;
            transform: translateY(-50%);
            color: var(--text-muted); pointer-events: none; font-size: 12px;
        }
        .select-wrap select { padding-right: 32px; }

        /* ===== FILTER GRID ===== */
        .filter-grid {
            display: grid; gap: 12px;
            grid-template-columns: 1fr 1fr 1fr;
            margin-bottom: 14px;
        }
        .filter-grid-2 { grid-template-columns: 1fr 1fr; }

        /* ===== PAGINATION ===== */
        .pagination-wrap {
            display: flex; align-items: center; justify-content: space-between;
            padding: 14px 16px;
            border-top: 1px solid var(--border-color);
            font-size: 13px; color: var(--text-muted);
            flex-wrap: wrap; gap: 8px;
        }
        .pagination-btns { display: flex; gap: 6px; }
        .page-btn {
            width: 32px; height: 32px; border-radius: 6px;
            border: 1px solid var(--border-color);
            background: white; color: var(--text-dark);
            font-size: 13px; font-weight: 500;
            cursor: pointer; display: flex; align-items: center; justify-content: center;
            text-decoration: none; transition: all .15s;
        }
        .page-btn:hover { background: var(--bg-page); }
        .page-btn.active { background: var(--green-primary); color: white; border-color: var(--green-primary); }
        .page-btn:disabled { opacity: 0.4; cursor: default; }

        /* ===== ALERT SUCCESS ===== */
        .alert-success-custom {
            background: #f0fdf4; border: 1px solid #bbf7d0;
            border-radius: 8px; padding: 14px 20px;
            font-size: 13.5px; color: #166534; font-weight: 500;
            display: flex; align-items: center; gap: 10px;
            margin-bottom: 24px;
        }

        /* ===== ACTION BUTTONS ===== */
        .action-icon-btn {
            width: 30px; height: 30px; border-radius: 6px;
            border: none; background: none;
            display: inline-flex; align-items: center; justify-content: center;
            cursor: pointer; font-size: 17px; transition: all .15s; text-decoration: none;
        }
        .action-icon-btn.edit { color: var(--green-primary); }
        .action-icon-btn.edit:hover { background: var(--green-light); }
        .action-icon-btn.del { color: #ef4444; }
        .action-icon-btn.del:hover { background: #fee2e2; }

        /* =============================================
           RESPONSIVE — MOBILE FIRST
           ============================================= */

        @media (max-width: 1024px) {
            .filter-grid { grid-template-columns: 1fr 1fr; }
        }

        @media (max-width: 768px) {
            .sidebar { transform: translateX(-100%); }
            .sidebar.open { transform: translateX(0); }
            .main-area { margin-left: 0; }
            .hamburger-btn { display: flex; }

            .topbar { padding: 0 16px; }
            .topbar-title { font-size: 15px; }
            .topbar-date { display: none; }

            .page-content { padding: 16px; }
            .page-header { flex-direction: column; align-items: stretch; gap: 12px; }
            .page-header-left h1 { font-size: 20px; }

            .filter-grid { grid-template-columns: 1fr; }
            .filter-grid-2 { grid-template-columns: 1fr; }
            .pagination-wrap { flex-direction: column; align-items: flex-start; }
        }

        @media (max-width: 480px) {
            .page-content { padding: 12px; }
            .topbar-icon-btn.hide-xs { display: none; }
        }

        /* ===== PAGE TRANSITION ===== */
        .page-content {
            transition: opacity 0.15s ease;
        }

        /* ===== NPROGRESS CUSTOM ===== */
        #nprogress .bar {
            background: var(--green-primary) !important;
            height: 3px !important;
        }
        #nprogress .peg {
            box-shadow: 0 0 10px var(--green-primary), 0 0 5px var(--green-primary) !important;
        }
    </style>
    @stack('styles')
</head>
<body>

<!-- ===== SIDEBAR OVERLAY (Mobile) ===== -->
<div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>

<!-- ===== SIDEBAR ===== -->
<aside class="sidebar" id="mainSidebar">
    <div class="sidebar-logo">
        <div class="sidebar-logo-icon"><i class="bi bi-moon-stars-fill"></i></div>
        <div class="sidebar-logo-text">
            <div class="sidebar-logo-title">Zakat Warga</div>
            <div class="sidebar-logo-sub">Ketua RT Dashboard</div>
        </div>
    </div>

    <nav class="sidebar-nav">
        <a href="{{ url('/rt/dashboard') }}"
           class="nav-item {{ request()->is('rt/dashboard') ? 'active' : '' }}">
            <i class="bi bi-grid-1x2"></i> Dashboard
        </a>
        <a href="{{ url('/rt/data-warga') }}"
           class="nav-item {{ request()->is('rt/data-warga*') ? 'active' : '' }}">
            <i class="bi bi-people"></i> Data KK & Mustahik
        </a>
        <a href="{{ url('/rt/status-pengajuan') }}"
           class="nav-item {{ request()->is('rt/status-pengajuan*') ? 'active' : '' }}">
            <i class="bi bi-check2-square"></i> Status Pengajuan
        </a>
    </nav>

    <div class="sidebar-footer">
        <form method="POST" action="{{ route('logout') }}" class="sidebar-logout-form">
            @csrf
            <button type="submit" class="sidebar-logout">
                <i class="bi bi-box-arrow-right"></i> Keluar
            </button>
        </form>
    </div>
</aside>

<!-- ===== MAIN AREA ===== -->
<div class="main-area">

    <!-- TOPBAR -->
    <header class="topbar">
        <div class="topbar-left">
            <button class="hamburger-btn" id="hamburgerBtn" onclick="openSidebar()" aria-label="Buka menu">
                <i class="bi bi-list"></i>
            </button>
            <div class="topbar-title">
                @yield('topbar_title')
            </div>
        </div>
        <div class="topbar-right">
            <div class="topbar-date" id="dateDisplay">—</div>
            <div class="topbar-icon-btn hide-xs">
                <i class="bi bi-bell"></i>
                <div class="notif-dot"></div>
            </div>
            <div style="display:flex;align-items:center;gap:8px;margin-left:8px;padding-left:16px;border-left:1px solid var(--border-color);">
                <div style="text-align: right; line-height: 1.3;" class="hide-xs-block">
                    <div style="font-size: 13px; font-weight: 600; color: var(--text-dark);">{{ Auth::user()->nama_user }}</div>
                    <div style="font-size: 11px; color: var(--text-muted);">Ketua RT {{ Auth::user()->wilayah ? str_pad(Auth::user()->wilayah->rt, 2, '0', STR_PAD_LEFT) : '' }}</div>
                </div>
                <div style="overflow: hidden; width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;"><img src="https://avatar.iran.liara.run/public/boy" alt="Avatar" style="width: 100%; height: 100%; object-fit: cover;"></div>
            </div>
        </div>
    </header>

    <!-- PAGE CONTENT -->
    <main class="page-content">
        @if(session('success'))
            <div class="alert-success-custom">
                <i class="bi bi-check-circle-fill" style="font-size:18px;"></i>
                {{ session('success') }}
            </div>
        @endif

        @yield('content')

        {{-- Modals rendered inside so fast-nav can update them --}}
        @yield('modals')
        
        {{-- Page-specific scripts rendered inside so fast-nav can evaluate them --}}
        @stack('scripts')
    </main>
</div>

<script>
// ===== Hamburger Sidebar Toggle =====
function openSidebar() {
    document.getElementById('mainSidebar').classList.add('open');
    document.getElementById('sidebarOverlay').classList.add('active');
    document.body.style.overflow = 'hidden';
}
function closeSidebar() {
    document.getElementById('mainSidebar').classList.remove('open');
    document.getElementById('sidebarOverlay').classList.remove('active');
    document.body.style.overflow = '';
}
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') closeSidebar();
});

// ===== Tanggal Masehi & Hijriah =====
(function() {
    const today = new Date();
    const masehi = today.toLocaleDateString('id-ID', { day: '2-digit', month: 'short', year: 'numeric' });
    const hijriMonths = ['Muharram','Safar','Rabiul Awal','Rabiul Akhir','Jumadil Awal','Jumadil Akhir','Rajab','Syaban','Ramadan','Syawal','Zulkaidah','Zulhijjah'];
    const jd = Math.floor((today.getTime() / 86400000) + 2440587.5);
    const l = jd - 1948440 + 10632;
    const n = Math.floor((l - 1) / 10631);
    const ll = l - 10631 * n + 354;
    const j = Math.floor((10985 - ll) / 5316) * Math.floor((50 * ll) / 17719) + Math.floor(ll / 5670) * Math.floor((43 * ll) / 15238);
    const lll = ll - Math.floor((30 - j) / 15) * Math.floor((17719 * j) / 50) - Math.floor(j / 16) * Math.floor((15238 * j) / 43) + 29;
    const m = Math.floor((24 * lll) / 709);
    const d = lll - Math.floor((709 * m) / 24);
    const y = 30 * n + j - 30;
    const hijriStr = `${d} ${hijriMonths[m - 1] ?? ''} ${y}H`;
    document.getElementById('dateDisplay').textContent = `${masehi} / ${hijriStr}`;
})();
</script>
{{-- NProgress + Fast Navigation (tanpa reload) --}}
<script src="{{ asset('js/nprogress.min.js') }}"></script>
<script>
// NProgress untuk form POST (hapus, simpan, dll)
document.addEventListener('submit', function(e) {
    var form = e.target;
    if (form.method && form.method.toLowerCase() === 'post') {
        if (typeof NProgress !== 'undefined') NProgress.start();
    }
});
</script>
<script src="{{ asset('js/fast-nav.js') }}"></script>
</body>
</html>
