<?php
if (!isLoggedIn()) {
    redirectToLogin();
}
$current_user = getCurrentUser();
$current_page = basename($_SERVER['PHP_SELF'], '.php');
?>
<!DOCTYPE html>
<html lang="ro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo APP_NAME; ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Syne:wght@400;500;600;700&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
    <style>
        *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
        :root{
          --bg:#0E0F11;--bg2:#141517;--bg3:#1A1C1F;--bg4:#222428;
          --border:#2A2D32;--border2:#333740;--text:#F0ECE3;--text2:#9A9590;--text3:#5A5650;
          --accent:#D4694A;--accent2:#E8856A;--accent-bg:rgba(212,105,74,0.12);
          --green:#3DB87A;--green-bg:rgba(61,184,122,0.1);
          --yellow:#E8C24A;--yellow-bg:rgba(232,194,74,0.1);
          --red:#E05252;--red-bg:rgba(224,82,82,0.1);
          --blue:#4A8FD4;--blue-bg:rgba(74,143,212,0.1);
          --purple:#9B7DD4;--purple-bg:rgba(155,125,212,0.1);
          --radius:10px;--radius2:14px;
        }
        html,body{height:100%;background:var(--bg);color:var(--text);font-family:'DM Sans',sans-serif;font-size:14px;overflow:hidden}
        #app{display:flex;height:100vh;overflow:hidden}

        /* SIDEBAR */
        #sidebar{width:220px;min-width:220px;background:var(--bg2);border-right:1px solid var(--border);display:flex;flex-direction:column;z-index:10}
        .sb-logo{padding:22px 20px 16px;border-bottom:1px solid var(--border)}
        .sb-logo-text{font-family:'Syne',sans-serif;font-size:16px;font-weight:700;letter-spacing:-0.3px}
        .sb-logo-text span{color:var(--accent)}
        .sb-sub{font-size:11px;color:var(--text3);margin-top:2px;letter-spacing:0.3px}
        .sb-nav{flex:1;padding:14px 10px;overflow-y:auto;display:flex;flex-direction:column;gap:2px}
        .sb-section{font-size:10px;letter-spacing:1.2px;text-transform:uppercase;color:var(--text3);padding:12px 10px 6px;font-weight:600}
        .sb-item{display:flex;align-items:center;gap:10px;padding:9px 10px;border-radius:var(--radius);cursor:pointer;transition:all 0.15s;color:var(--text2);font-size:13.5px;border:1px solid transparent;text-decoration:none}
        .sb-item:hover{background:var(--bg3);color:var(--text)}
        .sb-item.active{background:var(--accent-bg);color:var(--accent);border-color:rgba(212,105,74,0.2)}
        .sb-item svg{width:16px;height:16px;flex-shrink:0;opacity:0.8}
        .sb-item.active svg{opacity:1}
        .sb-badge{margin-left:auto;background:var(--accent);color:white;font-size:10px;border-radius:20px;padding:1px 7px;font-weight:600}
        .sb-footer{padding:14px 10px;border-top:1px solid var(--border)}
        .sb-stats{background:var(--bg3);border-radius:var(--radius);padding:12px;border:1px solid var(--border)}
        .sb-stats-label{font-size:11px;color:var(--text3);margin-bottom:6px}
        .sb-stats-row{display:flex;justify-content:space-between;align-items:center}
        .sb-stats-val{font-family:'Syne',sans-serif;font-size:20px;font-weight:700}
        .sb-stats-delta{font-size:11px;color:var(--green);background:var(--green-bg);padding:2px 7px;border-radius:20px}

        /* MAIN */
        #main{flex:1;overflow:hidden;display:flex;flex-direction:column}
        #topbar{height:56px;min-height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 24px;border-bottom:1px solid var(--border)}
        .tb-title{font-family:'Syne',sans-serif;font-size:17px;font-weight:600;letter-spacing:-0.2px}
        .tb-user{display:flex;align-items:center;gap:10px}
        #content{flex:1;overflow-y:auto;padding:24px}

        /* BUTTONS */
        .btn{display:inline-flex;align-items:center;gap:7px;padding:8px 16px;border-radius:8px;cursor:pointer;font-size:13px;font-weight:500;border:none;transition:all 0.15s;font-family:'DM Sans',sans-serif;text-decoration:none}
        .btn-primary{background:var(--accent);color:white}.btn-primary:hover{background:var(--accent2)}
        .btn-ghost{background:var(--bg3);color:var(--text);border:1px solid var(--border2)}.btn-ghost:hover{background:var(--bg4)}
        .btn-danger{background:var(--red-bg);color:var(--red);border:1px solid rgba(224,82,82,0.2)}.btn-danger:hover{background:rgba(224,82,82,0.2)}
        .btn-sm{padding:5px 11px;font-size:12px}
        .btn svg{width:15px;height:15px}

        /* CARDS */
        .card{background:var(--bg2);border:1px solid var(--border);border-radius:var(--radius2);overflow:hidden}
        .card-header{padding:16px 20px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between}
        .card-title{font-family:'Syne',sans-serif;font-size:14px;font-weight:600}
        .card-body{padding:20px}

        /* STAT CARDS */
        .stats-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:14px;margin-bottom:22px}
        .stat-card{background:var(--bg2);border:1px solid var(--border);border-radius:var(--radius2);padding:18px 20px;position:relative;overflow:hidden}
        .stat-label{font-size:11.5px;color:var(--text2);margin-bottom:10px;text-transform:uppercase;letter-spacing:0.6px;font-weight:500}
        .stat-value{font-family:'Syne',sans-serif;font-size:26px;font-weight:700;line-height:1}
        .stat-sub{font-size:12px;color:var(--text3);margin-top:8px}
        .stat-icon{position:absolute;top:18px;right:20px;width:34px;height:34px;border-radius:9px;display:flex;align-items:center;justify-content:center}
        .stat-icon.orange{background:var(--accent-bg);color:var(--accent)}
        .stat-icon.green{background:var(--green-bg);color:var(--green)}
        .stat-icon.blue{background:var(--blue-bg);color:var(--blue)}
        .stat-icon.purple{background:var(--purple-bg);color:var(--purple)}
        .stat-icon svg{width:17px;height:17px}

        /* TABLE */
        .table-wrap{overflow-x:auto}
        table{width:100%;border-collapse:collapse}
        thead tr{border-bottom:1px solid var(--border)}
        th{font-size:11.5px;font-weight:600;color:var(--text3);text-transform:uppercase;letter-spacing:0.7px;padding:10px 14px;text-align:left;white-space:nowrap}
        td{padding:11px 14px;font-size:13.5px;color:var(--text);border-bottom:1px solid var(--border)}
        tr:last-child td{border-bottom:none}
        tbody tr{transition:background 0.1s}
        tbody tr:hover td{background:var(--bg3)}
        .td-muted{color:var(--text2)}
        .td-actions{display:flex;gap:5px;justify-content:flex-end;align-items:center;flex-wrap:nowrap}

        /* BADGE */
        .badge{display:inline-flex;align-items:center;gap:4px;padding:3px 10px;border-radius:20px;font-size:11.5px;font-weight:500}
        .badge-green{background:var(--green-bg);color:var(--green)}
        .badge-red{background:var(--red-bg);color:var(--red)}
        .badge-yellow{background:var(--yellow-bg);color:var(--yellow)}
        .badge-blue{background:var(--blue-bg);color:var(--blue)}
        .badge-orange{background:var(--accent-bg);color:var(--accent)}
        .badge-gray{background:var(--bg4);color:var(--text2)}

        /* FORM INPUTS */
        .search-bar{display:flex;align-items:center;gap:10px;background:var(--bg3);border:1px solid var(--border);border-radius:8px;padding:8px 14px;flex:1;max-width:300px}
        .search-bar svg{width:15px;height:15px;color:var(--text3);flex-shrink:0}
        .search-bar input{background:none;border:none;outline:none;color:var(--text);font-size:13.5px;font-family:'DM Sans',sans-serif;width:100%}
        .search-bar input::placeholder{color:var(--text3)}
        .toolbar{display:flex;align-items:center;gap:10px;margin-bottom:16px;flex-wrap:wrap}
        .form-grid{display:grid;grid-template-columns:1fr 1fr;gap:14px}
        .form-grid.c1{grid-template-columns:1fr}
        .form-grid.c3{grid-template-columns:1fr 1fr 1fr}
        .form-group{display:flex;flex-direction:column;gap:6px}
        .form-group.full{grid-column:1/-1}
        .form-label{font-size:11.5px;font-weight:600;color:var(--text2);letter-spacing:0.4px;text-transform:uppercase}
        .form-input,.form-select{background:var(--bg3);border:1px solid var(--border);color:var(--text);border-radius:8px;padding:9px 13px;font-family:'DM Sans',sans-serif;font-size:13.5px;outline:none;width:100%;transition:border-color 0.15s}
        .form-input:focus,.form-select:focus{border-color:var(--accent)}
        .form-input::placeholder{color:var(--text3)}
        textarea.form-input{resize:vertical;min-height:72px}
        .form-select{cursor:pointer;appearance:auto}

        /* MODAL */
        .modal-overlay{position:fixed;inset:0;background:rgba(0,0,0,0.72);z-index:100;display:flex;align-items:center;justify-content:center;padding:20px;backdrop-filter:blur(4px);opacity:0;pointer-events:none;transition:opacity 0.2s}
        .modal-overlay.open{opacity:1;pointer-events:all}
        .modal{background:var(--bg2);border:1px solid var(--border2);border-radius:16px;width:100%;max-width:640px;max-height:90vh;overflow-y:auto;transform:translateY(14px) scale(0.98);transition:transform 0.2s}
        .modal-overlay.open .modal{transform:none}
        .modal-xl{max-width:840px}
        .modal-header{padding:20px 24px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;background:var(--bg2);z-index:2}
        .modal-title{font-family:'Syne',sans-serif;font-size:16px;font-weight:600}
        .modal-close{background:none;border:none;color:var(--text2);cursor:pointer;padding:4px;border-radius:6px;display:flex;transition:all 0.1s}
        .modal-close:hover{background:var(--bg3);color:var(--text)}
        .modal-body{padding:24px}
        .modal-footer{padding:16px 24px;border-top:1px solid var(--border);display:flex;justify-content:flex-end;gap:10px;position:sticky;bottom:0;background:var(--bg2)}

        /* AVATAR */
        .avatar{width:32px;height:32px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:12px;font-weight:600;flex-shrink:0;background:var(--accent-bg);color:var(--accent)}

        /* TOAST */
        #toast-container{position:fixed;bottom:24px;right:24px;z-index:999;display:flex;flex-direction:column;gap:8px}
        .toast{background:var(--bg2);border:1px solid var(--border2);border-radius:10px;padding:12px 16px;font-size:13.5px;min-width:240px;display:flex;align-items:center;gap:10px;box-shadow:0 8px 32px rgba(0,0,0,0.5);animation:sIn 0.25s ease}
        .toast.success{border-left:3px solid var(--green)}.toast.error{border-left:3px solid var(--red)}.toast.info{border-left:3px solid var(--blue)}
        @keyframes sIn{from{transform:translateX(20px);opacity:0}to{transform:none;opacity:1}}
        @keyframes sOut{to{opacity:0;transform:translateX(20px)}}
        .toast.hide{animation:sOut 0.25s ease forwards}
        ::-webkit-scrollbar{width:5px;height:5px}::-webkit-scrollbar-track{background:transparent}::-webkit-scrollbar-thumb{background:var(--border2);border-radius:3px}

        /* EMPTY */
        .empty-state{text-align:center;padding:48px 20px;color:var(--text3)}
        .empty-state svg{width:38px;height:38px;margin:0 auto 12px;opacity:0.3;display:block}
        .empty-state h3{font-size:15px;color:var(--text2);margin-bottom:6px;font-family:'Syne',sans-serif}

        .dash-grid{display:grid;grid-template-columns:2fr 1fr;gap:16px;margin-top:16px}
        .dash-grid2{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-top:16px}
        .progress-bar{height:6px;background:var(--bg4);border-radius:3px;overflow:hidden;margin-top:5px}
        .progress-fill{height:100%;border-radius:3px;transition:width 0.5s}

        /* MOBILE RESPONSIVE */
        @media (max-width: 768px) {
            #sidebar{width:70px;min-width:70px}
            .sb-logo{padding:12px 10px;text-align:center}
            .sb-logo-text{font-size:18px}
            .sb-sub{display:none}
            .sb-nav{padding:8px 6px;gap:0px}
            .sb-section{display:none}
            .sb-item{padding:10px;border-radius:8px;font-size:0;gap:0;justify-content:center}
            .sb-item svg{width:18px;height:18px;margin:0}
            .sb-footer{display:none}
            
            #topbar{padding:0 16px;height:50px;min-height:50px}
            .tb-title{font-size:15px}
            #content{padding:16px}
            
            .stats-grid{grid-template-columns:repeat(2,1fr);gap:10px;margin-bottom:16px}
            .stat-card{padding:14px 16px}
            .stat-label{font-size:10px}
            .stat-value{font-size:22px}
            
            .form-grid{grid-template-columns:1fr}
            .form-grid.c3{grid-template-columns:1fr}
            .dash-grid{grid-template-columns:1fr}
            .dash-grid2{grid-template-columns:1fr}
            
            .table-wrap{font-size:12px}
            th{padding:8px 10px;font-size:10px}
            td{padding:8px 10px;font-size:12px}
            .btn{padding:6px 12px;font-size:12px}
            .btn-sm{padding:4px 8px;font-size:11px}
            
            .search-bar{max-width:100%;padding:6px 10px}
            .search-bar svg{width:14px;height:14px}
            .modal{border-radius:12px}
            .modal-header{padding:16px 16px}
            .modal-body{padding:16px}
            
            #toast-container{bottom:12px;right:12px}
            .toast{min-width:120px;padding:10px 12px;font-size:12px}
            .empty-state{padding:32px 16px}
            .empty-state svg{width:32px;height:32px}
            .empty-state h3{font-size:14px}
        }

        @media (max-width: 480px) {
            #sidebar{width:60px}
            .sb-item svg{width:16px;height:16px}
            .sb-logo-text{font-size:16px}
            .stats-grid{grid-template-columns:1fr}
            .stat-card{padding:12px 14px}
            .stat-label{font-size:9px}
            .stat-value{font-size:18px}
        }
    </style>
