<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}IP Manager{% endblock %}</title>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <!-- Font Awesome - Versión estable y específica -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <!-- Fallback para FontAwesome -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.4.0/css/all.css">

    <!-- Favicon personalizado PNG -->
    <link rel="icon" type="image/png" href="{{ url_for('static', filename='img/favicon.ico') }}">

    <style>
        /* CSS de respaldo para iconos de FontAwesome */
        .fa-database::before, .fas.fa-database::before { content: "\f1c0"; }
        .fa-server::before, .fas.fa-server::before { content: "\f233"; }
        .fa-network-wired::before, .fas.fa-network-wired::before { content: "\f6ff"; }
        .fa-shield-alt::before, .fas.fa-shield-alt::before { content: "\f3ed"; }
        .fa-globe::before, .fas.fa-globe::before { content: "\f0ac"; }
        .fa-cloud::before, .fas.fa-cloud::before { content: "\f0c2"; }
        .fa-desktop::before, .fas.fa-desktop::before { content: "\f108"; }
        .fa-mobile-alt::before, .fas.fa-mobile-alt::before { content: "\f3cd"; }
        .fa-tools::before, .fas.fa-tools::before { content: "\f7d9"; }
        .fa-cogs::before, .fas.fa-cogs::before { content: "\f085"; }

        /* Asegurar que todos los iconos tengan la familia de fuente correcta */
        .fas, .far, .fab, .fal, .fad, .fa {
            font-family: "Font Awesome 6 Free", "Font Awesome 6 Pro", "Font Awesome 5 Free", FontAwesome, serif !important;
            font-weight: 900;
            display: inline-block;
        }

        :root {
            --primary-color: #2563eb;
            --primary-dark: #1d4ed8;
            --secondary-color: #f8fafc;
            --accent-color: #10b981;
            --text-primary: #1e293b;
            --text-secondary: #64748b;
            --border-color: #e2e8f0;
            --sidebar-width: 280px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8fafc;
            color: var(--text-primary);
        }

        /* Sidebar */
        .sidebar {
            position: fixed;
            top: 0;
            left: 0;
            height: 100vh;
            width: var(--sidebar-width);
            background: linear-gradient(180deg, #1e293b 0%, #334155 100%);
            color: white;
            z-index: 1000;
            transition: transform 0.3s ease;
            overflow-y: auto;
        }

        .sidebar-header {
            padding: 1.5rem;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .sidebar-logo {
            font-size: 1.5rem;
            font-weight: 700;
            color: white;
            text-decoration: none;
            display: flex;
            align-items: center;
        }

        .sidebar-nav {
            padding: 1rem 0;
        }

        .nav-item {
            margin: 0.25rem 1rem;
        }

        .nav-link {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            padding: 0.75rem 1rem;
            border-radius: 0.5rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            transition: all 0.3s ease;
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
            transform: translateX(4px);
        }

        /* Main Content */
        .main-content {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
            transition: margin-left 0.3s ease;
        }

        /* Header */
        .header {
            background: white;
            padding: 1rem 2rem;
            border-bottom: 1px solid var(--border-color);
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            display: flex;
            justify-content: between;
            align-items: center;
            max-width: 100%;
        }

        .header-left {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-left: auto;
        }

        .user-menu {
            position: relative;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--primary-color);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 600;
            cursor: pointer;
        }

        /* Page Content */
        .page-content {
            padding: 2rem;
            max-width: 1400px;
            margin: 0 auto;
        }

        .page-header {
            margin-bottom: 2rem;
        }

        .page-title {
            font-size: 1.875rem;
            font-weight: 700;
            color: var(--text-primary);
            margin-bottom: 0.5rem;
        }

        .page-subtitle {
            color: var(--text-secondary);
            font-size: 1rem;
        }

        /* Cards */
        .card {
            border: 1px solid var(--border-color);
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            background: white;
        }

        .card-header {
            background: transparent;
            border-bottom: 1px solid var(--border-color);
            padding: 1.5rem;
            font-weight: 600;
        }

        .card-body {
            padding: 1.5rem;
        }

        /* App Grid */
        .app-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 1.5rem;
            margin-top: 1.5rem;
        }

        .app-card {
            background: white;
            border: 1px solid var(--border-color);
            border-radius: 12px;
            padding: 1.5rem;
            transition: all 0.3s ease;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .app-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
            border-color: var(--primary-color);
        }

        .app-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
            background: var(--secondary-color);
        }

        .app-icon img {
            width: 32px;
            height: 32px;
            object-fit: cover;
            border-radius: 6px;
        }

        .app-name {
            font-weight: 600;
            margin-bottom: 0.5rem;
            color: var(--text-primary);
        }

        .app-description {
            color: var(--text-secondary);
            font-size: 0.875rem;
            line-height: 1.4;
            margin-bottom: 1rem;
        }

        .app-actions {
            display: flex;
            gap: 0.5rem;
            align-items: center;
        }

        .btn-favorite {
            background: none;
            border: none;
            color: var(--text-secondary);
            font-size: 1.25rem;
            transition: all 0.3s ease;
            padding: 0.25rem;
        }

        .btn-favorite.active {
            color: #fbbf24;
        }

        .btn-favorite:hover {
            color: #fbbf24;
            transform: scale(1.2);
        }

        /* Alerts */
        .alert {
            border: none;
            border-radius: 8px;
            border-left: 4px solid;
            margin-bottom: 1.5rem;
        }

        /* Responsive */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
            }

            .sidebar.show {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .page-content {
                padding: 1rem;
            }

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

        /* Utilities */
        .btn-primary {
            background: var(--primary-color);
            border-color: var(--primary-color);
            border-radius: 8px;
            font-weight: 500;
            padding: 0.5rem 1rem;
        }

        .btn-primary:hover {
            background: var(--primary-dark);
            border-color: var(--primary-dark);
        }

        .text-muted {
            color: var(--text-secondary) !important;
        }

        .search-box {
            position: relative;
            margin-bottom: 1.5rem;
        }

        .search-input {
            padding-left: 2.5rem;
            border-radius: 8px;
            border: 1px solid var(--border-color);
        }

        .search-icon {
            position: absolute;
            left: 0.75rem;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-secondary);
        }
    </style>

    {% block extra_css %}{% endblock %}
