:root {
  --bg: #0a0a0f;
  --bg-card: #0d0d18;
  --fg: #e2e8f0;
  --muted: #7c8496;
  --border: rgba(148, 163, 184, 0.16);
  --accent: #8b5cf6;
  --accent-bright: #c084fc;
  --accent-soft: rgba(139, 92, 246, 0.14);
  --pink: #ec4899;
  --danger: #ef4444;
  --success: #10b981;
  --radius: 18px;
  --font-display: "Sora", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-display);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
}

#bg-canvas {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

body.no-scroll { overflow: hidden; }

::selection { background: var(--accent); color: #fff; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #6d28d9; border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

.app {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Splash screen ---------- */
.splash {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 30%, #14141f, var(--bg) 70%);
  cursor: pointer;
  transition: opacity 0.6s ease, visibility 0.6s ease;
}

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

.splash-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 20px;
  max-width: 380px;
}

.splash-icon {
  font-size: 3.6rem;
  display: inline-block;
  animation: splashFlip 1.8s ease-in-out infinite;
  filter: drop-shadow(0 0 24px rgba(139, 92, 246, 0.5));
}

@keyframes splashFlip {
  0%, 100% { transform: rotateY(0deg); }
  50% { transform: rotateY(180deg); }
}

.splash-title {
  margin: 14px 0 6px;
  font-size: 2.4rem;
  font-weight: 800;
  background: linear-gradient(100deg, var(--accent-bright), var(--pink) 55%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  opacity: 0;
  animation: fadeUp 0.6s ease forwards;
  animation-delay: 0.1s;
}

.splash-tagline {
  margin: 0 0 20px;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  opacity: 0;
  animation: fadeUp 0.6s ease forwards;
  animation-delay: 0.22s;
}

.splash-tagline .dots span {
  animation: dotBlink 1.4s infinite;
  opacity: 0;
}
.splash-tagline .dots span:nth-child(2) { animation-delay: 0.2s; }
.splash-tagline .dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotBlink {
  0%, 20% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 0; }
}

.splash-bar {
  width: 180px;
  height: 4px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
  opacity: 0;
  animation: fadeUp 0.6s ease forwards;
  animation-delay: 0.3s;
}

.splash-bar-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), var(--accent-bright));
  animation: splashLoad 2.2s ease forwards 0.4s;
}

@keyframes splashLoad {
  to { width: 100%; }
}

.splash-credit {
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  width: 100%;
  opacity: 0;
  animation: fadeUp 0.6s ease forwards;
  animation-delay: 0.5s;
}

.splash-by {
  margin: 0 0 8px;
  font-size: 0.88rem;
  color: var(--muted);
}

.splash-by strong { color: var(--fg); }

.splash-links {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.splash-links a {
  color: var(--accent-bright);
  text-decoration: none;
  transition: color 0.2s ease;
}

.splash-links a:hover { color: var(--pink); text-decoration: underline; }
.splash-links .dot { color: var(--border); }

.splash-skip {
  margin: 26px 0 0;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  opacity: 0;
  animation: splashSkipIn 0.6s ease forwards 1.4s;
}

@keyframes splashSkipIn {
  from { opacity: 0; }
  to { opacity: 0.5; }
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px clamp(16px, 5vw, 48px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.02em;
}

.brand-icon {
  font-size: 1.4rem;
  display: inline-block;
  animation: brandPulse 4s ease-in-out infinite;
}

@keyframes brandPulse {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(1.12) rotate(-6deg); }
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn, .lang-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  transition: border-color 0.2s ease, transform 0.15s ease, background 0.2s ease;
}

.icon-btn { padding: 8px 10px; }

.icon-btn:hover, .lang-btn:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: translateY(-1px);
}

.icon-btn:active, .lang-btn:active { transform: translateY(0); }

.flag { font-size: 1rem; }

/* ---------- Stage / Hero ---------- */
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 20px 40px;
  gap: 34px;
}

.hero {
  text-align: center;
  max-width: 620px;
  margin-top: clamp(6px, 3vh, 24px);
}

.eyebrow {
  font-family: var(--font-mono);
  color: var(--accent-bright);
  letter-spacing: 0.25em;
  font-size: 0.72rem;
  margin: 0 0 10px;
  text-transform: uppercase;
  opacity: 0;
  animation: fadeUp 0.6s ease forwards;
}

