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

    <!-- Open Graph meta tags default - will be overridden by page-specific tags -->
    @stack('head')

    <!-- Favicon -->
    <link rel="icon" type="image/png" href="{{ asset('storage/images/logo.png') }}">
    <link rel="apple-touch-icon" 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">
    <style>
        :root {
            --brand-red: #e61e1e;
            --bg-dark: #0f0f0f;
            --bg-secondary: #1e1e1e;
            --border-color: #303030;
            --text-primary: #f1f1f1;
            --text-secondary: #aaaaaa;
        }

        * { box-sizing: border-box; }

        html {
            overflow-x: hidden;
            max-width: 100%;
        }

        body {
            background-color: var(--bg-dark);
            color: var(--text-primary);
            font-family: "Roboto", "Arial", sans-serif;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
            max-width: 100%;
        }

        /* Header */
        .yt-header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 56px;
            background: var(--bg-dark);
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 16px;
            z-index: 1000;
            border-bottom: 1px solid var(--border-color);
        }

        .yt-header-left {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .yt-menu-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: background 0.2s;
            background: transparent;
            border: none;
            color: var(--text-primary);
        }

        .yt-menu-btn:hover { background: var(--border-color); }

        .yt-logo {
            display: flex;
            align-items: center;
            text-decoration: none;
            gap: 4px;
        }

        .yt-logo-text {
            font-size: 1.4rem;
            font-weight: 700;
            color: var(--text-primary);
            letter-spacing: -1px;
        }

