<?php
require_once __DIR__ . '/auth_helper.php';

if (!check_installed()) { header('Location: install.php'); exit; }
$user = currentUser();
if (!$user) { header('Location: index.php#login'); exit; }

$siteName  = 'pk999.com';
$amount    = round((float)($_GET['amount'] ?? 0), 2);
$methodId  = (int)($_GET['method_id'] ?? 0);
$minAmount = 100;
$maxAmount = 50000;

if ($amount < $minAmount || $amount > $maxAmount) { header('Location: deposit.php'); exit; }

$methods = payment_methods_for_country($user['country_code']);
$method = null;
foreach ($methods as $m) {
    if ((int)($m['id'] ?? 0) === $methodId) { $method = $m; break; }
}
if (!$method && $methods) { $method = $methods[0]; $methodId = (int)$method['id']; }

$error = '';
if (!$method) { $error = 'No payment method is active right now.'; }

$label        = trim((string)($method['label'] ?? 'Payment'));
$provider     = trim((string)($method['provider'] ?? $label));
$wallet       = trim((string)($method['wallet_number'] ?? ''));
$instructions = trim((string)($method['instructions'] ?? ''));

$detectText = strtolower($label . ' ' . $provider . ' ' . ($method['method_key'] ?? ''));
$isNagad = strpos($detectText, 'nagad') !== false || strpos($detectText, 'নগদ') !== false;
$isBkash = strpos($detectText, 'bkash') !== false || strpos($detectText, 'b-kash') !== false || strpos($detectText, 'বিকাশ') !== false;

$bankName  = $isNagad ? 'NAGAD' : ($isBkash ? 'BKASH' : strtoupper($provider ?: $label));
$bankImg   = $isNagad ? 'banks/nagad.png' : ($isBkash ? 'banks/bKash.png' : 'banks/payment.png');
$bankTitle = $bankName . ' Deposit';
$bankColor = $isNagad ? '#e87722' : ($isBkash ? '#e2136e' : '#1a7d5a');
$bankGrad  = $isNagad ? 'linear-gradient(135deg,#e87722 0%,#c05a10 100%)' : ($isBkash ? 'linear-gradient(135deg,#e2136e 0%,#9c0d4a 100%)' : 'linear-gradient(135deg,#1a7d5a 0%,#0d4f38 100%)');

if ($instructions === '') {
    $instructions = 'এই ' . $bankName . ' নাম্বারে শুধুমাত্র ক্যাশআউট গ্রহণ করা হয়';
}

$amountText = number_format($amount, 2, '.', ',');
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
<title>Payment — <?= e($siteName) ?></title>
<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=Noto+Sans+Bengali:wght@400;500;600;700;800;900&family=Sora:wght@300;400;500;600;700;800&family=DM+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
/* ── RESET & BASE ── */
*,*::before,*::after{box-sizing:border-box;-webkit-tap-highlight-color:transparent;margin:0;padding:0}
:root{
  --ink:       #0a0a0f;
  --ink2:      #1c1c27;
  --surface:   #f4f3f0;
  --card:      #ffffff;
  --border:    #e0ddd8;
  --muted:     #8a8680;
  --gold:      #c9a84c;
  --gold2:     #e8c96a;
  --red:       #c93c38;
  --red-soft:  #fdf1f1;
  --green:     #1a7d5a;
  --success:   #e6f9f1;
  --radius:    14px;
  --bank-color: <?= $bankColor ?>;
  --bank-grad:  <?= $bankGrad ?>;
  --transition: cubic-bezier(.4,0,.2,1);
}
html,body{min-height:100%;background:var(--surface);font-family:'Noto Sans Bengali','Sora',sans-serif;color:var(--ink);overscroll-behavior:none;-webkit-font-smoothing:antialiased}
body{display:flex;justify-content:center;align-items:flex-start}

/* ── PAGE SHELL ── */
.page{width:100%;max-width:520px;min-height:100vh;position:relative;overflow:hidden}

