<?php
if(!isset($_SESSION)) session_start();
if(!isset($_SESSION['admin_logged'])){
    header("Location: login.php");
    $timeout_duration = 900; // 15 minutes

if (isset($_SESSION['LAST_ACTIVITY']) && 
   (time() - $_SESSION['LAST_ACTIVITY']) > $timeout_duration) {

    session_unset();
    session_destroy();
    header("Location: login.php?timeout=1");
    exit;
}

$_SESSION['LAST_ACTIVITY'] = time();
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GoMAPS Admin</title>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<style>
:root{
    --primary:#007185;
    --dark:#1e293b;
    --light-bg:#f1f5f9;
    --card-bg:#ffffff;
    --text:#111;
}

body.dark{
    --light-bg:#0f172a;
    --card-bg:#1e293b;
    --text:#f1f5f9;
}

*{box-sizing:border-box;}

body{
    margin:0;
    font-family:'Segoe UI',sans-serif;
    background:var(--light-bg);
    color:var(--text);
    display:flex;
    min-height:100vh;
    overflow-x:hidden;   /* ADD THIS */
}

/* Sidebar */
.sidebar{
    width:250px;
    min-width:250px;   /* ADD THIS */
    max-width:250px;   /* ADD THIS */
    background:var(--primary);
    color:#fff;
    padding:20px;
    transition:0.3s;
}

.sidebar.collapsed{
    width:70px;
}

.sidebar h2{
    margin-top:0;
}

.sidebar a{
    display:block;
    color:#fff;
    text-decoration:none;
    padding:10px;
    border-radius:6px;
    margin-bottom:5px;
}

.sidebar a:hover{
    background:rgba(255,255,255,0.2);
}

/* Main */
.main{
    flex:1;
    display:flex;
    flex-direction:column;
    min-height:100vh;
    width:100%;      /* KEEP THIS */
}

/* Topbar */
.topbar{
    background:var(--card-bg);
    padding:15px 20px;
    display:flex;
    justify-content:space-between;
    align-items:center;
    box-shadow:0 2px 8px rgba(0,0,0,0.05);
}

/* Dropdown */
.profile{
    position:relative;
}

.profile-menu{
    position:absolute;
    right:0;
    top:40px;
    background:var(--card-bg);
    box-shadow:0 5px 15px rgba(0,0,0,0.1);
    display:none;
    border-radius:8px;
    overflow:hidden;
}

.profile-menu a{
    display:block;
    padding:10px 15px;
    text-decoration:none;
    color:var(--text);
}

.profile-menu a:hover{
    background:#eee;
}

/* Content */
.content{
    padding:20px;
    flex:1;
    display:flex;
    flex-direction:column;
}

/* Card */
.card{
    background:var(--card-bg);
    padding:20px;
    border-radius:12px;
    box-shadow:0 5px 20px rgba(0,0,0,0.05);
    margin-bottom:20px;
}

/* Button */
.btn{
    padding:8px 12px;
    border:none;
    border-radius:6px;
    background:var(--primary);
    color:#fff;
    cursor:pointer;
}

/* Toggle Switch */
.switch{
    position:relative;
    display:inline-block;
    width:40px;
    height:20px;
}
.switch input{display:none;}
.slider{
    position:absolute;
    cursor:pointer;
    background:#ccc;
    border-radius:20px;
    top:0; left:0; right:0; bottom:0;
}
.slider:before{
    content:"";
    position:absolute;
    height:16px;
    width:16px;
    left:2px;
    bottom:2px;
    background:white;
    border-radius:50%;
    transition:0.3s;
}
input:checked + .slider{
    background:#4CAF50;
}
input:checked + .slider:before{
    transform:translateX(20px);
}

/* Responsive */
@media(max-width:768px){
    body{flex-direction:column;}
    .sidebar{width:100%;}
}
.admin-footer{
    text-align:center;
    padding:15px 10px;
    font-size:13px;
    background:var(--card-bg);
    color:var(--text);
    border-top:1px solid rgba(0,0,0,0.05);
}
*{
    box-sizing:border-box;
}

html, body{
    width:100%;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>

<div class="sidebar" id="sidebar">
    <h2>GoMAPS</h2>
    <a href="dashboard.php">Dashboard</a>
    <a href="view_search_logs.php">View Search Log</a>
    <a href="blocked_ips.php">Blocked IPs List</a>
    <a href="recommend_sponsored.php">Auto Recommend</a>
    <a href="add_sponsored.php">Add Sponsored</a>
    <a href="view_sponsored.php">View Sponsored</a>
    <a href="activity_logs.php">Activity Logs</a>
    <a href="admin_profile.php">Admin Profile</a>
    <a href="logout.php">Logout</a>
</div>

<div class="main">

<div class="topbar">
    <div>
        <button class="btn" onclick="toggleSidebar()">+</button>
    </div>

    <div class="profile">
        <button class="btn" onclick="toggleProfile()">
<?= $_SESSION['admin_name'] ?> ▼
</button>
        <div class="profile-menu" id="profileMenu">
            <a href="#" onclick="toggleDark()">Dark-Mode</a>
            <a href="logout.php">Logout</a>
        </div>
    </div>
</div>

<div class="content">

<script>
function toggleSidebar(){
    document.getElementById('sidebar').classList.toggle('collapsed');
}

function toggleProfile(){
    document.getElementById('profileMenu').style.display =
    document.getElementById('profileMenu').style.display === 'block'
    ? 'none' : 'block';
}

function toggleDark(){
    document.body.classList.toggle('dark');
}
</script>