:root {
  --ink: #0d0b1a;
  --ink-2: #16132b;
  --ink-3: #221d40;
  --gold: #f0d9b5;
  --gold-2: #c9a878;
  --violet: #8b6cf0;
  --violet-2: #5a3fb8;
  --potion: #7ff0b8;
  --text: #efe8f7;
  --muted: #a9a0c2;
  --serif: 'Gowun Batang', 'Nanum Myeongjo', 'Apple SD Gothic Neo', serif;
  --sans: 'Gowun Dodum', 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  --w: min(100vw, 520px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--text); font-family: var(--sans); }
body { overflow: hidden; overscroll-behavior: none; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; display: flex; justify-content: center; background: #07060f; }
#app::before { /* 넓은 화면에서 양옆 흐린 배경 */
  content: ''; position: absolute; inset: -40px; background: url(../assets/bg-talk.jpg) center/cover;
  filter: blur(28px) brightness(.35); z-index: 0;
}

/* ───────── 무대 ───────── */
#stage {
  position: relative; width: var(--w); height: 100%; overflow: hidden; z-index: 1;
  user-select: none; -webkit-user-select: none; cursor: pointer; background: var(--ink);
}
.bg { position: absolute; inset: 0; background-size: cover; background-position: center 30%; opacity: 0; transition: opacity 1.2s ease; transform: scale(1.04); }
.bg.is-on { opacity: 1; animation: drift 24s ease-in-out infinite alternate; }
.bg-talk { background-image: url(../assets/bg-talk.jpg); }
.bg-brew { background-image: url(../assets/bg-brew.jpg); }
@keyframes drift { from { transform: scale(1.04) translate(0, 0); } to { transform: scale(1.1) translate(-1.5%, -1%); } }
.vignette { position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 35%, transparent 40%, rgba(5, 4, 12, .55) 100%),
              linear-gradient(to bottom, rgba(5,4,12,.35), transparent 18%, transparent 55%, rgba(5,4,12,.8)); }
.glow { position: absolute; left: 50%; top: 66%; width: 90%; aspect-ratio: 1; transform: translate(-50%, -50%);
  background: radial-gradient(circle, rgba(127, 240, 184, .45), transparent 60%); opacity: 0; pointer-events: none;
  transition: opacity 1s; mix-blend-mode: screen; }
.glow.is-on { opacity: 1; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; transform: translate(-50%, -50%) scale(1.08); } }
#sparkles { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.skip { position: absolute; top: max(14px, env(safe-area-inset-top)); right: 14px; z-index: 6;
  background: rgba(13, 11, 26, .6); border: 1px solid rgba(240, 217, 181, .35); color: var(--gold);
  padding: 7px 12px; border-radius: 999px; font-size: 13px; backdrop-filter: blur(6px); cursor: pointer; }

/* ───────── 대화창 (원본 이미지 3조각) ───────── */
.dialog { --dw: calc(var(--w) - 12px); position: absolute; left: 6px; right: 6px; bottom: max(10px, env(safe-area-inset-bottom)); z-index: 5;
  display: grid; transition: transform .35s ease, opacity .35s; }
