:root {
  --bg: #0b0614;
  --bg-elev: #120a1c;
  --bg-card: #161022;
  --bg-card-hover: #1e1530;
  --border: #2a1f3d;
  --border-strong: #3d2d5c;
  --text: #f3eefc;
  --muted: #a89bc0;
  --faint: #6f6288;
  --accent: #c026d3;
  --accent-2: #a21caf;
  --accent-3: #e879f9;
  --accent-soft: rgba(192, 38, 211, 0.15);
  --accent-glow: rgba(192, 38, 211, 0.35);
  --success: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-tap-highlight-color: transparent; }
body {
  min-height: 100dvh;
  font-family: var(--font);
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(162, 28, 175, 0.35), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(88, 28, 135, 0.25), transparent),
    var(--bg);
  color: var(--text);
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent-3); text-decoration: none; }
a:hover { text-decoration: underline; }
img, svg { display: block; max-width: 100%; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.wrap {
  width: min(720px, 100% - 2rem);
  margin: 0 auto;
  padding: 1.25rem 0 calc(2rem + var(--safe-b));
  flex: 1;
}
.wrap-wide { width: min(960px, 100% - 2rem); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 0 1.25rem;
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(180deg, var(--bg) 60%, transparent);
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, #e879f9, #c026d3 55%, #7c3aed);
  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 8px 20px var(--accent-glow);
  display: grid; place-items: center;
  font-size: 0.85rem; font-weight: 800; color: #fff;
}
.brand-sub { color: var(--muted); font-weight: 500; font-size: 0.85rem; margin-left: 0.15rem; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  display: grid; place-items: center;
  color: var(--text);
}
.icon-btn:hover { border-color: var(--border-strong); background: var(--bg-card-hover); }

.badge {
  display: inline-flex; align-items: center; gap: 0.35rem;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--accent-3);
  background: var(--accent-soft);
  border: 1px solid rgba(192, 38, 211, 0.35);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
}
h1 {
  font-size: clamp(1.75rem, 5vw, 2.4rem);
  font-weight: 750; letter-spacing: -0.03em; line-height: 1.15;
}
h1 .grad {
  background: linear-gradient(135deg, #e879f9, #c026d3 55%, #9333ea);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.lead { color: var(--muted); font-size: 1.02rem; max-width: 36rem; }
.hero { text-align: center; padding: 1rem 0 1.75rem; display: grid; gap: 0.85rem; justify-items: center; }

.cat-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.9rem;
}
@media (min-width: 640px) {
  .cat-grid { grid-template-columns: repeat(3, 1fr); }
}
.cat-card, .game-card {
  display: flex; flex-direction: column; gap: 0.65rem;
  padding: 1.15rem 1.15rem 1.2rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: linear-gradient(180deg, var(--bg-card), var(--bg-elev));
  box-shadow: var(--shadow);
  color: var(--text);
  text-decoration: none;
  transition: transform .15s ease, border-color .15s ease, background .15s ease;
}
.cat-card:hover, .game-card:hover {
  transform: translateY(-2px);
  border-color: rgba(192, 38, 211, 0.45);
  background: linear-gradient(180deg, var(--bg-card-hover), var(--bg-card));
  text-decoration: none;
}
.cat-icon {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center; font-size: 1.35rem;
  background: var(--accent-soft); border: 1px solid rgba(192,38,211,.25);
}
.cat-card h2, .game-card h3 { font-size: 1.1rem; letter-spacing: -0.02em; }
.cat-card p, .game-card p { color: var(--muted); font-size: 0.92rem; }
.meta-row { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: auto; padding-top: 0.35rem; }
.chip {
  font-size: 0.72rem; font-weight: 600; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px; padding: 0.18rem 0.55rem;
  background: rgba(255,255,255,0.02);
}
.chip.accent { color: var(--accent-3); border-color: rgba(192,38,211,.4); background: var(--accent-soft); }

.site-footer {
  text-align: center; color: var(--faint); font-size: 0.85rem;
  padding: 1.5rem 1rem calc(1.25rem + var(--safe-b));
}
.site-footer strong { color: var(--muted); font-weight: 600; }

.filters {
  display: flex; gap: 0.45rem; overflow-x: auto; padding-bottom: 0.25rem;
  margin: 0.25rem 0 1rem; -webkit-overflow-scrolling: touch;
}
.filter-btn {
  flex: 0 0 auto;
  padding: 0.45rem 0.9rem; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-card);
  color: var(--muted); font-weight: 600; font-size: 0.88rem;
}
.filter-btn.active, .filter-btn[aria-pressed="true"] {
  color: #fff; border-color: transparent;
  background: linear-gradient(135deg, #c026d3, #7c3aed);
}
.game-grid {
  display: grid; grid-template-columns: 1fr; gap: 0.85rem;
}
@media (min-width: 560px) { .game-grid { grid-template-columns: 1fr 1fr; } }

.panel {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg-card); padding: 1.15rem; box-shadow: var(--shadow);
}
.panel + .panel { margin-top: 0.9rem; }
.panel h2 { font-size: 1.05rem; margin-bottom: 0.65rem; }
.stack { display: grid; gap: 0.75rem; }
.row { display: flex; gap: 0.65rem; align-items: center; flex-wrap: wrap; }
.muted { color: var(--muted); }
.faint { color: var(--faint); font-size: 0.85rem; }
.status {
  min-height: 1.35rem; font-size: 0.9rem; color: var(--muted);
}
.status.ok { color: var(--success); }
.status.err { color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.7rem 1.1rem; border-radius: 12px; font-weight: 650;
  border: 1px solid transparent; transition: transform .12s ease, opacity .12s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary {
  color: #fff;
  background: linear-gradient(135deg, #e879f9, #c026d3 50%, #7c3aed);
  box-shadow: 0 8px 24px var(--accent-glow);
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-ghost {
  color: var(--text); background: transparent; border-color: var(--border);
}
.btn-ghost:hover { background: var(--bg-card-hover); }
.btn-block { width: 100%; }
.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0.35rem;
  padding: 0.3rem; border-radius: 12px; background: var(--bg-elev); border: 1px solid var(--border);
}
.seg button {
  padding: 0.55rem 0.5rem; border-radius: 9px; font-weight: 650; color: var(--muted);
}
.seg button.active {
  color: #fff; background: linear-gradient(135deg, #c026d3, #7c3aed);
}

.code-display {
  font-variant-numeric: tabular-nums;
  font-size: clamp(2rem, 8vw, 2.75rem);
  font-weight: 750; letter-spacing: 0.18em; text-align: center;
  padding: 0.6rem 0.4rem;
  background: var(--bg-elev); border-radius: var(--radius-sm);
  border: 1px dashed var(--border-strong);
}
.code-input {
  width: 100%; text-align: center; letter-spacing: 0.35em;
  font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums;
  padding: 0.85rem 0.5rem; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elev); outline: none;
}
.code-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.qr-box {
  width: min(220px, 70vw); aspect-ratio: 1; margin: 0.5rem auto 0;
  padding: 0.75rem; border-radius: 14px; background: #fff; color: #000;
}
.qr-box svg { width: 100%; height: 100%; }
.waiting-pulse {
  display: inline-flex; align-items: center; gap: 0.5rem;
}
.waiting-pulse::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent-3); box-shadow: 0 0 0 0 var(--accent-glow);
  animation: pulse 1.4s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(232,121,249,.55); }
  70% { box-shadow: 0 0 0 12px rgba(232,121,249,0); }
  100% { box-shadow: 0 0 0 0 rgba(232,121,249,0); }
}
.connected-banner {
  display: flex; align-items: center; gap: 0.55rem;
  padding: 0.7rem 0.85rem; border-radius: 12px;
  background: rgba(52, 211, 153, 0.12); border: 1px solid rgba(52,211,153,.35);
  color: var(--success); font-weight: 600;
}

