<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/tracker.php';
track_visitor();

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Lili Records Radio - La mejor música en vivo.';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? 'assets/pasted-20260215-164611-6d2aee42.png';

// WhatsApp info
$whatsapp_link = "https://chat.whatsapp.com/DkG96pTzAFO3hvLqmzwmTY";
$whatsapp_number = '+5359177041';
$facebook_link = "https://www.facebook.com/profile.php?id=61587890927489";
?>
<!doctype html>
<html lang="es">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Lili Records Radio</title>
    
    <!-- Meta tags SEO -->
    <meta name="description" content="<?= htmlspecialchars($projectDescription) ?>" />
    <meta property="og:title" content="Lili Records Radio" />
    <meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
    <meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
    <meta property="twitter:card" content="summary_large_image" />

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <script type="module" src="https://cdn.jsdelivr.net/npm/emoji-picker-element@1/index.js"></script>

    <style>
        :root {
            --accent-color: #00e676; /* WhatsApp Green */
            --primary-color: #38bdf8; /* Sky Blue */
            --bg-overlay: rgba(15, 23, 42, 0.5); /* More transparent overlay */
            --glass-bg: rgba(15, 23, 42, 0.3); /* Transparent card background */
            --glass-border: rgba(255, 255, 255, 0.2);
        }

        body, html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            font-family: 'Inter', sans-serif;
            color: #ffffff;
            background-color: #000;
        }

        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: url('assets/images/background.jpg') center/cover no-repeat;
            z-index: -1;
        }

        .background::after {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: var(--bg-overlay);
        }

        .app-container {
            display: flex;
            flex-direction: column;
            width: 100%;
            min-height: 100vh;
            align-items: center;
            justify-content: center;
            padding: 4rem 2rem;
            box-sizing: border-box;
            gap: 3rem;
        }

        /* Center Section: Player */
        .player-section {
            width: 100%;
            max-width: 850px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            z-index: 10;
        }

        .glass-card {
            background: var(--glass-bg);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 2px solid transparent;
            border-radius: 32px;
            padding: 2.5rem;
            box-shadow: 0 15px 45px rgba(0, 0, 0, 0.4),
                        0 0 30px var(--dynamic-glow-dim, transparent);
            position: relative;
            background-clip: padding-box;
            overflow: hidden;
            transition: all 0.5s ease, box-shadow 0.1s ease;
        }

        /* Animated colorful border */
        .glass-card::before {
            content: "";
            position: absolute;
            inset: 0;
            border-radius: 32px;
            padding: 2px; /* thickness */
            background: linear-gradient(45deg, var(--dynamic-glow, #00e676), #38bdf8, var(--dynamic-glow-dim, #facc15), #f472b6, var(--dynamic-glow, #00e676));
            background-size: 400% 400%;
            -webkit-mask: 
                linear-gradient(#fff 0 0) content-box, 
                linear-gradient(#fff 0 0);
            -webkit-mask-composite: xor; 
            mask-composite: exclude;
            pointer-events: none;
            animation: gradient-border 8s linear infinite;
            transition: background 0.1s ease;
        }

        @keyframes gradient-border {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .brand h1 {
            font-size: 2.8rem;
            font-weight: 800;
            margin: 0 0 0.5rem;
            background: linear-gradient(to right, #fff, var(--primary-color), var(--accent-color));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: -1px;
        }

        .brand-logo {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            object-fit: cover;
            border: 4px solid var(--dynamic-glow, var(--primary-color));
            box-shadow: 0 0 25px var(--dynamic-glow, rgba(56, 189, 248, 0.7)), 
                        0 0 50px var(--dynamic-glow-dim, rgba(56, 189, 248, 0.4));
            transition: transform 0.3s ease, border-color 0.1s ease, box-shadow 0.1s ease;
        }

        .logo-wrapper {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 2rem;
            margin-bottom: 1.5rem;
            flex-wrap: wrap;
        }

        .brand-logo:hover {
            transform: scale(1.1) rotate(5deg);
        }

        .brand p {
            font-size: 1rem;
            opacity: 0.8;
            margin-bottom: 2rem;
        }

        /* Radio Player UI */
        .radio-player {
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
        }

        .now-playing {
            display: flex;
            align-items: center;
            gap: 1rem;
            background: rgba(0, 0, 0, 0.3);
            padding: 1rem;
            border-radius: 12px;
        }

        .now-playing i {
            font-size: 1.5rem;
            color: var(--primary-color);
            animation: pulse 2s infinite;
        }

        .track-info {
            overflow: hidden;
            flex: 1;
        }

        .track-title {
            font-weight: 800;
            font-size: 1.5rem;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            transition: opacity 0.5s ease;
            color: #fff;
            text-shadow: 0 0 15px var(--primary-color), 0 0 30px rgba(0,0,0,0.5);
            display: block;
            margin-top: 0.2rem;
        }

        .track-label {
            font-size: 0.75rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 2px;
            color: var(--accent-color);
            margin-bottom: -5px;
            display: block;
        }

        @keyframes marquee {
            0% { transform: translateX(0); }
            100% { transform: translateX(-50%); }
        }

        .track-title.scrolling {
            text-overflow: clip;
        }

        .track-title.scrolling span {
            display: inline-block;
            animation: marquee 15s linear infinite;
            padding-right: 2rem;
        }

        .track-status {
            font-size: 0.85rem;
            opacity: 0.6;
        }

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

        .play-btn {
            width: 72px;
            height: 72px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--primary-color), #0ea5e9);
            border: none;
            color: #fff;
            font-size: 2rem;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            box-shadow: 0 0 20px rgba(56, 189, 248, 0.4), inset 0 0 10px rgba(255,255,255,0.2);
            position: relative;
        }

        .play-btn::after {
            content: '';
            position: absolute;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            border: 2px solid var(--primary-color);
            opacity: 0;
            transition: all 0.4s;
        }

        .play-btn:hover::after {
            opacity: 1;
            transform: scale(1.2);
        }

        .play-btn.playing {
            background: linear-gradient(135deg, #ff4444, #cc0000) !important;
            box-shadow: 0 0 25px rgba(255, 68, 68, 0.6);
            transform: scale(1.1);
            animation: pulse-button 1.5s infinite;
        }

        @keyframes pulse-button {
            0% { box-shadow: 0 0 0 0 rgba(255, 68, 68, 0.7); }
            70% { box-shadow: 0 0 0 20px rgba(255, 68, 68, 0); }
            100% { box-shadow: 0 0 0 0 rgba(255, 68, 68, 0); }
        }

        .now-playing {
            display: flex;
            align-items: center;
            gap: 1rem;
            background: rgba(255, 255, 255, 0.05);
            padding: 1.2rem;
            border-radius: 16px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: inset 0 0 15px rgba(0,0,0,0.2);
        }

        .play-btn:hover {
            transform: scale(1.05);
            background: #0ea5e9;
        }

        .visualizer-container {
            width: 100%;
            height: 120px;
            margin-bottom: 1rem;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            gap: 4px;
            overflow: hidden;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            background: radial-gradient(circle at center, rgba(56, 189, 248, 0.05) 0%, transparent 70%);
            position: relative;
            padding: 0 10px;
        }

        .audio-visualizer {
            display: flex;
            align-items: flex-end;
            justify-content: center;
            gap: 4px;
            width: 100%;
            height: 100%;
        }

        .visualizer-bar {
            width: 18px;
            height: 5px;
            border-radius: 4px 4px 0 0;
            background: var(--primary-color);
            transition: height 0.05s ease;
            min-height: 5px;
        }

        /* Colores vibrantes para las barras */
        .visualizer-bar:nth-child(5n+1) { background: linear-gradient(to top, #00e676, #69f0ae); }
        .visualizer-bar:nth-child(5n+2) { background: linear-gradient(to top, #38bdf8, #7dd3fc); }
        .visualizer-bar:nth-child(5n+3) { background: linear-gradient(to top, #facc15, #fde047); }
        .visualizer-bar:nth-child(5n+4) { background: linear-gradient(to top, #f472b6, #fb923c); }
        .visualizer-bar:nth-child(5n+5) { background: linear-gradient(to top, #818cf8, #a5b4fc); }

        /* Playing Animations */
        body.is-playing .glass-card {
            animation: card-pulse 4s infinite ease-in-out;
            border-color: rgba(56, 189, 248, 0.5);
        }

        .featured-img-container {
            width: 100%;
            max-width: 900px;
            position: relative;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
            aspect-ratio: 16 / 10;
            transition: all 0.5s ease;
        }

        @keyframes card-pulse {
            0%, 100% { 
                transform: translate(var(--shake-offset-x, 0), var(--shake-offset-y, 0)) scale(1); 
                box-shadow: 0 15px 45px rgba(0, 0, 0, 0.4), 0 0 20px var(--dynamic-glow-dim, rgba(0,0,0,0)); 
            }
            50% { 
                transform: translate(var(--shake-offset-x, 0), var(--shake-offset-y, 0)) scale(1.01); 
                box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5), 0 0 40px var(--dynamic-glow, rgba(56, 189, 248, 0.4)); 
            }
        }

        @keyframes float-img {
            0%, 100% { transform: translateY(0) scale(1); }
            50% { transform: translateY(-15px) scale(1.02); }
        }

        .volume-slider {
            flex: 1;
            height: 6px;
            -webkit-appearance: none;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 3px;
            outline: none;
        }

        .volume-slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 16px;
            height: 16px;
            background: #fff;
            border-radius: 50%;
            cursor: pointer;
        }

        /* Interaction Form */
        .interaction-form {
            margin-top: 2rem;
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
            text-align: left;
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1.5rem;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 0.8rem;
        }

        .interaction-form label {
            font-size: 0.85rem;
            font-weight: 700;
            opacity: 0.9;
            letter-spacing: 0.5px;
        }

        .interaction-form input, 
        .interaction-form textarea {
            width: 100%;
            padding: 0.8rem;
            border-radius: 12px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            background-color: rgba(255, 255, 255, 0.9); /* Semi-transparent white */
            color: #0f172a; 
            font-family: inherit;
            font-size: 0.95rem;
            box-sizing: border-box;
            outline: none;
            transition: all 0.2s;
        }

        .interaction-form textarea {
            resize: none;
            height: 80px;
        }

        .interaction-form input::placeholder,
        .interaction-form textarea::placeholder {
            color: #64748b;
        }

        .send-whatsapp-btn {
            background-color: var(--accent-color);
            color: #fff;
            border: none;
            padding: 0.8rem;
            border-radius: 12px;
            font-weight: 700;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            transition: transform 0.2s, background 0.2s;
        }

        .send-whatsapp-btn:hover {
            transform: translateY(-2px);
            background-color: #00c853;
        }

        /* Center Section: Featured Image */
        .image-section {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 5;
        }

        .featured-img-container {
            width: 100%;
            max-width: 900px;
            position: relative;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
            aspect-ratio: 16 / 10;
        }

        .featured-img-container img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            background: rgba(0,0,0,0.2);
            transition: transform 0.5s;
        }

        .featured-img-container:hover img {
            transform: scale(1.05);
        }

        /* Transfermovil QR Section */
        .payment-section {
            margin-top: 2rem;
            text-align: center;
        }

        .qr-placeholder {
            width: 180px;
            height: 180px;
            margin: 1rem auto;
            background: rgba(255, 255, 255, 0.85);
            border-radius: 12px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: #333;
            padding: 10px;
            backdrop-filter: blur(4px);
        }

        .qr-placeholder i {
            font-size: 3rem;
            margin-bottom: 10px;
            color: #000;
        }

        .qr-placeholder span {
            font-size: 0.75rem;
            font-weight: 700;
            text-transform: uppercase;
        }

        /* Floating Social Buttons */
        .social-float-container {
            position: fixed;
            bottom: 2rem;
            right: 2rem;
            display: flex;
            flex-direction: column;
            gap: 1rem;
            z-index: 100;
        }

        .social-float {
            width: 60px;
            height: 60px;
            color: #FFF;
            border-radius: 50%;
            text-align: center;
            font-size: 28px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            position: relative;
        }

        .social-float::before {
            content: "";
            position: absolute;
            inset: 0;
            border-radius: 50%;
            padding: 3px;
            background: linear-gradient(45deg, #00e676, #38bdf8, #facc15, #f472b6, #00e676);
            background-size: 400% 400%;
            -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
            -webkit-mask-composite: xor;
            mask-composite: exclude;
            pointer-events: none;
            animation: gradient-border 6s linear infinite;
        }

        .social-float:hover {
            transform: scale(1.1) rotate(8deg);
            color: #fff;
        }

        .social-float.whatsapp { 
            background-color: var(--accent-color); 
            box-shadow: 0 0 20px rgba(0, 230, 118, 0.5);
        }
        
        .social-float.youtube { 
            background-color: #ff0000; 
            box-shadow: 0 0 20px rgba(255, 0, 0, 0.5);
        }

        .social-float.facebook { 
            background-color: #1877F2; 
            box-shadow: 0 0 20px rgba(24, 119, 242, 0.5);
        }

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

        @keyframes pulse {
            0% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.1); opacity: 0.7; }
            100% { transform: scale(1); opacity: 1; }
        }

        @keyframes alert-pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.3); background: #facc15 !important; }
            100% { transform: scale(1); }
        }
        .counter-alert {
            animation: alert-pulse 1s infinite ease-in-out;
            box-shadow: 0 0 10px #facc15;
        }

        /* Responsive */
        @media (max-width: 992px) {
            .app-container {
                flex-direction: column;
                height: auto;
                overflow-y: auto;
                padding-top: 4rem;
            }
            .player-section, .interaction-center {
                max-width: 100% !important;
                margin-bottom: 2rem;
            }
            .interaction-center {
                grid-template-columns: 1fr !important;
            }
            .chat-window, .ranking-window {
                height: 400px !important;
            }
            .image-section {
                padding-left: 0;
                width: 100%;
            }
            .featured-img-container {
                aspect-ratio: 1 / 1;
            }
        }
        @media (max-width: 600px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            .glass-card {
                padding: 1.5rem;
            }
            .brand h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="background"></div>

    <div class="app-container">
        <!-- Left Section: Player -->
        <section class="player-section">
            <div class="glass-card">
                <header class="brand">
                    <div class="logo-wrapper">
                        <img src="./assets/pasted-20260215-163754-def41f49.png" alt="Lili Records Logo" class="brand-logo">
                        <img src="./assets/pasted-20260215-171328-d90df4ce.jpg" alt="Logo Secundario" class="brand-logo">
                    </div>
                    <h1>Lili Records</h1>
                    <p>Siente la música, vive el ritmo.</p>
                </header>

                <div class="radio-player">
                    <div class="visualizer-container">
                        <div id="audio-visualizer" class="audio-visualizer"></div>
                    </div>
                    <div class="now-playing">
                        <i class="bi bi-broadcast"></i>
                        <div class="track-info">
                            <span class="track-label">ESTÁS ESCUCHANDO:</span>
                            <div id="track-title" class="track-title">Cargando stream...</div>
                            <div class="track-status">EN VIVO</div>
                        </div>
                        <button id="like-song-btn" onclick="likeSong()" style="background: none; border: none; color: #ff4444; font-size: 1.8rem; cursor: pointer; transition: transform 0.2s; display: flex; align-items: center; gap: 5px;">
                            <i class="bi bi-heart"></i>
                            <span id="like-count" style="font-size: 0.9rem; color: white; opacity: 0.8; font-weight: bold;"></span>
                        </button>
                    </div>

                    <div class="controls">
                        <button id="play-pause" class="play-btn" onclick="togglePlay()">
                            <i id="play-icon" class="bi bi-play-fill"></i>
                        </button>
                        <i class="bi bi-volume-up"></i>
                        <input type="range" class="volume-slider" min="0" max="1" step="0.01" value="1" oninput="changeVolume(this.value)">
                    </div>

                    <div class="interaction-form">
                        <div class="form-row">
                            <div class="form-group">
                                <label for="user-name">NOMBRE</label>
                                <input type="text" id="user-name" placeholder="Tu nombre..." oninput="this.style.borderColor='rgba(255, 255, 255, 0.2)'">
                            </div>
                            
                            <div class="form-group">
                                <label for="user-phone">MOVIL</label>
                                <input type="tel" id="user-phone" placeholder="Tu número..." oninput="this.style.borderColor='rgba(255, 255, 255, 0.2)'" onchange="savePhone(this.value)">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="user-message">MENSAJE</label>
                            <textarea id="user-message" placeholder="¿Qué quieres escuchar?"></textarea>
                        </div>
                        
                        <button class="send-whatsapp-btn" onclick="sendToWhatsApp()">
                            <i class="bi bi-whatsapp"></i> WHATSAPP
                        </button>
                    </div>

                    <!-- Espacio para Código QR -->
                    <div class="payment-section">
                        <div class="qr-placeholder">
                            <i class="bi bi-qr-code-scan"></i>
                            <span>TU CÓDIGO QR AQUÍ</span>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (isset($_GET['admin']) && $_GET['admin'] === '1'): ?>
            <!-- Admin Real-Time Stats (Only visible with ?admin=1) -->
            <div class="glass-card mt-4" style="margin-top: 2rem;">
                <h3 style="font-size: 1.2rem; margin-bottom: 1rem; color: var(--accent-color);">
                    <i class="bi bi-shield-lock"></i> Panel Admin Real-Time
                </h3>
                <iframe src="admin.php?token=lili_admin_2026" style="width: 100%; height: 400px; border: none; border-radius: 12px; background: rgba(0,0,0,0.2);"></iframe>
            </div>
            <?php endif; ?>
        </section>

        <!-- Interaction Center: Live Chat -->
        <section class="interaction-center" style="width: 100%; max-width: 600px; z-index: 10;">
            <!-- Live Web Chat -->
            <div class="glass-card chat-window" style="height: 500px; display: flex; flex-direction: column;">
                <h3 style="font-size: 1.2rem; margin-bottom: 1rem; color: var(--primary-color);">
                    <i class="bi bi-chat-dots-fill"></i> CHAT EN VIVO
                </h3>
                <div id="chat-messages" style="flex: 1; overflow-y: auto; margin-bottom: 1rem; padding-right: 5px; display: flex; flex-direction: column; gap: 0.8rem;">
                    <!-- Mensajes se cargarán aquí -->
                    <div style="opacity: 0.5; font-size: 0.9rem; text-align: center; margin-top: 2rem;">Cargando mensajes...</div>
                </div>
                <div class="chat-input-area" style="display: flex; gap: 0.5rem; position: relative;">
                    <input type="hidden" id="chat-user">
                    <button onclick="toggleEmojiPicker()" style="background: rgba(255,255,255,0.1); border: none; border-radius: 8px; color: white; padding: 0 0.5rem; cursor: pointer;">
                        <i class="bi bi-emoji-smile"></i>
                    </button>
                    <button id="camera-btn" onclick="document.getElementById('chat-file').click()" title="Subir foto" style="background: rgba(255,255,255,0.1); border: none; border-radius: 8px; color: white; padding: 0 0.5rem; cursor: pointer; position: relative; transition: all 0.3s;">
                        <i class="bi bi-camera-fill"></i>
                        <span id="photo-counter" style="position: absolute; top: -10px; right: -5px; background: var(--primary-color); color: white; font-size: 0.6rem; padding: 1px 4px; border-radius: 10px; font-weight: bold; border: 1px solid rgba(255,255,255,0.3); display: none;">0/5</span>
                    </button>
                    <div id="photo-limit-msg" style="display: none; position: absolute; bottom: -20px; left: 0; width: 100%; text-align: center; font-size: 0.65rem; color: #ff4444; font-weight: bold; animation: fadeIn 0.3s;"></div>
                    <input type="file" id="chat-file" style="display: none;" accept="image/*" onchange="uploadImage(this)">
                    <input type="text" id="chat-msg" placeholder="Escribe un mensaje..." style="flex: 1; font-size: 0.8rem; padding: 0.5rem; border-radius: 8px; border: none; background: rgba(255,255,255,0.1); color: white;">
                    <button onclick="sendChatMessage()" style="background: var(--primary-color); border: none; border-radius: 8px; color: white; padding: 0 1rem; cursor: pointer;">
                        <i class="bi bi-send-fill"></i>
                    </button>

                    <emoji-picker id="emoji-picker" style="display: none; position: absolute; bottom: 50px; left: 0; z-index: 1000; --num-columns: 6; --category-button-size: 1.5rem; width: 300px; height: 350px;"></emoji-picker>
                </div>
            </div>
        </section>

    </div>

    <!-- Floating Social Links -->
    <div class="social-float-container">
        <a href="<?= $facebook_link ?>" class="social-float facebook" target="_blank" title="Facebook">
            <i class="bi bi-facebook"></i>
        </a>
        <a href="https://www.youtube.com/@lilirecords" class="social-float youtube" target="_blank" title="YouTube">
            <i class="bi bi-youtube"></i>
        </a>
        <a href="<?= $whatsapp_link ?>" class="social-float whatsapp" target="_blank" title="WhatsApp">
            <i class="bi bi-whatsapp"></i>
        </a>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.6.0/dist/confetti.browser.min.js"></script>
    <audio id="radio-audio" src="https://listen.radioking.com/radio/828046/stream/897251" preload="none" crossorigin="anonymous"></audio>
    <audio id="welcome-sound" src="https://assets.mixkit.co/active_storage/sfx/2013/2013-preview.mp3" preload="auto"></audio>

    <script>
        const audio = document.getElementById('radio-audio');
        const playBtn = document.getElementById('play-pause');
        const playIcon = document.getElementById('play-icon');
        const trackTitle = document.getElementById('track-title');
        const visualizerContainer = document.getElementById('audio-visualizer');

        let audioCtx;
        let analyzer;
        let source;
        let animationId;
        const BAR_COUNT = 16;
        let visualizerBars = [];

        function initVisualizer() {
            if (audioCtx) return;
            
            // Create bars
            visualizerContainer.innerHTML = '';
            visualizerBars = [];
            for (let i = 0; i < BAR_COUNT; i++) {
                const bar = document.createElement('div');
                bar.className = 'visualizer-bar';
                visualizerContainer.appendChild(bar);
                visualizerBars.push(bar);
            }

            audioCtx = new (window.AudioContext || window.webkitAudioContext)();
            analyzer = audioCtx.createAnalyser();
            source = audioCtx.createMediaElementSource(audio);
            source.connect(analyzer);
            analyzer.connect(audioCtx.destination);
            
            analyzer.fftSize = 128;
            const bufferLength = analyzer.frequencyBinCount;
            const dataArray = new Uint8Array(bufferLength);

            let colorOffset = 0;
            function draw() {
                if (audio.paused) {
                    visualizerBars.forEach(bar => bar.style.height = '5px');
                    document.documentElement.style.setProperty('--dynamic-glow', 'rgba(56, 189, 248, 0.7)');
                    document.documentElement.style.setProperty('--dynamic-glow-dim', 'rgba(56, 189, 248, 0.4)');
                    return;
                }
                animationId = requestAnimationFrame(draw);
                analyzer.getByteFrequencyData(dataArray);

                colorOffset += 1;

                for (let i = 0; i < BAR_COUNT; i++) {
                    // Map frequency data to bars
                    const freqIndex = Math.floor((i / BAR_COUNT) * (dataArray.length * 0.8));
                    const val = dataArray[freqIndex];
                    const height = (val / 255) * 100 + 5;
                    
                    if (visualizerBars[i]) {
                        visualizerBars[i].style.height = `${height}%`;
                    }
                }

                // Dynamic glow effect based on average volume
                let sum = 0;
                for(let i=0; i<dataArray.length; i++) sum += dataArray[i];
                const average = sum / dataArray.length;
                const dominantHue = (average * 2 + colorOffset) % 360;
                document.documentElement.style.setProperty('--dynamic-glow', `hsla(${dominantHue}, 100%, 60%, 0.8)`);
                document.documentElement.style.setProperty('--dynamic-glow-dim', `hsla(${dominantHue}, 100%, 60%, 0.3)`);
            }
            draw();
        }

        function togglePlay() {
            if (audioCtx && audioCtx.state === 'suspended') {
                audioCtx.resume();
            }

            if (audio.paused) {
                initVisualizer();
                audio.play();
                playIcon.classList.remove('bi-play-fill');
                playIcon.classList.add('bi-pause-fill');
                playBtn.classList.add('playing');
                document.body.classList.add('is-playing');
            } else {
                audio.pause();
                playIcon.classList.remove('bi-pause-fill');
                playIcon.classList.add('bi-play-fill');
                playBtn.classList.remove('playing');
                document.body.classList.remove('is-playing');
            }
        }

        function changeVolume(val) {
            audio.volume = val;
        }

        function savePhone(phone) {
            const phoneInput = document.getElementById('user-phone');
            const phoneRegex = /^\+?[0-9]{7,15}$/;
            const cleanPhone = phone.replace(/\s/g, '');
            
            if (cleanPhone && !phoneRegex.test(cleanPhone)) {
                phoneInput.style.borderColor = '#ff4444';
                return;
            }
            
            phoneInput.style.borderColor = 'rgba(255, 255, 255, 0.2)';
            
            const formData = new FormData();
            formData.append('phone', cleanPhone);
            fetch('api/save_phone.php', {
                method: 'POST',
                body: formData
            });
        }

        function sendToWhatsApp() {
            const name = document.getElementById('user-name').value.trim();
            const phone = document.getElementById('user-phone').value.trim();
            const message = document.getElementById('user-message').value.trim();
            const phoneRegex = /^\+?[0-9]{7,15}$/;

            if (name.length < 3) {
                alert('Por favor, ingresa un nombre válido (mínimo 3 caracteres).');
                document.getElementById('user-name').style.borderColor = '#ff4444';
                return;
            }

            if (!phone) {
                alert('Por favor, ingresa tu número de móvil para continuar.');
                document.getElementById('user-phone').style.borderColor = '#ff4444';
                return;
            }

            if (!phoneRegex.test(phone.replace(/\s/g, ''))) {
                alert('Por favor, ingresa un número de móvil válido (ej: +5359177041).');
                document.getElementById('user-phone').style.borderColor = '#ff4444';
                return;
            }

            const now = new Date();
            const dateTime = now.toLocaleDateString() + ' ' + now.toLocaleTimeString();
            const refId = Math.floor(1000 + Math.random() * 9000);
            const currentSong = document.getElementById('track-title').innerText.replace(/\s{2,}/g, ' ').trim();
            
            const text = `*PETICIÓN RADIO* (Ref: #${refId})\n\n` +
                         `*Nombre:* ${name}\n` +
                         `*Móvil:* ${phone}\n` +
                         `*Fecha:* ${dateTime}\n` +
                         `*Sonando ahora:* ${currentSong}\n\n` +
                         `*Mensaje:* ${message || 'Sin mensaje específico'}`;
            
            const url = `https://wa.me/<?= str_replace('+', '', $whatsapp_number) ?>?text=${encodeURIComponent(text)}`;
            window.open(url, '_blank');
        }

        // --- Chat Functionality ---
        const chatMessages = document.getElementById('chat-messages');
        const chatUser = document.getElementById('chat-user');
        const chatMsg = document.getElementById('chat-msg');
        const emojiPicker = document.getElementById('emoji-picker');
        let lastMessageCount = 0;

        function toggleEmojiPicker() {
            emojiPicker.style.display = emojiPicker.style.display === 'none' ? 'block' : 'none';
        }

        emojiPicker.addEventListener('emoji-click', event => {
            chatMsg.value += event.detail.unicode;
            emojiPicker.style.display = 'none';
            chatMsg.focus();
        });

        // Close emoji picker when clicking outside
        document.addEventListener('click', (e) => {
            if (!emojiPicker.contains(e.target) && !e.target.closest('button[onclick="toggleEmojiPicker()"]')) {
                emojiPicker.style.display = 'none';
            }
        });

        async function uploadImage(input) {
            if (!input.files || !input.files[0]) return;
            
            const file = input.files[0];
            const formData = new FormData();
            formData.append('image', file);

            try {
                const response = await fetch('api/upload.php', {
                    method: 'POST',
                    body: formData
                });
                const result = await response.json();
                if (result.success) {
                    sendChatMessage(result.url, 'image');
                    updatePhotoCounter();
                } else {
                    alert(result.error || 'Error al subir la imagen');
                }
            } catch (error) {
                console.error('Upload error:', error);
                alert('Error de conexión al subir la imagen');
            }
            input.value = ''; // Reset input
        }

        async function fetchMessages() {
            try {
                const response = await fetch('api/chat.php');
                const messages = await response.json();
                
                const currentUserName = document.getElementById('user-name').value.trim() || 'Anónimo';
                chatMessages.innerHTML = '';
                messages.forEach(msg => {
                    const div = document.createElement('div');
                    const isLike = msg.message.includes('❤️');
                    
                    const customColor = msg.custom_color ? `color: ${msg.custom_color} !important;` : '';

                    div.style.background = isLike ? 'rgba(255, 68, 68, 0.15)' : 'rgba(255,255,255,0.05)';
                    div.style.padding = '0.8rem';
                    div.style.borderRadius = '12px';
                    div.style.borderLeft = `4px solid ${isLike ? '#ff4444' : (msg.username === currentUserName ? 'var(--primary-color)' : 'rgba(255,255,255,0.2)')}`;
                    
                    let content = '';
                    if (msg.type === 'image') {
                        content = `<img src="${msg.message}" style="max-width: 100%; border-radius: 8px; margin-top: 5px; cursor: pointer;" onclick="window.open('${msg.message}', '_blank')">`;
                    } else {
                        content = `<div style="font-size: 0.85rem; line-height: 1.4; ${isLike ? 'font-weight: 600;' : ''}">${msg.message}</div>`;
                    }

                    div.innerHTML = `
                        <div style="font-size: 0.7rem; font-weight: bold; ${customColor || (isLike ? 'color: #ff4444' : 'var(--primary-color)')}; margin-bottom: 2px;">
                            ${msg.username}
                        </div>
                        ${content}
                        <div style="font-size: 0.6rem; opacity: 0.4; margin-top: 4px; text-align: right;">${new Date(msg.created_at).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})}</div>
                    `;
                    chatMessages.appendChild(div);
                });
                chatMessages.scrollTop = chatMessages.scrollHeight;
                lastMessageCount = messages.length;
            } catch (error) {
                console.error('Chat error:', error);
            }
        }

        async function sendChatMessage(msgContent = null, type = 'text') {
            const userNameInput = document.getElementById('user-name');
            const userPhoneInput = document.getElementById('user-phone');
            const user = userNameInput.value.trim();
            const phone = userPhoneInput.value.trim();
            const message = msgContent || chatMsg.value.trim();
            const phoneRegex = /^\+?[0-9]{7,15}$/;

            if (user.length < 3) {
                alert('Por favor, ingresa un nombre válido arriba (mínimo 3 caracteres).');
                userNameInput.style.borderColor = '#ff4444';
                return;
            }

            if (!phone) {
                alert('Por favor, regístrate con tu móvil arriba para poder chatear.');
                userPhoneInput.style.borderColor = '#ff4444';
                return;
            }

            if (!phoneRegex.test(phone.replace(/\s/g, ''))) {
                alert('Por favor, ingresa un número de móvil válido arriba.');
                userPhoneInput.style.borderColor = '#ff4444';
                return;
            }

            if (!message) return;

            try {
                const response = await fetch('api/chat.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username: user, message: message, type: type })
                });
                const result = await response.json();
                if (result.success) {
                    if (!msgContent) chatMsg.value = '';
                    fetchMessages();
                }
            } catch (error) {
                console.error('Error sending message:', error);
            }
        }

        chatMsg.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') sendChatMessage();
        });

        async function updatePhotoCounter() {
            try {
                const response = await fetch('api/photo_status.php');
                const data = await response.json();
                const counter = document.getElementById('photo-counter');
                const cameraBtn = document.getElementById('camera-btn');
                const fileInput = document.getElementById('chat-file');
                const limitMsg = document.getElementById('photo-limit-msg');

                if (data && typeof data.count !== 'undefined') {
                    counter.innerText = `${data.count}/${data.limit}`;
                    counter.style.display = 'block';
                    
                    // Alerta cuando solo queda 1 foto (4 de 5)
                    if (data.count === data.limit - 1) {
                        counter.classList.add('counter-alert');
                    } else {
                        counter.classList.remove('counter-alert');
                    }
                    
                    if (data.count >= data.limit) {
                        counter.style.background = '#ff4444';
                        // Bloquear visualmente el botón
                        cameraBtn.style.opacity = '0.4';
                        cameraBtn.style.filter = 'grayscale(1)';
                        cameraBtn.style.pointerEvents = 'none';
                        cameraBtn.title = 'Límite de fotos alcanzado';
                        fileInput.disabled = true;

                        // Mostrar tiempo restante
                        if (data.reset_in_seconds > 0) {
                            const hours = Math.floor(data.reset_in_seconds / 3600);
                            const minutes = Math.floor((data.reset_in_seconds % 3600) / 60);
                            const timeStr = hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
                            limitMsg.innerText = `Disponible en ${timeStr}`;
                            limitMsg.style.display = 'block';
                        }
                    } else {
                        counter.style.background = 'var(--primary-color)';
                        // Restaurar el botón
                        cameraBtn.style.opacity = '1';
                        cameraBtn.style.filter = 'none';
                        cameraBtn.style.pointerEvents = 'auto';
                        cameraBtn.title = 'Subir foto';
                        fileInput.disabled = false;
                        limitMsg.style.display = 'none';
                    }
                }
            } catch (error) {
                console.error('Error updating photo counter:', error);
            }
        }

        // Initial fetch and poll
        fetchMessages();
        updatePhotoCounter();
        setInterval(fetchMessages, 3000);
        setInterval(updatePhotoCounter, 30000); // Check limit status every 30s
        // --- End Chat Functionality ---

        async function likeSong() {
            const nameInput = document.getElementById('user-name');
            const name = nameInput.value.trim();
            const song = document.getElementById('track-title').innerText.replace(/\s{2,}/g, ' ').trim();
            
            if (name.length < 3) {
                alert('Por favor, ingresa tu nombre arriba para dar Like.');
                nameInput.style.borderColor = '#ff4444';
                return;
            }

            const btn = document.getElementById('like-song-btn');
            const icon = btn.querySelector('i');
            
            icon.classList.remove('bi-heart');
            icon.classList.add('bi-heart-fill');
            btn.style.transform = 'scale(1.3)';
            
            try {
                const response = await fetch('api/like_song.php', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username: name, song_title: song })
                });
                const result = await response.json();
                if (result.success) {
                    // Confetti explosion!
                    confetti({
                        particleCount: 100,
                        spread: 70,
                        origin: { y: 0.6 },
                        colors: ['#ff4444', '#00e676', '#38bdf8', '#facc15']
                    });

                    document.getElementById('like-count').innerText = result.likes_count;
                    setTimeout(() => {
                        btn.style.transform = 'scale(1)';
                    }, 200);
                    fetchMessages(); 
                    fetchTopFans(); // Update ranking
                }
            } catch (error) {
                console.error('Error liking song:', error);
            }
        }

        // Fetch Now Playing Metadata from RadioKing
        async function updateMetadata() {
            try {
                // RadioKing Public API for current track
                const response = await fetch('https://api.radioking.io/widget/radio/828046/track/current');
                const data = await response.json();
                
                if (data && data.title) {
                    const title = data.title;
                    const artist = data.artist || 'Lili Records';
                    // Clean up title if it contains artist
                    let fullDisplay = title.includes(artist) ? title : `${artist} - ${title}`;
                    
                    if (trackTitle.textContent !== fullDisplay) {
                        trackTitle.style.opacity = '0';
                        
                        // Reset Like Button
                        const likeIcon = document.querySelector('#like-song-btn i');
                        likeIcon.classList.remove('bi-heart-fill');
                        likeIcon.classList.add('bi-heart');
                        document.getElementById('like-count').innerText = '';

                        trackTitle.classList.remove('scrolling');
                        setTimeout(async () => {
                            trackTitle.textContent = fullDisplay;
                            trackTitle.style.opacity = '1';
                            
                            // Fetch likes for this song
                            try {
                                const lResp = await fetch(`api/get_likes.php?song_title=${encodeURIComponent(fullDisplay)}`);
                                const lData = await lResp.json();
                                if (lData.likes_count > 0) {
                                    document.getElementById('like-count').innerText = lData.likes_count;
                                }
                            } catch (e) {}

                            // Check if scrolling is needed
                            if (trackTitle.scrollWidth > trackTitle.clientWidth) {
                                trackTitle.classList.add('scrolling');
                                // Double the text for seamless loop
                                trackTitle.innerHTML = `<span>${fullDisplay} &nbsp;&nbsp;&nbsp;&nbsp; ${fullDisplay} &nbsp;&nbsp;&nbsp;&nbsp;</span>`;
                            }
                        }, 500);
                    }
                    
                    document.title = `▶ ${fullDisplay} | Lili Records Radio`;

                    // Media Session API for System Controls
                    if ('mediaSession' in navigator) {
                        navigator.mediaSession.metadata = new MediaMetadata({
                            title: title,
                            artist: artist,
                            album: 'Lili Records Radio',
                            artwork: [
                                { src: data.cover || './assets/pasted-20260215-163754-def41f49.png', sizes: '512x512', type: 'image/png' },
                                { src: data.cover || './assets/pasted-20260215-163754-def41f49.png', sizes: '256x256', type: 'image/png' }
                            ]
                        });
                    }
                } else {
                    trackTitle.textContent = "Lili Records Radio - En Vivo";
                }
            } catch (error) {
                console.error('Error fetching metadata:', error);
                if (trackTitle.textContent === "Cargando stream...") {
                    trackTitle.textContent = "Lili Records Radio - En Vivo";
                }
            }
        }

        // Update every 30 seconds
        updateMetadata();
        setInterval(updateMetadata, 30000);

        // Handle possible audio interruptions
        audio.addEventListener('error', function(e) {
            console.error('Audio error:', e);
            trackTitle.textContent = "Error de conexión. Reintentando...";
            setTimeout(() => {
                audio.load();
                if (!audio.paused) audio.play();
            }, 5000);
        });
    </script>
</body>
</html>