/* 별이 빛나는 밤 퍼즐 — 스타일 (외부 글꼴·CDN 없이 오프라인 동작) */
:root {
  --night-0: #050b24;
  --night-1: #0a1538;
  --night-2: #13245c;
  --blue: #3b6fd8;
  --sky: #8fc6ff;
  --star: #ffd23f;
  --star-soft: #fff1a8;
  --ink: #f4f7ff;
  --ink-dim: #aebbe0;
  --card: rgba(16, 30, 78, 0.92);
  --line: rgba(160, 190, 255, 0.22);
  --danger: #ef4c5a;
  --tb-h: 52px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  position: fixed; inset: 0; overflow: hidden; overscroll-behavior: none;
  background: var(--night-0); color: var(--ink); font-family: var(--font);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--star); outline-offset: 2px; }
[hidden] { display: none !important; }

#sky { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; }
#fx { position: fixed; inset: 0; z-index: 60; pointer-events: none; }
.screen { position: fixed; inset: 0; z-index: 1; }

/* ───── 메뉴 ───── */
#menu { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(20px + var(--safe-b)) calc(16px + var(--safe-l)); }
.menu-wrap { max-width: 980px; margin: 0 auto; display: grid; gap: 14px; justify-items: center; text-align: center; }
.menu-head { display: grid; justify-items: center; gap: 2px; }
.menu-head h1 { position: relative; margin: 2px 0 0; font-size: clamp(32px, 6.6vw, 58px); font-weight: 900; letter-spacing: -0.02em; line-height: 1.1; }
.t-main { background: linear-gradient(180deg, #fff9d6 10%, var(--star) 60%, #ffb21f); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 3px 14px rgba(255, 210, 63, 0.35)); }
/* 「퍼즐」: 그림 하늘색 꼬리표, 살짝 기울여 손으로 붙인 스티커처럼 */
.t-tag { position: absolute; left: calc(100% + 0.16em); top: 0.12em; white-space: nowrap; padding: 0.12em 0.42em 0.16em; border-radius: 0.5em; font-size: 0.36em; font-weight: 800; letter-spacing: 0.04em; color: #0a1538;
  background: linear-gradient(180deg, #cfe8ff, var(--sky)); box-shadow: 0 4px 14px rgba(143, 198, 255, 0.35); transform: rotate(-6deg); }
/* 「by 이름」: 그림 귀퉁이 서명처럼 — 기울인 세리프 + 양옆 가는 선 */
.byline { margin: 4px 0 0; display: flex; align-items: center; gap: 12px; color: #c9dcff; font: italic 400 clamp(16px, 2.4vw, 20px) / 1.2 Georgia, "Times New Roman", serif; letter-spacing: 0.06em; }
.byline::before, .byline::after { content: ""; width: clamp(28px, 8vw, 60px); height: 1px; background: linear-gradient(90deg, transparent, rgba(201, 220, 255, 0.7)); }
.byline::after { transform: scaleX(-1); }
.byline span { color: var(--star-soft); font-size: 1.12em; }
.menu-art { width: min(560px, 88vw); aspect-ratio: 3 / 2; animation: float 6s ease-in-out infinite; }
.menu-art canvas { width: 100%; height: 100%; filter: drop-shadow(0 14px 30px rgba(0, 0, 0, 0.55)) drop-shadow(0 0 30px rgba(120, 170, 255, 0.25)); }
@keyframes float { 50% { transform: translateY(-8px) rotate(-0.6deg); } }
.resume { display: flex; gap: 10px; align-items: center; justify-content: center; min-height: 52px; padding: 10px 24px; border: 0; border-radius: 999px;
  background: linear-gradient(180deg, #ffe066, #ffbf1f); color: #2a1a00; font-weight: 800; font-size: 18px; box-shadow: 0 6px 20px rgba(255, 190, 31, 0.35); }
.resume small { font-weight: 600; opacity: 0.8; font-size: 14px; }
.levels-title { margin: 6px 0 0; font-size: 17px; color: var(--ink-dim); font-weight: 700; }
.levels { width: 100%; display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; }
.loading { grid-column: 1 / -1; color: var(--ink-dim); }
.level { position: relative; display: grid; gap: 4px; justify-items: center; padding: 14px 8px 12px; border-radius: 18px; border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(40, 70, 160, 0.55), rgba(16, 30, 78, 0.85)); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3); transition: transform 0.12s; }
.level:hover { transform: translateY(-3px); border-color: rgba(255, 210, 63, 0.6); }
.level:active { transform: scale(0.97); }
.level .lv-ic { font-size: 32px; line-height: 1; }
.level .lv-name { font-size: 19px; font-weight: 800; }
.level .lv-pcs { font-size: 14px; color: var(--sky); font-weight: 700; }
.level .lv-meta { font-size: 12.5px; color: var(--ink-dim); }
.level .lv-best { font-size: 13px; color: var(--star); min-height: 1.3em; }
.level .lv-best.none { color: var(--ink-dim); opacity: 0.7; }
.level .lv-tag { position: absolute; top: -8px; right: 8px; font-size: 11px; font-weight: 800; padding: 2px 8px; border-radius: 99px; background: var(--star); color: #2a1a00; }
.menu-foot { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.chip { border: 1px solid var(--line); background: rgba(16, 30, 78, 0.7); border-radius: 999px; padding: 10px 16px; min-height: 44px; font-size: 15px; }
.chip[aria-pressed="false"] { opacity: 0.7; }
@media (max-width: 760px) {
  .levels { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .level:last-child { grid-column: 1 / -1; }
}

/* ───── 게임 화면 ───── */
#game { display: flex; flex-direction: column; }
.toolbar { position: relative; z-index: 5; flex: 0 0 auto; display: flex; align-items: center; gap: 6px; height: calc(var(--tb-h) + var(--safe-t));
  padding: var(--safe-t) calc(8px + var(--safe-r)) 0 calc(8px + var(--safe-l)); background: rgba(6, 12, 40, 0.82); border-bottom: 1px solid var(--line);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); white-space: nowrap; }
.tools { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.tools::-webkit-scrollbar { display: none; }
.tb { position: relative; display: inline-flex; align-items: center; gap: 5px; height: 40px; min-width: 40px; padding: 0 11px; border-radius: 12px;
  border: 1px solid transparent; background: rgba(255, 255, 255, 0.06); font-size: 15px; font-weight: 700; flex: 0 0 auto; }
.tb:hover { background: rgba(255, 255, 255, 0.13); }
.tb:active { transform: scale(0.95); }
.tb[aria-pressed="true"] { background: rgba(255, 210, 63, 0.2); border-color: rgba(255, 210, 63, 0.6); }
.tb .ic { font-size: 18px; line-height: 1; }
.tb.chance { background: linear-gradient(180deg, rgba(255, 224, 102, 0.28), rgba(255, 191, 31, 0.18)); border-color: rgba(255, 210, 63, 0.5); }
.tb.chance.empty { opacity: 0.45; }
.tb .badge { min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; background: var(--star); color: #2a1a00; font-size: 12px; line-height: 20px; text-align: center; }
.tb.shake { animation: shake 0.4s; }
@keyframes shake { 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }
.status { display: flex; align-items: baseline; gap: 10px; min-width: 0; font-variant-numeric: tabular-nums; }
.st-level { font-weight: 800; color: var(--star-soft); overflow: hidden; text-overflow: ellipsis; }
.st-count { font-size: 15px; color: var(--ink-dim); }
.st-count b { color: var(--ink); font-size: 18px; }
.st-time { font-size: 15px; color: var(--sky); }
.progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 3px; background: rgba(255, 255, 255, 0.06); }
.progress i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--sky), var(--star)); transition: width 0.3s; }
/* 좁은 화면: 도구 글씨 감추고 아이콘만(툴바는 한 줄 유지) */
@media (max-width: 820px) { .tb .lb { display: none; } .tb { padding: 0 9px; } }
@media (max-width: 480px) { .st-level { display: none; } .tb { min-width: 36px; height: 38px; padding: 0 6px; gap: 3px; } .tb .ic { font-size: 17px; } .tools { gap: 2px; } .toolbar { gap: 4px; padding-left: calc(4px + var(--safe-l)); padding-right: calc(4px + var(--safe-r)); } .status { gap: 6px; } .tb .badge { min-width: 18px; height: 18px; line-height: 18px; } }
@media (max-width: 360px) { #btnHelp { display: none; } }

#table { position: relative; flex: 1 1 auto; overflow: hidden; touch-action: none; }
#board { position: absolute; left: 0; top: 0; pointer-events: none; transition: filter 0.6s;
  filter: drop-shadow(0 0 1.5px rgba(170, 205, 255, 0.85)) drop-shadow(0 0 16px rgba(90, 140, 255, 0.35)); }
#pieces { position: absolute; inset: 0; }
.pc { position: absolute; left: 0; top: 0; will-change: transform; pointer-events: none; }
.pc.lift { filter: drop-shadow(0 10px 12px rgba(0, 0, 0, 0.55)); }
.pc.sel { filter: drop-shadow(0 0 6px rgba(255, 220, 90, 0.95)) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.5)); }
.pc.glide { transition: transform 0.35s cubic-bezier(0.2, 0.8, 0.2, 1); }
.pc.snapping { transition: transform 0.1s ease-out; }
.pc.pop { animation: pop 0.32s ease-out; }
@keyframes pop { 40% { filter: brightness(1.45); } }
.pc.flying { filter: drop-shadow(0 0 10px rgba(255, 230, 120, 1)) drop-shadow(0 8px 12px rgba(0, 0, 0, 0.5)); }
#table.done .pc { transition: opacity 0.8s; opacity: 0; }
#table.done #board { filter: drop-shadow(0 0 22px rgba(255, 220, 120, 0.55)); animation: glow 2.4s ease-in-out infinite; }
@keyframes glow { 50% { filter: drop-shadow(0 0 40px rgba(255, 220, 120, 0.85)); } }

#toast { position: fixed; left: 50%; top: calc(var(--tb-h) + var(--safe-t) + 14px); z-index: 30; transform: translate(-50%, -10px); opacity: 0; pointer-events: none;
  background: rgba(10, 20, 60, 0.92); border: 1px solid rgba(255, 210, 63, 0.55); color: var(--ink); padding: 10px 18px; border-radius: 999px;
  font-weight: 800; font-size: 16px; transition: opacity 0.2s, transform 0.2s; white-space: nowrap; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ───── 모달(완성·확인) ───── */
.modal { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; padding: 16px; background: rgba(2, 6, 22, 0.55); animation: fade 0.25s; }
@keyframes fade { from { opacity: 0; } }
.card { width: min(440px, 100%); background: var(--card); border: 1px solid rgba(255, 210, 63, 0.4); border-radius: 24px; padding: 22px 20px 18px;
  text-align: center; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6); animation: rise 0.35s cubic-bezier(0.2, 1.4, 0.4, 1); }
@keyframes rise { from { transform: translateY(30px) scale(0.9); opacity: 0; } }
.card h2 { margin: 4px 0 6px; font-size: 30px; color: var(--star-soft); }
.card p { margin: 6px 0; color: var(--ink-dim); line-height: 1.5; }
.res-stars { display: flex; justify-content: center; gap: 6px; font-size: 52px; line-height: 1; }
.res-stars span { color: rgba(255, 255, 255, 0.16); transform: scale(0.4); transition: transform 0.35s cubic-bezier(0.2, 1.8, 0.4, 1), color 0.2s; }
.res-stars span.on { color: var(--star); transform: scale(1); text-shadow: 0 0 18px rgba(255, 210, 63, 0.8); }
.res-stars span.off { transform: scale(0.8); }
.res-stats { list-style: none; margin: 12px 0 6px; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.res-stats li { background: rgba(255, 255, 255, 0.06); border-radius: 14px; padding: 8px 4px; display: grid; gap: 2px; }
.res-stats span { font-size: 12.5px; color: var(--ink-dim); }
.res-stats b { font-size: 19px; font-variant-numeric: tabular-nums; }
.res-record { color: var(--star) !important; font-weight: 800; font-size: 17px; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }
.res-tip { font-size: 13.5px; }
.res-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.btn { min-height: 46px; padding: 10px 16px; border-radius: 14px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); font-weight: 800; font-size: 16px; }
.btn:hover { background: rgba(255, 255, 255, 0.15); }
.btn.primary { background: linear-gradient(180deg, #ffe066, #ffbf1f); color: #2a1a00; border: 0; }
.btn.ghost { background: transparent; }
.confirm p { color: var(--ink); font-size: 17px; font-weight: 700; }

/* ───── 플로팅 창 ───── */
.fwin { position: fixed; z-index: 50; display: flex; flex-direction: column; min-width: 220px; min-height: 160px; background: rgba(12, 24, 66, 0.96);
  border: 1px solid rgba(160, 190, 255, 0.35); border-radius: 14px; box-shadow: 0 16px 44px rgba(0, 0, 0, 0.55); overflow: hidden; }
.fwin-bar { flex: 0 0 auto; display: flex; align-items: center; height: 40px; padding: 0 4px 0 12px; background: rgba(255, 255, 255, 0.07); cursor: move; touch-action: none; }
.fwin-title { flex: 1; font-size: 15px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fwin-close { width: 34px; height: 30px; border: 0; border-radius: 9px; background: var(--danger); color: #fff; font-size: 15px; font-weight: 900; line-height: 1; }
.fwin-close:hover { filter: brightness(1.15); }
.fwin-body { flex: 1 1 auto; min-height: 0; position: relative; }
.fwin-body.art canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.fwin-body.text { overflow-y: auto; padding: 10px 16px 14px; font-size: 15px; line-height: 1.6; -webkit-user-select: text; user-select: text; }
.fwin-resize { position: absolute; right: 0; bottom: 0; width: 26px; height: 26px; cursor: nwse-resize; touch-action: none;
  background: linear-gradient(135deg, transparent 50%, rgba(255, 255, 255, 0.35) 50%, rgba(255, 255, 255, 0.35) 58%, transparent 58%, transparent 70%, rgba(255, 255, 255, 0.35) 70%, rgba(255, 255, 255, 0.35) 78%, transparent 78%); }
.howto { padding-left: 1.3em; margin: 4px 0; }
.howto li { margin: 6px 0; }
.keys { color: var(--ink-dim); font-size: 13.5px; margin: 8px 0 0; }
kbd { display: inline-block; min-width: 1.6em; padding: 0 5px; border-radius: 5px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); text-align: center; font-family: inherit; font-size: 12.5px; }
.records { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.records th, .records td { padding: 7px 6px; border-bottom: 1px solid var(--line); text-align: center; }
.records th { color: var(--ink-dim); font-size: 13px; font-weight: 700; }
.records td.stars { color: var(--star); letter-spacing: 1px; }

.noscript { position: fixed; inset: 0; display: grid; place-items: center; z-index: 99; background: var(--night-0); }
@media (prefers-reduced-motion: reduce) { .menu-art, #table.done #board, .res-record { animation: none; } }
