<?php
session_start();
date_default_timezone_set('Asia/Jakarta');
if(!isset($_SESSION['login'])){ header("Location: login.php"); exit(); }
if($_SESSION['role']==='admin'){ header("Location: dashboard.php"); exit(); }
if(in_array($_SESSION['role'],['staff','tu'])){ header("Location: dashboard_staff.php"); exit(); }
include 'koneksi.php';

$user_nama = $_SESSION['nama'] ?? 'Guru';
$user_role = $_SESSION['role'] ?? 'guru';
$hour = (int)date('H');
$hari_arr=['','Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Minggu'];
$bulan_arr=['','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember'];

if($hour>=5&&$hour<11)     { $salam='Selamat Pagi';  $time_period='Morning'; }
elseif($hour>=11&&$hour<15){ $salam='Selamat Siang'; $time_period='Afternoon'; }
elseif($hour>=15&&$hour<18){ $salam='Selamat Sore';  $time_period='Evening'; }
else                        { $salam='Selamat Malam'; $time_period='Night'; }

$total_siswa = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM siswa WHERE status_siswa='Aktif'"))['c'];
$total_kelas = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM kelas"))['c'];
$total_mapel = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM mata_pelajaran"))['c'];
$total_nilai = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM nilai"))['c'];
$bln=date('Y-m');
$nilai_bulan = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM nilai WHERE DATE_FORMAT(created_at,'%Y-%m')='$bln'"))['c'];
$abs_hari = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM absensi WHERE tanggal='".date('Y-m-d')."'"))['c'];
$abs_bulan= mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) c FROM absensi WHERE DATE_FORMAT(tanggal,'%Y-%m')='$bln'"))['c'];

$abs_stat=['Hadir'=>0,'Izin'=>0,'Sakit'=>0,'Alpha'=>0];
$qas=mysqli_query($conn,"SELECT status,COUNT(*) c FROM absensi WHERE tanggal='".date('Y-m-d')."' GROUP BY status");
if($qas) while($r=mysqli_fetch_assoc($qas)) $abs_stat[$r['status']]=(int)$r['c'];

$siswa_baru=[];
$qsb=mysqli_query($conn,"SELECT nama,kelas,jenis_kelamin,foto FROM siswa ORDER BY created_at DESC LIMIT 6");
if($qsb) while($r=mysqli_fetch_assoc($qsb)) $siswa_baru[]=$r;

$nilai_terbaru=[];
$qnv=mysqli_query($conn,"SELECT n.nilai_akhir,n.predikat,s.nama,s.kelas,m.nama_mapel FROM nilai n JOIN siswa s ON n.siswa_id=s.id JOIN mata_pelajaran m ON n.mapel_id=m.id ORDER BY n.created_at DESC LIMIT 8");
if($qnv) while($r=mysqli_fetch_assoc($qnv)) $nilai_terbaru[]=$r;

$absen_terbaru=[];
$qab=mysqli_query($conn,"SELECT a.tanggal,a.status,s.nama,s.kelas FROM absensi a JOIN siswa s ON a.siswa_id=s.id ORDER BY a.tanggal DESC,a.id DESC LIMIT 8");
if($qab) while($r=mysqli_fetch_assoc($qab)) $absen_terbaru[]=$r;

$kelas_list=[];
$qkl=mysqli_query($conn,"SELECT k.*,(SELECT COUNT(*) FROM siswa WHERE kelas=k.nama_kelas AND status_siswa='Aktif') jumlah FROM kelas k ORDER BY tingkat,nama_kelas LIMIT 8");
if($qkl) while($r=mysqli_fetch_assoc($qkl)) $kelas_list[]=$r;
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>📚 Dashboard Guru — <?=htmlspecialchars($user_nama)?></title>
<link rel="icon" href="assets/sekolah.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;500;600;700;800;900&family=Inter:wght@300;400;500;600;700&family=Crimson+Text:ital,wght@0,400;0,600;0,700;1,400&display=swap" rel="stylesheet">
<style>
/* ══════════════════════════════════════════════
   MODERN CLASSIC DESIGN SYSTEM - GURU
══════════════════════════════════════════════ */

