<!DOCTYPE html>
<html lang="<?= htmlspecialchars($config['locale'] ?? 'en', ENT_QUOTES, 'UTF-8') ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <title><?php
        $esc = fn(string $v): string => htmlspecialchars($v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        echo $esc($config['title'] ?? $mainTitle ?? 'Maintenance');
    ?></title>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

        :root {
            --navy: #1e3a5f;
            --navy-dark: #0d2240;
            --navy-light: #2a4f7a;
            --gold: #c5a55a;
            --gold-light: #d4b96e;
            --bg: #f9fafb;
            --surface: #ffffff;
            --text: #1a202c;
            --text-muted: #64748b;
            --border: #e2e8f0;
            --success: #10b981;
            --warning: #f59e0b;
        }

        html { height: 100%; }

        body {
            min-height: 100%;
            margin: 0;
            font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
            color: var(--text);
            background: var(--bg);
            display: flex;
            flex-direction: column;
            -webkit-font-smoothing: antialiased;
        }

        /* Top gold bar */
        .maint-topbar {
            background: var(--navy-dark);
            padding: .4rem 0;
            border-bottom: 2px solid var(--gold);
            text-align: center;
        }
        .maint-topbar__text {
            color: rgba(255,255,255,.6);
            font-size: .75rem;
            letter-spacing: .06em;
            text-transform: uppercase;
            font-weight: 600;
        }

        /* Header */
        .maint-header {
            background: linear-gradient(135deg, var(--navy-dark) 0%, var(--navy) 50%, var(--navy-light) 100%);
            padding: 3rem 1.5rem 2.5rem;
            text-align: center;
            position: relative;
        }
        .maint-header::after {
            content: '';
            position: absolute;
            bottom: 0; left: 0; right: 0;
            height: 3px;
            background: linear-gradient(90deg, transparent, var(--gold), transparent);
        }

        .maint-header__logo {
            max-width: 180px;
            height: auto;
            margin: 0 auto 1.5rem;
            filter: brightness(0) invert(1) drop-shadow(0 2px 6px rgba(0,0,0,.2));
        }

        .maint-header__badge {
            display: inline-flex;
            align-items: center;
            gap: .5rem;
            font-size: .75rem;
            font-weight: 700;
            letter-spacing: .1em;
            text-transform: uppercase;
            color: var(--gold-light);
            margin-bottom: 1rem;
        }
        .maint-dot {
            width: 8px; height: 8px;
            background: var(--gold);
            border-radius: 50%;
            animation: pulse 1.8s ease-in-out infinite;
        }
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: .35; }
        }

        .maint-header__title {
            font-size: 2.1rem;
            font-weight: 700;
            color: #fff;
            line-height: 1.2;
            margin-bottom: .4rem;
        }
        .maint-header__subtitle {
            font-size: 1.1rem;
            color: rgba(255,255,255,.8);
        }

        /* Content */
        .maint-wrapper {
            flex: 1;
            max-width: 560px;
            width: 100%;
            margin: -1.5rem auto 0;
            padding: 0 1.5rem 2rem;
            position: relative;
            z-index: 1;
        }
        .maint-card {
            background: var(--surface);
            border-radius: 10px;
            box-shadow: 0 4px 24px rgba(0,0,0,.08);
            padding: 2.5rem 2rem;
            text-align: center;
            border-top: 3px solid var(--gold);
        }
        .maint-message {
            font-size: 1rem;
            line-height: 1.7;
            color: var(--text-muted);
            margin-bottom: 2rem;
            white-space: pre-wrap;
            word-break: break-word;
        }

        .maint-section {
            margin-top: 1.25rem;
            padding-top: 1.25rem;
            border-top: 1px solid var(--border);
            text-align: left;
        }
        .maint-section__label {
            font-size: .75rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: .06em;
            color: var(--text-muted);
            margin-bottom: .5rem;
        }
        .maint-section__content {
            font-size: .95rem;
            line-height: 1.5;
            color: var(--text);
        }

        .maint-info--contact {
            background: #f0f4fa;
            border-left: 3px solid var(--navy);
            padding: .75rem 1rem;
            border-radius: 6px;
        }
        .maint-info--return {
            background: #f0fdf4;
            border-left: 3px solid var(--success);
            padding: .75rem 1rem;
            border-radius: 6px;
        }
        .maint-info--admin {
            background: #fef3c7;
            border-left: 3px solid var(--warning);
            padding: .75rem 1rem;
            border-radius: 6px;
            color: #92400e;
        }
        .maint-info--admin code {
            background: rgba(0,0,0,.06);
            padding: .1em .35em;
            border-radius: 3px;
        }

        .maint-status {
            margin-top: 1.5rem;
            padding-top: 1.5rem;
            border-top: 1px solid var(--border);
            display: flex;
            align-items: center;
            justify-content: center;
            gap: .5rem;
            font-size: .85rem;
            color: var(--text-muted);
        }

        /* Footer */
        .maint-footer {
            background: var(--navy-dark);
            color: rgba(255,255,255,.5);
            text-align: center;
            padding: 1.25rem;
            font-size: .8rem;
            border-top: 2px solid var(--gold);
        }

        @media (max-width: 640px) {
            .maint-header { padding: 2rem 1rem 2rem; }
            .maint-header__title { font-size: 1.6rem; }
            .maint-card { padding: 1.75rem 1.25rem; }
        }
    </style>
