 <?php include 'sidebar.php'; ?>
<?php
require_once 'auth_check.php';
require_once 'db.php';

// Ensure $conn is active
if (!isset($conn) && isset($GLOBALS['conn'])) {
    $conn = $GLOBALS['conn'];
}
if (!isset($conn) && isset($mysqli)) {
    $conn = $mysqli;
}

$message = ''; 
$error = '';

// Auto-Ensure Required Database Tables and ALL Columns Exist
if ($conn) {
    // 1. Sliders Table Structure (Updated to support media_type and media for both images and videos)
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `sliders` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `image` VARCHAR(255) NOT NULL,
        `media_type` VARCHAR(50) DEFAULT 'image',
        `caption` VARCHAR(255) DEFAULT NULL,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    // Check & Add Missing Columns in `sliders`
    $sliders_cols = [];
    $res = mysqli_query($conn, "SHOW COLUMNS FROM `sliders`");
    if ($res) {
        while ($row = mysqli_fetch_assoc($res)) {
            $sliders_cols[] = $row['Field'];
        }
    }
    if (!in_array('image', $sliders_cols)) {
        mysqli_query($conn, "ALTER TABLE `sliders` ADD `image` VARCHAR(255) NOT NULL AFTER `id`");
    }
    if (!in_array('media_type', $sliders_cols)) {
        mysqli_query($conn, "ALTER TABLE `sliders` ADD `media_type` VARCHAR(50) DEFAULT 'image' AFTER `image`");
    }
    if (!in_array('caption', $sliders_cols)) {
        mysqli_query($conn, "ALTER TABLE `sliders` ADD `caption` VARCHAR(255) DEFAULT NULL AFTER `media_type`");
    }

    // 2. Videos Table Structure
    mysqli_query($conn, "CREATE TABLE IF NOT EXISTS `videos` (
        `id` INT PRIMARY KEY AUTO_INCREMENT,
        `title` VARCHAR(255) DEFAULT NULL,
        `url` VARCHAR(255) NOT NULL,
        `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");

    $videos_cols = [];
    $res_v = mysqli_query($conn, "SHOW COLUMNS FROM `videos`");
    if ($res_v) {
        while ($row = mysqli_fetch_assoc($res_v)) {
            $videos_cols[] = $row['Field'];
        }
    }
    if (!in_array('title', $videos_cols)) {
        mysqli_query($conn, "ALTER TABLE `videos` ADD `title` VARCHAR(255) DEFAULT NULL AFTER `id`");
    }
    if (!in_array('url', $videos_cols)) {
        mysqli_query($conn, "ALTER TABLE `videos` ADD `url` VARCHAR(255) NOT NULL AFTER `title`");
    }
}

// 1. Slider Media (Image or Video Banner) Upload Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['upload_slider'])) {
    $caption = trim($_POST['caption'] ?? '');
    
    if (isset($_FILES['slider_image']) &&$_FILES['slider_image']['error'] === 0) {
        $file_name =$_FILES['slider_image']['name'];
        $file_tmp =$_FILES['slider_image']['tmp_name'];
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        
        // Allowed extensions for images and videos
        $image_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
        $video_extensions = ['mp4', 'webm', 'ogg', 'mov'];$media_type = '';
        if (in_array($file_ext, $image_extensions)) {$media_type = 'image';
        } elseif (in_array($file_ext, $video_extensions)) {$media_type = 'video';
        }
        
        if (!empty($media_type)) {$new_file_name = time() . '_' . basename($file_name);$target_dir = "../images/";
            
            if (!is_dir($target_dir)) {
                mkdir($target_dir, 0777, true);
            }
            
            $target_file = $target_dir .$new_file_name;
            
            if (move_uploaded_file($file_tmp,$target_file)) {
                $stmt = mysqli_prepare($conn, "INSERT INTO sliders (image, media_type, caption) VALUES (?, ?, ?)");
                if ($stmt) {
                    mysqli_stmt_bind_param($stmt, "sss", $new_file_name, $media_type,$caption);
                    if (mysqli_stmt_execute($stmt)) {$message = "Banner media (Image/Video) successfully upload ho gaya!";
                    } else {
                        $error = "Execution failed: " . mysqli_stmt_error($stmt);
                    }
                    mysqli_stmt_close($stmt);
                } else {
                    $error = "Prepare failed: " . mysqli_error($conn);
                }
            } else {
                $error = "File upload karne me problem aayi.";
            }
        } else {
            $error = "Kripya valid Image ya Video file select karein.";
        }
    } else {
        $error = "Kripya koi media file select karein.";
    }
}

// 2. Video Link Add Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_video'])) {
    $video_title = trim($_POST['video_title'] ?? '');
    $video_url = trim($_POST['video_url'] ?? '');

    if (!empty($video_url)) {
        $stmt = mysqli_prepare($conn, "INSERT INTO videos (title, url) VALUES (?, ?)");
        if ($stmt) {
            mysqli_stmt_bind_param($stmt, "ss", $video_title,$video_url);
            if (mysqli_stmt_execute($stmt)) {$message = "Video link successfully add ho gaya!";
            } else {
                $error = "Execution failed: " . mysqli_stmt_error($stmt);
            }
            mysqli_stmt_close($stmt);
        } else {
            $error = "Prepare failed: " . mysqli_error($conn);
        }
    } else {
        $error = "Video URL zaroori hai.";
    }
}

// Delete Logic
if (isset($_GET['del_slider'])) {
    $id = intval($_GET['del_slider']);
    mysqli_query($conn, "DELETE FROM sliders WHERE id = $id");
    header("Location: manage-media.php");
    exit();
}

if (isset($_GET['del_video'])) {
    $id = intval($_GET['del_video']);
    mysqli_query($conn, "DELETE FROM videos WHERE id = $id");
    header("Location: manage-media.php");
    exit();
}

// Data Fetching
$sliders_res = mysqli_query($conn, "SELECT * FROM sliders ORDER BY id DESC");
$videos_res = mysqli_query($conn, "SELECT * FROM videos ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Slider & Videos | Wings Admin</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --brand-green: #12521f;
            --brand-saffron: #D4A359;
            --dark-navy: #0F172A;
            --sidebar-width: 260px;
            --bg-light: #F8FAFC;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background-color: var(--bg-light);
            display: flex;
            min-height: 100vh;
        }

        /* SIDEBAR */
        .admin-sidebar {
            width: var(--sidebar-width);
            background-color: var(--dark-navy);
            color: #fff;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            display: flex;
            flex-direction: column;
            z-index: 100;
            box-shadow: 4px 0 15px rgba(0, 0, 0, 0.05);
        }

        .sidebar-brand {
            padding: 24px 20px;
            background: rgba(0, 0, 0, 0.25);
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        }

        .sidebar-brand h2 {
            font-size: 22px;
            color: var(--brand-saffron);
            letter-spacing: 1.5px;
            font-weight: 700;
        }

        .sidebar-brand p {
            font-size: 11px;
            color: #94A3B8;
            margin-top: 2px;
        }

        .sidebar-menu {
            list-style: none;
            padding: 20px 0;
            flex: 1;
            overflow-y: auto;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 13px 24px;
            color: #CBD5E1;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.25s ease;
        }

        .sidebar-menu li a:hover,
        .sidebar-menu li.active a {
            background: rgba(255, 255, 255, 0.08);
            color: #fff;
            border-left: 4px solid var(--brand-saffron);
        }

        .sidebar-menu li a i {
            font-size: 16px;
            width: 20px;
            text-align: center;
        }

        /* MAIN CONTENT */
        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            width: calc(100% - var(--sidebar-width));
        }

        .admin-topbar {
            background: #fff;
            height: 70px;
            padding: 0 35px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .topbar-title {
            font-size: 15px;
            color: var(--dark-navy);
            font-weight: 600;
        }

        .btn-logout {
            padding: 8px 16px;
            background: #FEF2F2;
            color: #EF4444;
            border-radius: 8px;
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: all 0.2s ease;
            border: 1px solid #FCA5A5;
        }

        .btn-logout:hover {
            background: #EF4444;
            color: #ffffff;
        }

        .admin-content {
            padding: 35px;
        }

        .page-title {
            font-size: 22px;
            color: var(--dark-navy);
            margin-bottom: 25px;
            font-weight: 700;
        }

        .media-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 25px;
        }

        .card {
            background: #fff;
            padding: 24px;
            border-radius: 14px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03);
            border: 1px solid #E2E8F0;
        }

        .card-header {
            font-size: 16px;
            font-weight: 600;
            color: var(--dark-navy);
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-bottom: 2px solid #F1F5F9;
            padding-bottom: 12px;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .form-group label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: var(--dark-navy);
            margin-bottom: 8px;
        }

        .form-group input {
            width: 100%;
            padding: 12px 14px;
            border: 1.5px solid #CBD5E1;
            border-radius: 10px;
            font-size: 14px;
            outline: none;
            transition: all 0.25s ease;
        }

        .form-group input:focus {
            border-color: var(--brand-green);
            box-shadow: 0 0 0 3px rgba(18, 82, 31, 0.1);
        }

        .btn-submit {
            background: var(--brand-green);
            color: #fff;
            padding: 12px 18px;
            border: none;
            border-radius: 10px;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
            font-size: 14px;
            transition: background 0.2s ease;
            box-shadow: 0 4px 12px rgba(18, 82, 31, 0.15);
        }

        .btn-submit:hover {
            background: #0d3d17;
        }

        .table-container {
            width: 100%;
            overflow-x: auto;
            margin-top: 15px;
            border-radius: 8px;
            border: 1px solid #E2E8F0;
        }

        .media-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 13.5px;
            text-align: left;
            white-space: nowrap;
        }

        .media-table th, 
        .media-table td {
            padding: 12px 16px;
            border-bottom: 1px solid #E2E8F0;
            color: #334155;
        }

        .media-table th {
            background: #F1F5F9;
            color: var(--dark-navy);
            font-weight: 600;
        }

        .media-table tr:hover {
            background-color: #F8FAFC;
        }

        .btn-del {
            color: #EF4444;
            background: #FEF2F2;
            padding: 6px 12px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
            transition: all 0.2s ease;
        }

        .btn-del:hover {
            background: #EF4444;
            color: #ffffff;
        }

        .alert-success {
            padding: 12px 16px;
            background: #DCFCE7;
            color: #166534;
            border-radius: 8px;
            font-size: 13px;
            margin-bottom: 20px;
            border: 1px solid #BBF7D0;
        }

        .alert-error {
            padding: 12px 16px;
            background: #FEF2F2;
            color: #991B1B;
            border-radius: 8px;
            font-size: 13px;
            margin-bottom: 20px;
            border: 1px solid #FCA5A5;
        }

        @media (max-width: 992px) {
            .media-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 768px) {
            .admin-sidebar {
                width: 70px;
            }
            .sidebar-brand h2,
            .sidebar-brand p,
            .sidebar-menu li a span {
                display: none;
            }
            .admin-main {
                margin-left: 70px;
                width: calc(100% - 70px);
            }
            .sidebar-menu li a {
                justify-content: center;
                padding: 16px;
            }
            .admin-content {
                padding: 20px;
            }
            .admin-topbar {
                padding: 0 20px;
            }
        }


        /* ===== PREMIUM MOBILE RESPONSIVE OVERRIDES ===== */
        html, body { overflow-x: hidden; }
        body { -webkit-text-size-adjust: 100%; }
        .admin-main, .admin-content, .media-grid, .card { min-width: 0; }
        .form-group input, .btn-submit, .btn-del { min-height: 46px; }
        .form-group input[type="file"] { padding: 10px; }
        .table-container { -webkit-overflow-scrolling: touch; scrollbar-width: thin; }
        .media-table { min-width: 520px; }
        .media-table td, .media-table th { vertical-align: middle; }
        .media-table img, .media-table video { max-width: 60px; }
        .sidebar-backdrop {
            display:none; position:fixed; inset:0; background:rgba(15,23,42,.48);
            z-index:98; backdrop-filter:blur(2px);
        }
        .mobile-menu-btn {
            display:none; width:42px; height:42px; border:1px solid #E2E8F0;
            border-radius:10px; background:#fff; color:var(--dark-navy); cursor:pointer;
            align-items:center; justify-content:center; font-size:17px; flex:0 0 auto;
        }
        @media (max-width: 1100px) {
            .admin-sidebar {
                transform: translateX(-105%); width:260px; transition:transform .25s ease;
                box-shadow: 8px 0 28px rgba(15,23,42,.18);
            }
            .admin-sidebar.sidebar-open { transform:translateX(0); }
            .sidebar-brand h2, .sidebar-brand p, .sidebar-menu li a span { display:block; }
            .sidebar-menu li a { justify-content:flex-start; padding:13px 24px; }
            .admin-main { margin-left:0; width:100%; }
            .mobile-menu-btn { display:inline-flex; }
            .sidebar-backdrop.active { display:block; }
            .admin-topbar { padding:0 22px; gap:12px; }
            .topbar-title { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
            .admin-content { padding:26px; }
        }
        @media (max-width: 768px) {
            .admin-topbar { height:62px; padding:0 14px; }
            .topbar-title { font-size:13px; }
            .topbar-title i { margin-right:4px !important; }
            .btn-logout { padding:8px 10px; font-size:11px; gap:4px; }
            .btn-logout i { margin:0; }
            .btn-logout { white-space:nowrap; }
            .admin-content { padding:16px 12px 24px; }
            .page-title { font-size:19px; line-height:1.3; margin-bottom:17px; }
            .media-grid { gap:15px; }
            .card { padding:16px; border-radius:12px; }
            .card-header { font-size:14px; line-height:1.4; margin-bottom:16px; padding-bottom:10px; }
            .form-group { margin-bottom:14px; }
            .form-group label { font-size:12px; margin-bottom:6px; }
            .form-group input { padding:11px 12px; font-size:13px; border-radius:9px; }
            .form-group small { font-size:10.5px !important; line-height:1.45; }
            .btn-submit { font-size:13px; padding:12px 14px; border-radius:9px; }
            .table-container { margin-top:11px; border-radius:8px; }
            .media-table { min-width:500px; font-size:12px; }
            .media-table th, .media-table td { padding:10px 11px; }
            .btn-del { min-height:38px; padding:8px 10px; font-size:11px; display:inline-flex; align-items:center; gap:4px; }
        }
        @media (max-width: 380px) {
            .admin-topbar { padding:0 10px; gap:7px; }
            .mobile-menu-btn { width:38px; height:38px; font-size:15px; }
            .topbar-title { font-size:11px; }
            .btn-logout { padding:7px 8px; font-size:10px; }
            .admin-content { padding:13px 9px 20px; }
            .page-title { font-size:17px; margin-bottom:14px; }
            .card { padding:13px; }
            .card-header { font-size:12.5px; }
            .form-group input { min-height:44px; font-size:12px; }
            .btn-submit { min-height:44px; font-size:12px; }
            .media-table { min-width:470px; font-size:11px; }
            .media-table th, .media-table td { padding:9px 9px; }
        }
        @media (prefers-reduced-motion: reduce) {
            *, *::before, *::after { scroll-behavior:auto !important; transition:none !important; animation:none !important; }
        }

    </style>
</head>
<body>
    <div class="sidebar-backdrop" id="sidebarBackdrop"></div>


    <!-- MAIN CONTENT -->
    <div class="admin-main">
        <header class="admin-topbar">
            <button type="button" class="mobile-menu-btn" id="mobileMenuBtn" aria-label="Open menu" aria-expanded="false"><i class="fas fa-bars"></i></button>
            <div class="topbar-title">
                <i class="fas fa-photo-video" style="color: var(--brand-green); margin-right: 6px;"></i> Slider & Video Management
            </div>
            <a href="logout.php" class="btn-logout"><i class="fas fa-power-off"></i> Logout</a>
        </header>

        <main class="admin-content">
            <h1 class="page-title">Manage Slider Banners & Videos</h1>

            <?php if (!empty($message)): ?>
                <div class="alert-success"><?php echo $message; ?></div>
            <?php endif; ?>
            <?php if (!empty($error)): ?>
                <div class="alert-error"><?php echo $error; ?></div>
            <?php endif; ?>

            <!-- LEFT & RIGHT LAYOUT -->
            <div class="media-grid">
                
                <!-- LEFT COLUMN: SLIDER BANNER (IMAGE/VIDEO) -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-images" style="color: var(--brand-green);"></i>
                        <span>Upload Hero Banner (Image or Video)</span>
                    </div>

                    <form action="" method="POST" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>Select Banner Image or Video File</label>
                            <input type="file" name="slider_image" accept="image/*,video/mp4,video/webm,video/ogg,video/mov" required>
                            <small style="color: #64748B; font-size: 11.5px; display: block; margin-top: 5px;">Supports: JPG, PNG, WEBP, MP4, MOV, etc.</small>
                        </div>
                        <div class="form-group">
                            <label>Caption / Heading</label>
                            <input type="text" name="caption" placeholder="e.g. Experience Luxury Heli Tours">
                        </div>
                        <button type="submit" name="upload_slider" class="btn-submit"><i class="fas fa-upload"></i> Upload Banner Media</button>
                    </form>

                    <h4 style="margin-top: 30px; font-size: 15px; color: var(--dark-navy); margin-bottom: 12px; font-weight: 600;">Uploaded Slider Banners</h4>
                    <div class="table-container">
                        <table class="media-table">
                            <thead>
                                <tr>
                                    <th>Media Preview</th>
                                    <th>Type</th>
                                    <th>Caption</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($sliders_res && mysqli_num_rows($sliders_res) > 0): ?>
                                    <?php while ($slide = mysqli_fetch_assoc($sliders_res)): ?>
                                        <tr>
                                            <td>
                                                <?php if (($slide['media_type'] ?? 'image') === 'video'): ?>
                                                    <video width="60" height="40" style="object-fit:cover; border-radius:4px;" muted>
                                                        <source src="../images/<?php echo htmlspecialchars($slide['image'] ?? ''); ?>" type="video/mp4">
                                                    </video>
                                                <?php else: ?>
                                                    <img src="../images/<?php echo htmlspecialchars($slide['image'] ?? ''); ?>" width="60" height="40" style="object-fit:cover; border-radius:4px;">
                                                <?php endif; ?>
                                            </td>
                                            <td><span style="text-transform: uppercase; font-size: 10px; font-weight: bold; background: #E2E8F0; padding: 3px 8px; border-radius: 4px; color: var(--dark-navy);"><?php echo htmlspecialchars($slide['media_type'] ?? 'image'); ?></span></td>
                                            <td><?php echo htmlspecialchars($slide['caption'] ?? 'N/A'); ?></td>
                                            <td><a href="manage-media.php?del_slider=<?php echo $slide['id']; ?>" class="btn-del" onclick="return confirm('Delete media?')"><i class="fas fa-trash"></i> Delete</a></td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr><td colspan="4" style="text-align:center; color:#94A3B8; padding: 20px;">No slider banners found.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- RIGHT COLUMN: MANAGE YOUTUBE / EXTERNAL VIDEOS -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-video" style="color: var(--brand-saffron);"></i>
                        <span>Manage External Videos (Right)</span>
                    </div>

                    <form action="" method="POST">
                        <div class="form-group">
                            <label>Video Title</label>
                            <input type="text" name="video_title" placeholder="e.g. Kedarnath Helicopter Yatra Promo">
                        </div>
                        <div class="form-group">
                            <label>YouTube / Video Link</label>
                            <input type="url" name="video_url" placeholder="https://www.youtube.com/watch?v=..." required>
                        </div>
                        <button type="submit" name="add_video" class="btn-submit" style="background: var(--dark-navy); box-shadow: 0 4px 12px rgba(15, 23, 42, 0.2);"><i class="fas fa-plus"></i> Add Video Link</button>
                    </form>

                    <h4 style="margin-top: 30px; font-size: 15px; color: var(--dark-navy); margin-bottom: 12px; font-weight: 600;">Saved Videos</h4>
                    <div class="table-container">
                        <table class="media-table">
                            <thead>
                                <tr>
                                    <th>Title</th>
                                    <th>URL</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($videos_res && mysqli_num_rows($videos_res) > 0): ?>
                                    <?php while ($vid = mysqli_fetch_assoc($videos_res)): ?>
                                        <tr>
                                            <td><strong><?php echo htmlspecialchars($vid['title'] ?? 'Video'); ?></strong></td>
                                            <td><a href="<?php echo htmlspecialchars($vid['url']); ?>" target="_blank" style="color:#2563EB; font-weight: 500; text-decoration: none;"><i class="fas fa-external-link-alt"></i> Watch</a></td>
                                            <td><a href="manage-media.php?del_video=<?php echo $vid['id']; ?>" class="btn-del" onclick="return confirm('Delete video?')"><i class="fas fa-trash"></i> Delete</a></td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr><td colspan="3" style="text-align:center; color:#94A3B8; padding: 20px;">No videos added yet.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            </div>
        </main>
    </div>


<script>
(function(){
    const btn = document.getElementById('mobileMenuBtn');
    const backdrop = document.getElementById('sidebarBackdrop');
    const sidebar = document.querySelector('.admin-sidebar');
    if(!btn || !backdrop || !sidebar) return;
    function closeMenu(){ sidebar.classList.remove('sidebar-open'); backdrop.classList.remove('active'); btn.setAttribute('aria-expanded','false'); }
    function toggleMenu(){ const open = sidebar.classList.toggle('sidebar-open'); backdrop.classList.toggle('active', open); btn.setAttribute('aria-expanded', open ? 'true' : 'false'); }
    btn.addEventListener('click', toggleMenu);
    backdrop.addEventListener('click', closeMenu);
    sidebar.querySelectorAll('a').forEach(a => a.addEventListener('click', () => { if(window.innerWidth <= 1100) closeMenu(); }));
    window.addEventListener('resize', () => { if(window.innerWidth > 1100) closeMenu(); });
})();
</script>

</body>
</html>