@extends('admin.layouts.app')
@section('title', 'Dashboard')

@section('styles')
<style>
/* ══════════════════════════════════════════════
   DASHBOARD PREMIUM — EduScan Admin Panel
══════════════════════════════════════════════ */

/* ── Hero de bienvenida ── */
.dash-hero {
    background: linear-gradient(135deg, #0a1628 0%, #0d47a1 40%, #0288d1 70%, #00acc1 100%);
    border-radius: 22px;
    padding: 28px 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 24px;
    box-shadow: 0 12px 40px rgba(13,71,161,.32);
    position: relative;
    overflow: hidden;
}
.dash-hero::before {
    content: '';
    position: absolute;
    top: -60px; right: -60px;
    width: 220px; height: 220px;
    border-radius: 50%;
    background: rgba(255,255,255,.06);
    pointer-events: none;
}
.dash-hero::after {
    content: '';
    position: absolute;
    bottom: -80px; left: 30%;
    width: 300px; height: 300px;
    border-radius: 50%;
    background: rgba(255,255,255,.04);
    pointer-events: none;
}
.hero-greeting {
    font-size: .8rem;
    color: rgba(255,255,255,.55);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 6px;
}
.hero-title {
    font-size: 1.7rem;
    font-weight: 900;
    color: #fff;
    margin: 0 0 6px;
    line-height: 1.2;
}
.hero-subtitle {
    font-size: .85rem;
    color: rgba(255,255,255,.55);
    margin: 0;
}
.hero-actions {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
    position: relative;
    z-index: 1;
}
.hero-btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 11px 22px;
    background: linear-gradient(135deg, #ffe082, #ffca28);
    color: #0d47a1;
    border-radius: 14px;
    font-weight: 800;
    font-size: .88rem;
    text-decoration: none;
    box-shadow: 0 4px 16px rgba(255,202,40,.35);
    transition: all .2s;
}
.hero-btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(255,202,40,.45); }
.hero-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 11px 20px;
    background: rgba(255,255,255,.12);
    border: 1.5px solid rgba(255,255,255,.28);
    color: #fff;
    border-radius: 14px;
    font-weight: 700;
    font-size: .88rem;
    text-decoration: none;
    transition: all .2s;
}
.hero-btn-secondary:hover { background: rgba(255,255,255,.22); }

/* ── Tarjetas de estadísticas ── */
.stats-grid-premium {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.stat-card-premium {
    border-radius: 18px;
    padding: 20px 18px 18px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,.08);
    transition: transform .2s, box-shadow .2s;
    cursor: default;
    text-decoration: none;
}
.stat-card-premium:hover { transform: translateY(-4px); box-shadow: 0 10px 30px rgba(0,0,0,.13); }
.stat-card-premium::after {
    content: '';
    position: absolute;
    bottom: -20px; right: -20px;
    width: 80px; height: 80px;
    border-radius: 50%;
    background: rgba(255,255,255,.1);
    pointer-events: none;
}
.scp-icon {
    width: 44px; height: 44px;
    border-radius: 13px;
    background: rgba(255,255,255,.18);
    display: flex; align-items: center; justify-content: center;
    margin-bottom: 14px;
    box-shadow: 0 2px 8px rgba(0,0,0,.1);
}
.scp-icon i { font-size: 20px; color: #fff; }
.scp-num {
    font-size: 2rem;
    font-weight: 900;
    color: #fff;
    line-height: 1;
    font-family: 'Outfit', 'Inter', sans-serif;
}
.scp-label {
    font-size: .75rem;
    color: rgba(255,255,255,.7);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .5px;
    margin-top: 4px;
}
.scp-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: .73rem;
    color: rgba(255,255,255,.55);
    text-decoration: none;
    margin-top: 10px;
    font-weight: 600;
    transition: color .15s;
}
.scp-link:hover { color: rgba(255,255,255,.9); }

