:root {
  --ink: #16131c;
  --panel: rgba(22, 19, 28, 0.86);
  --paper: #fbf6ec;
  --muted: #a69fb4;
  --accent: #ffcd4f;
  --red: #eb1622;
  --xp: #4fb3ff;
  --font: "Galmuri11", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  --font-big: "Galmuri14", "Galmuri11", "Apple SD Gothic Neo", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

canvas#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  touch-action: none;
  image-rendering: pixelated;
}

[hidden] { display: none !important; }

/* HUD */
#hud {
  position: fixed;
  inset: 0 0 auto 0;
  padding: calc(env(safe-area-inset-top) + 0px) 0 0;
  pointer-events: none;
}
.xp {
  position: relative;
  height: 14px;
  background: rgba(0, 0, 0, 0.55);
  border-bottom: 2px solid var(--ink);
}
.xp-fill {
  height: 100%;
  width: 0;
  background: var(--xp);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2);
}
.lv {
  position: absolute;
  right: 8px;
  top: 0;
  font-size: 11px;
  line-height: 14px;
  text-shadow: 1px 1px 0 var(--ink);
}
.hud-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px 0;
}
.age-box { text-shadow: 2px 2px 0 var(--ink); }
.age { font-family: var(--font-big); font-size: 30px; line-height: 1; }
.age span { color: var(--accent); }
.era-name { font-size: 12px; margin-top: 4px; color: var(--paper); opacity: 0.9; }
.timeline { display: flex; gap: 3px; margin-top: 4px; }
.timeline .seg {
  width: 28px;
  height: 8px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.25);
  overflow: hidden;
}
.timeline .seg i { display: block; height: 100%; width: 0; background: var(--accent); }
.weapons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px 12px 0;
  max-width: 380px;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px 4px;
  font-size: 11px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.15);
}
.chip img { width: 32px; height: 32px; image-rendering: pixelated; }
.chip b { color: var(--accent); font-weight: normal; }
.chip.old { opacity: 0.55; }

