<!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 {
            --primary: #2563eb;
            --primary-dark: #1d4ed8;
            --primary-light: #3b82f6;
            --bg: #f8fafc;
            --surface: #ffffff;
            --text: #1e293b;
            --text-muted: #64748b;
            --border: #e2e8f0;
            --success: #10b981;
            --warning: #f59e0b;
        }

        html { height: 100%; }

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

        /* Header */
        .maint-header {
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 50%, var(--primary-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: 4px;
            background: linear-gradient(90deg, var(--primary-light), var(--primary-dark), var(--primary-light));
        }

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

        .maint-header__badge {
            display: inline-flex;
            align-items: center;
            gap: .5rem;
            font-size: .75rem;
            font-weight: 700;
            letter-spacing: .1em;
            text-transform: uppercase;
            color: rgba(255,255,255,.7);
            margin-bottom: 1rem;
        }
        .maint-dot {
            width: 8px; height: 8px;
            background: var(--warning);
            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: 2rem;
            font-weight: 700;
            color: #fff;
            line-height: 1.25;
            margin-bottom: .4rem;
        }
        .maint-header__subtitle {
            font-size: 1.1rem;
            color: rgba(255,255,255,.8);
        }

        /* Content card */
        .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: 12px;
            box-shadow: 0 4px 24px rgba(0,0,0,.08);
            padding: 2.5rem 2rem;
            text-align: center;
        }
        .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: #eff6ff;
            border-left: 3px solid var(--primary);
            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);
        }

        .maint-footer {
            text-align: center;
            padding: 1.5rem;
            font-size: .8rem;
            color: var(--text-muted);
        }

        @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(30,41,59,.7);"></div>
    <?php endif; ?>

    <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</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>
