:root {
  --bg: #f6f1e7;
  --panel: #fffaf0;
  --text: #2d2a26;
  --muted: #7a7266;
  --border: #e3d8c3;
  --accent: #7b5cd6;
  --accent-text: #ffffff;
  --bar-bg: #ece3d2;
  --stress: #d9534f;
  --heart: #e0609a;
  --star: #c99a1e;
  --shadow: 0 2px 8px rgba(60, 40, 10, 0.08);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1a22;
    --panel: #25232f;
    --text: #ece8f4;
    --muted: #a29db0;
    --border: #3a3747;
    --accent: #9d84f0;
    --accent-text: #15131c;
    --bar-bg: #34313f;
    --stress: #ef6b66;
    --heart: #f07fb2;
    --star: #f2c94c;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a22; --panel: #25232f; --text: #ece8f4; --muted: #a29db0;
  --border: #3a3747; --accent: #9d84f0; --accent-text: #15131c;
  --bar-bg: #34313f; --stress: #ef6b66; --heart: #f07fb2; --star: #f2c94c;
  --shadow: 0 2px 8px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", "Malgun Gothic", "Apple SD Gothic Neo", system-ui, sans-serif;
  line-height: 1.5;
}
h1, h2 { margin: 0; }
h2 { font-size: 1.05rem; margin-bottom: 12px; }

.topbar {
  position: relative; z-index: 30; /* 대화 중에도 소리 버튼을 누를 수 있게 */
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.topbar h1 { font-size: 1.3rem; }
.turn { flex: 1; color: var(--muted); font-weight: 600; white-space: nowrap; }
@media (max-width: 600px) {
  .topbar { padding: 12px 16px; gap: 8px; }
  .topbar h1 { font-size: 1.1rem; }
  .turn { font-size: .85rem; order: 3; flex-basis: 100%; }
  .top-buttons { margin-left: auto; }
  .top-buttons button { padding: 6px 9px; font-size: .85rem; }
}

button {
  font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); padding: 8px 14px;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.ghost { background: transparent; }
button:disabled { opacity: .45; cursor: not-allowed; }

.layout {
  display: grid; gap: 16px; padding: 16px; max-width: 1200px; margin: 0 auto;
  grid-template-columns: 280px 1fr 300px;
}
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}

#profile { text-align: center; }
.profile-head { display: flex; flex-direction: column; align-items: center; margin-bottom: 12px; }
.portrait { width: 108px; height: 108px; margin: 4px auto 8px; }

/* ===== 변화 연출 (+n / -n) ===== */
.stat-head, #goldRow { position: relative; }
.float {
  position: absolute; right: 2.6em; top: -4px; font-weight: 700; font-size: .85rem;
  pointer-events: none; animation: floatUp 1.4s ease-out forwards;
}
#goldRow .float { right: auto; left: calc(50% + 3.2em); }
.float.good { color: var(--accent); }
.float.bad { color: var(--stress); }
@keyframes floatUp {
  0% { opacity: 0; transform: translateY(6px); }
  15% { opacity: 1; transform: translateY(0); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-14px); }
}

/* ===== 튜토리얼 강조 ===== */
.hl {
  position: relative; z-index: 21; background: var(--panel); border-radius: 12px;
  box-shadow: 0 0 0 3px var(--accent), 0 0 22px var(--accent);
  animation: hlPulse 1.2s ease-in-out infinite;
}
@keyframes hlPulse { 50% { box-shadow: 0 0 0 3px var(--accent), 0 0 6px var(--accent); } }
.pt { display: block; width: 100%; height: 100%; }
.top-buttons { display: flex; gap: 6px; }
.icon-btn { padding: 8px 10px; }
.mood { color: var(--muted); margin: 4px 0 14px; font-size: .9rem; }

