/* ═══════════════════════════════════════════════════════
   케이튼 교수와 사라진 소스코드 — 스타일
   팔레트: 세피아 밤(#1a1008) · 양피지(#f2e3c2) · 금(#d4a017) · 버건디(#7a2e2e)
   ═══════════════════════════════════════════════════════ */

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

:root {
  --bg-deep: #1a1008;
  --bg-panel: #2b1d0e;
  --parchment: #f2e3c2;
  --parchment-dark: #e4d0a6;
  --ink: #3a2a18;
  --gold: #d4a017;
  --gold-bright: #f0c34e;
  --burgundy: #7a2e2e;
  --teal: #7df2c8;
  --shadow: rgba(0, 0, 0, .45);
}

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

body {
  background:
    radial-gradient(ellipse at 50% -10%, #3d2a14 0%, var(--bg-deep) 62%),
    var(--bg-deep);
  color: var(--parchment);
  font-family: 'Georgia', 'Apple SD Gothic Neo', 'Noto Serif KR', 'Malgun Gothic', serif;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  height: 100dvh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  animation: fadeIn .45s ease both;
  user-select: none;
  -webkit-user-select: none;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes pop    { 0% { transform: scale(.85); opacity: 0; } 60% { transform: scale(1.05); } 100% { transform: scale(1); opacity: 1; } }
@keyframes shimmer{ 0%,100% { opacity: .55; } 50% { opacity: 1; } }
@keyframes floaty { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-7px); } }

/* ── 공통 버튼 ─────────────────────────────────────── */
.btn {
  appearance: none;
  border: 2px solid var(--gold);
  border-radius: 12px;
  background: linear-gradient(180deg, #45301a 0%, #2c1d0d 100%);
  color: var(--parchment);
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  padding: .8rem 1.2rem;
  cursor: pointer;
  letter-spacing: .02em;
  box-shadow: 0 3px 0 rgba(0,0,0,.4), inset 0 1px 0 rgba(255,255,255,.08);
  transition: transform .08s ease, filter .15s ease;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.4); }
