:root {
  --bg: #06070b;
  --ink: #eef0f7;
  --ink-2: #a4a9bd;
  --ink-3: #6d7288;
  --line: rgba(255, 255, 255, .08);
  --line-2: rgba(255, 255, 255, .14);
  --panel: rgba(14, 16, 26, .6);
  --up: #ffb86b;
  --down: #a193ff;
  --hit: #5cf2c4;
  --lose: #ff6b8b;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --spring: cubic-bezier(.3, 1.5, .5, 1);
  color-scheme: dark;
}
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: 'Pretendard Variable', Pretendard, -apple-system, BlinkMacSystemFont, system-ui, 'Apple SD Gothic Neo', sans-serif;
  letter-spacing: -0.011em; word-break: keep-all;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  display: flex; flex-direction: column;
  height: 100svh;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; touch-action: manipulation; }
a { color: inherit; text-decoration: none; }
svg { display: block; }
:focus-visible { outline: 2px solid var(--down); outline-offset: 3px; border-radius: 10px; }

.atmos {
  position: fixed; inset: -10%; z-index: -1; pointer-events: none;
  background:
    radial-gradient(40% 40% at 28% 45%, rgba(120, 100, 255, .16), transparent 70%),
    radial-gradient(35% 30% at 80% 90%, rgba(60, 220, 180, .06), transparent 70%),
    radial-gradient(60% 50% at 50% 0%, rgba(40, 50, 110, .25), transparent 70%),
    var(--bg);
}
.grain {
  position: fixed; inset: 0; z-index: 50; pointer-events: none; opacity: .06; mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---------- Bar ---------- */
.bar {
  position: relative; z-index: 5;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) clamp(14px, 3vw, 28px) 12px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.02em; justify-self: start; }
