:root {
  --bg: #0a0e0d;
  --panel: #131a18;
  --cell: #101715;
  --line: #26332e;
  --ink: #d7e4dc;
  --dim: #7f9a8f;
  --green: #4ade80;
  --amber: #f2b544;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Apple SD Gothic Neo", "Malgun Gothic", ui-monospace, SFMono-Regular, Menlo, "Courier New", monospace;
  font-size: 14px;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow-y: auto;
  -webkit-text-size-adjust: 100%;
}

[hidden] {
  display: none !important;
}

.app {
  width: 100%;
  max-width: 900px;
  padding: 16px 14px 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.app-title {
  margin: 2px 0;
  font-size: clamp(17px, 4.4vw, 24px);
  letter-spacing: 0.06em;
  align-items: center;
  text-align: center;
}

/* 영어(위) + 한국어(아래) 병기 */
.bi {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1.18;
}

.bi-en {
  font-size: 0.9em;
  letter-spacing: 0.04em;
}

.bi-ko {
  font-size: 0.8em;
  opacity: 0.72;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.panel-label {
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--dim);
  margin-bottom: 8px;
}

.btn {
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
  background: #1a2420;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.btn:hover {
  border-color: var(--green);
  color: var(--green);
}

.btn:active {
  background: #233029;
}

.btn:focus-visible,
.key:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

/* 로터 창 */
.rotor-windows {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.rotor {
  flex: 1 1 0;
  max-width: 150px;
  background: linear-gradient(180deg, #0c1211, #151d1b);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 6px 12px;
  text-align: center;
  box-shadow: inset 0 0 18px rgba(0, 0, 0, 0.5);
}

.rotor-label {
  font-size: 12px;
  color: var(--dim);
  margin-bottom: 4px;
}

.rotor-letter {
  display: block;
  font-size: clamp(26px, 7vw, 40px);
  font-weight: 700;
  color: var(--amber);
  line-height: 1;
}

.rotor-letter.turn {
  animation: turn 0.25s ease;
}

@keyframes turn {
  from {
    transform: translateY(-45%);
    opacity: 0.15;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

/* 신호 경로 */
.trace {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
}

.trace-cell {
  flex: 1 1 0;
  min-width: 62px;
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 4px;
  text-align: center;
}

.trace-label {
  font-size: 10px;
  color: var(--dim);
  align-items: center;
  margin-bottom: 2px;
}

.trace-letter {
  font-size: clamp(15px, 3.4vw, 21px);
  font-weight: 700;
  color: var(--ink);
  min-height: 1.2em;
}

.trace-cell.sweep {
  animation: sweep 0.5s ease;
  animation-delay: calc(var(--i) * 45ms);
}

@keyframes sweep {
  0% {
    border-color: var(--green);
    background: #16301f;
  }
  100% {
    border-color: var(--line);
    background: var(--cell);
  }
}

/* 키보드 / 전구 */
.boards {
  display: grid;
  gap: 10px;
}

@media (min-width: 640px) {
  .boards {
    grid-template-columns: 1fr 1fr;
  }
}

.key-grid,
.lamp-grid {
  display: grid;
  grid-template-columns: repeat(13, minmax(0, 1fr));
  gap: 4px;
}

.key {
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-family: inherit;
  font-size: clamp(10px, 2.3vw, 15px);
  color: var(--ink);
  background: #19211f;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  touch-action: manipulation;
}

.key:hover {
  border-color: var(--amber);
}

.key.press {
  animation: keyFlash 0.35s ease;
}

@keyframes keyFlash {
  0% {
    background: var(--amber);
    border-color: var(--amber);
    color: #141a18;
  }
  100% {
    background: #19211f;
    border-color: var(--line);
    color: var(--ink);
  }
}

.lamp {
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(9px, 2.1vw, 14px);
  color: #46584f;
  background: #161d1b;
  border: 1px solid var(--line);
  border-radius: 50%;
}

.lamp.on {
  color: var(--green);
  background: #12251b;
  border-color: var(--green);
}

.lamp.flash {
  animation: lampFlash 0.6s ease;
}

@keyframes lampFlash {
  0% {
    background: #6bffab;
    color: #04150c;
    border-color: #6bffab;
    box-shadow: 0 0 20px 6px rgba(74, 222, 128, 0.55);
  }
  55% {
    background: #1d3f2c;
    color: #cdffdf;
    border-color: var(--green);
    box-shadow: 0 0 10px 2px rgba(74, 222, 128, 0.3);
  }
  100% {
    box-shadow: none;
  }
}

/* 출력 */
.output {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.text-box {
  min-height: 2.4em;
  padding: 8px 10px;
  font-size: clamp(15px, 3.6vw, 20px);
  letter-spacing: 0.18em;
  word-break: break-all;
  line-height: 1.4;
  color: var(--ink);
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.text-box.cipher {
  color: var(--green);
}

.new-char {
  animation: pop 0.35s ease;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(-7px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rotor-letter.turn,
  .key.press,
  .lamp.flash,
  .trace-cell.sweep,
  .new-char,
  .intro-line.line-in {
    animation: none !important;
  }
}

/* 임무(암호 해독) */
.mission {
  border-color: #33453c;
  background: linear-gradient(180deg, #16201d, var(--panel));
}

.mission-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.mission-head .panel-label {
  margin-bottom: 0;
}

.mission-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

.mission-objective {
  margin: 0;
  max-width: 74ch;
  font-size: 12px;
  line-height: 1.55;
  color: var(--dim);
}

.mission-target {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 20px;
  padding: 12px 14px;
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.mission-stage {
  display: flex;
  align-items: baseline;
  gap: 3px;
  color: var(--dim);
  font-size: 12px;
}

.mission-stage-num {
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
  color: var(--amber);
}

.mission-line {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.mission-line-label {
  font-size: 10px;
  letter-spacing: 0.12em;
  color: var(--dim);
}

.mission-line-value {
  font-size: 14px;
  letter-spacing: 0.1em;
}

.mission-code {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.22em;
  color: var(--green);
  text-shadow: 0 0 16px rgba(74, 222, 128, 0.35);
}

.mission-choices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  gap: 8px;
}

.mission-choice {
  min-height: 58px;
  padding: 0 12px;
  font-family: inherit;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--ink);
  background: #19211f;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.mission-choice:hover:not(:disabled) {
  border-color: var(--amber);
  color: var(--amber);
  background: #1f2a24;
}

.mission-choice:disabled {
  opacity: 0.4;
  cursor: default;
}

.mission-choice:focus-visible {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

.mission-status {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.45;
}

.mission-status.good {
  color: var(--green);
}

.mission-status.dead {
  color: #ff6b6b;
}

.mission-status.dead .bi-ko {
  font-size: 1em;
  opacity: 1;
}

/* 인트로 브리핑 */
.briefing {
  order: -1;
  padding: 22px 18px;
  border-color: #3a3320;
  background: linear-gradient(180deg, #1c1a12, var(--panel));
}

.briefing .screen-actions .btn {
  padding: 10px 24px;
  font-size: 14px;
}

/* 인트로 이야기 (한 줄씩) */
.intro-line {
  margin: 0;
  min-height: 4.4em;
  font-size: clamp(16px, 4.4vw, 20px);
  line-height: 1.45;
  align-items: flex-start;
}

.intro-line .bi-en {
  font-size: 1em;
}

.intro-line .bi-ko {
  font-size: 0.88em;
  opacity: 1;
  margin-top: 6px;
}

.intro-line.line-in {
  animation: introLineIn 0.4s ease;
}

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

/* 화면 전환 (인트로 → 미션 → 엔딩) */
.screen {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 인트로는 이야기에 집중하도록 세로 가운데에 둔다. */
#screenIntro {
  justify-content: center;
  min-height: min(58vh, 420px);
}

/* 넓은 화면: 임무 → (로터 | 신호 경로) → (키보드 | 전구) → 출력 순으로 놓는다. */
@media (min-width: 920px) {
  .app {
    max-width: 1120px;
  }

  #screenMission {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "mission mission"
      "rotor trace"
      "boards boards"
      "output output";
    gap: 12px;
    align-items: start;
  }

  #screenMission > .mission {
    grid-area: mission;
  }

  #screenMission > .rotor-panel {
    grid-area: rotor;
  }

  #screenMission > .trace-panel {
    grid-area: trace;
  }

  #screenMission > .boards {
    grid-area: boards;
  }

  #screenMission > .output {
    grid-area: output;
  }
}

.screen-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

/* 임무 화면에서는 화면 키보드로 시험할 단어를 만든다 */
.boards .board.keys .key {
  cursor: pointer;
}

/* 엔딩 */
.ending {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-height: min(52vh, 360px);
  padding: 32px 18px;
  text-align: center;
  border-color: #33453c;
}

.ending-badge {
  font-size: 11px;
  letter-spacing: 0.16em;
  color: var(--green);
  align-items: center;
  padding: 6px 16px;
  background: #12251b;
  border: 1px solid #2c4a37;
  border-radius: 999px;
}

.ending-title {
  margin: 0;
  font-size: clamp(22px, 6vw, 34px);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: 0.04em;
  color: var(--green);
  align-items: center;
}

.ending-title .bi-en {
  font-size: 1em;
}

.ending-title .bi-ko {
  font-size: 0.72em;
  opacity: 1;
}

.ending-text {
  margin: 0;
  font-size: 13px;
  color: var(--dim);
  align-items: center;
}

.ending-text .bi-ko {
  font-size: 1em;
  opacity: 1;
}

.ending .screen-actions {
  justify-content: center;
  margin-top: 6px;
}

/* 임무 시험 안내와 기록 */
.mission-note {
  margin: 0;
  font-size: 11px;
  line-height: 1.5;
  color: var(--dim);
}

.mission-note .bi-ko {
  font-size: 1em;
  opacity: 0.9;
}

.mission-choices-head {
  font-size: 11px;
  color: var(--dim);
}

.mission-tests {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.mission-log {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 32px;
  padding: 6px;
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.mission-test {
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--dim);
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 8px;
}

.mission-test.hit {
  color: var(--green);
  border-color: var(--green);
  background: #12251b;
}

/* 시험해 본 키 표시 */
.key.tried {
  color: var(--amber);
  border-color: #4b4128;
}

/* 시험할 단어 입력 */
.word-input {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.word-display {
  flex: 1 1 96px;
  min-height: 38px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: 0.22em;
  color: var(--green);
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.word-display:empty::before {
  content: "\2013\2013";
  color: #46584f;
  letter-spacing: 0.1em;
}

.word-actions {
  display: flex;
  gap: 6px;
}

.btn.small {
  padding: 6px 10px;
  font-size: 12px;
}

.btn.small:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn.small:disabled:hover {
  border-color: var(--line);
  color: var(--ink);
}

