/* X를 찾아서 — 픽셀 레트로 테마
   팔레트: 밤바다 남색 배경 / 모래색 보드 / 골드·레드 포인트 / 완두 그린(엔딩 전용) */

:root {
  --bg: #14162b;
  --bg-deep: #0e0f1e;
  --panel: #232649;
  --panel-line: #4a4f7c;
  --ink: #f4efe2;
  --muted: #9aa0c3;
  --gold: #f2b63d;
  --gold-soft: #ffe08a;
  --red: #e5484d;
  --pea: #8fce4e;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Galmuri11', 'Galmuri9', monospace;
  color: var(--ink);
  word-break: keep-all;
  background-color: var(--bg);
  /* 픽셀 별이 뜬 밤하늘 */
  background-image:
    radial-gradient(circle, rgba(244, 239, 226, 0.28) 1px, transparent 1.6px),
    radial-gradient(circle, rgba(244, 239, 226, 0.14) 1px, transparent 1.6px);
  background-size: 187px 187px, 113px 113px;
  background-position: 0 0, 47px 61px;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

/* 은은한 CRT 스캔라인 */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.28) 0 1px, transparent 1px 3px);
  opacity: 0.14;
  pointer-events: none;
  z-index: 100;
}

#app {
  max-width: 430px;
  margin: 0 auto;
  padding: 16px;
  min-height: 100vh;
  min-height: 100dvh;
}

button {
  font-family: inherit;
}

/* ---------- 공용: 픽셀 버튼 ---------- */

.px-btn {
  display: inline-block;
  font-size: 0.9rem;
  color: var(--ink);
  background: var(--panel);
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line), 0 4px 0 var(--bg-deep);
  padding: 12px 22px;
  cursor: pointer;
  user-select: none;
  text-decoration: none;
  text-align: center;
}

.px-btn:active {
  transform: translateY(3px);
  box-shadow: inset 0 0 0 2px var(--panel-line), 0 1px 0 var(--bg-deep);
}

.px-btn.primary {
  background: var(--gold);
  color: #3b2a1e;
  box-shadow: inset 0 0 0 2px var(--gold-soft), 0 4px 0 var(--bg-deep);
}

.px-btn.primary:active {
  box-shadow: inset 0 0 0 2px var(--gold-soft), 0 1px 0 var(--bg-deep);
}

.px-btn.small {
  font-size: 0.7rem;
  padding: 7px 10px;
  box-shadow: inset 0 0 0 2px var(--panel-line), 0 3px 0 var(--bg-deep);
}

.px-btn.wide {
  width: 100%;
}

:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.pixel-icon {
  image-rendering: pixelated;
}

/* ---------- 타이틀 ---------- */

.screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
}

#screen-title {
  justify-content: center;
  min-height: calc(100vh - 32px);
  min-height: calc(100dvh - 32px);
  text-align: center;
  gap: 18px;
}

.title-x {
  width: 92px;
  filter: drop-shadow(0 0 14px rgba(229, 72, 77, 0.55));
  animation: float 2.6s ease-in-out infinite;
}

#screen-title h1 {
  font-size: clamp(2rem, 11vw, 2.7rem);
  letter-spacing: 0.04em;
  text-shadow: 0 4px 0 var(--bg-deep);
}

.tagline {
  font-size: 0.72rem;
  letter-spacing: 0.3em;
  color: var(--gold);
}

.title-desc {
  font-size: 0.85rem;
  line-height: 1.9;
  color: var(--muted);
}

.press {
  font-size: 0.78rem;
  letter-spacing: 0.2em;
  color: var(--muted);
}

.blink {
  animation: blink 1.2s steps(2) infinite;
}

/* ---------- 게임 화면 ---------- */

#screen-game {
  gap: 12px;
  padding-top: 8px;
  touch-action: none;
}

.hud {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.78rem;
}

#hud-stage {
  color: var(--gold);
}

#hud-moves {
  color: var(--muted);
}

.hud-btns {
  display: flex;
  gap: 6px;
}

.stage-hint {
  font-size: 0.78rem;
  color: var(--gold-soft);
  min-height: 1.3em;
  text-align: center;
}

#board-wrap {
  width: 100%;
  touch-action: none;
}

#board {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px #6d5a35, 0 6px 0 rgba(0, 0, 0, 0.4);
}

