:root {
  --bg: #0b0d12;
  --bg-soft: #11141c;
  --panel: #151a24;
  --panel-2: #1b2130;
  --border: #283044;
  --text: #e8eef7;
  --muted: #8b95a8;
  --accent: #ff5e1a;
  --accent-2: #ffb648;
  --glow: rgba(255, 94, 26, 0.32);
  --green: #22c55e;
  --cyan: #38bdf8;
  --purple: #a855f7;
  --red: #ef4444;
  --gold: #f5b041;
  --radius: 16px;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background:
    radial-gradient(900px 500px at 12% -10%, rgba(255, 94, 26, 0.14), transparent 60%),
    radial-gradient(800px 500px at 95% 0%, rgba(56, 189, 248, 0.1), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font-family: inherit;
}

a {
  color: inherit;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 18px;
  background: rgba(17, 20, 28, 0.85);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 40;
  flex-wrap: wrap;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 7px 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel);
  transition: 0.15s;
  white-space: nowrap;
}

.back-link:hover {
  color: var(--text);
  border-color: var(--accent);
  transform: translateX(-2px);
}

.game-title {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.game-title .emoji {
  font-size: 1.3rem;
}

.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 7px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.stat b {
  color: var(--text);
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

.stat.accent b {
  color: var(--accent-2);
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 0.84rem;
  font-weight: 700;
  cursor: pointer;
  transition: 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  white-space: nowrap;
}

.btn:hover {
  border-color: var(--accent);
  background: #232b3d;
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn.primary {
  background: linear-gradient(135deg, var(--accent), #ff8a3d);
  border-color: transparent;
  color: #1a0d05;
  box-shadow: 0 8px 24px -10px var(--glow);
}

.btn.primary:hover {
  filter: brightness(1.08);
  background: linear-gradient(135deg, var(--accent), #ff8a3d);
}

.btn.ghost {
  background: transparent;
}

.btn.small {
  padding: 6px 10px;
  font-size: 0.76rem;
  border-radius: 8px;
}

.stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px 16px 30px;
  width: 100%;
}

.stage.col {
  flex-direction: column;
  gap: 16px;
}

.game-card {
  background: linear-gradient(180deg, rgba(27, 33, 48, 0.9), rgba(17, 20, 28, 0.92));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 30px 70px -40px rgba(0, 0, 0, 0.9);
}

.canvas-wrap {
  position: relative;
  display: inline-flex;
  border-radius: calc(var(--radius) - 4px);
  overflow: hidden;
  border: 1px solid var(--border);
  background: #080a0f;
  touch-action: none;
  max-width: 100%;
}

canvas {
  display: block;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  padding: 22px;
  background: rgba(8, 10, 15, 0.78);
  backdrop-filter: blur(4px);
  z-index: 10;
  opacity: 1;
  visibility: visible;
  transition: opacity 0.22s var(--ease), visibility 0.22s;
}

.overlay.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.overlay h2 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.4px;
}

.overlay p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  max-width: 36ch;
  line-height: 1.5;
}

.overlay .big {
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent-2);
}

.hint {
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
  line-height: 1.6;
}

.hint kbd {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 0.72rem;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 620px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
}

.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  overflow: hidden;
}

.seg button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 7px 11px;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  transition: 0.15s;
}

.seg button.active {
  background: var(--accent);
  color: #1a0d05;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- shared motion ---------- */
.topbar {
  animation: barDrop 0.5s var(--ease) backwards;
}

.stage {
  animation: stageIn 0.55s var(--ease) backwards;
  animation-delay: 0.05s;
}

.game-card {
  animation: cardRise 0.6s var(--ease) backwards;
  animation-delay: 0.08s;
}

.canvas-wrap {
  animation: frameGlow 5.5s ease-in-out infinite;
}

.hint {
  animation: fadeUp 0.6s var(--ease) backwards;
  animation-delay: 0.18s;
}

.stat {
  animation: fadeUp 0.45s var(--ease) backwards;
}

.stat:nth-child(2) { animation-delay: 0.04s; }
.stat:nth-child(3) { animation-delay: 0.08s; }
.stat:nth-child(4) { animation-delay: 0.12s; }
.stat:nth-child(5) { animation-delay: 0.16s; }

.overlay:not(.hidden) h2 {
  animation: overlayPop 0.34s var(--ease-spring) backwards;
}

.overlay:not(.hidden) .big {
  animation: overlayPop 0.42s var(--ease-spring) backwards;
  animation-delay: 0.05s;
}

.overlay:not(.hidden) p {
  animation: fadeUp 0.42s var(--ease) backwards;
  animation-delay: 0.08s;
}

.overlay:not(.hidden) .row {
  animation: fadeUp 0.45s var(--ease) backwards;
  animation-delay: 0.14s;
}

.btn {
  transition: transform 0.16s var(--ease), border-color 0.16s var(--ease), background 0.16s var(--ease),
    box-shadow 0.16s var(--ease), filter 0.16s var(--ease);
}

.btn:active {
  transform: scale(0.96);
}

.back-link:active {
  transform: scale(0.96);
}

@keyframes barDrop {
  from { transform: translateY(-100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes stageIn {
  from { transform: translateY(16px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes cardRise {
  from { transform: translateY(22px) scale(0.97); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes frameGlow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 94, 26, 0); }
  50% { box-shadow: 0 0 40px -14px rgba(255, 94, 26, 0.5); }
}

@keyframes overlayPop {
  from { transform: translateY(14px) scale(0.9); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes fadeUp {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 720px) {
  .topbar {
    padding: 9px 12px;
    gap: 10px;
  }
  .game-title {
    font-size: 0.95rem;
  }
  .stat {
    padding: 5px 9px;
    font-size: 0.72rem;
  }
  .stat b {
    font-size: 0.86rem;
  }
  .stage {
    padding: 14px 10px 24px;
  }
  .game-card {
    padding: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
