:root {
  --background: #050914;
  --panel: #0B1424;
  --primary: #168BFF;
  --secondary: #00D4FF;
  --text: #FFFFFF;
  --muted: #8293A8;
  --border: rgba(0, 160, 255, 0.25);
  --danger: #FF4D6A;
  --ok: #19F2A6;
  --glow: 0 0 18px rgba(22, 139, 255, .55), 0 0 42px rgba(0, 212, 255, .18);
  --font: "Rajdhani", "Orbitron", "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --mono: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(ellipse at 50% -10%, #0d2246 0%, var(--background) 55%) fixed, var(--background);
  color: var(--text); font-family: var(--font); letter-spacing: .02em; overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}
#bg { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; }
.scan { position: fixed; inset: 0; z-index: 1; pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(255,255,255,.025) 0 1px, transparent 1px 3px);
  mix-blend-mode: overlay; }
.scan::after { content: ""; position: absolute; left: 0; right: 0; height: 22vh; top: -22vh;
  background: linear-gradient(to bottom, transparent, rgba(0, 212, 255, .06), transparent); animation: sweep 7s linear infinite; }
.vignette { position: fixed; inset: 0; z-index: 1; pointer-events: none; background: radial-gradient(ellipse at center, transparent 55%, rgba(0,0,0,.65) 100%); }
@keyframes sweep { to { top: 100vh; } }

.shell { position: relative; z-index: 2; min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom)); gap: 22px; }
.brand { text-align: center; animation: logoIn 1.6s cubic-bezier(.2,.7,.2,1) both; }
.logo { font-size: clamp(34px, 6vw, 62px); font-weight: 800; letter-spacing: .18em; text-shadow: var(--glow); display: inline-flex; align-items: center; gap: .35em; }
.logo .dot { color: var(--secondary); }
.logo-mark { width: .9em; height: .9em; border: 2px solid var(--secondary); transform: rotate(45deg); box-shadow: var(--glow); position: relative; }
.logo-mark::after { content: ""; position: absolute; inset: 22%; background: var(--primary); }
.tagline { margin-top: 6px; color: var(--secondary); font-size: clamp(12px, 1.6vw, 15px); letter-spacing: .55em; opacity: .9; }
@keyframes logoIn { from { opacity: 0; transform: translateY(-10px) scale(.97); filter: blur(6px); } to { opacity: 1; transform: none; filter: none; } }

.steps { display: flex; gap: 6px; list-style: none; padding: 0; margin: 0; animation: fadeUp .9s .5s both; }
.steps li { font-size: 12px; letter-spacing: .2em; color: var(--muted); padding: 6px 12px; border: 1px solid var(--border); clip-path: polygon(8px 0, 100% 0, calc(100% - 8px) 100%, 0 100%); background: rgba(11, 20, 36, .6); }
.steps li span { color: var(--primary); margin-right: 6px; }
.steps li.done { color: var(--ok); border-color: rgba(25, 242, 166, .35); }
.steps li.active { color: var(--text); border-color: var(--secondary); box-shadow: var(--glow); }

.panel { width: min(440px, 100%); background: linear-gradient(180deg, rgba(11,20,36,.92), rgba(6,12,24,.94)); border: 1px solid var(--border);
  padding: 30px 30px 24px; position: relative; box-shadow: 0 30px 80px rgba(0,0,0,.55), inset 0 0 0 1px rgba(255,255,255,.02);
  clip-path: polygon(18px 0, 100% 0, 100% calc(100% - 18px), calc(100% - 18px) 100%, 0 100%, 0 18px); animation: fadeUp .9s .25s both; }