</head>

<body>
    <!-- Sidebar -->
    <nav class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <a href="{{ url_for('dashboard') }}" class="sidebar-logo">
                <img src="{{ url_for('static', filename='img/icono.png') }}" alt="Avatar" style="width:80px; height:80px;">
                IP Manager
            </a>
        </div>

        <div class="sidebar-nav">
            <div class="nav-item">
                <a href="{{ url_for('dashboard') }}" class="nav-link {% if request.endpoint == 'dashboard' %}active{% endif %}">
                    <i class="fas fa-home"></i>
                    Dashboard
                </a>
            </div>
            <div class="nav-item">
                <a href="{{ url_for('browse_applications') }}" class="nav-link {% if request.endpoint == 'browse_applications' %}active{% endif %}">
                    <i class="fas fa-th-large"></i>
                    Explorar Apps
                </a>
            </div>
            <div class="nav-item">
                <a href="{{ url_for('search') }}" class="nav-link {% if request.endpoint == 'search' %}active{% endif %}">
                    <i class="fas fa-search"></i>
                    Búsqueda
                </a>
            </div>

            {% if session.is_admin %}
            <hr style="margin: 1rem; opacity: 0.3;">
            <div class="nav-item">
                <a href="{{ url_for('admin_dashboard') }}" class="nav-link {% if 'admin' in request.endpoint %}active{% endif %}">
                    <i class="fas fa-cog"></i>
                    Administración
                </a>
            </div>
            <div class="nav-item">
                <a href="{{ url_for('admin_users') }}" class="nav-link">
                    <i class="fas fa-users"></i>
                    Usuarios
                </a>
            </div>
            <div class="nav-item">
                <a href="{{ url_for('admin_applications') }}" class="nav-link">
                    <i class="fas fa-server"></i>
                    Aplicaciones
                </a>
            </div>
            <div class="nav-item">
                <a href="{{ url_for('admin_logs') }}" class="nav-link">
                    <i class="fas fa-clipboard-list"></i>
                    Logs
                </a>
            </div>
            {% endif %}
        </div>
    </nav>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Header -->
        <header class="header">
            <div class="header-content">
                <div class="header-left">
                    <button class="btn btn-link d-md-none" onclick="toggleSidebar()">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h6 class="mb-0 d-none d-md-block">{% block header_title %}IP Manager{% endblock %}</h6>
                </div>

                <div class="header-right">
                    <div class="user-menu dropdown">
                        <div class="user-avatar" data-bs-toggle="dropdown">
                            {{ session.username[0].upper() }}
                        </div>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><h6 class="dropdown-header">{{ session.username }}</h6></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="{{ url_for('logout') }}">
                                <i class="fas fa-sign-out-alt me-2"></i>Cerrar Sesión
                            </a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </header>

        <!-- Page Content -->
        <main class="page-content">
            <!-- Flash Messages -->
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="alert alert-{{ 'danger' if category == 'error' else category }} alert-dismissible fade show">
                            <i class="fas fa-{{ 'exclamation-circle' if category == 'error' else 'check-circle' if category == 'success' else 'info-circle' }} me-2"></i>
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}

            {% block content %}{% endblock %}
        </main>
    </div>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            sidebar.classList.toggle('show');
        }

        // Cerrar sidebar al hacer click fuera en móviles
        document.addEventListener('click', function(e) {
            const sidebar = document.getElementById('sidebar');
            const toggleBtn = e.target.closest('[onclick="toggleSidebar()"]');

            if (!sidebar.contains(e.target) && !toggleBtn && window.innerWidth <= 768) {
                sidebar.classList.remove('show');
            }
        });

        // Auto-cerrar alerts después de 5 segundos
        setTimeout(function() {
            document.querySelectorAll('.alert').forEach(function(alert) {
                if (alert.querySelector('.btn-close')) {
                    alert.querySelector('.btn-close').click();
                }
            });
        }, 5000);

        // Función para toggle de favoritos
        function toggleFavorite(appId, btn) {
            fetch(`/favorite/${appId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    btn.classList.toggle('active');
                    btn.innerHTML = data.is_favorite ?
                        '<i class="fas fa-star"></i>' :
                        '<i class="far fa-star"></i>';
                }
            })
            .catch(error => console.error('Error:', error));
        }

        // Función para acceder a aplicación
        function accessApp(appId) {
            window.open(`/access/${appId}`, '_blank');
        }
    </script>

    {% block extra_js %}{% endblock %}
</body>
</html>
