<?php
require_once __DIR__ . '/config.php';
requireLogin();
$admin = currentAdmin();
$flash = getFlash();
$currentPage = basename($_SERVER['PHP_SELF'], '.php');

$unreadCount = 0;
try {
    $r = db()->query("SELECT COUNT(*) as c FROM contact_messages WHERE is_read=0");
    $unreadCount = $r ? (int)$r->fetch_assoc()['c'] : 0;
} catch (Exception $e) {}

// Get logo from settings
$siteLogo = '';
try {
    $r = db()->query("SELECT setting_value FROM site_settings WHERE setting_key='logo' LIMIT 1");
    $siteLogo = $r ? ($r->fetch_assoc()['setting_value'] ?? '') : '';
} catch(Exception $e) {}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><?= isset($pageTitle) ? e($pageTitle).' — ' : '' ?>CY Admin Panel</title>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@300;400;500;600;700&family=DM+Serif+Display:ital@0;1&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
:root{
  --brand:#1a56db;--brand2:#1e429f;--accent:#f59e0b;--dark:#111827;
  --sidebar-w:260px;--bg:#f3f4f6;--white:#ffffff;--border:#e5e7eb;
  --text:#374151;--muted:#6b7280;--danger:#ef4444;--success:#10b981;
  --warn:#f59e0b;--radius:12px;
  --shadow:0 1px 3px rgba(0,0,0,.1),0 1px 2px rgba(0,0,0,.06);
  --shadow-lg:0 10px 40px rgba(0,0,0,.12);
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'DM Sans',sans-serif;background:var(--bg);color:var(--text);min-height:100vh;display:flex;}