.panel::before { content: ""; position: absolute; inset: 0; pointer-events: none; border-top: 2px solid var(--primary); width: 38%; box-shadow: var(--glow); }
@keyframes fadeUp { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.stage { display: flex; flex-direction: column; gap: 14px; animation: stageIn .45s both; }
.stage[hidden] { display: none; }
@keyframes stageIn { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }
.title { margin: 0 0 4px; font-size: 22px; letter-spacing: .32em; text-align: center; color: var(--text); text-shadow: var(--glow); }
.sub { margin: -6px 0 0; text-align: center; color: var(--secondary); letter-spacing: .3em; font-size: 13px; }
.hint { margin: 0; color: var(--muted); font-size: 14px; text-align: center; line-height: 1.5; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; letter-spacing: .25em; color: var(--muted); }
.field input, .otp input { font: inherit; font-size: 17px; letter-spacing: .04em; color: var(--text); background: rgba(3, 8, 18, .85); border: 1px solid var(--border);
  padding: 12px 14px; outline: none; width: 100%; min-height: 48px; border-radius: 0; transition: border-color .2s, box-shadow .2s; }
.field input:focus, .otp input:focus { border-color: var(--secondary); box-shadow: 0 0 0 1px var(--secondary), var(--glow); }
.pw { position: relative; display: flex; }
.pw input { padding-right: 74px; }
.eye { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); font: inherit; font-size: 11px; letter-spacing: .18em; color: var(--secondary);
  background: transparent; border: 1px solid var(--border); padding: 7px 9px; cursor: pointer; min-height: 34px; }
.eye:focus-visible { outline: 2px solid var(--secondary); }

.btn { position: relative; font: inherit; font-weight: 700; letter-spacing: .32em; font-size: 15px; color: #fff; padding: 14px; min-height: 52px; cursor: pointer;
  border: 1px solid var(--primary); background: linear-gradient(90deg, rgba(22,139,255,.25), rgba(0,212,255,.18)); text-transform: uppercase;
  clip-path: polygon(12px 0, 100% 0, calc(100% - 12px) 100%, 0 100%); transition: box-shadow .2s, background .2s, transform .1s; }
.btn:hover:not(:disabled) { box-shadow: var(--glow); background: linear-gradient(90deg, rgba(22,139,255,.5), rgba(0,212,255,.35)); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn:focus-visible { outline: 2px solid var(--secondary); outline-offset: 2px; }
.spinner { display: none; position: absolute; right: 18px; top: 50%; width: 18px; height: 18px; margin-top: -9px; border: 2px solid rgba(255,255,255,.25); border-top-color: var(--secondary); border-radius: 50%; animation: spin .8s linear infinite; }
.btn.loading .spinner { display: block; }
.btn.loading .btn-label { opacity: .6; }
@keyframes spin { to { transform: rotate(360deg); } }

.error { margin: 0; color: var(--danger); font-size: 14px; text-align: center; min-height: 1em; }
.status-row { font-size: 11px; letter-spacing: .26em; color: var(--muted); display: flex; align-items: center; gap: 8px; justify-content: center; }
.status-row b { color: var(--text); font-weight: 600; }
.led { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); box-shadow: 0 0 6px currentColor; }
.led.ok { background: var(--ok); color: var(--ok); }
.led.lock { background: var(--danger); color: var(--danger); animation: blink 1.6s infinite; }
@keyframes blink { 50% { opacity: .35; } }
.link { background: none; border: 0; color: var(--secondary); font: inherit; font-size: 14px; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; padding: 6px; min-height: 36px; }
.link:focus-visible { outline: 2px solid var(--secondary); }
.turnstile { display: flex; justify-content: center; min-height: 65px; }

