<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Admin') — {{ config('app.name') }}</title>

    <link rel="icon" type="image/png" href="{{ asset('storage/images/logo.png') }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="{{ asset('vendor/flag-icons/css/flag-icons.min.css') }}">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>

    <style>
        /* ── Override Bootstrap defaults ───────────────────────────────── */
        body { background-color: var(--bg) !important; color: var(--text) !important; }
        a { color: inherit; }

        /* ── Reset & Tokens ─────────────────────────────────────────────── */
        *, *::before, *::after { box-sizing: border-box; }

        :root {
            --brand:          #e61e1e;
            --brand-dim:      rgba(230,30,30,.12);
            --bg:             #0a0a0a;
            --bg-card:        #141414;
            --bg-card2:       #1a1a1a;
            --border:         #252525;
            --border-light:   #2e2e2e;
            --text:           #f0f0f0;
            --text-2:         #888;
            --text-3:         #555;
            --sidebar-w:      240px;
            --header-h:       56px;
            --radius:         10px;
            --shadow:         0 4px 24px rgba(0,0,0,.4);

            /* Aliases — dashboard.blade.php uses these names */
            --bg-secondary:   #1e1e1e;
            --border-color:   #252525;
            --text-primary:   #f0f0f0;
            --text-secondary: #888;
            --brand-red:      #e61e1e;
        }

        body {
            background: var(--bg);
            color: var(--text);
            font-family: "Inter", "Roboto", sans-serif;
            font-size: 14px;
            line-height: 1.5;
            overflow-x: hidden;
        }

        a { color: inherit; text-decoration: none; }
        button { font-family: inherit; cursor: pointer; }

        /* ── Top Header ─────────────────────────────────────────────────── */
        .adm-header {
            position: fixed; top: 0; left: 0; right: 0;
            height: var(--header-h);
            background: var(--bg-card);
            border-bottom: 1px solid var(--border);
            display: flex; align-items: center;
            padding: 0 16px 0 0;
            z-index: 900;
            gap: 0;
        }

        /* Sidebar toggle + logo area matches sidebar width */
        .adm-header-brand {
            width: var(--sidebar-w);
            flex-shrink: 0;
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 0 16px;
            border-right: 1px solid var(--border);
            height: 100%;
        }

        .adm-toggle {
            width: 34px; height: 34px;
            display: flex; align-items: center; justify-content: center;
            background: transparent; border: none;
            border-radius: 8px;
            color: var(--text-2); font-size: 18px;
            transition: background .15s, color .15s;
            flex-shrink: 0;
        }
        .adm-toggle:hover { background: var(--border-light); color: var(--text); }

        .adm-logo img { height: 26px; display: block; }

        .adm-header-center { flex: 1; padding-left: 20px; }
        .adm-page-label {
            font-size: 13px; color: var(--text-2);
            display: flex; align-items: center; gap: 6px;
        }
        .adm-page-label i { font-size: 12px; }

        /* Right section */
        .adm-header-right {
            display: flex; align-items: center; gap: 4px;
        }

        .adm-icon-btn {
            width: 36px; height: 36px;
            display: flex; align-items: center; justify-content: center;
            border: none; background: transparent; border-radius: 8px;
            color: var(--text-2); font-size: 16px;
            transition: background .15s, color .15s;
            position: relative;
        }
        .adm-icon-btn:hover { background: var(--border-light); color: var(--text); }

        /* User menu trigger */
        .adm-user-btn {
            display: flex; align-items: center; gap: 8px;
            padding: 4px 10px 4px 4px;
            border: 1px solid transparent; border-radius: 10px;
            background: transparent;
            transition: background .15s, border-color .15s;
            margin-left: 4px;
        }
        .adm-user-btn:hover { background: var(--border-light); border-color: var(--border); }
        .adm-user-btn.open  { background: var(--border-light); border-color: var(--border); }

        .adm-user-avatar {
            width: 30px; height: 30px;
            border-radius: 50%;
            object-fit: cover;
            display: block;
            border: 2px solid var(--border);
        }
        .adm-user-info { line-height: 1.2; text-align: left; }
        .adm-user-name { font-size: 13px; font-weight: 600; color: var(--text); }
        .adm-user-role { font-size: 11px; color: var(--text-2); }
        .adm-user-caret { font-size: 10px; color: var(--text-2); transition: transform .2s; flex-shrink: 0; }
        .adm-user-btn.open .adm-user-caret { transform: rotate(180deg); }

        /* User dropdown panel */
        .adm-user-panel {
            position: fixed;
            z-index: 9999;
            background: var(--bg-card2);
            border: 1px solid var(--border-light);
            border-radius: var(--radius);
            box-shadow: 0 16px 48px rgba(0,0,0,.6);
            min-width: 200px;
            padding: 6px 0;
            display: none;
        }
        .adm-user-panel.open { display: block; }

        .adm-drop-header {
            padding: 10px 14px 8px;
            border-bottom: 1px solid var(--border);
            margin-bottom: 4px;
        }
        .adm-drop-header-name { font-size: 13px; font-weight: 600; }
        .adm-drop-header-email { font-size: 11px; color: var(--text-2); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }

        .adm-drop-item {
            display: flex; align-items: center; gap: 9px;
            padding: 8px 14px;
            font-size: 13px; color: var(--text);
            transition: background .1s;
            border: none; background: transparent; width: 100%; text-align: left;
        }
        .adm-drop-item:hover { background: rgba(255,255,255,.05); }
        .adm-drop-item i { font-size: 14px; color: var(--text-2); width: 16px; text-align: center; }
        .adm-drop-item.danger { color: #f87171; }
        .adm-drop-item.danger i { color: #f87171; }
        .adm-drop-item.danger:hover { background: rgba(248,113,113,.08); }

        .adm-drop-divider { height: 1px; background: var(--border); margin: 4px 0; }

        /* ── Sidebar ────────────────────────────────────────────────────── */
        .adm-sidebar {
            position: fixed;
            top: var(--header-h); left: 0; bottom: 0;
            width: var(--sidebar-w);
            background: var(--bg-card);
            border-right: 1px solid var(--border);
            display: flex; flex-direction: column;
            z-index: 800;
            transition: transform .25s ease;
            overflow: hidden;
        }

        .adm-nav { flex: 1; overflow-y: auto; padding: 12px 0; }
        .adm-nav::-webkit-scrollbar { width: 3px; }
        .adm-nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

        .adm-nav-section {
            padding: 8px 16px 4px;
            font-size: 10px; font-weight: 700;
            letter-spacing: 1px; text-transform: uppercase;
            color: var(--text-3);
        }

        .adm-nav-link {
            display: flex; align-items: center; gap: 10px;
            padding: 9px 16px;
            margin: 1px 8px;
            border-radius: 8px;
            color: var(--text-2);
            font-size: 13px; font-weight: 500;
            transition: background .15s, color .15s;
            position: relative;
        }
        .adm-nav-link i { font-size: 15px; flex-shrink: 0; width: 18px; text-align: center; }
        .adm-nav-link:hover { background: var(--border-light); color: var(--text); }
        .adm-nav-link.active {
            background: var(--brand-dim);
            color: var(--brand);
        }
        .adm-nav-link.active i { color: var(--brand); }

        .adm-nav-divider { height: 1px; background: var(--border); margin: 10px 16px; }

        /* Sidebar footer */
        .adm-sidebar-footer {
            padding: 12px;
            border-top: 1px solid var(--border);
        }
        .adm-sidebar-user {
            display: flex; align-items: center; gap: 10px;
            padding: 8px;
            border-radius: 8px;
            transition: background .15s;
        }
        .adm-sidebar-user:hover { background: var(--border-light); }
        .adm-sidebar-user img {
            width: 32px; height: 32px;
            border-radius: 50%; object-fit: cover;
            border: 2px solid var(--border);
            flex-shrink: 0;
        }
        .adm-sidebar-user-info { line-height: 1.3; min-width: 0; }
        .adm-sidebar-user-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .adm-sidebar-user-role { font-size: 11px; color: var(--brand); }

        /* Sidebar overlay (mobile) */
        .adm-overlay {
            display: none; position: fixed;
            inset: var(--header-h) 0 0 0;
            background: rgba(0,0,0,.6);
            z-index: 799;
            backdrop-filter: blur(2px);
        }
        .adm-overlay.open { display: block; }

        /* ── Main content area ──────────────────────────────────────────── */
        .adm-main {
            margin-top: var(--header-h);
            margin-left: var(--sidebar-w);
            padding: 28px;
            min-height: calc(100vh - var(--header-h));
            transition: margin-left .25s ease;
        }

        /* ── Page header ────────────────────────────────────────────────── */
        .adm-page-header {
            display: flex; align-items: center; justify-content: space-between;
            margin-bottom: 24px; gap: 12px; flex-wrap: wrap;
        }
        .adm-page-title {
            font-size: 22px; font-weight: 700; letter-spacing: -.3px;
            display: flex; align-items: center; gap: 10px;
        }
        .adm-page-title i { color: var(--brand); font-size: 20px; }

        /* ── Cards ──────────────────────────────────────────────────────── */
        .adm-card {
            background: var(--bg-card);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            margin-bottom: 20px;
        }
        .adm-card-body { padding: 20px; }

        .adm-card-header {
            display: flex; align-items: center; justify-content: space-between;
            padding: 16px 20px;
            border-bottom: 1px solid var(--border);
            gap: 12px;
        }
        .adm-card-title {
            font-size: 14px; font-weight: 600;
            display: flex; align-items: center; gap: 8px;
        }
        .adm-card-title i { color: var(--text-2); }

        /* ── Stat cards ─────────────────────────────────────────────────── */
        .adm-stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
            gap: 16px;
            margin-bottom: 24px;
        }
        .adm-stat {
            background: var(--bg-card);
            border: 1px solid var(--border);
            border-radius: var(--radius);
            padding: 20px;
            display: flex; flex-direction: column; gap: 6px;
        }
        .adm-stat-icon { font-size: 22px; color: var(--brand); }
        .adm-stat-value { font-size: 28px; font-weight: 700; line-height: 1; }
        .adm-stat-label { font-size: 12px; color: var(--text-2); text-transform: uppercase; letter-spacing: .5px; }

        /* ── Filter bar ─────────────────────────────────────────────────── */
        .adm-filter-form {
            display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
        }
        .adm-filter-search {
            position: relative; flex: 1; min-width: 220px;
        }
        .adm-filter-search i {
            position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
            color: var(--text-2); font-size: 13px; pointer-events: none;
        }
        .adm-input, .adm-select {
            height: 36px;
            background: var(--bg);
            border: 1px solid var(--border-light);
            border-radius: 8px;
            color: var(--text);
            font-size: 13px;
            font-family: inherit;
            outline: none;
            transition: border-color .2s;
        }
        .adm-input { padding: 0 12px 0 32px; width: 100%; }
        .adm-input.no-icon { padding-left: 12px; }
        .adm-select { padding: 0 10px; min-width: 130px; cursor: pointer; }
        .adm-input:focus, .adm-select:focus { border-color: var(--brand); }
        .adm-input::placeholder { color: var(--text-3); }

        /* ── Buttons ────────────────────────────────────────────────────── */
        .adm-btn {
            display: inline-flex; align-items: center; gap: 6px;
            height: 36px; padding: 0 14px;
            border-radius: 8px; border: 1px solid var(--border-light);
            background: transparent; color: var(--text-2);
            font-size: 13px; font-weight: 500; font-family: inherit;
            transition: all .15s; white-space: nowrap; cursor: pointer;
            text-decoration: none;
        }
        .adm-btn:hover { background: var(--border-light); color: var(--text); border-color: var(--border-light); }
        .adm-btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
        .adm-btn-primary:hover { background: #c91a1a; border-color: #c91a1a; color: #fff; }
        .adm-btn-danger { color: #f87171; border-color: rgba(248,113,113,.3); }
        .adm-btn-danger:hover { background: rgba(248,113,113,.1); border-color: rgba(248,113,113,.5); color: #fca5a5; }
        .adm-btn-impersonate { color: #fbbf24; border-color: rgba(251,191,36,.3); }
        .adm-btn-impersonate:hover { background: rgba(251,191,36,.1); border-color: rgba(251,191,36,.5); color: #fde68a; }
        .adm-btn-verify { color: #34d399; border-color: rgba(52,211,153,.3); }
        .adm-btn-verify:hover { background: rgba(52,211,153,.1); border-color: rgba(52,211,153,.5); color: #6ee7b7; }

        /* ── Impersonation Banner ───────────────────────────────────── */
        .adm-impersonate-bar {
            position: fixed; top: var(--header-h); left: var(--sidebar-w); right: 0;
            background: #78350f; border-bottom: 2px solid #f59e0b;
            color: #fef3c7; padding: 9px 24px;
            display: flex; align-items: center; gap: 12px; font-size: 13px;
            z-index: 990;
        }
        .adm-impersonate-bar i { font-size: 16px; color: #fbbf24; flex-shrink: 0; }
        .adm-impersonate-bar span { flex: 1; }
        .adm-impersonate-exit {
            display: inline-flex; align-items: center; gap: 6px;
            background: #f59e0b; color: #000; border: none;
            padding: 5px 14px; border-radius: 6px; font-size: 12px; font-weight: 600;
            cursor: pointer; transition: background .15s; white-space: nowrap;
        }
        .adm-impersonate-exit:hover { background: #fbbf24; }
        /* When banner is visible, push main content down an extra 40px */
        body.has-impersonate-bar .adm-main { padding-top: calc(24px + 40px); }
        .adm-btn-sm { height: 30px; padding: 0 10px; font-size: 12px; }
        .adm-btn i { font-size: 13px; }

        /* ── Table ──────────────────────────────────────────────────────── */
        .adm-table-wrap { overflow-x: auto; }
        .adm-table {
            width: 100%; border-collapse: collapse;
            min-width: 560px;
        }
        .adm-table thead th {
            padding: 10px 16px;
            font-size: 11px; font-weight: 600; letter-spacing: .6px;
            text-transform: uppercase; color: var(--text-2);
            border-bottom: 1px solid var(--border);
            white-space: nowrap;
        }
        .adm-table tbody td {
            padding: 13px 16px;
            border-bottom: 1px solid var(--border);
            font-size: 13px; vertical-align: middle;
        }
        .adm-table tbody tr:last-child td { border-bottom: none; }
        .adm-table tbody tr:hover td { background: rgba(255,255,255,.018); }

        /* User cell */
        .adm-user-cell { display: flex; align-items: center; gap: 10px; }
        .adm-user-cell img {
            width: 34px; height: 34px; border-radius: 50%; object-fit: cover;
            border: 2px solid var(--border); flex-shrink: 0;
        }
        .adm-user-cell-name { font-weight: 500; font-size: 13px; }
        .adm-user-cell-you {
            font-size: 10px; font-weight: 600; letter-spacing: .3px;
            background: rgba(99,102,241,.2); color: #a5b4fc;
            padding: 1px 6px; border-radius: 4px; margin-left: 4px;
        }
        .adm-user-cell-email { font-size: 12px; color: var(--text-2); margin-top: 1px; }

        /* Badges */
        .adm-badge {
            display: inline-flex; align-items: center; gap: 4px;
            padding: 3px 9px;
            border-radius: 20px; font-size: 11px; font-weight: 600;
            letter-spacing: .2px; white-space: nowrap;
        }
        .adm-badge-superadmin { background: rgba(230,30,30,.15); color: #fca5a5; border: 1px solid rgba(230,30,30,.25); }
        .adm-badge-admin      { background: rgba(251,146,60,.15); color: #fdba74; border: 1px solid rgba(251,146,60,.25); }
        .adm-badge-user       { background: rgba(148,163,184,.1);  color: #94a3b8; border: 1px solid rgba(148,163,184,.2); }
        .adm-badge-verified   { background: rgba(34,197,94,.12);   color: #86efac; border: 1px solid rgba(34,197,94,.2); }
        .adm-badge-unverified { background: rgba(251,191,36,.12);  color: #fcd34d; border: 1px solid rgba(251,191,36,.2); }
        .adm-badge-ready      { background: rgba(34,197,94,.12);   color: #86efac; border: 1px solid rgba(34,197,94,.2); }
        .adm-badge-public     { background: rgba(34,197,94,.12);   color: #86efac; border: 1px solid rgba(34,197,94,.2); }
        .adm-badge-private    { background: rgba(148,163,184,.1);  color: #94a3b8; border: 1px solid rgba(148,163,184,.2); }
        .adm-badge-unlisted   { background: rgba(251,146,60,.15);  color: #fdba74; border: 1px solid rgba(251,146,60,.25); }

        /* Row actions */
        .adm-row-actions { display: flex; align-items: center; gap: 4px; }

        /* ── Alerts ─────────────────────────────────────────────────────── */
        .adm-alert {
            display: flex; align-items: flex-start; gap: 10px;
            padding: 12px 16px; border-radius: 8px;
            font-size: 13px; margin-bottom: 16px;
        }
        .adm-alert-success { background: rgba(34,197,94,.1); border: 1px solid rgba(34,197,94,.25); color: #86efac; }
        .adm-alert-error   { background: rgba(248,113,113,.1); border: 1px solid rgba(248,113,113,.25); color: #fca5a5; }
        .adm-alert i { flex-shrink: 0; margin-top: 1px; }
        .adm-alert-close { margin-left: auto; background: none; border: none; color: inherit; opacity: .6; cursor: pointer; font-size: 14px; }
        .adm-alert-close:hover { opacity: 1; }

        /* ── Confirm Dialog ─────────────────────────────────────────────── */
        .adm-dialog-overlay {
            display: none; position: fixed; inset: 0; z-index: 9000;
            background: rgba(0,0,0,.7); backdrop-filter: blur(4px);
            align-items: center; justify-content: center; padding: 20px;
        }
        .adm-dialog-overlay.open { display: flex; }
        .adm-dialog {
            background: var(--bg-card2); border: 1px solid var(--border-light);
            border-radius: 14px; width: 100%; max-width: 420px;
            box-shadow: 0 32px 80px rgba(0,0,0,.7);
            animation: dlgIn .18s ease;
        }
        @keyframes dlgIn { from { opacity:0; transform:scale(.96) translateY(-8px); } to { opacity:1; transform:none; } }
        .adm-dialog-header {
            display: flex; align-items: center; justify-content: space-between;
            padding: 18px 20px 0;
        }
        .adm-dialog-title { font-size: 15px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
        .adm-dialog-title i { color: #f87171; }
        .adm-dialog-body { padding: 16px 20px 20px; color: var(--text-2); line-height: 1.7; font-size: 13px; }
        .adm-dialog-body strong { color: var(--text); }
        .adm-dialog-warning {
            margin-top: 12px; padding: 10px 12px;
            background: rgba(251,191,36,.08); border: 1px solid rgba(251,191,36,.2);
            border-radius: 8px; color: #fcd34d; font-size: 12px;
            display: flex; gap: 8px;
        }
        .adm-dialog-footer {
            display: flex; justify-content: flex-end; gap: 8px;
            padding: 0 20px 18px;
        }

        /* ── Misc helpers ───────────────────────────────────────────────── */
        .text-dim { color: var(--text-2); }
        .text-muted-sm { font-size: 12px; color: var(--text-2); }
        .mt-0 { margin-top: 0 !important; }
        .empty-state { text-align: center; padding: 48px 20px; color: var(--text-2); }
        .empty-state i { font-size: 40px; display: block; margin-bottom: 12px; opacity: .3; }
        .empty-state p { font-size: 14px; }

        /* ── Responsive ─────────────────────────────────────────────────── */
        @media (max-width: 991px) {
            .adm-sidebar { transform: translateX(-100%); }
            .adm-sidebar.open { transform: translateX(0); }
            .adm-main { margin-left: 0; padding: 20px 16px; }
            .adm-header-brand { width: auto; border-right: none; }
            .adm-user-info { display: none; }
        }
        @media (max-width: 600px) {
            .adm-page-header { flex-direction: column; align-items: flex-start; }
            .adm-filter-form { gap: 6px; }
            .adm-filter-search { min-width: 100%; }
        }
    </style>

    @yield('extra_styles')
</head>
<body>

{{-- ── Header ──────────────────────────────────────────────────────── --}}
<header class="adm-header">
    <div class="adm-header-brand">
        <button class="adm-toggle" id="sidebarToggle" title="Toggle sidebar">
            <i class="bi bi-layout-sidebar"></i>
        </button>
        <a href="{{ route('admin.dashboard') }}" class="adm-logo">
            <img src="{{ asset('storage/images/fullLogo.png') }}" alt="{{ config('app.name') }}">
        </a>
    </div>

    <div class="adm-header-center">
        <span class="adm-page-label">
            <i class="bi bi-shield-lock-fill" style="color:var(--brand);"></i>
            Admin Panel
        </span>
    </div>

    <div class="adm-header-right">
        <a href="{{ route('videos.index') }}" class="adm-icon-btn" title="View site">
            <i class="bi bi-box-arrow-up-right"></i>
        </a>

        @auth
        <button class="adm-user-btn" id="userMenuBtn" type="button">
            @if(Auth::user()->avatar)
                <img src="{{ route('media.avatar', Auth::user()->avatar) }}" class="adm-user-avatar" alt="">
            @else
                <img src="https://i.pravatar.cc/150?u={{ Auth::user()->id }}" class="adm-user-avatar" alt="">
            @endif
            <div class="adm-user-info">
                <div class="adm-user-name">{{ Str::limit(Auth::user()->name, 18) }}</div>
                <div class="adm-user-role">{{ Auth::user()->role === 'super_admin' ? 'Super Admin' : 'Admin' }}</div>
            </div>
            <i class="bi bi-chevron-down adm-user-caret"></i>
        </button>

        <div class="adm-user-panel" id="userMenuPanel">
            <div class="adm-drop-header">
                <div class="adm-drop-header-name">{{ Auth::user()->name }}</div>
                <div class="adm-drop-header-email">{{ Auth::user()->email }}</div>
            </div>
            <a href="{{ route('profile') }}" class="adm-drop-item"><i class="bi bi-person"></i> My Profile</a>
            <a href="{{ route('channel', Auth::user()->channel) }}" class="adm-drop-item"><i class="bi bi-play-btn"></i> My Channel</a>
            <div class="adm-drop-divider"></div>
            <a href="{{ route('admin.dashboard') }}" class="adm-drop-item"><i class="bi bi-speedometer2"></i> Dashboard</a>
            <a href="{{ route('admin.users') }}" class="adm-drop-item"><i class="bi bi-people"></i> Users</a>
            <a href="{{ route('admin.videos') }}" class="adm-drop-item"><i class="bi bi-play-circle"></i> Videos</a>
            @if(Auth::user()->isSuperAdmin())
            <a href="{{ route('admin.audit') }}" class="adm-drop-item"><i class="bi bi-shield-check"></i> Audit Logs</a>
            <a href="{{ route('admin.logs') }}" class="adm-drop-item"><i class="bi bi-bug"></i> Error Logs</a>
            @endif
            <div class="adm-drop-divider"></div>
            <form method="POST" action="{{ route('logout') }}">
                @csrf
                <button type="submit" class="adm-drop-item danger"><i class="bi bi-box-arrow-right"></i> Sign out</button>
            </form>
        </div>
        @endauth
    </div>
</header>

{{-- ── Sidebar ──────────────────────────────────────────────────────── --}}
<div class="adm-overlay" id="sidebarOverlay"></div>

<aside class="adm-sidebar" id="adminSidebar">
    <nav class="adm-nav">
        <div class="adm-nav-section">Main</div>
        <a href="{{ route('admin.dashboard') }}"
           class="adm-nav-link {{ request()->routeIs('admin.dashboard') ? 'active' : '' }}">
            <i class="bi bi-grid-1x2"></i> Dashboard
        </a>

        <div class="adm-nav-section" style="margin-top:8px;">Content</div>
        <a href="{{ route('admin.users') }}"
           class="adm-nav-link {{ request()->routeIs('admin.users*') ? 'active' : '' }}">
            <i class="bi bi-people"></i> Users
        </a>
        <a href="{{ route('admin.videos') }}"
           class="adm-nav-link {{ request()->routeIs('admin.videos*') ? 'active' : '' }}">
            <i class="bi bi-play-circle"></i> Videos
        </a>

        @if(Auth::user() && Auth::user()->isSuperAdmin())
        <div class="adm-nav-section" style="margin-top:8px;">System</div>
        <a href="{{ route('admin.settings') }}"
           class="adm-nav-link {{ request()->routeIs('admin.settings*') ? 'active' : '' }}">
            <i class="bi bi-stars"></i> AI / LLM
        </a>
        <a href="{{ route('admin.lyrics') }}"
           class="adm-nav-link {{ request()->routeIs('admin.lyrics') ? 'active' : '' }}">
            <i class="bi bi-music-note-list"></i> Lyrics Pipeline
        </a>
        <a href="{{ route('admin.gpu') }}"
           class="adm-nav-link {{ request()->routeIs('admin.gpu') ? 'active' : '' }}">
            <i class="bi bi-gpu-card"></i> GPU Accelerator
        </a>
        <a href="{{ route('admin.nas-storage') }}"
           class="adm-nav-link {{ request()->routeIs('admin.nas-storage') ? 'active' : '' }}">
            <i class="bi bi-hdd-network"></i> NAS Storage
        </a>
        <a href="{{ route('admin.backup') }}"
           class="adm-nav-link {{ request()->routeIs('admin.backup') ? 'active' : '' }}">
            <i class="bi bi-archive"></i> Backup &amp; Restore
        </a>
        <a href="{{ route('admin.audit') }}"
           class="adm-nav-link {{ request()->routeIs('admin.audit') ? 'active' : '' }}">
            <i class="bi bi-shield-check"></i> Audit Logs
        </a>
        <a href="{{ route('admin.logs') }}"
           class="adm-nav-link {{ request()->routeIs('admin.logs') ? 'active' : '' }}">
            <i class="bi bi-bug"></i> Error Logs
        </a>
        @endif

        <div class="adm-nav-divider"></div>
        <a href="{{ route('videos.index') }}" class="adm-nav-link">
            <i class="bi bi-arrow-left"></i> Back to Site
        </a>
    </nav>

    @auth
    <div class="adm-sidebar-footer">
        <a href="{{ route('profile') }}" class="adm-sidebar-user">
            @if(Auth::user()->avatar)
                <img src="{{ route('media.avatar', Auth::user()->avatar) }}" alt="">
            @else
                <img src="https://i.pravatar.cc/150?u={{ Auth::user()->id }}" alt="">
            @endif
            <div class="adm-sidebar-user-info">
                <div class="adm-sidebar-user-name">{{ Str::limit(Auth::user()->name, 20) }}</div>
                <div class="adm-sidebar-user-role">{{ Auth::user()->role === 'super_admin' ? 'Super Admin' : 'Admin' }}</div>
            </div>
        </a>
    </div>
    @endauth
</aside>

{{-- ── Impersonation Banner ────────────────────────────────────────── --}}
@if(session('impersonator_id'))
<div class="adm-impersonate-bar">
    <i class="bi bi-person-fill-gear"></i>
    <span>Impersonating <strong>{{ Auth::user()->name }}</strong> &mdash; any actions you take are performed as this user</span>
    <form method="POST" action="{{ route('impersonate.exit') }}" style="margin:0;">
        @csrf
        <button type="submit" class="adm-impersonate-exit">
            <i class="bi bi-box-arrow-right"></i> Exit Impersonation
        </button>
    </form>
</div>
@endif

{{-- ── Main ─────────────────────────────────────────────────────────── --}}
<main class="adm-main" id="adminMain">
    @yield('content')
</main>

<script>
(function () {
    const toggle   = document.getElementById('sidebarToggle');
    const sidebar  = document.getElementById('adminSidebar');
    const overlay  = document.getElementById('sidebarOverlay');
    const main     = document.getElementById('adminMain');
    const userBtn  = document.getElementById('userMenuBtn');
    const userPanel= document.getElementById('userMenuPanel');

    // ── Sidebar toggle ────────────────────────────────────────────────
    let sidebarOpen = window.innerWidth > 991;

    function setSidebar(open) {
        sidebarOpen = open;
        if (window.innerWidth <= 991) {
            sidebar.classList.toggle('open', open);
            overlay.classList.toggle('open', open);
        } else {
            // Desktop: slide sidebar in/out and shift main content
            sidebar.style.transform = open ? '' : 'translateX(-100%)';
            main.style.marginLeft   = open ? 'var(--sidebar-w)' : '0';
        }
    }

    if (toggle) toggle.addEventListener('click', () => setSidebar(!sidebarOpen));
    if (overlay) overlay.addEventListener('click', () => setSidebar(false));

    window.addEventListener('resize', () => {
        if (window.innerWidth > 991) {
            overlay.classList.remove('open');
            // Restore desktop state
            sidebar.style.transform = sidebarOpen ? '' : 'translateX(-100%)';
            main.style.marginLeft   = sidebarOpen ? 'var(--sidebar-w)' : '0';
        } else {
            sidebar.style.transform = '';
            main.style.marginLeft   = '';
        }
    });

    // ── User dropdown ─────────────────────────────────────────────────
    if (userBtn && userPanel) {
        userBtn.addEventListener('click', function (e) {
            e.stopPropagation();
            const open = userPanel.classList.toggle('open');
            userBtn.classList.toggle('open', open);
            if (open) {
                const r = userBtn.getBoundingClientRect();
                userPanel.style.top   = (r.bottom + 6) + 'px';
                userPanel.style.right = (window.innerWidth - r.right) + 'px';
                userPanel.style.left  = 'auto';
            }
        });

        document.addEventListener('click', function (e) {
            if (!userPanel.contains(e.target) && !userBtn.contains(e.target)) {
                userPanel.classList.remove('open');
                userBtn.classList.remove('open');
            }
        });
    }

    // ── Alert dismiss ─────────────────────────────────────────────────
    document.querySelectorAll('.adm-alert-close').forEach(btn => {
        btn.addEventListener('click', () => btn.closest('.adm-alert').remove());
    });
})();
</script>

@yield('scripts')
</body>
</html>
