:root {
  --bg: #090d16; --bg2: #0f1524; --panel: #141c2e; --line: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9; --dim: #94a3b8; --accent: #38bdf8; --flag: #f43f5e; --gold: #fbbf24; --ok: #34d399;
  --cell: #1e293b; --cell-hi: #2b3a52; --open: #101a2e;
  --cs: 32px; --r: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(circle at 50% -10%, #12233d, var(--bg) 65%);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
.back-btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px;
  background: rgba(255,255,255,0.07); border: 1px solid var(--line); color: var(--text);
  text-decoration: none; font-size: 0.85rem; font-weight: 600;
}
.back-btn:hover { background: rgba(255,255,255,0.14); }
.total { margin-left: auto; font-weight: 700; }
.total small { color: var(--dim); font-weight: 500; }
.tools { display: flex; gap: 6px; }
.mini { width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); background: rgba(255,255,255,0.07); font-weight: 700; }

/* ── 메뉴 ── */
.screen { flex: 1; overflow-y: auto; padding: 4px 16px 32px; }
.logo { text-align: center; margin: 4px 0 12px; }
.logo-flag { font-size: 2.6rem; display: inline-block; animation: wave 1.6s ease-in-out infinite; transform-origin: 20% 90%; }
@keyframes wave { 0%,100% { transform: rotate(-6deg); } 50% { transform: rotate(8deg); } }
.logo h1 { margin: 2px 0 4px; font-size: 1.55rem; letter-spacing: -0.5px; text-shadow: 0 0 18px rgba(56,189,248,0.45); }
.logo h1 span { color: var(--accent); }
.tagline { margin: 0; color: var(--dim); font-size: 0.9rem; }
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; max-width: 720px; margin: 0 auto 12px; }
.tab { padding: 9px 4px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg2); font-size: 0.85rem; font-weight: 700; white-space: nowrap; }
.tab.on { background: rgba(56,189,248,0.16); border-color: var(--accent); color: #bae6fd; }
.panel-wrap { max-width: 720px; margin: 0 auto; display: grid; gap: 12px; }
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r); padding: 12px; }
.card h2 { margin: 0 0 2px; font-size: 1.05rem; display: flex; gap: 8px; align-items: baseline; }
.card h2 small { margin-left: auto; color: var(--gold); font-size: 0.85rem; }
.card p { margin: 0 0 10px; color: var(--dim); font-size: 0.82rem; line-height: 1.45; }
.lv-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (min-width: 560px) { .lv-grid { grid-template-columns: repeat(6, 1fr); } }
.lv-btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 8px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 74px;
}
.lv-btn:hover:not(:disabled) { border-color: var(--accent); }
.lv-btn b { font-size: 1rem; }
.lv-btn span { font-size: 0.7rem; color: var(--dim); text-align: center; line-height: 1.2; }
.lv-btn i { font-style: normal; font-size: 0.75rem; letter-spacing: 1px; color: var(--gold); }
.lv-btn:disabled { opacity: 0.38; cursor: not-allowed; }
.lv-btn.cleared { background: #13314a; }
.diff-list { display: grid; gap: 8px; }
.diff {
  display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); text-align: left; width: 100%;
}
.diff:hover { border-color: var(--accent); }
.diff .ico { font-size: 1.6rem; }
.diff b { display: block; font-size: 1rem; }
.diff span { color: var(--dim); font-size: 0.8rem; }
.diff .rec { margin-left: auto; text-align: right; color: var(--gold); font-weight: 700; font-size: 0.85rem; }
.custom { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 8px; }
.custom label { font-size: 0.75rem; color: var(--dim); display: grid; gap: 4px; }
.custom input { width: 100%; padding: 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }
.opt { display: flex; align-items: center; gap: 10px; padding: 8px 2px; font-size: 0.88rem; }
.opt input { width: 20px; height: 20px; accent-color: var(--accent); }
.opt small { color: var(--dim); display: block; font-size: 0.75rem; }
.big-btn {
  display: block; width: 100%; padding: 14px; border-radius: 14px; border: 0; font-weight: 800; font-size: 1.05rem;
  background: linear-gradient(135deg, #38bdf8, #6366f1); color: #fff; box-shadow: 0 6px 22px rgba(56,189,248,0.3);
}
.stat-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.stat-table th, .stat-table td { padding: 7px 4px; border-bottom: 1px solid var(--line); text-align: center; }
.stat-table th { color: var(--dim); font-weight: 600; }
.stat-table td:first-child, .stat-table th:first-child { text-align: left; }
.week { display: flex; gap: 6px; justify-content: center; margin: 8px 0 12px; }
.week div { width: 36px; text-align: center; font-size: 0.7rem; color: var(--dim); }
.week div b { display: block; font-size: 1.1rem; margin-bottom: 2px; }

/* ── 플레이 ── */
#play { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 0 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.p-head { display: flex; align-items: center; gap: 8px; max-width: 960px; width: 100%; margin: 0 auto; }
.p-title { flex: 1; text-align: center; font-weight: 800; font-size: 0.98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-title small { color: var(--dim); font-weight: 500; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 12px; font-weight: 700; }
.btn.small { padding: 6px 10px; font-size: 0.85rem; }
.btn.primary { background: linear-gradient(135deg, #38bdf8, #6366f1); border: 0; color: #fff; }
.hud { display: flex; gap: 6px; justify-content: center; margin: 8px 0; flex-wrap: wrap; }
.chip { padding: 5px 10px; border-radius: 999px; background: var(--bg2); border: 1px solid var(--line); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.chip b { font-family: ui-monospace, Menlo, monospace; }
.chip.warn { border-color: var(--flag); color: #fda4af; animation: blink 0.6s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0.55; } }
.chip.goal { color: var(--gold); }
.board-wrap {
  flex: 1; min-height: 0; overflow: auto; display: flex; padding: 8px;
  border-radius: var(--r); background: rgba(2,6,15,0.55); border: 1px solid var(--line);
  max-width: 960px; width: 100%; margin: 0 auto; touch-action: manipulation; overscroll-behavior: contain;
}
.board-wrap.shake { animation: shake 0.35s; }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(5px); } 60% { transform: translateX(-3px); } 80% { transform: translateX(2px); } }
.grid { margin: auto; display: grid; gap: 2px; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.c {
  width: var(--cs); height: var(--cs); border-radius: calc(var(--cs) * 0.18);
  background: var(--cell); border: 1px solid #334155; display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: calc(var(--cs) * 0.56); line-height: 1; cursor: pointer;
  font-family: ui-monospace, Menlo, Consolas, monospace;
}
.c.wall { visibility: hidden; }
@media (hover: hover) { .c:not(.o):hover { background: var(--cell-hi); border-color: var(--accent); } }
.c.press { transform: scale(0.9); }
.c.o { background: var(--open); border-color: #172036; cursor: default; }
.c.o.pop { animation: pop 0.28s ease-out backwards; }
@keyframes pop { from { transform: scale(0.4); background: #1e3a5f; } }
.c.f { background: rgba(244,63,94,0.16); border-color: var(--flag); font-size: calc(var(--cs) * 0.5); }
.c.x { background: #b91c1c; border-color: #fca5a5; box-shadow: 0 0 14px #ef4444; font-size: calc(var(--cs) * 0.5); }
.c.m { background: #3b0d16; border-color: #7f1d1d; font-size: calc(var(--cs) * 0.5); }
.c.wrong { background: #3f3f46; font-size: calc(var(--cs) * 0.5); }
.c.start { background: #065f46; border-color: var(--ok); box-shadow: 0 0 12px rgba(52,211,153,0.7); animation: glow 1.1s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 0 2px rgba(52,211,153,0.3); } }
.c.hint { animation: hint 0.9s ease-out; }
@keyframes hint { from { box-shadow: 0 0 0 6px rgba(56,189,248,0.9); } }
.n1 { color: #38bdf8; } .n2 { color: #34d399; } .n3 { color: #f43f5e; } .n4 { color: #a78bfa; }
.n5 { color: #f59e0b; } .n6 { color: #22d3ee; } .n7 { color: #e2e8f0; } .n8 { color: #94a3b8; }
.c.done { opacity: 0.45; }

.toast {
  position: fixed; left: 50%; top: 110px; transform: translateX(-50%); z-index: 20;
  background: rgba(15,23,42,0.95); border: 1px solid var(--accent); padding: 8px 14px; border-radius: 999px;
  font-size: 0.88rem; font-weight: 700; pointer-events: none; white-space: nowrap; animation: toastIn 0.2s;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -8px); } }
.toolbar { display: grid; grid-template-columns: 1.5fr 1.5fr 1fr 1fr; gap: 8px; margin: 8px auto 0; max-width: 560px; width: 100%; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg2); font-size: 1.1rem; min-height: 54px;
}
.tool i { font-style: normal; }
.tool span { font-size: 0.72rem; color: var(--dim); font-weight: 700; }
.tool.mode.flag { background: rgba(244,63,94,0.18); border-color: var(--flag); }
.tool.mode.flag span { color: #fda4af; }
.tool.radar { border-color: rgba(56,189,248,0.45); }
.tool.radar span { color: #7dd3fc; }
.tool:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── 모달 ── */
.modal { position: fixed; inset: 0; background: rgba(2,6,15,0.72); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 30; }
.modal .panel {
  background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; padding: 18px; width: min(420px, 100%);
  max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.6); animation: rise 0.22s ease-out;
}
@keyframes rise { from { transform: translateY(14px); opacity: 0; } }
.modal h2 { margin: 0 0 8px; text-align: center; }
.modal p, .modal li { color: var(--dim); font-size: 0.88rem; line-height: 1.5; }
.modal ul { padding-left: 18px; margin: 6px 0 10px; }
.modal b.hl { color: var(--text); }
.res-stars { color: var(--gold); text-shadow: 0 0 14px rgba(251,191,36,0.5); text-align: center; font-size: 2.2rem; letter-spacing: 6px; margin: 4px 0; }
.res-stars .off { opacity: 0.2; }
.res-time { text-align: center; font-size: 1.6rem; font-weight: 800; font-family: ui-monospace, Menlo, monospace; }
.res-list { list-style: none; padding: 0; margin: 10px 0; }
.res-list li { display: flex; gap: 8px; padding: 4px 0; }
.res-list li.y { color: var(--text); }
.btn-row { display: flex; gap: 8px; margin-top: 12px; }
.btn-row .btn { flex: 1; padding: 11px; }
.panel.err h2 { color: #fda4af; }
.noscript { text-align: center; padding: 40px; }

@media (max-width: 420px) {
  .back-txt { display: none; }
  .tab { font-size: 0.78rem; }
}

/* 지난 판 다시 풀기 */
.past-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
@media (min-width: 560px) { .past-grid { grid-template-columns: repeat(7, 1fr); } }
.past-btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 6px 2px; display: grid; gap: 1px; justify-items: center; }
.past-btn:hover { border-color: var(--accent); }
.past-btn b { font-size: 0.85rem; }
.past-btn span { font-size: 0.9rem; }
.past-btn i { font-style: normal; font-size: 0.68rem; color: var(--dim); }
.past-btn.cleared { background: #13314a; }
.past-btn.cleared i { color: var(--gold); }

/* 업적 */
.ach-bar { height: 6px; border-radius: 3px; background: var(--panel); overflow: hidden; margin: 6px 0 10px; }
.ach-bar div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--gold)); }
.ach-grid { display: grid; grid-template-columns: 1fr; gap: 6px; }
@media (min-width: 560px) { .ach-grid { grid-template-columns: 1fr 1fr; } }
.ach { display: flex; gap: 10px; align-items: center; padding: 7px 9px; border-radius: 10px; background: var(--panel); border: 1px solid var(--line); opacity: 0.5; }
.ach.on { opacity: 1; border-color: rgba(251,191,36,0.45); }
.ach i { font-style: normal; font-size: 1.3rem; width: 28px; text-align: center; }
.ach b { display: block; font-size: 0.85rem; }
.ach span { display: block; font-size: 0.72rem; color: var(--dim); line-height: 1.3; }
.ach-new { margin-top: 10px; display: grid; gap: 6px; }
.ach-new div { display: grid; grid-template-columns: 30px 1fr; column-gap: 8px; align-items: center; padding: 8px 10px; border-radius: 10px; background: rgba(251,191,36,0.12); border: 1px solid rgba(251,191,36,0.5); animation: achIn 0.5s ease both; }
.ach-new i { font-style: normal; font-size: 1.4rem; grid-row: span 2; text-align: center; }
.ach-new b { color: var(--gold); font-size: 0.88rem; }
.ach-new span { color: var(--dim); font-size: 0.75rem; }
@keyframes achIn { from { transform: scale(0.9); opacity: 0; } to { transform: none; opacity: 1; } }