/* the realm: pixel game */
.game { gap: 10px; }
.hud { display: flex; justify-content: space-between; align-items: center; font-family: var(--mono); font-size: 13px; letter-spacing: .18em; color: var(--muted); }
.hud b { color: #ffcf3f; display: inline-block; min-width: 3ch; }
.hud b.pop { animation: pop .35s ease-out; }
@keyframes pop { 40% { transform: scale(1.5); color: #fff2a8; } }
.runes { display: inline-flex; gap: 5px; }
.runes i { width: 10px; height: 10px; border: 1px solid rgba(0,212,255,.25); transform: rotate(45deg); transition: background .15s, border-color .15s; }
.runes.awake i { border-color: var(--secondary); box-shadow: 0 0 6px rgba(0,212,255,.5); }
.runes i.on { background: var(--secondary); }
.runes.wrong i { animation: wrong .4s; }
@keyframes wrong { 30% { border-color: var(--danger); background: rgba(255,77,106,.6); } }
.runes.solved i { background: var(--ok); border-color: var(--ok); box-shadow: 0 0 8px rgba(25,242,166,.7); }
/* the game gets a bigger screen than the forms (the panel widens while the realm is shown; a body class, so older browsers too) */
body.in-game .panel { width: min(720px, 100%); }
.side { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.screen { border: 2px solid var(--border); box-shadow: var(--glow); background: #05060d; line-height: 0; }
.screen canvas { width: 100%; height: auto; aspect-ratio: 160 / 112; image-rendering: pixelated; image-rendering: crisp-edges; display: block; outline: none; }
.screen:focus-within { border-color: var(--secondary); }
.quest { margin: 0; text-align: center; display: flex; flex-direction: column; gap: 2px; min-height: 3em; }
.quest b { font-family: var(--mono); font-weight: 400; letter-spacing: .22em; font-size: 13px; color: var(--text); }
.quest span { font-size: 13px; color: var(--muted); }
.quest.awake b { color: var(--secondary); text-shadow: var(--glow); }
.quest.solved b { color: var(--ok); }
.dpad { display: grid; grid-template-columns: repeat(3, 60px); grid-template-rows: repeat(2, 56px); gap: 6px; justify-content: center; touch-action: none; user-select: none; -webkit-user-select: none; }
.dpad button { font: inherit; font-size: 20px; color: var(--secondary); background: rgba(11,20,36,.9); border: 1px solid var(--border); border-radius: 6px; touch-action: none; -webkit-tap-highlight-color: transparent; }
.dpad button.held { background: rgba(0,212,255,.22); border-color: var(--secondary); color: #fff; }
.dpad [data-dir=up] { grid-column: 2; grid-row: 1; } .dpad [data-dir=left] { grid-column: 1; grid-row: 2; } .dpad [data-dir=down] { grid-column: 2; grid-row: 2; } .dpad [data-dir=right] { grid-column: 3; grid-row: 2; }
.keys-hint { margin: 0; text-align: center; font-size: 12px; color: var(--muted); letter-spacing: .08em; }
.dpad button:focus-visible, .chip:focus-visible { outline: 2px solid var(--secondary); outline-offset: 2px; }
@media (hover: hover) and (pointer: fine) { .dpad { grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(2, 40px); } .dpad button { font-size: 16px; } }
.game-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.chip { font: inherit; font-size: 12px; letter-spacing: .14em; color: var(--muted); background: rgba(11,20,36,.9); border: 1px solid var(--border); padding: 7px 12px; min-height: 36px; cursor: pointer; }
.chip:hover { color: var(--text); border-color: var(--secondary); }
#sound-toggle[aria-pressed=false] { color: var(--danger); border-color: rgba(255,77,106,.4); }
/* wide screens (desktop / tablet): the same side-by-side layout — the scene sized to the window height, controls beside it */
@media (min-width: 760px) and (min-height: 521px) {
  body.in-game .panel { width: min(980px, 100%); }
  .game { display: grid; grid-template-columns: auto minmax(220px, 260px); grid-template-rows: auto 1fr; gap: 10px 22px; align-items: center; justify-content: center; }
  .game .hud { grid-column: 1 / -1; }
  .game .screen { width: min(640px, calc((100vh - 330px) * 1.4286), calc(100vw - 400px)); width: min(640px, calc((100dvh - 330px) * 1.4286), calc(100vw - 400px)); }
}
/* short portrait phones (e.g. iPhone SE): while playing, drop the progress steps, tagline and footer so the controls fit */
@media (orientation: portrait) and (max-height: 700px) {
  body.in-game .steps, body.in-game .tagline, body.in-game .foot { display: none; }
  body.in-game .shell { gap: 10px; padding-block: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-bottom)); }
  body.in-game .panel { padding: 14px 14px 12px; }
  .game, .game .side { gap: 8px; }
  .game .quest { min-height: 0; }
  .game .keys-hint { display: none; }
}
/* landscape phones: the scene on the left sized to the screen height, quest + D-pad + buttons on the right — all visible without scrolling */
@media (orientation: landscape) and (max-height: 520px) {
  body.in-game .brand, body.in-game .steps, body.in-game .foot { display: none; }
  body.in-game .shell { padding-block: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-bottom)); gap: 0; justify-content: center; }
  body.in-game .panel { width: min(100%, 980px); padding: 10px 14px; }
  .game { display: grid; grid-template-columns: auto minmax(200px, 1fr); grid-template-rows: auto 1fr; gap: 6px 16px; align-items: center; }
  .game .hud { grid-column: 1 / -1; }
  .game .screen { height: calc(100vh - 66px); height: calc(100dvh - 66px); max-width: calc(100vw - 280px); aspect-ratio: 160 / 112; width: auto; }
  .game .screen canvas { width: 100%; height: 100%; }
  .game .side { gap: 8px; }
  .game .quest { min-height: 0; }
  .game .keys-hint { display: none; }
}
.term { list-style: none; margin: 0; padding: 10px 12px; font-family: var(--mono); font-size: 12.5px; color: var(--secondary); background: rgba(0,0,0,.35); border-left: 2px solid var(--primary); min-height: 84px; }
.term li { opacity: 0; animation: typeIn .35s forwards; }
.term li.ok { color: var(--ok); }
.term li.bad { color: var(--danger); }
.term.big { font-size: 15px; text-align: center; border: 0; background: transparent; min-height: 200px; }
@keyframes typeIn { to { opacity: 1; } }

/* OTP */
.otp { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.otp input { text-align: center; font-family: var(--mono); font-size: 24px; padding: 10px 0; min-height: 56px; }
.qr { display: flex; justify-content: center; }
.qr img { background: #fff; padding: 8px; width: 200px; height: 200px; image-rendering: pixelated; }
.key { margin: 0; font-size: 12px; color: var(--muted); text-align: center; word-break: break-all; }
.key code { color: var(--text); font-family: var(--mono); }
.codes { columns: 2; font-family: var(--mono); font-size: 15px; margin: 0; padding: 12px 12px 12px 34px; background: rgba(0,0,0,.35); border: 1px solid var(--border); }
.check { font-size: 14px; color: var(--muted); display: flex; gap: 8px; align-items: center; }

/* stage 4 */
.unlock { position: relative; width: 120px; height: 120px; margin: 4px auto 0; }
.lock-body { position: absolute; left: 25px; right: 25px; bottom: 12px; height: 56px; border: 3px solid var(--secondary); background: rgba(0,212,255,.08); box-shadow: var(--glow); }
.lock-shackle { position: absolute; left: 38px; right: 38px; top: 10px; height: 52px; border: 3px solid var(--secondary); border-bottom: 0; border-radius: 30px 30px 0 0; transform-origin: 85% 100%; transition: transform .8s cubic-bezier(.3,1.6,.5,1); }
.unlock.open .lock-shackle { transform: translateY(-10px) rotate(-28deg); border-color: var(--ok); }
.unlock.open .lock-body { border-color: var(--ok); }
.beam { position: absolute; left: -40px; right: -40px; height: 3px; top: 0; background: linear-gradient(90deg, transparent, var(--secondary), transparent); opacity: 0; }
.unlock.open .beam { animation: beam 1.2s ease-in-out 2; }
@keyframes beam { 0% { top: 0; opacity: 0 } 20% { opacity: 1 } 100% { top: 120px; opacity: 0 } }
body.leaving .shell { animation: leave .9s ease-in forwards; }
@keyframes leave { to { opacity: 0; transform: scale(1.04); filter: blur(4px); } }

.foot { font-size: 11px; letter-spacing: .3em; color: var(--muted); text-align: center; animation: fadeUp .9s .8s both; }

@media (max-width: 520px) {
  .logo { font-size: clamp(22px, 7.2vw, 30px); letter-spacing: .1em; gap: .3em; }
  .logo-mark { width: .9em; height: .9em; }
  .panel { padding: 24px 18px 18px; }
  .steps li { padding: 5px 8px; font-size: 10px; letter-spacing: .12em; }
  .title { font-size: 19px; letter-spacing: .22em; }
  .tagline { letter-spacing: .35em; }
  .otp { gap: 6px; } .otp input { font-size: 21px; min-height: 52px; }
  .codes { columns: 1; }
}
@media (max-height: 640px) and (min-width: 700px) { .shell { justify-content: flex-start; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  .scan::after { display: none; }
}
