function initiateTopup() {
  const amount = Number(document.getElementById("amount").value);
  const method = document.getElementById("method").value;

  if (!amount || amount < 1000) {
    alert("Enter a valid amount (minimum UGX 1,000).");
    return;
  }

  if (!method) {
    alert("Select a payment method.");
    return;
  }

  const user = JSON.parse(localStorage.getItem("flowpesa_onboarding"));

  if (!user || user.account_status !== "active") {
    alert("Session expired.");
    return;
  }

  // ---- MOCK PSP INITIATION ----
  console.log("Top-up initiated:", {
    user: user.phone,
    amount,
    method
  });

  // ---- MOCK LEDGER UPDATE ----
  const tx = {
    type: "topup",
    direction: "credit",
    amount,
    reference: method.toUpperCase() + " Top-Up",
    status: "pending",
    date: "Just now"
  };

  let txs = JSON.parse(localStorage.getItem("flowpesa_transactions")) || [];
  txs.unshift(tx);
  localStorage.setItem("flowpesa_transactions", JSON.stringify(txs));

  alert("Mobile money prompt sent. Complete payment to finish top-up.");

  window.location.href = "transactions.html";
}
