:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #111827;
  --text-2: #4b5563;
  --muted: #6b7280;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --primary: #1f5eff;
  --primary-hover: #1848c9;
  --primary-soft: #e8efff;
  --success: #0f7a55;
  --success-soft: #e3f5ee;
  --warn: #92560a;
  --warn-soft: #fff4de;
  --danger: #c4312a;
  --danger-soft: #fdecea;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  overscroll-behavior: none;
  touch-action: pan-x pan-y;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", sans-serif;
  font-size: 16px;
  line-height: 1.65;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

img {
  -webkit-user-drag: none;
}

input,
textarea {
  -webkit-user-select: text;
  user-select: text;
}

.noscript {
  padding: 24px 16px;
  text-align: center;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
}

.screen {
  display: none;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(20px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}

.screen.is-active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.center-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.card {
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.eyebrow {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: 0.02em;
}

.title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  line-height: 1.4;
}

.text {
  margin: 0;
  color: var(--text-2);
}

.muted {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.center {
  text-align: center;
  align-items: center;
}

.card-centered,
.t-center {
  text-align: center;
}

.card-centered .steps,
.card-centered .tips,
.card-centered .info-list {
  text-align: left;
}

.card-centered .review-grid {
  justify-content: center;
}

.notice {
  margin: 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--primary-soft);
  color: #1e3a8a;
  font-size: 14.5px;
}

.notice-warn {
  background: var(--warn-soft);
  color: #6f4200;
}

.notice-danger {
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 600;
}

.form-error {
  margin: 0;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 14.5px;
}

.input {
  width: 100%;
  min-height: 52px;
  padding: 10px 14px;
  border: 1.5px solid var(--border-strong);
  border-radius: 12px;
  background: #ffffff;
  color: var(--text);
  font: inherit;
  font-size: 18px;
}

.input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(31, 94, 255, 0.16);
}

.input-code {
  text-align: center;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  padding-left: calc(14px + 0.3em);
}

.input-sid {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 0.08em;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 16px;
  border: 1.5px solid var(--border-strong);
  border-radius: 12px;
  background: #ffffff;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  text-align: center;
}

.btn:active {
  background: #f3f4f6;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-lg {
  min-height: 54px;
  font-size: 17px;
}

.btn-sm {
  min-height: 38px;
  padding: 4px 12px;
  font-size: 14.5px;
  border-radius: 10px;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.btn-primary:active {
  background: var(--primary-hover);
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-2);
}

.actions-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.steps {
  margin: 0;
  padding-left: 22px;
  color: var(--text-2);
}

.steps li {
  margin: 4px 0;
}

.identity {
  padding: 16px;
  border-radius: 12px;
  background: var(--primary-soft);
  text-align: center;
}

.identity-nick {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  color: #1e3a8a;
}

.identity-name {
  margin: 2px 0 0;
  font-size: 19px;
  font-weight: 600;
}

.identity-sid {
  margin: 2px 0 0;
  color: var(--text-2);
  letter-spacing: 0.05em;
}

.tips {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fafafa;
}

.tips-title {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-2);
}

.tips-list {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
  color: var(--text-2);
}

.spinner {
  width: 36px;
  height: 36px;
  margin: 0 auto;
  border: 4px solid #dbe3f5;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

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

.info-list {
  margin: 0;
  display: grid;
  gap: 6px;
}

.info-list div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.info-list dt {
  color: var(--muted);
}

.info-list dd {
  margin: 0;
  font-weight: 600;
  text-align: right;
}

.conn-ok {
  color: var(--success);
}

.conn-bad {
  color: var(--danger);
}

.progress {
  height: 8px;
  border-radius: 999px;
  background: #e5e7eb;
  overflow: hidden;
}

.progress-bar {
  width: 0;
  height: 100%;
  background: var(--primary);
  transition: width 0.3s ease;
}

.progress-bar.is-done {
  background: var(--success);
}

.done-mark {
  width: 64px;
  height: 64px;
  margin: 0 auto;
  border-radius: 50%;
  background: var(--success);
  position: relative;
}

.done-mark::after {
  content: "";
  position: absolute;
  left: 23px;
  top: 12px;
  width: 16px;
  height: 30px;
  border: solid #ffffff;
  border-width: 0 5px 5px 0;
  transform: rotate(45deg);
}

.screen-exam.is-active {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  padding: 0;
  background: #ffffff;
}

.exam-head {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: rgba(255, 255, 255, 0.97);
  border-bottom: 1px solid var(--border);
}

.exam-head-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex-wrap: wrap;
}

