* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Layout controls. Theme overrides below adjust typography and frame centers.
   Offsets are percentages of the frame; positive values move right/down.
   Font size uses cqw (1% of the frame width), so digits scale with the frames. */
:root {
  --code-row-top: 50%;
  --code-row-left: 50%;
  --code-row-width: 1200px;
  --code-row-gutter: 16px;
  --code-window-size: 180px;
  --code-window-gap: clamp(4px, 1.2vw, 20px);
  --digit-font-size: 42cqw;
  --digit-font-family: 'Courier New', monospace;
  --digit-font-weight: 700;
  --digit-colour: #fff1d5;
  --digit-offset-x: 0%;
  --digit-offset-y: 2%;
  --digit-shadow: 0 2px 4px #000, 0 0 12px rgba(0, 0, 0, 0.8);
  --digit-stroke: 0px;
  --timer-colour: #fff1d5;
  --timer-top-font-size: clamp(40px, 7vw, 86px);
  --timer-top: max(16px, env(safe-area-inset-top));
  --welcome-colour: #fff4df;
  --intro-text-size: clamp(17px, 1.8vw, 28px);
}

.theme-duck {
  --timer-colour: #fff5d4;
  --background-colour: #e9b947;
  --digit-font-family: 'Fredoka', 'Trebuchet MS', sans-serif;
  --digit-colour: #153f49;
  --digit-shadow: 0 1px 1px rgba(255, 255, 255, 0.7);
  --digit-offset-y: 1%;
}

.theme-candy {
  --timer-colour: #fff0da;
  --background-colour: #e98ba6;
  --digit-font-family: 'Fredoka', 'Trebuchet MS', sans-serif;
  --digit-colour: #6c2343;
  --digit-shadow: 0 1px 1px rgba(255, 255, 255, 0.65);
  --digit-offset-y: 0%;
}

.theme-mini {
  --timer-colour: #fff4df;
  --background-colour: #243f61;
  --digit-font-family: 'Fredoka', 'Trebuchet MS', sans-serif;
  --digit-colour: #fff4df;
  --digit-stroke: 1px #281c18;
  --digit-offset-y: 1%;
}

.theme-scarecrow {
  --timer-colour: #ffe3ac;
  --background-colour: #7a4a20;
  --digit-font-family: 'Trebuchet MS', sans-serif;
  --digit-colour: #3c2010;
  --digit-shadow: 0 1px 2px rgba(255, 237, 201, 0.6);
  --digit-offset-y: 0%;
}

.theme-dark {
  --background-colour: #080606;
  --digit-colour: #fff1d5;
  --digit-shadow: 0 2px 3px #160000, 0 0 12px rgba(255, 226, 178, 0.35);
  --digit-font-weight: 400;
  --digit-offset-y: 1%;
}

.theme-spider {
  --timer-colour: #f0ddff;
  --background-colour: #170b24;
  --digit-font-family: 'Cinzel Decorative', Georgia, serif;
  --digit-colour: #f8e8ff;
  --digit-shadow: 0 0 12px rgba(176, 106, 235, 0.4), 0 2px 3px #000;
  --digit-offset-y: 8%;
  --welcome-colour: #f0ddff;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000;
  font-family: 'Courier New', monospace;
}

#screen {
  position: fixed;
  inset: 0;
  background-color: var(--background-colour);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  transition: opacity 700ms ease;
  opacity: 1;
}

#screen.fade { opacity: 0; }

#ui-layer {
  position: fixed;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}

#timer-panel {
  position: absolute;
  top: var(--timer-top);
  left: 50%;
  width: calc(100% - 32px);
  transform: translateX(-50%);
  display: flex;
  justify-content: center;
}

#timer {
  color: var(--timer-colour);
  font-family: var(--digit-font-family);
  font-weight: var(--digit-font-weight);
  font-size: var(--timer-top-font-size);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  white-space: nowrap;
  text-shadow: 0 3px 3px #000, 0 0 24px rgba(0, 0, 0, 0.8);
  -webkit-text-stroke: 1px rgba(0, 0, 0, 0.5);
}

#welcome-screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
  background: radial-gradient(ellipse at center, rgba(12, 9, 20, 0.78), rgba(12, 9, 20, 0.3));
}

.welcome-message {
  width: min(100%, 1100px);
  color: var(--welcome-colour);
  text-align: center;
  text-shadow: 0 3px 16px #000;
  animation: welcome-fade-in 3s ease-out both;
}

#welcome-title {
  margin: 0;
  font-size: clamp(14px, 1.6vw, 24px);
  line-height: 1.5;
  letter-spacing: 0.1em;
}

#welcome-name {
  display: block;
  margin: 12px 0;
  font-family: var(--digit-font-family);
  font-size: clamp(42px, 8vw, 120px);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0;
  overflow-wrap: anywhere;
}

#welcome-pun {
  max-width: 46ch;
  margin: 28px auto 0;
  font-family: 'Trebuchet MS', sans-serif;
  font-size: clamp(18px, 2.2vw, 30px);
  line-height: 1.5;
}

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

