<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loans in Arrears - Advanced Dashboard</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary: #4f46e5;
            --primary-dark: #4338ca;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
            --info: #3b82f6;
            --dark: #1e293b;
            --light: #f8fafc;
            --text: #475569;
            --border: #e2e8f0;
            --shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
            --shadow-sm: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            position: relative;
            overflow-x: hidden;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="%23ffffff" fill-opacity="0.03" d="M0,224L48,213.3C96,203,192,181,288,186.7C384,192,480,224,576,229.3C672,235,768,213,864,186.7C960,160,1056,128,1152,133.3C1248,139,1344,181,1392,202.7L1440,224L1440,0L1392,0C1344,0,1248,0,1152,0C1056,0,960,0,864,0C768,0,672,0,576,0C480,0,384,0,288,0C192,0,96,0,48,0L0,0Z"></path></svg>') no-repeat;
            background-size: cover;
            pointer-events: none;
            z-index: 0;
        }

        .container {
            position: relative;
            z-index: 1;
            max-width: 1400px;
            margin: 0 auto;
            padding: 2rem;
        }

        /* Animated Background Particles */
        @keyframes float {
            0%, 100% { transform: translateY(0) rotate(0deg); opacity: 0.1; }
            33% { transform: translateY(-100px) rotate(120deg); opacity: 0.3; }
            66% { transform: translateY(-50px) rotate(240deg); opacity: 0.1; }
        }

        .particle {
            position: fixed;
            width: 40px;
            height: 40px;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            border-radius: 50%;
            animation: float 20s infinite;
            z-index: 0;
        }

        /* Header */
        .header {
            background: rgba(255, 255, 255, 0.98);
            backdrop-filter: blur(20px);
            border-radius: 24px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: var(--shadow);
            border: 1px solid rgba(255, 255, 255, 0.2);
            animation: slideDown 0.6s ease-out;
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1.5rem;
        }

        .header h1 {
            font-size: 2rem;
            font-weight: 800;
            background: linear-gradient(135deg, var(--primary) 0%, var(--danger) 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .header p {
            color: var(--text);
            margin-top: 0.5rem;
        }

        /* Action Buttons */
        .action-buttons {
            display: flex;
            gap: 1rem;
            flex-wrap: wrap;
        }

        .btn {
            padding: 0.75rem 1.5rem;
            border-radius: 12px;
            border: none;
            font-weight: 600;
            font-size: 0.9rem;
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            position: relative;
            overflow: hidden;
        }

        .btn::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0;
            height: 0;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.3);
            transform: translate(-50%, -50%);
            transition: width 0.5s, height 0.5s;
        }

        .btn:hover::before {
            width: 300px;
            height: 300px;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
        }

        .btn-success {
            background: linear-gradient(135deg, var(--success) 0%, #059669 100%);
            color: white;
        }

        .btn-danger {
            background: linear-gradient(135deg, var(--danger) 0%, #dc2626 100%);
            color: white;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
        }

        /* Filter Section */
        .filter-card {
            background: rgba(255, 255, 255, 0.98);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: var(--shadow-sm);
            animation: fadeInUp 0.8s ease-out 0.2s both;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .filter-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1.5rem;
            margin-top: 1.5rem;
        }

        .form-group label {
            display: block;
            font-size: 0.875rem;
            font-weight: 600;
            color: var(--dark);
            margin-bottom: 0.5rem;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 2px solid var(--border);
            border-radius: 12px;
            font-size: 0.9rem;
            transition: all 0.3s;
            background: white;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
        }

        /* Metric Cards */
        .metrics-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }

        .metric-card {
            background: rgba(255, 255, 255, 0.98);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 1.5rem;
            position: relative;
            overflow: hidden;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            cursor: pointer;
            border: 1px solid rgba(255, 255, 255, 0.2);
            animation: fadeInUp 0.8s ease-out both;
        }

        .metric-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary) 0%, var(--info) 100%);
        }

        .metric-card.danger::before {
            background: linear-gradient(90deg, var(--danger) 0%, var(--warning) 100%);
        }

        .metric-card.success::before {
            background: linear-gradient(90deg, var(--success) 0%, var(--info) 100%);
        }

        .metric-card:hover {
            transform: translateY(-8px) scale(1.02);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
        }

        .metric-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, var(--primary) 0%, var(--info) 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            margin-bottom: 1rem;
        }

        .metric-card.danger .metric-icon {
            background: linear-gradient(135deg, var(--danger) 0%, var(--warning) 100%);
        }

        .metric-card.success .metric-icon {
            background: linear-gradient(135deg, var(--success) 0%, var(--info) 100%);
        }

        .metric-value {
            font-size: 2rem;
            font-weight: 800;
            color: var(--dark);
            line-height: 1;
            margin-bottom: 0.5rem;
        }

        .metric-label {
            font-size: 0.875rem;
            color: var(--text);
            font-weight: 500;
        }

        .metric-change {
            position: absolute;
            top: 1.5rem;
            right: 1.5rem;
            padding: 0.25rem 0.75rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
        }

        .metric-change.positive {
            background: rgba(16, 185, 129, 0.1);
            color: var(--success);
        }

        .metric-change.negative {
            background: rgba(239, 68, 68, 0.1);
            color: var(--danger);
        }

        /* Charts Section */
        .charts-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }

        .chart-card {
            background: rgba(255, 255, 255, 0.98);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 1.5rem;
            box-shadow: var(--shadow-sm);
            animation: fadeInUp 0.8s ease-out 0.4s both;
        }

        .chart-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1.5rem;
        }

        .chart-title {
            font-size: 1.125rem;
            font-weight: 700;
            color: var(--dark);
        }

        /* Data Table */
        .table-card {
            background: rgba(255, 255, 255, 0.98);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            overflow: hidden;
            box-shadow: var(--shadow);
            animation: fadeInUp 0.8s ease-out 0.6s both;
        }

        .table-header {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            padding: 1.5rem;
            color: white;
        }

        .table-header h3 {
            font-size: 1.25rem;
            font-weight: 700;
        }

        .table-wrapper {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        thead th {
            background: var(--light);
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            color: var(--dark);
            font-size: 0.875rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            border-bottom: 2px solid var(--border);
        }

        tbody tr {
            transition: all 0.3s;
            border-bottom: 1px solid var(--border);
        }

        tbody tr:hover {
            background: rgba(79, 70, 229, 0.05);
            transform: scale(1.01);
        }

        tbody td {
            padding: 1rem;
            color: var(--text);
        }

        .borrower-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .borrower-avatar {
            width: 40px;
            height: 40px;
            border-radius: 12px;
            background: linear-gradient(135deg, var(--primary) 0%, var(--info) 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 700;
        }

        .severity-badge {
            padding: 0.375rem 0.875rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            display: inline-block;
        }

        .severity-low {
            background: rgba(16, 185, 129, 0.1);
            color: var(--success);
        }

        .severity-medium {
            background: rgba(245, 158, 11, 0.1);
            color: var(--warning);
        }

        .severity-high {
            background: rgba(239, 68, 68, 0.1);
            color: var(--danger);
        }

        .severity-critical {
            background: var(--danger);
            color: white;
        }

        .action-icons {
            display: flex;
            gap: 0.5rem;
        }

        .action-icon {
            width: 36px;
            height: 36px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s;
            border: none;
            background: var(--light);
            color: var(--text);
        }

        .action-icon:hover {
            background: var(--primary);
            color: white;
            transform: scale(1.1);
        }

        /* Loading Animation */
        .loading {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 9999;
        }

        .loading-spinner {
            width: 50px;
            height: 50px;
            border: 4px solid rgba(255, 255, 255, 0.3);
            border-top: 4px solid white;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            100% { transform: rotate(360deg); }
        }

        /* Responsive */
        @media (max-width: 768px) {
            .container {
                padding: 1rem;
            }

            .header h1 {
                font-size: 1.5rem;
            }

            .metrics-grid {
                grid-template-columns: 1fr;
            }

            .charts-grid {
                grid-template-columns: 1fr;
            }

            .filter-grid {
                grid-template-columns: 1fr;
            }

            .action-buttons {
                flex-direction: column;
                width: 100%;
            }

            .btn {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <!-- Animated Background Particles -->
    <div class="particle" style="left: 10%; top: 20%; animation-delay: 0s;"></div>
    <div class="particle" style="left: 70%; top: 60%; animation-delay: 5s;"></div>
    <div class="particle" style="left: 30%; top: 80%; animation-delay: 10s;"></div>
    <div class="particle" style="left: 90%; top: 10%; animation-delay: 15s;"></div>

    <!-- Loading Spinner -->
    <div class="loading" id="loadingSpinner">
        <div class="loading-spinner"></div>
    </div>

    <div class="container">
        <!-- Header -->
        <div class="header">
            <div class="header-content">
                <div>
                    <h1>
                        <span>📈</span>
                        Loans in Arrears Dashboard
                    </h1>
                    <p>Real-time monitoring and advanced analytics for overdue loans</p>
                </div>
                <div class="action-buttons">
                    <button class="btn btn-success" onclick="exportData('excel')">
                        <span>📊</span> Export Excel
                    </button>
                    <button class="btn btn-danger" onclick="exportData('pdf')">
                        <span>📄</span> Export PDF
                    </button>
                    <button class="btn btn-primary" onclick="refreshDashboard()">
                        <span>🔄</span> Refresh
                    </button>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="filter-card">
            <h3 style="color: var(--dark); margin-bottom: 1rem;">
                <span>🔍</span>
                Advanced Filters
            </h3>
            <div class="filter-grid">
                <div class="form-group">
                    <label>Risk Level</label>
                    <select class="form-control" id="riskFilter" onchange="applyFilters()">
                        <option value="">All Risk Levels</option>
                        <option value="low">Low Risk (< KSh 10k)</option>
                        <option value="medium">Medium Risk (10k-50k)</option>
                        <option value="high">High Risk (> 50k)</option>
                        <option value="critical">Critical (> 90 days)</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Sort By</label>
                    <select class="form-control" id="sortBy" onchange="applyFilters()">
                        <option value="amount_desc">Amount (High to Low)</option>
                        <option value="amount_asc">Amount (Low to High)</option>
                        <option value="days_desc">Days Overdue (Most)</option>
                        <option value="borrower">Borrower Name</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Start Date</label>
                    <input type="date" class="form-control" id="startDate" onchange="applyFilters()">
                </div>
                <div class="form-group">
                    <label>End Date</label>
                    <input type="date" class="form-control" id="endDate" onchange="applyFilters()">
                </div>
            </div>
        </div>

        <!-- Key Metrics -->
        <div class="metrics-grid">
            <div class="metric-card" style="animation-delay: 0.1s;">
                <div class="metric-change negative">+12%</div>
                <div class="metric-icon">
                    <span>⚠️</span>
                </div>
                <div class="metric-value">247</div>
                <div class="metric-label">Total Loans in Arrears</div>
            </div>

            <div class="metric-card danger" style="animation-delay: 0.2s;">
                <div class="metric-change negative">+8.5%</div>
                <div class="metric-icon">
                    <span>💵</span>
                </div>
                <div class="metric-value">KSh 3.2M</div>
                <div class="metric-label">Total Arrears Amount</div>
            </div>

            <div class="metric-card" style="animation-delay: 0.3s;">
                <div class="metric-icon" style="background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);">
                    <span>📈</span>
                </div>
                <div class="metric-value">KSh 12,955</div>
                <div class="metric-label">Average Arrears</div>
            </div>

            <div class="metric-card success" style="animation-delay: 0.4s;">
                <div class="metric-change positive">+5.2%</div>
                <div class="metric-icon">
                    <i class="fas fa-percentage"></i>
                </div>
                <div class="metric-value">68.4%</div>
                <div class="metric-label">Recovery Rate</div>
            </div>
        </div>

        <!-- Risk Distribution -->
        <div class="metrics-grid">
            <div class="metric-card success" style="animation-delay: 0.5s;">
                <div class="metric-icon">
                    <i class="fas fa-shield-alt"></i>
                </div>
                <div class="metric-value">89</div>
                <div class="metric-label">Low Risk (< KSh 10k)</div>
            </div>

            <div class="metric-card" style="animation-delay: 0.6s; border-top: 4px solid var(--warning);">
                <div class="metric-icon" style="background: linear-gradient(135deg, var(--warning) 0%, #d97706 100%);">
                    <span>⚠️</span>
                </div>
                <div class="metric-value">72</div>
                <div class="metric-label">Medium Risk (10k-50k)</div>
            </div>

            <div class="metric-card danger" style="animation-delay: 0.7s;">
                <div class="metric-icon">
                    <span>🔥</span>
                </div>
                <div class="metric-value">58</div>
                <div class="metric-label">High Risk (> 50k)</div>
            </div>

            <div class="metric-card danger" style="animation-delay: 0.8s;">
                <div class="metric-icon">
                    <i class="fas fa-skull-crossbones"></i>
                </div>
                <div class="metric-value">28</div>
                <div class="metric-label">Critical (> 90 days)</div>
            </div>
        </div>

        <!-- Charts -->
        <div class="charts-grid">
            <div class="chart-card">
                <div class="chart-header">
                    <h3 class="chart-title">Arrears Trend Analysis</h3>
                    <select class="form-control" style="width: 120px;">
                        <option>Last 7 days</option>
                        <option>Last 30 days</option>
                        <option>Last 90 days</option>
                    </select>
                </div>
                <canvas id="trendChart" height="200"></canvas>
            </div>

            <div class="chart-card">
                <div class="chart-header">
                    <h3 class="chart-title">Risk Distribution</h3>
                    <button class="btn btn-primary" style="padding: 0.5rem 1rem; font-size: 0.875rem;">
                        <span>⬇️</span>
                    </button>
                </div>
                <canvas id="riskChart" height="200"></canvas>
            </div>
        </div>

        <!-- Data Table -->
        <div class="table-card">
            <div class="table-header">
                <h3><i class="fas fa-table" style="margin-right: 0.5rem;"></i>Detailed Arrears List</h3>
            </div>
            <div class="table-wrapper">
                <table>
                    <thead>
                        <tr>
                            <th>Borrower</th>
                            <th>Loan Details</th>
                            <th style="text-align: right;">Arrears Amount</th>
                            <th style="text-align: center;">Days Overdue</th>
                            <th style="text-align: center;">Risk Level</th>
                            <th>Last Payment</th>
                            <th style="text-align: center;">Actions</th>
                        </tr>
                    </thead>
                    <tbody id="loansTableBody">
                        <!-- Dynamic content will be inserted here -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
    <script>
        // Sample data for demonstration
        const sampleLoans = [
            {
                id: 1,
                borrower_name: 'John Mwangi',
                borrower_phone: '+254 712 345678',
                loan_number: 'LN-2024-0892',
                due_date: '2024-10-15',
                arrears_amount: 45000,
                days_overdue: 21,
                severity: 'medium',
                last_payment_date: '2024-09-20',
                last_payment_amount: 5000
            },
            {
                id: 2,
                borrower_name: 'Mary Wanjiru',
                borrower_phone: '+254 722 987654',
                loan_number: 'LN-2024-0756',
                due_date: '2024-09-30',
                arrears_amount: 125000,
                days_overdue: 36,
                severity: 'high',
                last_payment_date: '2024-08-15',
                last_payment_amount: 10000
            },
            {
                id: 3,
                borrower_name: 'Peter Ochieng',
                borrower_phone: '+254 733 456789',
                loan_number: 'LN-2024-0423',
                due_date: '2024-07-20',
                arrears_amount: 250000,
                days_overdue: 108,
                severity: 'critical',
                last_payment_date: null,
                last_payment_amount: null
            },
            {
                id: 4,
                borrower_name: 'Sarah Kamau',
                borrower_phone: '+254 745 234567',
                loan_number: 'LN-2024-0981',
                due_date: '2024-11-01',
                arrears_amount: 8500,
                days_overdue: 5,
                severity: 'low',
                last_payment_date: '2024-10-25',
                last_payment_amount: 2000
            },
            {
                id: 5,
                borrower_name: 'James Kiprop',
                borrower_phone: '+254 756 890123',
                loan_number: 'LN-2024-0567',
                due_date: '2024-08-10',
                arrears_amount: 180000,
                days_overdue: 87,
                severity: 'high',
                last_payment_date: '2024-07-01',
                last_payment_amount: 15000
            }
        ];

        // Initialize Charts
        let trendChart, riskChart;

        function initCharts() {
            // Trend Chart
            const trendCtx = document.getElementById('trendChart').getContext('2d');
            trendChart = new Chart(trendCtx, {
                type: 'line',
                data: {
                    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                    datasets: [{
                        label: 'Total Arrears (KSh)',
                        data: [2800000, 2950000, 3100000, 3050000, 3200000, 3150000, 3200000],
                        borderColor: '#4f46e5',
                        backgroundColor: 'rgba