<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Dashboard - RUSO LMS</title>
    <!-- Fonts -->
    <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=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Inter', 'sans-serif'],
                    },
                    colors: {
                        primary: '#1a56db',
                        'primary-hover': '#1e40af',
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gray-50 text-gray-900 font-sans antialiased flex">

    <?php 
        $class_level = $profile && !empty($profile->class_level) ? $profile->class_level : 'Form 1'; 
        $first_name = $profile ? explode(' ', trim($profile->full_name))[0] : 'Student';
    ?>

    <!-- ================= DESKTOP SIDEBAR ================= -->
    <aside class="hidden md:flex flex-col w-64 bg-gray-900 text-white h-screen fixed left-0 top-0 z-50 overflow-y-auto">
        <!-- Brand -->
        <div class="p-6 border-b border-gray-800">
            <img src="<?= base_url('assets/images/logo.png') ?>" alt="CSO Logo" class="h-12 w-auto bg-white rounded p-1">
        </div>
        
        <!-- Navigation Links -->
        <nav class="flex-1 px-4 py-6 space-y-2">
            <a href="<?= site_url('student/dashboard') ?>" class="flex items-center gap-3 px-4 py-3 bg-gray-800 text-white rounded-lg border-l-4 border-blue-500 transition-colors">
                <svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg>
                <span class="font-medium">Dashboard</span>
            </a>
            
            <a href="<?= site_url('student/courses') ?>" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                <span class="font-medium">My Subjects</span>
            </a>
            
            <a href="#library" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                <span class="font-medium">Library</span>
            </a>

            <!-- Dynamic Payment Link -->
            <div class="pt-4 mt-4 border-t border-gray-800">
                <a href="#" class="flex items-center gap-3 px-4 py-3 text-gray-400 hover:bg-gray-800 hover:text-white rounded-lg transition-colors">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="2" y="5" width="20" height="14" rx="2"></rect><line x1="2" y1="10" x2="22" y2="10"></line></svg>
                    <span class="font-medium"><?= htmlspecialchars($class_level) ?> Payment</span>
                </a>
            </div>
        </nav>
        
        <!-- Sign Out -->
        <div class="p-4 border-t border-gray-800">
            <a href="<?= site_url('auth/logout') ?>" class="flex items-center gap-3 px-4 py-3 text-red-400 hover:bg-red-500 hover:bg-opacity-10 hover:text-red-300 rounded-lg transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"></path></svg>
                <span class="font-medium">Sign Out</span>
            </a>
        </div>
    </aside>

    <!-- ================= MOBILE BOTTOM BAR ================= -->
    <nav class="md:hidden fixed bottom-0 w-full bg-white border-t border-gray-200 z-50 shadow-[0_-4px_10px_rgba(0,0,0,0.05)] pb-safe">
        <div class="flex justify-around items-center h-16 px-2">
            <!-- Home -->
            <a href="<?= site_url('student/dashboard') ?>" class="flex flex-col items-center justify-center w-full text-blue-600 transition-colors">
                <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg>
                <span class="text-[10px] font-medium">Home</span>
            </a>
            
            <!-- Subjects -->
            <a href="#" class="flex flex-col items-center justify-center w-full text-gray-500 hover:text-gray-900 transition-colors">
                <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                <span class="text-[10px] font-medium">Subjects</span>
            </a>

            <!-- Library -->
            <a href="#library" class="flex flex-col items-center justify-center w-full text-gray-500 hover:text-gray-900 transition-colors">
                <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                <span class="text-[10px] font-medium">Library</span>
            </a>
            
            <!-- Pay -->
            <a href="#" class="flex flex-col items-center justify-center w-full text-gray-500 hover:text-gray-900 transition-colors">
                <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="2" y="5" width="20" height="14" rx="2"></rect><line x1="2" y1="10" x2="22" y2="10"></line></svg>
                <span class="text-[10px] font-medium">Pay</span>
            </a>
        </div>
    </nav>

    <!-- ================= MAIN CONTENT ================= -->
    <!-- ml-0 on mobile, ml-64 on desktop to accommodate fixed sidebar -->
    <!-- pb-20 on mobile to accommodate fixed bottom bar, pb-0 on desktop -->
    <main class="flex-1 ml-0 md:ml-64 pb-20 md:pb-0 min-h-screen">
        
        <!-- Topbar -->
        <header class="bg-white h-16 flex items-center justify-between md:justify-end px-6 shadow-sm sticky top-0 z-40">
            <!-- Mobile Brand -->
            <div class="md:hidden flex items-center">
                <img src="<?= base_url('assets/images/logo.png') ?>" alt="CSO Logo" class="h-8 w-auto">
            </div>
            
            <div class="flex items-center gap-3">
                <div class="text-right hidden sm:block">
                    <div class="text-sm font-bold text-gray-900"><?= $profile ? htmlspecialchars($profile->full_name) : 'Student' ?></div>
                    <div class="text-xs text-gray-500 font-medium">Student</div>
                </div>
                <div class="w-10 h-10 rounded-full bg-blue-600 flex items-center justify-center text-white font-bold text-lg shadow-md ring-2 ring-white">
                    <?= $profile ? strtoupper(substr($profile->full_name, 0, 1)) : 'S' ?>
                </div>
            </div>
        </header>

        <div class="p-4 sm:p-6 lg:p-8 max-w-7xl mx-auto space-y-8">
            
            <!-- Welcome Banner -->
            <div class="bg-gradient-to-br from-blue-600 to-blue-400 rounded-2xl p-6 sm:p-8 text-white shadow-lg relative overflow-hidden">
                <!-- Background Pattern -->
                <div class="absolute right-0 top-0 h-full w-1/2 opacity-20" style="background: radial-gradient(circle, rgba(255,255,255,0.8) 1px, transparent 1px); background-size: 20px 20px;"></div>
                
                <div class="relative z-10">
                    <h1 class="text-3xl sm:text-4xl font-bold mb-2">Welcome back, <?= htmlspecialchars($first_name) ?>!</h1>
                    <p class="text-blue-50 text-lg max-w-xl">Track your academic progress, access learning materials, and view library books.</p>
                    <div class="mt-6 inline-flex items-center px-4 py-1.5 rounded-full bg-white/20 backdrop-blur-sm border border-white/30 text-sm font-semibold tracking-wide shadow-sm">
                        Class: <?= htmlspecialchars($class_level) ?>
                    </div>
                </div>
            </div>

            <!-- Subjects Section -->
            <section>
                <div class="flex justify-between items-end mb-4">
                    <h2 class="text-xl font-bold text-gray-900">My Subjects</h2>
                    <a href="#" class="text-sm font-medium text-blue-600 hover:text-blue-700">View All</a>
                </div>
                
                <?php if (!empty($subjects)): ?>
                    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-6">
                        <?php foreach ($subjects as $subject): ?>
                            <div class="bg-white rounded-xl p-5 border border-gray-200 shadow-sm hover:shadow-md hover:border-blue-200 transition-all group">
                                <div class="w-12 h-12 bg-blue-50 text-blue-600 rounded-lg flex items-center justify-center mb-4 group-hover:bg-blue-600 group-hover:text-white transition-colors">
                                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path></svg>
                                </div>
                                <h3 class="font-bold text-lg text-gray-900 mb-1 line-clamp-1"><?= htmlspecialchars($subject->name) ?></h3>
                                <div class="text-sm text-gray-500 mb-4 space-y-0.5">
                                    <p><span class="font-medium text-gray-600">Code:</span> <?= htmlspecialchars($subject->code) ?></p>
                                    <p><span class="font-medium text-gray-600">Teacher:</span> <?= htmlspecialchars($subject->teacher_name ?? 'Not Assigned') ?></p>
                                </div>
                                <div class="pt-4 border-t border-gray-100 flex justify-between items-center">
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-md text-xs font-medium bg-blue-50 text-blue-700">
                                        <?= htmlspecialchars($subject->credit_hours) ?> Credits
                                    </span>
                                    <a href="<?= site_url('student/subject_details/' . $subject->id) ?>" class="text-sm font-semibold text-blue-600 group-hover:text-blue-800">Materials &rarr;</a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="bg-white rounded-2xl border-2 border-dashed border-gray-200 p-8 text-center sm:p-12">
                        <svg class="mx-auto h-12 w-12 text-gray-400 mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                        </svg>
                        <h3 class="text-lg font-bold text-gray-900 mb-1">No subjects assigned</h3>
                        <p class="text-sm text-gray-500 max-w-sm mx-auto">You haven't been enrolled in any subjects for your class yet. Please contact administration.</p>
                    </div>
                <?php endif; ?>
            </section>

            <!-- Library Section -->
            <section id="library">
                <div class="flex justify-between items-end mb-4">
                    <div>
                        <h2 class="text-xl font-bold text-gray-900">Library</h2>
                        <p class="text-sm text-gray-500 mt-1">Recently added books</p>
                    </div>
                    <a href="#" class="text-sm font-medium text-blue-600 hover:text-blue-700">Browse All</a>
                </div>
                
                <?php if (!empty($books)): ?>
                    <!-- Horizontal scroll container on mobile, grid on desktop -->
                    <div class="flex overflow-x-auto pb-4 -mx-4 px-4 sm:mx-0 sm:px-0 sm:grid sm:grid-cols-2 lg:grid-cols-4 sm:gap-6 hide-scroll">
                        <?php foreach ($books as $book): ?>
                            <div class="flex-none w-[200px] sm:w-auto bg-white rounded-xl overflow-hidden border border-gray-200 shadow-sm hover:shadow-lg transition-shadow mr-4 sm:mr-0 flex flex-col">
                                <!-- Book Cover Placeholder -->
                                <div class="h-40 bg-gray-100 flex items-center justify-center relative group">
                                    <?php if (!empty($book->cover_image)): ?>
                                        <!-- Assuming the cover_image is a valid URL or path -->
                                        <img src="<?= htmlspecialchars($book->cover_image) ?>" alt="Cover" class="w-full h-full object-cover">
                                    <?php else: ?>
                                        <!-- Fallback generic cover -->
                                        <div class="w-full h-full bg-gradient-to-br from-indigo-100 to-blue-50 flex flex-col items-center justify-center text-blue-300">
                                            <svg class="w-12 h-12 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"></path></svg>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <!-- Availability Badge Overlay -->
                                    <div class="absolute top-2 right-2">
                                        <?php if ($book->copies_available > 0): ?>
                                            <span class="px-2 py-1 text-[10px] font-bold uppercase tracking-wide rounded-full bg-emerald-100 text-emerald-800 backdrop-blur-sm bg-opacity-90 shadow-sm border border-emerald-200">
                                                Available
                                            </span>
                                        <?php else: ?>
                                            <span class="px-2 py-1 text-[10px] font-bold uppercase tracking-wide rounded-full bg-rose-100 text-rose-800 backdrop-blur-sm bg-opacity-90 shadow-sm border border-rose-200">
                                                Checked Out
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                
                                <!-- Book Info -->
                                <div class="p-4 flex flex-col flex-grow border-t border-gray-100">
                                    <h3 class="font-bold text-gray-900 text-[15px] leading-tight mb-1 line-clamp-2"><?= htmlspecialchars($book->title) ?></h3>
                                    <p class="text-xs text-gray-500 mb-3 line-clamp-1"><?= htmlspecialchars($book->author ?: 'Unknown Author') ?></p>
                                    
                                    <div class="mt-auto pt-3 flex justify-between items-center">
                                        <span class="text-xs font-medium text-gray-400 truncate w-1/2">
                                            <?= htmlspecialchars($book->subject ?: 'General') ?>
                                        </span>
                                        <button class="text-blue-600 hover:bg-blue-50 p-1.5 rounded-full transition-colors" title="Reserve Book">
                                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    
                    <style>
                        /* Hide scrollbar for horizontal scrolling area but keep functionality */
                        .hide-scroll::-webkit-scrollbar { display: none; }
                        .hide-scroll { -ms-overflow-style: none; scrollbar-width: none; }
                    </style>
                <?php else: ?>
                    <div class="bg-white rounded-2xl border border-gray-200 p-8 text-center shadow-sm">
                        <div class="w-16 h-16 bg-gray-50 rounded-full flex items-center justify-center mx-auto mb-4">
                            <svg class="h-8 w-8 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                            </svg>
                        </div>
                        <h3 class="text-lg font-bold text-gray-900 mb-1">Library empty</h3>
                        <p class="text-sm text-gray-500 max-w-sm mx-auto">No books have been added to the library catalogue yet.</p>
                    </div>
                <?php endif; ?>
            </section>
            
        </div>
    </main>

</body>
</html>