/* Search */
        .yt-header-center {
            flex: 1;
            max-width: 640px;
            margin: 0 40px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .yt-search {
            flex: 1;
            display: flex;
            height: 40px;
            align-items: center;
            gap: 8px;
        }

        .yt-search-form {
            flex: 1;
            display: flex;
            height: 40px;
        }

        .yt-search-input {
            flex: 1;
            background: #121212;
            border: 1px solid var(--border-color);
            border-right: none;
            border-radius: 20px 0 0 20px;
            padding: 0 16px;
            color: var(--text-primary);
            font-size: 16px;
        }

        .yt-search-input:focus {
            outline: none;
            border-color: #1c62b9;
        }

        .yt-search-btn {
            width: 64px;
            background: #222;
            border: 1px solid var(--border-color);
            border-radius: 0 20px 20px 0;
            color: var(--text-primary);
            cursor: pointer;
        }

        .yt-search-btn:hover { background: #303030; }

        .yt-search-mic {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #3f3f3f;
            border: none;
            color: var(--text-primary);
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.1rem;
            flex-shrink: 0;
            transition: background 0.2s;
        }
        .yt-search-mic:hover { background: #555; }

        /* Header Right */
        .yt-header-right {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .yt-icon-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            background: transparent;
            border: none;
            color: var(--text-primary);
            font-size: 1.2rem;
        }

        .yt-icon-btn:hover { background: var(--border-color); }

        .yt-user-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: #555;
            cursor: pointer;
        }

        /* Sidebar */
        .yt-sidebar {
            position: fixed;
            top: 56px;
            left: 0;
            bottom: 0;
            width: 240px;
            background: var(--bg-dark);
            overflow-y: auto;
            overflow-x: hidden;
            padding: 8px 0;
            transition: transform 0.25s ease, width 0.25s ease;
            z-index: 999;
            scrollbar-width: thin;
            scrollbar-color: var(--border-color) transparent;
        }

        .yt-sidebar-section {
            padding: 0 8px 4px;
            border-bottom: 1px solid var(--border-color);
            margin-bottom: 4px;
        }
        .yt-sidebar-section:last-child { border-bottom: none; }

        .yt-sidebar-link {
            display: flex;
            align-items: center;
            gap: 18px;
            padding: 0 12px;
            height: 40px;
            border-radius: 10px;
            color: var(--text-primary);
            text-decoration: none;
            cursor: pointer;
            transition: background 0.15s, color 0.15s;
            font-size: 14px;
            white-space: nowrap;
            overflow: hidden;
        }

        .yt-sidebar-link i {
            font-size: 18px;
            flex-shrink: 0;
            width: 22px;
            text-align: center;
        }

        .yt-sidebar-link:hover {
            background: var(--bg-secondary);
            color: var(--text-primary);
            text-decoration: none;
        }

        .yt-sidebar-link.active {
            background: rgba(230, 30, 30, 0.12);
            color: var(--brand-red);
            font-weight: 500;
        }

        .yt-sidebar-link.active i { color: var(--brand-red); }

        /* Sidebar section label */
        .yt-sidebar-section-header {
            font-size: 11px;
            font-weight: 600;
            color: var(--text-secondary);
            text-transform: uppercase;
            letter-spacing: 0.06em;
            padding: 12px 12px 4px;
        }

        /* ══════════════════════════════════════════
           GLOBAL BUTTON SYSTEM
           All UI buttons must use one of these classes.
           border-radius: 8px, padding: 8px 14px, flex icon+text.
        ══════════════════════════════════════════ */
        .action-btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 8px 14px;
            border-radius: 8px;
            border: 1px solid var(--border-color);
            background: var(--bg-secondary);
            color: var(--text-primary);
            font-size: 0.82rem;
            font-weight: 500;
            cursor: pointer;
            text-decoration: none;
            white-space: nowrap;
            transition: background .15s, border-color .15s, color .15s;
            font-family: inherit;
            line-height: 1;
        }
        .action-btn:hover {
            background: var(--border-color);
            color: var(--text-primary);
            text-decoration: none;
        }
        .action-btn:focus { outline: none; box-shadow: 0 0 0 2px rgba(255,255,255,.15); }
        .action-btn:disabled, .action-btn[disabled] { opacity: .5; cursor: not-allowed; }

        /* Primary variant — brand red */
        .action-btn-primary, .action-btn.primary {
            background: var(--brand-red);
            border-color: var(--brand-red);
            color: #fff;
        }
        .action-btn-primary:hover, .action-btn.primary:hover {
            background: #cc1a1a;
            border-color: #cc1a1a;
            color: #fff;
        }

        /* Danger variant — destructive actions */
        .action-btn-danger, .action-btn.danger {
            border-color: #c53030;
            color: #fc8181;
            background: transparent;
        }
        .action-btn-danger:hover, .action-btn.danger:hover {
            background: rgba(197,48,48,.15);
            color: #fc8181;
        }

        /* Icon-only variant (no text label) */
        .action-btn.icon-only { padding: 8px; }

        /* Filter chip bar */
        .yt-filter-bar {
            display: flex;
            gap: 12px;
            overflow-x: auto;
            scrollbar-width: none;
            -ms-overflow-style: none;
            padding: 12px 24px;
            margin: -24px -24px 20px;
            background: var(--bg-dark);
            position: sticky;
            top: 56px;
            z-index: 90;
            border-bottom: 1px solid var(--border-color);
        }
        .yt-filter-bar::-webkit-scrollbar { display: none; }

        .yt-chip {
            flex-shrink: 0;
            background: #3f3f3f;
            color: var(--text-primary);
            border: none;
            padding: 6px 12px;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            transition: background 0.15s;
            white-space: nowrap;
        }
        .yt-chip:hover {
            background: #555;
            color: var(--text-primary);
            text-decoration: none;
        }
        .yt-chip.active {
            background: var(--text-primary);
            color: #0f0f0f;
        }

        /* Mobile sidebar overlay */
        .yt-sidebar-overlay {
            position: fixed;
            top: 56px;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 998;
            display: none;
        }

        .yt-sidebar-overlay.show { display: block; }

        /* Impersonation banner */
        .impersonate-bar {
            position: fixed;
            top: 56px;
            left: 0;
            right: 0;
            z-index: 999;
            height: 40px;
            background: #7a4f00;
            border-bottom: 1px solid #c47f00;
            color: #ffd166;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            font-size: 13px;
            font-weight: 500;
            padding: 0 16px;
        }
        .impersonate-bar i { font-size: 15px; }
        .impersonate-exit-btn {
            margin-left: 16px;
            background: rgba(255,255,255,.12);
            border: 1px solid rgba(255,209,102,.5);
            color: #ffd166;
            border-radius: 6px;
            padding: 4px 12px;
            font-size: 12px;
            font-weight: 600;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            transition: background .15s;
        }
        .impersonate-exit-btn:hover { background: rgba(255,255,255,.2); }
        body.has-impersonate-bar .yt-main { margin-top: calc(56px + 40px); }

        /* Main Content */
        .yt-main {
            margin-top: 56px;
            margin-left: 240px;
            padding: 24px;
            min-height: calc(100vh - 56px);
            transition: margin-left 0.3s;
        }

        /* Upload Button */
        .yt-upload-btn {
            background: var(--brand-red);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            text-decoration: none;
        }

        .yt-upload-btn:hover { background: #cc1a1a; }

        /* ── Header user dropdown (custom, no Bootstrap) ───────────────── */
        /* ── Notification bell ── */
        @keyframes bell-ring {
            0%   { transform: rotate(0)        scale(1); }
            8%   { transform: rotate(-22deg)   scale(1.15); }
            20%  { transform: rotate(20deg)    scale(1.15); }
            32%  { transform: rotate(-16deg)   scale(1.1); }
            44%  { transform: rotate(12deg)    scale(1.05); }
            56%  { transform: rotate(-8deg)    scale(1); }
            68%  { transform: rotate(5deg)     scale(1); }
            80%  { transform: rotate(-3deg)    scale(1); }
            100% { transform: rotate(0)        scale(1); }
        }
        @keyframes bell-sway {
            0%, 100% { transform: rotate(0); }
            30%      { transform: rotate(-10deg); }
            70%      { transform: rotate(10deg); }
        }
        #notifBtn {
            transform-origin: center top;
            transition: transform 0.1s;
        }
        #notifBtn.bell-ring {
            animation: bell-ring 0.85s cubic-bezier(.36,.07,.19,.97) both;
        }
        #notifBtn.bell-has-unread:not(.bell-ring) {
            animation: bell-sway 2s ease-in-out infinite;
        }
        .yt-notif-wrap { position: relative; }
        .yt-notif-badge {
            position: absolute; top: 4px; right: 4px;
            min-width: 16px; height: 16px; border-radius: 8px;
            background: var(--brand-red); color: #fff;
            font-size: 10px; font-weight: 700; line-height: 16px;
            text-align: center; padding: 0 3px;
            pointer-events: none;
        }
        .yt-notif-panel {
            position: fixed;
            width: 420px;
            max-height: 600px;
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 14px;
            box-shadow: 0 12px 40px rgba(0,0,0,.6);
            z-index: 10001;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        .yt-notif-header {
            display: flex; align-items: center; justify-content: space-between;
            padding: 16px 18px 14px;
            border-bottom: 1px solid var(--border-color);
            flex-shrink: 0;
        }
        .yt-notif-title { font-size: 16px; font-weight: 700; color: var(--text-primary); }
        .yt-notif-mark-all {
            font-size: 12px; color: var(--brand-red); background: none;
            border: none; cursor: pointer; padding: 0; font-weight: 500;
        }
        .yt-notif-mark-all:hover { text-decoration: underline; }
        .yt-notif-list { overflow-y: auto; flex: 1; min-height: 0; }
        .yt-notif-item {
            display: flex; align-items: flex-start; gap: 12px;
            padding: 12px 16px; cursor: pointer; text-decoration: none;
            border-bottom: 1px solid rgba(255,255,255,.04);
            transition: background .12s;
        }
        .yt-notif-item:hover { background: rgba(255,255,255,.05); }
        .yt-notif-item.unread { background: rgba(230,30,30,.06); }
        .yt-notif-item.unread:hover { background: rgba(230,30,30,.1); }
        .yt-notif-thumb {
            width: 96px; height: 54px; border-radius: 6px; object-fit: cover;
            flex-shrink: 0; background: var(--bg-dark); display: block;
        }
        .yt-notif-thumb-placeholder {
            width: 96px; height: 54px; border-radius: 6px; flex-shrink: 0;
            background: var(--border-color); display: flex; align-items: center;
            justify-content: center; color: var(--text-secondary); font-size: 20px;
        }
        .yt-notif-body { flex: 1; min-width: 0; }
        .yt-notif-text {
            font-size: 13px; color: var(--text-primary); line-height: 1.45;
            display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
            overflow: hidden;
        }
        .yt-notif-text strong { font-weight: 600; }
        .yt-notif-preview { font-style: italic; opacity: .75; }
        .yt-notif-time { font-size: 11px; color: var(--text-secondary); margin-top: 4px; }
        .yt-notif-dot {
            width: 8px; height: 8px; border-radius: 50%;
            background: var(--brand-red); flex-shrink: 0; margin-top: 8px;
        }
        .yt-notif-empty {
            padding: 48px 16px; text-align: center;
            font-size: 13px; color: var(--text-secondary);
        }
        @media (max-width: 480px) {
            .yt-notif-panel { width: calc(100vw - 16px); }
        }

        .yt-user-dropdown { position: relative; }

        .yt-user-panel {
            display: none;
            position: fixed;
            width: 224px;
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,.55);
            z-index: 10001;
            overflow: hidden;
        }
        .yt-user-panel.open { display: block; }

        .yt-panel-info {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 14px 16px;
            border-bottom: 1px solid var(--border-color);
        }
        .yt-panel-info img {
            width: 36px; height: 36px;
            border-radius: 50%; flex-shrink: 0;
            object-fit: cover;
        }
        .yt-panel-name  { font-size: 13px; font-weight: 600; color: var(--text-primary); }
        .yt-panel-email {
            font-size: 11px; color: var(--text-secondary); margin-top: 1px;
            overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 145px;
        }

        .yt-panel-links { padding: 6px 0; }

        .yt-drop-item {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 9px 16px;
            color: var(--text-primary);
            text-decoration: none;
            font-size: 13px;
            cursor: pointer;
            transition: background 0.12s;
            width: 100%;
            background: transparent;
            border: none;
            text-align: left;
            font-family: inherit;
            line-height: 1;
        }
        .yt-drop-item:hover {
            background: var(--border-color);
            color: var(--text-primary);
            text-decoration: none;
        }
        .yt-drop-item i { font-size: 15px; width: 18px; text-align: center; flex-shrink: 0; }
        .yt-drop-item.danger  { color: #fc8181; }
        .yt-drop-item.danger:hover { background: rgba(197,48,48,.15); color: #fc8181; }

        .yt-panel-divider { height: 1px; background: var(--border-color); margin: 4px 0; }

        /* Mobile circle button */
        @media (max-width: 576px) {
            .yt-upload-btn {
                width: 40px;
                height: 40px;
                padding: 0;
                border-radius: 50%;
                justify-content: center;
            }
            .yt-upload-btn span {
                display: none;
            }

            /* Show text on larger mobile */
            @media (min-width: 400px) {
                .yt-upload-btn {
                    width: auto;
                    border-radius: 20px;
                    padding: 8px 16px;
                }
                .yt-upload-btn span {
                    display: inline;
                }
            }
        }

        /* Responsive */
        /* ─── Desktop: mini-guide (collapsed = 72 px stacked icon+label) ─── */
        @media (min-width: 992px) {
            .yt-sidebar.collapsed {
                width: 72px;
                padding: 8px 0;
                overflow: hidden;
            }
            .yt-sidebar.collapsed .yt-sidebar-link {
                flex-direction: column;
                height: auto;
                padding: 10px 4px;
                gap: 4px;
                border-radius: 12px;
                align-items: center;
                text-align: center;
                margin: 0 4px;
            }
            .yt-sidebar.collapsed .yt-sidebar-link i {
                font-size: 20px;
                width: auto;
            }
            .yt-sidebar.collapsed .yt-sidebar-link span {
                font-size: 10px;
                line-height: 1.2;
                white-space: nowrap;
            }
            .yt-sidebar.collapsed .yt-sidebar-section {
                border-bottom: none;
                padding-bottom: 4px;
                margin-bottom: 0;
            }
            .yt-sidebar.collapsed .yt-sidebar-section-header { display: none; }
            .yt-main.collapsed { margin-left: 72px; }
        }

        @media (max-width: 991px) {
            .yt-sidebar {
                transform: translateX(-100%);
            }

            .yt-sidebar.open {
                transform: translateX(0);
            }

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

            .yt-search-mic { display: none; }
        }

        @media (max-width: 768px) {
            .yt-header-center { display: none; }
            .yt-main { padding: 16px; }
            .yt-main.video-view-page { padding: 0 !important; }
            .video-view-page .yt-sidebar-container { padding: 0 16px; }
        }

        /* Mobile Search Toggle Button */
        .yt-mobile-search-toggle {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            background: transparent;
            border: none;
            color: var(--text-primary);
            font-size: 1.2rem;
        }

        .yt-mobile-search-toggle:hover {
            background: var(--border-color);
        }

        /* Mobile Search Overlay */
        .mobile-search-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: var(--bg-dark);
            z-index: 1001;
            padding: 60px 16px 16px;
            justify-content: center;
            align-items: flex-start;
        }

        .mobile-search-overlay.active {
            display: flex;
        }

        .mobile-search-form {
            display: flex;
            width: 100%;
            max-width: 600px;
            height: 44px;
        }

        .mobile-search-input {
            flex: 1;
            background: #121212;
            border: 1px solid var(--border-color);
            border-right: none;
            border-radius: 20px 0 0 20px;
            padding: 0 16px;
            color: var(--text-primary);
            font-size: 16px;
        }

        .mobile-search-input:focus {
            outline: none;
            border-color: #1c62b9;
        }

        .mobile-search-submit {
            width: 50px;
            background: #222;
            border: 1px solid var(--border-color);
            border-radius: 0 20px 20px 0;
            color: var(--text-primary);
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .mobile-search-submit:hover {
            background: #303030;
        }

        /* Dropdown */
        .dropdown-menu-dark {
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
        }

        .dropdown-item {
            color: var(--text-primary);
        }

        .dropdown-item:hover {
            background: var(--border-color);
            color: var(--text-primary);
        }

        /* Modal input focus */
        #deleteVideoInput:focus {
            outline: none;
            border-color: #ef4444 !important;
            box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.2);
        }

        /* ── Playlist controls bar (sidebar, all video types) ── */
        .pl-controls-bar {
            display: flex;
            align-items: center;
            gap: 4px;
            margin-bottom: 10px;
            padding: 6px 8px;
            background: var(--bg-secondary);
            border-radius: 8px;
            border: 1px solid var(--border-color);
            flex-shrink: 0;
        }
        .pl-ctrl-btn {
            display: flex;
            align-items: center;
            gap: 5px;
            background: none;
            border: none;
            color: var(--text-secondary);
            cursor: pointer;
            padding: 5px 8px;
            border-radius: 6px;
            font-size: 15px;
            transition: color .15s, background .15s;
            white-space: nowrap;
        }
        .pl-ctrl-btn:hover:not(:disabled) { color: var(--text-primary); background: rgba(255,255,255,.07); }
        .pl-ctrl-btn:disabled { opacity: .35; cursor: default; }
        .pl-ctrl-btn.pl-ctrl-active { color: var(--brand-red); }
        .pl-ctrl-divider { width: 1px; height: 18px; background: var(--border-color); margin: 0 2px; flex-shrink: 0; }
        .pl-ctrl-autoplay { margin-left: auto; }
        .pl-autoplay-label { font-size: 12px; font-weight: 500; }
    </style>

    @yield('extra_styles')