/* ── HERO HEADER ── */
.hero{
  position:relative;
  background:var(--ink2);
  padding:28px 28px 100px;
  overflow:hidden;
  isolation:isolate;
}
.hero::before{
  content:'';
  position:absolute;
  inset:0;
  background:
    radial-gradient(ellipse 80% 60% at 10% 110%, rgba(201,168,76,.18) 0%, transparent 60%),
    radial-gradient(ellipse 50% 40% at 90% -20%, rgba(201,168,76,.10) 0%, transparent 55%);
  pointer-events:none;
}
/* animated orbs */
.hero-orb{
  position:absolute;
  border-radius:50%;
  pointer-events:none;
  animation:orb-drift 8s ease-in-out infinite alternate;
}
.hero-orb-1{
  width:220px;height:220px;
  right:-60px;top:-60px;
  background:radial-gradient(circle, rgba(201,168,76,.15) 0%, transparent 70%);
  animation-delay:0s;
}
.hero-orb-2{
  width:160px;height:160px;
  left:-40px;bottom:0;
  background:radial-gradient(circle, rgba(201,168,76,.08) 0%, transparent 70%);
  animation-delay:-3s;
}
@keyframes orb-drift{
  from{transform:translate(0,0) scale(1)}
  to{transform:translate(12px,-18px) scale(1.08)}
}

