<?php
$appSetting = function_exists('app_setting') ? app_setting() : [];
$appName    = trim((string) ($appSetting['nama_aplikasi'] ?? 'NusaPOS')) ?: 'NusaPOS';
$appTitle   = trim((string) ($appSetting['judul_aplikasi'] ?? '')) ?: $appName;
$appTagline = trim((string) ($appSetting['tagline'] ?? '')) ?: 'Point of Sale dan service toko.';
$appLogo    = $appSetting['logo'] ?? null;
$appFavicon = $appSetting['favicon'] ?? null;
$logoUrl    = ! empty($appLogo) ? base_url('uploads/settings/' . $appLogo) : null;
$faviconUrl = ! empty($appFavicon) ? base_url('uploads/settings/' . $appFavicon) : base_url('favicon.ico');
$brandIconUrl = $logoUrl ?: $faviconUrl;
$isLoggedIn = (bool) session()->get('isLoggedIn');
$primaryUrl = $isLoggedIn ? site_url('admin/dashboard') : site_url('/');
$primaryLabel = $isLoggedIn ? 'Ke Dashboard' : 'Ke Login';
$messageText = 'Alamat menu atau transaksi yang Anda buka tidak tersedia di ' . $appName . '. Data mungkin sudah dipindahkan, dihapus, atau akses halaman tidak tersedia untuk akun ini.';
$technicalMessage = ENVIRONMENT !== 'production'
    ? trim((string) ($message ?? ''))
    : '';
