:root {
  --ink: #1a100c;
  --night: #120c0a;
  --red: #c8321f;
  --red-deep: #8e1d12;
  --gold: #f2c14e;
  --gold-deep: #b9832a;
  --cream: #fff4dc;
  --jade: #1f6b4f;
  --muted: rgba(255, 244, 220, 0.72);
  --panel: rgba(24, 12, 8, 0.78);
  --line: rgba(242, 193, 78, 0.38);
  --card-ratio: 1.411;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-size: 16px;
}
:root[data-fontsize="sm"] { font-size: 14px; }
:root[data-fontsize="lg"] { font-size: 19px; }
:root[data-fontsize="xl"] { font-size: 23px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--night); overscroll-behavior: none; }
body {
  font-family: 'Noto Sans KR', system-ui, sans-serif;
  color: var(--cream);
  min-height: 100%;
  background: var(--night);
}
button { font-family: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(circle at 50% -10%, rgba(242, 193, 78, 0.12), transparent 55%),
    var(--night);
  padding: calc(var(--safe-t) + 10px) 10px calc(var(--safe-b) + 10px);
}

.title { font-family: 'Black Han Sans', sans-serif; letter-spacing: 0.5px; }

/* ---------- 상단 바 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px 10px;
}
.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.2;
  text-align: center;
  background: linear-gradient(180deg, #fff6c9 0%, #ffd24d 45%, #e08a18 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.topbar .stats { display: flex; gap: 10px; font-size: 0.8125rem; color: var(--muted); }
.topbar .stats b { color: var(--gold); font-weight: 900; }
.topbar-icons { display: flex; align-items: center; gap: 4px; flex: none; }

.iconbtn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 1rem;
}

/* ---------- 난이도 선택 화면 ---------- */
.onboard {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 18px;
  padding: 10px 6px 30px;
  text-align: center;
  background:
    linear-gradient(180deg, rgba(18, 12, 10, 0.15) 0%, rgba(18, 12, 10, 0.55) 65%, rgba(18, 12, 10, 0.9) 100%),
    url('../img/bg_onboard.png') center / cover no-repeat;
  border-radius: 18px;
}
.onboard-icons { position: absolute; top: 4px; right: 4px; }
.offline-block {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  padding: 30px;
}
.offline-block .offline-ico { font-size: 46px; margin-bottom: 4px; }
.offline-block h1 { font-family: 'Black Han Sans', sans-serif; font-size: 1.25rem; margin: 0; color: var(--gold); }
.offline-block p { color: var(--muted); font-size: 0.875rem; line-height: 1.6; margin: 0 0 16px; max-width: 320px; }
.offline-block .bbtn { flex: none; min-width: 160px; }
.onboard .logo { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 4px; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5); }
.onboard p.desc { color: var(--muted); font-size: 0.875rem; line-height: 1.6; margin: 0 auto; max-width: 320px; }
.levels { display: flex; flex-direction: column; gap: 12px; max-width: 360px; margin: 6px auto 0; width: 100%; }
.level-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 18px;
  border-radius: 16px;
  background: var(--panel);
  border: 1.5px solid var(--line);
  text-align: left;
}
.level-btn .lv-name { font-weight: 900; font-size: 1.0625rem; color: var(--cream); }
.level-btn.menu-btn { justify-content: center; padding: 12px 18px; background: rgba(255, 244, 220, 0.04); }
.level-btn.menu-btn .lv-name { font-size: 0.9375rem; font-weight: 700; }
.level-btn .lv-desc { font-size: 0.78125rem; color: var(--muted); margin-top: 2px; }
.level-btn .lv-free { font-size: 0.6875rem; color: var(--gold); margin-top: 3px; opacity: 0.85; }
.level-btn .lv-go {
  font-family: 'Black Han Sans', sans-serif;
  font-size: 0.875rem;
  color: var(--ink);
  background: linear-gradient(180deg, #fff1a8 0%, #f6c445 50%, #c8871f 100%);
  padding: 10px 14px;
  min-width: 84px;
  text-align: center;
  border-radius: 999px;
  flex: none;
}

/* ---------- 게임 보드 ---------- */
.board-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.deckrow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 10px;
  gap: 10px;
}
.deckrow.right { flex-direction: row-reverse; }
.deck-pile {
  position: relative;
  width: 44px;
  height: 62px;
  flex: none;
}
.deck-pile .card-back { position: absolute; inset: 0; }
.deck-pile .card-back:nth-child(2) { transform: translate(-3px, -3px); }
.deck-pile.empty { opacity: 0.35; }
.deck-pile .cnt {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  z-index: 2;
  font-size: 0.75rem; font-weight: 900; color: var(--cream); white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.hint-msg { flex: 1; text-align: center; font-size: 0.8125rem; color: var(--muted); min-height: 16px; }

.board {
  flex: 1;
  display: grid;
  gap: 8px;
  padding: 4px 4px 20px;
  overflow-y: auto;
}

.col {
  position: relative;
  border-radius: 8px;
  background: rgba(255, 244, 220, 0.05);
  border: 1px dashed rgba(242, 193, 78, 0.22);
  min-height: 70px;
  padding: 6px 4px 14px;
}
.col.target-ok { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(242, 193, 78, 0.55) inset; background: rgba(242, 193, 78, 0.1); }
.col.hint-dst, .deck-pile.hint-src { animation: hintPulse 0.9s ease-in-out infinite; border-radius: 6px; }
.slot.hint-card .card, .slot.hint-card .card-back { animation: hintCardPulse 0.9s ease-in-out infinite; }
@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 2px rgba(242, 193, 78, 0.9) inset, 0 0 14px 2px rgba(242, 193, 78, 0.6); border-color: var(--gold); }
  50% { box-shadow: 0 0 0 2px rgba(242, 193, 78, 0.4) inset, 0 0 4px 0 rgba(242, 193, 78, 0.2); border-color: var(--line); }
}
@keyframes hintCardPulse {
  0%, 100% { box-shadow: 0 0 0 3px var(--gold), 0 0 16px 3px rgba(242, 193, 78, 0.75); }
  50% { box-shadow: 0 0 0 3px rgba(242, 193, 78, 0.35), 0 0 4px 0 rgba(242, 193, 78, 0.25); }
}