.title {
  margin: 0 0 14px;
  font-size: clamp(2.1rem, 6vw, 3.4rem);
  line-height: 1.08;
  font-weight: 800;
}

.title-line {
  display: block;
  opacity: 0;
  transform: translateY(16px);
  animation: fadeUp 0.7s ease forwards;
}

.title-line.accent {
  background: linear-gradient(100deg, var(--accent-bright), var(--pink) 55%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation-delay: 0.12s;
}

.tagline {
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.6;
  margin: 0 auto;
  max-width: 520px;
  opacity: 0;
  animation: fadeUp 0.7s ease forwards;
  animation-delay: 0.24s;
}

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

/* ---------- Difficulty picker ---------- */
.difficulty-picker {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  opacity: 0;
  animation: fadeUp 0.7s ease forwards;
  animation-delay: 0.32s;
}

.diff-btn {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 14px;
  padding: 10px 18px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: all 0.2s ease;
  font-family: var(--font-display);
}

.diff-btn:hover { border-color: var(--accent); transform: translateY(-2px); }

.diff-btn.is-active {
  border-color: var(--accent);
  background: linear-gradient(180deg, var(--accent-soft), rgba(139, 92, 246, 0.04));
  box-shadow: 0 0 0 1px var(--accent-soft), 0 8px 24px -8px rgba(139, 92, 246, 0.5);
}

.diff-name { font-weight: 700; font-size: 0.95rem; }
.diff-meta { font-family: var(--font-mono); font-size: 0.72rem; color: var(--muted); }
.diff-btn.is-active .diff-meta { color: var(--accent-bright); }

/* ---------- Game Card ---------- */
.game-card {
  position: relative;
  width: min(620px, 94vw);
  background: linear-gradient(180deg, rgba(255,255,255,0.035), rgba(255,255,255,0.01));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px clamp(14px, 4vw, 30px) 22px;
  backdrop-filter: blur(14px);
  box-shadow: 0 20px 60px -20px rgba(0,0,0,0.6);
  opacity: 0;
  animation: cardIn 0.7s cubic-bezier(.16,1,.3,1) forwards;
  animation-delay: 0.4s;
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(24px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.game-card-glow {
  position: absolute;
  inset: -1px;
  border-radius: inherit;
  background: radial-gradient(600px circle at var(--mx, 50%) var(--my, 0%), rgba(139,92,246,0.12), transparent 60%);
  pointer-events: none;
  z-index: -1;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 18px;
}

.stat { display: flex; flex-direction: column; gap: 6px; }
.stat.center { align-items: center; }
.stat.right { align-items: flex-end; }

.stat-label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1.05rem;
}

/* ---------- Board / Cards ---------- */
.board-wrap { position: relative; margin-bottom: 8px; }

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), 1fr);
  gap: clamp(6px, 1.6vw, 12px);
}

.card {
  aspect-ratio: 1;
  perspective: 900px;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.card:hover:not(.flipped):not(.matched) { transform: translateY(-3px); }

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(.2,.8,.2,1);
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card.mismatch .card-inner { animation: cardShake 0.45s ease; }

@keyframes cardShake {
  10%, 90% { transform: rotateY(180deg) translateX(-2px); }
  20%, 80% { transform: rotateY(180deg) translateX(3px); }
  30%, 50%, 70% { transform: rotateY(180deg) translateX(-6px); }
  40%, 60% { transform: rotateY(180deg) translateX(6px); }
}

.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: clamp(8px, 1.6vw, 12px);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.3rem, 4.4vw, 2.1rem);
  border: 1px solid var(--border);
  overflow: hidden;
}

.card-face--back {
  background: linear-gradient(135deg, rgba(139,92,246,0.22), rgba(236,72,153,0.08));
  color: rgba(226, 232, 240, 0.28);
}

.card-face--back::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(115deg, transparent 30%, rgba(255,255,255,0.14) 50%, transparent 70%);
  background-size: 250% 250%;
  animation: cardShine 3.6s ease-in-out infinite;
}

@keyframes cardShine {
  0% { background-position: 200% 0%; }
  100% { background-position: -50% 0%; }
}

