:root {
  --bg: #07070b;
  --ink: #ece9e4;
  --muted: #9a94a6;
  --line: rgba(255, 255, 255, 0.09);
  --crimson: #e23b3b;
  --crimson-soft: rgba(226, 59, 59, 0.16);
  --violet: #8b5cf6;
  --cyan: #4dd6ff;
  --gold: #e8c07a;
  --mono: "D2Coding", "Consolas", "SF Mono", ui-monospace, monospace;
  --sans: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background:
    radial-gradient(90% 60% at 15% 0%, rgba(226, 59, 59, 0.14), transparent 60%),
    radial-gradient(80% 60% at 88% 100%, rgba(139, 92, 246, 0.16), transparent 60%),
    var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  background: radial-gradient(60% 40% at 50% 45%, rgba(255, 255, 255, 0.03), transparent 70%);
  pointer-events: none;
}

/* ---------- App frame ---------- */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  overflow: hidden;
  isolation: isolate;
}

@media (min-width: 640px) {
  body { display: grid; place-items: center; }
  .app {
    width: 480px;
    height: min(940px, calc(100dvh - 40px));
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 20px;
    box-shadow: 0 40px 100px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(0, 0, 0, 0.7);
  }
}

/* ---------- Top bar ---------- */
.topbar {
  flex: 0 0 auto;
  padding: calc(9px + env(safe-area-inset-top)) 12px 0;
  background: linear-gradient(180deg, rgba(9, 9, 14, 0.98), rgba(9, 9, 14, 0.88));
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand { display: flex; align-items: center; gap: 7px; min-width: 0; }

.brand-mark {
  color: var(--crimson);
  font-size: 13px;
  text-shadow: 0 0 12px rgba(226, 59, 59, 0.9);
}

.brand-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid rgba(232, 192, 122, 0.35);
  background: rgba(232, 192, 122, 0.08);
  color: var(--gold);
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
}

.badge:hover { background: rgba(232, 192, 122, 0.16); border-color: rgba(232, 192, 122, 0.55); }
.badge:active { transform: scale(0.97); }

.badge-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 8px currentColor;
}

.badge.is-complete {
  color: #ffe9b0;
  border-color: rgba(255, 233, 176, 0.65);
  background: rgba(232, 192, 122, 0.18);
}

.topbar-sub { margin-top: 7px; }

.ep {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.18em;
  color: var(--muted);
  text-transform: uppercase;
  white-space: nowrap;
}

.tools { display: flex; gap: 6px; }

.ghost {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 0 9px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: #cfc9d4;
  font-family: var(--sans);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: -0.01em;
  cursor: pointer;
  transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease;
  white-space: nowrap;
}

.ghost:hover:not(:disabled) { color: #fff; border-color: rgba(255, 255, 255, 0.28); background: rgba(255, 255, 255, 0.07); }
.ghost:disabled { opacity: 0.32; cursor: default; }

.progress-track {
  height: 2px;
  margin: 9px -12px 0;
  background: rgba(255, 255, 255, 0.06);
}

.progress-track span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--crimson), var(--violet));
  box-shadow: 0 0 10px rgba(226, 59, 59, 0.7);
  transition: width 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ---------- Comic screen ---------- */
.screen {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: #050508;
}

.cut {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 26%;
  display: block;
}

/* 이미지를 못 받은 경우(WebP 미지원 등) 깨진 아이콘 대신 어두운 배경만 남긴다 */
.cut.is-missing,
.dex-thumb img.is-missing {
  opacity: 0;
}

.stage.anim .cut { animation: cutIn 0.5s cubic-bezier(0.2, 0.7, 0.3, 1) both; }

@keyframes cutIn {
  from { opacity: 0; transform: scale(1.045); filter: brightness(0.6) saturate(0.7); }
  to { opacity: 1; transform: scale(1); filter: brightness(1) saturate(1); }
}

.grade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(120% 78% at 50% 30%, transparent 38%, rgba(0, 0, 0, 0.62) 100%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.35) 0%, transparent 22%, transparent 72%, rgba(7, 7, 11, 0.9) 100%);
}

.grade::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.28;
  background: repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.035) 0 1px, transparent 1px 3px);
}

.stage-tag {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 4px 7px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(0, 0, 0, 0.45);
  color: rgba(255, 255, 255, 0.72);
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.22em;
  backdrop-filter: blur(4px);
}

/* ---------- System window ---------- */
.system {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: min(74%, 292px);
  padding: 9px 11px 10px;
  border-radius: 9px;
  border: 1px solid rgba(77, 214, 255, 0.34);
  background: rgba(5, 11, 18, 0.74);
  box-shadow:
    inset 0 0 0 1px rgba(77, 214, 255, 0.07),
    0 10px 30px rgba(0, 0, 0, 0.55),
    0 0 22px rgba(77, 214, 255, 0.14);
  backdrop-filter: blur(7px);
  animation: sysIn 0.55s ease 0.14s both;
}