/* Gradientes por módulo */
.sc-blue    { background: linear-gradient(135deg, #1565c0, #0288d1); }
.sc-teal    { background: linear-gradient(135deg, #064e3b, #0d9488); }
.sc-amber   { background: linear-gradient(135deg, #78350f, #d97706); }
.sc-violet  { background: linear-gradient(135deg, #4c1d95, #8b5cf6); }
.sc-cyan    { background: linear-gradient(135deg, #155e75, #06b6d4); }
.sc-rose    { background: linear-gradient(135deg, #881337, #f43f5e); }

/* ── Grid inferior ── */
.dash-bottom {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 20px;
    align-items: start;
}
@media(max-width: 900px) { .dash-bottom { grid-template-columns: 1fr; } }

/* ── Cards premium ── */
.dash-card {
    background: #fff;
    border-radius: 18px;
    box-shadow: 0 2px 16px rgba(0,0,0,.06);
    overflow: hidden;
}
.dash-card-hd {
    background: linear-gradient(135deg, #0d47a1, #0288d1);
    padding: 14px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}
.dash-card-hd-left { display: flex; align-items: center; gap: 10px; }
.dash-card-hd-icon {
    width: 32px; height: 32px;
    border-radius: 9px;
    background: rgba(255,255,255,.15);
    display: flex; align-items: center; justify-content: center;
}
.dash-card-hd-icon i { color: #fff; font-size: 15px; }
.dash-card-hd span { color: #fff; font-weight: 800; font-size: .92rem; }
.dash-card-new-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    background: rgba(255,255,255,.15);
    border: 1px solid rgba(255,255,255,.3);
    color: #fff;
    border-radius: 8px;
    font-size: .78rem;
    font-weight: 700;
    text-decoration: none;
    transition: all .15s;
}
.dash-card-new-btn:hover { background: rgba(255,255,255,.25); }

/* Tabla publicaciones */
.dash-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.dash-table thead th {
    background: #f0f7ff;
    color: #64748b;
    font-size: 10px;
    font-weight: 800;
    padding: 9px 16px;
    text-transform: uppercase;
    letter-spacing: .5px;
    border-bottom: 2px solid #dbeafe;
    white-space: nowrap;
}
.dash-table tbody tr { border-bottom: 1px solid #f8faff; transition: background .15s; }
.dash-table tbody tr:hover { background: #f0f7ff; }
.dash-table td { padding: 11px 16px; vertical-align: middle; }
.tipo-badge {
    padding: 3px 9px;
    border-radius: 50px;
    font-size: 10px;
    font-weight: 800;
    display: inline-flex;
    align-items: center;
    gap: 3px;
}

/* Accesos rápidos */
.quick-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 16px;
}
.quick-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 11px 13px;
    border-radius: 13px;
    text-decoration: none;
    transition: all .2s;
    border: 1.5px solid #e2e8f0;
    background: #f8faff;
    font-size: .82rem;
    font-weight: 700;
    color: #334155;
}
.quick-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0,0,0,.1);
    border-color: transparent;
    background: #fff;
}
.quick-btn-icon {
    width: 36px; height: 36px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    font-size: 17px;
}
.quick-btn:hover .quick-btn-icon { transform: scale(1.1); }
.quick-btn-label { font-size: .78rem; line-height: 1.3; }
.quick-btn-label strong { display: block; font-weight: 800; color: #1e293b; }
.quick-btn-label span { color: #94a3b8; font-size: .72rem; font-weight: 600; }

/* Actividad / mini-estado del sitio */
.site-status-card { padding: 16px; }
.status-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 12px;
    border-radius: 10px;
    margin-bottom: 8px;
    background: #f8faff;
    border: 1px solid #f1f5f9;
    font-size: .82rem;
}
.status-row:last-child { margin-bottom: 0; }
.status-row-left { display: flex; align-items: center; gap: 9px; }
.status-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
}
.dot-green { background: #10b981; box-shadow: 0 0 0 3px #dcfce7; }
.dot-blue  { background: #3b82f6; box-shadow: 0 0 0 3px #dbeafe; }
.dot-amber { background: #f59e0b; box-shadow: 0 0 0 3px #fef3c7; }
.dot-gray  { background: #94a3b8; box-shadow: 0 0 0 3px #f1f5f9; }
.status-num { font-weight: 900; color: #1e293b; font-size: .9rem; }

/* Animación de conteo */
@keyframes countUp { from { opacity:0; transform: translateY(10px); } to { opacity:1; transform: translateY(0); } }
.scp-num { animation: countUp .5s ease forwards; }

/* Fecha y hora */
.hero-clock { font-size: .78rem; color: rgba(255,255,255,.45); margin-top: 3px; }
</style>
@endsection

@section('content')

{{-- ══ HERO DE BIENVENIDA ══ --}}
<div class="dash-hero">
    <div style="position:relative;z-index:1;">
        <div class="hero-greeting">🎛️ Panel de Administración</div>
        <h1 class="hero-title">
            Bienvenido,
            {{ auth()->user()->nombre ?? auth()->user()->name ?? 'Administrador' }} 👋
        </h1>
        <p class="hero-subtitle">Gestiona el contenido de tu sitio desde aquí · Todo en un solo lugar</p>
        <div class="hero-clock" id="heroClock"></div>
    </div>
    <div class="hero-actions">
        <a href="{{ route('home') }}" target="_blank" class="hero-btn-primary">
            <i class="bi bi-eye-fill"></i> Ver Sitio Web
        </a>
        <a href="{{ route('admin.noticias.create') }}" class="hero-btn-secondary">
            <i class="bi bi-plus-lg"></i> Nueva Publicación
        </a>
    </div>
</div>

{{-- ══ TARJETAS DE ESTADÍSTICAS ══ --}}
<div class="stats-grid-premium">

    <a href="{{ route('admin.sliders.index') }}" class="stat-card-premium sc-violet" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-images"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['sliders'] }}">0</div>
        <div class="scp-label">Sliders</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todos</div>
    </a>

    <a href="{{ route('admin.servicios.index') }}" class="stat-card-premium sc-teal" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-grid-fill"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['servicios'] }}">0</div>
        <div class="scp-label">Servicios</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todos</div>
    </a>

    <a href="{{ route('admin.noticias.index') }}" class="stat-card-premium sc-blue" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-newspaper"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['noticias'] }}">0</div>
        <div class="scp-label">Publicaciones</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todas</div>
    </a>

    <a href="{{ route('admin.premios.index') }}" class="stat-card-premium sc-amber" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-trophy-fill"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['premios'] }}">0</div>
        <div class="scp-label">Premios</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todos</div>
    </a>

    <a href="{{ route('admin.testimonios.index') }}" class="stat-card-premium sc-cyan" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-people-fill"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['testimonios'] }}">0</div>
        <div class="scp-label">Testimonios</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todos</div>
    </a>

    <a href="{{ route('admin.ticker.index') }}" class="stat-card-premium sc-rose" style="text-decoration:none;">
        <div class="scp-icon"><i class="bi bi-megaphone-fill"></i></div>
        <div class="scp-num counter" data-target="{{ $stats['tickers'] }}">0</div>
        <div class="scp-label">Ticker Items</div>
        <div class="scp-link"><i class="bi bi-arrow-right"></i> Ver todos</div>
    </a>

</div>

{{-- ══ SECCIÓN INFERIOR ══ --}}
<div class="dash-bottom">

    {{-- Publicaciones recientes --}}
    <div class="dash-card">
        <div class="dash-card-hd">
            <div class="dash-card-hd-left">
                <div class="dash-card-hd-icon"><i class="bi bi-newspaper"></i></div>
                <span>Publicaciones Recientes</span>
            </div>
            <a href="{{ route('admin.noticias.create') }}" class="dash-card-new-btn">
                <i class="bi bi-plus-lg"></i> Nueva
            </a>
        </div>
        <div style="overflow-x:auto;">
            <table class="dash-table">
                <thead>
                    <tr>
                        <th>Título</th>
                        <th>Tipo</th>
                        <th>Fecha</th>
                        <th style="text-align:center;">Estado</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($recientes as $noticia)
                    <tr>
                        <td style="max-width:220px;">
                            <div style="font-weight:700;color:#1e293b;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:220px;">
                                {{ $noticia->titulo }}
                            </div>
                        </td>
                        <td>
                            @php
                                $tipoConfig = [
                                    'noticia'     => ['bg'=>'#dbeafe','color'=>'#1d4ed8','icon'=>'bi-newspaper'],
                                    'comunicado'  => ['bg'=>'#fef3c7','color'=>'#b45309','icon'=>'bi-megaphone-fill'],
                                    'convocatoria'=> ['bg'=>'#dcfce7','color'=>'#15803d','icon'=>'bi-person-plus-fill'],
                                    'logro'       => ['bg'=>'#ede9fe','color'=>'#7c3aed','icon'=>'bi-trophy-fill'],
                                ];
                                $tc = $tipoConfig[$noticia->tipo] ?? ['bg'=>'#f1f5f9','color'=>'#64748b','icon'=>'bi-dot'];
                            @endphp
                            <span class="tipo-badge" style="background:{{ $tc['bg'] }};color:{{ $tc['color'] }};">
                                <i class="bi {{ $tc['icon'] }}"></i>
                                {{ $noticia->tipo_label ?? ucfirst($noticia->tipo) }}
                            </span>
                        </td>
                        <td style="white-space:nowrap;color:#64748b;font-size:.78rem;">
                            <i class="bi bi-calendar3 me-1"></i>{{ $noticia->fecha_publicacion->format('d/m/Y') }}
                        </td>
                        <td style="text-align:center;">
                            @if($noticia->activo)
                                <span style="width:8px;height:8px;border-radius:50%;background:#10b981;display:inline-block;box-shadow:0 0 0 3px #dcfce7;"></span>
                            @else
                                <span style="width:8px;height:8px;border-radius:50%;background:#94a3b8;display:inline-block;box-shadow:0 0 0 3px #f1f5f9;"></span>
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="4" style="padding:48px;text-align:center;">
                            <i class="bi bi-newspaper" style="font-size:40px;color:#bfdbfe;display:block;margin-bottom:8px;"></i>
                            <strong style="color:#334155;display:block;">Sin publicaciones</strong>
                            <a href="{{ route('admin.noticias.create') }}" style="color:#0288d1;font-size:.83rem;font-weight:700;">Crear primera →</a>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div style="padding:12px 16px;border-top:1px solid #f1f5f9;text-align:right;">
            <a href="{{ route('admin.noticias.index') }}" style="font-size:.8rem;color:#0288d1;font-weight:700;text-decoration:none;">
                Ver todas las publicaciones <i class="bi bi-arrow-right"></i>
            </a>
        </div>
    </div>

    {{-- Panel lateral derecho --}}
    <div>

        {{-- Accesos rápidos --}}
        <div class="dash-card" style="margin-bottom:16px;">
            <div class="dash-card-hd">
                <div class="dash-card-hd-left">
                    <div class="dash-card-hd-icon"><i class="bi bi-lightning-fill"></i></div>
                    <span>Accesos Rápidos</span>
                </div>
            </div>
            <div class="quick-grid">
                <a href="{{ route('admin.sliders.create') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#ede9fe;"><i class="bi bi-plus-circle-fill" style="color:#7c3aed;"></i></div>
                    <div class="quick-btn-label"><strong>Slider</strong><span>Nuevo</span></div>
                </a>
                <a href="{{ route('admin.noticias.create') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#dbeafe;"><i class="bi bi-plus-circle-fill" style="color:#1d4ed8;"></i></div>
                    <div class="quick-btn-label"><strong>Noticia</strong><span>Nueva</span></div>
                </a>
                <a href="{{ route('admin.servicios.create') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#dcfce7;"><i class="bi bi-plus-circle-fill" style="color:#0f766e;"></i></div>
                    <div class="quick-btn-label"><strong>Servicio</strong><span>Nuevo</span></div>
                </a>
                <a href="{{ route('admin.premios.create') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#fef3c7;"><i class="bi bi-plus-circle-fill" style="color:#d97706;"></i></div>
                    <div class="quick-btn-label"><strong>Premio</strong><span>Nuevo</span></div>
                </a>
                <a href="{{ route('admin.menu-items.index') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#e0f2fe;"><i class="bi bi-list-ul" style="color:#0369a1;"></i></div>
                    <div class="quick-btn-label"><strong>Menú</strong><span>Gestionar</span></div>
                </a>
                <a href="{{ route('admin.configuracion.index') }}" class="quick-btn">
                    <div class="quick-btn-icon" style="background:#f1f5f9;"><i class="bi bi-gear-fill" style="color:#475569;"></i></div>
                    <div class="quick-btn-label"><strong>Config</strong><span>Ajustes</span></div>
                </a>
            </div>
        </div>

        {{-- Estado rápido del sitio --}}
        <div class="dash-card">
            <div class="dash-card-hd">
                <div class="dash-card-hd-left">
                    <div class="dash-card-hd-icon"><i class="bi bi-activity"></i></div>
                    <span>Estado del Contenido</span>
                </div>
            </div>
            <div class="site-status-card">
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-violet" style="background:#8b5cf6;box-shadow:0 0 0 3px #ede9fe;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Sliders activos</span>
                    </div>
                    <span class="status-num">{{ $stats['sliders'] }}</span>
                </div>
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-teal" style="background:#0d9488;box-shadow:0 0 0 3px #ccfbf1;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Servicios</span>
                    </div>
                    <span class="status-num">{{ $stats['servicios'] }}</span>
                </div>
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-blue" style="background:#3b82f6;box-shadow:0 0 0 3px #dbeafe;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Publicaciones</span>
                    </div>
                    <span class="status-num">{{ $stats['noticias'] }}</span>
                </div>
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-amber" style="background:#f59e0b;box-shadow:0 0 0 3px #fef3c7;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Premios</span>
                    </div>
                    <span class="status-num">{{ $stats['premios'] }}</span>
                </div>
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-cyan" style="background:#06b6d4;box-shadow:0 0 0 3px #cffafe;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Testimonios</span>
                    </div>
                    <span class="status-num">{{ $stats['testimonios'] }}</span>
                </div>
                <div class="status-row">
                    <div class="status-row-left">
                        <div class="status-dot dot-rose" style="background:#f43f5e;box-shadow:0 0 0 3px #ffe4e6;"></div>
                        <span style="font-size:.82rem;font-weight:600;color:#334155;">Ticker Items</span>
                    </div>
                    <span class="status-num">{{ $stats['tickers'] }}</span>
                </div>
                <div style="margin-top:14px;padding:10px 12px;background:linear-gradient(135deg,#f0f9ff,#e0f2fe);border-radius:10px;border:1px solid #bae6fd;display:flex;align-items:center;gap:8px;">
                    <i class="bi bi-check-circle-fill" style="color:#0288d1;font-size:16px;"></i>
                    <span style="font-size:.78rem;color:#0369a1;font-weight:700;">Sitio web funcionando correctamente</span>
                </div>
            </div>
        </div>

    </div>
</div>

@endsection

@section('scripts')
<script>
// ── Reloj en tiempo real ──────────────────────────────────────
(function() {
    var el = document.getElementById('heroClock');
    if (!el) return;
    function tick() {
        var now = new Date();
        var dias = ['Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado'];
        var meses = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
        var h = now.getHours().toString().padStart(2,'0');
        var m = now.getMinutes().toString().padStart(2,'0');
        var s = now.getSeconds().toString().padStart(2,'0');
        el.textContent = dias[now.getDay()] + ', ' + now.getDate() + ' de ' + meses[now.getMonth()] + ' ' + now.getFullYear() + '  ·  ' + h + ':' + m + ':' + s;
    }
    tick();
    setInterval(tick, 1000);
})();

// ── Animación de contadores ───────────────────────────────────
(function() {
    var counters = document.querySelectorAll('.counter');
    var duration = 800;
    counters.forEach(function(el) {
        var target = parseInt(el.getAttribute('data-target'), 10) || 0;
        if (target === 0) { el.textContent = '0'; return; }
        var start = 0;
        var startTime = null;
        function step(timestamp) {
            if (!startTime) startTime = timestamp;
            var progress = Math.min((timestamp - startTime) / duration, 1);
            // easeOutExpo
            var eased = progress === 1 ? 1 : 1 - Math.pow(2, -10 * progress);
            el.textContent = Math.floor(eased * target);
            if (progress < 1) requestAnimationFrame(step);
            else el.textContent = target;
        }
        requestAnimationFrame(step);
    });
})();
</script>
@endsection