:root {
    /* Classic Heritage Palette */
    --navy:          #1a2b4a;
    --navy-deep:     #0f1829;
    --navy-light:    #2d4263;
    --burgundy:      #8b4049;
    --burgundy-deep: #6b3039;
    --gold:          #b8860b;
    --gold-light:    #daa520;
    --sage:          #6b7f6e;
    --sage-light:    #8fa892;
    --cream:         #faf8f3;
    --cream-dark:    #f5f2ea;
    --slate:         #4a5568;
    --slate-light:   #718096;
    
    /* Modern Accents */
    --emerald:       #047857;
    --emerald-light: #d1fae5;
    --azure:         #1e40af;
    --azure-light:   #dbeafe;
    --amber:         #d97706;
    --amber-light:   #fef3c7;
    
    /* UI Foundation */
    --bg:            #fdfcf9;
    --surface:       #ffffff;
    --border:        #e5dfd1;
    --border-dark:   #d4cbbf;
    --text:          #1a2b4a;
    --text-muted:    #64748b;
    --shadow:        rgba(26, 43, 74, 0.08);
    --shadow-mid:    rgba(26, 43, 74, 0.12);
    --shadow-strong: rgba(26, 43, 74, 0.18);
}

* { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
}

html { 
    scroll-behavior: smooth; 
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    display: flex;
}

/* Subtle texture overlay */
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image: 
        linear-gradient(90deg, transparent 79px, var(--border) 79px, var(--border) 81px, transparent 81px),
        linear-gradient(transparent 79px, var(--border) 79px, var(--border) 81px, transparent 81px);
    background-size: 80px 80px;
    opacity: 0.15;
    pointer-events: none;
    z-index: 0;
}

/* ══════════════════════════════════════════════
   SIDEBAR NAVIGATION
══════════════════════════════════════════════ */
.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    background: linear-gradient(180deg, var(--navy-deep) 0%, var(--navy) 100%);
    border-right: 1px solid rgba(184, 134, 11, 0.2);
    z-index: 100;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.1);
}

.sidebar::-webkit-scrollbar {
    width: 8px;
}

.sidebar::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
}

.sidebar::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, var(--gold), var(--gold-light));
    border-radius: 10px;
}

.sidebar-header {
    padding: 2rem 1.5rem;
    border-bottom: 1px solid rgba(184, 134, 11, 0.2);
    text-align: center;
}

.sidebar-logo {
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, var(--gold), var(--gold-light));
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    margin: 0 auto 1rem;
    box-shadow: 
        0 0 0 4px var(--navy-deep),
        0 0 0 6px var(--gold),
        0 8px 16px rgba(0, 0, 0, 0.2);
}

.sidebar-logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 50%;
}

.sidebar-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--cream);
    margin-bottom: 0.25rem;
}

.sidebar-subtitle {
    font-size: 0.75rem;
    color: rgba(250, 248, 243, 0.6);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.sidebar-nav {
    flex: 1;
    padding: 1.5rem 0;
}

.nav-section {
    margin-bottom: 2rem;
}

.nav-section-title {
    font-size: 0.7rem;
    font-weight: 700;
    color: rgba(250, 248, 243, 0.5);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    padding: 0 1.5rem;
    margin-bottom: 0.75rem;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.875rem 1.5rem;
    color: rgba(250, 248, 243, 0.8);
    text-decoration: none;
    transition: all 0.3s ease;
    position: relative;
    font-size: 0.9rem;
    font-weight: 500;
}

.nav-item::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 0;
    background: var(--gold);
    transition: height 0.3s ease;
}

.nav-item:hover,
.nav-item.active {
    background: rgba(184, 134, 11, 0.1);
    color: var(--gold-light);
}

.nav-item:hover::before,
.nav-item.active::before {
    height: 100%;
}

.nav-item-icon {
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
    transition: all 0.3s ease;
}

.nav-item:hover .nav-item-icon {
    background: rgba(184, 134, 11, 0.2);
    transform: scale(1.1);
}

.nav-item-text {
    flex: 1;
}

.sidebar-footer {
    padding: 1.5rem;
    border-top: 1px solid rgba(184, 134, 11, 0.2);
}

.sidebar-user {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    margin-bottom: 1rem;
}

.sidebar-user-avatar {
    width: 48px;
    height: 48px;
    background: linear-gradient(135deg, var(--gold), var(--gold-light));
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    flex-shrink: 0;
    overflow: hidden;
}

.sidebar-user-avatar img {
    width: 48px;
    height: 48px;
    object-fit: cover;
}

.sidebar-user-info h4 {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--cream);
    margin-bottom: 0.125rem;
}