/* Game shell */
.shell-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  padding: 0.65rem 0 0.9rem;
}
.shell-title { font-weight: 700; letter-spacing: -0.02em; }
.shell-stage {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg-card); box-shadow: var(--shadow);
  overflow: hidden; min-height: 320px;
  display: flex; flex-direction: column;
}
.shell-hud {
  display: flex; justify-content: space-between; align-items: center; gap: 0.5rem;
  padding: 0.75rem 0.9rem; border-bottom: 1px solid var(--border);
  font-size: 0.9rem; color: var(--muted);
}
.shell-hud strong { color: var(--text); }
.shell-body {
  flex: 1; display: grid; place-items: center; padding: 0.85rem;
  touch-action: none;
}
.shell-actions {
  display: flex; gap: 0.5rem; padding: 0.75rem 0.9rem; border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

/* Story reader */
.story-art {
  height: 140px; border-radius: 14px; margin-bottom: 1rem;
  background:
    linear-gradient(135deg, rgba(15,10,30,.2), rgba(88,28,135,.45)),
    radial-gradient(circle at 20% 80%, rgba(232,121,249,.35), transparent 50%),
    linear-gradient(160deg, #1a1030, #0b0614);
  border: 1px solid var(--border);
  display: grid; place-items: end start; padding: 0.85rem;
}
.story-art span { font-size: 0.75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-3); }
.story-text { font-size: 1.05rem; line-height: 1.65; margin-bottom: 1.1rem; white-space: pre-wrap; }
.choice-list { display: grid; gap: 0.55rem; }
.choice-btn {
  text-align: left; padding: 0.85rem 1rem; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-elev);
  color: var(--text); font-weight: 600;
}
.choice-btn:hover { border-color: rgba(192,38,211,.5); background: var(--bg-card-hover); }
.progress-bar {
  height: 4px; border-radius: 99px; background: var(--border); overflow: hidden; margin: 0.5rem 0 1rem;
}
.progress-bar > i {
  display: block; height: 100%; width: 0%;
  background: linear-gradient(90deg, #c026d3, #e879f9);
  transition: width .25s ease;
}

/* Snake */
.snake-canvas {
  width: min(100%, 360px); aspect-ratio: 1; border-radius: 12px;
  background: #0a0712; border: 1px solid var(--border); image-rendering: pixelated;
  touch-action: none;
}
.dpad {
  display: grid; grid-template-columns: 56px 56px 56px; grid-template-rows: 48px 48px 48px;
  gap: 0.35rem; margin-top: 0.75rem; justify-content: center;
}
.dpad button {
  border-radius: 12px; border: 1px solid var(--border); background: var(--bg-elev);
  font-size: 1.1rem; color: var(--text);
}
.dpad .u { grid-column: 2; grid-row: 1; }
.dpad .l { grid-column: 1; grid-row: 2; }
.dpad .r { grid-column: 3; grid-row: 2; }
.dpad .d { grid-column: 2; grid-row: 3; }

/* 2048 */
.g2048 {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  width: min(100%, 360px); aspect-ratio: 1;
  padding: 8px; border-radius: 14px; background: #1a1228; border: 1px solid var(--border);
}
.g2048 .cell {
  border-radius: 8px; display: grid; place-items: center;
  font-weight: 800; font-size: clamp(1rem, 5vw, 1.45rem);
  background: #241834; color: var(--text);
}
.g2048 .t2 { background: #3b2758; }
.g2048 .t4 { background: #4a2d6e; }
.g2048 .t8 { background: #6b2d8a; color: #fff; }
.g2048 .t16 { background: #8b2aad; color: #fff; }
.g2048 .t32 { background: #a21caf; color: #fff; }
.g2048 .t64 { background: #c026d3; color: #fff; }
.g2048 .t128, .g2048 .t256, .g2048 .t512, .g2048 .t1024, .g2048 .t2048 {
  background: linear-gradient(135deg, #e879f9, #7c3aed); color: #fff;
  font-size: clamp(0.85rem, 4vw, 1.2rem);
}

/* Memory */
.memory-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  width: min(100%, 360px);
}
.memory-card {
  aspect-ratio: 1; border-radius: 12px; border: 1px solid var(--border);
  background: linear-gradient(145deg, #241834, #160f24);
  display: grid; place-items: center; font-size: 1.5rem;
  transition: transform .2s ease;
  transform-style: preserve-3d;
}
.memory-card.flipped, .memory-card.matched {
  background: var(--accent-soft); border-color: rgba(192,38,211,.45);
}
.memory-card.matched { opacity: 0.7; }
.memory-card .face { display: none; }
.memory-card.flipped .face, .memory-card.matched .face { display: block; }
.memory-card .back { display: block; color: var(--faint); font-weight: 800; }
.memory-card.flipped .back, .memory-card.matched .back { display: none; }

/* Tic-tac-toe / Connect4 */
.ttt {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  width: min(100%, 320px); aspect-ratio: 1;
}
.ttt button {
  border-radius: 12px; border: 1px solid var(--border); background: var(--bg-elev);
  font-size: clamp(1.8rem, 8vw, 2.6rem); font-weight: 800;
}
.ttt button.x { color: var(--accent-3); }
.ttt button.o { color: #60a5fa; }
.ttt button:disabled { opacity: 1; }
.c4 {
  display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px;
  width: min(100%, 380px); background: #1a1228; padding: 8px; border-radius: 14px;
  border: 1px solid var(--border);
}
.c4 button {
  aspect-ratio: 1; border-radius: 50%; border: 1px solid #2a1f3d; background: #0b0614;
}
.c4 button.p1 { background: radial-gradient(circle at 35% 30%, #f0abfc, #c026d3); border-color: #e879f9; }
.c4 button.p2 { background: radial-gradient(circle at 35% 30%, #93c5fd, #2563eb); border-color: #60a5fa; }

.hidden { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); border: 0;
}