.dialog.is-hidden { opacity: 0; transform: translateY(20px); pointer-events: none; }
.dialog > * { grid-area: 1 / 1; }
.frame { display: flex; flex-direction: column; }
.dialog-top { flex: none; aspect-ratio: 1000 / 176; background: url(../assets/box-top.png) center/100% 100% no-repeat; margin-bottom: -1px; }
.dialog-mid { flex: 1; min-height: 0; background: url(../assets/box-mid.png) center/100% 100% no-repeat; }
.dialog-bot { flex: none; position: relative; aspect-ratio: 1000 / 90; background: url(../assets/box-bot.png) center/100% 100% no-repeat; margin-top: -1px; }
.dialog-text { position: relative; margin: 0; padding: calc(var(--dw) * .145) 8% calc(var(--dw) * .05); min-height: calc(var(--dw) * .43);
  font-family: var(--serif); font-size: clamp(15px, 4.1vw, 19px);
  line-height: 1.62; color: #f4ecdf; word-break: keep-all; text-shadow: 0 1px 2px rgba(0,0,0,.6); white-space: pre-line; }
.dialog-text em { font-style: normal; color: var(--potion); }
.dialog-text strong { color: var(--gold); font-weight: 700; }
.next-arrow { position: absolute; right: 7%; top: -8%; font-size: clamp(12px, 3vw, 15px); color: #a99cf5; opacity: 0; }
.next-arrow.is-on { opacity: 1; animation: bob 1s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(4px); } }
.tap-hint { position: absolute; bottom: calc(max(10px, env(safe-area-inset-bottom)) + var(--w) * .37); left: 0; right: 0;
  text-align: center; font-size: 12px; color: rgba(240, 217, 181, .65); letter-spacing: .08em; z-index: 4; transition: opacity .5s; pointer-events: none; }
.tap-hint.is-off { opacity: 0; }

/* ───────── 입력 패널 ───────── */
.panel { position: absolute; left: 14px; right: 14px; bottom: calc(max(10px, env(safe-area-inset-bottom)) + var(--w) * .40); z-index: 6;
  background: linear-gradient(180deg, rgba(34, 29, 64, .94), rgba(18, 16, 36, .96)); border: 1px solid rgba(240, 217, 181, .45);
  border-radius: 14px; padding: 16px 16px 14px; box-shadow: 0 10px 40px rgba(0,0,0,.6), inset 0 0 0 3px rgba(13, 11, 26, .6);
  cursor: default; animation: rise .45s ease both; max-height: calc(100% - var(--w) * .40 - 40px); overflow-y: auto; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }
.panel h3 { margin: 0 0 12px; font-family: var(--serif); font-size: 15px; color: var(--gold); text-align: center; letter-spacing: .04em; }
.field { margin-bottom: 11px; }
.field > label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 5px; }
.row { display: flex; gap: 6px; }
.row > * { flex: 1; min-width: 0; }
.input, select.input { width: 100%; height: 44px; border-radius: 9px; border: 1px solid rgba(169, 160, 194, .35);
  background: rgba(7, 6, 15, .6); padding: 0 10px; font-size: 16px; outline: none; }
select.input { appearance: none; -webkit-appearance: none; padding-right: 22px;
  background-image: linear-gradient(45deg, transparent 50%, var(--gold-2) 50%), linear-gradient(135deg, var(--gold-2) 50%, transparent 50%);
  background-position: calc(100% - 13px) 19px, calc(100% - 8px) 19px; background-size: 5px 5px; background-repeat: no-repeat; }