.sidebar-user-info p {
    font-size: 0.75rem;
    color: rgba(250, 248, 243, 0.6);
}

.sidebar-logout {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.875rem;
    background: linear-gradient(135deg, var(--burgundy-deep), var(--burgundy));
    color: var(--cream);
    border: none;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 600;
    font-family: 'Inter', sans-serif;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.sidebar-logout:hover {
    background: linear-gradient(135deg, var(--burgundy), var(--burgundy-deep));
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(139, 64, 73, 0.3);
}

/* ══════════════════════════════════════════════
   ANIMATIONS
══════════════════════════════════════════════ */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

/* ══════════════════════════════════════════════
   MAIN CONTENT
══════════════════════════════════════════════ */
.main-content {
    margin-left: 280px;
    flex: 1;
    min-height: 100vh;
}

.container {
    position: relative;
    z-index: 1;
    max-width: 1500px;
    margin: 0 auto;
    padding: 2.5rem 2rem;
}

/* ══════════════════════════════════════════════
   MASTHEAD
══════════════════════════════════════════════ */
.masthead {
    background: linear-gradient(135deg, var(--navy-deep) 0%, var(--navy) 100%);
    border-radius: 20px;
    margin-bottom: 2.5rem;
    padding: 2.5rem 3rem;
    position: relative;
    overflow: hidden;
    box-shadow: 0 12px 40px var(--shadow-strong);
    animation: fadeInUp 0.6s ease-out;
}

.masthead::before,
.masthead::after {
    content: '';
    position: absolute;
    left: 3rem;
    right: 3rem;
    height: 1px;
    background: linear-gradient(90deg, 
        transparent, 
        rgba(184, 134, 11, 0.3) 20%, 
        rgba(184, 134, 11, 0.6) 50%, 
        rgba(184, 134, 11, 0.3) 80%, 
        transparent
    );
}

.masthead::before { top: 1.5rem; }
.masthead::after { bottom: 1.5rem; }

.masthead-content {
    position: relative;
    z-index: 2;
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 2rem;
    align-items: center;
}

.masthead-avatar {
    width: 100px;
    height: 100px;
    border-radius: 24px;
    background: rgba(255, 255, 255, 0.15);
    border: 3px solid rgba(255, 255, 255, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.masthead-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 21px;
}

.masthead-info {
    flex: 1;
}

.masthead-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 1rem;
    background: rgba(184, 134, 11, 0.15);
    border: 1px solid rgba(184, 134, 11, 0.3);
    border-radius: 50px;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--gold-light);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
}

.masthead-title {
    font-family: 'Playfair Display', serif;
    font-size: 2.25rem;
    font-weight: 800;
    color: var(--cream);
    line-height: 1.2;
    margin-bottom: 0.5rem;
}

.masthead-subtitle {
    font-family: 'Crimson Text', serif;
    font-size: 1.125rem;
    color: rgba(250, 248, 243, 0.75);
    font-style: italic;
    margin-bottom: 1rem;
}

.masthead-meta {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    font-size: 0.875rem;
    color: rgba(250, 248, 243, 0.65);
    font-weight: 500;
}

.masthead-meta-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.masthead-stats {
    display: flex;
    gap: 1rem;
}

.stat-badge {
    background: rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 16px;
    padding: 1rem 1.5rem;
    text-align: center;
    min-width: 90px;
}

.stat-badge-value {
    font-family: 'Playfair Display', serif;
    font-size: 1.875rem;
    font-weight: 700;
    color: var(--cream);
    line-height: 1;
}

.stat-badge-label {
    font-size: 0.7rem;
    color: rgba(250, 248, 243, 0.65);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-top: 0.25rem;
}

/* ══════════════════════════════════════════════
   QUICK ACTIONS
══════════════════════════════════════════════ */
.quick-actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
    margin-bottom: 2.5rem;
}

.action-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.75rem 1.5rem;
    text-decoration: none;
    color: var(--text);
    text-align: center;
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
    box-shadow: 0 2px 12px var(--shadow);
    animation: fadeInUp 0.6s ease-out backwards;
}

.action-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    transition: height 0.3s ease;
}

.action-card:nth-child(1) { animation-delay: 0.1s; }
.action-card:nth-child(2) { animation-delay: 0.15s; }
.action-card:nth-child(3) { animation-delay: 0.2s; }
.action-card:nth-child(4) { animation-delay: 0.25s; }

