document.getElementById("createPinBtn").addEventListener("click", () => {
  const pin = document.getElementById("pin").value.trim();
  const confirmPin = document.getElementById("confirmPin").value.trim();

  if (pin.length !== 6 || isNaN(pin)) {
    alert("PIN must be exactly 6 digits.");
    return;
  }

  if (pin !== confirmPin) {
    alert("PINs do not match.");
    return;
  }

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

  if (!onboarding || !onboarding.phone_verified || !onboarding.email_verified) {
    alert("Verification incomplete. Please restart onboarding.");
    return;
  }

  // NEVER store raw PIN in production
  onboarding.pin_set = true;
  onboarding.step = "pin_created";
  onboarding.account_status = "active";
  onboarding.kyc_level = 0;

  localStorage.setItem("flowpesa_onboarding", JSON.stringify(onboarding));

  // Redirect to dashboard (limited access)
  window.location.href = "dashboard.php";
});
