/* ==========================================================================
   JLearn Online 2.0 — Assessment Screen Guard
   --------------------------------------------------------------------------
   Two jobs while a student is answering an assessment:

     1. Keep the display awake (Screen Wake Lock API).
     2. Ask for confirmation before a reload / refresh can throw the student
        out — including PWA standalone mode, where the browser's own
        "Leave site?" prompt is silently suppressed.

   Loaded by index.html and student-portal.html.
   ========================================================================== */

/* ── Suppress the browser's native pull-to-refresh while guarding ───────── */
html.jl-asg-active,
html.jl-asg-active body {
  overscroll-behavior-y: contain;
}

/* ── Confirmation dialog ────────────────────────────────────────────────── */
.asg-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(18px, env(safe-area-inset-top, 0px))
    max(18px, env(safe-area-inset-right, 0px))
    max(18px, env(safe-area-inset-bottom, 0px))
    max(18px, env(safe-area-inset-left, 0px));
  background: rgba(2, 6, 23, 0.74);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  overflow-y: auto;
  overscroll-behavior: contain;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.18s ease, visibility 0.18s ease;
  -webkit-tap-highlight-color: transparent;
}

.asg-overlay.is-open {
  opacity: 1;
  visibility: visible;
}

.asg-card {
  width: min(420px, 100%);
  margin: auto;
  padding: 24px 22px 18px;
  border-radius: 22px;
  border: 1px solid rgba(148, 163, 184, 0.22);
  background: linear-gradient(165deg, #151c2e 0%, #0b1120 100%);
  box-shadow: 0 28px 70px rgba(2, 6, 23, 0.68), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  text-align: center;
  transform: translateY(14px) scale(0.97);
  transition: transform 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.asg-overlay.is-open .asg-card {
  transform: translateY(0) scale(1);
}

.asg-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 14px;
  border-radius: 19px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f59e0b 0%, #f97316 100%);
  box-shadow: 0 12px 26px rgba(249, 115, 22, 0.36);
  color: #ffffff;
}

.asg-icon svg {
  width: 28px;
  height: 28px;
}

/* Slow spin on the circular arrow so the dialog feels "live" without noise */
.asg-icon svg .asg-spin {
  transform-origin: 12px 12px;
  animation: asg-spin 5.5s linear infinite;
}

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

.asg-title {
  margin: 0 0 8px;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 1.16rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: #f1f5f9;
}

.asg-desc {
  margin: 0 0 14px;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 0.88rem;
  line-height: 1.55;
  font-weight: 500;
  color: #9aa8bd;
}

.asg-desc strong {
  color: #e2e8f0;
  font-weight: 700;
}

.asg-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  margin-bottom: 18px;
}

.asg-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: #c7d2fe;
  background: rgba(99, 102, 241, 0.16);
  border: 1px solid rgba(129, 140, 248, 0.34);
  white-space: nowrap;
}

.asg-chip svg {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
}

.asg-chip--warn {
  color: #fecaca;
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(248, 113, 113, 0.38);
}

.asg-actions {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.asg-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 48px;
  padding: 12px 16px;
  border-radius: 13px;
  border: 1px solid transparent;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.92rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: transform 0.14s ease, box-shadow 0.18s ease, background 0.18s ease;
  -webkit-tap-highlight-color: transparent;
}

.asg-btn:active {
  transform: scale(0.985);
}

.asg-btn:focus-visible {
  outline: 3px solid rgba(129, 140, 248, 0.75);
  outline-offset: 2px;
}

.asg-btn--stay {
  color: #ffffff;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  box-shadow: 0 12px 26px rgba(79, 70, 229, 0.38);
}