.action-card:nth-child(1)::before { background: linear-gradient(90deg, var(--navy), var(--azure)); }
.action-card:nth-child(2)::before { background: linear-gradient(90deg, var(--emerald), var(--sage)); }
.action-card:nth-child(3)::before { background: linear-gradient(90deg, var(--amber), var(--gold)); }
.action-card:nth-child(4)::before { background: linear-gradient(90deg, var(--burgundy), #be123c); }

.action-card:hover {
    transform: translateY(-6px);
    box-shadow: 0 12px 28px var(--shadow-mid);
    border-color: var(--border-dark);
}

.action-card:hover::before {
    height: 5px;
}

.action-icon {
    width: 64px;
    height: 64px;
    background: var(--cream-dark);
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    margin: 0 auto 1rem;
    border: 2px solid var(--border);
    transition: all 0.3s ease;
}

.action-card:hover .action-icon {
    transform: scale(1.1) rotate(-5deg);
}

.action-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--navy);
    margin-bottom: 0.375rem;
}

.action-subtitle {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 500;
}

/* ══════════════════════════════════════════════
   STATS CARDS
══════════════════════════════════════════════ */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
    margin-bottom: 2.5rem;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.75rem 1.5rem;
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
    box-shadow: 0 2px 12px var(--shadow);
    animation: fadeInUp 0.6s ease-out backwards;
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    transition: height 0.3s ease;
}

.stat-card:nth-child(1) { animation-delay: 0.3s; }
.stat-card:nth-child(2) { animation-delay: 0.35s; }
.stat-card:nth-child(3) { animation-delay: 0.4s; }
.stat-card:nth-child(4) { animation-delay: 0.45s; }

.stat-card:nth-child(1)::before { background: linear-gradient(90deg, var(--navy), var(--azure)); }
.stat-card:nth-child(2)::before { background: linear-gradient(90deg, var(--emerald), var(--sage)); }
.stat-card:nth-child(3)::before { background: linear-gradient(90deg, var(--amber), var(--gold)); }
.stat-card:nth-child(4)::before { background: linear-gradient(90deg, var(--burgundy), #be123c); }

.stat-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px var(--shadow-mid);
    border-color: var(--border-dark);
}

.stat-card:hover::before {
    height: 5px;
}

.stat-card-icon {
    font-size: 2.5rem;
    margin-bottom: 0.75rem;
    display: block;
    animation: pulse 3s ease-in-out infinite;
}

.stat-card-value {
    font-family: 'Playfair Display', serif;
    font-size: 2.5rem;
    font-weight: 900;
    line-height: 1;
    margin-bottom: 0.375rem;
}

.stat-card:nth-child(1) .stat-card-value { color: var(--navy); }
.stat-card:nth-child(2) .stat-card-value { color: var(--emerald); }
.stat-card:nth-child(3) .stat-card-value { color: var(--amber); }
.stat-card:nth-child(4) .stat-card-value { color: var(--burgundy); }

.stat-card-label {
    font-size: 0.75rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.stat-card-caption {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
}

/* ══════════════════════════════════════════════
   CARDS & TABLES
══════════════════════════════════════════════ */
.two-col {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 2px 12px var(--shadow);
    overflow: hidden;
    animation: fadeInUp 0.6s ease-out backwards;
}

.card:nth-child(odd) { animation-delay: 0.5s; }
.card:nth-child(even) { animation-delay: 0.55s; }

.card-header {
    padding: 1.5rem 1.75rem;
    border-bottom: 2px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.card-header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.card-badge {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    background: var(--cream-dark);
    border: 2px solid var(--border);
}

.card-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--navy);
}

.card-subtitle {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
    margin-top: 0.125rem;
}

.card-link {
    padding: 0.5rem 1.125rem;
    background: var(--cream-dark);
    border: 2px solid var(--border);
    border-radius: 8px;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--navy);
    text-decoration: none;
    transition: all 0.3s ease;
}

.card-link:hover {
    background: var(--navy);
    color: white;
    border-color: var(--navy);
}

.card-body {
    padding: 1.75rem;
}

/* Table Styles */
.table-wrapper {
    overflow-x: auto;
}

.table-wrapper::-webkit-scrollbar {
    height: 6px;
}

.table-wrapper::-webkit-scrollbar-thumb {
    background: var(--gold);
    border-radius: 50px;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px;
}

thead tr {
    background: var(--cream-dark);
    border-bottom: 2px solid var(--border);
}

