<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            min-height: 100vh; display: flex; align-items: center; justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #0a0a0a; color: #e0e0e0; padding: 20px;
        }
        .card {
            background: rgba(255,255,255,0.04); border: 1px solid rgba(255,255,255,0.08);
            border-radius: 16px; padding: 44px; max-width: 520px; width: 100%;
            box-shadow: 0 4px 24px rgba(0,0,0,0.5);
        }
        .card h1 {
            font-size: 24px; text-align: center; margin-bottom: 6px; color: #fff;
        }
        .subtitle { text-align: center; color: #666; font-size: 13px; margin-bottom: 28px; }
        .form-group { margin-bottom: 18px; }
        .form-group label { display: block; font-size: 12px; font-weight: 600; color: #888; margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.5px; }
        .form-group input {
            width: 100%; padding: 12px 14px; border: 1px solid rgba(255,255,255,0.1);
            border-radius: 8px; background: rgba(0,0,0,0.5); color: #fff;
            font-size: 14px; transition: border-color 0.2s; outline: none;
            font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
        }
        .form-group input:focus { border-color: #4a9eff; }
        .form-group input[readonly] { opacity: 0.5; cursor: not-allowed; }
        .form-group small { display: block; margin-top: 3px; font-size: 11px; color: #555; }
        .btn {
            width: 100%; padding: 14px; border: none; border-radius: 10px;
            color: #fff; font-size: 15px; font-weight: 600; cursor: pointer;
            transition: all 0.2s; margin-top: 6px;
        }
        .btn-primary { background: #2563eb; }
        .btn-primary:hover { background: #1d4ed8; }
        .btn-danger { background: #dc2626; }
        .btn-danger:hover { background: #b91c1c; }
        .btn:disabled { opacity: 0.5; cursor: not-allowed; }
        .alert {
            padding: 12px 16px; border-radius: 8px; margin-bottom: 18px;
            font-size: 13px; line-height: 1.5;
        }
        .alert-error { background: rgba(220,38,38,0.12); border: 1px solid rgba(220,38,38,0.25); color: #f87171; }
        .alert-success { background: rgba(34,197,94,0.12); border: 1px solid rgba(34,197,94,0.25); color: #4ade80; }
        .already { text-align: center; padding: 14px; background: rgba(234,179,8,0.1);
            border: 1px solid rgba(234,179,8,0.2); border-radius: 8px;
            margin-bottom: 18px; color: #eab308; font-size: 13px; }
        .post-install { margin-top: 20px; padding: 14px; background: rgba(255,255,255,0.03);
            border-radius: 8px; border: 1px solid rgba(255,255,255,0.06); }
        .post-install h3 { font-size: 13px; color: #eab308; margin-bottom: 6px; }
        .post-install p { font-size: 12px; color: #666; line-height: 1.6; }
        .post-install code { background: rgba(0,0,0,0.4); padding: 1px 5px; border-radius: 3px; font-size: 11px; color: #aaa; }
        a.dash-link {
            display: inline-block; margin-top: 14px; padding: 10px 28px;
            background: #16a34a; color: #fff; text-decoration: none;
            border-radius: 8px; font-weight: 600; font-size: 13px;
        }
        .icon { text-align: center; font-size: 40px; margin-bottom: 12px; }
        .divider { border: none; border-top: 1px solid rgba(255,255,255,0.06); margin: 24px 0; }
        .toggle-link { text-align: center; margin-top: 16px; }
        .toggle-link a { color: #4a9eff; font-size: 12px; text-decoration: none; }
        .toggle-link a:hover { text-decoration: underline; }
    </style>
</head>
<body>
    <div class="card">
        <div class="icon">🔐</div>
        <h1>{{ title }}</h1>
        <p class="subtitle">Secure license activation system</p>

        {% if error == 1 %}
        <div class="alert alert-error">{{ errorText }}</div>
        {% endif %}

        {% if success == 1 %}
        <div class="alert alert-success">{{ successText }}</div>

        {% if not data.showResetForm %}
        <div class="post-install">
            <h3>⚠️ Post-Installation Security</h3>
            <p>Delete the installer after successful activation:<br><code>app/controller/install.php</code></p>
            <p style="margin-top: 10px;"><a href="{{ site.url }}" class="dash-link">Go to Panel →</a></p>
        </div>
        {% endif %}

        {% elseif data.showResetForm %}
        {# RESET TOKEN FORM #}
        <form method="POST" id="resetForm">
            <div class="form-group">
                <label>License Key *</label>
                <input type="text" name="license_key_reset" placeholder="Enter your license key" required>
                <small>The license key from your original purchase</small>
            </div>
            <div class="form-group">
                <label>Reset Token *</label>
                <input type="text" name="reset_token" placeholder="Enter one-time reset token" required autofocus>
                <small>Get this token from your license provider's dashboard</small>
            </div>
            <div class="form-group">
                <label>API Server URL</label>
                <input type="url" name="api_url" value="{{ data.formData.api_url }}" required>
            </div>
            <button type="submit" class="btn btn-danger" id="resetBtn">🔄 Reset License</button>
        </form>
        <div class="toggle-link"><a href="?">← Back to Activation</a></div>
        <script>
        document.getElementById('resetForm').addEventListener('submit', function(){
            var b = document.getElementById('resetBtn'); b.textContent='⏳ Validating reset...'; b.disabled=true;
        });
        </script>

        {% else %}
        {# ACTIVATION FORM #}
        {% if data.alreadyInstalled %}
        <div class="already">⚠️ A license is already active. Re-activating will overwrite the current license.</div>
        {% endif %}

        <form method="POST" id="installForm">
            <div class="form-group">
                <label>License Key *</label>
                <input type="text" name="license_key" value="{{ data.formData.license_key }}" placeholder="e.g. LIC-20250218-A1B2C3D4" required autofocus>
                <small>Enter the license key from your purchase</small>
            </div>
            <div class="form-group">
                <label>API Server URL</label>
                <input type="url" name="api_url" value="{{ data.formData.api_url }}" required>
                <small>Do not change unless instructed</small>
            </div>
            <div class="form-group">
                <label>Product</label>
                <input type="text" name="product_slug" placeholder="product-slug" required>
            </div>
            <button type="submit" class="btn btn-primary" id="submitBtn">🚀 Activate License</button>
        </form>
        <div class="toggle-link"><a href="?reset=1" onclick="event.preventDefault();document.querySelector('.card h1').textContent='License Reset';document.getElementById('installForm').style.display='none';document.getElementById('resetSection').style.display='block';">Have a reset token?</a></div>

        {# Hidden reset form that shows on click #}
        <div id="resetSection" style="display:none;">
            <hr class="divider">
            <form method="POST" id="resetForm2">
                <div class="form-group">
                    <label>License Key *</label>
                    <input type="text" name="license_key_reset" placeholder="Enter your license key" required>
                    <small>The license key from your original purchase</small>
                </div>
                <div class="form-group">
                    <label>Reset Token *</label>
                    <input type="text" name="reset_token" placeholder="Enter one-time reset token" required>
                    <small>Get this from your license provider</small>
                </div>
                <div class="form-group">
                    <label>API Server URL</label>
                    <input type="url" name="api_url" value="{{ data.formData.api_url }}" required>
                </div>
                <button type="submit" class="btn btn-danger">🔄 Reset License</button>
            </form>
        </div>

        <script>
        document.getElementById('installForm').addEventListener('submit', function(){
            var b = document.getElementById('submitBtn'); b.textContent='⏳ Verifying...'; b.disabled=true;
        });
        </script>
        {% endif %}
    </div>
</body>
</html>