?>
<!doctype html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>404 - <?= esc($appTitle) ?></title>
    <link rel="icon" href="<?= esc($faviconUrl, 'attr') ?>">
    <link rel="shortcut icon" href="<?= esc($faviconUrl, 'attr') ?>">
    <link rel="apple-touch-icon" href="<?= esc($faviconUrl, 'attr') ?>">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <style>
        :root {
            --admin-bg: #f7f9fc;
            --admin-sidebar: #101828;
            --admin-text: #101828;
            --admin-muted: #667085;
            --admin-border: #e4e7ec;
            --admin-primary: #2563eb;
            --admin-card: #ffffff;
            --admin-soft: #eff6ff;
        }

        * {
            box-sizing: border-box;
        }

        body {
            min-height: 100vh;
            margin: 0;
            display: grid;
            place-items: center;
            padding: 24px;
            background: var(--admin-bg);
            color: var(--admin-text);
            font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
        }

        .error-shell {
            width: min(980px, 100%);
            display: grid;
            grid-template-columns: minmax(280px, .78fr) minmax(0, 1.22fr);
            overflow: hidden;
            border: 1px solid var(--admin-border);
            border-radius: 8px;
            background: var(--admin-card);
            box-shadow: 0 20px 55px rgba(16, 24, 40, .10);
        }

        .brand-panel {
            min-height: 520px;
            padding: 32px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            background: var(--admin-sidebar);
            color: #ffffff;
        }

        .brand-lockup {
            display: flex;
            align-items: center;
            gap: 12px;
            min-width: 0;
        }

        .brand-logo,
        .brand-mark {
            width: 46px;
            height: 46px;
            flex: 0 0 46px;
            border-radius: 8px;
            background: #ffffff;
        }

        .brand-logo {
            object-fit: contain;
            padding: 5px;
        }

        .brand-mark {
            display: grid;
            place-items: center;
            color: var(--admin-sidebar);
            font-size: 21px;
            font-weight: 800;
        }

        .brand-name {
            font-size: 19px;
            font-weight: 800;
            line-height: 1.15;
        }

        .brand-tagline {
            margin-top: 2px;
            color: rgba(255, 255, 255, .72);
            font-size: 13px;
            line-height: 1.45;
        }

        .error-code {
            font-size: clamp(72px, 11vw, 128px);
            line-height: .9;
            font-weight: 850;
            letter-spacing: 0;
        }

        .error-code-caption {
            margin-top: 16px;
            max-width: 320px;
            color: rgba(255, 255, 255, .78);
            font-size: 14px;
            line-height: 1.7;
        }

        .status-list {
            display: grid;
            gap: 10px;
        }

        .status-item {
            display: flex;
            align-items: center;
            gap: 10px;
            color: rgba(255, 255, 255, .82);
            font-size: 13px;
            font-weight: 650;
        }

        .status-item i {
            width: 30px;
            height: 30px;
            display: grid;
            place-items: center;
            border-radius: 8px;
            background: rgba(255, 255, 255, .10);
        }

        .content-panel {
            padding: 56px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .eyebrow {
            width: fit-content;
            margin-bottom: 18px;
            padding: 7px 10px;
            border-radius: 8px;
            background: var(--admin-soft);
            color: var(--admin-primary);
            font-size: 13px;
            font-weight: 750;
        }

        h1 {
            margin: 0;
            max-width: 560px;
            font-size: clamp(30px, 4vw, 44px);
            line-height: 1.15;
            font-weight: 850;
            letter-spacing: 0;
        }

        .message {
            max-width: 600px;
            margin: 16px 0 0;
            color: var(--admin-muted);
            font-size: 15px;
            line-height: 1.75;
        }

        .quick-grid {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 10px;
            margin-top: 26px;
        }

        .quick-link {
            min-height: 92px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding: 14px;
            border: 1px solid var(--admin-border);
            border-radius: 8px;
            background: #ffffff;
            color: var(--admin-text);
            text-decoration: none;
        }

        .quick-link i {
            width: 34px;
            height: 34px;
            display: grid;
            place-items: center;
            border-radius: 8px;
            background: var(--admin-soft);
            color: var(--admin-primary);
            font-size: 17px;
        }

        .quick-link span {
            margin-top: 12px;
            font-size: 13px;
            font-weight: 750;
        }

        .technical-message {
            margin-top: 16px;
            color: var(--admin-muted);
            font-size: 12px;
            line-height: 1.55;
        }

        .technical-message summary {
            cursor: pointer;
            color: var(--admin-text);
            font-weight: 700;
        }

        .actions {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 30px;
        }

        .btn {
            min-height: 44px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 10px 16px;
            border: 1px solid transparent;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 750;
            text-decoration: none;
            cursor: pointer;
        }

        .btn-primary {
            background: var(--admin-sidebar);
            border-color: var(--admin-sidebar);
            color: #ffffff;
        }

        .btn-primary:hover,
        .btn-primary:focus {
            background: #1d2939;
            border-color: #1d2939;
        }

        .btn-light {
            background: #ffffff;
            border-color: var(--admin-border);
            color: var(--admin-text);
        }

        .btn-light:hover,
        .btn-light:focus {
            background: #f8fafc;
        }

        .hint {
            margin-top: 28px;
            padding: 14px 16px;
            border: 1px solid var(--admin-border);
            border-radius: 8px;
            background: #f9fafb;
            color: var(--admin-muted);
            font-size: 13px;
            line-height: 1.6;
        }

        @media (max-width: 820px) {
            body {
                padding: 16px;
                place-items: stretch;
            }

            .error-shell {
                grid-template-columns: 1fr;
                min-height: calc(100vh - 32px);
            }

            .brand-panel {
                min-height: 260px;
                padding: 24px;
            }

            .status-list {
                display: none;
            }

            .content-panel {
                padding: 32px 24px;
            }
        }

        @media (max-width: 520px) {
            body {
                padding: 0;
            }

            .error-shell {
                min-height: 100vh;
                border: 0;
                border-radius: 0;
            }

            .actions {
                display: grid;
            }

            .quick-grid {
                grid-template-columns: 1fr;
            }

            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <main class="error-shell">
        <section class="brand-panel" aria-label="<?= esc($appName, 'attr') ?>">
            <div class="brand-lockup">
                <?php if ($brandIconUrl) : ?>
                    <img src="<?= esc($brandIconUrl, 'attr') ?>" alt="Logo <?= esc($appName) ?>" class="brand-logo">
                <?php else : ?>
                    <div class="brand-mark"><?= esc(strtoupper(substr($appName, 0, 1))) ?></div>
                <?php endif; ?>
                <div class="min-w-0">
                    <div class="brand-name"><?= esc($appName) ?></div>
                    <div class="brand-tagline"><?= esc($appTagline) ?></div>
                </div>
            </div>

            <div>
                <div class="error-code">404</div>
                <div class="error-code-caption">NusaPOS tidak menemukan halaman, nota, atau transaksi yang sedang dibuka.</div>
            </div>

            <div class="status-list">
                <div class="status-item"><i class="bi bi-cart-check"></i>Cek transaksi penjualan</div>
                <div class="status-item"><i class="bi bi-wrench-adjustable"></i>Cek transaksi service</div>
                <div class="status-item"><i class="bi bi-box-seam"></i>Kembali ke data barang</div>
            </div>
        </section>

        <section class="content-panel">
            <div class="eyebrow">Error 404</div>
            <h1>Halaman NusaPOS tidak ditemukan</h1>
            <p class="message"><?= nl2br(esc($messageText)) ?></p>

            <?php if ($isLoggedIn) : ?>
                <div class="quick-grid" aria-label="Shortcut NusaPOS">
                    <a class="quick-link" href="<?= site_url('admin/penjualan') ?>">
                        <i class="bi bi-cart-check"></i>
                        <span>Data Penjualan</span>
                    </a>
                    <a class="quick-link" href="<?= site_url('admin/service') ?>">
                        <i class="bi bi-wrench-adjustable"></i>
                        <span>Transaksi Service</span>
                    </a>
                    <a class="quick-link" href="<?= site_url('admin/barang') ?>">
                        <i class="bi bi-box-seam"></i>
                        <span>Data Barang</span>
                    </a>
                </div>
            <?php endif; ?>

            <div class="actions">
                <a class="btn btn-primary" href="<?= esc($primaryUrl, 'attr') ?>">
                    <i class="bi bi-house-door"></i><?= esc($primaryLabel) ?>
                </a>
                <button class="btn btn-light" type="button" data-fallback="<?= esc($primaryUrl, 'attr') ?>" onclick="history.length > 1 ? history.back() : window.location.href = this.dataset.fallback">
                    <i class="bi bi-arrow-left"></i>Kembali
                </button>
            </div>

            <div class="hint">
                Jika error ini muncul dari tombol di aplikasi, coba kembali ke daftar data terkait lalu buka ulang detail transaksinya.
            </div>

            <?php if ($technicalMessage !== '') : ?>
                <details class="technical-message">
                    <summary>Detail teknis</summary>
                    <?= nl2br(esc($technicalMessage)) ?>
                </details>
            <?php endif; ?>
        </section>
    </main>
</body>
</html>
