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

:root {
  --bg: #1a1a2e;
  --panel: #16213e;
  --accent: #e94560;
  --gold: #f5c842;
  --text: #eee;
  --grid-border: #334;
  --grid-bg: #0f0f23;
  --cell: 100px;
  --hand-cell: 46px;
}

body {
  background: #000;
  color: var(--text);
  font-family: 'Segoe UI', sans-serif;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
}

/* 9:16 고정 비율 컨테이너 — 뷰포트에 맞춰 자동 축소 */
#game-container {
  width: min(100vw, 56.25dvh, 450px);
  height: min(100dvh, 177.78vw, 800px);
  position: relative;
  overflow: hidden;
  background: var(--bg);
}

/* ===== Screens ===== */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.screen.active { display: flex; }

/* ===== Title ===== */
.title-content { text-align: center; }
.title-content h1 {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
  color: var(--gold);
}
.subtitle {
  color: #888;
  margin-bottom: 2rem;
  font-size: 1rem;
}

/* ===== Buttons ===== */
.btn-main {
  background: var(--accent);
  color: white;
  border: none;
  padding: 14px 40px;
  font-size: 1.2rem;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.1s;
}
.btn-main:active { transform: scale(0.95); }

.btn-action {
  background: var(--panel);
  color: var(--text);
  border: 2px solid #445;
  padding: 12px 24px;
  font-size: 1.1rem;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.1s;
}
.btn-action:active { transform: scale(0.95); }
.btn-action:disabled { opacity: 1; cursor: default; background: rgba(22,33,62,0.4); border-color: rgba(255,255,255,0.3); color: rgba(255,255,255,0.6); }
.btn-action:disabled:active { transform: none; }

/* ===== Stage Intro ===== */
.intro-content { text-align: center; padding: 2rem; }
.intro-content h2 {
  font-size: 1.8rem;
  color: var(--gold);
  margin-bottom: 1rem;
}
.intro-content p {
  margin-bottom: 0.8rem;
  font-size: 1.1rem;
  line-height: 1.6;
}
#stage-goal {
  color: var(--gold);
  font-weight: bold;
  font-size: 1.3rem;
  margin: 1.5rem 0;
}

/* ===================================================
   게임 화면: 상단 1/3 월드 + 하단 2/3 가방
   =================================================== */
#game-screen {
  justify-content: flex-start;
}

/* --- 상단: 월드 (1/3) --- */
#top-half {
  width: 100%;
  height: 33.33%;
  position: relative;
  overflow: hidden;
  background: linear-gradient(180deg, #2d1b4e 0%, #1a1a2e 60%, #0d1117 100%);
  flex-shrink: 0;
  transition: filter 0.3s;
}
#top-half.slowmo {
  filter: brightness(0.7) saturate(0.5);
}

#hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  justify-content: center;
  padding: 6px 10px;
  background: rgba(0,0,0,0.6);
  z-index: 10;
  font-size: 0.85rem;
  font-weight: bold;
}

#progress-bar {
  position: absolute;
  top: 30px; left: 0; right: 0;
  height: 6px;
  background: #111;
  z-index: 10;
}
#progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s linear;
}
#progress-flag {
  position: absolute;
  right: 4px;
  top: -8px;
  font-size: 0.9rem;
  z-index: 11;
}

#world-viewport {
  position: absolute;
  top: 36px; left: 0; right: 0; bottom: 0;
  overflow: hidden;
}

#world {
  position: absolute;
  bottom: 0; left: 0;
  height: 100%;
}

#ground {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 36px;
  background: linear-gradient(180deg, #2a4a2a 0%, #1a3a1a 100%);
}

#player {
  position: absolute;
  bottom: 28px;
  left: 60px;
  width: 30px;
  height: 48px;
  z-index: 5;
  transform-origin: bottom center;
  animation: walkBob 0.4s ease-in-out infinite;
}
#player.picking {
  animation: pickBounce 0.3s ease;
}
@keyframes walkBob {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50%      { transform: translateY(-1px) rotate(1deg); }
}
@keyframes pickBounce {
  0%   { transform: scaleY(1); }
  40%  { transform: scaleY(0.7); }
  100% { transform: scaleY(1); }
}

/* 월드 아이템 */
.world-item {
  position: absolute;
  bottom: 30px;
  font-size: 2rem;
  z-index: 3;
  transition: opacity 0.3s, transform 0.3s;
}
.world-item.picked {
  opacity: 0;
  transform: translateY(-20px) scale(0.5);
}
.world-item.passed { opacity: 0.3; }