.card {
  display: block;
  width: 100%;
  aspect-ratio: 1 / var(--card-ratio);
  border-radius: 5px;
  object-fit: cover;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
  position: relative;
}
.card-back {
  display: block;
  border-radius: 5px;
  background:
    repeating-linear-gradient(45deg, rgba(255, 207, 112, 0.25) 0 2px, transparent 2px 7px),
    linear-gradient(135deg, #e04a2e, #8a1a0e);
  border: 2px solid #ffd27a;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
}

.col .slot {
  position: absolute;
  left: 4px;
  right: 4px;
  transition: transform 0.15s ease;
}
.col .slot .card, .col .slot .card-back { width: 100%; aspect-ratio: 1 / var(--card-ratio); }
.col .slot.sel .card { box-shadow: 0 0 0 3px var(--gold), 0 8px 18px rgba(0, 0, 0, 0.55); transform: translateY(-6px); }
.slot.shake .card, .slot.shake .card-back { animation: shakeX 0.4s ease; }
@keyframes shakeX {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}
.col .slot.clearing { animation: clearRun 0.5s ease forwards; }
@keyframes clearRun {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-40px) scale(0.7); }
}
.card.dropin { animation: dropin 0.22s ease-out; }
@keyframes dropin { from { transform: translateY(-24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.month-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 54px;
  height: 54px;
  padding: 0 8px;
  border-radius: 999px;
  background: rgba(10, 6, 4, 0.72);
  border: 2px solid var(--line);
  color: var(--gold);
  font-size: 33px;
  font-weight: 900;
  line-height: 50px;
  text-align: center;
}

/* ---------- 하단 버튼 ---------- */
.bottombar {
  display: flex;
  gap: 8px;
  padding: 10px 6px 2px;
}
.bbtn {
  flex: 1;
  padding: 12px 0;
  text-align: center;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-weight: 700;
  font-size: 0.875rem;
}
.bbtn.primary {
  background: linear-gradient(180deg, #ff6a3d 0%, var(--red) 45%, var(--red-deep) 100%);
  border-color: transparent;
}
.bbtn:disabled { opacity: 0.4; }

/* ---------- 오버레이 (모달) ---------- */
#overlay:empty { display: none; }
#overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(6, 4, 2, 0.78);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.modal {
  width: 100%; max-width: 380px;
  background: linear-gradient(180deg, rgba(40, 20, 10, 0.97), rgba(20, 10, 6, 0.97));
  border: 1.5px solid var(--line);
  border-radius: 20px;
  padding: 22px 20px calc(20px + var(--safe-b));
  max-height: 82vh;
  overflow-y: auto;
}
.modal h2 { font-family: 'Black Han Sans', sans-serif; font-size: 1.25rem; margin: 0 0 10px; color: var(--gold); }
.modal p, .modal li { font-size: 0.875rem; line-height: 1.65; color: var(--cream); }
.modal ol, .modal ul { margin: 0; padding-left: 20px; }
.modal .stars { font-size: 30px; text-align: center; margin: 6px 0 14px; letter-spacing: 4px; }
.modal .close-x {
  position: absolute; top: 16px; right: 16px;
  width: 30px; height: 30px; border-radius: 50%;
  background: rgba(255, 244, 220, 0.1); display: grid; place-items: center;
}
.modal { position: relative; }
.modal .actions { display: flex; gap: 10px; margin-top: 16px; }
.modal .actions .bbtn { padding: 13px 0; }

/* ---------- 보상형 광고 (시뮬레이션) ---------- */
@keyframes fadeIn { from { opacity: 0; } }
.ad-full {
  position: absolute;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  background: #0b0b0d;
  border-radius: 20px;
  animation: fadeIn 0.2s ease;
}
.ad-tag { position: absolute; top: 16px; left: 16px; color: var(--muted); font-size: 10px; font-weight: 900; letter-spacing: 0.08em; padding: 2px 6px; border-radius: 4px; background: rgba(255, 244, 220, 0.18); }
.ad-body {
  width: 100%;
  max-width: 300px;
  aspect-ratio: 9 / 12;
  border-radius: 18px;
  border: 1px dashed rgba(255, 255, 255, 0.25);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  background: linear-gradient(160deg, #1d1f2b, #0f1016);
  color: var(--cream);
}
.ad-title { font-size: 1.25rem; font-weight: 900; }
.ad-sub { font-size: 0.78125rem; color: rgba(255, 255, 255, 0.55); padding: 0 20px; }
.ad-count span {
  display: grid;
  place-items: center;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.3);
  font-size: 20px;
  font-weight: 900;
  color: var(--cream);
}
.ad-close {
  min-width: 170px;
  min-height: 46px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--cream);
  font-weight: 900;
}
.ad-close:disabled { opacity: 0.35; }
.ad-close.ready { background: linear-gradient(180deg, #fff1a8, #f6c445 50%, #c8871f); color: #4a2308; }

/* ---------- 닉네임 ---------- */
.name-field {
  display: flex;
  align-items: center;
  gap: 8px;
  border-radius: 12px;
  background: var(--panel);
  border: 1.5px solid var(--line);
  padding: 12px 14px;
  margin: 4px 0 2px;
}
.name-field input {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  color: var(--cream);
  font-size: 0.9375rem;
  font-family: inherit;
}
.name-field input:focus { outline: none; }

/* ---------- 랭킹 ---------- */
.rank-tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.rank-tab {
  flex: 1;
  padding: 8px 0;
  text-align: center;
  border-radius: 999px;
  font-size: 0.8125rem;
  font-weight: 700;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
}
.rank-tab.on { background: linear-gradient(180deg, #fff1a8 0%, #f6c445 50%, #c8871f 100%); color: var(--ink); border-color: transparent; }
.rank-list { list-style: none; margin: 0; padding: 0; max-height: 46vh; overflow-y: auto; }
.rank-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 6px;
  border-bottom: 1px solid rgba(255, 244, 220, 0.08);
  font-size: 0.875rem;
}
.rank-list li.me { background: rgba(242, 193, 78, 0.12); border-radius: 8px; }
.rank-list .rk-no { width: 26px; flex: none; text-align: center; font-weight: 900; color: var(--gold); }
.rank-list .rk-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-list .rk-score { font-weight: 900; color: var(--cream); }
.rank-empty { text-align: center; color: var(--muted); font-size: 0.8125rem; padding: 24px 0; }

/* ---------- 설정 ---------- */
.opt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 2px;
  border-bottom: 1px solid rgba(255, 244, 220, 0.08);
  flex-wrap: wrap;
}
.opt-label { font-size: 0.875rem; color: var(--cream); }
.opt-value { font-size: 0.875rem; color: var(--muted); }
.opt-switch {
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: rgba(255, 244, 220, 0.16);
  position: relative;
  flex: none;
}
.opt-switch::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--cream);
  transition: transform 0.15s ease;
}
.opt-switch.on { background: linear-gradient(180deg, #fff1a8, #f6c445 50%, #c8871f); }
.opt-switch.on::after { transform: translateX(18px); background: var(--ink); }
.opt-divider { height: 1px; background: rgba(255, 244, 220, 0.1); margin: 10px 0; }
.opt-link {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 2px;
  font-size: 0.875rem;
  color: var(--cream);
  text-decoration: none;
  border-bottom: 1px solid rgba(255, 244, 220, 0.08);
}
.opt-version { text-align: center; font-size: 0.75rem; color: var(--muted); margin-top: 14px; cursor: default; user-select: none; }

.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg-btn {
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
}
.seg-btn.on { background: linear-gradient(180deg, #fff1a8 0%, #f6c445 50%, #c8871f 100%); color: var(--ink); border-color: transparent; }

/* ---------- 튜토리얼 ---------- */
.tut-step {
  display: table;
  margin: 0 auto 10px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(242, 193, 78, 0.14);
  border: 1px solid var(--line);
  color: var(--gold);
  font-size: 0.75rem;
  font-weight: 900;
}
.tut-dots { display: flex; justify-content: center; gap: 6px; margin-top: 14px; }
.tut-dot { width: 7px; height: 7px; border-radius: 50%; background: rgba(255, 244, 220, 0.25); }
.tut-dot.on { background: var(--gold); }

/* ---------- 내부 알림(토스트) ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 90px);
  transform: translateX(-50%);
  z-index: 70;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(20, 10, 6, 0.92);
  border: 1px solid var(--gold);
  color: var(--cream);
  font-weight: 700;
  font-size: 0.875rem;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}
#toast.show { animation: toast 2.2s ease forwards; }
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 10px); }
  12%, 80% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -6px); }
}

.hidden { display: none !important; }
