<!DOCTYPE html>
<html lang="<%= lang %>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title><%= t('leaderboard.squadronsTitle') %> | <%= botName %></title>
    <meta name="description" content="<%= t('leaderboard.squadronsSubtitle') %>, wins, and total score.">
    <link rel="icon" type="image/png" href="/images/transparent_toothlessssss.png">
    
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
    <link rel="preload" href="/Fonts/symbols_skyquake.ttf" as="font" type="font/ttf" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    
    <!-- Tailwind CSS -->
    <link rel="stylesheet" href="/css/output.css">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" media="print" onload="this.media='all'">
    
    <style>
        @font-face {
            font-family: 'skyquakesymbols';
            src: url('/Fonts/symbols_skyquake.ttf') format('truetype');
            font-display: swap;
        }
        
        body {
            background: #1b1b1b;
            min-height: 100vh;
        }
        
        .text-accent { color: #F5F5DC; }
        .text-muted { color: #90EE90; }
        
        .btn-primary {
            background: linear-gradient(135deg, #F5F5DC 0%, #E8E8D0 100%);
            box-shadow: 0 4px 20px rgba(245, 245, 220, 0.15), inset 0 1px 0 rgba(255, 255, 255, 0.4);
            color: #1E1E1E;
            font-weight: 700;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        .btn-primary:hover {
            box-shadow: 0 8px 25px rgba(245, 245, 220, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.4);
            transform: translateY(-2px);
        }
        
        .leaderboard-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 6rem 1rem 2rem;
            min-height: calc(100vh - 200px);
        }
        
        .leaderboard-header {
            text-align: center;
            margin-bottom: 2rem;
        }
        
        .leaderboard-title {
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
            background: linear-gradient(135deg, #F5F5DC 0%, #E8E8D0 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .leaderboard-subtitle {
            font-size: 1.1rem;
            color: rgba(255, 255, 255, 0.8);
            margin-bottom: 1rem;
        }
        
        .disclaimer {
            font-size: 0.9rem;
            opacity: 0.8;
            font-style: italic;
            color: rgba(255, 255, 255, 0.7);
        }
        
        .leaderboard-nav {
            background: rgba(30, 30, 30, 0.6);
            border-radius: 1rem;
            padding: 1rem;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(144, 238, 144, 0.1);
            margin-bottom: 2rem;
            display: flex;
            justify-content: center;
            gap: 1rem;
            flex-wrap: wrap;
        }
        
        .leaderboard-tab {
            padding: 0.75rem 1.5rem;
            background: rgba(30, 30, 30, 0.8);
            border: 1px solid rgba(144, 238, 144, 0.2);
            border-radius: 0.5rem;
            color: #ffffff;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .leaderboard-tab:hover {
            background: rgba(144, 238, 144, 0.1);
            color: #90EE90;
            border-color: #90EE90;
            text-shadow: 0 0 10px rgba(144, 238, 144, 0.3);
        }
        
        .leaderboard-tab.active {
            background: linear-gradient(135deg, rgba(144, 238, 144, 0.15), rgba(144, 238, 144, 0.05));
            color: #90EE90;
            border-color: #90EE90;
            text-shadow: 0 0 15px rgba(144, 238, 144, 0.6);
        }
        
        .leaderboard-content {
            background: rgba(30, 30, 30, 0.6);
            border-radius: 1rem;
            padding: 2rem;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(144, 238, 144, 0.1);
        }
        
        .loading-state {
            text-align: center;
            padding: 4rem;
            color: #90EE90;
        }
        
        .loading-spinner {
            border: 3px solid rgba(144, 238, 144, 0.3);
            border-top: 3px solid #90EE90;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 1rem;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .leaderboard-table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(30, 30, 30, 0.8);
            border-radius: 0.75rem;
            overflow: hidden;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(144, 238, 144, 0.1);
            table-layout: auto;
            min-width: 900px;
        }
        
        .leaderboard-table th {
            background: linear-gradient(135deg, rgba(144, 238, 144, 0.15), rgba(144, 238, 144, 0.05));
            color: #F5F5DC;
            padding: 1.25rem;
            text-align: center;
            font-weight: 600;
            border-bottom: 1px solid rgba(144, 238, 144, 0.2);
            font-size: 0.95rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            text-shadow: 0 0 10px rgba(144, 238, 144, 0.3);
        }
        
        .leaderboard-table th:first-child {
            text-align: left;
        }
        
        .leaderboard-table td {
            padding: 1rem;
            border-bottom: 1px solid rgba(144, 238, 144, 0.1);
            background: rgba(30, 30, 30, 0.4);
            color: #ffffff;
            transition: all 0.3s ease;
            text-align: center;
        }
        
        .leaderboard-table td:first-child {
            text-align: left;
        }
        
        .leaderboard-table tr:hover td {
            background: rgba(144, 238, 144, 0.05);
            cursor: pointer;
        }
        
        .leaderboard-table tr:last-child td {
            border-bottom: none;
        }
        
        .rank-cell {
            font-weight: 700;
            font-size: 1.1rem;
            width: 80px;
        }
        
        .rank-1 { color: #ffd700; text-shadow: 0 0 10px rgba(255, 215, 0, 0.5); }
        .rank-2 { color: #c0c0c0; text-shadow: 0 0 10px rgba(192, 192, 192, 0.5); }
        .rank-3 { color: #cd7f32; text-shadow: 0 0 10px rgba(205, 127, 50, 0.5); }
        
        .squadron-name {
            font-family: 'skyquakesymbols', 'Inter', sans-serif !important;
            font-weight: 600;
            color: #90EE90;
            font-size: 1.1rem;
            text-shadow: 0 0 10px rgba(144, 238, 144, 0.3);
        }
        
        .stat-cell {
            font-weight: 600;
            font-size: 1rem;
        }

        .stat-points { color: #ffd700; text-shadow: 0 0 10px rgba(255, 215, 0, 0.5); }
        .stat-members { color: #a855f7; text-shadow: 0 0 10px rgba(168, 85, 247, 0.5); }
        .stat-kills { color: #ff4757; text-shadow: 0 0 10px rgba(255, 71, 87, 0.5); }
        .stat-battles { color: #a855f7; text-shadow: 0 0 10px rgba(168, 85, 247, 0.5); }
        .stat-wins { color: #10b981; text-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }
        .stat-winrate { color: #f59e0b; text-shadow: 0 0 10px rgba(245, 158, 11, 0.5); }
        .stat-kdr { color: #90EE90; text-shadow: 0 0 10px rgba(144, 238, 144, 0.5); }
        
        .error-state {
            text-align: center;
            padding: 4rem;
            color: #ff3838;
        }
        
        .error-icon {
            font-size: 3rem;
            margin-bottom: 1rem;
        }
        
        .retry-btn {
            background: linear-gradient(135deg, #F5F5DC 0%, #E8E8D0 100%);
            color: #1E1E1E;
            border: none;
            padding: 0.75rem 1.5rem;
            border-radius: 0.5rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 1rem;
        }
        
        .retry-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(245, 245, 220, 0.4);
        }
        
        .responsive-table {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }
        
        .leaderboard-controls {
            background: rgba(30, 30, 30, 0.6);
            border-radius: 1rem;
            padding: 1.5rem;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(144, 238, 144, 0.1);
            margin-bottom: 2rem;
        }
        
        .controls-row {
            display: flex;
            gap: 1.5rem;
            align-items: end;
            flex-wrap: wrap;
            margin-bottom: 0.75rem;
        }
        
        .filter-group {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }
        
        .filter-group label {
            color: #ffffff;
            font-weight: 600;
            font-size: 0.9rem;
        }
        
        .filter-input {
            background: rgba(30, 30, 30, 0.9);
            border: 2px solid rgba(144, 238, 144, 0.2);
            border-radius: 0.75rem;
            padding: 0.75rem 1rem;
            color: #ffffff;
            font-size: 1rem;
            transition: all 0.3s ease;
            min-width: 200px;
        }
        
        .filter-input:focus {
            outline: none;
            border-color: #90EE90;
            box-shadow: 0 0 20px rgba(144, 238, 144, 0.4);
        }
        
        .clear-filters-btn {
            background: linear-gradient(135deg, rgba(239, 68, 68, 0.2), rgba(220, 38, 38, 0.2));
            border: 2px solid rgba(239, 68, 68, 0.3);
            color: #ef4444;
            padding: 0.75rem 1rem;
            border-radius: 0.75rem;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .clear-filters-btn:hover {
            background: linear-gradient(135deg, rgba(239, 68, 68, 0.3), rgba(220, 38, 38, 0.3));
            border-color: #ef4444;
        }
        
        .results-info {
            color: #90EE90;
            font-weight: 600;
            text-align: center;
        }
        
        .sortable {
            cursor: pointer;
            user-select: none;
        }
        
        .sortable:hover {
            background: linear-gradient(135deg, rgba(144, 238, 144, 0.3), rgba(144, 238, 144, 0.15));
        }
        
        .sort-icon {
            font-size: 0.8rem;
            opacity: 0.6;
            margin-left: 0.5rem;
        }
        
        .sortable.sorted-asc .sort-icon {
            opacity: 1;
        }
        
        .sortable.sorted-asc .sort-icon::before {
            content: "\f0de";
        }
        
        .sortable.sorted-desc .sort-icon {
            opacity: 1;
        }
        
        .sortable.sorted-desc .sort-icon::before {
            content: "\f0dd";
        }
        
        .pagination-controls {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 1rem;
            margin-top: 2rem;
            padding: 1rem;
            background: rgba(30, 30, 30, 0.6);
            border-radius: 1rem;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(144, 238, 144, 0.1);
        }
        
        .pagination-btn {
            background: rgba(30, 30, 30, 0.8);
            border: 1px solid rgba(144, 238, 144, 0.2);
            color: #ffffff;
            padding: 0.5rem 0.75rem;
            border-radius: 0.5rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .pagination-btn:hover:not(:disabled) {
            background: rgba(144, 238, 144, 0.1);
            border-color: #90EE90;
            color: #90EE90;
        }
        
        .pagination-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        .pagination-info {
            color: #90EE90;
            font-weight: 600;
        }
        
        /* Footer Styles */
        footer {
            background: rgba(30, 30, 30, 0.8);
            backdrop-filter: blur(10px);
            border-top: 1px solid rgba(144, 238, 144, 0.1);
            padding: 2rem 0;
            margin-top: auto;
        }
        
        footer a {
            color: #888;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        footer a:hover {
            color: #90EE90;
            text-shadow: 0 0 10px rgba(144, 238, 144, 0.5);
        }
        
    </style>
</head>
<body class="text-white antialiased">
    <%- include('partials/nav', { activePage: 'leaderboards' }) %>

    <div class="leaderboard-container">
        <div class="leaderboard-header">
            <h1 class="leaderboard-title"><%= t('leaderboard.squadronsTitle') %></h1>
            <p class="leaderboard-subtitle"><%= t('leaderboard.squadronsSubtitle') %></p>
            <div class="disclaimer">
                <i class="fas fa-info-circle" style="margin-right: 0.5rem;"></i>
                <%= t('common.recordingSince') %>
            </div>
        </div>
        
        <div class="leaderboard-nav">
            <a href="/leaderboard/players" class="leaderboard-tab">
                <i class="fas fa-user"></i> <%= t('common.players') %>
            </a>
            <a href="/leaderboard/vehicles" class="leaderboard-tab">
                <i class="fas fa-fighter-jet"></i> <%= t('common.vehicles') %>
            </a>
            <a href="/leaderboard/squadrons" class="leaderboard-tab active">
                <i class="fas fa-users"></i> <%= t('common.squadrons') %>
            </a>
            <a href="/leaderboard/stats" class="leaderboard-tab">
                <i class="fas fa-chart-bar"></i> <%= t('common.statistics') %>
            </a>
            <a href="/leaderboard/comparison" class="leaderboard-tab">
                <i class="fas fa-balance-scale"></i> <%= t('common.comparison') %>
            </a>
        </div>
        
        <div class="leaderboard-controls">
            <div class="controls-row">
                <div class="filter-group">
                    <label for="squadronSearch"><%= t('leaderboard.searchSquadron') %></label>
                    <input type="text" id="squadronSearch" class="filter-input" placeholder="<%= t('leaderboard.searchBySquadronName') %>">
                </div>
                <div class="filter-group">
                    <label for="minPlayers"><%= t('leaderboard.minPlayers') %></label>
                    <input type="number" id="minPlayers" class="filter-input" placeholder="<%= t('leaderboard.minPlayersPlaceholder') %>" min="0" value="1" style="min-width: 150px;">
                </div>
                <div class="filter-group">
                    <button class="clear-filters-btn" onclick="clearFilters()">
                        <i class="fas fa-times"></i> <%= t('leaderboard.resetFilters') %>
                    </button>
                </div>
            </div>

            <div class="results-info" id="resultsInfo">
                <span id="filteredCount">0</span> <%= t('leaderboard.squadronsShown') %>
            </div>
        </div>
        
        <div class="leaderboard-content">
            <div class="loading-state" id="loadingState">
                <div class="loading-spinner"></div>
                <p><%= t('leaderboard.loadingSquadronLeaderboard') %></p>
            </div>
            
            <div class="error-state" id="errorState" style="display: none;">
                <i class="fas fa-exclamation-triangle error-icon"></i>
                <h3><%= t('leaderboard.failedToLoadLeaderboard') %></h3>
                <p id="errorMessage"><%= t('leaderboard.unableToFetch') %></p>
                <button class="retry-btn" onclick="loadLeaderboard()">
                    <i class="fas fa-redo"></i> <%= t('common.retry') %>
                </button>
            </div>
            
            <div class="responsive-table" id="leaderboardTable" style="display: none;">
                <table class="leaderboard-table">
                    <thead>
                        <tr>
                            <th><%= t('common.rank') %></th>
                            <th class="sortable" onclick="sortTable('points')" data-sort="points">
                                <%= t('common.points') %> <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('squadron_name')" data-sort="squadron_name">
                                <%= t('common.squadron') %> <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('player_count')" data-sort="player_count">
                                <%= t('common.members') %> <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('total_kills')" data-sort="total_kills">
                                <%= t('common.totalKills') %> <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('total_battles')" data-sort="total_battles">
                                <%= t('common.battles') %> <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('wins')" data-sort="wins">
                                Wins <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('win_rate')" data-sort="win_rate">
                                Win Rate <i class="fas fa-sort sort-icon"></i>
                            </th>
                            <th class="sortable" onclick="sortTable('kdr')" data-sort="kdr">
                                KDR <i class="fas fa-sort sort-icon"></i>
                            </th>
                        </tr>
                    </thead>
                    <tbody id="leaderboardBody">
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <%- include('partials/footer') %>

    <script>
        window.__lang = '<%= lang %>';
        window.__i18n = <%- localeJson %>;
        window.__t = function(key) {
            var parts = key.split('.'), obj = window.__i18n;
            for (var i = 0; i < parts.length; i++) { obj = obj && obj[parts[i]]; }
            return obj !== undefined ? obj : key;
        };
        window.switchLanguage = function(lang) {
            var next = lang || (document.documentElement.lang === 'en' ? 'ru' : 'en');
            if (next === document.documentElement.lang) return;
            document.cookie = 'lang=' + next + ';path=/;max-age=31536000;SameSite=Lax';
            window.location.reload();
        };
    </script>
    <script src="/js/main.js?v=3"></script>
    <script src="/js/api-client.js?v=2"></script>
    <script src="/js/header-search.js?v=2"></script>
    <script src="/js/seasons-filter.js"></script>
    <script>
        let allSquadrons = [];
        let filteredSquadrons = [];
        let currentSort = { field: 'points', direction: 'desc' };

        async function loadLeaderboard() {
            const loadingState = document.getElementById('loadingState');
            const errorState = document.getElementById('errorState');
            const leaderboardTable = document.getElementById('leaderboardTable');

            loadingState.style.display = 'block';
            errorState.style.display = 'none';
            leaderboardTable.style.display = 'none';

            try {
                await window.ensureAPIClient();

                const leaderboardData = await window.apiClient.getSquadronLeaderboard();
                
                if (!leaderboardData || !leaderboardData.squadrons || leaderboardData.squadrons.length === 0) {
                    showError(__t('leaderboard.noSquadronsInLeaderboard'));
                    return;
                }

                allSquadrons = leaderboardData.squadrons;
                applyFiltersAndDisplay();

                loadingState.style.display = 'none';
                leaderboardTable.style.display = 'block';

            } catch (error) {
                console.error('Error loading squadron leaderboard:', error);
                showError(error.message || __t('leaderboard.failedToLoadLeaderboard'));
            }
        }
        
        function applyFiltersAndDisplay() {
            const searchTerm = document.getElementById('squadronSearch').value.toLowerCase().trim();
            const minPlayers = parseInt(document.getElementById('minPlayers').value) || 0;
            
            filteredSquadrons = allSquadrons.filter(squadron => {
                if (searchTerm) {
                    const names = [squadron.long_name, squadron.tag_name, squadron.short_name, squadron.squadron_name]
                        .filter(Boolean)
                        .map(n => n.toLowerCase());
                    if (!names.some(n => n.includes(searchTerm))) return false;
                }
                if (squadron.player_count < minPlayers) {
                    return false;
                }
                return true;
            });
            
            sortSquadrons();
            displaySquadrons();
        }
        
        function sortSquadrons() {
            filteredSquadrons.sort((a, b) => {
                let valueA, valueB;
                
                if (currentSort.field === 'points') {
                    valueA = (a.points && a.points.total_points) || 0;
                    valueB = (b.points && b.points.total_points) || 0;
                } else if (currentSort.field === 'squadron_name') {
                    const nameA = a.long_name || a.tag_name || a.squadron_name || '';
                    const nameB = b.long_name || b.tag_name || b.squadron_name || '';
                    valueA = nameA.toLowerCase();
                    valueB = nameB.toLowerCase();
                } else {
                    valueA = parseFloat(a[currentSort.field]) || 0;
                    valueB = parseFloat(b[currentSort.field]) || 0;
                }
                
                if (currentSort.direction === 'asc') {
                    return valueA > valueB ? 1 : valueA < valueB ? -1 : 0;
                } else {
                    return valueA < valueB ? 1 : valueA > valueB ? -1 : 0;
                }
            });
        }
        
        function displaySquadrons() {
            const tbody = document.getElementById('leaderboardBody');
            tbody.innerHTML = '';
            
            filteredSquadrons.forEach((squadron, index) => {
                const row = document.createElement('tr');
                row.className = 'row-link';
                const squadronUrlPath = squadron.clan_id != null
                    ? String(squadron.clan_id)
                    : encodeURIComponent(squadron.long_name || squadron.tag_name || squadron.squadron_name);
                
                const rank = index + 1;
                const rankClass = rank <= 3 ? `rank-${rank}` : '';
                
                const squadronDisplayName = squadron.tag_name || squadron.squadron_name;
                
                const points = (squadron.points && squadron.points.total_points) || 0;
                const pointsDisplay = squadron.points && squadron.points.has_points_data 
                    ? formatNumber(points) 
                    : '<span style="opacity: 0.5;">N/A</span>';
                
                row.innerHTML = `
                    <td class="rank-cell ${rankClass}">
                        <a href="/squadrons/${squadronUrlPath}" class="row-link-overlay" aria-label="View ${escapeHtml(squadronDisplayName)}"></a>
                        ${rank <= 3 ? getRankIcon(rank) : '#' + rank}
                    </td>
                    <td class="stat-cell stat-points">${pointsDisplay}</td>
                    <td class="squadron-name" style="font-family: 'skyquakesymbols', 'Inter', sans-serif !important;">${escapeHtml(squadronDisplayName)}</td>
                    <td class="stat-cell stat-members">${squadron.player_count}</td>
                    <td class="stat-cell stat-kills">${formatNumber(squadron.total_kills)}</td>
                    <td class="stat-cell stat-battles">${formatNumber(squadron.total_battles)}</td>
                    <td class="stat-cell stat-wins">${formatNumber(squadron.wins)}</td>
                    <td class="stat-cell stat-winrate">${squadron.win_rate.toFixed(1)}%</td>
                    <td class="stat-cell stat-kdr">${squadron.kdr.toFixed(2)}</td>
                `;
                
                tbody.appendChild(row);
            });
            
            document.getElementById('filteredCount').textContent = filteredSquadrons.length;
        }
        
        function sortTable(field) {
            if (currentSort.field === field) {
                currentSort.direction = currentSort.direction === 'asc' ? 'desc' : 'asc';
            } else {
                currentSort.field = field;
                currentSort.direction = (field === 'squadron_name') ? 'asc' : 'desc';
            }
            
            document.querySelectorAll('.sortable').forEach(header => {
                header.classList.remove('sorted-asc', 'sorted-desc');
            });
            
            const currentHeader = document.querySelector(`[data-sort="${field}"]`);
            if (currentHeader) {
                currentHeader.classList.add(currentSort.direction === 'asc' ? 'sorted-asc' : 'sorted-desc');
            }
            
            applyFiltersAndDisplay();
        }
        
        function clearFilters() {
            document.getElementById('squadronSearch').value = '';
            document.getElementById('minPlayers').value = '1';
            applyFiltersAndDisplay();
        }
        
        function showError(message) {
            const loadingState = document.getElementById('loadingState');
            const errorState = document.getElementById('errorState');
            const errorMessage = document.getElementById('errorMessage');
            
            loadingState.style.display = 'none';
            errorState.style.display = 'block';
            errorMessage.textContent = message;
        }
        
        function getRankIcon(rank) {
            switch(rank) {
                case 1: return '<i class="fas fa-crown" style="margin-right: 0.5rem; color: #ffd700;"></i>';
                case 2: return '<i class="fas fa-medal" style="margin-right: 0.5rem; color: #c0c0c0;"></i>';
                case 3: return '<i class="fas fa-trophy" style="margin-right: 0.5rem; color: #cd7f32;"></i>';
                default: return '';
            }
        }
        
        function formatNumber(num) {
            if (num >= 1000000) {
                return (num / 1000000).toFixed(1) + 'M';
            } else if (num >= 1000) {
                return (num / 1000).toFixed(1) + 'K';
            }
            return num.toString();
        }
        
        function escapeHtml(text) {
            const div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }
        
        let searchTimeout;
        document.getElementById('squadronSearch').addEventListener('input', function() {
            clearTimeout(searchTimeout);
            searchTimeout = setTimeout(() => {
                applyFiltersAndDisplay();
            }, 300);
        });
        
        document.getElementById('minPlayers').addEventListener('input', function() {
            clearTimeout(searchTimeout);
            searchTimeout = setTimeout(() => {
                applyFiltersAndDisplay();
            }, 300);
        });
        
        document.addEventListener('DOMContentLoaded', () => {
            setTimeout(loadLeaderboard, 100);
        });
    </script>
</body>
</html>
