:root {
  --bg: #0f1220;
  --bg-alt: #171b2e;
  --card: #1b2038;
  --border: #2a2f4a;
  --text: #e7e9f5;
  --muted: #8d92b2;
  --accent: #7c5cff;
  --accent-2: #22d3ee;
  --danger: #ef4444;
  --success: #22c55e;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: radial-gradient(1200px 600px at 50% -10%, #23285a 0%, var(--bg) 55%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  min-height: 100vh;
}
a { color: var(--accent-2); text-decoration: none; }

.wrap { max-width: 1000px; margin: 0 auto; padding: 0 20px; }
.wrap.narrow { max-width: 420px; padding-top: 12vh; }

.site-header { border-bottom: 1px solid var(--border); background: rgba(15,18,32,0.7); backdrop-filter: blur(6px); position: sticky; top: 0; z-index: 5; }
.header-row { display: flex; align-items: center; justify-content: space-between; height: 68px; }
.logo { font-size: 1.4rem; margin: 0; font-weight: 800; letter-spacing: -0.02em; }
.logo .accent { color: var(--accent-2); }

.user-chip { display: flex; align-items: center; gap: 10px; font-size: 0.9rem; }
.user-chip img { width: 28px; height: 28px; border-radius: 50%; }

.btn-primary {
  display: inline-block; background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #0b0d19; font-weight: 700; padding: 10px 18px; border-radius: 10px; border: none;
  cursor: pointer; font-size: 0.95rem;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); padding: 7px 12px; border-radius: 8px; font-size: 0.85rem; }
.btn-play { padding: 8px 16px; font-size: 0.85rem; }

.flash { padding: 10px 14px; border-radius: 8px; margin: 16px 0; font-size: 0.9rem; }
.flash-error { background: rgba(239,68,68,0.15); border: 1px solid var(--danger); color: #fecaca; }
.flash-success { background: rgba(34,197,94,0.15); border: 1px solid var(--success); color: #bbf7d0; }
.flash-info { background: rgba(124,92,255,0.15); border: 1px solid var(--accent); color: #ded9ff; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

.game-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; padding: 28px 0 60px; }
.game-card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 20px; }
.game-card-head { display: flex; align-items: center; justify-content: space-between; }
.game-card-head h2 { margin: 0; font-size: 1.15rem; }

.leaderboard { list-style: none; margin: 16px 0 0; padding: 0; border-top: 1px solid var(--border); }
.leaderboard li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
.leaderboard li:first-child { border-top: none; }
.leaderboard .rank { color: var(--muted); width: 30px; }
.leaderboard .name { flex: 1; font-weight: 600; }
.leaderboard .score { color: var(--accent-2); font-weight: 700; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 28px; text-align: center; }
.card h1 { margin-top: 0; }
.card form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.card input[type=text] {
  background: var(--bg-alt); border: 1px solid var(--border); color: var(--text);
  padding: 12px 14px; border-radius: 10px; font-size: 1rem; text-align: center;
}

/* --- shared game-page chrome --- */
.game-top { display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; border-bottom: 1px solid var(--border); gap: 10px; }
.game-top a.back { color: var(--muted); font-size: 0.9rem; flex-shrink: 0; }
.game-top h2 { font-size: 1.1rem; }
.game-stage { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 24px 16px 40px; width: 100%; }
.game-hud { display: flex; gap: 24px; font-size: 1rem; flex-wrap: wrap; justify-content: center; }
.game-hud b { color: var(--accent-2); }
canvas { background: var(--bg-alt); border: 1px solid var(--border); border-radius: 12px; touch-action: none; max-width: 100%; height: auto; }
.game-msg { min-height: 1.4em; font-size: 0.95rem; color: var(--muted); text-align: center; }
.game-msg.win { color: var(--success); }

/* --- on-screen touch controls: fixed to the screen edges. Shown ONLY when the device's
   primary pointer has no hover AND is coarse (a real touch-primary phone/tablet) — using AND
   (not OR) so a desktop with an incidental touchscreen/mouse combo never triggers it, and
   never above tablet width either. One game's own key set only. --- */
.touch-controls { display: none; }
@media (hover: none) and (pointer: coarse) and (max-width: 820px) {
  .touch-controls { display: flex; }
  /* leave room so the fixed control bar never covers the canvas/message */
  .game-stage { padding-bottom: 220px; }
}
.touch-controls {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  align-items: flex-end; justify-content: space-between;
  gap: 12px;
  padding: 0 18px calc(20px + env(safe-area-inset-bottom, 0px));
  pointer-events: none;
  user-select: none; -webkit-user-select: none;
  z-index: 20;
}
.touch-controls.center-only { justify-content: center; }

/* one consistent button family (gradient-filled circles) for every on-screen control —
   .touch-edge = primary (bigger, pinned to the screen edge), .touch-btn = secondary (smaller,
   grouped in the center cluster) */
.touch-edge, .touch-btn {
  pointer-events: auto;
  background: linear-gradient(150deg, var(--accent), var(--accent-2));
  border: none; color: #0b0d19; font-weight: 900; line-height: 1;
  box-shadow: 0 10px 26px rgba(124,92,255,0.4), inset 0 1px 0 rgba(255,255,255,0.3);
  display: flex; align-items: center; justify-content: center;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
  transition: transform 0.08s ease, filter 0.08s ease;
}
.touch-edge:active, .touch-btn:active, .touch-edge.active, .touch-btn.active {
  transform: scale(0.88); filter: brightness(0.9);
}

.touch-edge { width: 84px; height: 84px; border-radius: 50%; flex-shrink: 0; font-size: 2.1rem; }

.touch-center { pointer-events: auto; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.touch-center .touch-row { display: flex; gap: 12px; justify-content: center; }

.touch-btn { width: 64px; height: 64px; border-radius: 50%; font-size: 1.5rem; }
.touch-btn.wide { width: auto; padding: 0 26px; border-radius: 32px; font-size: 0.9rem; letter-spacing: 0.05em; }
.touch-btn.flap { width: 96px; height: 96px; font-size: 1.05rem; }

@media (max-width: 420px) {
  .game-top { padding: 10px 12px; }
  .game-stage { padding-left: 8px; padding-right: 8px; gap: 10px; }
  .touch-controls { padding: 0 12px calc(16px + env(safe-area-inset-bottom, 0px)); }
  .touch-edge { width: 72px; height: 72px; font-size: 1.8rem; }
  .touch-btn { width: 56px; height: 56px; font-size: 1.3rem; }
  .touch-btn.flap { width: 84px; height: 84px; }
}
