/* Snake — matches portal dark theme */
:root {
  --bg: #0f0f1a;
  --bg-2: #1a1a2e;
  --bg-3: #232342;
  --accent: #e94560;
  --accent-2: #ff6b6b;
  --gold: #ffd54a;
  --green: #4caf50;
  --green-2: #66bb6a;
  --green-3: #2e7d32;
  --text: #e8e8f0;
  --text-dim: #9999b3;
  --border: #2a2a4a;
  --shadow: 0 8px 30px rgba(0,0,0,0.45);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: contain;
}

.container {
  max-width: 980px;
  margin: 0 auto;
  padding: 0 1rem;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.subtitle {
  color: var(--text-dim);
  font-size: 0.95rem;
  margin-top: 0.25rem;
}

.back-link {
  color: var(--text);
  text-decoration: none;
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  font-size: 0.9rem;
  transition: all 0.15s ease;
}
.back-link:hover {
  background: var(--bg-3);
  border-color: var(--accent);
  color: var(--accent);
}

.game-shell {
  background: var(--bg-2);
  border-radius: 16px;
  padding: 1.25rem;
  margin: 1.5rem 0;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

/* HUD */
.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.hud-cell {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  text-align: center;
}

.hud-label {
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  color: var(--text-dim);
  margin-bottom: 0.15rem;
}

.hud-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.hud-cell:has(#score) .hud-value.bump {
  animation: bump 0.3s ease;
  color: var(--accent-2);
}
.hud-cell:has(#best) .hud-value.gold {
  color: var(--gold);
  text-shadow: 0 0 10px rgba(255,213,74,0.5);
}

@keyframes bump {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.18); }
  100% { transform: scale(1); }
}

/* Board */
.board-wrap {
  position: relative;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  aspect-ratio: 1 / 1;
  background: #0a0a14;
  border: 2px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(10,10,20,0.85);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 1rem;
  animation: overlayFade 0.25s ease;
}
.overlay.hidden { display: none; }

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

.overlay-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem 1.5rem;
  text-align: center;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
  animation: cardPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes cardPop {
  from { transform: scale(0.85); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

.overlay-card h2 {
  margin: 0 0 0.75rem;
  font-size: 1.5rem;
  color: var(--text);
}

.overlay-text {
  color: var(--text-dim);
  margin: 0.5rem 0;
  font-size: 0.9rem;
}
.overlay-text.small {
  font-size: 0.8rem;
}

kbd {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 0.8rem;
  color: var(--text);
  min-width: 1.5rem;
  text-align: center;
}

.btn-primary {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  width: 100%;
}
.btn-primary:hover {
  background: var(--accent-2);
  transform: translateY(-1px);
}
.btn-primary:active {
  transform: translateY(0);
}

.btn-ghost {
  display: inline-block;
  margin-top: 0.5rem;
  padding: 0.55rem 1rem;
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s ease;
  width: 100%;
}
.btn-ghost:hover {
  color: var(--text);
  border-color: var(--accent);
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 1rem;
  gap: 0.5rem;
}

.setting {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--text-dim);
  font-size: 0.85rem;
  cursor: pointer;
}
.setting input { accent-color: var(--accent); }

/* Result */
.result-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin: 1rem 0;
}

.result-cell {
  background: var(--bg-3);
  border-radius: 8px;
  padding: 0.6rem 0.4rem;
  text-align: center;
}

.result-label {
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  color: var(--text-dim);
}

.result-value {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.new-best {
  background: linear-gradient(90deg, var(--gold), #ffb300, var(--gold));
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: 0.05em;
  animation: shimmer 1.6s linear infinite, glow 1.4s ease-in-out infinite alternate;
  margin: 0.5rem 0;
}
.new-best.hidden { display: none; }

@keyframes shimmer {
  from { background-position: 0% 0; }
  to   { background-position: 200% 0; }
}
@keyframes glow {
  from { filter: drop-shadow(0 0 4px rgba(255,213,74,0.4)); }
  to   { filter: drop-shadow(0 0 12px rgba(255,213,74,0.9)); }
}

.reason {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin: 0.5rem 0 0.25rem;
}

/* Popups */
.popups {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 5;
}

.popup {
  position: absolute;
  font-weight: 800;
  color: var(--accent-2);
  text-shadow: 0 0 8px rgba(255,107,107,0.6), 0 2px 4px rgba(0,0,0,0.6);
  font-size: 1.2rem;
  animation: popupRise 1s ease-out forwards;
  transform: translate(-50%, -50%);
  white-space: nowrap;
}
.popup.gold {
  color: var(--gold);
  text-shadow: 0 0 10px rgba(255,213,74,0.7), 0 2px 4px rgba(0,0,0,0.6);
  font-size: 1.4rem;
}

@keyframes popupRise {
  0%   { opacity: 0; transform: translate(-50%, -40%) scale(0.5); }
  20%  { opacity: 1; transform: translate(-50%, -55%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -130%) scale(1); }
}

/* Mobile d-pad */
.mobile-controls {
  display: none;
  justify-content: center;
  margin-top: 1rem;
}

.dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}
.dpad-mid {
  display: flex;
  gap: 0.4rem;
}
.dbtn {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 1.3rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: all 0.1s ease;
  touch-action: manipulation;
}
.dbtn:active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  transform: scale(0.95);
}

/* Footer */
.footer-bar {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  justify-content: center;
  color: var(--text-dim);
  font-size: 0.85rem;
  padding: 0.5rem 0;
}

.tip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.kbd {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 0.7rem;
  color: var(--text);
  min-width: 1.4rem;
  text-align: center;
}

/* Responsive */
@media (max-width: 640px) {
  .hud-value { font-size: 1.2rem; }
  .hud-label { font-size: 0.6rem; }
  .mobile-controls { display: flex; }
  .board-wrap { max-width: 100%; }
  .overlay-card { padding: 1.25rem 1rem; }
  .overlay-card h2 { font-size: 1.25rem; }
  .footer-bar { gap: 0.75rem; font-size: 0.75rem; }
  .result-value { font-size: 1.1rem; }
}

@media (max-width: 380px) {
  .hud { grid-template-columns: repeat(2, 1fr); }
  .dbtn { width: 50px; height: 50px; font-size: 1.1rem; }
}

/* Flash overlay for game over */
.board-wrap.flash-red::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(233,69,96,0.35);
  z-index: 9;
  pointer-events: none;
  animation: flashRed 0.5s ease-out forwards;
}

@keyframes flashRed {
  0%   { opacity: 1; }
  100% { opacity: 0; }
}