button {
  pointer-events: auto;
  min-height: 48px;
  padding: 12px 28px;
  border: 2px solid #fff1d5;
  border-radius: 12px;
  background: #241727;
  color: #fff1d5;
  font-family: var(--digit-font-family);
  font-size: clamp(22px, 3vw, 32px);
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.4);
}

button:hover { background: #412d45; }
button:focus-visible, .joke-confirmation:has(input:focus-visible) {
  outline: 3px solid #ffe600;
  outline-offset: 5px;
}
button:disabled { opacity: 0.5; cursor: default; }

#timeout-dialog {
  width: min(620px, calc(100% - 32px));
  max-height: calc(100dvh - 32px);
  padding: clamp(20px, 4vw, 40px);
  border: 2px solid var(--timer-colour);
  border-radius: 20px;
  background: rgba(27, 17, 29, 0.97);
  color: #fff1d5;
  text-align: center;
  box-shadow: 0 12px 60px rgba(0, 0, 0, 0.6);
}

#timeout-dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
#timeout-dialog h1 {
  margin: 0 0 16px;
  font-family: var(--digit-font-family);
  font-size: clamp(32px, 5vw, 54px);
  color: var(--timer-colour);
}
#timeout-dialog p, .joke-confirmation {
  font-family: 'Trebuchet MS', sans-serif;
  font-size: clamp(17px, 2vw, 22px);
  line-height: 1.5;
}
#timeout-pun { font-weight: 700; }
.joke-confirmation {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 24px 0;
  text-align: left;
  cursor: pointer;
}
.joke-confirmation input { width: 24px; height: 24px; flex: none; accent-color: #ffe3a3; }

@media (max-height: 480px) {
  :root {
    --timer-top-font-size: 40px;
    --timer-top: 8px;
  }
  #welcome-name { font-size: clamp(38px, 9vh, 60px); margin: 6px 0; }
  #welcome-pun { margin-top: 16px; }
}

#code-display {
  position: absolute;
  top: var(--code-row-top);
  left: var(--code-row-left);
  transform: translate(-50%, -50%);
  width: min(var(--code-row-width), calc(100% - 2 * var(--code-row-gutter)));
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  justify-items: center;
  align-items: center;
  gap: var(--code-window-gap);
}

.code-window {
  position: relative;
  width: min(100%, var(--code-window-size));
  aspect-ratio: 1;
  container-type: inline-size;
}

.number-window {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.5));
}

/* Keep placement on a wrapper so digit animations never reset the offsets. */
.digit-position {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: grid;
  place-items: center;
  transform: translate(var(--digit-offset-x), var(--digit-offset-y));
}

.digit {
  display: block;
  text-align: center;
  font-family: var(--digit-font-family);
  font-size: var(--digit-font-size);
  font-weight: var(--digit-font-weight);
  line-height: 1;
  color: var(--digit-colour);
  text-shadow: var(--digit-shadow);
  -webkit-text-stroke: var(--digit-stroke);
}

.digit.placeholder { opacity: 0.28; }

#code-input {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  font-size: 16px;
  opacity: 0;
  pointer-events: none;
}

.theme-candy .digit.pop { animation: candy-bounce 300ms ease; }
.theme-duck .digit.pop { animation: candy-bounce 300ms ease; }
@keyframes candy-bounce {
  0% { transform: scale(0.8); }
  55% { transform: scale(1.12) rotate(2deg); }
  78% { transform: scale(0.96) rotate(-1deg); }
  100% { transform: scale(1); }
}

.theme-mini .digit.pop { animation: mini-pop 250ms ease; }
@keyframes mini-pop {
  0% { transform: scale(1.6); opacity: 0; }
  70% { transform: scale(0.95); opacity: 1; }
  100% { transform: scale(1); }
}

.theme-scarecrow .digit.pop { animation: scarecrow-flicker 400ms ease; }
@keyframes scarecrow-flicker {
  0% { opacity: 0.2; }
  25% { opacity: 1; }
  40% { opacity: 0.4; }
  60%, 100% { opacity: 1; }
}

.theme-dark .digit.pop { animation: dark-reveal 600ms ease; }
@keyframes dark-reveal {
  0% { opacity: 0; }
  100% { opacity: 1; }
}

.theme-spider .digit.pop { animation: dark-reveal 600ms ease; }

.digit.wrong, .launch-digit.wrong {
  animation: wrong-shake 400ms ease;
  color: #ff5252 !important;
}
@keyframes wrong-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

/* Completion screen: existing terminal presentation. */

#intro-screen {
  position: fixed;
  inset: 0;
  z-index: 21;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 0 max(24px, env(safe-area-inset-bottom));
  overflow-y: auto;
  background: radial-gradient(ellipse at center, #07130c 0%, #000 75%);
}

#intro-screen::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(transparent 0 3px, rgba(0, 0, 0, 0.18) 3px 4px);
}

#intro-screen .intro-terminal {
  width: min(90vw, 1000px);
  min-height: 0;
  margin: auto;
  font-size: var(--intro-text-size);
  white-space: normal;
}