.exam-head-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.fs-ctl {
  display: inline-flex;
  gap: 4px;
  margin-right: 4px;
}

.fs-btn {
  min-width: 44px;
  padding: 4px 8px;
  font-weight: 700;
}

.exam-qnum {
  font-weight: 700;
  font-size: 17px;
  white-space: nowrap;
}

.save-badge {
  font-size: 12.5px;
  font-weight: 600;
  padding: 1px 9px;
  border-radius: 999px;
  white-space: nowrap;
  background: var(--success-soft);
  color: var(--success);
}

.save-badge.is-pending {
  background: var(--warn-soft);
  color: var(--warn);
}

.save-badge.is-offline {
  background: var(--danger-soft);
  color: var(--danger);
}

.exam-banner {
  padding: 8px 14px;
  background: var(--warn-soft);
  color: #6f4200;
  font-size: 14.5px;
  text-align: center;
}

.exam-body {
  flex: 1 1 auto;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 16px 16px 24px;
}

.exam-foot {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.97);
  border-top: 1px solid var(--border);
}

.nav-sheet {
  position: fixed;
  inset: 0;
  z-index: 7000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(17, 24, 39, 0.45);
}

.nav-sheet-panel {
  width: 100%;
  max-width: 560px;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  background: #ffffff;
  border-radius: 18px 18px 0 0;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.nav-sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-sheet-head .muted {
  flex: 1 1 auto;
}

.nav-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 8px;
}

.nav-cell {
  min-height: 48px;
  border: 1.5px solid var(--border-strong);
  border-radius: 10px;
  background: #ffffff;
  color: var(--text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.nav-cell.is-answered {
  background: var(--success-soft);
  border-color: #9fd9c1;
  color: var(--success);
}

.nav-cell.is-current {
  box-shadow: 0 0 0 3px var(--primary);
  border-color: var(--primary);
}

.nav-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 13px;
  color: var(--muted);
}

.dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-right: 5px;
  border-radius: 4px;
  vertical-align: -1px;
  border: 1.5px solid var(--border-strong);
}

.dot-answered {
  background: var(--success-soft);
  border-color: #9fd9c1;
}

.dot-current {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary);
}

.review-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 8px;
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px;
  background: rgba(17, 24, 39, 0.72);
}

.overlay-ack {
  z-index: 8100;
}

.overlay-message {
  z-index: 8200;
}

.overlay-paused {
  z-index: 8300;
  background: #1f2937;
}

.overlay-locked {
  z-index: 8400;
  background: #7f1d1d;
}

.overlay-fullscreen {
  z-index: 8350;
  background: #111827;
}

.overlay-card {
  width: 100%;
  max-width: 440px;
  padding: 24px 20px;
  border-radius: var(--radius);
  background: #ffffff;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}

.overlay-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
}

.overlay-text {
  margin: 0;
  font-size: 17px;
  color: var(--text-2);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.toast-root {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 8000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: calc(100% - 32px);
  max-width: 460px;
  pointer-events: none;
}

.toast {
  padding: 12px 16px;
  border-radius: 12px;
  background: #111827;
  color: #ffffff;
  font-size: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast-admin {
  background: #1e3a8a;
}

.toast-error {
  background: var(--danger);
}

@media (min-width: 768px) {
  .exam-body {
    padding: 24px 24px 32px;
  }
}

.copy-helper {
  position: fixed;
  left: -9999px;
  top: 0;
  opacity: 0;
}

@keyframes screen-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@keyframes ov-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes ov-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: none; }
}

@keyframes sheet-in {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

@keyframes lock-shake {
  0%, 100% { transform: translateX(0) rotate(0); }
  15% { transform: translateX(-7px) rotate(-6deg); }
  30% { transform: translateX(6px) rotate(5deg); }
  45% { transform: translateX(-5px) rotate(-4deg); }
  60% { transform: translateX(4px) rotate(3deg); }
  75% { transform: translateX(-2px) rotate(-1deg); }
}

@keyframes shackle-close {
  from { transform: translateY(-9px); }
  60% { transform: translateY(2px); }
  to { transform: translateY(0); }
}

@keyframes shackle-open {
  from { transform: translateY(0) rotate(0); }
  to { transform: translateY(-9px) rotate(-28deg); }
}

@keyframes lock-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(254, 202, 202, 0.55); }
  50% { box-shadow: 0 0 0 16px rgba(254, 202, 202, 0); }
}

