<?php
/**
 * Dashboard
 * Main user dashboard with statistics and recent activity
 */

define('APP_RUNNING', true);
require_once 'db_connect.php';
require_once 'includes/auth.php';

initSecureSession();

// Require authentication
if(!isAuthenticated() || !validateSession()) {
    header('Location: login');
    exit;
}

$user_id = $_SESSION['user_id'];
$user_role = $_SESSION['role'] ?? 'user';
$current_year = date('Y');

// Get user's lands count (including client lands for brokers)
if(in_array($user_role, ['broker', 'admin'])) {
    $lands_sql = $user_role == 'admin'
        ? "SELECT COUNT(*) as total FROM lands"
        : "SELECT COUNT(*) as total FROM lands WHERE user_id = $user_id OR client_id IN (SELECT id FROM clients WHERE broker_id = $user_id)";
} else {
    $lands_sql = "SELECT COUNT(*) as total FROM lands WHERE user_id = $user_id";
}
$lands_result = mysqli_query($conn, $lands_sql);
$lands_count = mysqli_fetch_assoc($lands_result)['total'];

// Get total beacon points
$points_result = mysqli_query($conn, "SELECT COUNT(*) as total FROM beacon_points bp
    JOIN lands l ON bp.land_id = l.id WHERE l.user_id = $user_id");
$points_count = mysqli_fetch_assoc($points_result)['total'];

// Get recent lands (including client lands for brokers)
if(in_array($user_role, ['broker', 'admin'])) {
    $recent_sql = $user_role == 'admin'
        ? "SELECT l.*, c.full_name as client_name FROM lands l LEFT JOIN clients c ON l.client_id = c.id ORDER BY l.created_at DESC LIMIT 5"
        : "SELECT l.*, c.full_name as client_name FROM lands l LEFT JOIN clients c ON l.client_id = c.id WHERE l.user_id = $user_id OR l.client_id IN (SELECT id FROM clients WHERE broker_id = $user_id) ORDER BY l.created_at DESC LIMIT 5";
} else {
    $recent_sql = "SELECT * FROM lands WHERE user_id = $user_id ORDER BY created_at DESC LIMIT 5";
}
$recent_lands = mysqli_query($conn, $recent_sql);

// Get client stats for brokers/admins
// Now clients are users who registered with a broker's code (referred_by_broker)
$clients_count = 0;
$clients_paid = 0;
$clients_unpaid = 0;
$broker_code = null;

if($user_role == 'broker') {
    // Get broker's code
    $broker_info = mysqli_fetch_assoc(mysqli_query($conn, "SELECT broker_code FROM users WHERE id = $user_id"));
    $broker_code = $broker_info['broker_code'];

    // Count clients referred by this broker
    $clients_count = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM users WHERE referred_by_broker = $user_id AND role = 'user'"))['total'];

    // Paid this year (from user_payments table)
    $clients_paid = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(DISTINCT up.user_id) as total FROM user_payments up JOIN users u ON up.user_id = u.id WHERE u.referred_by_broker = $user_id AND up.payment_year = $current_year AND up.status = 'confirmed'"))['total'] ?? 0;
    $clients_unpaid = $clients_count - $clients_paid;
} elseif($user_role == 'admin') {
    // Admin sees all users (excluding admins)
    $clients_count = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM users WHERE role IN ('user', 'broker')"))['total'];

    // Paid this year
    $clients_paid = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(DISTINCT user_id) as total FROM user_payments WHERE payment_year = $current_year AND status = 'confirmed'"))['total'] ?? 0;
    $clients_unpaid = $clients_count - $clients_paid;
}

// Get shared lands count
$shared = mysqli_query($conn, "SELECT COUNT(*) as total FROM lands WHERE user_id = $user_id AND share_token IS NOT NULL");
$shared_count = mysqli_fetch_assoc($shared)['total'];

// Get pending alerts count
$alerts_sql = in_array($user_role, ['admin'])
    ? "SELECT COUNT(*) as total FROM overlap_alerts WHERE is_resolved = 0"
    : "SELECT COUNT(*) as total FROM overlap_alerts oa JOIN lands l ON (oa.land_id_1 = l.id OR oa.land_id_2 = l.id) WHERE l.user_id = $user_id AND oa.is_resolved = 0";
$alerts_count = mysqli_fetch_assoc(mysqli_query($conn, $alerts_sql))['total'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="theme-color" content="#1a1f2e">
    <title>Dashboard - Land Secure</title>
    <link rel="manifest" href="manifest.json">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/font-awesome/css/all.min.css">
    <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="app-main">
        <!-- Page Header -->
        <div class="page-header">
            <div class="page-header-content">
                <div class="page-title">
                    <h1><i class="fas fa-home"></i> Dashboard</h1>
                    <p>Welcome back, <?php echo htmlspecialchars($_SESSION['full_name'] ?: $_SESSION['username']); ?>!</p>
                </div>
                <?php if($user_role != 'broker'): ?>
                <div class="header-actions">
                    <a href="<?php echo url('add-land'); ?>" class="btn-action primary">
                        <i class="fas fa-plus"></i>
                        <span>Add Land</span>
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Stats Grid -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon lands">
                    <i class="fas fa-map"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo number_format($lands_count); ?></h3>
                    <p>Total Lands</p>
                </div>
                <div class="stat-trend up">
                    <i class="fas fa-arrow-up"></i>
                </div>
            </div>

            <div class="stat-card">
                <div class="stat-icon points">
                    <i class="fas fa-map-marker-alt"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo number_format($points_count); ?></h3>
                    <p>Beacon Points</p>
                </div>
            </div>

            <div class="stat-card">
                <div class="stat-icon shared">
                    <i class="fas fa-share-alt"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $shared_count; ?></h3>
                    <p>Shared Lands</p>
                </div>
            </div>

            <?php if($alerts_count > 0): ?>
            <div class="stat-card warning">
                <div class="stat-icon alerts">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $alerts_count; ?></h3>
                    <p>Active Alerts</p>
                </div>
                <a href="<?php echo url('alerts'); ?>" class="stat-link">View</a>
            </div>
            <?php endif; ?>
        </div>

        <?php if($user_role == 'broker' && $broker_code): ?>
        <!-- Broker Code Card -->
        <div class="broker-code-card">
            <div class="broker-code-content">
                <div class="broker-code-icon"><i class="fas fa-qrcode"></i></div>
                <div class="broker-code-info">
                    <span class="label">Your Broker Code</span>
                    <h2 class="code"><?php echo htmlspecialchars($broker_code); ?></h2>
                    <p class="hint">Share this code with clients so they can register under you</p>
                </div>
                <button class="copy-btn" onclick="copyBrokerCode('<?php echo htmlspecialchars($broker_code); ?>')">
                    <i class="fas fa-copy"></i> Copy
                </button>
            </div>
        </div>
        <?php endif; ?>

        <?php if(in_array($user_role, ['broker', 'admin'])): ?>
        <!-- Client Stats (Brokers/Admins only) -->
        <div class="section-title">
            <h2><i class="fas fa-users"></i> <?php echo $user_role == 'broker' ? 'My Referred Clients' : 'Users Overview'; ?></h2>
        </div>
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon clients">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo number_format($clients_count); ?></h3>
                    <p><?php echo $user_role == 'broker' ? 'Referred Clients' : 'Total Users'; ?></p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon paid">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $clients_paid; ?></h3>
                    <p>Paid (<?php echo $current_year; ?>)</p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon unpaid">
                    <i class="fas fa-clock"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $clients_unpaid; ?></h3>
                    <p>Pending Payment</p>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Quick Actions -->
        <div class="content-card">
            <div class="card-header-custom">
                <h3><i class="fas fa-bolt"></i> Quick Actions</h3>
            </div>
            <div class="card-body">
                <div class="quick-actions">
                    <?php if($user_role != 'broker'): ?>
                    <a href="<?php echo url('add-land'); ?>" class="quick-action-btn primary">
                        <div class="action-icon"><i class="fas fa-plus-circle"></i></div>
                        <span>Add New Land</span>
                    </a>
                    <a href="<?php echo url('lands'); ?>" class="quick-action-btn secondary">
                        <div class="action-icon"><i class="fas fa-map"></i></div>
                        <span>View All Lands</span>
                    </a>
                    <?php endif; ?>
                    <?php if($user_role == 'broker'): ?>
                    <a href="<?php echo url('my-clients'); ?>" class="quick-action-btn success">
                        <div class="action-icon"><i class="fas fa-users"></i></div>
                        <span>View My Clients</span>
                    </a>
                    <?php elseif($user_role == 'admin'): ?>
                    <a href="<?php echo url('admin/users'); ?>" class="quick-action-btn success">
                        <div class="action-icon"><i class="fas fa-users"></i></div>
                        <span>Manage Users</span>
                    </a>
                    <?php endif; ?>
                    <?php if($user_role != 'broker'): ?>
                    <a href="<?php echo url('alerts'); ?>" class="quick-action-btn warning">
                        <div class="action-icon"><i class="fas fa-bell"></i></div>
                        <span>Check Alerts</span>
                    </a>
                    <?php endif; ?>
                    <?php if($user_role == 'admin'): ?>
                    <a href="<?php echo url('admin'); ?>" class="quick-action-btn dark">
                        <div class="action-icon"><i class="fas fa-shield-alt"></i></div>
                        <span>Admin Panel</span>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Recent Lands -->
        <div class="content-card">
            <div class="card-header-custom">
                <h3><i class="fas fa-clock"></i> Recent Lands</h3>
                <a href="<?php echo url('lands'); ?>" class="btn-view-all">View All <i class="fas fa-arrow-right"></i></a>
            </div>
            <div class="card-body">
                <?php if(mysqli_num_rows($recent_lands) > 0): ?>
                <div class="lands-list">
                    <?php while($land = mysqli_fetch_assoc($recent_lands)): ?>
                    <div class="land-item">
                        <div class="land-icon">
                            <i class="fas fa-map-marked-alt"></i>
                        </div>
                        <div class="land-details">
                            <h4><?php echo htmlspecialchars($land['land_name']); ?></h4>
                            <p>
                                <i class="fas fa-map-marker-alt"></i>
                                <?php echo htmlspecialchars($land['location_address'] ?: 'Location not specified'); ?>
                            </p>
                            <div class="land-meta">
                                <span class="area-badge">
                                    <i class="fas fa-ruler-combined"></i>
                                    <?php echo number_format($land['total_area'], 2) . ' ' . $land['area_unit']; ?>
                                </span>
                                <span class="date-badge">
                                    <i class="fas fa-calendar"></i>
                                    <?php echo date('M d, Y', strtotime($land['created_at'])); ?>
                                </span>
                                <?php if($land['share_token']): ?>
                                <span class="shared-badge">
                                    <i class="fas fa-share-alt"></i> Shared
                                </span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="land-actions">
                            <a href="<?php echo url('land/' . $land['id']); ?>" class="btn-icon view" title="View">
                                <i class="fas fa-eye"></i>
                            </a>
                            <a href="<?php echo url('edit-land/' . $land['id']); ?>" class="btn-icon edit" title="Edit">
                                <i class="fas fa-edit"></i>
                            </a>
                        </div>
                    </div>
                    <?php endwhile; ?>
                </div>
                <?php else: ?>
                <div class="empty-state">
                    <div class="empty-icon">
                        <i class="fas fa-map"></i>
                    </div>
                    <h4>No lands yet</h4>
                    <p>Start by adding your first land parcel</p>
                    <a href="<?php echo url('add-land'); ?>" class="btn-action primary">
                        <i class="fas fa-plus"></i> Add New Land
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <style>
    /* Page Header */
    .page-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 20px;
        padding: 30px;
        margin-bottom: 30px;
        color: white;
    }

    .page-header-content {
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 20px;
    }

    .page-title h1 {
        font-size: 28px;
        font-weight: 700;
        margin: 0 0 8px 0;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .page-title p {
        margin: 0;
        opacity: 0.9;
        font-size: 15px;
    }

    .header-actions {
        display: flex;
        gap: 12px;
    }

    .btn-action {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 12px 24px;
        border-radius: 12px;
        font-weight: 600;
        text-decoration: none;
        transition: all 0.3s ease;
    }

    .btn-action.primary {
        background: white;
        color: #667eea;
    }

    .btn-action.primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        text-decoration: none;
        color: #764ba2;
    }

    /* Section Title */
    .section-title {
        margin: 30px 0 20px;
    }

    .section-title h2 {
        font-size: 18px;
        font-weight: 600;
        color: #1e293b;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    /* Stats Grid */
    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 20px;
        margin-bottom: 30px;
    }

    .stat-card {
        background: white;
        border-radius: 16px;
        padding: 24px;
        display: flex;
        align-items: center;
        gap: 16px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        position: relative;
        transition: all 0.3s ease;
    }

    .stat-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
    }

    .stat-card.warning {
        border-left: 4px solid #f59e0b;
    }

    .stat-icon {
        width: 56px;
        height: 56px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        color: white;
    }

    .stat-icon.lands { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
    .stat-icon.points { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }
    .stat-icon.shared { background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); }
    .stat-icon.clients { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); }
    .stat-icon.paid { background: linear-gradient(135deg, #56ab2f 0%, #a8e063 100%); }
    .stat-icon.unpaid { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); }
    .stat-icon.alerts { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); }

    .stat-info h3 {
        font-size: 28px;
        font-weight: 700;
        margin: 0;
        color: #1e293b;
    }

    .stat-info p {
        margin: 4px 0 0 0;
        color: #64748b;
        font-size: 14px;
    }

    .stat-trend {
        position: absolute;
        top: 16px;
        right: 16px;
        width: 28px;
        height: 28px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
    }

    .stat-trend.up {
        background: rgba(34, 197, 94, 0.1);
        color: #22c55e;
    }

    .stat-link {
        position: absolute;
        bottom: 16px;
        right: 16px;
        font-size: 13px;
        color: #667eea;
        text-decoration: none;
        font-weight: 600;
    }

    /* Content Card */
    .content-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        margin-bottom: 30px;
        overflow: hidden;
    }

    .card-header-custom {
        padding: 24px 30px;
        border-bottom: 1px solid #f1f5f9;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .card-header-custom h3 {
        font-size: 18px;
        font-weight: 600;
        margin: 0;
        color: #1e293b;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .btn-view-all {
        font-size: 14px;
        color: #667eea;
        text-decoration: none;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .btn-view-all:hover {
        color: #764ba2;
    }

    .card-body {
        padding: 24px 30px;
    }

    /* Quick Actions */
    .quick-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
    }

    .quick-action-btn {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 14px 20px;
        border-radius: 12px;
        font-weight: 500;
        text-decoration: none;
        transition: all 0.3s ease;
        color: white;
    }

    .quick-action-btn .action-icon {
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: rgba(255, 255, 255, 0.2);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .quick-action-btn.primary { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
    .quick-action-btn.secondary { background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%); }
    .quick-action-btn.success { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); }
    .quick-action-btn.warning { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); }
    .quick-action-btn.dark { background: linear-gradient(135deg, #1e293b 0%, #334155 100%); }

    .quick-action-btn:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        color: white;
        text-decoration: none;
    }

    /* Lands List */
    .lands-list {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .land-item {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 20px;
        background: #f8fafc;
        border-radius: 14px;
        transition: all 0.3s ease;
    }

    .land-item:hover {
        background: #f1f5f9;
        transform: translateX(4px);
    }

    .land-icon {
        width: 50px;
        height: 50px;
        border-radius: 12px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 20px;
    }

    .land-details {
        flex: 1;
    }

    .land-details h4 {
        font-size: 16px;
        font-weight: 600;
        color: #1e293b;
        margin: 0 0 6px 0;
    }

    .land-details p {
        font-size: 13px;
        color: #64748b;
        margin: 0 0 10px 0;
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .land-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
    }

    .area-badge, .date-badge, .shared-badge {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 4px 10px;
        border-radius: 6px;
        font-size: 12px;
        font-weight: 500;
    }

    .area-badge {
        background: rgba(102, 126, 234, 0.1);
        color: #667eea;
    }

    .date-badge {
        background: rgba(100, 116, 139, 0.1);
        color: #64748b;
    }

    .shared-badge {
        background: rgba(34, 197, 94, 0.1);
        color: #22c55e;
    }

    .land-actions {
        display: flex;
        gap: 8px;
    }

    .btn-icon {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        text-decoration: none;
        transition: all 0.3s ease;
    }

    .btn-icon.view {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
        color: white;
    }

    .btn-icon.edit {
        background: linear-gradient(135deg, #f5af19 0%, #f59e0b 100%);
        color: white;
    }

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

    /* Empty State */
    .empty-state {
        text-align: center;
        padding: 60px 20px;
    }

    .empty-icon {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        background: #f1f5f9;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 20px;
        font-size: 32px;
        color: #94a3b8;
    }

    .empty-state h4 {
        font-size: 18px;
        font-weight: 600;
        color: #1e293b;
        margin: 0 0 8px 0;
    }

    .empty-state p {
        color: #64748b;
        margin: 0 0 24px 0;
    }

    /* Broker Code Card */
    .broker-code-card {
        background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
        border-radius: 20px;
        padding: 24px 30px;
        margin-bottom: 30px;
        color: white;
    }

    .broker-code-content {
        display: flex;
        align-items: center;
        gap: 20px;
        flex-wrap: wrap;
    }

    .broker-code-icon {
        width: 60px;
        height: 60px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28px;
    }

    .broker-code-info {
        flex: 1;
    }

    .broker-code-info .label {
        font-size: 13px;
        opacity: 0.9;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    .broker-code-info .code {
        font-size: 32px;
        font-weight: 700;
        margin: 4px 0;
        letter-spacing: 3px;
    }

    .broker-code-info .hint {
        font-size: 13px;
        margin: 0;
        opacity: 0.85;
    }

    .copy-btn {
        background: white;
        color: #11998e;
        border: none;
        padding: 12px 24px;
        border-radius: 12px;
        font-weight: 600;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 8px;
        transition: all 0.3s ease;
    }

    .copy-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
    }

    .copy-btn.copied {
        background: #22c55e;
        color: white;
    }

    /* Responsive */
    @media (max-width: 768px) {
        .page-header {
            padding: 20px;
        }

        .page-header-content {
            flex-direction: column;
            align-items: flex-start;
        }

        .page-title h1 {
            font-size: 22px;
        }

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

        .stat-card {
            padding: 16px;
        }

        .stat-icon {
            width: 44px;
            height: 44px;
            font-size: 18px;
        }

        .stat-info h3 {
            font-size: 20px;
        }

        .card-header-custom,
        .card-body {
            padding: 16px 20px;
        }

        .quick-actions {
            flex-direction: column;
        }

        .land-item {
            flex-direction: column;
            align-items: flex-start;
            text-align: left;
        }

        .land-actions {
            width: 100%;
            justify-content: flex-end;
            margin-top: 12px;
        }
    }

    @media (max-width: 480px) {
        .stats-grid {
            grid-template-columns: 1fr;
        }
    }
    </style>

    <script src="assets/vendor/jquery/jquery.min.js"></script>
    <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/offline-storage.js"></script>

    <!-- Register Service Worker -->
    <script>
    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
            navigator.serviceWorker.register('/sw.js')
                .then(function(registration) {
                    console.log('ServiceWorker registered:', registration.scope);
                })
                .catch(function(error) {
                    console.log('ServiceWorker registration failed:', error);
                });
        });
    }

    function copyBrokerCode(code) {
        navigator.clipboard.writeText(code).then(function() {
            const btn = document.querySelector('.copy-btn');
            btn.innerHTML = '<i class="fas fa-check"></i> Copied!';
            btn.classList.add('copied');
            setTimeout(() => {
                btn.innerHTML = '<i class="fas fa-copy"></i> Copy';
                btn.classList.remove('copied');
            }, 2000);
        });
    }
    </script>
</body>
</html>