</head>
<body>
<div id="app">
    <nav id="sidebar">
        <div class="sb-logo">
            <div class="sb-logo-text">Blid<span>Store</span></div>
            <div class="sb-sub">Magazin online</div>
        </div>
        <div class="sb-nav">
            <div class="sb-section">General</div>
            <a href="<?php echo APP_URL; ?>/admin/dashboard.php" class="sb-item <?php echo $current_page == 'dashboard' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg>
                Dashboard
            </a>
            <div class="sb-section">Vânzări</div>
            <a href="<?php echo APP_URL; ?>/admin/orders.php" class="sb-item <?php echo $current_page == 'orders' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2"/><rect x="9" y="3" width="6" height="4" rx="1"/><path d="M9 12h6M9 16h4"/></svg>
                Comenzi
            </a>
            <div class="sb-section">Inventar</div>
            <a href="<?php echo APP_URL; ?>/admin/products.php" class="sb-item <?php echo $current_page == 'products' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M20 7H4a2 2 0 00-2 2v10a2 2 0 002 2h16a2 2 0 002-2V9a2 2 0 00-2-2z"/><path d="M16 7V5a2 2 0 00-2-2h-4a2 2 0 00-2 2v2"/></svg>
                Produse
            </a>
            <a href="<?php echo APP_URL; ?>/admin/stock.php" class="sb-item <?php echo $current_page == 'stock' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><rect x="2" y="3" width="6" height="6" rx="1"/><rect x="9" y="3" width="6" height="6" rx="1"/><rect x="16" y="3" width="6" height="6" rx="1"/><rect x="2" y="12" width="6" height="6" rx="1"/><rect x="9" y="12" width="6" height="6" rx="1"/><rect x="16" y="12" width="6" height="6" rx="1"/></svg>
                Stoc
            </a>
            <div class="sb-section">Configurare</div>
            <a href="<?php echo APP_URL; ?>/admin/discounts.php" class="sb-item <?php echo $current_page == 'discounts' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M12 2L2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5M2 12l10 5 10-5"/></svg>
                Reduceri
            </a>
            <a href="<?php echo APP_URL; ?>/admin/settings.php" class="sb-item <?php echo $current_page == 'settings' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
                Setări
            </a>
            <a href="<?php echo APP_URL; ?>/admin/audit.php" class="sb-item <?php echo $current_page == 'audit' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="12" y1="11" x2="8" y2="11"/><line x1="12" y1="15" x2="8" y2="15"/><line x1="12" y1="19" x2="8" y2="19"/></svg>
                Audit Log
            </a>
            <a href="<?php echo APP_URL; ?>/admin/backups.php" class="sb-item <?php echo $current_page == 'backups' ? 'active' : ''; ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
                Backup
            </a>
        </div>
        <div class="sb-footer">
            <div class="sb-stats">
                <div class="sb-stats-label">Vânzări azi</div>
                <div class="sb-stats-row">
                    <div class="sb-stats-val" id="sb-revenue-today">0 lei</div>
                    <div class="sb-stats-delta" id="sb-orders-today">0 comenzi</div>
                </div>
            </div>
        </div>
    </nav>

    <div id="main">
        <div id="topbar">
            <div class="tb-title" id="page-title"><?php echo isset($page_title) ? $page_title : 'Dashboard'; ?></div>
            <div class="tb-user">
                <div id="topbar-actions"></div>
                <div class="avatar"><?php echo $current_user ? strtoupper(substr($current_user['name'], 0, 2)) : '?'; ?></div>
                <a href="<?php echo APP_URL; ?>/logout.php" class="btn btn-ghost btn-sm">Deconectare</a>
            </div>
        </div>
        <div id="content">