@keyframes unlock-pop {
  0% { transform: scale(0.94); }
  50% { transform: scale(1.03); }
  100% { transform: scale(1); }
}

@keyframes continue-pulse {
  0% { box-shadow: 0 0 0 0 rgba(15, 122, 85, 0.55); }
  70% { box-shadow: 0 0 0 18px rgba(15, 122, 85, 0); }
  100% { box-shadow: 0 0 0 0 rgba(15, 122, 85, 0); }
}

@keyframes continue-nudge {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.screen.is-active {
  animation: screen-in 0.28s ease-out;
}

.screen-exam.is-active {
  animation: none;
}

.overlay:not([hidden]) {
  animation: ov-in 0.22s ease-out;
}

.overlay:not([hidden]) .overlay-card {
  animation: card-in 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.overlay.is-leaving {
  animation: ov-out 0.2s ease-in forwards;
  pointer-events: none;
}

.nav-sheet:not([hidden]) {
  animation: ov-in 0.2s ease-out;
}

.nav-sheet:not([hidden]) .nav-sheet-panel {
  animation: sheet-in 0.26s ease-out;
}

.exam-banner:not([hidden]) {
  animation: screen-in 0.3s ease-out;
}

.toast {
  transform: translateY(12px) scale(0.97);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast.is-visible {
  transform: none;
}

.lock-card {
  align-items: center;
}

.lock-icon {
  position: relative;
  width: 64px;
  height: 70px;
  margin: 2px auto 4px;
}

.lock-shackle {
  position: absolute;
  left: 14px;
  top: 2px;
  width: 36px;
  height: 34px;
  border: 7px solid #b91c1c;
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  transform-origin: 90% 100%;
}

.lock-body {
  position: absolute;
  left: 4px;
  bottom: 0;
  width: 56px;
  height: 40px;
  border-radius: 10px;
  background: #b91c1c;
}

.lock-body::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 11px;
  width: 8px;
  height: 16px;
  margin-left: -4px;
  border-radius: 4px;
  background: #fecaca;
}

.overlay-locked:not([hidden]) .lock-icon {
  animation: lock-shake 0.6s ease-in-out 0.25s;
}

.overlay-locked:not([hidden]) .lock-shackle {
  animation: shackle-close 0.45s ease-out 0.1s both;
}

.overlay-locked:not([hidden]):not(.is-unlocked) .overlay-card {
  animation: card-in 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2), lock-glow 2.4s ease-out 0.6s infinite;
}

.overlay-locked {
  transition: background-color 0.4s ease;
}

.overlay-locked.is-unlocked {
  background: #065f46;
}

.overlay-locked.is-unlocked .lock-shackle {
  border-color: var(--success);
  transform: translateY(-9px) rotate(-28deg);
  animation: none;
}

.overlay-locked.is-unlocked.is-unlocking .lock-shackle {
  animation: shackle-open 0.5s ease-out both;
}

.overlay-locked.is-unlocked .lock-body {
  background: var(--success);
}

.overlay-locked.is-unlocked .lock-body::after {
  background: #d1fae5;
}

.overlay-locked.is-unlocked .lock-icon {
  animation: none;
}

.overlay-locked.is-unlocked .overlay-title {
  color: var(--success);
  font-size: 26px;
}

.overlay-locked.is-unlocking .overlay-card {
  animation: unlock-pop 0.45s ease-out;
}

.btn-continue {
  width: 100%;
  min-height: 64px;
  margin-top: 6px;
  padding: 14px 18px;
  border: 0;
  border-radius: 14px;
  background: var(--success);
  color: #ffffff;
  font: inherit;
  font-size: 21px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  animation: continue-pulse 1.6s ease-out infinite, continue-nudge 1.6s ease-in-out infinite;
}

.btn-continue:active {
  background: #0b5e41;
  transform: scale(0.98);
}

@media (prefers-reduced-motion: reduce) {
  .screen.is-active,
  .overlay:not([hidden]),
  .overlay:not([hidden]) .overlay-card,
  .overlay-locked:not([hidden]):not(.is-unlocked) .overlay-card,
  .overlay-locked:not([hidden]) .lock-icon,
  .overlay-locked:not([hidden]) .lock-shackle,
  .overlay-locked.is-unlocking .overlay-card,
  .overlay-locked.is-unlocked.is-unlocking .lock-shackle,
  .nav-sheet:not([hidden]),
  .nav-sheet:not([hidden]) .nav-sheet-panel,
  .exam-banner:not([hidden]),
  .btn-continue {
    animation: none;
  }
}