.intro-terminal::before { display: none; }
.intro-channel {
  margin: 0 0 28px;
  color: #7dd495;
  font-size: clamp(11px, 1.1vw, 16px);
  letter-spacing: 0.12em;
}
.intro-channel::before { content: ">_ "; }

.intro-terminal #intro-text {
  padding-left: 0;
  min-height: 12em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.intro-terminal #intro-text::after {
  content: "▍";
  color: #8ef9ac;
  opacity: 0.7;
}
body[data-intro-step="remote"] .intro-terminal #intro-text { min-height: 0; }

#intro-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 32px;
}

#intro-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border: 1px solid #7efca3;
  border-radius: 4px;
  background: #06120b;
  color: #8ef9ac;
  font-family: inherit;
  font-size: clamp(18px, 1.5vw, 24px);
  text-shadow: 0 0 8px rgba(126, 252, 163, 0.4);
}
#intro-actions button:hover { background: #133020; }
#intro-actions button:focus-visible, #launch-code:focus-visible { outline-color: #8ef9ac; }

#launch-form {
  margin-top: 32px;
  width: 100%;
  text-align: center;
  font-size: clamp(16px, 1.5vw, 24px);
  letter-spacing: 0;
}
#launch-form label { display: block; color: #8ef9ac; }
#machine-name {
  margin: 8px 0 20px;
  color: #7dd495;
  font-size: clamp(12px, 1.2vw, 18px);
}
#launch-code-display {
  display: grid;
  grid-template-columns: repeat(var(--launch-code-length, 4), minmax(0, 1fr));
  gap: clamp(8px, 2vw, 20px);
  width: min(100%, 580px);
  margin: 0 auto;
  cursor: text;
}
.launch-window {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border: 2px solid #7efca3;
  border-radius: 8px;
  background: rgba(6, 18, 11, 0.85);
  box-shadow: inset 0 0 18px rgba(126, 252, 163, 0.06), 0 0 12px rgba(126, 252, 163, 0.12);
}
.launch-digit {
  display: block;
  font-size: clamp(32px, 8vw, 76px);
  line-height: 1;
  color: #8ef9ac;
}
#launch-form:focus-within .launch-window { border-color: #b5ffcd; }
#launch-code-display:has(.wrong) .launch-window { border-color: #ffb19c; }
#launch-code {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
  font-size: 16px;
}
#launch-error {
  margin: 12px 0 0;
  color: #ffb19c;
  font-size: clamp(14px, 1.2vw, 18px);
  line-height: 1.5;
  text-shadow: none;
}

/* A CRT channel change: hold the transmission, collapse it to a thin line,
   then fade into the first background. No flashing strobe or extra assets. */
#intro-screen.launching .intro-terminal { animation: channel-launch 3200ms ease-in-out forwards; }
@keyframes channel-launch {
  0%, 70% { opacity: 1; transform: scale(1); }
  83% { opacity: 0.7; transform: scale(1.02, 0.02); }
  92%, 100% { opacity: 0; transform: scale(0, 0.002); }
}

@media (max-height: 480px) {
  #intro-screen { display: block; padding: 20px 0; }
  .intro-terminal #intro-text { min-height: 0; }
  body[data-intro-step="remote"] .intro-terminal #intro-text { font-size: 14px; }
  .intro-channel { margin-bottom: 16px; }
  #intro-actions { margin-top: 20px; }
  #launch-form { margin-top: 20px; }
  #machine-name { margin-bottom: 12px; }
  #launch-code-display { width: min(100%, 300px); }
  .launch-digit { font-size: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .welcome-message,
  #intro-screen.launching .intro-terminal { animation: none; }
}


  #final-screen {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: none;
    align-items: center;
    justify-content: center;
    background: #000;
    color: #7efca3;
    opacity: 0;
    transition: opacity 700ms ease;
  }

  #final-screen.show {
    display: flex;
    opacity: 1;
  }

  .terminal-message {
    width: min(92vw, 1200px);
    min-height: min(45vh, 220px);
    padding: 0;
    border: none;
    background: transparent;
    box-shadow: none;
    font-family: "SFMono-Regular", "Consolas", "Liberation Mono", monospace;
    font-size: clamp(18px, 2vw, 34px);
    line-height: 1.6;
    letter-spacing: 0.05em;
    color: #8ef9ac;
    text-shadow: 0 0 8px rgba(126, 252, 163, 0.7), 0 0 18px rgba(126, 252, 163, 0.5);
    white-space: pre-wrap;
    overflow-wrap: break-word;
    position: relative;
  }

  .terminal-message::before {
    content: ">_";
    position: absolute;
    left: clamp(14px, 2vw, 24px);
    top: clamp(12px, 2vw, 20px);
    color: rgba(126, 252, 163, 0.75);
    font-weight: 700;
    opacity: 0.9;
  }

  .terminal-message {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    position: relative;
  }

  .terminal-message .typed-text,
  .terminal-message .typed-line {
    display: block;
    width: 100%;
    padding-left: clamp(42px, 6vw, 70px);
    min-height: 1.6em;
    position: relative;
  }