@keyframes sysIn {
  from { opacity: 0; transform: translateY(8px) skewX(-1.5deg); }
  to { opacity: 1; transform: none; }
}

.system-head {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 5px;
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: #86e6ff;
}

.sys-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 9px var(--cyan);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.system-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 2px;
}

.system-list li {
  display: flex;
  gap: 6px;
  font-family: var(--mono);
  font-size: 10.5px;
  line-height: 1.55;
  letter-spacing: 0.01em;
  color: #cdeeff;
  word-break: keep-all;
}

.system-list li::before { content: "▸"; color: var(--cyan); opacity: 0.85; }

.system-list li.warn { color: #ff9d9d; }
.system-list li.warn::before { color: #ff6b6b; }

/* ---------- Text panel ---------- */
.panel {
  flex: 0 0 auto;
  max-height: 42%;
  overflow-y: auto;
  padding: 13px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 11px;
  background:
    linear-gradient(180deg, rgba(7, 7, 11, 0.2), rgba(7, 7, 11, 0.96) 26%),
    var(--bg);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  scrollbar-width: none;
}

.panel::-webkit-scrollbar { width: 0; height: 0; }

.narration {
  font-size: 13px;
  line-height: 1.78;
  color: #d9d3cc;
  letter-spacing: -0.005em;
  word-break: keep-all;
  overflow-wrap: anywhere;
  padding-left: 10px;
  padding-right: 2px;
  border-left: 2px solid rgba(226, 59, 59, 0.75);
}

.narration p { margin: 0; }
.narration p + p { margin-top: 2px; }
.narration .dim { color: #a9a2ae; }
.narration .hl { color: #fff; font-weight: 600; }

.speech {
  position: relative;
  display: none;
  align-self: flex-start;
  max-width: 100%;
  padding: 9px 12px 10px;
  border-radius: 4px 14px 14px 14px;
  background: #f2efe9;
  color: #171319;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45);
  animation: speechIn 0.4s ease 0.12s both;
}

.speech.show { display: block; }

@keyframes speechIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.speaker {
  display: block;
  margin-bottom: 3px;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #a81f1f;
}

.speech-text {
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
  font-weight: 500;
  word-break: keep-all;
  overflow-wrap: anywhere;
  letter-spacing: -0.01em;
}

/* ---------- Choices ---------- */
.choices {
  flex: 0 0 auto;
  display: grid;
  gap: 8px;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  background: rgba(8, 8, 12, 0.96);
  border-top: 1px solid var(--line);
  box-shadow: 0 -14px 30px rgba(0, 0, 0, 0.45);
}

.choice {
  display: grid;
  grid-template-columns: 28px 1fr;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  text-align: left;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.055), rgba(255, 255, 255, 0.02));
  color: var(--ink);
  font-family: var(--sans);
  cursor: pointer;
  transition: border-color 0.18s ease, background 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
  animation: choiceIn 0.4s ease both;
}

.choice:nth-child(1) { animation-delay: 0.1s; }
.choice:nth-child(2) { animation-delay: 0.18s; }

@keyframes choiceIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.choice:hover {
  border-color: rgba(226, 59, 59, 0.65);
  background: linear-gradient(180deg, rgba(226, 59, 59, 0.16), rgba(226, 59, 59, 0.05));
  box-shadow: 0 6px 22px rgba(226, 59, 59, 0.2);
  transform: translateY(-1px);
}

.choice:active { transform: scale(0.985); }

.choice .letter {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid rgba(226, 59, 59, 0.55);
  background: rgba(226, 59, 59, 0.12);
  color: #ff8f8f;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
}

.choice .label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.choice .sub {
  display: block;
  margin-top: 2px;
  font-size: 10.5px;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: -0.005em;
}

/* ---------- Ending ---------- */
.ending {
  position: relative;
  display: grid;
  grid-template-rows: 1fr auto auto;
  min-height: 0;
}

.ending-art {
  grid-area: 1 / 1 / 3 / 2;
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.ending-art .cut { object-position: center 22%; animation: cutIn 0.7s ease both; }

.ending-veil {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(110% 70% at 50% 35%, transparent 30%, rgba(4, 4, 8, 0.82) 100%),
    linear-gradient(180deg, rgba(4, 4, 8, 0.5) 0%, rgba(4, 4, 8, 0.1) 30%, rgba(4, 4, 8, 0.92) 78%, #07070b 100%);
}

.ending-copy {
  grid-area: 2 / 1 / 3 / 2;
  position: relative;
  z-index: 2;
  padding: 34px 22px 18px;
  text-align: center;
  background: linear-gradient(180deg, rgba(7, 7, 11, 0) 0%, rgba(7, 7, 11, 0.78) 40%, rgba(7, 7, 11, 0.98) 100%);
  animation: endCopyIn 0.8s cubic-bezier(0.2, 0.7, 0.3, 1) 0.18s both;
}

@keyframes endCopyIn {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: none; }
}

.ending-type {
  display: inline-block;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid currentColor;
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.26em;
  color: #ff8b8b;
  background: rgba(226, 59, 59, 0.12);
}

.ending[data-type="HIDDEN"] .ending-type { color: #8ee4ff; background: rgba(77, 214, 255, 0.1); }
.ending[data-type="TRUE"] .ending-type { color: #d9b8ff; background: rgba(139, 92, 246, 0.14); }
.ending[data-type="SUB"] .ending-type { color: #b9c6ff; background: rgba(120, 140, 255, 0.12); }

.ending-title {
  margin: 11px 0 8px;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.22;
  color: #fff;
  text-shadow: 0 4px 24px rgba(0, 0, 0, 0.85);
}

.ending[data-type="TRUE"] .ending-title { text-shadow: 0 0 26px rgba(139, 92, 246, 0.7), 0 4px 24px rgba(0, 0, 0, 0.9); }
.ending[data-type="HIDDEN"] .ending-title { text-shadow: 0 0 26px rgba(77, 214, 255, 0.6), 0 4px 24px rgba(0, 0, 0, 0.9); }

.ending-body {
  margin: 0 auto;
  max-width: 34ch;
  font-size: 12.5px;
  line-height: 1.8;
  color: #d5cfc9;
  word-break: keep-all;
  overflow-wrap: anywhere;
  text-wrap: balance;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.9);
}

.ending-collect {
  margin: 13px 0 0;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--gold);
}

.ending-collect.is-new { animation: collectPop 1.4s ease 0.6s both; }

@keyframes collectPop {
  0% { opacity: 0; transform: translateY(6px) scale(0.96); }
  60% { opacity: 1; transform: none; }
  100% { opacity: 1; }
}

.ending-actions {
  grid-area: 3 / 1 / 4 / 2;
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
  background: rgba(7, 7, 11, 0.98);
  border-top: 1px solid var(--line);
}

.btn {
  padding: 12px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
}

.btn:hover { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.28); }
.btn:active { transform: scale(0.98); }

.btn.primary {
  grid-column: 1 / -1;
  border-color: rgba(255, 255, 255, 0.16);
  background: linear-gradient(180deg, #e23b3b, #a32020);
  color: #fff;
  box-shadow: 0 10px 26px rgba(226, 59, 59, 0.3);
}

.btn.primary:hover { background: linear-gradient(180deg, #ef4a4a, #b02727); }

/* ---------- Ending dex modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  background: rgba(3, 3, 6, 0.76);
  backdrop-filter: blur(7px);
  animation: fadeIn 0.22s ease both;
}

.modal[hidden] { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  width: 100%;
  max-width: 460px;
  max-height: 84%;
  display: flex;
  flex-direction: column;
  background: #111119;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -20px 60px rgba(0, 0, 0, 0.7);
  animation: sheetIn 0.3s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  overflow: hidden;
}

@keyframes sheetIn {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

@media (min-width: 640px) {
  .modal { align-items: center; padding: 20px; }
  .modal-card { border-radius: 20px; }
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 15px 16px;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.modal-count {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.04em;
}

.modal-close {
  margin-left: auto;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: #cfc9d4;
  font-size: 12px;
  cursor: pointer;
}

.modal-close:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }

.modal-body {
  padding: 10px 16px 4px;
  overflow-y: auto;
  scrollbar-width: none;
}

.modal-body::-webkit-scrollbar { width: 0; }

.dex-item {
  display: grid;
  grid-template-columns: 62px 1fr;
  gap: 11px;
  padding: 11px 0;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.08);
  align-items: center;
}

.dex-item:last-child { border-bottom: 0; }

.dex-thumb {
  position: relative;
  width: 62px;
  height: 78px;
  border-radius: 9px;
  overflow: hidden;
  background: #1a1a24;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.dex-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.dex-item.locked .dex-thumb img { filter: grayscale(1) brightness(0.32) blur(3px); }

.dex-thumb .lock {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--mono);
  font-size: 18px;
  color: rgba(255, 255, 255, 0.55);
  background: rgba(0, 0, 0, 0.35);
}

.dex-type {
  display: inline-block;
  margin-bottom: 4px;
  font-family: var(--mono);
  font-size: 8.5px;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: #ff8b8b;
}

.dex-item[data-type="HIDDEN"] .dex-type { color: #8ee4ff; }
.dex-item[data-type="TRUE"] .dex-type { color: #d9b8ff; }
.dex-item[data-type="SUB"] .dex-type { color: #b9c6ff; }

.dex-name {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: #fff;
}

.dex-desc {
  margin: 4px 0 0;
  font-size: 11px;
  line-height: 1.6;
  color: var(--muted);
  word-break: keep-all;
}

.dex-item.locked .dex-name { color: #6d6779; }
.dex-item.locked .dex-desc { color: #565062; }

.dex-new {
  display: inline-block;
  margin-left: 5px;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(232, 192, 122, 0.18);
  color: var(--gold);
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.1em;
  vertical-align: middle;
}

.modal-foot {
  margin: 0;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  font-size: 10.5px;
  line-height: 1.6;
  color: #7f7889;
  word-break: keep-all;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