.world-item.in-range {
  animation: bounce 0.5s ease-in-out infinite;
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.world-icon { display: block; width: 40px; height: 40px; }
.world-icon svg { width: 100%; height: 100%; }
.world-item.nel-item .world-icon { width: 48px; height: 48px; }

.world-item .item-label {
  position: absolute;
  top: -18px; left: 50%;
  transform: translateX(-50%);
  font-size: 0.7rem;
  color: #ccc;
  white-space: nowrap;
  pointer-events: none;
}
.world-item.in-range .item-label {
  color: var(--gold);
  font-weight: bold;
}
.world-item.nel-item {
  font-size: 1.7rem;
  bottom: 28px;
}
.world-item.nel-item .item-label {
  color: var(--accent);
  font-size: 0.75rem;
}

/* 줍기 버튼 — 경계선 걸침 */
#btn-pickup {
  position: absolute;
  top: 33.33%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 30;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  padding: 0;
  font-size: 1.1rem;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}
#btn-pickup:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  animation: pulse 0.8s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(233, 69, 96, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(233, 69, 96, 0); }
}

/* 말풍선 */
#speech-bubble {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  color: #222;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 10px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 60;
  margin-bottom: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
#speech-bubble::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #fff;
}
#speech-bubble.show {
  animation: bubblePop 1.2s ease-out forwards;
}
@keyframes bubblePop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.5); }
  10% { opacity: 1; transform: translateX(-50%) scale(1.05); }
  20% { opacity: 1; transform: translateX(-50%) scale(1); }
  80% { opacity: 1; transform: translateX(-50%) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-8px); }
}

/* 피드백 */
#pickup-feedback {
  position: absolute;
  top: calc(33.33% - 60px);
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.95rem;
  font-weight: bold;
  color: var(--gold);
  pointer-events: none;
  z-index: 50;
  text-align: center;
  max-width: 90%;
  line-height: 1.4;
}
#pickup-feedback.show {
  animation: feedbackPop 0.8s ease-out forwards;
}
@keyframes feedbackPop {
  0% { opacity: 1; transform: translateX(-50%) scale(0.5); }
  30% { opacity: 1; transform: translateX(-50%) scale(1.2); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-20px) scale(1); }
}

/* 꽉 참 흔들림 */
.shake-full {
  animation: shakeFull 0.4s ease !important;
}
@keyframes shakeFull {
  0%, 100% { margin-left: 0; }
  20% { margin-left: -3px; }
  40% { margin-left: 3px; }
  60% { margin-left: -2px; }
  80% { margin-left: 2px; }
}

/* ===================================================
   하단: 가방 (2/3 영역)
   =================================================== */
#bottom-half {
  width: 100%;
  height: 66.67%;
  background: var(--panel);
  border-top: 2px solid #445;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 10px 6px;
  flex-shrink: 0;
}

/* 쓰레기통 드롭 영역 */
#trash-zone {
  width: 100%;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 6px;
  border: 2px dashed transparent;
  border-radius: 8px;
  font-size: 1.1rem;
  color: #666;
  transition: all 0.2s;
}
#trash-zone:not(.hidden) {
  border-color: #555;
  background: rgba(233, 69, 96, 0.05);
}
#trash-zone.drag-over {
  border-color: var(--accent);
  background: rgba(233, 69, 96, 0.15);
  color: var(--accent);
}

/* 가방 헤더: [왼손] [금액] [오른손] */
#bag-header {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 10px;
  margin-bottom: 8px;
}

#bag-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 80px;
}
#bag-gold {
  color: var(--gold);
  font-weight: bold;
  font-size: 2.2rem;
}
#bag-goal {
  color: #aaa;
  font-size: 0.9rem;
}

/* 양손 슬롯 — 3x2 그리드 */
.hand-slot {
  background: var(--grid-bg);
  border: 2px solid var(--grid-border);
  border-radius: 4px;
  position: relative;
  flex-shrink: 0;
  width: calc(var(--hand-cell) * 3 + 2px + 4px);
  height: calc(var(--hand-cell) * 2 + 1px + 4px);
}
.hand-bg {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%) scaleX(-1);
  width: 60px;
  height: 60px;
  pointer-events: none;
  z-index: 0;
}
.hand-bg-right {
  transform: translate(-50%, -50%);
}
.hand-bg svg {
  width: 100%;
  height: 100%;
}

.hand-content {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}

/* 양손 안의 아이템 */
.hand-item-display {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  cursor: grab;
  z-index: 5;
  border: 2px solid transparent;
  transition: border-color 0.15s;
}
.hand-item-display:hover {
  border-color: var(--accent);
}
.hand-item-display .inv-icon svg { width: 30px; height: 30px; }
.hand-item-grid {
  position: absolute;
  top: 0; left: 0;
  display: grid;
  gap: 1px;
  pointer-events: none;
}
.hand-item-grid .hand-cell {
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.08);
}
.hand-item-display .item-value {
  position: absolute;
  bottom: 2px; right: 4px;
  font-size: 0.6rem;
  color: var(--gold);
  font-weight: bold;
}

