/*
 * UX3 fail-closed authentication bootstrap and navigation continuity.
 * The protected module remains covered until auth/profile state is explicit.
 */

/*
 * Same-origin module navigations keep the outgoing page image on screen while
 * the incoming document restores auth. The bootstrap releases this static
 * image only after the new page has an explicit authenticated/login/error state.
 */
@view-transition {
  navigation: auto;
}

html.auth-navigation-continuity-hold::view-transition-old(root) {
  z-index: 2;
  opacity: 1;
  animation: auth-navigation-continuity-hold 4s linear both;
}

html.auth-navigation-continuity-hold::view-transition-new(root) {
  z-index: 1;
  opacity: 0;
  animation: auth-navigation-continuity-new-hold 4s linear both;
}

@keyframes auth-navigation-continuity-hold {
  from,
  to {
    opacity: 1;
  }
}

@keyframes auth-navigation-continuity-new-hold {
  from,
  to {
    opacity: 0;
  }
}

html.auth-navigation-continuity-hold body {
  pointer-events: none !important;
}

.auth-navigation-indicator {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 2147482500;
  display: flex;
  align-items: center;
  max-width: min(360px, calc(100vw - 32px));
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.9);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.2);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.45;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  transform: translateX(-50%);
}

.auth-navigation-indicator::before {
  content: "";
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  margin-right: 8px;
  border-radius: 50%;
  background: #60a5fa;
  box-shadow: 0 0 0 4px rgba(96, 165, 250, 0.16);
}

/* Minimum fail-closed geometry even if a module stylesheet fails to load. */
.auth-overlay.auth-restoring,
.auth-overlay.auth-bootstrap-managed {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  box-sizing: border-box;
  min-width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: auto;
  background:
    radial-gradient(circle at 18% 18%, rgba(37, 99, 235, 0.14), transparent 28%),
    radial-gradient(circle at 82% 12%, rgba(147, 197, 253, 0.18), transparent 32%),
    linear-gradient(180deg, #f8fbff 0%, #eef4ff 100%);
}

/* First-paint fallback: available before the head script can discover the body. */
.auth-overlay.auth-restoring,
.auth-overlay.auth-bootstrap-managed.auth-state-load-error {
  align-items: center;
  justify-content: center;
  padding: 22px;
}

.auth-overlay.auth-restoring > .auth-card,
.auth-overlay.auth-bootstrap-managed > .auth-card {
  display: none;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}

.auth-overlay.auth-restoring:not(.auth-bootstrap-managed)::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30px;
  height: 30px;
  margin: -34px 0 0 -18px;
  border: 3px solid rgba(37, 99, 235, 0.18);
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: auth-restoring-spin 0.8s linear infinite;
}

.auth-overlay.auth-restoring:not(.auth-bootstrap-managed)::after {
  content: "正在打开页面…";
  position: absolute;
  top: calc(50% + 15px);
  left: 50%;
  width: min(320px, calc(100vw - 48px));
  transform: translateX(-50%);
  color: #334155;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.5;
  text-align: center;
  animation: auth-restoring-copy-failsafe 15s steps(1, end) forwards;
}

@keyframes auth-restoring-spin {
  to { transform: rotate(360deg); }
}

@keyframes auth-restoring-copy-failsafe {
  0% { content: "正在打开页面…"; }
  26.6667% { content: "网络较慢，仍在连接…"; }
  100% { content: "页面暂时无法加载，请刷新重试"; }
}

/* Injected as soon as #authOverlay appears during HTML parsing. */
.auth-recovery-card {
  display: none;
  box-sizing: border-box;
  width: min(340px, calc(100vw - 44px));
  min-height: 132px;
  padding: 24px 22px 20px;
  border: 1px solid rgba(255, 255, 255, 0.92);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 22px 60px rgba(15, 23, 42, 0.12);
  backdrop-filter: blur(18px) saturate(150%);
  -webkit-backdrop-filter: blur(18px) saturate(150%);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 13px;
  color: #334155;
  text-align: center;
}

.auth-overlay.auth-bootstrap-managed.auth-restoring > .auth-recovery-card,
.auth-overlay.auth-bootstrap-managed.auth-state-load-error > .auth-recovery-card {
  display: flex;
}

.auth-recovery-icon {
  position: relative;
  display: block;
  box-sizing: border-box;
  width: 30px;
  height: 30px;
  border: 3px solid rgba(37, 99, 235, 0.18);
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: auth-restoring-spin 0.8s linear infinite;
}

.auth-recovery-message {
  max-width: 100%;
  color: #334155;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.auth-recovery-refresh {
  min-width: 112px;
  min-height: 40px;
  margin-top: 2px;
  padding: 9px 18px;
  border: 0;
  border-radius: 12px;
  background: #2563eb;
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.auth-recovery-refresh:hover {
  background: #1d4ed8;
}

.auth-recovery-refresh:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.28);
  outline-offset: 3px;
}

.auth-recovery-refresh[hidden] {
  display: none;
}

.auth-overlay.auth-state-load-error > .auth-recovery-card {
  min-height: 156px;
}

.auth-overlay.auth-state-load-error .auth-recovery-icon {
  animation: none;
  border-color: #fecaca;
  background: #fef2f2;
}

.auth-overlay.auth-state-load-error .auth-recovery-icon::after {
  content: "!";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #b91c1c;
  font-size: 17px;
  font-weight: 800;
  line-height: 1;
}

/* Only explicit login/profile states may expose the original auth card. */
.auth-overlay.auth-bootstrap-managed.auth-state-login > .auth-card,
.auth-overlay.auth-bootstrap-managed.auth-state-profile > .auth-card {
  display: block;
  visibility: visible;
  opacity: 1;
  pointer-events: auto;
}

.auth-overlay.auth-state-login #authProfileBox {
  display: none !important;
}

.auth-overlay.auth-state-profile .auth-tabs,
.auth-overlay.auth-state-profile #loginForm,
.auth-overlay.auth-state-profile #registerForm {
  display: none !important;
}

.auth-overlay.auth-state-profile #authProfileBox {
  display: block !important;
}

@media (prefers-reduced-motion: reduce) {
  @view-transition {
    navigation: none;
  }

  .auth-overlay.auth-restoring:not(.auth-bootstrap-managed)::before,
  .auth-recovery-icon {
    animation: none;
    border-color: rgba(37, 99, 235, 0.28);
    background: radial-gradient(circle, #2563eb 0 4px, transparent 5px);
  }

  .auth-overlay.auth-restoring:not(.auth-bootstrap-managed)::after {
    /* Keep the discrete status fallback; it contains no spatial motion. */
    animation-name: auth-restoring-copy-failsafe;
  }
}

@media (max-width: 640px) {
  .auth-navigation-indicator {
    bottom: calc(82px + env(safe-area-inset-bottom, 0px));
    max-width: calc(100vw - 28px);
  }
}
