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

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 100%;
  background: #0f172a;
  color: #e2e8f0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-y: auto;
}

:root {
  /* HUD + 컨트롤 + 여백이 차지하는 세로 공간 (게임 영역 계산용) */
  --reserved: 200px;
}

/* 게임 전체 폭을 화면 크기에 맞춰 9:16 비율로 스케일.
   가로가 좁으면 화면 폭에 맞추고, 세로가 남으면 높이에 맞춰 커진다. */
#wrap {
  text-align: center;
  padding: 4px 0 8px;
  margin: 0 auto;
  width: min(calc(100vw - 8px), calc((100vh - var(--reserved)) * 9 / 16));
  width: min(calc(100vw - 8px), calc((100dvh - var(--reserved)) * 9 / 16));
}

/* ---- Title / character select screen ---- */

#titleScreen {
  border-radius: 12px;
}

#titleArt {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 16;
  background-image: url("image/Title.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  border-radius: 12px;
  border: 2px solid #334155;
  margin-bottom: 14px;
  overflow: hidden;
}

#titleOverlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px 10px 14px;
  background: linear-gradient(to top, rgba(15, 23, 42, 0.9) 40%, rgba(15, 23, 42, 0));
}

#howToPlay {
  margin: 16px 0 0;
  padding: 10px 12px;
  background: rgba(15, 23, 42, 0.7);
  border: 2px solid #334155;
  border-radius: 8px;
}

#howToPlay p {
  font-size: 0.85rem;
  font-weight: bold;
  color: #e2e8f0;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
  margin: 0 0 6px;
  text-align: left;
}

#howToPlay p:last-child {
  margin-bottom: 0;
}

#charGrid {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
}

.charCard {
  flex: 1;
  max-width: 120px;
  background: rgba(15, 23, 42, 0.85);
  border: 2px solid #334155;
  border-radius: 10px;
  padding: 10px 6px;
  cursor: pointer;
}

.charCard.selected {
  border-color: #38bdf8;
  background: rgba(15, 41, 66, 0.9);
}

.charCard img {
  width: 100%;
  max-width: 64px;
  height: auto;
  margin: 0 auto 6px;
  display: block;
}

.charCard .charName {
  font-weight: bold;
  font-size: 0.9rem;
  margin-bottom: 4px;
}

.statRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  font-size: 0.7rem;
  color: #94a3b8;
  margin-bottom: 2px;
}

.pips {
  display: flex;
  gap: 2px;
}

.pip {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: #334155;
}

.pip.filled {
  background: #facc15;
}

#startBtn {
  display: block;
  width: 100%;
  max-width: 320px;
  margin: 0 auto;
  padding: 16px 32px;
  font-size: 1.3rem;
  font-weight: bold;
  border-radius: 10px;
  border: 2px solid #0ea5e9;
  background: #0369a1;
  color: #e2e8f0;
}

#startBtn:hover {
  background: #0284c7;
  border-color: #38bdf8;
}

#startBtn:active {
  background: #075985;
  border-color: #0ea5e9;
}

#startBtn:disabled {
  opacity: 0.4;
  border-color: #475569;
  background: #1e293b;
}

/* ---- HUD ---- */

#hud {
  margin: 0 auto 8px;
  padding: 0 4px;
}

.barWrap {
  width: 100%;
  height: 14px;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 7px;
  overflow: hidden;
  margin-bottom: 6px;
}

.barFill {
  height: 100%;
  width: 100%;
  transition: width 0.15s linear;
}

.timerFill {
  background: #38bdf8;
}

.hpFill {
  background: #4ade80;
}

.segmentBar {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.hpSeg {
  flex: 1;
  height: 14px;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 4px;
  overflow: hidden;
}

.hpSegFill {
  height: 100%;
  width: 0%;
  background: #4ade80;
  transition: width 0.15s linear;
}

#scoreRow {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  padding: 0 2px;
  gap: 4px;
}

#invincibleIndicator {
  margin-top: 6px;
  padding: 4px 10px;
  border-radius: 6px;
  background: rgba(168, 85, 247, 0.2);
  border: 1px solid #a855f7;
  color: #d8b4fe;
  font-size: 0.8rem;
  font-weight: bold;
  text-align: center;
}

/* ---- Canvas + overlays ---- */

#canvasWrap {
  position: relative;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 9 / 16;
  background: #1e293b;
  border: 2px solid #334155;
  border-radius: 8px;
  touch-action: none;
  user-select: none;
}

#countdownOverlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-size: 4rem;
  font-weight: bold;
  color: #e2e8f0;
  background: rgba(15, 23, 42, 0.6);
  border-radius: 8px;
  pointer-events: none;
  padding: 16px;
  text-align: center;
}

.countdownCaption {
  font-size: 1rem;
  font-weight: bold;
  color: #e2e8f0;
}

#flashOverlay {
  position: absolute;
  inset: 0;
  border-radius: 8px;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at center, rgba(220, 38, 38, 0) 55%, rgba(220, 38, 38, 0.6) 100%);
}

#flashOverlay.flash {
  animation: flashPulse 1s ease-in-out infinite;
}

@keyframes flashPulse {
  0%, 100% { opacity: 0.15; }
  50% { opacity: 0.75; }
}

/* ---- Controls ---- */

#controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
  padding: 0 8px;
}

#hint {
  margin-top: 8px;
  font-size: 0.85rem;
  color: #94a3b8;
}

.dpad {
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px;
  grid-template-areas:
    ".    up    ."
    "left down right";
  gap: 4px;
}

.dirBtn {
  border-radius: 6px;
  border: 2px solid #475569;
  background: #1e293b;
  color: #e2e8f0;
  font-size: 0.95rem;
  font-weight: bold;
  touch-action: manipulation;
}

.dirBtn.up { grid-area: up; }
.dirBtn.left { grid-area: left; }
.dirBtn.right { grid-area: right; }
.dirBtn.down { grid-area: down; }

.attackBig {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 2px solid #0ea5e9;
  background: #0369a1;
  color: #e2e8f0;
  font-size: 1rem;
  font-weight: bold;
  touch-action: manipulation;
}

.attackBig:disabled {
  opacity: 0.4;
  border-color: #475569;
  background: #1e293b;
}

/* ---- Game over modal ---- */

#gameOverModal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.8);
  z-index: 10;
  padding: 16px;
}

.modalBox {
  background: #1e293b;
  border: 2px solid #334155;
  border-radius: 12px;
  padding: 28px 32px;
  text-align: center;
  min-width: 240px;
}

#gameOverTitle {
  font-size: 1.4rem;
  font-weight: bold;
  color: #94a3b8;
  margin-bottom: 12px;
}

#gameOverTitle.complete {
  color: #facc15;
}

#gameOverScore, #gameOverBest {
  font-size: 1.5rem;
  font-weight: bold;
  color: #facc15;
  margin-bottom: 4px;
}

#gameOverGrade {
  font-size: 1.2rem;
  font-weight: bold;
  color: #e2e8f0;
  margin-bottom: 10px;
}

.modalButtons {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.modalButtons button {
  flex: 1;
  padding: 10px 0;
  font-size: 0.95rem;
  font-weight: bold;
  border-radius: 8px;
  color: #e2e8f0;
}

#retryBtn {
  border: 2px solid #0ea5e9;
  background: #0369a1;
}

#changeCharBtn {
  border: 2px solid #475569;
  background: #334155;
}