.stat { text-align: left; margin-bottom: 8px; font-size: .9rem; }
.stat-head { display: flex; justify-content: space-between; }
.bar { height: 8px; background: var(--bar-bg); border-radius: 4px; overflow: hidden; margin-top: 3px; }
.bar > i { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.stat.stress .bar > i { background: var(--stress); }
.gold { margin-top: 14px; font-weight: 700; font-size: 1.1rem; }

.actions { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.action { text-align: left; padding: 12px; display: flex; flex-direction: column; gap: 2px; }
.action:not(:disabled):hover { border-color: var(--accent); }
.action .icon { font-size: 1.6rem; }
.action .name { font-weight: 700; }
.action .desc, .action .cost { font-size: .8rem; color: var(--muted); }

.log { list-style: none; padding: 0; margin: 0; max-height: 520px; overflow-y: auto; font-size: .85rem; }
.log li { padding: 6px 0; border-bottom: 1px dashed var(--border); }
.log li:first-child { font-weight: 600; }

.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10;
}
.overlay.hidden { display: none; }
.modal {
  background: var(--panel); border-radius: 16px; padding: 22px; width: 100%; max-width: 440px;
  border: 1px solid var(--border); box-shadow: var(--shadow);
}
.modal p { margin: 0 0 10px; }
.modal input {
  width: 100%; font: inherit; padding: 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.modal-buttons { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; }
.ending-icon { font-size: 56px; text-align: center; }
.ending-title { text-align: center; font-size: 1.1rem; }
.ending-art { position: relative; width: 120px; margin: 0 auto 6px; }
.ending-portrait { width: 120px; height: 120px; }
.ending-art .ending-icon { position: absolute; right: -14px; bottom: -6px; font-size: 40px; }
.badge {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 8px; vertical-align: middle;
  background: var(--heart); color: #fff; font-size: .7rem; font-weight: 700;
}

/* ===== 엔딩 도감 ===== */
.modal:has(.cards) { max-width: 560px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; max-height: 60vh; overflow-y: auto; }
.card { border: 1px dashed var(--border); border-radius: 12px; padding: 10px 8px; text-align: center; background: var(--bg); }
.card.seen { border-style: solid; border-color: var(--accent); }
.card-icon { font-size: 30px; }
.card:not(.seen) .card-icon { opacity: .5; }
.card-title { font-weight: 700; font-size: .9rem; margin-top: 2px; }
.card-sub { font-size: .75rem; color: var(--muted); margin-top: 2px; line-height: 1.35; }
.final-stats { font-size: .9rem; color: var(--muted); }

/* ===== 인연 패널 ===== */
#profile h3 { font-size: .95rem; margin: 18px 0 8px; text-align: left; }
.pendant { margin-top: 6px; color: var(--star); letter-spacing: 1px; font-size: .95rem; }
.bond { display: flex; gap: 8px; align-items: center; text-align: left; margin-bottom: 8px; font-size: .9rem; }
.bond-icon { width: 34px; height: 34px; flex-shrink: 0; }
.bond-main { flex: 1; min-width: 0; }
.hearts { color: var(--heart); font-size: .8rem; }
.bar.heart > i { background: var(--heart); }
.log li.sub { color: var(--muted); font-weight: 400; border-bottom: none; padding-top: 0; }

/* ===== 대화창 ===== */
.dlg-overlay { align-items: flex-end; z-index: 20; background: rgba(0, 0, 0, .3); }
.dialogue {
  width: 100%; max-width: 760px; min-height: 160px; margin-bottom: 8px;
  display: flex; gap: 16px; padding: 18px 20px; cursor: pointer;
  background: var(--panel); border: 2px solid var(--accent); border-radius: 18px; box-shadow: var(--shadow);
}
.dlg-portrait {
  font-size: 40px; line-height: 1; width: 92px; height: 92px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  background: var(--bg); border-radius: 50%; border: 2px solid var(--border);
}
.dlg-portrait:empty { display: none; }
.dlg-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.dlg-name { font-weight: 700; color: var(--accent); min-height: 1.4em; }
.dlg-text { white-space: pre-wrap; font-size: 1.05rem; min-height: 3em; }
.dialogue.narration .dlg-text { color: var(--muted); font-style: italic; }
.dialogue.sys .dlg-text { color: var(--accent); font-weight: 600; font-size: .95rem; }
.dlg-choices { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.dlg-choices button { text-align: left; cursor: pointer; }
.dlg-choices button:hover, .dlg-choices button:focus-visible { border-color: var(--accent); outline: none; }
.dlg-input { display: flex; gap: 8px; }
.dlg-input input {
  flex: 1; min-width: 0; font: inherit; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.dlg-next { margin-top: auto; text-align: right; color: var(--muted); font-size: .75rem; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .3; } }
@media (max-width: 600px) {
  .dialogue { gap: 10px; padding: 14px; }
  .dlg-portrait { width: 60px; height: 60px; font-size: 28px; }
  .dlg-text { font-size: 1rem; }
}

/* ===== 모바일: 일정 버튼 2열 (기본 .actions 규칙보다 뒤에 있어야 적용됨) ===== */
@media (max-width: 900px) {
  .actions { grid-template-columns: 1fr 1fr; gap: 8px; }
  .action { padding: 9px 10px; }
  .action .icon { font-size: 1.25rem; }
  .action .desc, .action .cost { font-size: .72rem; }
}

/* ===== 모바일: 프로필 압축 (기본 규칙보다 뒤에 있어야 적용됨) ===== */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 12px; gap: 12px; }
  /* 프로필 압축: 초상 옆에 이름·기분·골드·펜던트, 능력치와 인연은 2열 */
  .profile-head { flex-direction: row; gap: 14px; margin-bottom: 8px; }
  .profile-info { text-align: left; }
  .profile-info h2 { margin-bottom: 0; }
  .portrait { width: 72px; height: 72px; margin: 0; flex-shrink: 0; }
  .mood { margin: 0 0 2px; }
  .gold { margin-top: 0; font-size: 1rem; }
  #goldRow .float { left: 6em; }
  .pendant { margin-top: 0; }
  #stats, #bonds { display: grid; grid-template-columns: 1fr 1fr; column-gap: 14px; }
  .stat { margin-bottom: 6px; }
  #profile h3 { margin: 8px 0 6px; }
  .bond { margin-bottom: 6px; }
  .bond-icon { width: 28px; height: 28px; }
}
