<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= e($pageTitle ?? 'MetLAB') ?> &mdash; Ghana Standards Authority</title>
  <link rel="stylesheet" href="<?= APP_URL ?>/public/css/bootstrap.min.css">
  <link rel="stylesheet" href="<?= APP_URL ?>/public/css/bootstrap-icons.css">
  <link rel="stylesheet" href="<?= APP_URL ?>/public/css/style.css">
</head>
<body>

<!-- Sidebar -->
<div class="sidebar" id="sidebar">
  <div class="sidebar-brand">
    <div class="brand-logo"><i class="bi bi-activity"></i></div>
    <div>
      <div class="brand-name">MetLAB</div>
      <div class="brand-sub">Ghana Standards Authority</div>
    </div>
  </div>

  <nav class="sidebar-nav">
    <a href="<?= APP_URL ?>/index.php?page=dashboard" class="nav-item <?= ($page === 'dashboard') ? 'active' : '' ?>">
      <i class="bi bi-speedometer2"></i> Dashboard
    </a>

    <?php if ($_SESSION['role'] === 'admin'): ?>
    <div class="nav-section">Administration</div>
    <a href="<?= APP_URL ?>/index.php?page=labs" class="nav-item <?= ($page === 'labs') ? 'active' : '' ?>">
      <i class="bi bi-building"></i> Laboratories
    </a>
    <a href="<?= APP_URL ?>/index.php?page=users" class="nav-item <?= ($page === 'users') ? 'active' : '' ?>">
      <i class="bi bi-people"></i> Users
    </a>
    <a href="<?= APP_URL ?>/index.php?page=logs" class="nav-item <?= ($page === 'logs') ? 'active' : '' ?>">
      <i class="bi bi-journal-text"></i> Activity Logs
    </a>
    <?php endif; ?>

    <?php if (in_array($_SESSION['role'], ['admin', 'front_desk'])): ?>
    <div class="nav-section">Front Desk</div>
    <a href="<?= APP_URL ?>/index.php?page=companies" class="nav-item <?= ($page === 'companies') ? 'active' : '' ?>">
      <i class="bi bi-briefcase"></i> Companies
    </a>
    <a href="<?= APP_URL ?>/index.php?page=equipment" class="nav-item <?= ($page === 'equipment') ? 'active' : '' ?>">
      <i class="bi bi-cpu"></i> Equipment
    </a>
    <a href="<?= APP_URL ?>/index.php?page=requests&action=create" class="nav-item">
      <i class="bi bi-plus-circle"></i> New Request
    </a>
    <?php endif; ?>

    <div class="nav-section">Laboratory</div>
    <a href="<?= APP_URL ?>/index.php?page=requests" class="nav-item <?= ($page === 'requests') ? 'active' : '' ?>">
      <i class="bi bi-clipboard-check"></i> Requests
    </a>
    <a href="<?= APP_URL ?>/index.php?page=certificates" class="nav-item <?= ($page === 'certificates') ? 'active' : '' ?>">
      <i class="bi bi-award"></i> Certificates
    </a>
  </nav>

  <div class="sidebar-footer">
    <a href="<?= APP_URL ?>/index.php?page=profile" class="nav-item">
      <i class="bi bi-person-circle"></i> My Profile
    </a>
    <a href="<?= APP_URL ?>/index.php?page=logout" class="nav-item text-danger">
      <i class="bi bi-box-arrow-left"></i> Logout
    </a>
  </div>
</div>

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

  <!-- Top bar -->
  <div class="topbar">
    <button class="btn btn-sm btn-light" id="sidebarToggle">
      <i class="bi bi-list"></i>
    </button>
    <div class="topbar-right">
      <span class="badge bg-primary me-2"><?= e(ucfirst(str_replace('_',' ', $_SESSION['role']))) ?></span>
      <span class="text-muted small"><i class="bi bi-person me-1"></i><?= e($_SESSION['name'] ?? '') ?></span>
    </div>
  </div>

  <!-- Flash Messages -->
  <?php $flash = getFlash(); if ($flash): ?>
  <div class="alert alert-<?= $flash['type'] === 'error' ? 'danger' : $flash['type'] ?> alert-dismissible fade show mx-3 mt-3" role="alert">
    <?= e($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
  </div>
  <?php endif; ?>

  <!-- Page Content -->
  <div class="content-area">
