<?php
/**
 * expense_theme.php
 * Shared layout/CSS for the Expense Management module, matching the visual
 * theme established in tasks_employee.php (DM Sans, indigo/cyan gradient,
 * dark sidebar, card-based content).
 *
 * Usage in a page:
 *   expense_render_head('My Claims');
 *   expense_render_sidebar_and_topbar($role, $username, 'expense_claim_list.php', 'My Claims');
 *   ... page content inside <div class="page"> ... </div>
 *   expense_render_footer_scripts();
 */

function expense_render_head($title) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>MPC · <?= h($title) ?></title>
<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=DM+Sans:ital,opsz,wght@0,9..40,300;0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
:root{
  --ink:#0f172a;--ink-2:#334155;--muted:#64748b;--border:#e2e8f0;
  --surface:#ffffff;--bg:#f0f4f8;--sidebar-bg:#111827;--sidebar-w:260px;
  --accent:#4f46e5;--accent-2:#06b6d4;--green:#059669;--red:#dc2626;--amber:#d97706;
  --radius:14px;--radius-sm:8px;
  --shadow:0 1px 3px rgba(15,23,42,.06),0 4px 16px rgba(15,23,42,.07);
  --shadow-md:0 4px 6px rgba(15,23,42,.07),0 12px 32px rgba(15,23,42,.10);
  font-family:'DM Sans',system-ui,sans-serif;
}
html,body{height:100%}
body{background:var(--bg);color:var(--ink);font-size:14px;-webkit-font-smoothing:antialiased}
.layout{display:flex;min-height:100vh}
.sidebar{width:var(--sidebar-w);background:var(--sidebar-bg);position:fixed;inset:0 auto 0 0;z-index:300;display:flex;flex-direction:column;overflow-y:auto;transition:transform .28s cubic-bezier(.4,0,.2,1);scrollbar-width:none}
.sidebar::-webkit-scrollbar{display:none}
.sidebar-brand{padding:22px 18px 14px;display:flex;align-items:center;gap:10px;border-bottom:1px solid rgba(255,255,255,.07);margin-bottom:6px}
.brand-logo{width:34px;height:34px;background:linear-gradient(135deg,var(--accent),var(--accent-2));border-radius:8px;display:flex;align-items:center;justify-content:center;font-weight:700;color:#fff;font-size:15px;flex-shrink:0}
.brand-name{font-size:14px;font-weight:700;color:#fff;letter-spacing:-.2px}
.brand-sub{font-size:9px;color:rgba(255,255,255,.35);letter-spacing:1.8px;text-transform:uppercase;margin-top:1px}
.nav-group-label{font-size:9px;font-weight:600;letter-spacing:1.8px;text-transform:uppercase;color:rgba(255,255,255,.25);padding:12px 18px 4px}
.nav-list{list-style:none;padding:0}
.nav-item{list-style:none}
.nav-link{display:flex;align-items:center;gap:9px;padding:9px 14px;margin:1px 8px;border-radius:8px;color:rgba(255,255,255,.6);text-decoration:none;font-size:13px;font-weight:500;transition:all .15s}
.nav-link:hover{background:rgba(255,255,255,.07);color:#fff}
.nav-link.active{background:rgba(79,70,229,.25);color:#a5b4fc}
.nav-link i{width:16px;text-align:center;font-size:13px;flex-shrink:0}
.nav-link.danger{color:#f87171}.nav-link.danger:hover{background:rgba(248,113,113,.1);color:#fca5a5}
.nav-badge{margin-left:auto;background:var(--red);color:#fff;font-size:9.5px;font-weight:700;padding:1px 6px;border-radius:10px}
.sidebar-footer{margin-top:auto;padding:12px 8px;border-top:1px solid rgba(255,255,255,.06)}
.sidebar-user-info{display:flex;align-items:center;gap:10px;padding:10px 14px;margin:0 8px 4px;border-radius:8px;background:rgba(255,255,255,.04)}
.sidebar-avatar{width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg,var(--accent),var(--accent-2));display:flex;align-items:center;justify-content:center;color:#fff;font-size:12px;font-weight:700;flex-shrink:0}
.sidebar-uname{font-size:12px;font-weight:600;color:rgba(255,255,255,.8);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.sidebar-role{font-size:10px;color:rgba(255,255,255,.35);text-transform:uppercase;letter-spacing:.8px}
.sidebar-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.55);z-index:290;backdrop-filter:blur(2px);opacity:0;transition:opacity .28s}
.sidebar-overlay.active{display:block;opacity:1}
.content{flex:1;margin-left:var(--sidebar-w);min-width:0;display:flex;flex-direction:column}
.topbar{height:56px;background:var(--surface);border-bottom:1px solid var(--border);display:flex;align-items:center;gap:12px;padding:0 18px;position:sticky;top:0;z-index:100;box-shadow:0 1px 0 var(--border)}
.hamburger-btn{width:36px;height:36px;border-radius:8px;border:1px solid var(--border);background:transparent;display:flex;align-items:center;justify-content:center;cursor:pointer;color:var(--ink-2);font-size:15px;flex-shrink:0;transition:background .15s}
.hamburger-btn:hover{background:var(--bg)}
.topbar-breadcrumb{display:flex;align-items:center;gap:6px;font-size:12px;color:var(--muted);overflow:hidden}
.topbar-breadcrumb .current{color:var(--ink);font-weight:600;font-size:13px;white-space:nowrap}
.topbar-spacer{flex:1}
.topbar-user{display:flex;align-items:center;gap:8px;font-size:13px;color:var(--ink-2);font-weight:500;flex-shrink:0}
.topbar-avatar{width:30px;height:30px;border-radius:50%;background:linear-gradient(135deg,var(--accent),var(--accent-2));display:flex;align-items:center;justify-content:center;color:#fff;font-size:11px;font-weight:700;flex-shrink:0}
.page{padding:18px;flex:1;max-width:100%}
.page-header{margin-bottom:18px;display:flex;align-items:flex-start;justify-content:space-between;flex-wrap:wrap;gap:10px}
.page-title{font-size:20px;font-weight:700;color:var(--ink);letter-spacing:-.4px}
.page-subtitle{font-size:12px;color:var(--muted);margin-top:2px}
.back-btn{display:inline-flex;align-items:center;gap:6px;padding:8px 14px;background:var(--surface);border:1px solid var(--border);border-radius:var(--radius-sm);font-size:12px;font-weight:600;color:var(--ink-2);text-decoration:none;box-shadow:var(--shadow);transition:background .15s;white-space:nowrap;cursor:pointer;font-family:inherit}
.back-btn:hover{background:var(--bg);color:var(--ink)}
.stats-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:12px;margin-bottom:16px}
.stat-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:14px 16px;box-shadow:var(--shadow);display:flex;align-items:center;gap:12px}
.stat-icon{width:38px;height:38px;border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:15px;flex-shrink:0}
.stat-icon.blue{background:#dbeafe;color:#1d4ed8}
.stat-icon.indigo{background:#e0e7ff;color:#4338ca}
.stat-icon.green{background:#d1fae5;color:#059669}
.stat-icon.red{background:#fee2e2;color:#dc2626}
.stat-icon.amber{background:#fef3c7;color:#92400e}
.stat-label{font-size:10px;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:.5px}
.stat-value{font-size:22px;font-weight:700;color:var(--ink);letter-spacing:-.5px;line-height:1.2}
.dash-card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);box-shadow:var(--shadow);margin-bottom:16px;overflow:hidden}
.dash-card-header{padding:14px 18px;display:flex;align-items:center;justify-content:space-between;border-bottom:1px solid var(--border);flex-wrap:wrap;gap:8px}
.dash-card-title{font-size:13px;font-weight:700;color:var(--ink);display:flex;align-items:center;gap:7px}
.dash-card-title i{color:var(--accent);font-size:12px}
.dash-card-body{padding:16px}
.task-item{border:1px solid var(--border);border-radius:10px;margin-bottom:10px;overflow:hidden;transition:box-shadow .2s}
.task-item:hover{box-shadow:var(--shadow-md)}
.task-item.overdue{border-left:4px solid var(--red)}
.task-item.in-progress{border-left:4px solid var(--accent)}
.task-item.approved{border-left:4px solid var(--green)}
.task-header{padding:11px 14px;display:flex;align-items:center;justify-content:space-between;gap:8px;flex-wrap:wrap}
.task-body{padding:2px 14px 12px}
.task-title-text{font-size:14px;font-weight:700;color:var(--ink);margin-bottom:4px}
.task-desc-text{font-size:12px;color:var(--muted);line-height:1.6}
.task-meta{display:flex;align-items:center;gap:12px;margin-top:8px;font-size:11.5px;color:var(--muted);flex-wrap:wrap}
.task-footer{padding:9px 14px;background:#f8fafc;border-top:1px solid var(--border);display:flex;align-items:center;gap:8px;flex-wrap:wrap}
.remarks-area{background:#f1f5f9;border:1px solid var(--border);border-radius:8px;padding:9px 11px;font-size:11.5px;white-space:pre-wrap;line-height:1.7;color:var(--ink-2);margin-top:8px}
.badge-custom{display:inline-flex;align-items:center;gap:4px;padding:3px 9px;border-radius:20px;font-size:10.5px;font-weight:600;white-space:nowrap}
.badge-pending{background:#f1f5f9;color:#475569}
.badge-progress{background:#e0e7ff;color:#4338ca}
.badge-completed{background:#d1fae5;color:#065f46}
.badge-high{background:#fee2e2;color:#991b1b}
.badge-medium{background:#fef3c7;color:#92400e}
.badge-low{background:#d1fae5;color:#065f46}
.badge-overdue{background:#fee2e2;color:#991b1b}
.badge-purple{background:#ede9fe;color:#5b21b6}
.badge-role{background:#f1f5f9;color:var(--muted);font-size:9.5px;padding:2px 6px;border-radius:10px;margin-left:3px}
.time-chip{display:inline-flex;align-items:center;gap:4px;background:#e0e7ff;color:#4338ca;font-size:10.5px;font-weight:700;padding:3px 9px;border-radius:20px}
.btn-action{display:inline-flex;align-items:center;gap:5px;padding:7px 14px;border-radius:20px;font-size:12px;font-weight:700;border:none;cursor:pointer;font-family:'DM Sans',system-ui,sans-serif;transition:opacity .15s,transform .15s;-webkit-tap-highlight-color:transparent}
.btn-action:hover{opacity:.88;transform:translateY(-1px)}
.btn-action:active{transform:translateY(0)}
.btn-start{background:linear-gradient(135deg,#059669,#10b981);color:#fff}
.btn-finish{background:linear-gradient(135deg,var(--accent),#6366f1);color:#fff}
.btn-danger{background:linear-gradient(135deg,#dc2626,#ef4444);color:#fff}
.btn-remarks{background:var(--surface);border:1px solid var(--border) !important;color:var(--ink-2)}
.btn-save-rem{padding:7px 16px;border-radius:20px;border:none;background:#0f172a;color:#fff;font-size:12px;font-weight:700;cursor:pointer;font-family:'DM Sans',system-ui,sans-serif;margin-top:6px}
.form-group{margin-bottom:14px}
.form-label{display:block;font-size:12px;font-weight:600;color:var(--ink-2);margin-bottom:5px}
.form-control{width:100%;border:1.5px solid var(--border);border-radius:8px;padding:9px 12px;font-size:13px;font-family:'DM Sans',system-ui,sans-serif;color:var(--ink);background:var(--bg);outline:none;resize:vertical;transition:border-color .15s}
.form-control:focus{border-color:var(--accent);background:var(--surface);box-shadow:0 0 0 3px rgba(79,70,229,.08)}
.form-row{display:grid;grid-template-columns:1fr 1fr;gap:14px}
.file-drop{border:2px dashed var(--border);border-radius:10px;padding:20px;text-align:center;color:var(--muted);font-size:12px;background:var(--bg);cursor:pointer;transition:border-color .15s}
.file-drop:hover{border-color:var(--accent)}
.file-drop i{font-size:22px;display:block;margin-bottom:6px;color:var(--accent)}
.alert-custom{padding:12px 14px;border-radius:var(--radius-sm);font-size:13px;margin-bottom:14px;display:flex;align-items:flex-start;gap:10px}
.alert-success-custom{background:#d1fae5;border-left:3px solid var(--green);color:#065f46}
.alert-danger-custom{background:#fee2e2;border-left:3px solid var(--red);color:#991b1b}
.alert-warning-custom{background:#fef3c7;border-left:3px solid var(--amber);color:#78350f}
.tbl-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch}
.dash-table{width:100%;border-collapse:collapse;font-size:12.5px}
.dash-table thead th{background:#f8fafc;color:var(--muted);font-weight:600;font-size:10px;text-transform:uppercase;letter-spacing:.6px;padding:10px 12px;text-align:left;white-space:nowrap;border-bottom:2px solid var(--border)}
.dash-table tbody td{padding:10px 12px;border-bottom:1px solid var(--border);color:var(--ink-2);vertical-align:middle}
.dash-table tbody tr:last-child td{border-bottom:none}
.dash-table tbody tr:hover td{background:#fafbfc}
.empty-state{text-align:center;padding:40px 24px;color:var(--muted)}
.empty-state i{font-size:32px;display:block;margin-bottom:10px;opacity:.3}
.bill-thumb{width:44px;height:44px;border-radius:8px;object-fit:cover;border:1px solid var(--border);cursor:pointer}
.filter-bar{display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px}
.filter-bar select, .filter-bar input{border:1.5px solid var(--border);border-radius:8px;padding:8px 12px;font-size:12.5px;font-family:'DM Sans',system-ui,sans-serif;color:var(--ink-2);background:var(--surface)}
@media(max-width:992px){.stats-grid{grid-template-columns:repeat(2,1fr)}.form-row{grid-template-columns:1fr}}
@media(max-width:768px){
  .sidebar{transform:translateX(-100%)}
  .sidebar.open{transform:translateX(0)}
  .content{margin-left:0}
  .page{padding:14px}
  .stats-grid{grid-template-columns:repeat(2,1fr)}
  .page-header{flex-direction:column;gap:8px}
}
</style>
</head>
<body>
<div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>
<div class="layout">
<?php
}

/**
 * Renders the sidebar + topbar. $role drives which nav group is shown.
 * $nav_extra_badge lets a page show e.g. a pending-count badge on a link.
 */
function expense_render_sidebar_and_topbar($role, $username, $active_page, $page_label, $badges = []) {
    $dashboard_link = match ($role) {
        'admin', 'superadmin' => 'dashboard_admin.php',
        'manager'              => 'dashboard_manager.php',
        default                => 'dashboard_employee.php',
    };
    $role_label = ucfirst($role);
?>
<nav class="sidebar" id="sidebar">
  <div class="sidebar-brand">
    <div class="brand-logo">M</div>
    <div>
      <div class="brand-name">MPC Portal</div>
      <div class="brand-sub"><?= h($role_label) ?></div>
    </div>
  </div>

  <div class="sidebar-user-info">
    <div class="sidebar-avatar"><?= strtoupper(substr($username, 0, 2)) ?></div>
    <div>
      <div class="sidebar-uname"><?= h($username) ?></div>
      <div class="sidebar-role"><?= h($role_label) ?></div>
    </div>
  </div>

  <div class="nav-group-label">Main</div>
  <ul class="nav-list">
    <li class="nav-item"><a href="<?= h($dashboard_link) ?>" class="nav-link"><i class="fas fa-gauge-high"></i> Dashboard</a></li>
  </ul>

  <div class="nav-group-label">Expenses</div>
  <ul class="nav-list">
<?php if ($role === 'employee'): ?>
    <li class="nav-item"><a href="expense_claim_list.php" class="nav-link <?= $active_page==='expense_claim_list.php'?'active':'' ?>"><i class="fas fa-receipt"></i> My Claims</a></li>
    <li class="nav-item"><a href="expense_claim_add.php" class="nav-link <?= $active_page==='expense_claim_add.php'?'active':'' ?>"><i class="fas fa-circle-plus"></i> New Claim</a></li>
<?php elseif ($role === 'manager'): ?>
    <li class="nav-item"><a href="expense_manager_dashboard.php" class="nav-link <?= $active_page==='expense_manager_dashboard.php'?'active':'' ?>">
        <i class="fas fa-list-check"></i> Approvals
        <?php if (!empty($badges['pending'])): ?><span class="nav-badge"><?= (int)$badges['pending'] ?></span><?php endif; ?>
    </a></li>
<?php elseif (in_array($role, ['admin','superadmin'], true)): ?>
    <li class="nav-item"><a href="expense_admin_dashboard.php" class="nav-link <?= $active_page==='expense_admin_dashboard.php'?'active':'' ?>"><i class="fas fa-gauge"></i> Overview</a></li>
    <li class="nav-item"><a href="expense_reports.php" class="nav-link <?= $active_page==='expense_reports.php'?'active':'' ?>"><i class="fas fa-chart-column"></i> Reports</a></li>
    <li class="nav-item"><a href="expense_settings.php" class="nav-link <?= $active_page==='expense_settings.php'?'active':'' ?>"><i class="fas fa-sliders"></i> Settings</a></li>
<?php endif; ?>
  </ul>

  <?php if ($role === 'employee'): ?>
  <div class="nav-group-label">Work</div>
  <ul class="nav-list">
    <li class="nav-item"><a href="apply_leave.php" class="nav-link"><i class="fas fa-calendar-minus"></i> Apply Leave</a></li>
    <li class="nav-item"><a href="tasks_employee.php" class="nav-link"><i class="fas fa-list-check"></i> My Tasks</a></li>
    <li class="nav-item"><a href="daily_report.php" class="nav-link"><i class="fas fa-file-lines"></i> Attendance &amp; Daily Report</a></li>
    <li class="nav-item"><a href="employee_grievance.php" class="nav-link"><i class="fas fa-exclamation-triangle"></i> Grievance</a></li>
  </ul>
  <?php endif; ?>

  <div class="sidebar-footer">
    <ul class="nav-list">
      <li class="nav-item">
        <a href="logout.php" class="nav-link danger" onclick="return confirm('Are you sure you want to logout?')">
          <i class="fas fa-right-from-bracket"></i> Logout
        </a>
      </li>
    </ul>
  </div>
</nav>

<div class="content" id="content">
  <div class="topbar">
    <button class="hamburger-btn" onclick="toggleSidebar()" aria-label="Toggle menu"><i class="fas fa-bars"></i></button>
    <div class="topbar-breadcrumb">
      <span><?= h($role_label) ?></span>
      <i class="fas fa-chevron-right" style="font-size:8px;margin:0 2px;opacity:.5"></i>
      <span>Expenses</span>
      <i class="fas fa-chevron-right" style="font-size:8px;margin:0 2px;opacity:.5"></i>
      <span class="current"><?= h($page_label) ?></span>
    </div>
    <div class="topbar-spacer"></div>
    <div class="topbar-user">
      <div class="topbar-avatar"><?= strtoupper(substr($username, 0, 2)) ?></div>
    </div>
  </div>
<?php
}

function expense_render_flash() {
    if ($flash = get_flash()) {
        $icon = $flash['type'] === 'success' ? 'check-circle' : ($flash['type'] === 'warning' ? 'exclamation-triangle' : 'exclamation-circle');
        echo '<div class="alert-custom alert-' . h($flash['type']) . '-custom"><i class="fas fa-' . $icon . '" style="margin-top:1px;flex-shrink:0"></i><span>' . h($flash['msg']) . '</span></div>';
    }
}

function expense_render_footer_scripts() {
?>
</div>
</div>
<script>
function toggleSidebar() {
  const sidebar = document.getElementById('sidebar');
  const overlay = document.getElementById('sidebarOverlay');
  if (sidebar.classList.contains('open')) { sidebar.classList.remove('open'); overlay.classList.remove('active'); }
  else { sidebar.classList.add('open'); overlay.classList.add('active'); }
}
function closeSidebar() {
  document.getElementById('sidebar').classList.remove('open');
  document.getElementById('sidebarOverlay').classList.remove('active');
}
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeSidebar(); });
</script>
</body>
</html>
<?php
}
