<?php
$errorLang = 'en';
$errorDir = 'ltr';
$errorFontImports = [];
$errorBodyFont = "system-ui,-apple-system,'Segoe UI',Roboto,sans-serif";
if (class_exists('\Core\System\Language')) {
    try {
        $errorLang = \Core\System\Language::code();
        $errorDir = \Core\System\Language::direction();
    } catch (\Throwable $e) {
        $errorLang = 'en';
        $errorDir = 'ltr';
    }
}

if (class_exists('\Core\App\Models\ThemeSettings')) {
    try {
        $errorThemeSettings = (new \Core\App\Models\ThemeSettings())->getAll();
        $errorAdminTheme = $errorThemeSettings['admin_theme'] ?? [];
        $errorAdminFonts = $errorAdminTheme['fonts'] ?? [];
        $errorFontMap = [
            'ar_cairo' => [
                'family' => "'Cairo', sans-serif",
                'import' => '/public/assets/fonts/cairo/cairo.css',
            ],
            'ar_tajawal' => [
                'family' => "'Tajawal', sans-serif",
                'import' => '/public/assets/fonts/tajawal/tajawal.css',
            ],
            'ar_noto_kufi' => [
                'family' => "'Noto Kufi Arabic', sans-serif",
                'import' => '/public/assets/fonts/noto-kufi-arabic/noto-kufi-arabic.css',
            ],
            'en_poppins' => [
                'family' => "'Poppins', sans-serif",
                'import' => '/public/assets/fonts/poppins/poppins.css',
            ],
            'en_roboto' => [
                'family' => "'Roboto', sans-serif",
                'import' => '/public/assets/fonts/roboto/roboto.css',
            ],
        ];

        $errorBodyKey = isset($errorAdminFonts['body']) ? $errorAdminFonts['body'] : null;
        if ($errorBodyKey && isset($errorFontMap[$errorBodyKey])) {
            $errorFontImports[$errorBodyKey] = $errorFontMap[$errorBodyKey]['import'];
            $errorBodyFont = $errorFontMap[$errorBodyKey]['family'];
        }
    } catch (\Throwable $e) {
    }
}
?>
<!DOCTYPE html>
<html lang="<?= htmlspecialchars($errorLang, ENT_QUOTES) ?>" dir="<?= htmlspecialchars($errorDir, ENT_QUOTES) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>500</title>
    <link rel="stylesheet" href="/adminlte/plugins/bootstrap/css/bootstrap.min.css">
    <?php foreach ($errorFontImports as $errorFontCss): ?>
        <link rel="stylesheet" href="<?= htmlspecialchars($errorFontCss, ENT_QUOTES, 'UTF-8') ?>">
    <?php endforeach; ?>
    <style>
        body{min-height:100vh;display:flex;align-items:center;justify-content:center;background:#f4f6f9;font-family:<?= $errorBodyFont ?>}
        .error-card{max-width:560px;background:#fff;border-radius:18px;box-shadow:0 10px 30px rgba(0,0,0,.08);padding:32px;text-align:center}
        .code{font-size:64px;font-weight:800;color:#dc3545;line-height:1}
    </style>
</head>
<body>
<div class="error-card">
    <div class="code">500</div>
    <h1 class="h4 mt-3 mb-2"><?= __('errors.500.title', 'Something went wrong') ?></h1>
    <p class="text-muted mb-4"><?= __('errors.500.message', 'An unexpected error occurred. The error has been logged for review.') ?></p>
    <a href="/admin/dashboard" class="btn btn-primary"><?= __('sidebar.dashboard', 'Dashboard') ?></a>
</div>
</body>
</html>