.hero-top{display:flex;align-items:flex-start;justify-content:space-between;margin-bottom:28px;position:relative;z-index:1}
.brand{display:flex;flex-direction:column;gap:4px}
.brand-name{font-family:'Sora',sans-serif;font-size:11px;font-weight:600;letter-spacing:4px;text-transform:uppercase;color:var(--gold);opacity:.85}
.brand-tag{font-family:'Sora',sans-serif;font-size:19px;font-weight:800;color:#fff;letter-spacing:-.3px;line-height:1}

.lang-pill{display:inline-flex;background:rgba(255,255,255,.08);border:1px solid rgba(255,255,255,.14);border-radius:30px;overflow:hidden;backdrop-filter:blur(8px)}
.lang-pill button{
  height:30px;padding:0 14px;border:0;background:transparent;
  font-family:'Sora',sans-serif;font-size:11px;font-weight:600;
  letter-spacing:.5px;color:rgba(255,255,255,.55);cursor:pointer;
  transition:all .2s var(--transition);
}
.lang-pill button.active{background:rgba(255,255,255,.14);color:#fff;border-radius:30px}

/* amount display */
.hero-amount{position:relative;z-index:1}
.amount-eyebrow{font-family:'Sora',sans-serif;font-size:10px;font-weight:500;letter-spacing:3.5px;text-transform:uppercase;color:rgba(255,255,255,.4);margin-bottom:6px}
.amount-value{
  font-family:'Sora',sans-serif;
  font-size:42px;
  font-weight:800;
  color:#fff;
  letter-spacing:-1.5px;
  line-height:1;
  display:flex;align-items:baseline;gap:8px;
}
.amount-currency{font-size:18px;font-weight:600;color:var(--gold);letter-spacing:1px;opacity:.9;align-self:center}
.amount-warning{
  margin-top:12px;
  display:inline-flex;align-items:center;gap:7px;
  background:rgba(201,168,76,.12);
  border:1px solid rgba(201,168,76,.25);
  border-radius:30px;
  padding:5px 12px;
}
.amount-warning-dot{width:5px;height:5px;border-radius:50%;background:var(--gold);flex:0 0 auto;animation:pulse-dot 1.8s ease infinite}
@keyframes pulse-dot{0%,100%{opacity:1;transform:scale(1)}50%{opacity:.4;transform:scale(.7)}}
.amount-warning-text{font-size:11.5px;font-weight:600;color:var(--gold);font-family:'Sora',sans-serif;opacity:.9}

/* ── CARD PANEL (overlaps hero) ── */
.card-panel{
  position:relative;
  background:var(--surface);
  border-radius:28px 28px 0 0;
  margin-top:-56px;
  min-height:calc(100vh - 200px);
  padding:0 0 40px;
  z-index:5;
}
.card-panel-inner{padding:28px 24px 0}

/* ── BANK CARD ── */
.bank-card{
  background:var(--bank-grad);
  border-radius:18px;
  padding:18px 20px;
  display:flex;align-items:center;gap:16px;
  box-shadow:0 8px 32px rgba(0,0,0,.18), 0 1px 0 rgba(255,255,255,.08) inset;
  margin-bottom:24px;
  position:relative;
  overflow:hidden;
  animation:slide-up .5s var(--transition) .1s both;
}
.bank-card::after{
  content:'';
  position:absolute;
  inset:0;
  background:linear-gradient(135deg, rgba(255,255,255,.12) 0%, transparent 60%);
  pointer-events:none;
}
@keyframes slide-up{from{opacity:0;transform:translateY(16px)}to{opacity:1;transform:none}}

.bank-logo-ring{
  width:56px;height:56px;flex:0 0 56px;
  border-radius:50%;
  background:rgba(255,255,255,.95);
  box-shadow:0 4px 14px rgba(0,0,0,.2);
  display:flex;align-items:center;justify-content:center;
  overflow:hidden;
  border:2px solid rgba(255,255,255,.5);
}
.bank-logo-ring img{width:100%;height:100%;object-fit:cover}
.bank-logo-fallback{display:none;font-family:'Sora',sans-serif;font-size:11px;font-weight:900;color:var(--bank-color);text-align:center}

.bank-info{flex:1;min-width:0}
.bank-info-label{font-family:'Sora',sans-serif;font-size:9.5px;font-weight:600;letter-spacing:3px;text-transform:uppercase;color:rgba(255,255,255,.6);margin-bottom:3px}
.bank-info-name{font-family:'Sora',sans-serif;font-size:22px;font-weight:800;color:#fff;letter-spacing:-.3px;line-height:1}

.bank-badge{
  background:rgba(255,255,255,.15);
  border:1px solid rgba(255,255,255,.25);
  border-radius:8px;
  padding:6px 10px;
  font-family:'Sora',sans-serif;
  font-size:9px;font-weight:700;
  letter-spacing:2px;text-transform:uppercase;
  color:#fff;
  backdrop-filter:blur(4px);
}

/* ── SECTION LABELS ── */
.section-label{
  font-family:'Sora',sans-serif;
  font-size:10px;font-weight:700;
  letter-spacing:3px;text-transform:uppercase;
  color:var(--muted);
  margin-bottom:8px;
  animation:slide-up .5s var(--transition) .2s both;
}
.section-label .req{color:var(--red);margin-left:2px}

/* ── WALLET FIELD ── */
.wallet-wrap{
  background:var(--card);
  border:1.5px solid var(--border);
  border-radius:var(--radius);
  display:flex;align-items:stretch;
  overflow:hidden;
  box-shadow:0 2px 12px rgba(0,0,0,.05);
  margin-bottom:8px;
  animation:slide-up .5s var(--transition) .25s both;
}
.wallet-input{
  flex:1;min-width:0;
  border:0;background:transparent;outline:0;
  font-family:'DM Mono','Sora',monospace;
  font-size:17px;font-weight:500;
  letter-spacing:1.5px;
  color:var(--ink);
  padding:18px 0 18px 18px;
}
.copy-btn{
  width:58px;flex:0 0 58px;
  border:0;background:transparent;
  border-left:1.5px solid var(--border);
  cursor:pointer;
  display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;
  color:var(--green);
  transition:background .2s;
}
.copy-btn:hover{background:#f0faf5}
.copy-btn:active{background:#e0f4ec}
.copy-icon{display:flex;flex-direction:column;align-items:center;justify-content:center}
.copy-icon svg{width:18px;height:18px}
.copy-label{font-family:'Sora',sans-serif;font-size:8.5px;font-weight:700;letter-spacing:1px;text-transform:uppercase;color:var(--green);opacity:.75}

.wallet-hint{
  font-family:'Noto Sans Bengali','Sora',sans-serif;
  font-size:12px;font-weight:500;
  color:var(--muted);line-height:1.55;
  margin-bottom:22px;
  animation:slide-up .5s var(--transition) .3s both;
}

/* ── TRX INPUT ── */
.trx-wrap{
  background:var(--card);
  border:2px solid var(--red);
  border-radius:var(--radius);
  overflow:hidden;
  box-shadow:0 0 0 4px rgba(201,60,56,.06);
  margin-bottom:6px;
  transition:box-shadow .2s;
  animation:slide-up .5s var(--transition) .35s both;
}
.trx-wrap:focus-within{box-shadow:0 0 0 5px rgba(201,60,56,.12)}
.trx-input{
  width:100%;border:0;background:transparent;outline:0;
  padding:19px 18px;
  font-family:'DM Mono','Sora',monospace;
  font-size:16px;font-weight:500;letter-spacing:.5px;
  color:var(--ink);
}
.trx-input::placeholder{
  font-family:'Noto Sans Bengali','Sora',sans-serif;
  font-size:14px;font-weight:400;letter-spacing:0;
  color:#c0bbb5;
}

/* ── NOTICE PILL ── */
.notice{
  display:flex;align-items:flex-start;gap:10px;
  background:rgba(201,60,56,.05);
  border:1px solid rgba(201,60,56,.15);
  border-radius:10px;
  padding:11px 13px;
  margin-bottom:26px;
  animation:slide-up .5s var(--transition) .4s both;
}
.notice-icon{font-size:14px;flex:0 0 auto;margin-top:1px}
.notice-text{font-size:12.5px;font-weight:600;color:var(--red);line-height:1.5;font-family:'Noto Sans Bengali','Sora',sans-serif}

/* ── SUBMIT BUTTON ── */
.submit-btn{
  width:100%;height:58px;
  border:0;
  background:var(--ink2);
  color:#fff;
  border-radius:var(--radius);
  font-family:'Sora',sans-serif;
  font-size:15px;font-weight:700;
  letter-spacing:.5px;
  cursor:pointer;
  position:relative;overflow:hidden;
  display:flex;align-items:center;justify-content:center;gap:10px;
  box-shadow:0 6px 24px rgba(10,10,15,.22), 0 1px 0 rgba(255,255,255,.07) inset;
  transition:transform .15s, box-shadow .15s, background .2s;
  animation:slide-up .5s var(--transition) .45s both;
}
.submit-btn::before{
  content:'';
  position:absolute;
  inset:0;
  background:linear-gradient(135deg, rgba(201,168,76,.18) 0%, transparent 60%);
  pointer-events:none;
}
.submit-btn:hover{background:#2a2a3a;box-shadow:0 8px 28px rgba(10,10,15,.3),0 1px 0 rgba(255,255,255,.1) inset}
.submit-btn:active{transform:scale(.98);box-shadow:0 3px 12px rgba(10,10,15,.2)}
.submit-btn:disabled{opacity:.45;cursor:not-allowed;transform:none}
.submit-btn-arrow{font-size:16px;transition:transform .2s}
.submit-btn:not(:disabled):hover .submit-btn-arrow{transform:translateX(4px)}

/* loading shimmer on button */
.submit-btn.loading::after{
  content:'';
  position:absolute;
  inset:0;
  background:linear-gradient(90deg, transparent 0%, rgba(255,255,255,.12) 50%, transparent 100%);
  background-size:200% 100%;
  animation:shimmer 1.2s linear infinite;
}
@keyframes shimmer{from{background-position:200% 0}to{background-position:-200% 0}}

/* ── MESSAGES ── */
.pay-message{
  display:none;margin:0 0 16px;
  border-radius:10px;
  padding:12px 14px;
  font-size:13px;font-weight:600;line-height:1.5;
  font-family:'Noto Sans Bengali','Sora',sans-serif;
  animation:slide-up .3s var(--transition) both;
}
.pay-message.ok{display:block;background:var(--success);color:#0d5c39;border:1px solid #b3e8d0}
.pay-message.err{display:block;background:var(--red-soft);color:var(--red);border:1px solid rgba(201,60,56,.2)}

/* ── DISCLAIMER ── */
.disclaimer{
  margin-top:24px;
  border-top:1px solid var(--border);
  padding-top:18px;
  animation:slide-up .5s var(--transition) .5s both;
}
.disclaimer-title{
  font-family:'Sora',sans-serif;
  font-size:10px;font-weight:700;letter-spacing:2.5px;text-transform:uppercase;
  color:var(--muted);margin-bottom:10px;
}
.disclaimer-item{
  display:flex;align-items:flex-start;gap:8px;
  margin-bottom:7px;
}
.disclaimer-item::before{content:'•';color:var(--bank-color);font-size:14px;line-height:1.4;flex:0 0 auto}
.disclaimer-text{font-size:12px;font-weight:500;color:var(--muted);line-height:1.55;font-family:'Noto Sans Bengali','Sora',sans-serif}
.disclaimer-text strong{color:#5a5755;font-weight:700}

/* ── MODAL ── */
.modal-layer{
  position:fixed;inset:0;
  background:rgba(10,10,15,.6);
  backdrop-filter:blur(8px);
  display:none;align-items:center;justify-content:center;
  z-index:100;padding:24px;
}
.modal-layer.show{display:flex}
.modal-card{
  width:min(380px,90vw);
  background:var(--card);
  border-radius:22px;
  padding:32px 28px 24px;
  box-shadow:0 24px 60px rgba(0,0,0,.3);
  animation:modal-in .3s var(--transition) both;
}
@keyframes modal-in{
  from{opacity:0;transform:scale(.9) translateY(20px)}
  to{opacity:1;transform:none}
}
.modal-icon{
  width:52px;height:52px;
  background:var(--success);
  border-radius:50%;
  display:flex;align-items:center;justify-content:center;
  margin:0 auto 16px;
  font-size:22px;
}
.modal-title{font-family:'Sora',sans-serif;font-size:20px;font-weight:800;text-align:center;color:var(--ink);margin-bottom:8px;letter-spacing:-.3px}
.modal-text{font-size:13.5px;font-weight:500;line-height:1.55;color:var(--muted);text-align:center;margin-bottom:24px}
.modal-btn{
  width:100%;height:50px;
  border:0;
  background:var(--ink2);
  color:#fff;
  border-radius:12px;
  font-family:'Sora',sans-serif;
  font-size:14px;font-weight:700;letter-spacing:.5px;
  cursor:pointer;
  transition:background .2s,transform .15s;
  box-shadow:0 4px 16px rgba(10,10,15,.18);
}
.modal-btn:hover{background:#2a2a3a}
.modal-btn:active{transform:scale(.98)}

/* ── ERROR CARD ── */
.error-card{
  margin:24px;padding:18px;
  border-radius:14px;
  background:var(--red-soft);
  border:1.5px solid rgba(201,60,56,.2);
  color:var(--red);
  font-size:14px;font-weight:700;line-height:1.5;
}

/* ── RESPONSIVE ── */
@media(max-width:430px){
  .hero{padding:22px 20px 90px}
  .brand-name{font-size:10px;letter-spacing:3px}
  .brand-tag{font-size:17px}
  .amount-value{font-size:34px}
  .card-panel-inner{padding:22px 18px 0}
  .bank-info-name{font-size:19px}
  .wallet-input{font-size:15px;padding:15px 0 15px 16px}
  .trx-input{padding:16px 14px;font-size:14px}
  .submit-btn{height:52px;font-size:14px}
  .modal-card{padding:26px 20px 18px}
}
</style>
</head>
<body>
<div class="page">

  <!-- HERO -->
  <div class="hero">
    <div class="hero-orb hero-orb-1"></div>
    <div class="hero-orb hero-orb-2"></div>

    <div class="hero-top">
      <div class="brand">
        <span class="brand-name">Pay Service</span>
        <span class="brand-tag"><?= e($siteName) ?></span>
      </div>
      <div class="lang-pill" aria-label="Language">
        <button type="button">EN</button>
        <button type="button" class="active">বাং</button>
      </div>
    </div>

    <div class="hero-amount">
      <div class="amount-eyebrow">Total Amount Due</div>
      <div class="amount-value">
        <span class="amount-currency">BDT</span>
        <?= e($amountText) ?>
      </div>
      <div class="amount-warning">
        <span class="amount-warning-dot"></span>
        <span class="amount-warning-text">কম বা বেশি ক্যাশআউট করবেন না</span>
      </div>
    </div>
  </div>

  <!-- CARD PANEL -->
  <div class="card-panel">
    <div class="card-panel-inner">

      <?php if($error): ?>
        <div class="error-card"><?= e($error) ?></div>
      <?php else: ?>

        <!-- BANK CARD -->
        <div class="bank-card">
          <div class="bank-logo-ring">
            <img src="<?= e($bankImg) ?>" alt="<?= e($bankName) ?>"
              onerror="this.style.display='none';this.nextElementSibling.style.display='flex';">
            <span class="bank-logo-fallback"><?= e($bankName) ?></span>
          </div>
          <div class="bank-info">
            <div class="bank-info-label">Payment via</div>
            <div class="bank-info-name"><?= e($bankTitle) ?></div>
          </div>
          <div class="bank-badge">ACTIVE</div>
        </div>

        <!-- WALLET NUMBER -->
        <div class="section-label">Wallet Number</div>
        <div class="wallet-wrap">
          <input class="wallet-input" id="walletNo" type="text" value="<?= e($wallet) ?>" readonly>
          <button class="copy-btn" type="button" onclick="copyWallet()" aria-label="Copy wallet number">
            <div class="copy-icon">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
                <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
              </svg>
            </div>
            <span class="copy-label">Copy</span>
          </button>
        </div>
        <div class="wallet-hint"><?= e($instructions) ?></div>

        <!-- NOTICE -->
        <div class="notice">
          <span class="notice-icon">⚠️</span>
          <span class="notice-text">আপনি যদি টাকার পরিমাণ পরিবর্তন করেন (BDT <?= e($amountText) ?>), আপনি ক্রেডিট পেতে সক্ষম হবেন না।</span>
        </div>

        <!-- FORM -->
        <form id="paymentForm">
          <input type="hidden" name="action" value="deposit_request">
          <input type="hidden" name="method_id" value="<?= (int)$methodId ?>">
          <input type="hidden" name="amount" value="<?= e(number_format($amount, 2, '.', '')) ?>">

          <div class="section-label">Transaction ID <span class="req">*</span></div>
          <div class="trx-wrap">
            <input class="trx-input" type="text" name="trxid"
              placeholder="TrxID অবশ্যই পূরণ করতে হবে!"
              required autocomplete="off">
          </div>

          <div class="pay-message" id="payMsg"></div>

          <button class="submit-btn" type="submit" id="confirmBtn">
            <span>নিশ্চিত করুন</span>
            <span class="submit-btn-arrow">→</span>
          </button>
        </form>

        <!-- DISCLAIMER -->
        <div class="disclaimer">
          <div class="disclaimer-title">Important Notes</div>
          <div class="disclaimer-item">
            <span class="disclaimer-text">লেনদেন আইডি সঠিকভাবে পূরণ করতে হবে, অন্যথায় <strong>জমা ব্যর্থ হবে।</strong></span>
          </div>
          <div class="disclaimer-item">
            <span class="disclaimer-text">শুধুমাত্র <?= e($bankName) ?> ওয়ালেট নাম্বারে ক্যাশ আউট করুন। অন্য কোনো নাম্বারে পাঠালে ফেরত পাওয়ার কোনো সম্ভাবনা নেই।</span>
          </div>
          <div class="disclaimer-item">
            <span class="disclaimer-text">BDT <?= e($amountText) ?> এর চেয়ে বেশি বা কম পাঠাবেন না।</span>
          </div>
        </div>

      <?php endif; ?>
    </div>
  </div><!-- /card-panel -->
</div><!-- /page -->

<!-- SUCCESS MODAL -->
<div class="modal-layer" id="okModal">
  <div class="modal-card">
    <div class="modal-icon">✓</div>
    <h2 class="modal-title">জমা সফল হয়েছে!</h2>
    <p class="modal-text" id="okText">আপনার আবেদন পাঠানো হয়েছে। অ্যাডমিনের অনুমোদনের জন্য অপেক্ষা করুন।</p>
    <button class="modal-btn" type="button" onclick="location.href='index.php#me'">ড্যাশবোর্ডে ফিরুন →</button>
  </div>
</div>

<script>
function showMsg(type, text) {
  var box = document.getElementById('payMsg');
  if (!box) return;
  box.className = 'pay-message ' + (type === 'ok' ? 'ok' : 'err');
  box.textContent = text;
  box.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
function showModal(text) {
  var modal = document.getElementById('okModal');
  var txt = document.getElementById('okText');
  if (txt && text) txt.textContent = text;
  if (modal) modal.classList.add('show');
}
function copyWallet() {
  var el = document.getElementById('walletNo');
  if (!el) return;
  var val = el.value || '';
  if (navigator.clipboard && navigator.clipboard.writeText) {
    navigator.clipboard.writeText(val).then(function () { showMsg('ok', '✓ Wallet number copied to clipboard'); });
  } else {
    el.focus(); el.select();
    try { document.execCommand('copy'); showMsg('ok', '✓ Wallet number copied'); } catch (e) { showMsg('err', 'Copy failed — please copy manually'); }
  }
}
var form = document.getElementById('paymentForm');
if (form) {
  form.addEventListener('submit', async function (e) {
    e.preventDefault();
    var btn = document.getElementById('confirmBtn');
    if (btn) { btn.disabled = true; btn.classList.add('loading'); btn.querySelector('span').textContent = 'জমা হচ্ছে...'; }
    showMsg('ok', 'আবেদন পাঠানো হচ্ছে...');
    try {
      var response = await fetch('action.php', { method: 'POST', body: new FormData(form) });
      var data = await response.json();
      if (data && data.success) {
        form.reset();
        showModal(data.message || undefined);
      } else {
        showMsg('err', (data && (data.error || data.message)) ? (data.error || data.message) : 'Request failed. Please try again.');
        if (btn) { btn.disabled = false; btn.classList.remove('loading'); btn.querySelector('span').textContent = 'নিশ্চিত করুন'; }
      }
    } catch (err) {
      showMsg('err', 'Server error. Please check your connection and try again.');
      if (btn) { btn.disabled = false; btn.classList.remove('loading'); btn.querySelector('span').textContent = 'নিশ্চিত করুন'; }
    }
  });
}
</script>
</body>
</html>