.btn-gold {
  background: linear-gradient(180deg, var(--gold-bright) 0%, var(--gold) 55%, #a87c10 100%);
  color: #2a1c08;
  border-color: #f6dd90;
  text-shadow: 0 1px 0 rgba(255,255,255,.35);
}
.btn-danger { border-color: #b05050; color: #f0b8b8; }
.btn-hint { border-color: #8a6f3a; }

i { font-style: normal; color: var(--gold-bright); }

.dim { opacity: .65; font-size: .88em; }

/* ── HUD ──────────────────────────────────────────── */
.hud {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .55rem .8rem;
  background: linear-gradient(180deg, rgba(0,0,0,.35), rgba(0,0,0,.15));
  border-bottom: 1px solid rgba(212,160,23,.35);
  flex: none;
}
.hud-menu {
  appearance: none; background: none; border: 1.5px solid rgba(212,160,23,.5);
  border-radius: 8px; color: var(--gold-bright); font-size: 1rem;
  width: 2.1rem; height: 2.1rem; cursor: pointer; flex: none;
}
.hud-label {
  flex: 1; font-size: .82rem; opacity: .8;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.hud-stats { display: flex; gap: .7rem; flex: none; font-size: .95rem; font-weight: 700; }
.stat i { margin-right: .18rem; }
.stat.coin i { color: #e8b93c; }

/* ── 타이틀 화면 ──────────────────────────────────── */
.title-screen {
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 1.4rem;
  gap: .4rem;
}
.title-art { animation: floaty 3.4s ease-in-out infinite; }
.title-hat .portrait-svg { width: 130px; height: 130px; filter: drop-shadow(0 8px 18px rgba(0,0,0,.5)); }
.title-sub {
  letter-spacing: .42em; font-size: .72rem; color: var(--gold);
  margin-top: .8rem; text-indent: .42em;
}
.title-logo {
  font-size: clamp(1.9rem, 8.5vw, 2.7rem);
  line-height: 1.22;
  color: var(--gold-bright);
  text-shadow: 0 2px 0 #7a5a10, 0 6px 18px rgba(0,0,0,.55);
  margin: .3rem 0 .2rem;
}
.title-logo span { font-size: .6em; color: var(--parchment); }
.title-tag { opacity: .85; font-size: .95rem; margin-bottom: 1.4rem; }
.title-buttons { display: flex; flex-direction: column; gap: .7rem; width: min(300px, 82vw); }
.title-buttons .btn { width: 100%; font-size: 1.06rem; }
.title-credit { margin-top: 1.6rem; font-size: .74rem; opacity: .5; }

/* ── 챕터 카드 ────────────────────────────────────── */
.chapter-screen { justify-content: center; padding: 1.4rem; }
.chapter-card {
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  color: var(--ink);
  border: 3px double var(--gold);
  border-radius: 16px;
  padding: 2.2rem 1.6rem;
  text-align: center;
  box-shadow: 0 14px 40px var(--shadow);
  animation: pop .5s ease both;
}
.chapter-no { letter-spacing: .3em; font-size: .85rem; color: var(--burgundy); font-weight: 700; text-indent: .3em; }
.chapter-rule {
  height: 1px; margin: .9rem auto; width: 72%;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
}
.chapter-title { font-size: 1.65rem; margin: .1rem 0; }
.chapter-lead { font-size: .95rem; line-height: 1.7; opacity: .85; margin-bottom: 1.5rem; }

/* ── 대화 화면 ────────────────────────────────────── */
.dialogue-screen { }
.stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  background:
    radial-gradient(ellipse at 50% 80%, rgba(212,160,23,.10), transparent 65%);
}
.stage-portrait .portrait-svg {
  width: min(46vw, 210px);
  height: auto;
  filter: drop-shadow(0 10px 22px rgba(0,0,0,.5));
  animation: pop .4s ease both;
}
.stage-narr-deco { color: var(--gold); letter-spacing: 1em; opacity: .5; text-indent: 1em; }

.dialogue-box {
  flex: none;
  position: relative;
  margin: 0 .7rem calc(.7rem + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  color: var(--ink);
  border: 2.5px solid var(--gold);
  border-radius: 14px;
  padding: 1.15rem 1.1rem 1.35rem;
  min-height: 8.2rem;
  box-shadow: 0 10px 26px var(--shadow);
  cursor: pointer;
}
.dialogue-box.narr { font-style: italic; text-align: center; background: #efe0bd; }
.nameplate {
  position: absolute;
  top: -0.95rem; left: .9rem;
  background: linear-gradient(180deg, var(--burgundy), #5c2020);
  color: #f6e6c8;
  font-size: .82rem; font-weight: 700;
  padding: .28rem .9rem;
  border-radius: 999px;
  border: 1.5px solid var(--gold);
  letter-spacing: .04em;
  box-shadow: 0 3px 8px rgba(0,0,0,.35);
}
.dialogue-text { line-height: 1.75; font-size: 1.02rem; word-break: keep-all; }
.dialogue-next {
  position: absolute; right: 1rem; bottom: .55rem;
  color: var(--burgundy); font-size: .8rem;
  animation: shimmer 1.1s ease-in-out infinite;
}

/* ── 탐색 화면 ────────────────────────────────────── */
.explore-head { padding: .9rem 1rem .4rem; text-align: center; flex: none; }
.explore-head h2 { font-size: 1.2rem; color: var(--gold-bright); }
.explore-head p { font-size: .84rem; opacity: .8; margin-top: .25rem; word-break: keep-all; }
.explore-count { color: #e8b93c; font-weight: 700; }
.explore-scene {
  flex: 1;
  position: relative;
  margin: .6rem .8rem;
  min-height: 0;
  border: 2px solid rgba(212,160,23,.45);
  border-radius: 14px;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(212,160,23,.08), transparent 55%),
    linear-gradient(180deg, #241708 0%, #16100a 100%);
  overflow: hidden;
}
.explore-item {
  position: absolute;
  transform: translate(-50%, -50%);
  appearance: none; background: none; border: none;
  display: flex; flex-direction: column; align-items: center; gap: .1rem;
  cursor: pointer;
  padding: .4rem;
}
.explore-emoji { font-size: clamp(1.9rem, 9vw, 2.6rem); filter: drop-shadow(0 4px 8px rgba(0,0,0,.5)); }
.explore-label { font-size: .68rem; color: var(--parchment); opacity: .75; }
.explore-item.has-secret .explore-emoji { animation: floaty 2.2s ease-in-out infinite; }
.explore-item:active .explore-emoji { transform: scale(.9); }
.explore-toast {
  position: absolute;
  left: 50%; bottom: 6.4rem;
  transform: translateX(-50%);
  width: min(88%, 420px);
  background: rgba(20, 12, 4, .94);
  border: 1.5px solid var(--gold);
  border-radius: 12px;
  padding: .7rem .9rem;
  font-size: .88rem;
  line-height: 1.55;
  text-align: center;
  z-index: 5;
  word-break: keep-all;
  box-shadow: 0 8px 20px var(--shadow);
}
.explore-toast b { color: #e8b93c; }
.explore-toast.pop { animation: pop .3s ease both; }
.explore-footer { flex: none; padding: .5rem .9rem calc(.8rem + env(safe-area-inset-bottom)); display: flex; }
.explore-footer .btn { flex: 1; }

/* ── 퍼즐 화면 ────────────────────────────────────── */
.puzzle-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: .9rem .9rem 0;
  min-height: 0;
}
.puzzle-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: .45rem;
}
.puzzle-no {
  background: linear-gradient(180deg, var(--burgundy), #5c2020);
  border: 1.5px solid var(--gold);
  padding: .25rem .8rem;
  border-radius: 999px;
  font-size: .8rem; font-weight: 700;
  letter-spacing: .06em;
}
.puzzle-value { font-weight: 700; color: var(--gold-bright); font-size: .95rem; }
.puzzle-title { font-size: 1.45rem; color: var(--gold-bright); margin-bottom: .35rem; }
.puzzle-intro {
  font-size: .88rem; font-style: italic; opacity: .8;
  line-height: 1.65; margin-bottom: .8rem; word-break: keep-all;
}
.puzzle-body {
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  color: var(--ink);
  border: 2.5px solid var(--gold);
  border-radius: 14px;
  padding: 1.1rem 1rem 1.2rem;
  box-shadow: 0 10px 26px var(--shadow);
  margin-bottom: 1rem;
}
.puzzle-desc { line-height: 1.8; font-size: 1rem; word-break: keep-all; }
.puzzle-desc .cipher {
  display: inline-block;
  font-family: 'Courier New', monospace;
  font-weight: 700; font-size: 1.15em;
  letter-spacing: .22em;
  background: #2a1c08; color: var(--gold-bright);
  padding: .5rem .9rem; border-radius: 8px;
  margin: .4rem 0;
}
.puzzle-code {
  background: #23180b;
  color: #e8dcc0;
  border-radius: 10px;
  border: 1px solid rgba(212,160,23,.4);
  padding: .85rem .95rem;
  margin-top: .9rem;
  overflow-x: auto;
  font-size: .86rem;
  line-height: 1.65;
}
.puzzle-code code { font-family: 'SF Mono', 'Consolas', 'Courier New', monospace; }
.puzzle-input {
  width: 100%;
  margin-top: 1rem;
  font: inherit;
  font-size: 1.15rem;
  font-weight: 700;
  text-align: center;
  letter-spacing: .1em;
  padding: .75rem .8rem;
  border-radius: 10px;
  border: 2px solid var(--burgundy);
  background: #fdf6e4;
  color: var(--ink);
  outline: none;
}
.puzzle-input:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(212,160,23,.3); }
.puzzle-choices { display: flex; flex-direction: column; gap: .55rem; margin-top: 1rem; }
.choice {
  appearance: none;
  display: flex; align-items: center; gap: .65rem;
  text-align: left;
  font: inherit; font-size: .96rem; font-weight: 600;
  background: #fdf6e4;
  color: var(--ink);
  border: 2px solid #c9b183;
  border-radius: 10px;
  padding: .72rem .8rem;
  cursor: pointer;
  transition: border-color .12s ease, background .12s ease;
}
.choice-key { color: var(--burgundy); font-weight: 800; flex: none; }
.choice.selected {
  border-color: var(--burgundy);
  background: #f6e2c0;
  box-shadow: inset 0 0 0 1.5px var(--burgundy);
}
.puzzle-actions {
  flex: none;
  display: flex;
  gap: .6rem;
  padding: .55rem .9rem calc(.8rem + env(safe-area-inset-bottom));
  background: linear-gradient(0deg, rgba(0,0,0,.3), transparent);
}
.btn-hint { flex: none; }
.puzzle-actions .btn-gold { flex: 1; }

/* ── 모달 ─────────────────────────────────────────── */
.modal-layer[hidden] { display: none; }
.modal-layer {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(8, 5, 2, .72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.1rem;
  animation: fadeIn .2s ease both;
}
.modal {
  width: min(100%, 420px);
  max-height: 86%;
  overflow-y: auto;
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  color: var(--ink);
  border: 3px double var(--gold);
  border-radius: 16px;
  padding: 1.4rem 1.2rem;
  text-align: center;
  box-shadow: 0 18px 46px rgba(0,0,0,.6);
  animation: pop .32s ease both;
}
.modal-title { font-size: 1.3rem; color: var(--burgundy); margin-bottom: .8rem; }
.modal-text { line-height: 1.75; word-break: keep-all; }
.modal-buttons { display: flex; flex-direction: column; gap: .55rem; margin-top: 1.1rem; }
.modal-wrong { border-color: #a05050; }
.modal-correct { border-color: var(--gold-bright); }
.correct-stamp {
  display: inline-block;
  font-size: 2rem; font-weight: 800;
  color: var(--burgundy);
  border: 3px solid var(--burgundy);
  border-radius: 12px;
  padding: .2rem 1.2rem;
  transform: rotate(-4deg);
  margin-bottom: .7rem;
  animation: pop .45s ease both;
}
.correct-picarats { font-size: 1.25rem; font-weight: 800; color: #8a6a10; margin-bottom: .7rem; }
.modal-explain {
  font-size: .92rem; line-height: 1.75;
  background: rgba(122, 46, 46, .08);
  border-radius: 10px;
  padding: .8rem .9rem;
  text-align: left;
  word-break: keep-all;
}
.hint-tabs { display: flex; gap: .4rem; justify-content: center; margin-bottom: .8rem; }
.hint-tab {
  appearance: none; font: inherit; font-size: .84rem; font-weight: 700;
  border: 2px solid #c9b183; border-radius: 999px;
  background: #fdf6e4; color: var(--ink);
  padding: .4rem .85rem; cursor: pointer;
}
.hint-tab.open { border-color: var(--gold); background: #f2dfae; }
.hint-body {
  min-height: 4.2rem;
  font-size: .95rem; line-height: 1.75;
  display: flex; align-items: center; justify-content: center;
  padding: .5rem .3rem;
  word-break: keep-all;
}
.menu-stats { text-align: left; line-height: 2; margin-bottom: .4rem; font-size: .98rem; }
.menu-stats b { color: var(--burgundy); }
.menu-buttons { display: flex; flex-direction: column; gap: .55rem; margin-top: .8rem; }

/* ── 엔딩 ─────────────────────────────────────────── */
.ending-screen { justify-content: center; padding: 1.2rem; overflow-y: auto; }
.ending-card {
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  color: var(--ink);
  border: 3px double var(--gold);
  border-radius: 16px;
  padding: 1.9rem 1.4rem;
  text-align: center;
  box-shadow: 0 14px 40px var(--shadow);
  animation: pop .5s ease both;
}
.ending-fin { letter-spacing: .35em; color: var(--burgundy); font-size: .8rem; font-weight: 700; text-indent: .35em; }
.ending-title { font-size: 1.9rem; margin-top: .3rem; }
.ending-stats { display: flex; flex-direction: column; gap: .45rem; padding: .2rem .4rem; }
.ending-stat { display: flex; justify-content: space-between; font-size: .98rem; }
.ending-stat b { color: var(--burgundy); }
.ending-stat i { color: #8a6a10; }
.ending-rank-label { font-size: .8rem; letter-spacing: .2em; opacity: .7; text-indent: .2em; }
.ending-rank {
  font-size: 1.5rem; font-weight: 800; color: var(--burgundy);
  margin: .25rem 0 .5rem;
}
.ending-rank-desc { font-size: .9rem; line-height: 1.7; opacity: .85; margin-bottom: 1.2rem; word-break: keep-all; }
.ending-card .title-buttons { width: 100%; }
.ending-card .title-credit { color: var(--ink); }

/* ── 태블릿/데스크톱 보정 ─────────────────────────── */
@media (min-width: 561px) {
  #app {
    border-left: 1px solid rgba(212,160,23,.25);
    border-right: 1px solid rgba(212,160,23,.25);
    box-shadow: 0 0 80px rgba(0,0,0,.5);
  }
}
@media (min-height: 800px) {
  .dialogue-box { min-height: 9.5rem; }
}
/* 가로가 짧은 폰(landscape) */
@media (max-height: 480px) {
  .stage-portrait .portrait-svg { width: 120px; }
  .dialogue-box { min-height: 6.4rem; padding: .9rem 1rem 1.1rem; }
  .title-hat .portrait-svg { width: 84px; height: 84px; }
}