.asg-btn--stay:hover {
  background: linear-gradient(135deg, #4338ca 0%, #6d28d9 100%);
}

.asg-btn--reload {
  color: #fca5a5;
  background: transparent;
  border-color: rgba(248, 113, 113, 0.42);
}

.asg-btn--reload:hover {
  background: rgba(239, 68, 68, 0.12);
}

.asg-hint {
  margin: 12px 0 0;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.7rem;
  font-weight: 600;
  color: #64748b;
}

.asg-hint kbd {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(148, 163, 184, 0.12);
  font-family: inherit;
  font-size: 0.68rem;
  font-weight: 700;
  color: #cbd5e1;
}

/* ── Pull-to-reload indicator (mobile) ─────────────────────────────────── */
.asg-pull {
  position: fixed;
  top: 0;
  left: 50%;
  z-index: 2147482000;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border-radius: 0 0 13px 13px;
  background: rgba(15, 23, 42, 0.94);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-top: 0;
  box-shadow: 0 8px 20px rgba(2, 6, 23, 0.45);
  color: #cbd5e1;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -64px);
  transition: opacity 0.14s ease;
  will-change: transform;
}

.asg-pull.is-armed {
  color: #fde68a;
  border-color: rgba(251, 191, 36, 0.42);
  background: rgba(69, 26, 3, 0.94);
}

.asg-pull-arrow {
  display: inline-flex;
  transition: transform 0.16s ease;
}

.asg-pull-arrow svg {
  width: 13px;
  height: 13px;
}

.asg-pull.is-armed .asg-pull-arrow {
  transform: rotate(180deg);
}

/* ── Light theme ───────────────────────────────────────────────────────── */
html[data-theme='light'] .asg-overlay {
  background: rgba(15, 23, 42, 0.42);
}

html[data-theme='light'] .asg-card {
  border-color: rgba(148, 163, 184, 0.34);
  background: linear-gradient(165deg, #ffffff 0%, #f8fafc 100%);
  box-shadow: 0 28px 70px rgba(15, 23, 42, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.9);
}

html[data-theme='light'] .asg-title {
  color: #0f172a;
}

html[data-theme='light'] .asg-desc {
  color: #52627a;
}

html[data-theme='light'] .asg-desc strong {
  color: #1e293b;
}

html[data-theme='light'] .asg-chip {
  color: #3730a3;
  background: rgba(99, 102, 241, 0.1);
  border-color: rgba(99, 102, 241, 0.3);
}

html[data-theme='light'] .asg-chip--warn {
  color: #b91c1c;
  background: rgba(239, 68, 68, 0.08);
  border-color: rgba(239, 68, 68, 0.28);
}

html[data-theme='light'] .asg-btn--reload {
  color: #b91c1c;
  border-color: rgba(239, 68, 68, 0.4);
}

html[data-theme='light'] .asg-btn--reload:hover {
  background: rgba(239, 68, 68, 0.07);
}

html[data-theme='light'] .asg-hint {
  color: #7c8ba3;
}

html[data-theme='light'] .asg-hint kbd {
  color: #475569;
  border-color: rgba(100, 116, 139, 0.32);
  background: rgba(100, 116, 139, 0.08);
}

html[data-theme='light'] .asg-pull {
  color: #334155;
  background: rgba(255, 255, 255, 0.97);
  border-color: rgba(148, 163, 184, 0.4);
}

html[data-theme='light'] .asg-pull.is-armed {
  color: #92400e;
  border-color: rgba(217, 119, 6, 0.5);
  background: rgba(254, 243, 199, 0.98);
}

/* ── Compact screens ───────────────────────────────────────────────────── */
@media (max-width: 380px) {
  .asg-card {
    padding: 20px 16px 14px;
    border-radius: 18px;
  }

  .asg-icon {
    width: 52px;
    height: 52px;
    border-radius: 16px;
    margin-bottom: 12px;
  }

  .asg-icon svg {
    width: 24px;
    height: 24px;
  }

  .asg-title {
    font-size: 1.06rem;
  }

  .asg-desc {
    font-size: 0.84rem;
  }
}

/* ── Landscape phones: keep both buttons reachable ─────────────────────── */
@media (max-height: 460px) and (orientation: landscape) {
  .asg-card {
    padding: 16px 18px 12px;
  }

  .asg-icon {
    width: 44px;
    height: 44px;
    border-radius: 14px;
    margin-bottom: 8px;
  }

  .asg-icon svg {
    width: 20px;
    height: 20px;
  }

  .asg-desc {
    margin-bottom: 10px;
  }

  .asg-chips {
    margin-bottom: 12px;
  }

  .asg-hint {
    display: none;
  }
}

/* ── Motion & contrast preferences ─────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .asg-overlay,
  .asg-card,
  .asg-pull,
  .asg-pull-arrow,
  .asg-icon svg .asg-spin {
    transition: none !important;
    animation: none !important;
  }
}
