* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; user-select: none; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: #1c1f2e;
  font-family: "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
}

#app { width: 100vw; height: 100dvh; height: 100vh; position: relative; }

.screen {
  position: absolute;
  inset: 0;
  display: none;
}
.screen.active { display: block; }

/* ---------------- 시작 화면 ---------------- */
#screen-start {
  display: none;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #4b6cf5, #7d4bf5 55%, #f54bb8);
  position: relative;
  overflow: hidden;
}
#screen-start.active { display: flex; }

.float-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}
.fs {
  position: absolute;
  top: 100%;
  font-size: clamp(1.6rem, 4vmin, 2.6rem);
  opacity: 0;
  color: rgba(255,255,255,0.55);
  font-weight: 800;
  animation-name: floatUp;
  animation-timing-function: ease-in;
  animation-iteration-count: infinite;
}
@keyframes floatUp {
  0% { transform: translateY(0) rotate(0deg); opacity: 0; }
  8% { opacity: 0.8; }
  85% { opacity: 0.6; }
  100% { transform: translateY(-115vh) rotate(25deg); opacity: 0; }
}

.start-card {
  position: relative;
  z-index: 1;
  background: #fff;
  border-radius: 28px;
  padding: clamp(20px, 3.4vmin, 40px) clamp(22px, 4vmin, 44px);
  max-width: 640px;
  width: 92%;
  max-height: 94vh;
  overflow-y: auto;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  animation: cardPop 0.5s cubic-bezier(.24,1.4,.4,1);
}
@keyframes cardPop {
  0% { transform: scale(0.85) translateY(20px); opacity: 0; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}

/* 시작 화면 히어로 이미지 (제목 대신 사용) */
.hero-float {
  animation: heroFloat 3.6s ease-in-out infinite;
  margin: 0 auto 4px;
  max-width: min(62%, 280px);
}
.hero-img {
  display: block;
  width: 100%;
  height: auto;
  filter: drop-shadow(0 10px 18px rgba(0,0,0,0.18));
  animation: heroPop 0.6s cubic-bezier(.24,1.4,.4,1) both;
}
@keyframes heroFloat {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-9px) rotate(-0.6deg); }
}
@keyframes heroPop {
  0% { opacity: 0; transform: scale(0.82) translateY(24px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}
.subtitle { color: #666; margin: 0 0 22px; font-size: clamp(0.9rem, 2vmin, 1.05rem); }

.option-block { margin-bottom: 20px; text-align: left; }
.option-label { font-weight: 700; color: #333; margin-bottom: 10px; font-size: clamp(0.92rem, 2vmin, 1.05rem); }

.stepper-row { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 10px; }
.stepper-btn {
  width: 44px; height: 44px;
  border-radius: 12px;
  border: 2px solid #ddd;
  background: #f7f7fb;
  font-size: 1.4rem;
  font-weight: 800;
  color: #5a2fd6;
  cursor: pointer;
}
.stepper-input {
  width: 90px;
  height: 44px;
  text-align: center;
  font-size: 1.2rem;
  font-weight: 800;
  border-radius: 12px;
  border: 2px solid #ddd;
  color: #2a2d45;
}

.option-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.chip-btn, .seg-btn {
  padding: 10px 16px;
  border-radius: 999px;
  border: 2px solid #ddd;
  background: #f7f7fb;
  font-size: 0.9rem;
  font-weight: 700;
  color: #555;
  cursor: pointer;
}
.chip-btn.selected, .seg-btn.selected {
  border-color: #7d4bf5;
  background: #efe8ff;
  color: #5a2fd6;
}
.segmented .seg-btn { flex: 1; min-width: 130px; padding: 12px 16px; border-radius: 14px; }

.toggles { display: flex; flex-direction: column; gap: 12px; }
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: #f7f7fb;
  border-radius: 14px;
  padding: 12px 16px;
}
.toggle-label { font-weight: 600; color: #333; font-size: clamp(0.85rem, 1.9vmin, 0.98rem); }

.switch { position: relative; display: inline-block; width: 50px; height: 28px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background-color: #d5d5e0;
  border-radius: 999px;
  transition: 0.2s;
}
.slider::before {
  content: "";
  position: absolute;
  height: 22px; width: 22px;
  left: 3px; bottom: 3px;
  background-color: white;
  border-radius: 50%;
  transition: 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.switch input:checked + .slider { background-color: #7d4bf5; }
.switch input:checked + .slider::before { transform: translateX(22px); }

.btn-primary {
  background: linear-gradient(135deg, #ff8a3d, #ff5f6d);
  color: #fff;
  border: none;
  font-size: clamp(1.05rem, 2.4vmin, 1.3rem);
  font-weight: 800;
  padding: 16px 40px;
  border-radius: 18px;
  cursor: pointer;
  box-shadow: 0 10px 22px rgba(255, 95, 109, 0.4);
  margin-top: 6px;
  animation: btnPulse 1.8s ease-in-out infinite;
}
@keyframes btnPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.035); }
}
.btn-primary:active { transform: scale(0.96); }

.hint { margin-top: 20px; color: #999; font-size: 0.85rem; }

/* ---------------- 게임 화면 ---------------- */
#screen-game {
  display: none;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: #1c1f2e;
}
#screen-game.active { display: flex; }

.half {
  flex: 1 1 0;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.half-top { background: linear-gradient(200deg,#2c3568,#3a2e6b); }
.half-bottom { background: linear-gradient(20deg,#1f3a5f,#2a2e6b); }

.half-top .half-inner { transform: rotate(180deg); }
.half-inner { width: 100%; height: 100%; min-height: 0; display: flex; align-items: center; justify-content: center; }

/* ---------------- 중앙 에너지 바 ---------------- */
#divider {
  flex: 0 0 auto;
  background: #12141f;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  z-index: 5;
}
.energy-wrap { width: min(92%, 720px); display: flex; align-items: center; gap: 12px; }
.energy-track {
  flex: 1;
  height: clamp(16px, 2.6vmin, 24px);
  background: #23263a;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid #3a3d55;
  position: relative;
}
.energy-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg,#5be07f,#3ec6e0,#ffd76b);
  background-size: 200% 100%;
  border-radius: 999px;
  transition: width 0.45s cubic-bezier(.3,1,.4,1);
}
.energy-fill.pulse { animation: energyPulse 0.5s ease; }
@keyframes energyPulse {
  0% { filter: brightness(1); }
  40% { filter: brightness(1.6); }
  100% { filter: brightness(1); }
}
.energy-label {
  color: #ffd76b;
  font-weight: 800;
  font-size: clamp(0.8rem, 2vmin, 1rem);
  white-space: nowrap;
}

/* ---------------- 플레이어 패널 ----------------
   중요: 각 패널은 화면 "절반" 높이만 쓸 수 있으므로, 크기 기준을 vmin(전체 화면 기준)이
   아니라 vh/vw(실제 배정된 높이·너비 기준)로 잡고, flex 트리 전체에 min-height:0 을 걸어
   내용이 넘칠 때 clamp() 하한까지 스스로 줄어들 수 있게 한다. */
.panel {
  width: min(97%, 900px);
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(2px, 0.8vh, 8px);
  padding: clamp(3px, 0.8vh, 8px) clamp(8px, 1.6vw, 16px);
  color: #fff;
}

.panel-header {
  flex: 0 0 auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.combo-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(255,255,255,0.12);
  padding: clamp(3px, 0.6vh, 7px) clamp(10px, 1.6vw, 16px);
  border-radius: 999px;
  font-weight: 800;
  font-size: clamp(0.72rem, 1.6vh, 0.95rem);
  transition: transform 0.15s ease;
}
.combo-badge.pulse { animation: comboPulse 0.4s ease; }
@keyframes comboPulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); }
  100% { transform: scale(1); }
}
.combo-fire { font-size: 1.05em; }
.combo-word { font-size: 0.8em; opacity: 0.8; }

/* 문제(세로식) + 입력 영역을 가로로 배치 - 반씩 나뉜 화면은 "넓고 낮은" 모양이라
   세로로 쌓기보다 가로로 배치해야 실제로 다 들어간다. 공간이 부족하면 자동으로 줄바꿈. */
.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2.2vw, 26px);
}

/* 세로식 */
.vp-wrap { flex: 0 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }
.vp-grid {
  display: grid;
  grid-template-columns: auto;
  gap: 1px clamp(3px, 0.9vw, 9px);
  font-size: clamp(1.1rem, 3.6vh, 2.3rem);
  font-weight: 800;
  color: #fff;
}
.vp-cell {
  min-width: clamp(22px, 6vw, 50px);
  text-align: center;
}
.vp-op-cell { text-align: left; color: #ffd76b; }
.vp-hr { grid-column: 1 / -1; border-bottom: 4px solid #fff; margin: 2px 0; opacity: 0.85; }

.answer-row-cell { display: flex; align-items: center; justify-content: center; }

.digit-slot {
  position: relative;
  width: clamp(38px, 7vw, 78px);
  height: clamp(44px, 7.4vh, 92px);
  background: rgba(255,255,255,0.92);
  border-radius: 12px;
  box-shadow: inset 0 0 0 3px #d8d8e8;
  overflow: hidden;
}
.digit-slot.filled { box-shadow: inset 0 0 0 3px #8ecbff; }
.digit-slot.correct { box-shadow: inset 0 0 0 4px #4cd47d; background: #eafff1; }
.digit-slot.wrong { box-shadow: inset 0 0 0 4px #ff6767; background: #fff0f0; }
.digit-slot.active-key { box-shadow: inset 0 0 0 3px #ffd76b; }

.digit-canvas { width: 100%; height: 100%; touch-action: none; display: block; }

.digit-display {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(1.2rem, 4.4vh, 2.4rem);
  font-weight: 800;
  color: #2b2f4a;
}

.input-area {
  flex: 0 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(4px, 1vh, 10px);
}

.answer-buttons { display: flex; gap: clamp(6px, 1.2vw, 10px); }
.btn-clear, .btn-check {
  padding: clamp(6px, 1.1vh, 12px) clamp(10px, 2vw, 20px);
  border-radius: 10px;
  border: none;
  font-weight: 800;
  font-size: clamp(0.72rem, 1.7vh, 0.95rem);
  cursor: pointer;
}
.btn-clear { background: rgba(255,255,255,0.15); color: #fff; }
.btn-check { background: linear-gradient(135deg,#5be07f,#2fc98f); color: #063; }
.btn-check:active, .btn-clear:active { transform: scale(0.94); }

/* 숫자 패드 - 세로로 많이 쌓이지 않도록 6열로 넓게 배치 (2줄이면 충분) */
.keypad-grid {
  display: grid;
  grid-template-columns: repeat(6, clamp(30px, 5.4vw, 50px));
  gap: clamp(3px, 0.7vh, 7px);
}
.key-btn {
  width: clamp(30px, 5.4vw, 50px);
  height: clamp(26px, 4.6vh, 42px);
  border-radius: 10px;
  border: none;
  background: rgba(255,255,255,0.14);
  color: #fff;
  font-size: clamp(0.9rem, 2.4vh, 1.25rem);
  font-weight: 800;
  cursor: pointer;
}
.key-btn:active { transform: scale(0.92); background: rgba(255,255,255,0.28); }
.key-btn.key-back { background: rgba(255,110,110,0.25); }

.feedback {
  flex: 0 0 auto;
  min-height: clamp(16px, 2.8vh, 26px);
  font-size: clamp(0.78rem, 1.9vh, 1.02rem);
  font-weight: 800;
  text-align: center;
}
.feedback.ok { color: #6bffb0; }
.feedback.no { color: #ff8080; }

.panel.shake { animation: panelShake 0.4s ease; }
@keyframes panelShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-10px); }
  40% { transform: translateX(9px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(4px); }
}

/* ---------------- 오버레이 (미션 / 성공) ---------------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: rgba(10, 12, 24, 0.94);
}
.overlay.hidden { display: none; }

.overlay-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #fff;
  text-align: center;
  padding: 14px;
}
.overlay-top { transform: rotate(180deg); }

.overlay-mid { flex: 0 0 auto; display: flex; justify-content: center; padding: 14px; }

.mission-emoji, .success-emoji { font-size: clamp(2.2rem, 6vmin, 3.4rem); }
.mission-text {
  font-size: clamp(1.3rem, 4.6vmin, 2.2rem);
  font-weight: 800;
  max-width: 90%;
}
.success-title { font-size: clamp(1.8rem, 6.4vmin, 3rem); font-weight: 900; color: #ffd76b; }
.success-desc { font-size: clamp(0.95rem, 2.4vmin, 1.1rem); opacity: 0.9; }

#confetti-canvas { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 0; }