/* SIDEBAR */
.sidebar{width:var(--sidebar-w);min-height:100vh;background:var(--dark);display:flex;flex-direction:column;position:fixed;top:0;left:0;z-index:100;transition:transform .3s;}
.sidebar-logo{padding:16px 20px;border-bottom:1px solid rgba(255,255,255,.08);display:flex;align-items:center;gap:10px;min-height:64px;}
.logo-img{max-height:38px;max-width:120px;object-fit:contain;}
.logo-icon{width:38px;height:38px;background:var(--brand);border-radius:10px;display:grid;place-items:center;font-size:16px;color:#fff;font-weight:800;font-family:'DM Serif Display',serif;flex-shrink:0;}
.logo-text{color:#fff;font-weight:700;font-size:14px;line-height:1.2;}
.logo-text span{display:block;font-size:10px;font-weight:400;color:rgba(255,255,255,.4);margin-top:1px;}

.sidebar-nav{flex:1;overflow-y:auto;padding:8px 0;scrollbar-width:thin;scrollbar-color:rgba(255,255,255,.1) transparent;}
.nav-section{padding:10px 20px 3px;font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:.1em;color:rgba(255,255,255,.28);}
.nav-item{display:flex;align-items:center;gap:10px;padding:8px 20px;color:rgba(255,255,255,.62);text-decoration:none;font-size:13.5px;font-weight:500;transition:all .15s;position:relative;}
.nav-item:hover{color:#fff;background:rgba(255,255,255,.07);}
.nav-item.active{color:#fff;background:var(--brand);}
.nav-item .nav-icon{width:18px;text-align:center;font-size:13px;flex-shrink:0;}
.nav-badge{margin-left:auto;background:var(--danger);color:#fff;font-size:10px;font-weight:700;padding:2px 7px;border-radius:20px;}

.sidebar-footer{padding:14px 18px;border-top:1px solid rgba(255,255,255,.08);display:flex;align-items:center;gap:10px;}
.sf-avatar{width:32px;height:32px;background:var(--brand);border-radius:50%;display:grid;place-items:center;font-weight:700;color:#fff;font-size:12px;flex-shrink:0;}
.sf-name{color:#fff;font-size:13px;font-weight:600;}
.sf-role{color:rgba(255,255,255,.38);font-size:10.5px;}
.sf-logout{margin-left:auto;color:rgba(255,255,255,.35);font-size:16px;cursor:pointer;transition:color .15s;text-decoration:none;}
.sf-logout:hover{color:var(--danger);}

/* MAIN */
.main-wrap{margin-left:var(--sidebar-w);flex:1;display:flex;flex-direction:column;min-height:100vh;}
.topbar{background:var(--white);border-bottom:1px solid var(--border);padding:0 24px;height:62px;display:flex;align-items:center;gap:14px;position:sticky;top:0;z-index:50;box-shadow:var(--shadow);}
.topbar-title{font-size:17px;font-weight:700;color:var(--dark);font-family:'DM Serif Display',serif;}
.topbar-right{margin-left:auto;display:flex;align-items:center;gap:10px;}
.topbar-btn{width:36px;height:36px;border-radius:8px;border:1px solid var(--border);background:var(--white);display:grid;place-items:center;cursor:pointer;color:var(--muted);text-decoration:none;font-size:14px;transition:all .15s;position:relative;}
.topbar-btn:hover{background:var(--bg);color:var(--brand);}
.topbar-btn .badge{position:absolute;top:-4px;right:-4px;width:16px;height:16px;background:var(--danger);color:#fff;border-radius:50%;font-size:9px;font-weight:700;display:grid;place-items:center;}
.view-site-btn{display:flex;align-items:center;gap:6px;padding:7px 14px;background:var(--brand);color:#fff;border-radius:8px;text-decoration:none;font-size:13px;font-weight:600;transition:background .15s;}
.view-site-btn:hover{background:var(--brand2);color:#fff;}

.page-content{flex:1;padding:24px 28px;}

/* FLASH */
.flash{padding:12px 18px;border-radius:var(--radius);margin-bottom:18px;display:flex;align-items:center;gap:10px;font-weight:500;font-size:14px;}
.flash-success{background:#d1fae5;color:#065f46;border:1px solid #6ee7b7;}
.flash-error{background:#fee2e2;color:#991b1b;border:1px solid #fca5a5;}
.flash-info{background:#dbeafe;color:#1e3a8a;border:1px solid #93c5fd;}

/* CARDS */
.card{background:var(--white);border-radius:var(--radius);border:1px solid var(--border);box-shadow:var(--shadow);}
.card-header{padding:16px 22px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px;}
.card-header h2{font-size:15px;font-weight:700;font-family:'DM Serif Display',serif;}
.card-header .card-actions{margin-left:auto;display:flex;gap:8px;}
.card-body{padding:22px;}

/* STATS */
.stats-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(190px,1fr));gap:14px;margin-bottom:22px;}
.stat-card{background:var(--white);border-radius:var(--radius);border:1px solid var(--border);padding:18px;display:flex;align-items:flex-start;gap:14px;box-shadow:var(--shadow);transition:transform .15s,box-shadow .15s;}
.stat-card:hover{transform:translateY(-2px);box-shadow:var(--shadow-lg);}
.stat-icon{width:46px;height:46px;border-radius:12px;display:grid;place-items:center;font-size:20px;flex-shrink:0;}
.stat-num{font-size:26px;font-weight:700;font-family:'DM Serif Display',serif;line-height:1;}
.stat-label{font-size:12px;color:var(--muted);margin-top:3px;font-weight:500;}

/* TABLES */
.table-wrap{overflow-x:auto;}
table{width:100%;border-collapse:collapse;font-size:14px;}
thead tr{background:#f9fafb;}
th{padding:10px 14px;text-align:left;font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.05em;color:var(--muted);border-bottom:1px solid var(--border);}
td{padding:11px 14px;border-bottom:1px solid var(--border);color:var(--text);vertical-align:middle;}
tr:last-child td{border-bottom:none;}
tr:hover td{background:#fafafa;}

/* BADGES */
.badge{display:inline-flex;align-items:center;padding:3px 10px;border-radius:20px;font-size:11px;font-weight:600;}
.badge-green{background:#d1fae5;color:#065f46;}
.badge-red{background:#fee2e2;color:#991b1b;}
.badge-yellow{background:#fef3c7;color:#92400e;}
.badge-blue{background:#dbeafe;color:#1e40af;}

/* BUTTONS */
.btn{display:inline-flex;align-items:center;gap:6px;padding:9px 18px;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;border:none;text-decoration:none;transition:all .15s;font-family:inherit;}
.btn-primary{background:var(--brand);color:#fff;}
.btn-primary:hover{background:var(--brand2);color:#fff;}
.btn-secondary{background:var(--bg);color:var(--text);border:1px solid var(--border);}
.btn-secondary:hover{background:var(--border);}
.btn-danger{background:var(--danger);color:#fff;}
.btn-danger:hover{background:#dc2626;}
.btn-sm{padding:5px 12px;font-size:12px;}
.btn-icon{width:32px;height:32px;padding:0;justify-content:center;}

/* FORMS */
.form-group{margin-bottom:18px;}
.form-label{display:block;font-size:13px;font-weight:600;color:var(--dark);margin-bottom:6px;}
.form-control{width:100%;padding:9px 14px;border:1.5px solid var(--border);border-radius:8px;font-size:14px;font-family:inherit;background:var(--white);color:var(--text);transition:border-color .15s;outline:none;}
.form-control:focus{border-color:var(--brand);box-shadow:0 0 0 3px rgba(26,86,219,.1);}
textarea.form-control{resize:vertical;min-height:100px;}
.form-select{width:100%;padding:9px 14px;border:1.5px solid var(--border);border-radius:8px;font-size:14px;font-family:inherit;background:var(--white);color:var(--text);outline:none;cursor:pointer;}
.form-select:focus{border-color:var(--brand);}
.form-hint{font-size:11px;color:var(--muted);margin-top:4px;}
.form-row{display:grid;grid-template-columns:1fr 1fr;gap:16px;}
.form-row-3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:16px;}

/* PAGE HEADER */
.page-header{display:flex;align-items:center;gap:12px;margin-bottom:20px;}
.page-header h1{font-size:22px;font-weight:700;font-family:'DM Serif Display',serif;}
.page-header .page-header-actions{margin-left:auto;}

/* MISC */
.search-bar{position:relative;}
.search-bar input{padding-left:36px;}
.search-bar i{position:absolute;left:12px;top:50%;transform:translateY(-50%);color:var(--muted);font-size:13px;}
.img-preview{width:80px;height:56px;object-fit:cover;border-radius:6px;border:1px solid var(--border);}

/* MODAL */
.modal-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:1000;place-items:center;}
.modal-overlay.open{display:grid;}
.modal-box{background:var(--white);border-radius:16px;padding:28px;max-width:420px;width:90%;box-shadow:var(--shadow-lg);}
.modal-title{font-size:18px;font-weight:700;margin-bottom:10px;}
.modal-btns{display:flex;gap:10px;justify-content:flex-end;margin-top:20px;}

/* RESPONSIVE */
@media(max-width:900px){
  .sidebar{transform:translateX(-100%);}
  .sidebar.open{transform:translateX(0);}
  .main-wrap{margin-left:0;}
  .form-row,.form-row-3{grid-template-columns:1fr;}
  #hamburger{display:block !important;}
}
</style>
</head>
<body>

<!-- SIDEBAR -->
<aside class="sidebar" id="sidebar">
    <div class="sidebar-logo">
        <?php if ($siteLogo): ?>
            <img src="<?=e(SITE_URL.'/'.$siteLogo)?>" class="logo-img" alt="Logo">
        <?php else: ?>
            <div class="logo-icon">CY</div>
            <div class="logo-text">CY Consultancy<span>Admin Panel</span></div>
        <?php endif; ?>
    </div>
    <nav class="sidebar-nav">
        <div class="nav-section">Main</div>
        <a href="<?=ADMIN_URL?>/index.php" class="nav-item <?=$currentPage==='index'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-tachometer-alt"></i></span> Dashboard
        </a>

        <div class="nav-section">Content</div>
        <a href="<?=ADMIN_URL?>/pages/pages.php" class="nav-item <?=in_array($currentPage,['pages','page-add','page-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-file-alt"></i></span> Pages
        </a>
        <a href="<?=ADMIN_URL?>/pages/blog.php" class="nav-item <?=in_array($currentPage,['blog','blog-add','blog-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-rss"></i></span> Blog Posts
        </a>
        <a href="<?=ADMIN_URL?>/pages/services.php" class="nav-item <?=in_array($currentPage,['services','service-add','service-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-concierge-bell"></i></span> Services
        </a>
        <a href="<?=ADMIN_URL?>/pages/testimonials.php" class="nav-item <?=in_array($currentPage,['testimonials'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-star"></i></span> Testimonials
        </a>
        <a href="<?=ADMIN_URL?>/pages/team.php" class="nav-item <?=in_array($currentPage,['team'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-users"></i></span> Team Members
        </a>

        <div class="nav-section">Study Abroad</div>
        <a href="<?=ADMIN_URL?>/pages/destinations.php" class="nav-item <?=in_array($currentPage,['destinations','dest-add','dest-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-globe-asia"></i></span> Destinations
        </a>
        <a href="<?=ADMIN_URL?>/pages/universities.php" class="nav-item <?=in_array($currentPage,['universities','uni-add','uni-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-university"></i></span> Universities
        </a>
        <a href="<?=ADMIN_URL?>/pages/success.php" class="nav-item <?=in_array($currentPage,['success','success-add','success-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-star"></i></span> Success Stories
        </a>
        <a href="<?=ADMIN_URL?>/pages/gallery.php" class="nav-item <?=in_array($currentPage,['gallery','gallery-add','gallery-edit'])?'active':''?>">
            <span class="nav-icon"><i class="fas fa-images"></i></span> Gallery
        </a>

        <div class="nav-section">Communication</div>
        <a href="<?=ADMIN_URL?>/pages/applications.php" class="nav-item <?=$currentPage==='applications'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-file-alt"></i></span> Applications
            <?php try { $ac=db()->query("SELECT COUNT(*) as c FROM applications WHERE is_read=0 LIMIT 1"); $an=$ac?$ac->fetch_assoc()['c']:0; if($an>0) echo '<span class="nav-badge">'.$an.'</span>'; } catch(Exception $e) {} ?>
        </a>
        <a href="<?=ADMIN_URL?>/pages/messages.php" class="nav-item <?=$currentPage==='messages'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-envelope"></i></span> Messages
            <?php if($unreadCount>0): ?><span class="nav-badge"><?=$unreadCount?></span><?php endif; ?>
        </a>

        <div class="nav-section">Settings</div>
        <a href="<?=ADMIN_URL?>/pages/settings.php" class="nav-item <?=$currentPage==='settings'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-cog"></i></span> Site Settings
        </a>
        <a href="<?=ADMIN_URL?>/pages/header-footer.php" class="nav-item <?=$currentPage==='header-footer'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-code"></i></span> Header / Footer
        </a>
        <a href="<?=ADMIN_URL?>/pages/admin-users.php" class="nav-item <?=$currentPage==='admin-users'?'active':''?>">
            <span class="nav-icon"><i class="fas fa-users-cog"></i></span> Admin Users
        </a>
    </nav>
    <div class="sidebar-footer">
        <div class="sf-avatar"><?=strtoupper(substr($admin['full_name']??$admin['username'],0,1))?></div>
        <div>
            <div class="sf-name"><?=e($admin['full_name']??$admin['username'])?></div>
            <div class="sf-role"><?=ucfirst($admin['role']??'admin')?></div>
        </div>
        <a href="<?=ADMIN_URL?>/logout.php" class="sf-logout" title="Logout"><i class="fas fa-sign-out-alt"></i></a>
    </div>
</aside>

<!-- MAIN -->
<div class="main-wrap">
    <header class="topbar">
        <button id="hamburger" onclick="document.getElementById('sidebar').classList.toggle('open')" style="display:none;background:none;border:none;font-size:20px;cursor:pointer;color:var(--dark)">
            <i class="fas fa-bars"></i>
        </button>
        <div class="topbar-title"><?=isset($pageTitle)?e($pageTitle):'Dashboard'?></div>
        <div class="topbar-right">
            <?php if($unreadCount>0): ?>
            <a href="<?=ADMIN_URL?>/pages/messages.php" class="topbar-btn" title="Messages">
                <i class="fas fa-bell"></i>
                <span class="badge"><?=$unreadCount?></span>
            </a>
            <?php endif; ?>
            <a href="<?=SITE_URL?>/index.php" target="_blank" class="view-site-btn">
                <i class="fas fa-external-link-alt"></i> View Site
            </a>
        </div>
    </header>
    <div class="page-content">
    <?php if ($flash): ?>
        <div class="flash flash-<?=$flash['type']?>">
            <i class="fas <?=$flash['type']==='success'?'fa-check-circle':($flash['type']==='error'?'fa-exclamation-circle':'fa-info-circle')?>"></i>
            <?=e($flash['msg'])?>
        </div>
    <?php endif; ?>
<!-- PAGE CONTENT -->