.dpad {
  display: grid;
  grid-template-areas:
    '. up .'
    'left down right';
  gap: 8px;
  margin-top: 4px;
}

.dpad button {
  width: 64px;
  height: 52px;
  font-size: 1rem;
  padding: 0;
}

.dpad [data-dir='up'] { grid-area: up; }
.dpad [data-dir='left'] { grid-area: left; }
.dpad [data-dir='down'] { grid-area: down; }
.dpad [data-dir='right'] { grid-area: right; }

.control-hint {
  font-size: 0.68rem;
  color: var(--muted);
}

/* ---------- 오버레이 ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  background: rgba(10, 11, 22, 0.92);
  opacity: 0;
  transition: opacity 0.45s;
  overflow-y: auto;
}

/* 내용이 화면보다 길면 스크롤, 짧으면 가운데 정렬 */
.overlay > * {
  margin-top: auto;
  margin-bottom: auto;
}

.overlay.show {
  opacity: 1;
}

.overlay.dark {
  background: rgba(4, 5, 10, 0.98);
  transition: opacity 1.2s;
}

.panel {
  background: var(--panel);
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line), 0 6px 0 rgba(0, 0, 0, 0.45);
  padding: 28px 24px;
  max-width: 320px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.loot {
  width: 96px;
}

#interlude-title {
  font-size: 1.05rem;
  color: var(--gold);
}

#interlude-text {
  font-size: 0.85rem;
  line-height: 1.8;
  color: var(--ink);
  white-space: pre-line;
}

/* ---------- 마지막 스테이지 진입 연출 ---------- */

#stage-intro {
  transition: opacity 0.55s;
}

.intro-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  text-align: center;
}

.intro-label {
  font-size: 1.3rem;
  letter-spacing: 0.3em;
  color: var(--red);
  text-shadow: 0 0 12px rgba(229, 72, 77, 0.55);
}

.intro-line1 {
  font-size: 0.9rem;
  color: var(--ink);
}

.intro-line2 {
  font-size: 1rem;
  color: var(--gold);
}

#stage-intro .intro-label,
#stage-intro .intro-line1,
#stage-intro .intro-line2 {
  opacity: 0;
}

#stage-intro.show .intro-label {
  animation: intro-in 0.6s 0.4s forwards;
}

#stage-intro.show .intro-line1 {
  animation: intro-in 0.6s 1.3s forwards;
}

#stage-intro.show .intro-line2 {
  animation: intro-in 0.6s 2.1s forwards, breathe 1.8s 2.7s ease-in-out infinite;
}

@keyframes intro-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
  }
}

@keyframes breathe {
  50% { opacity: 0.45; }
}

/* ---------- 파이널 ---------- */

#finale-beat {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.heart {
  width: 88px;
  filter: drop-shadow(0 0 16px rgba(229, 72, 77, 0.5));
}

.heart.pulse {
  animation: pulse 0.45s ease-out;
}

#beat-text {
  font-size: 1.05rem;
  letter-spacing: 0.12em;
  color: var(--red);
  min-height: 1.5em;
}

#finale-reveal {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  max-width: 340px;
}

.reveal {
  width: 150px;
}

/* 공개된 아트 위에 뜨는 작은 하트 — 심장박동 리듬(쿵-쿵)으로 펄스 */
.reveal-heart {
  width: 30px;
  margin-bottom: -6px;
  filter: drop-shadow(0 0 8px rgba(229, 72, 77, 0.5));
  animation:
    heart-pop 0.45s ease-out,
    heart-pulse 1.2s 0.45s ease-in-out infinite;
}

@keyframes heart-pop {
  from {
    opacity: 0;
    transform: scale(0) translateY(8px);
  }
}

@keyframes heart-pulse {
  0%, 60%, 100% { transform: scale(1); }
  12% { transform: scale(1.3); }
  28% { transform: scale(1); }
  40% { transform: scale(1.2); }
}

.reveal.burst {
  filter: drop-shadow(0 0 22px rgba(255, 224, 138, 0.95));
  animation: pulse 0.5s ease-out;
}

.reveal.rise {
  filter: drop-shadow(0 0 16px rgba(143, 206, 78, 0.65));
  animation: rise 1.1s ease-out;
}

#reveal-equation {
  font-size: clamp(1.2rem, 6.5vw, 1.7rem);
  color: var(--gold);
  min-height: 1.4em;
  letter-spacing: 0.03em;
}