/* 보스 */
.bossbar {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 40px);
  left: 50%;
  transform: translateX(-50%);
  width: min(420px, 64vw);
  text-align: center;
  pointer-events: none;
}
.boss-name { font-family: var(--font-big); font-size: 15px; color: #f6757a; text-shadow: 2px 2px 0 #000; margin-bottom: 4px; }
.boss-track { height: 8px; background: rgba(0, 0, 0, 0.6); border: 2px solid #181425; }
.boss-track i { display: block; height: 100%; width: 0; background: #a22633; box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.3); }
.boss-card { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.boss-label { color: #8b9bb4; font-size: 13px; letter-spacing: 2px; animation: era-rise 0.6s ease-out both; }
.boss-title {
  font-family: var(--font-big);
  font-size: clamp(30px, 9vw, 64px);
  max-width: 92vw;
  text-align: center;
  word-break: keep-all; /* 긴 이름은 띄어쓰기에서 줄바꿈 */
  line-height: 1.2;
  color: #e43b44;
  text-shadow: 3px 3px 0 #000;
  animation: boss-in 0.9s ease-out both, boss-shake 0.12s 0.9s linear 6;
}
@keyframes boss-in { from { opacity: 0; transform: scale(1.3); filter: blur(4px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes boss-shake { 0%, 100% { transform: translate(0, 0); } 25% { transform: translate(-2px, 1px); } 75% { transform: translate(2px, -1px); } }

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 12%;
  transform: translateX(-50%);
  padding: 10px 16px;
  background: var(--panel);
  border: 2px solid var(--accent);
  text-align: center;
  font-size: 14px;
  pointer-events: none;
  max-width: calc(100vw - 32px);
}
.toast small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }

/* joystick */
#joystick {
  position: fixed;
  width: 96px;
  height: 96px;
  margin: -48px 0 0 -48px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.08);
  pointer-events: none;
}
#joystick .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
}

/* overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px 16px;
  background: rgba(14, 11, 20, 0.72);
  text-align: center;
  overflow-y: auto;
}
.overlay h1, .overlay h2 { font-family: var(--font-big); margin: 0; font-weight: normal; text-shadow: 3px 3px 0 #000; }
.overlay h1 { font-size: clamp(32px, 9vw, 56px); color: var(--accent); }
.overlay h2 { font-size: clamp(24px, 6vw, 36px); }
.overlay p { margin: 0; line-height: 1.6; color: var(--paper); }
.overlay .muted { color: var(--muted); font-size: 13px; }
.overlay img.hero { width: 120px; image-rendering: pixelated; }

.btn {
  font: inherit;
  font-family: var(--font-big);
  font-size: 18px;
  padding: 12px 22px;
  color: var(--ink);
  background: var(--accent);
  border: 3px solid var(--ink);
  box-shadow: 4px 4px 0 #000;
  cursor: pointer;
}
.btn:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 #000; }

.cards {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 780px;
}
.card {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  text-align: left;
  font: inherit;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  box-shadow: 5px 5px 0 #000;
  cursor: pointer;
}
.card:hover, .card:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* 레벨업 카드 연출: 등장 → 빛 지나감 → 고르면 번쩍이며 사라짐 */
.overlay.levelup { animation: lv-dim 0.25s ease-out both; }
.overlay.levelup.closing { animation: lv-undim 0.3s 0.12s ease-in forwards; }
.lv-title { animation: lv-title 0.5s cubic-bezier(0.2, 1.4, 0.4, 1) both; }
.overlay.levelup .card {
  position: relative;
  overflow: hidden;
  animation: card-in 0.45s cubic-bezier(0.2, 1.3, 0.4, 1) both;
  animation-delay: calc(var(--i) * 80ms + 0.08s);
}
.overlay.levelup .card::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(110deg, transparent 35%, rgba(255, 255, 255, 0.85) 50%, transparent 65%);
  transform: translateX(-130%);
  animation: card-shine 0.6s ease-out both;
  animation-delay: calc(var(--i) * 80ms + 0.4s);
  pointer-events: none;
}
.overlay.levelup .card.picked { animation: card-pick 0.42s ease-out forwards; z-index: 1; }
.overlay.levelup .card.dismissed { animation: card-out 0.3s ease-in forwards; pointer-events: none; }
@keyframes lv-dim { from { background: rgba(14, 11, 20, 0); } }
@keyframes lv-undim { to { background: rgba(14, 11, 20, 0); } }
@keyframes lv-title { from { transform: scale(1.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes card-in { from { transform: translateY(48px) scale(0.85); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes card-shine { to { transform: translateX(130%); } }
@keyframes card-pick {
  0% { transform: scale(1); }
  30% { transform: scale(1.08); background: #fff; box-shadow: 0 0 0 4px #fff, 0 0 28px 10px var(--accent); }
  100% { transform: translateY(-60px) scale(0.5); opacity: 0; box-shadow: 0 0 0 4px #fff, 0 0 28px 10px var(--accent); }
}
@keyframes card-out { to { transform: translateY(70px) scale(0.9); opacity: 0; } }
.card .top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card .icon { width: 48px; height: 48px; image-rendering: pixelated; }
.card .lvl { font-size: 12px; padding: 2px 6px; background: var(--ink); color: var(--accent); }
.card .name { font-family: var(--font-big); font-size: 17px; line-height: 1.3; }
.card .quote { font-size: 12px; line-height: 1.5; color: #5b5468; }
.card .effect { font-size: 13px; line-height: 1.4; color: #b3161f; }
.card .key { font-size: 11px; color: #8b849a; }

.overlay.era-mode { background: transparent; pointer-events: none; }
.overlay.fade-out { opacity: 0; transition: opacity 0.35s ease-out; }
.era-card { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.era-card .age-big { font-family: var(--font-big); font-size: clamp(56px, 16vw, 96px); color: var(--accent); line-height: 1; text-shadow: 4px 4px 0 #000; animation: era-pop 0.55s cubic-bezier(0.2, 1.3, 0.4, 1) both; will-change: transform, opacity; }
.era-card .event { font-size: clamp(18px, 5vw, 24px); animation: era-rise 0.45s 0.2s ease-out both; }
.era-card .title { color: var(--muted); animation: era-rise 0.45s 0.35s ease-out both; }
@keyframes era-pop { from { transform: scale(1.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes era-rise { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }

/* 일시정지 */
.pause-btn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 44px);
  right: 12px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0;
  background: rgba(0, 0, 0, 0.5);
  border: 2px solid rgba(255, 255, 255, 0.35);
  cursor: pointer;
}
.pause-btn i { display: block; width: 5px; height: 14px; background: var(--paper); }
.pause-btn:active { transform: translate(1px, 1px); }
.overlay[data-kind="pause"] { background: rgba(14, 11, 20, 0.8); }
.menu { display: flex; flex-direction: column; gap: 12px; width: min(280px, 100%); }
.menu .btn { width: 100%; outline: none; }
.btn.ghost { color: var(--paper); background: rgba(0, 0, 0, 0.45); border-color: rgba(255, 255, 255, 0.35); box-shadow: 4px 4px 0 #000; }
.btn.ghost.off { color: var(--muted); }
.btn small { display: block; margin-top: 4px; font-family: var(--font); font-size: 11px; color: var(--muted); }
.btn.ghost.warn { color: var(--ink); background: #f6757a; }
.menu .btn.focus { outline: 3px solid var(--accent); outline-offset: 3px; }
/* 폰에는 키보드가 없으니 키 안내를 숨긴다 */
@media (hover: none) { .overlay[data-kind="pause"] .muted { display: none; } }

.stats { display: grid; grid-template-columns: auto auto; gap: 6px 18px; font-size: 14px; }
.stats dt { color: var(--muted); text-align: right; }
.stats dd { margin: 0; text-align: left; }

@media (max-width: 640px) {
  .cards { flex-direction: column; gap: 10px; }
  .card { padding: 10px 12px; gap: 4px; }
  .card .icon { width: 32px; height: 32px; }
  .chip img { width: 16px; height: 16px; }
  .card .key { display: none; }
  .weapons { max-width: 220px; }
}