</head>
<body class="{{ $bodyClass ?? '' }} {{ session('impersonator_id') ? 'has-impersonate-bar' : '' }}">
    <!-- Header -->
    @include('layouts.partials.header')

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

    <!-- Mobile Search Overlay -->
    <div class="mobile-search-overlay" id="mobileSearchOverlay">
        <form action="{{ route('videos.search') }}" method="GET" class="mobile-search-form">
            <input type="text" name="q" class="mobile-search-input" placeholder="Search" value="{{ request('q') }}">
            <button type="submit" class="mobile-search-submit">
                <i class="bi bi-search"></i>
            </button>
        </form>
    </div>

    <!-- Sidebar Overlay (Mobile) -->
    <div class="yt-sidebar-overlay" onclick="toggleSidebar()"></div>

    <!-- Sidebar -->
    @include('layouts.partials.sidebar')

    <!-- Main Content -->
    <main class="yt-main @yield('main_class')" id="main">
        @yield('content')
    </main>

    <!-- Upload Modal -->
    @auth
    @include('layouts.partials.upload-modal')
    @include('layouts.partials.edit-video-modal')
    @endauth

    <!-- Add to Playlist Modal - Available for all users (shows login prompt if not authenticated) -->
    @include('layouts.partials.add-to-playlist-modal')

    <!-- Share Modal - Available on all pages -->
    @include('layouts.partials.share-modal')

    <!-- Delete Video Modal -->
    @auth
    <div class="modal fade" id="deleteVideoModal" tabindex="-1" aria-labelledby="deleteVideoModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content" style="background: #1a1a1a; border: 1px solid #3f3f3f; border-radius: 12px;">
                <div class="modal-header" style="border-bottom: 1px solid #3f3f3f; padding: 20px 24px;">
                    <h5 class="modal-title" id="deleteVideoModalLabel" style="color: #fff; font-weight: 600; display: flex; align-items: center; gap: 10px;">
                        <i class="bi bi-exclamation-triangle-fill" style="color: #ef4444;"></i>
                        Delete Video
                    </h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" style="padding: 24px;">
                    <div style="background: #282828; border-radius: 8px; padding: 16px; margin-bottom: 20px;">
                        <p style="color: #fff; margin: 0; font-size: 14px; line-height: 1.6;">
                            <strong style="color: #ef4444;">Warning:</strong> This action is permanent and cannot be undone.
                            All data associated with this video will be lost, including:
                        </p>
                        <ul style="color: #aaa; margin: 12px 0 0; padding-left: 20px; font-size: 14px; line-height: 1.8;">
                            <li>View count</li>
                            <li>Comments</li>
                            <li>Likes</li>
                            <li>Thumbnail</li>
                            <li>Video file</li>
                        </ul>
                    </div>
                    <div style="margin-bottom: 16px;">
                        <label for="deleteVideoInput" style="color: #aaa; font-size: 14px; margin-bottom: 8px; display: block;">
                            To confirm deletion, type <strong style="color: #fff;">"<span id="deleteVideoName"></span>"</strong> below:
                        </label>
                        <input type="text"
                               id="deleteVideoInput"
                               class="form-control"
                               style="background: #282828; border: 1px solid #3f3f3f; color: #fff; padding: 12px 16px; border-radius: 8px; font-size: 14px;"
                               placeholder="Enter video name">
                    </div>
                    @auth
                    @if(Auth::user()->two_factor_enabled)
                    <div id="deleteOtpWrap" style="margin-top: 4px;">
                        <label for="deleteOtpInput" style="color: #aaa; font-size: 14px; margin-bottom: 8px; display: flex; align-items: center; gap: 7px;">
                            <i class="bi bi-shield-lock-fill" style="color: #a78bfa;"></i>
                            <span>Enter your <strong style="color:#fff;">2FA code</strong> to confirm:</span>
                        </label>
                        <input type="text" id="deleteOtpInput" inputmode="numeric" pattern="[0-9]*"
                               maxlength="6" autocomplete="one-time-code"
                               class="form-input"
                               style="letter-spacing: 0.3em; font-size: 22px; text-align: center;"
                               placeholder="000000">
                    </div>
                    @endif
                    @endauth
                </div>
                <div class="modal-footer" style="border-top: 1px solid #3f3f3f; padding: 16px 24px; gap: 12px;">
                    <button type="button" class="btn" style="background: #3f3f3f; color: #fff; padding: 10px 20px; border-radius: 8px; font-weight: 500; border: none;" data-bs-dismiss="modal">
                        Cancel
                    </button>
                    <button type="button" id="confirmDeleteBtn" class="btn" style="background: #ef4444; color: #fff; padding: 10px 20px; border-radius: 8px; font-weight: 500; border: none; opacity: 0.5; cursor: not-allowed;" disabled onclick="confirmDeleteVideo()">
                        Delete Video
                    </button>
                </div>
            </div>
        </div>
    </div>
    @endauth

    <!-- Toast Container -->
    <div id="toast-container" style="position:fixed;bottom:24px;right:24px;z-index:99999;display:flex;flex-direction:column;gap:10px;pointer-events:none;"></div>

    <!-- Generic Confirm Modal -->
    <div class="modal fade" id="confirmModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-sm">
            <div class="modal-content" style="background:#1a1a1a;border:1px solid #3f3f3f;border-radius:12px;">
                <div class="modal-body" style="padding:24px;">
                    <p id="confirmModalMessage" style="color:#fff;margin:0 0 20px;font-size:15px;line-height:1.5;"></p>
                    <div style="display:flex;gap:10px;justify-content:flex-end;">
                        <button type="button" class="btn" data-bs-dismiss="modal" style="background:#3f3f3f;color:#fff;padding:8px 18px;border-radius:8px;border:none;font-size:14px;">Cancel</button>
                        <button type="button" id="confirmModalOkBtn" class="btn" style="background:#ef4444;color:#fff;padding:8px 18px;border-radius:8px;border:none;font-size:14px;">Confirm</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- YouTube-style Bottom Navigation Bar (Mobile) -->
    <nav class="yt-bottom-nav">
        <a href="{{ route('videos.index') }}" class="yt-bottom-nav-item {{ request()->routeIs('videos.index') ? 'active' : '' }}">
            <i class="bi bi-house-door-fill"></i>
            <span>Home</span>
        </a>
        <a href="{{ route('videos.trending') }}" class="yt-bottom-nav-item {{ request()->routeIs('videos.trending') ? 'active' : '' }}">
            <i class="bi bi-fire"></i>
            <span>Trending</span>
        </a>
        <a href="{{ auth()->check() ? route('videos.create') : route('login') }}" class="yt-bottom-nav-item {{ request()->routeIs('videos.create') ? 'active' : '' }}">
            <i class="bi bi-plus-circle-fill"></i>
            <span>Upload</span>
        </a>
        <a href="{{ route('history') }}" class="yt-bottom-nav-item {{ request()->routeIs('history') ? 'active' : '' }}">
            <i class="bi bi-collection-play-fill"></i>
            <span>History</span>
        </a>
        <a href="{{ auth()->check() ? route('channel', auth()->user()->channel) : route('login') }}" class="yt-bottom-nav-item">
            <i class="bi bi-person-fill"></i>
            <span>Profile</span>
        </a>
    </nav>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Global toast notification — replaces alert()
        function showToast(message, type) {
            type = type || 'info';
            const colors = { success: '#22c55e', error: '#ef4444', warning: '#f59e0b', info: '#3b82f6' };
            const icons  = { success: 'bi-check-circle-fill', error: 'bi-x-circle-fill', warning: 'bi-exclamation-triangle-fill', info: 'bi-info-circle-fill' };
            const color  = colors[type] || colors.info;
            const icon   = icons[type]  || icons.info;
            const container = document.getElementById('toast-container');
            const toast = document.createElement('div');
            toast.style.cssText = 'background:#1a1a1a;border:1px solid #3f3f3f;border-left:4px solid ' + color + ';color:#fff;padding:14px 16px;border-radius:8px;font-size:14px;display:flex;align-items:center;gap:10px;min-width:260px;max-width:380px;box-shadow:0 4px 20px rgba(0,0,0,.6);pointer-events:all;opacity:0;transition:opacity .25s ease;';
            toast.innerHTML = '<i class="bi ' + icon + '" style="color:' + color + ';font-size:16px;flex-shrink:0;"></i><span style="flex:1;">' + message + '</span><button onclick="this.parentElement.remove()" style="background:none;border:none;color:#888;cursor:pointer;padding:0;font-size:18px;line-height:1;">&times;</button>';
            container.appendChild(toast);
            requestAnimationFrame(function() { toast.style.opacity = '1'; });
            setTimeout(function() {
                toast.style.opacity = '0';
                setTimeout(function() { toast.remove(); }, 280);
            }, 4000);
        }

        // Flash session toasts
        @if(session('toast_error'))
            showToast(@json(session('toast_error')), 'error');
        @endif
        @if(session('toast_success'))
            showToast(@json(session('toast_success')), 'success');
        @endif
        @if(session('toast_warning'))
            showToast(@json(session('toast_warning')), 'warning');
        @endif
        @if(session('toast_info'))
            showToast(@json(session('toast_info')), 'info');
        @endif

        // Global confirm modal — replaces confirm()
        function showConfirm(message, onConfirm, confirmLabel) {
            document.getElementById('confirmModalMessage').textContent = message;
            const okBtn = document.getElementById('confirmModalOkBtn');
            okBtn.textContent = confirmLabel || 'Confirm';
            const modalEl = document.getElementById('confirmModal');
            const modal = new bootstrap.Modal(modalEl);
            const handler = function() {
                okBtn.removeEventListener('click', handler);
                modal.hide();
                onConfirm();
            };
            okBtn.addEventListener('click', handler);
            modalEl.addEventListener('hidden.bs.modal', function cleanup() {
                okBtn.removeEventListener('click', handler);
                modalEl.removeEventListener('hidden.bs.modal', cleanup);
            });
            modal.show();
        }

        // Mobile search toggle function
        function toggleMobileSearch() {
            const overlay = document.getElementById('mobileSearchOverlay');
            overlay.classList.toggle('active');

            // Focus input when overlay opens
            if (overlay.classList.contains('active')) {
                setTimeout(function() {
                    document.querySelector('.mobile-search-input').focus();
                }, 100);
            }
        }

        // Close mobile search on escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                const overlay = document.getElementById('mobileSearchOverlay');
                if (overlay.classList.contains('active')) {
                    overlay.classList.remove('active');
                }
            }
        });

        // Sidebar toggle function
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            const overlay = document.querySelector('.yt-sidebar-overlay');
            const main = document.getElementById('main');

            // Check if we're on mobile or desktop
            const isMobile = window.innerWidth < 992;

            if (isMobile) {
                // Mobile behavior - use 'open' class
                sidebar.classList.toggle('open');
                overlay.classList.toggle('show');
            } else {
                // Desktop behavior - use 'collapsed' class
                sidebar.classList.toggle('collapsed');
                main.classList.toggle('collapsed');

                // Save state to localStorage
                const isCollapsed = sidebar.classList.contains('collapsed');
                localStorage.setItem('sidebarCollapsed', isCollapsed);
            }
        }

        // Header user dropdown (custom, replaces Bootstrap)
        function toggleHeaderDropdown() {
            const btn   = document.getElementById('headerUserBtn');
            const panel = document.getElementById('headerUserPanel');
            const open  = panel.classList.contains('open');
            closeHeaderDropdown();
            if (!open) {
                const r = btn.getBoundingClientRect();
                panel.style.top   = (r.bottom + 6) + 'px';
                panel.style.right = Math.max(0, window.innerWidth - r.right) + 'px';
                panel.style.left  = '';
                panel.classList.add('open');
            }
        }
        function closeHeaderDropdown() {
            const panel = document.getElementById('headerUserPanel');
            if (panel) panel.classList.remove('open');
        }
        document.addEventListener('click', function(e) {
            if (!e.target.closest('.yt-user-dropdown')) closeHeaderDropdown();
            if (!e.target.closest('.yt-notif-wrap')) closeNotifPanel();
        });

        /* ── Notification panel ── */
        @auth
        (function () {
            var fetchUrl    = '{{ route("notifications.fetch") }}';
            var readAllUrl  = '{{ route("notifications.read-all") }}';
            var csrf        = '{{ csrf_token() }}';
            var loaded      = false;
            var prevUnread  = 0;
            var panel       = document.getElementById('notifPanel');
            var badge       = document.getElementById('notifBadge');
            var markAllBtn  = document.getElementById('notifMarkAll');
            var list        = document.getElementById('notifList');

            // ── Chime (Web Audio API — no file, no autoplay warm-up needed) ──
            var _actx        = null;
            var _chimeQueued = false;

            function _ensureCtx() {
                if (!_actx) {
                    try { _actx = new (window.AudioContext || window.webkitAudioContext)(); }
                    catch(e) {}
                }
                return _actx;
            }

            function _doChimeNow() {
                var ctx = _actx;
                if (!ctx) return;
                try {
                    var t = ctx.currentTime;
                    [[880,0],[1108,0.13],[1320,0.26]].forEach(function(n) {
                        var osc = ctx.createOscillator();
                        var g   = ctx.createGain();
                        osc.type = 'sine';
                        osc.frequency.value = n[0];
                        g.gain.setValueAtTime(0, t + n[1]);
                        g.gain.linearRampToValueAtTime(0.20, t + n[1] + 0.015);
                        g.gain.exponentialRampToValueAtTime(0.0001, t + n[1] + 0.75);
                        osc.connect(g);
                        g.connect(ctx.destination);
                        osc.start(t + n[1]);
                        osc.stop(t + n[1] + 0.8);
                    });
                } catch(e) {}
            }

            // On every user gesture: resume context and flush any queued chime
            function _onGesture() {
                var ctx = _ensureCtx();
                if (!ctx) return;
                if (ctx.state === 'suspended') {
                    ctx.resume().then(function() {
                        if (_chimeQueued) { _chimeQueued = false; _doChimeNow(); }
                    }).catch(function(){});
                } else if (_chimeQueued) {
                    _chimeQueued = false;
                    _doChimeNow();
                }
            }
            ['click','keydown','touchstart','scroll'].forEach(function(ev) {
                document.addEventListener(ev, _onGesture, { passive: true });
            });

            function playChime() {
                _ensureCtx();
                if (!_actx) return;
                if (_actx.state === 'running') {
                    _doChimeNow();
                } else {
                    // Context suspended — queue it, also try resume immediately
                    _chimeQueued = true;
                    _actx.resume().then(function() {
                        if (_chimeQueued) { _chimeQueued = false; _doChimeNow(); }
                    }).catch(function(){});
                }
            }

            // ── Bell ring animation ────────────────────────────────
            var _bellTimer = null;
            function ringBell() {
                var btn = document.getElementById('notifBtn');
                if (!btn) return;
                btn.classList.remove('bell-ring');
                // Force reflow so removing+adding the class restarts the animation
                void btn.getBoundingClientRect();
                btn.classList.add('bell-ring');
                clearTimeout(_bellTimer);
                _bellTimer = setTimeout(function () { btn.classList.remove('bell-ring'); }, 900);
            }

            function setBellUnread(hasUnread) {
                var btn = document.getElementById('notifBtn');
                if (!btn) return;
                btn.classList.toggle('bell-has-unread', !!hasUnread);
                if (!hasUnread) btn.classList.remove('bell-ring');
            }

            function post(url) {
                return fetch(url, {
                    method: 'POST',
                    headers: { 'X-CSRF-TOKEN': csrf, 'Accept': 'application/json' },
                    credentials: 'same-origin',
                });
            }

            function escHtml(s) {
                return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
            }

            function notifText(d) {
                var actor = '<strong>' + escHtml(d.actor_name || d.uploader_name || '') + '</strong>';
                var title = '"' + escHtml(d.video_title || '') + '"';
                var preview = d.comment_preview ? ' <em class="yt-notif-preview">"' + escHtml(d.comment_preview) + '"</em>' : '';
                switch (d.type) {
                    case 'new_comment': return actor + ' commented on your video ' + title + ':' + preview;
                    case 'new_reply':   return actor + ' replied to your comment:' + preview;
                    case 'comment_like':return actor + ' liked your comment on ' + title;
                    default:            return actor + ' uploaded a new video: ' + title;
                }
            }

            function renderNotifications(data) {
                var unread = data.unread_count;
                if (unread > 0) {
                    badge.textContent = unread > 99 ? '99+' : unread;
                    badge.style.display = '';
                    if (markAllBtn) markAllBtn.style.display = '';
                } else {
                    badge.style.display = 'none';
                    if (markAllBtn) markAllBtn.style.display = 'none';
                }

                if (!data.notifications || data.notifications.length === 0) {
                    list.innerHTML = '<div class="yt-notif-empty">You\'re all caught up!</div>';
                    return;
                }

                list.innerHTML = data.notifications.map(function (n) {
                    var d = n.data;
                    var thumb = d.video_thumbnail
                        ? '<img class="yt-notif-thumb" src="/storage/thumbnails/' + escHtml(d.video_thumbnail) + '" alt="" loading="lazy" onerror="notifThumbFallback(this)">'
                        : '<div class="yt-notif-thumb-placeholder"><i class="bi bi-play-circle"></i></div>';
                    var dot = !n.read ? '<div class="yt-notif-dot"></div>' : '';
                    return '<a class="yt-notif-item' + (!n.read ? ' unread' : '') + '" '
                         + 'href="/videos/' + escHtml(d.video_route_key) + '" '
                         + 'data-notif-id="' + escHtml(n.id) + '" '
                         + 'onclick="handleNotifClick(event, this)">'
                         + thumb
                         + '<div class="yt-notif-body">'
                         + '<div class="yt-notif-text">' + notifText(d) + '</div>'
                         + '<div class="yt-notif-time">' + escHtml(n.time) + '</div>'
                         + '</div>'
                         + dot
                         + '</a>';
                }).join('');
            }

            window.notifThumbFallback = function (img) {
                var ph = document.createElement('div');
                ph.className = 'yt-notif-thumb-placeholder';
                ph.innerHTML = '<i class="bi bi-play-circle"></i>';
                img.parentNode.replaceChild(ph, img);
            };

            function loadNotifications() {
                fetch(fetchUrl, { credentials: 'same-origin', headers: { 'Accept': 'application/json' } })
                    .then(function (r) { return r.json(); })
                    .then(function (data) { loaded = true; renderNotifications(data); })
                    .catch(function () { list.innerHTML = '<div class="yt-notif-empty">Could not load notifications.</div>'; });
            }

            window.toggleNotifPanel = function () {
                var btn = document.getElementById('notifBtn');
                if (panel.style.display === 'none' || !panel.style.display) {
                    closeHeaderDropdown();
                    var r = btn.getBoundingClientRect();
                    var maxH = Math.min(600, window.innerHeight - r.bottom - 16);
                    panel.style.top       = (r.bottom + 6) + 'px';
                    panel.style.right     = Math.max(8, window.innerWidth - r.right) + 'px';
                    panel.style.left      = '';
                    panel.style.maxHeight = maxH + 'px';
                    panel.style.display   = 'flex';
                    btn.setAttribute('aria-expanded', 'true');
                    if (!loaded) loadNotifications();
                } else {
                    closeNotifPanel();
                }
            };

            window.closeNotifPanel = function () {
                if (panel) { panel.style.display = 'none'; }
                var btn = document.getElementById('notifBtn');
                if (btn) btn.setAttribute('aria-expanded', 'false');
            };

            window.handleNotifClick = function (e, el) {
                var id = el.dataset.notifId;
                el.classList.remove('unread');
                el.querySelector('.yt-notif-dot')?.remove();
                post('{{ url("/notifications") }}/' + id + '/read')
                    .then(function () {
                        var remaining = list.querySelectorAll('.yt-notif-item.unread').length;
                        if (remaining === 0) {
                            badge.style.display = 'none';
                            if (markAllBtn) markAllBtn.style.display = 'none';
                            setBellUnread(false);
                        } else {
                            badge.textContent = remaining > 99 ? '99+' : remaining;
                        }
                        prevUnread = remaining;
                    });
                closeNotifPanel();
            };

            window.markAllRead = function () {
                list.querySelectorAll('.yt-notif-item.unread').forEach(function (el) {
                    el.classList.remove('unread');
                    el.querySelector('.yt-notif-dot')?.remove();
                });
                badge.style.display = 'none';
                if (markAllBtn) markAllBtn.style.display = 'none';
                prevUnread = 0;
                setBellUnread(false);
                post(readAllUrl);
            };

            // ── Poll every 30 s for new notifications ──────────────
            function poll() {
                fetch(fetchUrl, { credentials: 'same-origin', headers: { 'Accept': 'application/json' } })
                    .then(function (r) { return r.json(); })
                    .then(function (data) {
                        var newCount = data.unread_count || 0;
                        if (newCount > prevUnread) {
                            playChime();
                            ringBell();
                            badge.textContent = newCount > 99 ? '99+' : newCount;
                            badge.style.display = '';
                            if (markAllBtn) markAllBtn.style.display = '';
                            renderNotifications(data);
                            loaded = true;
                            setBellUnread(true);
                        }
                        prevUnread = newCount;
                    })
                    .catch(function () {});
            }
            setInterval(poll, 30000);

            // ── Initial badge load ─────────────────────────────────
            fetch(fetchUrl, { credentials: 'same-origin', headers: { 'Accept': 'application/json' } })
                .then(function (r) { return r.json(); })
                .then(function (data) {
                    prevUnread = data.unread_count || 0;
                    if (prevUnread > 0) {
                        badge.textContent = prevUnread > 99 ? '99+' : prevUnread;
                        badge.style.display = '';
                        if (markAllBtn) markAllBtn.style.display = '';
                        renderNotifications(data);
                        loaded = true;
                        setBellUnread(true);
                        ringBell();
                    }
                })
                .catch(function () {});
        })();
        @endauth

        // Restore sidebar state from localStorage on page load
        function restoreSidebarState() {
            const sidebar = document.getElementById('sidebar');
            const main = document.getElementById('main');
            const isMobile = window.innerWidth < 992;

            if (!isMobile) {
                const savedState = localStorage.getItem('sidebarCollapsed');
                // Default to mini (collapsed) if no preference saved — matches YouTube
                if (savedState !== 'false') {
                    sidebar.classList.add('collapsed');
                    main.classList.add('collapsed');
                }
            }
        }

        // Set active sidebar link based on current route
        document.addEventListener('DOMContentLoaded', function() {
            // Restore sidebar state
            restoreSidebarState();

            const currentPath = window.location.pathname;
            const sidebarLinks = document.querySelectorAll('.yt-sidebar-link');

            sidebarLinks.forEach(link => {
                const href = link.getAttribute('href');
                if (href === currentPath || (currentPath.startsWith(href) && href !== '/')) {
                    link.classList.add('active');
                } else if (href === '/' && currentPath === '/') {
                    link.classList.add('active');
                }
            });
        });

        // Handle window resize to reset state for mobile
        window.addEventListener('resize', function() {
            const sidebar = document.getElementById('sidebar');
            const main = document.getElementById('main');
            const isMobile = window.innerWidth < 992;

            if (isMobile) {
                // On mobile, remove collapsed state
                sidebar.classList.remove('collapsed');
                main.classList.remove('collapsed');
            } else {
                // On desktop, restore from localStorage
                restoreSidebarState();
            }
        });

        // Delete video modal functions
        let currentDeleteVideoId = null;
        let currentDeleteVideoTitle = '';

        const _2faEnabled = {{ Auth::check() && Auth::user()->two_factor_enabled ? 'true' : 'false' }};

        function showDeleteModal(videoId, videoTitle) {
            currentDeleteVideoId = videoId;
            currentDeleteVideoTitle = videoTitle.replace(/\s+/g, ' ').trim();

            document.getElementById('deleteVideoName').textContent = currentDeleteVideoTitle;
            document.getElementById('deleteVideoInput').value = '';
            const otpInput = document.getElementById('deleteOtpInput');
            if (otpInput) otpInput.value = '';

            const deleteBtn = document.getElementById('confirmDeleteBtn');
            deleteBtn.disabled = true;
            deleteBtn.style.opacity = '0.5';
            deleteBtn.style.cursor = 'not-allowed';

            const dropdown = document.querySelector('.dropdown-menu.show');
            if (dropdown) dropdown.classList.remove('show');

            const modalElement = document.getElementById('deleteVideoModal');
            const modal = new bootstrap.Modal(modalElement);
            modal.show();
        }

        function _updateDeleteBtn() {
            const titleOk = document.getElementById('deleteVideoInput').value.replace(/\s+/g, ' ').trim() === currentDeleteVideoTitle;
            const otpInput = document.getElementById('deleteOtpInput');
            const otpOk = !_2faEnabled || (otpInput && otpInput.value.replace(/\s/g,'').length === 6);
            const deleteBtn = document.getElementById('confirmDeleteBtn');
            const ok = titleOk && otpOk;
            deleteBtn.disabled = !ok;
            deleteBtn.style.opacity = ok ? '1' : '0.5';
            deleteBtn.style.cursor = ok ? 'pointer' : 'not-allowed';
        }

        document.getElementById('deleteVideoInput').addEventListener('input', _updateDeleteBtn);
        const _otpInput = document.getElementById('deleteOtpInput');
        if (_otpInput) _otpInput.addEventListener('input', _updateDeleteBtn);

        function confirmDeleteVideo() {
            if (!currentDeleteVideoId || !currentDeleteVideoTitle) return;

            const inputValue = document.getElementById('deleteVideoInput').value;
            if (inputValue.replace(/\s+/g,' ').trim() !== currentDeleteVideoTitle) {
                showToast('Video name does not match. Please try again.', 'error');
                return;
            }

            const otpInput = document.getElementById('deleteOtpInput');
            const otpCode = otpInput ? otpInput.value.replace(/\s/g, '') : '';
            if (_2faEnabled && otpCode.length !== 6) {
                showToast('Please enter your 6-digit 2FA code.', 'error');
                if (otpInput) otpInput.focus();
                return;
            }

            const headers = {
                'X-CSRF-TOKEN': '{{ csrf_token() }}',
                'Accept': 'application/json',
            };
            if (_2faEnabled) headers['X-2FA-Code'] = otpCode;

            fetch(`/videos/${currentDeleteVideoId}`, { method: 'DELETE', headers })
            .then(response => {
                if (response.status === 200 || response.status === 302 || response.redirected) {
                    const modal = bootstrap.Modal.getInstance(document.getElementById('deleteVideoModal'));
                    if (modal) modal.hide();
                    window.location.href = "{{ route('videos.index') }}";
                } else if (response.status === 403) {
                    showToast('You do not have permission to delete this video.', 'error');
                } else if (response.status === 404) {
                    showToast('Video not found.', 'error');
                } else if (response.status === 422) {
                    response.json().then(data => {
                        showToast(data.message || 'Invalid 2FA code. Please try again.', 'error');
                        if (otpInput) { otpInput.value = ''; otpInput.focus(); }
                        _updateDeleteBtn();
                    });
                } else {
                    response.json().then(data => {
                        showToast(data.message || 'Failed to delete video. Please try again.', 'error');
                    }).catch(() => {
                        showToast('Failed to delete video. Please try again.', 'error');
                    });
                }
            })
            .catch(() => showToast('An error occurred while deleting the video.', 'error'));
        }
    </script>

    @yield('scripts')

    {{-- ═══════════════════════════════════════════════════════════
         ADMIN ERROR CATCHER — only rendered for super_admins
    ═══════════════════════════════════════════════════════════ --}}
    @auth
    @if(Auth::user()->isSuperAdmin())
    <div id="errCatcher" style="position:fixed;bottom:80px;right:16px;z-index:99999;font-family:monospace;">
        <!-- Badge -->
        <button id="errBadge" onclick="toggleErrPanel()"
            style="display:none;background:#e61e1e;color:#fff;border:none;border-radius:50%;width:40px;height:40px;font-size:14px;font-weight:700;cursor:pointer;box-shadow:0 2px 12px rgba(0,0,0,.5);position:relative;">
            <span id="errCount">0</span>
            <span style="position:absolute;top:-2px;right:-2px;width:10px;height:10px;background:#ff0;border-radius:50%;"></span>
        </button>
        <!-- Panel -->
        <div id="errPanel" style="display:none;width:min(520px,90vw);max-height:70vh;overflow-y:auto;background:#111;border:1px solid #e61e1e;border-radius:10px;box-shadow:0 8px 32px rgba(0,0,0,.7);">
            <div style="display:flex;align-items:center;justify-content:space-between;padding:10px 14px;border-bottom:1px solid #333;background:#1a1a1a;border-radius:10px 10px 0 0;">
                <span style="color:#e61e1e;font-weight:700;font-size:13px;"><i class="bi bi-bug-fill"></i> Error Catcher</span>
                <div style="display:flex;gap:8px;">
                    <button onclick="clearErrors()" style="background:#333;border:none;color:#aaa;padding:3px 10px;border-radius:4px;font-size:11px;cursor:pointer;">Clear</button>
                    <button onclick="toggleErrPanel()" style="background:#333;border:none;color:#aaa;padding:3px 10px;border-radius:4px;font-size:11px;cursor:pointer;">Close</button>
                </div>
            </div>
            <div id="errList" style="padding:10px 14px;display:flex;flex-direction:column;gap:10px;"></div>
        </div>
    </div>

    <script>
    (function () {
        let errors = [];

        function renderErrors() {
            const list = document.getElementById('errList');
            const badge = document.getElementById('errBadge');
            const count = document.getElementById('errCount');
            count.textContent = errors.length;
            badge.style.display = errors.length ? 'block' : 'none';
            list.innerHTML = errors.map((e, i) => `
                <div style="background:#1a1a1a;border:1px solid #333;border-radius:6px;padding:10px;font-size:11px;">
                    <div style="display:flex;justify-content:space-between;margin-bottom:6px;">
                        <span style="color:#e61e1e;font-weight:700;">${e.status} ${e.method} ${e.url}</span>
                        <span style="color:#666;">${e.time}</span>
                    </div>
                    <pre style="color:#f8f8f2;white-space:pre-wrap;word-break:break-all;margin:0;max-height:200px;overflow-y:auto;background:#0a0a0a;padding:8px;border-radius:4px;">${escHtml(e.body)}</pre>
                </div>
            `).join('');
        }

        function escHtml(s) {
            return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
        }

        function addError(status, method, url, body) {
            errors.unshift({ status, method, url, body: tryFormat(body), time: new Date().toLocaleTimeString() });
            if (errors.length > 20) errors.pop();
            renderErrors();
            // Auto-open panel on first error
            if (errors.length === 1) document.getElementById('errPanel').style.display = 'block';
        }

        function tryFormat(raw) {
            try { return JSON.stringify(JSON.parse(raw), null, 2); } catch { return raw || '(empty)'; }
        }

        window.toggleErrPanel = function () {
            const p = document.getElementById('errPanel');
            p.style.display = p.style.display === 'none' ? 'block' : 'none';
        };
        window.clearErrors = function () {
            errors = [];
            renderErrors();
            document.getElementById('errPanel').style.display = 'none';
        };

        // ── Intercept XHR ──────────────────────────────────────────
        const OrigXHR = window.XMLHttpRequest;
        function PatchedXHR() {
            const xhr = new OrigXHR();
            let _method, _url;
            const origOpen = xhr.open.bind(xhr);
            xhr.open = function (method, url, ...rest) {
                _method = method; _url = url;
                return origOpen(method, url, ...rest);
            };
            xhr.addEventListener('loadend', function () {
                if (xhr.status !== 0 && (xhr.status < 200 || xhr.status >= 300)) {
                    addError(xhr.status, _method, _url, xhr.responseText);
                }
            });
            return xhr;
        }
        PatchedXHR.prototype = OrigXHR.prototype;
        window.XMLHttpRequest = PatchedXHR;

        // ── Intercept fetch ────────────────────────────────────────
        const origFetch = window.fetch;
        window.fetch = async function (...args) {
            const res = await origFetch(...args);
            if (!res.ok) {
                const url  = typeof args[0] === 'string' ? args[0] : args[0]?.url ?? '?';
                const meth = args[1]?.method ?? 'GET';
                res.clone().text().then(body => addError(res.status, meth, url, body));
            }
            return res;
        };
    })();
    </script>
    @endif
    @endauth

    <!-- ── Persistent Mini-Player ─────────────────────────────────────────
         Shown on non-video pages when the user navigated away while a
         video was playing.  State is stored in sessionStorage under the
         key "ytpMiniState".
    ──────────────────────────────────────────────────────────────────── -->
    <div id="ytpMini" style="display:none;" aria-label="Mini player">
        <div id="ytpMiniVideo">
            <video id="ytpMiniVid" playsinline></video>
        </div>
        <div id="ytpMiniBar">
            <div id="ytpMiniInfo">
                <span id="ytpMiniTitle"></span>
            </div>
            <div id="ytpMiniControls">
                <button id="ytpMiniPlay"  title="Play / Pause"><i class="bi bi-play-fill"></i></button>
                <a      id="ytpMiniExpand" title="Open video"><i class="bi bi-box-arrow-up-right"></i></a>
                <button id="ytpMiniClose" title="Close"><i class="bi bi-x-lg"></i></button>
            </div>
        </div>
    </div>

    <style>
    #ytpMini {
        position: fixed;
        bottom: calc(64px + env(safe-area-inset-bottom, 0px));
        right: 12px;
        width: 280px;
        background: #1a1a1a;
        border: 1px solid #333;
        border-radius: 10px;
        overflow: hidden;
        z-index: 1999;
        box-shadow: 0 4px 24px rgba(0,0,0,.6);
    }
    #ytpMiniVideo {
        position: relative;
        width: 100%;
        aspect-ratio: 16/9;
        background: #000;
    }
    #ytpMiniVid { width:100%; height:100%; object-fit:contain; display:block; }
    #ytpMiniBar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 6px 8px;
        gap: 6px;
        background: #1a1a1a;
    }
    #ytpMiniInfo {
        flex: 1;
        min-width: 0;
    }
    #ytpMiniTitle {
        font-size: 12px;
        color: #eee;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
    }
    #ytpMiniControls {
        display: flex;
        align-items: center;
        gap: 4px;
        flex-shrink: 0;
    }
    #ytpMiniControls button,
    #ytpMiniControls a {
        background: none;
        border: none;
        color: #ccc;
        cursor: pointer;
        padding: 4px 6px;
        font-size: 16px;
        border-radius: 4px;
        text-decoration: none;
        line-height: 1;
    }
    #ytpMiniControls button:hover,
    #ytpMiniControls a:hover { color: #fff; background: rgba(255,255,255,.1); }
    @media (max-width: 480px) {
        #ytpMini { width: calc(100vw - 24px); right: 12px; }
    }
    </style>

    <script>
    (function () {
        var STORAGE_KEY = 'ytpMiniState';

        /* ── Save state when nav bar is tapped while video plays ── */
        document.querySelectorAll('.yt-bottom-nav-item').forEach(function (link) {
            link.addEventListener('click', function () {
                var v = document.getElementById('videoPlayer');
                if (!v || v.paused || !v.currentSrc) return;

                var title = document.title.replace(/\s*\|.*$/, '').trim();
                var videoPageUrl = window.location.href;

                sessionStorage.setItem(STORAGE_KEY, JSON.stringify({
                    src:      v.currentSrc,
                    time:     v.currentTime,
                    title:    title,
                    url:      videoPageUrl,
                    muted:    v.muted,
                    volume:   v.volume
                }));
            });
        });

        /* ── On page load, restore mini-player if state exists ── */
        document.addEventListener('DOMContentLoaded', function () {
            /* If we're on the video page itself, clear the saved state */
            var mainVideo = document.getElementById('videoPlayer');
            if (mainVideo) {
                sessionStorage.removeItem(STORAGE_KEY);
                return;
            }

            var raw = sessionStorage.getItem(STORAGE_KEY);
            if (!raw) return;

            var state;
            try { state = JSON.parse(raw); } catch(e) { return; }
            if (!state || !state.src) return;

            var wrap      = document.getElementById('ytpMini');
            var vid       = document.getElementById('ytpMiniVid');
            var titleEl   = document.getElementById('ytpMiniTitle');
            var playBtn   = document.getElementById('ytpMiniPlay');
            var expandBtn = document.getElementById('ytpMiniExpand');
            var closeBtn  = document.getElementById('ytpMiniClose');
            if (!wrap || !vid) return;

            titleEl.textContent   = state.title || 'Video';
            expandBtn.href        = state.url || '#';
            vid.volume            = state.volume ?? 0.5;
            vid.muted             = state.muted  ?? false;

            /* Load HLS.js if needed, then attach source */
            function startMini(Hls) {
                if (Hls && Hls.isSupported() && /\.m3u8/.test(state.src)) {
                    var hls = new Hls({ startLevel: -1 });
                    hls.loadSource(state.src);
                    hls.attachMedia(vid);
                    hls.on(Hls.Events.MANIFEST_PARSED, function () {
                        vid.currentTime = state.time || 0;
                        vid.play().catch(function () {
                            vid.muted = true;
                            vid.play().catch(function () {});
                        });
                    });
                } else {
                    vid.src = state.src;
                    vid.addEventListener('loadedmetadata', function () {
                        vid.currentTime = state.time || 0;
                    }, { once: true });
                    vid.play().catch(function () {
                        vid.muted = true;
                        vid.play().catch(function () {});
                    });
                }
                wrap.style.display = 'block';
            }

            if (window.Hls) {
                startMini(window.Hls);
            } else {
                var s = document.createElement('script');
                s.src = 'https://cdn.jsdelivr.net/npm/hls.js@1.5/dist/hls.min.js';
                s.onload = function () { startMini(window.Hls); };
                s.onerror = function () { startMini(null); };
                document.head.appendChild(s);
            }

            /* Play / pause toggle */
            vid.addEventListener('play',  function () { playBtn.querySelector('i').className = 'bi bi-pause-fill'; });
            vid.addEventListener('pause', function () { playBtn.querySelector('i').className = 'bi bi-play-fill'; });
            playBtn.addEventListener('click', function () {
                if (vid.paused) { vid.play().catch(function(){}); }
                else            { vid.pause(); }
            });

            /* Expand → navigate to video page */
            expandBtn.addEventListener('click', function (e) {
                e.preventDefault();
                var t = vid.currentTime;
                sessionStorage.setItem(STORAGE_KEY, JSON.stringify({
                    src: state.src, time: t, title: state.title,
                    url: state.url, muted: vid.muted, volume: vid.volume
                }));
                window.location.href = state.url;
            });

            /* Close */
            closeBtn.addEventListener('click', function () {
                vid.pause();
                wrap.style.display = 'none';
                sessionStorage.removeItem(STORAGE_KEY);
            });
        });
    })();
    </script>