.brand svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.6; }
.brand svg circle { fill: currentColor; stroke: none; }
.modes {
  display: flex; padding: 4px; gap: 2px; border-radius: 999px;
  background: rgba(255, 255, 255, .04); border: 1px solid var(--line);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
.mode { height: 36px; padding: 0 16px; border-radius: 999px; font-size: 14px; font-weight: 600; color: var(--ink-2); transition: color .3s, background .3s; }
.mode:hover { color: var(--ink); }
.mode[aria-selected="true"] { background: var(--ink); color: #0b0c12; }
.bar-actions { display: flex; gap: 6px; justify-self: end; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  border: 1px solid var(--line); background: rgba(255, 255, 255, .03); color: var(--ink-2);
  transition: color .3s, background .3s;
}
.icon-btn:hover { color: var(--ink); background: rgba(255, 255, 255, .07); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
#sound .off { display: none; }
#sound[aria-pressed="false"] .on { display: none; }
#sound[aria-pressed="false"] .off { display: block; }

/* ---------- Layout ---------- */
.game {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 1fr minmax(360px, 440px);
  gap: clamp(8px, 2vw, 28px);
  padding: 0 clamp(14px, 3vw, 28px) calc(16px + env(safe-area-inset-bottom));
  max-width: 1280px; width: 100%; margin: 0 auto;
}
.stage { position: relative; min-height: 0; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 1.4s var(--ease); }
.is-ready #gl { opacity: 1; }
.gate-label {
  position: absolute; left: 0; top: 0; pointer-events: none;
  font: 600 13px/1 ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, monospace;
  padding: 5px 8px; border-radius: 8px; border: 1px solid var(--line-2);
  background: rgba(10, 11, 18, .7); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  will-change: transform;
}
.gate-label.lo { color: var(--up); }
.gate-label.hi { color: var(--down); }

.panel {
  position: relative; align-self: center;
  display: flex; flex-direction: column; gap: 14px;
  padding: 22px; border-radius: 28px;
  background: linear-gradient(180deg, rgba(22, 24, 36, .66), rgba(10, 11, 18, .62));
  border: 1px solid var(--line);
  backdrop-filter: blur(20px) saturate(1.3); -webkit-backdrop-filter: blur(20px) saturate(1.3);
  box-shadow: 0 30px 80px -30px rgba(0, 0, 0, .7);
}

/* ---------- Status ---------- */
.mode-desc { margin: 0; font-size: 13.5px; color: var(--ink-3); }
.status-row { display: flex; align-items: flex-end; justify-content: space-between; margin-top: 6px; }
.range { display: flex; align-items: baseline; gap: 6px; font-variant-numeric: tabular-nums; }
.range b { font-size: 26px; font-weight: 700; letter-spacing: -0.03em; }
.range b:first-child { color: var(--up); }
.range b:nth-of-type(2) { color: var(--down); }
.range span { color: var(--ink-3); }
.range em { font-style: normal; font-size: 13px; color: var(--ink-3); margin-left: 2px; }
[data-state="over"] .range b { color: var(--hit); }
.tries { display: flex; flex-direction: column; align-items: flex-end; }
.tries span { font-size: 12px; color: var(--ink-3); }
.tries b { font-size: 18px; font-variant-numeric: tabular-nums; }
.pips { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; margin-top: 10px; }
.pips i { height: 5px; border-radius: 3px; background: rgba(255, 255, 255, .08); transition: background .4s; }
.pips i.up { background: var(--up); }
.pips i.down { background: var(--down); }
.pips i.hit { background: var(--hit); }

/* ---------- Display ---------- */
.display {
  position: relative; height: 150px; border-radius: 22px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(80% 100% at 50% 0%, rgba(255, 255, 255, .05), transparent 70%), rgba(0, 0, 0, .25);
  border: 1px solid var(--line); overflow: hidden;
}
.display.is-shake { animation: shake .4s var(--ease); }
@keyframes shake { 20% { transform: translateX(-8px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(2px); } }
.entry {
  font-size: 68px; font-weight: 750; letter-spacing: -0.05em; line-height: 1;
  font-variant-numeric: tabular-nums; transition: color .2s;
}
.entry.is-empty { color: rgba(255, 255, 255, .14); }
.verdict {
  position: absolute; top: 14px; left: 0; right: 0; text-align: center;
  font-size: 13px; font-weight: 800; letter-spacing: .24em; opacity: 0;
}
.verdict.is-on { animation: verdict .7s var(--spring) forwards; }
@keyframes verdict { 0% { opacity: 0; transform: translateY(8px) scale(.7); } 100% { opacity: 1; transform: none; } }
.verdict.is-ready { color: var(--ink-3); letter-spacing: .12em; }
.verdict.is-up { color: var(--up); text-shadow: 0 0 24px rgba(255, 184, 107, .6); }
.verdict.is-down { color: var(--down); text-shadow: 0 0 24px rgba(161, 147, 255, .6); }
.verdict.is-hit { color: var(--hit); text-shadow: 0 0 24px rgba(92, 242, 196, .6); }
.verdict.is-lost { color: var(--lose); }
.verdict.is-up::before { content: '▲ '; }
.verdict.is-down::before { content: '▼ '; }
.heat { position: absolute; bottom: 12px; margin: 0; font-size: 13.5px; color: var(--ink-2); min-height: 1em; }

/* ---------- History ---------- */
.history {
  list-style: none; margin: 0; padding: 2px 0; display: flex; gap: 6px; overflow-x: auto; min-height: 34px;
  scrollbar-width: none; mask-image: linear-gradient(90deg, #000 85%, transparent);
}
.history::-webkit-scrollbar { display: none; }
.history:empty::before { content: '부른 숫자가 여기에 쌓여요'; font-size: 13px; color: var(--ink-3); align-self: center; }
.history li {
  flex: none; display: inline-flex; align-items: center; gap: 4px; height: 30px; padding: 0 10px; border-radius: 999px;
  font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; border: 1px solid var(--line);
  animation: pop .5s var(--spring);
}
.history li:first-child { border-color: var(--line-2); }
.history li i { font-style: normal; font-size: 12px; }
.history li.up i { color: var(--up); }
.history li.down i { color: var(--down); }
.history li.hit { color: #0b0c12; background: var(--hit); border-color: var(--hit); }
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 100% { transform: none; opacity: 1; } }

/* ---------- Keypad ---------- */
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button {
  height: 58px; border-radius: 16px; font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums;
  background: rgba(255, 255, 255, .05); border: 1px solid var(--line);
  transition: background .2s, transform .2s var(--ease);
  user-select: none; -webkit-user-select: none;
}
.keypad button:hover { background: rgba(255, 255, 255, .09); }
.keypad button:active, .keypad button.is-hit { animation: key .28s var(--ease); }
@keyframes key { 40% { transform: scale(.92); background: rgba(255, 255, 255, .16); } }
.keypad .k-fn { display: grid; place-items: center; color: var(--ink-2); }
.keypad .k-fn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.keypad .k-go {
  font-size: 18px; font-weight: 700; color: #0b0c12; background: var(--ink); border-color: var(--ink);
  box-shadow: 0 10px 30px -10px rgba(161, 147, 255, .7);
}
.keypad .k-go:hover { background: #fff; }

/* ---------- Result ---------- */
.result { display: none; }
[data-state="over"] .play { display: none; }
[data-state="over"] .result { display: block; animation: rise .8s var(--ease); }
@keyframes rise { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: none; } }
.r-kicker { margin: 4px 0 6px; font: 500 12.5px ui-monospace, 'SF Mono', Menlo, monospace; color: var(--ink-3); letter-spacing: .06em; }
.r-title { margin: 0; font-size: 34px; letter-spacing: -0.04em; line-height: 1.15; }
.r-sub { margin: 8px 0 0; color: var(--ink-2); }
.r-sub b { color: var(--hit); font-size: 18px; }
.r-marks { list-style: none; display: flex; flex-wrap: wrap; gap: 5px; padding: 0; margin: 18px 0 0; }
.r-marks li { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; font-size: 14px; font-weight: 700; }
.r-marks .up { background: rgba(255, 184, 107, .16); color: var(--up); }
.r-marks .down { background: rgba(161, 147, 255, .18); color: var(--down); }
.r-marks .hit { background: var(--hit); color: #0b0c12; }
.r-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 20px 0 0; }
.r-stats div, .stats dl div { display: flex; flex-direction: column-reverse; padding: 12px; border-radius: 14px; background: rgba(255, 255, 255, .04); border: 1px solid var(--line); }
.r-stats dt, .stats dt { font-size: 12px; color: var(--ink-3); }
.r-stats dd, .stats dd { margin: 0; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.next { margin: 16px 0 0; font-size: 14px; color: var(--ink-2); }
.next b { font-variant-numeric: tabular-nums; color: var(--ink); }
.r-actions { display: flex; gap: 8px; margin-top: 20px; }
.r-actions .btn { flex: 1; justify-content: center; }

.btn {
  display: inline-flex; align-items: center; gap: 8px; height: 50px; padding: 0 22px; border-radius: 999px;
  font-weight: 650; font-size: 15px; transition: background .3s, border-color .3s, transform .3s var(--ease);
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--ink); color: #0b0c12; box-shadow: 0 10px 34px -10px rgba(161, 147, 255, .65); }
.btn-ghost { border: 1px solid var(--line-2); background: rgba(255, 255, 255, .03); }
.btn-ghost:hover { background: rgba(255, 255, 255, .08); }

/* ---------- Toast / dialogs ---------- */
.toast {
  position: fixed; left: 50%; top: calc(76px + env(safe-area-inset-top)); z-index: 60;
  translate: -50% 0; padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  background: rgba(20, 22, 34, .92); border: 1px solid var(--line-2); backdrop-filter: blur(12px);
  opacity: 0; pointer-events: none;
}
.toast.is-on { animation: toast 1.8s var(--ease); }
@keyframes toast { 0% { opacity: 0; transform: translateY(-10px); } 12%, 80% { opacity: 1; transform: none; } 100% { opacity: 0; } }

dialog {
  width: min(440px, calc(100% - 28px)); padding: 0; border: 1px solid var(--line-2); border-radius: 26px;
  background: rgba(14, 16, 26, .92); color: var(--ink);
  backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
  box-shadow: 0 40px 120px -20px rgba(0, 0, 0, .8);
}
dialog[open] { animation: rise .5s var(--ease); }
dialog::backdrop { background: rgba(3, 4, 8, .6); backdrop-filter: blur(4px); }
.dlg { padding: 28px; display: flex; flex-direction: column; gap: 18px; }
.dlg h2 { margin: 0; font-size: 24px; letter-spacing: -0.03em; }
.dlg .btn { justify-content: center; }
.how { margin: 0; padding: 0 0 0 20px; display: flex; flex-direction: column; gap: 10px; color: var(--ink-2); font-size: 15px; line-height: 1.55; }
.how b { color: var(--ink); }
.c-up { color: var(--up) !important; }
.c-down { color: var(--down) !important; }
.how-modes { list-style: none; margin: 0; padding: 14px; border-radius: 16px; background: rgba(255, 255, 255, .04); display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--ink-2); }
.how-modes b { display: inline-block; width: 52px; color: var(--ink); }
.stats { display: flex; flex-direction: column; gap: 16px; }
.stats h3 { margin: 0 0 8px; font-size: 15px; }
.stats dl { margin: 0; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stats dd { font-size: 19px; }
.dlg-note { margin: 0; font-size: 13px; color: var(--ink-3); }

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  .bar { grid-template-columns: auto 1fr auto; padding-bottom: 6px; }
  .brand span { display: none; }
  .modes { justify-self: center; }
  .mode { padding: 0 12px; font-size: 13.5px; height: 34px; }
  .icon-btn { width: 36px; height: 36px; border-radius: 11px; }
  .bar-actions #open-help { display: none; }
  .game { grid-template-columns: 1fr; grid-template-rows: minmax(120px, 1fr) auto; gap: 0; padding: 0 12px calc(10px + env(safe-area-inset-bottom)); }
  .panel { align-self: end; padding: 14px; gap: 10px; border-radius: 24px; }
  .display { height: 112px; border-radius: 18px; }
  .entry { font-size: 54px; }
  .verdict { top: 10px; }
  .heat { bottom: 8px; font-size: 12.5px; }
  .range b { font-size: 22px; }
  .keypad { gap: 6px; }
  .keypad button { height: 52px; border-radius: 14px; font-size: 22px; }
  .r-title { font-size: 28px; }
  .r-stats dd { font-size: 19px; }
}
@media (max-width: 820px) and (max-height: 700px) {
  .keypad button { height: 44px; }
  .display { height: 92px; }
  .entry { font-size: 44px; }
  .mode-desc { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