/* 인벤토리 그리드 */
#inventory-grid {
  display: grid;
  grid-template-columns: repeat(4, var(--cell));
  grid-template-rows: repeat(3, var(--cell));
  gap: 2px;
  background: var(--grid-border);
  border: 2px solid var(--grid-border);
  border-radius: 4px;
  position: relative;
}
.grid-bg {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 80px;
  height: 80px;
  pointer-events: none;
  z-index: 0;
}
.grid-bg svg {
  width: 100%;
  height: 100%;
}

.grid-cell {
  width: var(--cell);
  height: var(--cell);
  background: var(--grid-bg);
}

/* SVG 아이콘 공통 */
.inv-icon { display: flex; align-items: center; justify-content: center; }
.inv-icon svg { width: 48px; height: 48px; }

/* 그리드 위 아이템 */
.inv-item {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2rem;
  border-radius: 4px;
  cursor: grab;
  z-index: 5;
  border: 2px solid rgba(255,255,255,0.4);
  transition: border-color 0.15s;
}
.inv-item:hover, .inv-item.selected {
  border-color: var(--accent);
}
.inv-item .item-value {
  position: absolute;
  bottom: 4px; right: 6px;
  font-size: 1rem;
  color: var(--gold);
  font-weight: bold;
}

/* ===== Result Screen ===== */
#result-screen {
  background: #0C0C0C;
}
.result-content {
  text-align: center;
  padding: 0 24px;
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  width: 100%;
}
#result-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: -1px;
}
#result-items {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  width: 100%;
}
.result-item {
  background: #1A1A1A;
  border: 1px solid #2A2A2A;
  border-radius: 8px;
  padding: 12px 8px;
  width: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.result-item .ri-icon { display: flex; align-items: center; justify-content: center; }
.result-item .ri-icon svg { width: 32px; height: 32px; }
.result-item .ri-name { color: #bbb; font-size: 0.7rem; font-weight: 500; }
.result-item .ri-value { color: var(--gold); font-size: 0.875rem; font-weight: 700; }

#result-divider {
  width: 200px;
  height: 1px;
  background: #2A2A2A;
}
#result-label {
  color: #999;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 1px;
}
#result-total {
  font-size: 3rem;
  color: var(--gold);
  font-weight: 700;
  letter-spacing: -2px;
}
#result-message {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #fff;
  max-width: 300px;
}
#btn-result {
  width: 200px;
  height: 52px;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  padding: 0;
}

/* ===== Dialog Screens (상인/넬/클리어) ===== */
.dialog-content {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, #2d1b4e 0%, #0C0C0C 30%, #0C0C0C 100%);
  padding: 0;
  position: relative;
}

#nel-clear-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  z-index: 1;
}
.clear-title {
  color: #f5c842;
  font-size: 42px;
  font-weight: 700;
  letter-spacing: -2px;
}
.clear-sub {
  color: #c4a0d0;
  font-size: 14px;
  font-weight: 500;
}

.dialog-illustration {
  width: 100%;
  height: 280px;
  overflow: hidden;
  flex-shrink: 0;
  margin-top: 100px;
}
.dialog-illustration img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.dialog-log {
  flex: 1;
  width: 100%;
  background: #0C0C0C;
  padding: 18px 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
}
.dialog-line {
  padding: 12px 0;
  border-bottom: 1px solid #1A1A1A;
}
.dialog-line:last-child {
  border-bottom: none;
}
.dialog-line .speaker-name {
  display: block;
  font-family: 'Sora', sans-serif;
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 4px;
}
.dialog-line .dialog-text {
  font-size: 15px;
  line-height: 1.7;
  color: #fff;
}
.dialog-line .speaker-name.merchant { color: #e8a84e; }
.dialog-line .speaker-name.nel { color: #c4a0d0; }

.dialog-tap-hint {
  text-align: center;
  padding: 12px 0 24px;
  color: #666;
  font-size: 11px;
  flex-shrink: 0;
}

.dialog-btn {
  background: #c4a0d0;
  color: #0C0C0C;
  font-weight: 700;
  border-radius: 12px;
  height: 44px;
  padding: 0 16px;
  flex-shrink: 0;
  border: none;
  font-size: 0.85rem;
  cursor: pointer;
}
#nel-links {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  flex-shrink: 0;
}
.nel-links-label {
  color: #c4a0d0;
  font-size: 14px;
  font-weight: 500;
}
.nel-links-row {
  display: flex;
  gap: 10px;
  font-size: 1rem;
  cursor: pointer;
}

/* ===== Ending ===== */
.ending-content {
  text-align: center;
  padding: 2rem;
  max-width: 400px;
  max-height: 90%;
  overflow-y: auto;
}
#ending-text {
  font-size: 1.1rem;
  line-height: 1.8;
  margin-bottom: 2rem;
  white-space: pre-line;
}
#ending-text .speaker { color: var(--gold); font-weight: bold; }
#ending-text .nel-angry { color: var(--accent); }

/* ===== Utility ===== */
.hidden { display: none !important; }

.screen.fade-in {
  animation: fadeIn 0.4s ease-in;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
