function showLoader() {
  // if already exists, do nothing
  if (document.getElementById("K_Global_Loader")) return;

  const overlay = document.createElement("div");
  overlay.id = "K_Global_Loader";
  overlay.style.position = "fixed";
  overlay.style.top = "0";
  overlay.style.left = "0";
  overlay.style.width = "100vw";
  overlay.style.height = "100vh";
  overlay.style.background = "rgba(255, 255, 255, 0.85)";
  overlay.style.display = "flex";
  overlay.style.flexDirection = "column";
  overlay.style.alignItems = "center";
  overlay.style.justifyContent = "center";
  overlay.style.zIndex = "99999"; // high enough to silence any div trying to be clever
  overlay.style.gap = "14px";

  // spinner
  const spinner = document.createElement("div");
  spinner.style.width = "60px";
  spinner.style.height = "60px";
  spinner.style.border = "6px solid rgba(0,0,0,0.15)";
  spinner.style.borderTop = "6px solid #FFC000";
  spinner.style.borderRadius = "50%";
  spinner.style.animation = "spin 1s linear infinite";
  overlay.appendChild(spinner);

  // persian text
  const text = document.createElement("div");
  text.textContent = "در حال بارگذاری داده‌ها...";
  text.style.fontSize = "18px";
  text.style.color = "#333";
  overlay.appendChild(text);

  document.body.appendChild(overlay);

  // add CSS (only once)
  if (!document.getElementById("loader-spin-style")) {
    const style = document.createElement("style");
    style.id = "loader-spin-style";
    style.textContent = `
      input[type=number][id*=K_Grid_Input] {
        -moz-appearance: textfield;
      }
      input[type=number][id*=K_Grid_Input]::-webkit-outer-spin-button,
      input[type=number][id*=K_Grid_Input]::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
      }
      @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
      }
    `;
    document.head.appendChild(style);
  }

  return overlay;
}

function hideLoader() {
  const loader = document.getElementById("K_Global_Loader");
  if (loader) loader.remove();
}
