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

:root {
  --bg: #1e1b2e;
  --panel: #2b2742;
  --text: #f3f0ff;
  --muted: #a8a3c4;
  --primary: #f97316;
  --primary-dark: #ea580c;
  --good: #16a34a;
  --bad: #dc2626;
}

body {
  font-family: system-ui, -apple-system, "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

.hidden { display: none !important; }

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  padding: 12px 20px;
  background: var(--primary);
  color: white;
  font-weight: 700;
  transition: transform 0.1s, background 0.2s;
}
button:hover:not(:disabled) { background: var(--primary-dark); }
button:active:not(:disabled) { transform: scale(0.96); }
button:disabled { opacity: 0.45; cursor: default; }
button.secondary { background: #4b4570; }
button.secondary:hover:not(:disabled) { background: #5d568a; }

#app { width: 100%; max-width: 760px; padding: 12px 16px 24px; }

/* ===== 상태창 ===== */
.hud {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap;
  gap: 8px 14px;
  background: var(--panel);
  border-radius: 14px;
  padding: 10px 14px;
  margin-bottom: 10px;
  font-size: 0.95rem;
}
.hud-left { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.lv { background: var(--primary); padding: 2px 8px; border-radius: 8px; font-size: 0.85rem; }
.hud-bars { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 160px; }
.mini-bar { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; color: var(--muted); }
.mini-bar span { width: 18px; }
.mini-bar b { color: var(--text); min-width: 52px; text-align: right; }
.mini-bar .bar { flex: 1; height: 10px; }
.hud-right { display: flex; gap: 12px; align-items: center; }
.pieces { letter-spacing: 1px; }

.bar { height: 12px; background: #15131f; border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: 100%; border-radius: 99px; transition: width 0.35s; }
.bar-fill.hp { background: linear-gradient(90deg, #22c55e, #4ade80); }
.bar-fill.xp { background: linear-gradient(90deg, #6366f1, #a78bfa); }
.bar-fill.mon { background: linear-gradient(90deg, #ef4444, #f87171); }

/* ===== 게임 화면 ===== */
.stage { position: relative; border-radius: 14px; overflow: hidden; line-height: 0; }
canvas { width: 100%; height: auto; display: block; image-rendering: pixelated; background: #000; }

.zone-banner {
  position: absolute; top: 14px; left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 18, 32, 0.85);
  padding: 8px 20px; border-radius: 99px;
  font-weight: 800; font-size: 1.05rem; line-height: 1.4;
  opacity: 0; transition: opacity 0.4s;
  pointer-events: none; white-space: nowrap;
}
.zone-banner.show { opacity: 1; }

/* ===== 대화창 ===== */
.dialog {
  position: absolute; left: 12px; right: 12px; bottom: 12px;
  background: rgba(255, 255, 255, 0.97);
  color: #1f2937;
  border: 4px solid #3b3561;
  border-radius: 14px;
  padding: 14px 18px 12px;
  line-height: 1.6;
  cursor: pointer;
}
.dlg-name {
  position: absolute; top: -16px; left: 16px;
  background: #3b3561; color: white;
  padding: 2px 12px; border-radius: 8px;
  font-weight: 800; font-size: 0.9rem;
}
.dlg-text { font-size: 1.05rem; min-height: 50px; word-break: keep-all; }
.dlg-options { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.dlg-options button { padding: 8px 14px; font-size: 0.95rem; }
.dlg-next { position: absolute; right: 14px; bottom: 6px; color: var(--primary); animation: blink 1s infinite; font-size: 0.85rem; }
@keyframes blink { 50% { opacity: 0.2; } }

/* ===== 방향키 ===== */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(2, 56px);
  gap: 6px;
  justify-content: center;
  margin-top: 12px;
  touch-action: none;
}
.dpad button { font-size: 1.4rem; padding: 0; background: var(--panel); }
.dpad button:active { background: var(--primary); }
.dpad [data-dir="up"] { grid-column: 2; grid-row: 1; }
.dpad [data-dir="left"] { grid-column: 1; grid-row: 2; }
.dpad [data-dir="down"] { grid-column: 2; grid-row: 2; }
.dpad [data-dir="right"] { grid-column: 3; grid-row: 2; }
@media (hover: none), (max-width: 700px) { .dpad { display: grid; } }

.help { text-align: center; color: var(--muted); font-size: 0.85rem; margin-top: 10px; }

/* ===== 덮개 화면 (배틀, 시작, 엔딩) ===== */
.overlay {
  position: fixed; inset: 0;
  background: rgba(15, 13, 25, 0.92);
  display: flex; justify-content: center; align-items: flex-start;
  overflow-y: auto;
  padding: 16px;
  z-index: 10;
}

.title-box {
  margin: auto;
  background: var(--panel);
  border-radius: 20px;
  padding: 32px 24px;
  text-align: center;
  width: 100%; max-width: 420px;
}
.title-emoji { font-size: 3.4rem; }
.title-box h1 { font-size: 1.9rem; margin: 8px 0; }
.sub { color: var(--muted); margin-bottom: 20px; line-height: 1.6; }
#nameInput {
  width: 100%; font-size: 1.2rem; text-align: center;
  padding: 12px; border-radius: 12px;
  border: 3px solid #4b4570; background: #1e1b2e; color: var(--text);
  margin-bottom: 16px;
}
#nameInput:focus { outline: none; border-color: var(--primary); }
.title-btns { display: flex; flex-direction: column; gap: 10px; }

/* ===== 배틀 ===== */
.battle-box { width: 100%; max-width: 620px; margin: auto; }
.arena {
  display: flex; align-items: center; justify-content: space-between;
  background: linear-gradient(180deg, #3b3561, #2b2742);
  border-radius: 18px;
  padding: 16px;
}
.fighter { position: relative; width: 40%; text-align: center; }
.f-name { font-weight: 800; margin-bottom: 4px; font-size: 0.95rem; }
.f-emoji { font-size: 4rem; line-height: 1.2; }
#bMonster.boss .f-emoji { font-size: 5.2rem; }
.bar-text { font-size: 0.8rem; color: var(--muted); margin-top: 3px; }
.vs { font-size: 1.5rem; font-weight: 900; color: var(--primary); }

.hit { animation: hit 0.45s; }
@keyframes hit {
  0%, 100% { transform: translateX(0); filter: none; }
  20% { transform: translateX(-10px); filter: brightness(2.2); }
  40% { transform: translateX(10px); }
  60% { transform: translateX(-6px); filter: brightness(2.2); }
}
.dmg {
  position: absolute; left: 50%; top: 20%;
  transform: translateX(-50%);
  font-weight: 900; font-size: 1.5rem;
  pointer-events: none;
  animation: floatUp 0.9s forwards;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}
.dmg.good { color: #fde047; }
.dmg.bad { color: #f87171; }
@keyframes floatUp { to { transform: translate(-50%, -50px); opacity: 0; } }

.combo { height: 30px; text-align: center; font-weight: 900; color: #fdba74; margin: 6px 0; line-height: 30px; }
.combo.pop { animation: pop 0.4s; }
@keyframes pop { 50% { transform: scale(1.3); } }

.qcard {
  background: white;
  color: #1f2937;
  border-radius: 18px;
  padding: 20px 18px;
  text-align: center;
}
.tag {
  display: inline-block;
  background: #ffedd5; color: var(--primary-dark);
  font-size: 0.8rem; font-weight: 700;
  padding: 4px 12px; border-radius: 99px;
  margin-bottom: 10px;
}
#qText { font-size: 1.2rem; line-height: 1.5; margin-bottom: 14px; word-break: keep-all; }
#qVisual { margin-bottom: 14px; }
#qVisual svg { max-width: 100%; height: auto; }
.legend { font-size: 0.85rem; color: #6b7280; margin-top: 6px; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice {
  background: #f3f4f6; color: #1f2937;
  border: 3px solid transparent;
  font-size: 1.05rem; padding: 14px;
}
.choice:hover:not(:disabled) { background: #ffedd5; border-color: #fdba74; }
.ox { display: flex; gap: 16px; justify-content: center; }
.ox .choice { font-size: 2.2rem; width: 110px; height: 86px; }
.choice.correct { background: #dcfce7; border-color: var(--good); opacity: 1; }
.choice.wrong { background: #fee2e2; border-color: var(--bad); opacity: 1; }
.choice.removed { visibility: hidden; }

.input-row { display: flex; gap: 8px; justify-content: center; align-items: center; }
.input-row input {
  width: 120px; font-size: 1.3rem; padding: 10px; text-align: center;
  border: 3px solid #fed7aa; border-radius: 12px;
}
.input-row input:focus { outline: none; border-color: var(--primary); }
.unit { font-size: 1.2rem; font-weight: 700; }

.net-cell { cursor: pointer; }
.net-cell:hover { filter: brightness(0.93); }
.locked .net-cell { cursor: default; }

#feedback { margin-top: 16px; padding: 14px; border-radius: 14px; text-align: left; line-height: 1.6; }
#feedback.good { background: #dcfce7; }
#feedback.bad { background: #fee2e2; }
.fb-title { font-size: 1.15rem; font-weight: 800; margin-bottom: 4px; }
#feedback button { margin-top: 12px; width: 100%; }

.items { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.item-btn { background: var(--panel); font-size: 0.95rem; padding: 10px 14px; }
.item-btn:hover:not(:disabled) { background: #3b3561; }

.flash { animation: flash 0.4s; }
@keyframes flash { 30% { background: rgba(220, 38, 38, 0.55); } }

@media (max-width: 480px) {
  .hud { font-size: 0.85rem; }
  .f-emoji { font-size: 3rem; }
  #bMonster.boss .f-emoji { font-size: 3.8rem; }
  #qText { font-size: 1.05rem; }
  .choice { font-size: 0.95rem; padding: 12px 8px; }
}