select.input option { background: var(--ink-2); }
.input:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(240, 217, 181, .15); }
.seg { display: flex; background: rgba(7, 6, 15, .6); border: 1px solid rgba(169, 160, 194, .35); border-radius: 9px; padding: 3px; }
.seg button { flex: 1; height: 36px; border: 0; border-radius: 7px; background: transparent; cursor: pointer; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--violet-2); color: #fff; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; margin-top: 6px; }
.check input { width: 17px; height: 17px; accent-color: var(--violet); }
.name-hint { margin: -4px 2px 8px; font-size: 12.5px; color: var(--muted); line-height: 1.5; word-break: keep-all; }
#nameSuggest { margin-bottom: 6px; }
#nameSuggest .btn { font-size: 14.5px; padding: 0 8px; }
.field > label .sub { font-size: 11.5px; opacity: .75; margin-left: 4px; }
.preview { margin: 5px 2px 0; font-size: 12.5px; min-height: 0; line-height: 1.45; word-break: keep-all; }
.preview:empty { display: none; }
.preview.ok { color: #9ff0c8; }
.preview.bad { color: #ff9aa8; }
.combo { position: relative; }
.combo-list { list-style: none; margin: 6px 0 0; padding: 4px; max-height: 208px; overflow-y: auto; border-radius: 10px;
  background: rgba(7, 6, 15, .92); border: 1px solid rgba(240, 217, 181, .3); -webkit-overflow-scrolling: touch; }
.combo-list li { display: flex; align-items: baseline; gap: 8px; padding: 10px 10px; border-radius: 7px; cursor: pointer; font-size: 15px; }
.combo-list li span { font-size: 12px; color: var(--muted); }
.combo-list li.on, .combo-list li:hover { background: rgba(139, 108, 240, .28); }
.combo-list li.empty { cursor: default; color: var(--muted); font-size: 13px; }
/* 모바일 입력 중: 패널을 위로, 대화창 숨김 */
#stage.typing .panel { top: max(12px, env(safe-area-inset-top)); bottom: auto; max-height: calc(100% - 24px); }
#stage.typing .dialog, #stage.typing .skip { opacity: 0; pointer-events: none; }
.err { color: #ff9aa8; font-size: 12.5px; min-height: 1em; margin: 2px 0 6px; text-align: center; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 50px; padding: 0 18px; border-radius: 12px;
  border: 0; cursor: pointer; font-family: var(--serif); font-weight: 700; font-size: 16px; transition: transform .1s, filter .2s; }
.btn:active { transform: scale(.97); }
.btn-gold { background: linear-gradient(180deg, #f6e3c0, #caa472); color: #2a1d10; box-shadow: 0 4px 18px rgba(240, 217, 181, .25); }
.btn-violet { background: linear-gradient(180deg, #9d82f7, #5a3fb8); color: #fff; box-shadow: 0 4px 18px rgba(139, 108, 240, .35); }
.btn-pink { background: linear-gradient(180deg, #ff9cc4, #d4508a); color: #fff; box-shadow: 0 4px 18px rgba(255, 120, 170, .35); }
.btn-ghost { background: transparent; border: 1px solid rgba(240, 217, 181, .4); color: var(--gold); }
.btn-block { width: 100%; }
.choice { display: flex; gap: 8px; }
.choice .btn { flex: 1; height: 46px; font-size: 15px; }

/* ───────── 기둥 공개 ───────── */
.pillars { position: absolute; top: 12%; left: 0; right: 0; display: flex; justify-content: center; gap: 8px; z-index: 4; pointer-events: none; }
.pillar { width: 62px; text-align: center; opacity: 0; transform: translateY(-16px) scale(.9); transition: all .6s cubic-bezier(.2, 1.4, .4, 1); }
.pillar.is-on { opacity: 1; transform: none; }
.pillar .pl { font-size: 11px; color: var(--gold); margin-bottom: 4px; text-shadow: 0 1px 3px #000; }
.pillar .ch { height: 58px; display: grid; place-items: center; font-family: var(--serif); font-size: 32px; font-weight: 700; border-radius: 8px;
  background: rgba(13, 11, 26, .78); border: 1px solid rgba(240, 217, 181, .35); margin-bottom: 4px; box-shadow: 0 0 18px rgba(127, 240, 184, .25); }
.pillar .ch small { display: block; font-size: 10px; font-family: var(--sans); font-weight: 400; color: var(--muted); margin-top: -8px; }
.el-0 { color: #8fe39a; } .el-1 { color: #ff8f7a; } .el-2 { color: #f1c76b; } .el-3 { color: #e6e9f2; } .el-4 { color: #7fb4ff; }
.pillar.unknown .ch { color: var(--muted); font-size: 22px; }

/* ───────── 결과지 ───────── */
#result { position: relative; width: var(--w); height: 100%; z-index: 2; background: var(--ink);
  background-image: radial-gradient(ellipse at 50% -10%, rgba(139, 108, 240, .28), transparent 55%); animation: fadein .8s ease both; }
@keyframes fadein { from { opacity: 0; } }
.scroll { height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 28px 16px calc(40px + env(safe-area-inset-bottom)); }
.r-head { text-align: center; margin-bottom: 18px; }
.r-kicker { margin: 0 0 8px; font-size: 12px; letter-spacing: .12em; color: var(--gold-2); }
.r-name { margin: 0; font-family: var(--serif); font-size: 26px; color: var(--gold); }
.r-potion { margin: 6px 0 0; color: var(--potion); font-family: var(--serif); font-size: 15px; }
.card-wrap { position: relative; border-radius: 16px; overflow: hidden; box-shadow: 0 16px 50px rgba(0,0,0,.6), 0 0 0 1px rgba(240,217,181,.25);
  aspect-ratio: 1080 / 1920; max-width: 400px; margin: 0 auto; background: var(--ink-2); }
.card-wrap img { display: block; width: 100%; height: 100%; object-fit: cover; -webkit-touch-callout: default; user-select: auto; }
.card-loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-family: var(--serif); }
.save-hint { text-align: center; font-size: 12px; color: var(--muted); margin: 10px 0 12px; }
.actions { display: flex; gap: 8px; max-width: 400px; margin: 0 auto 26px; }
.actions .btn { flex: 1; font-size: 15px; padding: 0 10px; }
.actions-bottom { flex-direction: column; margin-top: 10px; }
.actions-bottom .btn { flex: none; }

.sec { background: linear-gradient(180deg, rgba(34, 29, 64, .7), rgba(22, 19, 43, .7)); border: 1px solid rgba(240, 217, 181, .16);
  border-radius: 16px; padding: 18px 16px; margin-bottom: 14px; }
.sec-k { font-size: 12px; letter-spacing: .1em; color: var(--gold-2); margin: 0 0 6px; }
.sec h2 { margin: 0 0 10px; font-family: var(--serif); font-size: 20px; color: var(--gold); word-break: keep-all; }
.sec p { margin: 0 0 8px; line-height: 1.75; font-size: 15px; word-break: keep-all; color: #e2dbef; }
.sec p.witch { font-family: var(--serif); color: #f4ecdf; }
.sec p.witch::before { content: '✦ '; color: var(--violet); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 4px; }
.chip { font-size: 12.5px; padding: 5px 10px; border-radius: 999px; background: rgba(139, 108, 240, .18); border: 1px solid rgba(139, 108, 240, .4); color: #d9d0ff; }
.chip.gold { background: rgba(240, 217, 181, .12); border-color: rgba(240, 217, 181, .35); color: var(--gold); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 14.5px; margin: 6px 0 10px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; color: var(--text); word-break: keep-all; }
.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 50%; vertical-align: -2px; margin-right: 6px; border: 1px solid rgba(255,255,255,.4); }
.stat { display: grid; grid-template-columns: 64px 1fr 30px; align-items: center; gap: 10px; margin: 9px 0; font-size: 14px; }
.stat .bar { height: 10px; border-radius: 99px; background: rgba(255,255,255,.08); overflow: hidden; }
.stat .bar i { display: block; height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, #5a3fb8, #b69cff, #f0d9b5); transition: width 1.2s cubic-bezier(.2,.8,.2,1); }
.stat b { text-align: right; color: var(--gold); font-weight: 400; }
.charm { border-top: 1px dashed rgba(240, 217, 181, .18); padding-top: 12px; margin-top: 12px; }
.charm:first-of-type { border-top: 0; padding-top: 0; margin-top: 4px; }
.charm h3 { margin: 0 0 6px; font-family: var(--serif); font-size: 17px; color: #fff; }
.charm h3 span { font-size: 13px; color: var(--gold-2); margin-left: 6px; }
.charm .where { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.elbars { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; align-items: end; height: 110px; margin: 8px 0 4px; }
.elbar { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; font-size: 12px; }
.elbar i { width: 70%; border-radius: 6px 6px 2px 2px; margin: 4px 0; min-height: 4px; opacity: .85; }
.table-wrap { overflow-x: auto; }
table.saju { width: 100%; border-collapse: collapse; text-align: center; font-size: 13px; min-width: 300px; }
table.saju th { color: var(--muted); font-weight: 400; padding: 4px; }
table.saju td { padding: 5px 2px; border-top: 1px solid rgba(255,255,255,.06); }
table.saju td.big { font-family: var(--serif); font-size: 24px; font-weight: 700; }
details summary { cursor: pointer; color: var(--gold); font-family: var(--serif); font-size: 16px; list-style: none; }
details summary::-webkit-details-marker { display: none; }
details summary::after { content: ' ▾'; color: var(--muted); }
details[open] summary::after { content: ' ▴'; }
.chem { border-color: rgba(127, 240, 184, .35); background: linear-gradient(180deg, rgba(30, 60, 55, .55), rgba(22, 19, 43, .7)); }
.chem h2 { color: var(--potion); }

.tabs { position: sticky; top: -28px; z-index: 5; display: flex; gap: 6px; max-width: 400px; margin: -12px auto 18px; padding: 6px;
  background: rgba(13, 11, 26, .9); border: 1px solid rgba(240, 217, 181, .22); border-radius: 999px; backdrop-filter: blur(8px); }
.tabs button { flex: 1; height: 42px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font-family: var(--serif); font-size: 15px; cursor: pointer; }
.tabs button.on { background: linear-gradient(180deg, #ff9cc4, #d4508a); color: #fff; }
.tabs button[data-tab=mine].on { background: var(--violet-2); }
.r-name .heart { color: #ff8fb8; font-size: .8em; margin: 0 4px; }
.invite-hint { max-width: 400px; margin: -16px auto 24px; text-align: center; font-size: 12.5px; color: #ffb3d1; word-break: keep-all; line-height: 1.6; }
.vs-head { display: flex; justify-content: space-between; font-family: var(--serif); color: var(--gold); font-size: 15px; margin: 4px 0 6px; }
.vs-head.total { color: var(--muted); font-family: var(--sans); font-size: 13px; margin: 8px 0 12px; }
.vs-row { display: grid; grid-template-columns: 30px 1fr 64px 1fr 30px; align-items: center; gap: 6px; margin: 8px 0; font-size: 13.5px; text-align: center; }
.vs-row b { font-weight: 400; color: var(--muted); }
.vs-row b.win { color: var(--gold); font-weight: 700; }
.vs-bar { height: 9px; border-radius: 99px; background: rgba(255,255,255,.08); overflow: hidden; display: flex; }
.vs-bar i { display: block; height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, #b69cff, #f0d9b5); transition: width 1.1s cubic-bezier(.2,.8,.2,1); }
.vs-bar.left { justify-content: flex-end; }
.vs-bar.left i { background: linear-gradient(270deg, #ff9cc4, #f0d9b5); }
.pillar.heart .ch { background: transparent; border: 0; box-shadow: none; color: #ff8fb8; font-size: 30px; }
.made-by { margin: 6px 0 0 !important; font-size: 11px; letter-spacing: .08em; opacity: .55; }
.foot { margin-top: 24px; font-size: 12px; color: var(--muted); line-height: 1.7; text-align: center; word-break: keep-all; }
.foot p { margin: 0 0 6px; }
.foot a { color: var(--gold-2); }
.credit { margin: 0 0 4px !important; font-size: 10.5px; opacity: .45; user-select: none; -webkit-user-select: none; }

.toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); z-index: 50; background: rgba(13, 11, 26, .95);
  border: 1px solid rgba(240, 217, 181, .4); color: var(--gold); padding: 11px 18px; border-radius: 999px; font-size: 14px; animation: rise .3s ease both; white-space: nowrap; }
.sheet { position: fixed; inset: 0; z-index: 45; background: rgba(5, 4, 12, .72); display: flex; align-items: flex-end; justify-content: center; animation: fadein .2s ease both; }
.sheet-inner { width: min(100%, 520px); background: linear-gradient(180deg, #221d40, #16132b); border: 1px solid rgba(240, 217, 181, .3); border-bottom: 0;
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; animation: rise .3s ease both; }
.sheet-title { margin: 0 0 6px; text-align: center; font-family: var(--serif); color: var(--gold); font-size: 16px; }
.sheet-inner .btn { width: 100%; }
.btn-kakao { background: #FEE500; color: #191919; font-family: var(--sans); font-weight: 700; }
.kakao-icon { width: 20px; height: 20px; margin-right: 4px; background: no-repeat center/contain url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23191919' d='M12 3C6.48 3 2 6.58 2 11c0 2.85 1.86 5.35 4.66 6.77l-.95 3.48c-.08.3.26.54.52.37l4.13-2.74c.54.08 1.08.12 1.64.12 5.52 0 10-3.58 10-8S17.52 3 12 3z'/%3E%3C/svg%3E"); }
.sheet-close { background: none; border: 0; color: var(--muted); font-size: 14px; padding: 8px; cursor: pointer; }
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(5, 4, 12, .9); display: grid; place-items: center; padding: 20px; }
.modal-inner { width: min(100%, 380px); text-align: center; }
.modal-inner p { color: var(--gold); margin: 0 0 10px; }
.modal-inner img { width: 100%; border-radius: 12px; margin-bottom: 12px; -webkit-touch-callout: default; user-select: auto; }

@media (prefers-reduced-motion: reduce) {
  .bg.is-on, .glow.is-on, .next-arrow.is-on { animation: none; }
}