#ending-card {
  background: var(--panel);
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line), 0 6px 0 rgba(0, 0, 0, 0.45);
  padding: 22px 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  opacity: 0;
  transition: opacity 0.8s;
}

#ending-card.show {
  opacity: 1;
}

#card-msg {
  font-size: 0.95rem;
  line-height: 1.8;
  white-space: pre-line;
}

.card-media-el {
  display: block;
  width: 100%;
  max-height: 260px;
  object-fit: contain;
  background: var(--bg-deep);
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line);
}

#card-sub {
  font-size: 0.85rem;
  color: var(--pea);
  white-space: pre-line;
}

#card-from {
  font-size: 0.75rem;
  color: var(--muted);
}

#btn-replay {
  margin-top: 10px;
}

.confetti {
  position: fixed;
  top: -12px;
  width: 8px;
  height: 8px;
  z-index: 50;
  pointer-events: none;
  animation-name: fall;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
}

/* ---------- 설정 페이지 ---------- */

.setup {
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 16px 0 40px;
}

.setup h1 {
  font-size: 1.4rem;
  text-align: center;
}

.setup h2 {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 8px;
}

.setup-desc {
  font-size: 0.82rem;
  line-height: 1.8;
  color: var(--muted);
  text-align: center;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}

.setup input,
.setup textarea {
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--ink);
  background: #1b1e3a;
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line);
  padding: 11px 12px;
  resize: none;
}

.sprite-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sprite-field-label {
  font-size: 0.78rem;
  color: var(--muted);
}

.sprite-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.mode-picker {
  display: flex;
  gap: 10px;
}

.mode-opt {
  flex: 1;
  padding: 11px 8px;
  font-family: inherit;
  font-size: 0.85rem;
  background: #1b1e3a;
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line);
  color: var(--muted);
  cursor: pointer;
}

.mode-opt.selected {
  box-shadow: inset 0 0 0 2px var(--gold);
  color: var(--gold);
}

.sprite-opt {
  flex: 1 1 0;
  min-width: 62px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 4px 8px;
  background: #1b1e3a;
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line);
  color: var(--muted);
  font-size: 0.68rem;
  cursor: pointer;
}

.sprite-opt canvas {
  width: 44px;
  image-rendering: pixelated;
}

.sprite-opt.selected {
  box-shadow: inset 0 0 0 2px var(--gold);
  color: var(--gold);
}

.pv-sprite {
  width: 72px;
  align-self: center;
}

.preview-card {
  background: var(--panel);
  border: 3px solid var(--bg-deep);
  box-shadow: inset 0 0 0 2px var(--panel-line);
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.88rem;
  line-height: 1.7;
  white-space: pre-line;
}

.pv-equation {
  font-size: 1.25rem;
  color: var(--gold);
}

.pv-sub {
  color: var(--pea);
  font-size: 0.8rem;
}

.pv-from {
  color: var(--muted);
  font-size: 0.72rem;
}

#result {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#f-link {
  width: 100%;
  font-size: 0.75rem;
}

.result-btns {
  display: flex;
  gap: 8px;
}

.result-btns .px-btn {
  flex: 1;
  font-size: 0.8rem;
  padding: 11px 8px;
}

.upload-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: -4px;
}

.upload-status {
  font-size: 0.72rem;
  color: var(--muted);
}

.setup input[type='file'] {
  font-size: 0.72rem;
}

.setup-note {
  font-size: 0.72rem;
  line-height: 1.8;
  color: var(--muted);
  text-align: center;
}

/* ---------- 애니메이션 ---------- */

@keyframes blink {
  50% { opacity: 0; }
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes pulse {
  30% { transform: scale(1.25); }
}

@keyframes rise {
  from {
    transform: translateY(26px) scale(0.55);
    opacity: 0;
  }
}

@keyframes fall {
  to { transform: translateY(112vh) rotate(560deg); }
}

@media (prefers-reduced-motion: reduce) {
  .blink,
  .title-x,
  .heart.pulse,
  .reveal.burst,
  .reveal.rise {
    animation: none;
  }

  #stage-intro .intro-label,
  #stage-intro .intro-line1,
  #stage-intro .intro-line2 {
    animation: none;
    opacity: 1;
  }

  .reveal-heart {
    animation: none;
  }
}