</body>
</html>

    <!-- Extra Mobile Responsive Styles -->
    <style>
        /* Touch-friendly improvements */
        @media (max-width: 480px) {
            /* Better touch targets */
            .yt-menu-btn, .yt-icon-btn, .yt-mobile-search-toggle {
                min-width: 44px;
                min-height: 44px;
            }

            /* Reduce padding on main content */
            .yt-main {
                padding: 12px 8px !important;
            }

            /* Smaller video title */
            .yt-video-title, .video-title {
                font-size: 14px !important;
            }

            /* Channel info compact */
            .channel-info {
                gap: 8px !important;
            }
            .channel-avatar {
                width: 32px !important;
                height: 32px !important;
            }
            .channel-name {
                font-size: 14px !important;
            }
            .channel-subs {
                font-size: 12px !important;
            }

            /* Video meta smaller */
            .yt-channel-name, .yt-video-meta {
                font-size: 12px !important;
            }

            /* Action buttons horizontal scroll on mobile */
            .video-actions {
                overflow-x: auto;
                -webkit-overflow-scrolling: touch;
                padding-bottom: 8px;
                width: 100%;
            }

            .yt-action-btn {
                flex-shrink: 0;
                padding: 8px 12px;
                font-size: 13px;
            }

            /* Comment improvements */
            .comment-item {
                flex-direction: column;
            }
            .comment-item > img {
                width: 32px !important;
                height: 32px !important;
            }

            /* Search input mobile */
            .yt-search-input {
                font-size: 14px;
                padding: 0 12px;
            }

            /* Header spacing */
            .yt-header {
                padding: 0 8px;
            }

            /* User dropdown full width on mobile */
            .dropdown-menu {
                width: 100%;
                min-width: 200px;
            }
        }

        /* Very small screens */
        @media (max-width: 360px) {
            .yt-main {
                padding: 8px 4px !important;
            }
            .yt-main.video-view-page {
                padding: 0 !important;
            }

            .yt-header-right .yt-icon-btn:not(:first-child) {
                display: none;
            }
        }

        /* Landscape mobile */
        @media (max-height: 500px) and (orientation: landscape) {
            .yt-sidebar {
                width: 200px;
            }
            .yt-video-grid {
                grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            }
        }

        /* 4-column grid on wide screens (≥1440 px) */
        @media (min-width: 1440px) {
            .yt-video-grid {
                grid-template-columns: repeat(4, 1fr) !important;
            }
        }


        /* Better video player on mobile */
        @media (max-width: 768px) {
            .video-container {
                border-radius: 0 !important;
                margin: 0 !important;
                max-width: 100% !important;
                width: 100% !important;
            }
        }

        /* Sidebar link padding for touch */
        @media (hover: none) {
            .yt-sidebar-link {
                padding: 0 16px;
            }
            .yt-sidebar-link:hover {
                background: transparent;
            }
            .yt-sidebar-link:active {
                background: var(--border-color);
            }
        }

        /* YouTube-style Bottom Navigation Bar */
        .yt-bottom-nav {
            display: none;
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: calc(56px + env(safe-area-inset-bottom, 0px));
            padding-bottom: env(safe-area-inset-bottom, 0px);
            padding-left: 8px;
            padding-right: 8px;
            background: var(--bg-dark);
            border-top: 1px solid var(--border-color);
            z-index: 1000;
            justify-content: space-around;
            align-items: center;
            -webkit-tap-highlight-color: transparent;
            /* JS will manage transform via visualViewport API */
            will-change: transform;
        }

        .yt-bottom-nav-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            flex: 1;
            height: 100%;
            color: var(--text-secondary);
            text-decoration: none;
            font-size: 12px;
            gap: 4px;
            transition: color 0.2s;
            cursor: pointer;
            background: transparent;
            border: none;
            min-width: 56px;
        }

        .yt-bottom-nav-item:hover {
            color: var(--text-primary);
        }

        .yt-bottom-nav-item.active {
            color: var(--text-primary);
        }

        .yt-bottom-nav-item i {
            font-size: 24px;
        }

        .yt-bottom-nav-item span {
            font-size: 10px;
            font-weight: 500;
        }

        /* ── Mobile: native-app scroll model ─────────────────────────────────
           Lock html+body so the WINDOW never scrolls.
           Only .yt-main scrolls, keeping header and bottom nav truly fixed.
        ────────────────────────────────────────────────────────────────── */
        @media (max-width: 768px) {
            html {
                overflow: hidden;
                height: 100%;
                max-width: 100%;
            }

            body {
                overflow: hidden;
                height: 100%;
                max-width: 100%;
                /* Prevents iOS elastic-scroll bounce on body itself */
                position: fixed;
                width: 100%;
            }

            .yt-bottom-nav {
                display: flex;
                /* JS transform no longer needed — window never scrolls */
                transform: none !important;
            }

            .yt-main {
                /* Stretch between header and bottom nav */
                position: fixed !important;
                top: 56px !important;
                left: 0 !important;
                right: 0 !important;
                bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
                margin: 0 !important;
                padding: 16px !important;
                padding-bottom: 16px !important;
                min-height: unset !important;
                overflow-y: auto;
                overflow-x: hidden;
                -webkit-overflow-scrolling: touch;
                overscroll-behavior-y: contain;
            }

            body.has-impersonate-bar .yt-main {
                top: calc(56px + 40px) !important;
            }

            .yt-main.video-view-page {
                padding: 0 !important;
            }

            /* Filter bar: remove sticky so it scrolls with content */
            .yt-filter-bar {
                position: relative !important;
                top: auto !important;
                z-index: auto !important;
                margin: -16px -16px 16px !important;
            }
        }
    </style>