</head>
<body>
    <?php
    $showLogo        = $config['show_logo'] ?? true;
    $logoPath        = $config['logo_path'] ?? '';
    $customLogo      = $config['custom_logo'] ?? '';
    $backgroundImage = $config['background_image'] ?? '';
    $title           = $config['title'] ?? $mainTitle ?? 'Maintenance';
    $subtitle        = $config['subtitle'] ?? '';
    $message         = $config['message'] ?? $mainMessage ?? '';
    $contactInfo     = $config['contact_info'] ?? '';
    $returnInfo      = $config['return_info'] ?? '';
    $adminHint       = $config['admin_hint'] ?? false;
    $siteName        = $config['site_name'] ?? $mainSite ?? '';

    $effectiveLogo = '';
    if ($showLogo) {
        if (!empty($customLogo)) {
            $effectiveLogo = '/uploads/' . ltrim($customLogo, '/');
        } elseif (!empty($logoPath)) {
            $effectiveLogo = $logoPath;
        }
    }
    ?>

    <?php if (!empty($backgroundImage)): ?>
    <div style="position:fixed;inset:0;z-index:-2;background:url('/uploads/<?= $esc(ltrim($backgroundImage, '/')) ?>') center/cover no-repeat;"></div>
    <div style="position:fixed;inset:0;z-index:-1;background:rgba(13,34,64,.75);"></div>
    <?php endif; ?>

    <div class="maint-topbar">
        <span class="maint-topbar__text">
            <?= !empty($siteName) ? $esc($siteName) : 'Maintenance Mode' ?>
        </span>
    </div>

    <div class="maint-header">
        <?php if ($showLogo && !empty($effectiveLogo)): ?>
        <img src="<?= $esc($effectiveLogo) ?>" alt="Logo" class="maint-header__logo">
        <?php endif; ?>

        <?php if (!empty($siteName) && empty($effectiveLogo)): ?>
        <p class="maint-header__badge">
            <span class="maint-dot"></span>
            <?= $esc($siteName) ?>
        </p>
        <?php endif; ?>

        <h1 class="maint-header__title"><?= $esc($title) ?></h1>

        <?php if (!empty($subtitle)): ?>
        <p class="maint-header__subtitle"><?= $esc($subtitle) ?></p>
        <?php endif; ?>
    </div>

    <div class="maint-wrapper">
        <div class="maint-card">
            <?php if (!empty($message)): ?>
            <div class="maint-message"><?= nl2br($esc($message)) ?></div>
            <?php endif; ?>

            <?php if (!empty($contactInfo)): ?>
            <div class="maint-section">
                <div class="maint-section__label">Contact</div>
                <div class="maint-info--contact maint-section__content"><?= nl2br($esc($contactInfo)) ?></div>
            </div>
            <?php endif; ?>

            <?php if (!empty($returnInfo)): ?>
            <div class="maint-section">
                <div class="maint-section__label">Expected Return</div>
                <div class="maint-info--return maint-section__content"><?= $esc($returnInfo) ?></div>
            </div>
            <?php endif; ?>

            <?php if ($adminHint): ?>
            <div class="maint-section">
                <div class="maint-info--admin maint-section__content">
                    <strong>Administrators:</strong> You can still log in at <code><?= admin_url('login') ?></code>.
                </div>
            </div>
            <?php endif; ?>

            <div class="maint-status">
                <span class="maint-dot"></span>
                We'll be back shortly
            </div>
        </div>
    </div>

    <div class="maint-footer">
        &copy; <?= date('Y') ?> <?= !empty($siteName) ? $esc($siteName) : 'Website' ?>
    </div>
</body>
</html>