th {
    padding: 0.875rem 1rem;
    text-align: left;
    font-size: 0.7rem;
    font-weight: 800;
    color: var(--navy);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

tbody tr {
    border-bottom: 1px solid var(--border);
    transition: background 0.2s;
}

tbody tr:hover {
    background: var(--cream-dark);
}

td {
    padding: 1rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text);
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.75rem;
    border-radius: 50px;
    font-size: 0.7rem;
    font-weight: 800;
    border: 1.5px solid transparent;
}

.badge-a { background: #DCFCE7; color: #166534; border-color: #86EFAC; }
.badge-b { background: #DBEAFE; color: #1E40AF; border-color: #93C5FD; }
.badge-c { background: #FEF3C7; color: #92400E; border-color: #FCD34D; }
.badge-d { background: #FEE2E2; color: #991B1B; border-color: #FCA5A5; }

/* Progress Bars */
.progress-item {
    margin-bottom: 1.25rem;
}

.progress-label {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    font-weight: 700;
}

.progress-bar {
    height: 10px;
    background: var(--border);
    border-radius: 50px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    border-radius: 50px;
    transition: width 1s ease;
}

/* ══════════════════════════════════════════════
   RESPONSIVE
══════════════════════════════════════════════ */
@media (max-width: 1200px) {
    .quick-actions {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }
    
    .sidebar.open {
        transform: translateX(0);
    }
    
    .main-content {
        margin-left: 0;
    }
    
    .container {
        padding: 1.5rem 1rem;
    }
    
    .masthead {
        padding: 2rem 1.5rem;
    }
    
    .masthead-content {
        grid-template-columns: 1fr;
        text-align: center;
    }
    
    .masthead-title {
        font-size: 1.75rem;
    }
    
    .quick-actions {
        grid-template-columns: 1fr;
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .two-col {
        grid-template-columns: 1fr;
    }
}

/* Mobile Menu Toggle */
.mobile-menu-toggle {
    display: none;
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 101;
    width: 48px;
    height: 48px;
    background: var(--navy);
    border: 2px solid var(--gold);
    border-radius: 12px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.5rem;
    color: var(--gold);
    box-shadow: 0 4px 12px var(--shadow-strong);
}

@media (max-width: 768px) {
    .mobile-menu-toggle {
        display: flex;
    }
}
</style>
</head>
<body>

<!-- Mobile Menu Toggle -->
<div class="mobile-menu-toggle" onclick="document.querySelector('.sidebar').classList.toggle('open')">
    ☰
</div>

<!-- Sidebar Navigation -->
<aside class="sidebar">
    <div class="sidebar-header">
        <div class="sidebar-logo">
            <img src="assets/sekolah.png" alt="Logo" onerror="this.parentNode.innerHTML='📚'">
        </div>
        <h2 class="sidebar-title">Portal Guru</h2>
        <p class="sidebar-subtitle">Education System</p>
    </div>
    
    <nav class="sidebar-nav">
        <div class="nav-section">
            <h3 class="nav-section-title">Dashboard</h3>
            <a href="dashboard_guru.php" class="nav-item active">
                <div class="nav-item-icon">📊</div>
                <span class="nav-item-text">Overview</span>
            </a>
        </div>
        
        <div class="nav-section">
            <h3 class="nav-section-title">Manajemen Akademik</h3>
            <a href="nilai.php" class="nav-item">
                <div class="nav-item-icon">📝</div>
                <span class="nav-item-text">Input Nilai</span>
            </a>
            <a href="absensi.php" class="nav-item">
                <div class="nav-item-icon">✅</div>
                <span class="nav-item-text">Absensi Siswa</span>
            </a>
        </div>
        
        <div class="nav-section">
            <h3 class="nav-section-title">Data Sekolah</h3>
            <a href="siswa.php" class="nav-item">
                <div class="nav-item-icon">👨‍🎓</div>
                <span class="nav-item-text">Data Siswa</span>
            </a>
            <a href="kelas.php" class="nav-item">
                <div class="nav-item-icon">🏫</div>
                <span class="nav-item-text">Data Kelas</span>
            </a>
            <a href="mata_pelajaran.php" class="nav-item">
                <div class="nav-item-icon">📚</div>
                <span class="nav-item-text">Mata Pelajaran</span>
            </a>
        </div>
    </nav>
    
    <div class="sidebar-footer">
        <div class="sidebar-user">
            <div class="sidebar-user-avatar">
                <?php if(!empty($_SESSION['foto'])&&file_exists($_SESSION['foto'])): ?>
                    <img src="<?=htmlspecialchars($_SESSION['foto'])?>">
                <?php else: ?>
                    👨‍🏫
                <?php endif; ?>
            </div>
            <div class="sidebar-user-info">
                <h4><?= htmlspecialchars($user_nama) ?></h4>
                <p><?= ucfirst($user_role) ?></p>
            </div>
        </div>
        <a href="logout.php" class="sidebar-logout" onclick="return confirm('Yakin ingin keluar?')">
            <span>🚪</span>
            <span>Logout</span>
        </a>
    </div>
</aside>

<!-- Main Content -->
<div class="main-content">
<div class="container">

    <!-- Masthead -->
    <div class="masthead">
        <div class="masthead-content">
            <div class="masthead-avatar">
                <?php if(!empty($_SESSION['foto'])&&file_exists($_SESSION['foto'])): ?>
                    <img src="<?=htmlspecialchars($_SESSION['foto'])?>">
                <?php else: ?>
                    👨‍🏫
                <?php endif; ?>
            </div>
            <div class="masthead-info">
                <div class="masthead-badge">
                    📚 <?=$salam?> · <?=$hari_arr[(int)date('N')]?>, <?=date('d').' '.$bulan_arr[(int)date('m')].' '.date('Y')?>
                </div>
                <h1 class="masthead-title"><?=htmlspecialchars($user_nama)?> 👋</h1>
                <p class="masthead-subtitle">Selamat datang di Portal Guru MA As-Syafi'iyah 02</p>
                <div class="masthead-meta">
                    <div class="masthead-meta-item">
                        <span>📅</span>
                        <span><?= date('l, d F Y') ?></span>
                    </div>
                    <div class="masthead-meta-item">
                        <span>🕐</span>
                        <span><?= $time_period ?></span>
                    </div>
                </div>
            </div>
            <div class="masthead-stats">
                <div class="stat-badge">
                    <div class="stat-badge-value"><?=$total_siswa?></div>
                    <div class="stat-badge-label">Siswa</div>
                </div>
                <div class="stat-badge">
                    <div class="stat-badge-value"><?=$total_nilai?></div>
                    <div class="stat-badge-label">Nilai</div>
                </div>
                <div class="stat-badge">
                    <div class="stat-badge-value"><?=$abs_bulan?></div>
                    <div class="stat-badge-label">Absensi</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="quick-actions">
        <a href="nilai.php" class="action-card">
            <div class="action-icon">📝</div>
            <div class="action-title">Input Nilai</div>
            <div class="action-subtitle">Tugas · UTS · UAS</div>
        </a>
        <a href="absensi.php" class="action-card">
            <div class="action-icon">✅</div>
            <div class="action-title">Absensi Siswa</div>
            <div class="action-subtitle">Catat kehadiran harian</div>
        </a>
        <a href="siswa.php" class="action-card">
            <div class="action-icon">👨‍🎓</div>
            <div class="action-title">Data Siswa</div>
            <div class="action-subtitle">Lihat biodata siswa</div>
        </a>
        <a href="mata_pelajaran.php" class="action-card">
            <div class="action-icon">📚</div>
            <div class="action-title">Mata Pelajaran</div>
            <div class="action-subtitle">Daftar mapel & KKM</div>
        </a>
    </div>

    <!-- Statistics -->
    <div class="stats-grid">
        <div class="stat-card">
            <span class="stat-card-icon">👨‍🎓</span>
            <div class="stat-card-value"><?=$total_siswa?></div>
            <div class="stat-card-label">Siswa Aktif</div>
            <div class="stat-card-caption">Semua kelas</div>
        </div>
        <div class="stat-card">
            <span class="stat-card-icon">📝</span>
            <div class="stat-card-value"><?=$total_nilai?></div>
            <div class="stat-card-label">Total Nilai</div>
            <div class="stat-card-caption"><?=$nilai_bulan?> bulan ini</div>
        </div>
        <div class="stat-card">
            <span class="stat-card-icon">✅</span>
            <div class="stat-card-value"><?=$abs_hari?></div>
            <div class="stat-card-label">Absensi Hari Ini</div>
            <div class="stat-card-caption"><?=$abs_stat['Hadir']?> hadir</div>
        </div>
        <div class="stat-card">
            <span class="stat-card-icon">🏫</span>
            <div class="stat-card-value"><?=$total_kelas?></div>
            <div class="stat-card-label">Total Kelas</div>
            <div class="stat-card-caption"><?=$total_mapel?> mapel</div>
        </div>
    </div>

    <!-- Two Column Layout -->
    <div class="two-col">
        <!-- Absensi Card -->
        <div class="card">
            <div class="card-header">
                <div class="card-header-left">
                    <div class="card-badge">✅</div>
                    <div>
                        <div class="card-title">Absensi Hari Ini</div>
                        <div class="card-subtitle"><?=date('d/m/Y')?></div>
                    </div>
                </div>
                <a href="absensi.php" class="card-link">Kelola →</a>
            </div>
            <div class="card-body">
                <?php $ta=array_sum($abs_stat);$colors=['Hadir'=>'#10B981','Izin'=>'#3B82F6','Sakit'=>'#F59E0B','Alpha'=>'#EF4444']; ?>
                <?php foreach($abs_stat as $st=>$n): $p=$ta>0?round($n/$ta*100):0; ?>
                <div class="progress-item">
                    <div class="progress-label">
                        <span style="color:<?=$colors[$st]?>"><?=$st?></span>
                        <span style="color:<?=$colors[$st]?>"><?=$n?></span>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width:<?=$p?>%;background:<?=$colors[$st]?>"></div>
                    </div>
                </div>
                <?php endforeach; ?>
                <?php if($ta==0): ?>
                <div style="text-align:center;padding:1.5rem;color:#9CA3AF;font-weight:600">
                    📭 Belum ada absensi hari ini
                </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Nilai Terbaru Card -->
        <div class="card">
            <div class="card-header">
                <div class="card-header-left">
                    <div class="card-badge">📝</div>
                    <div>
                        <div class="card-title">Nilai Terbaru</div>
                        <div class="card-subtitle">8 entri terakhir</div>
                    </div>
                </div>
                <a href="nilai.php" class="card-link">Kelola →</a>
            </div>
            <div class="card-body" style="padding:0">
                <?php if(count($nilai_terbaru)>0): ?>
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th>Siswa</th>
                                <th>Mapel</th>
                                <th>Nilai</th>
                                <th>Predikat</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($nilai_terbaru as $n):
                                $pred=$n['predikat']??'';$na=floatval($n['nilai_akhir']);
                                if(!$pred) $pred=$na>=90?'A':($na>=80?'B':($na>=70?'C':($na>=60?'D':'E')));
                                $badge_class = $pred=='A'?'badge-a':($pred=='B'?'badge-b':($pred=='C'?'badge-c':'badge-d'));
                            ?>
                            <tr>
                                <td>
                                    <div style="font-weight:700"><?=htmlspecialchars($n['nama'])?></div>
                                    <div style="font-size:0.75rem;color:#6B7280"><?=htmlspecialchars($n['kelas'])?></div>
                                </td>
                                <td style="font-size:0.85rem"><?=htmlspecialchars($n['nama_mapel'])?></td>
                                <td style="font-family:'Playfair Display',serif;font-weight:800;color:var(--navy)"><?=number_format($na,1)?></td>
                                <td><span class="badge <?=$badge_class?>"><?=$pred?></span></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div style="text-align:center;padding:2rem;color:#9CA3AF;font-weight:600">
                    📭 Belum ada data nilai
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <div style="text-align:center;padding:2rem;border-top:2px solid var(--border);margin-top:2rem">
        <p style="font-size:0.875rem;color:var(--text-muted);font-weight:600">
            © <?=date('Y')?> MA As-Syafi'iyah 02 · Portal Guru · Crafted with excellence
        </p>
    </div>

</div>
</div>

<script>
// Close sidebar when clicking outside on mobile
document.addEventListener('click', (e) => {
    const sidebar = document.querySelector('.sidebar');
    const toggle = document.querySelector('.mobile-menu-toggle');
    
    if (window.innerWidth <= 768) {
        if (!sidebar.contains(e.target) && !toggle.contains(e.target)) {
            sidebar.classList.remove('open');
        }
    }
});

// Close sidebar when clicking nav item on mobile
document.querySelectorAll('.nav-item').forEach(item => {
    item.addEventListener('click', () => {
        if (window.innerWidth <= 768) {
            document.querySelector('.sidebar').classList.remove('open');
        }
    });
});
</script>

</body>
</html>