.card-face--front {
  background: linear-gradient(150deg, rgba(255,255,255,0.06), rgba(255,255,255,0.015));
  transform: rotateY(180deg);
}

.card.matched .card-face--front {
  border-color: rgba(16, 185, 129, 0.55);
  background: rgba(16, 185, 129, 0.1);
  animation: matchPop 0.45s cubic-bezier(.34,1.56,.64,1);
}

@keyframes matchPop {
  0% { transform: rotateY(180deg) scale(1); }
  55% { transform: rotateY(180deg) scale(1.12); }
  100% { transform: rotateY(180deg) scale(1); }
}

.card.matched { cursor: default; }

.peek-msg {
  position: absolute;
  left: 50%;
  bottom: -30px;
  transform: translateX(-50%);
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.74rem;
  letter-spacing: 0.05em;
  color: var(--accent-bright);
  opacity: 0;
  transition: opacity 0.25s ease;
  white-space: nowrap;
}

.peek-msg.is-visible { opacity: 0.85; }

/* ---------- Feedback / actions ---------- */
.feedback {
  min-height: 1.4em;
  text-align: center;
  font-size: 0.92rem;
  font-weight: 600;
  margin: 30px 0 6px;
  transition: color 0.2s ease;
}

.feedback.match { color: var(--success); }
.feedback.mismatch { color: var(--danger); }

.game-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--muted);
}

.best-line strong { color: var(--fg); }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  transition: all 0.2s ease;
}

.ghost-btn:hover { color: var(--accent-bright); border-color: var(--accent); }

.primary-btn {
  background: linear-gradient(135deg, var(--accent-bright), var(--accent));
  color: #0a0a0f;
  border: none;
  font-weight: 700;
  font-family: var(--font-display);
  padding: 0 24px;
  height: 44px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 0.98rem;
  transition: transform 0.15s ease, box-shadow 0.2s ease, filter 0.2s ease;
  box-shadow: 0 8px 20px -8px rgba(139, 92, 246, 0.7);
}

.primary-btn:hover { transform: translateY(-2px); filter: brightness(1.08); }
.primary-btn:active { transform: translateY(0); }

.hint-note {
  color: var(--muted);
  font-size: 0.78rem;
  opacity: 0.6;
  text-align: center;
  font-family: var(--font-mono);
}

/* ---------- Footer ---------- */
.site-footer {
  text-align: center;
  padding: 24px 20px 30px;
  color: var(--muted);
  font-size: 0.85rem;
}

.site-footer p { margin: 0 0 8px; }
.site-footer strong { color: var(--fg); }

.footer-links {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 0.78rem;
}

.footer-links a {
  color: var(--accent-bright);
  text-decoration: none;
  transition: color 0.2s ease;
}

.footer-links a:hover { color: var(--pink); text-decoration: underline; }
.footer-links .dot { color: var(--border); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(5, 5, 10, 0.75);
  backdrop-filter: blur(6px);
  animation: fadeIn 0.25s ease;
}

.modal-overlay.is-open { display: flex; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

#confetti-canvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.modal {
  position: relative;
  width: min(420px, 88vw);
  background: linear-gradient(180deg, #14141f, #0d0d18);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 26px 26px;
  text-align: center;
  box-shadow: 0 30px 80px -20px rgba(0,0,0,0.7);
  animation: modalIn 0.4s cubic-bezier(.16,1,.3,1);
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(30px) scale(0.94); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-icon {
  font-size: 2.6rem;
  display: block;
  margin-bottom: 10px;
  animation: pop 0.5s cubic-bezier(.34,1.56,.64,1);
}

@keyframes pop {
  0% { transform: scale(0); }
  70% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.modal-title {
  font-size: 1.5rem;
  margin: 0 0 8px;
  font-weight: 800;
}

.modal-text {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
  margin: 0 0 16px;
}

.modal-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin-bottom: 22px;
  font-family: var(--font-mono);
}

.modal-stats .m-stat { display: flex; flex-direction: column; gap: 2px; }
.modal-stats .m-stat-value { font-weight: 800; font-size: 1.2rem; color: var(--accent-bright); }
.modal-stats .m-stat-label { font-size: 0.65rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  .game-actions { flex-direction: column; gap: 10px; }
  .modal-stats { gap: 12px; }
}
