<?php
defined('BASEPATH') or exit('No direct script access allowed');

$page_title = trim(strip_tags(isset($heading) ? $heading : 'Access Notice'));
$page_message = trim(strip_tags(isset($message) ? $message : 'Something interrupted the request.'));
$is_unauthorized = stripos($page_title, 'unauthorized') !== false || stripos($page_message, 'unauthorized') !== false;
$status_code = http_response_code();
if (empty($status_code) || $status_code < 100) {
	$status_code = $is_unauthorized ? 403 : 500;
}

$eyebrow = $is_unauthorized ? 'Protected Workspace' : 'System Notice';
$headline = $is_unauthorized ? 'Access needs a different permission level' : ($page_title !== '' ? $page_title : 'Something went wrong');
$support_text = $is_unauthorized
	? 'Your account is signed in, but this page is outside the permissions currently assigned to your role.'
	: ($page_message !== '' ? $page_message : 'The request could not be completed right now.');
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title><?= html_escape($headline) ?></title>
	<style type="text/css">
		:root {
			color-scheme: light;
			--bg: #f3f5f9;
			--bg-accent: #e9eefc;
			--panel: rgba(255, 255, 255, 0.88);
			--panel-strong: #ffffff;
			--text: #101828;
			--muted: #667085;
			--border: rgba(148, 163, 184, 0.26);
			--primary: #4f46e5;
			--primary-soft: rgba(79, 70, 229, 0.12);
			--danger: #f97316;
			--shadow: 0 30px 80px rgba(15, 23, 42, 0.12);
		}

		* {
			box-sizing: border-box;
		}

		body {
			margin: 0;
			min-height: 100vh;
			font-family: "Inter", "Segoe UI", Arial, sans-serif;
			background:
				radial-gradient(circle at top left, rgba(79, 70, 229, 0.14), transparent 28%),
				radial-gradient(circle at bottom right, rgba(14, 165, 233, 0.12), transparent 24%),
				linear-gradient(180deg, var(--bg-accent), var(--bg));
			color: var(--text);
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 32px 18px;
		}

		.error-shell {
			width: min(1040px, 100%);
			display: grid;
			grid-template-columns: minmax(0, 1.1fr) minmax(280px, 0.78fr);
			background: var(--panel);
			backdrop-filter: blur(14px);
			border: 1px solid var(--border);
			border-radius: 32px;
			box-shadow: var(--shadow);
			overflow: hidden;
		}

		.error-main {
			padding: 52px;
			background:
				linear-gradient(145deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.72));
		}

		.error-side {
			padding: 40px 34px;
			background:
				linear-gradient(180deg, rgba(79, 70, 229, 0.98), rgba(30, 41, 59, 0.98));
			color: #eef2ff;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			gap: 28px;
		}

		.badge {
			display: inline-flex;
			align-items: center;
			gap: 10px;
			padding: 9px 14px;
			border-radius: 999px;
			background: var(--primary-soft);
			color: var(--primary);
			font-size: 12px;
			font-weight: 700;
			letter-spacing: 0.14em;
			text-transform: uppercase;
		}

		.badge-dot {
			width: 8px;
			height: 8px;
			border-radius: 999px;
			background: var(--danger);
			box-shadow: 0 0 0 6px rgba(249, 115, 22, 0.12);
		}

		h1 {
			margin: 22px 0 14px;
			font-size: clamp(32px, 4vw, 52px);
			line-height: 1.02;
			letter-spacing: -0.04em;
		}

		.lead {
			margin: 0;
			max-width: 54ch;
			font-size: 17px;
			line-height: 1.8;
			color: var(--muted);
		}

		.actions {
			display: flex;
			flex-wrap: wrap;
			gap: 14px;
			margin-top: 34px;
		}

		.action {
			display: inline-flex;
			align-items: center;
			justify-content: center;
			padding: 14px 18px;
			border-radius: 16px;
			text-decoration: none;
			font-weight: 700;
			font-size: 14px;
			transition: transform 0.18s ease, box-shadow 0.18s ease, background-color 0.18s ease;
		}

		.action:hover {
			transform: translateY(-1px);
		}

		.action-primary {
			background: var(--primary);
			color: #fff;
			box-shadow: 0 16px 30px rgba(79, 70, 229, 0.26);
		}

		.action-secondary {
			background: var(--panel-strong);
			color: var(--text);
			border: 1px solid var(--border);
		}

		.status-card {
			border-radius: 24px;
			padding: 24px;
			background: rgba(255, 255, 255, 0.08);
			border: 1px solid rgba(255, 255, 255, 0.14);
		}

		.status-label {
			font-size: 12px;
			letter-spacing: 0.16em;
			text-transform: uppercase;
			opacity: 0.72;
			margin-bottom: 10px;
		}

		.status-code {
			font-size: clamp(46px, 8vw, 72px);
			font-weight: 800;
			line-height: 1;
			letter-spacing: -0.06em;
			margin-bottom: 10px;
		}

		.status-copy {
			margin: 0;
			font-size: 14px;
			line-height: 1.8;
			color: rgba(238, 242, 255, 0.8);
		}

		.side-note {
			font-size: 13px;
			line-height: 1.8;
			color: rgba(238, 242, 255, 0.7);
		}

		.side-note strong {
			color: #fff;
		}

		@media (max-width: 860px) {
			.error-shell {
				grid-template-columns: 1fr;
			}

			.error-main,
			.error-side {
				padding: 30px 22px;
			}
		}
	</style>
</head>
<body>
	<div class="error-shell">
		<section class="error-main">
			<div class="badge">
				<span class="badge-dot"></span>
				<span><?= html_escape($eyebrow) ?></span>
			</div>

			<h1><?= html_escape($headline) ?></h1>
			<p class="lead"><?= html_escape($support_text) ?></p>

			<div class="actions">
				<a class="action action-primary" href="<?= html_escape(function_exists('base_url') ? base_url() : '/') ?>">Go To Workspace</a>
				<a class="action action-secondary" href="javascript:history.back()">Go Back</a>
			</div>
		</section>

		<aside class="error-side">
			<div class="status-card">
				<div class="status-label">Response</div>
				<div class="status-code"><?= (int) $status_code ?></div>
				<p class="status-copy">
					<?= $is_unauthorized ? 'This route is available only to users with the required access scope.' : 'The system returned a controlled error response for this request.' ?>
				</p>
			</div>

			<div class="side-note">
				<strong>Need access?</strong><br>
				If this page should be available to your account, ask an administrator to review your role, designation permissions, and allowed menu access.
			</div>
		</aside>
	</div>
</body>
</html>
