:root {
  --bg-deep: #020205;
  --ink: #f5f5fa;
  --ink-mid: #888899;
  --ink-low: #444455;
  --line: rgba(255, 255, 255, 0.08);
  --line-warm: rgba(255, 255, 255, 0.16);
  --serif: 'Instrument Serif', 'Times New Roman', serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  width: 100%;
  background: var(--bg-deep);
  color: var(--ink);
  font-family: var(--mono);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* Sans cette règle, body n'est pas un stacking context, donc les enfants
   en z-index négatif (cosmos -2, vignette -1) sont peints dans le contexte
   de <html> AVANT le background du body, qui les masque ensuite. */
body { isolation: isolate; }

/* Cosmic background canvas */
#cosmos {
  position: fixed; inset: 0;
  z-index: -2;
  pointer-events: none;
}

.warp-defs { position: absolute; left: -9999px; top: -9999px; }

/* Vignette + grain */
body::before {
  content: "";
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: -1;
  background: radial-gradient(ellipse at center, transparent 50%, rgba(0,0,0,0.7) 100%);
}
body::after {
  content: "";
  position: fixed; inset: 0;
  z-index: 9998;
  pointer-events: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='240' height='240'><filter id='n'><feTurbulence baseFrequency='0.85' numOctaves='3' stitchTiles='stitch' seed='3'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 0.04 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  mix-blend-mode: overlay;
  opacity: 0.45;
}

/* HUD */
.hud {
  position: fixed;
  top: 22px; left: 28px; right: 28px;
  z-index: 50;
  display: flex; justify-content: space-between; align-items: baseline;
  pointer-events: none;
  font-size: 9.5px;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--ink-mid);
}
.hud .brand {
  pointer-events: auto;
  font-family: var(--serif);
  font-style: italic;
  font-size: 30px;
  letter-spacing: -0.02em;
  background: linear-gradient(120deg, #fcd34d, #f97316 40%, #ec4899 70%, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-decoration: none;
  text-transform: none;
  line-height: 1;
}
.hud-meta { display: flex; gap: 22px; align-items: center; }

.hud-meta .live-dot {
  width: 6px; height: 6px;
  background: #22d3ee;
  border-radius: 50%;
  box-shadow: 0 0 10px #22d3ee;
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }
.hud-hint { color: var(--ink-low); }

/* Games counter — clickable dropdown trigger */
.games-dropdown {
  position: relative;
  pointer-events: auto;
}
.games-counter {
  background: transparent;
  border: none;
  color: var(--ink-mid);
  font-family: var(--mono);
  font-size: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color 0.2s ease;
}
.games-counter:hover,
.games-counter[aria-expanded="true"] { color: var(--ink); }
.games-counter-caret {
  font-size: 10px;
  line-height: 1;
  transition: transform 0.2s ease;
}
.games-counter[aria-expanded="true"] .games-counter-caret { transform: rotate(180deg); }

.games-menu {
  position: absolute;
  top: calc(100% + 12px);
  left: 50%;
  transform: translateX(-50%);
  min-width: 220px;
  list-style: none;
  margin: 0;
  padding: 6px;
  background: rgba(8, 8, 14, 0.85);
  border: 1px solid var(--line-warm);
  border-radius: 8px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  z-index: 60;
  /* 31 games au total — sur petits écrans la liste dépasse l'écran. On
     plafonne à 70 % de la hauteur viewport et on autorise le scroll vertical
     interne (smooth, scrollbar discrète). */
  max-height: min(70vh, 540px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.18) transparent;
}
.games-menu::-webkit-scrollbar { width: 6px; }
.games-menu::-webkit-scrollbar-track { background: transparent; }
.games-menu::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.16);
  border-radius: 3px;
}
.games-menu::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.28); }
.games-menu[hidden] { display: none; }
.games-menu li { margin: 0; }
.games-menu a {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 4px;
  text-decoration: none;
  color: var(--ink-mid);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  transition: background 0.15s ease, color 0.15s ease;
}
.games-menu a:hover,
.games-menu a:focus-visible {
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  outline: none;
}
.games-menu .menu-num {
  color: var(--ink-low);
  font-variant-numeric: tabular-nums;
  font-size: 9px;
}
.games-menu .menu-name { letter-spacing: 0.12em; }
.games-menu .menu-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: hsl(var(--hue), 80%, 60%);
  box-shadow: 0 0 8px hsla(var(--hue), 90%, 60%, 0.6);
  flex-shrink: 0;
  align-self: center;
}

/* ===== Mobile : menu en plein écran (overlay), pas en dropdown ===== */
@media (max-width: 760px) {
  .games-menu {
    /* On bascule complètement le contenant : du dropdown ancré au compteur,
       on passe à un overlay plein écran. C'est plus tactile, plus lisible,
       et adapté à 31 entrées (un dropdown scrollable est un pattern PC). */
    position: fixed;
    inset: 0;
    top: 0; left: 0; right: 0; bottom: 0;
    transform: none;
    width: 100vw;
    max-width: none;
    min-width: 0;
    max-height: none;
    height: 100dvh;
    margin: 0;
    border: none;
    border-radius: 0;
    background: rgba(4, 4, 10, 0.96);
    backdrop-filter: blur(24px) saturate(160%);
    -webkit-backdrop-filter: blur(24px) saturate(160%);
    box-shadow: none;
    z-index: 200;
    /* Layout : header (close) en haut, liste centrée, scroll vertical naturel */
    padding: 64px 18px 32px 18px;
    overflow-y: auto;
    overscroll-behavior: contain;
    /* On supprime la grille 2-cols, juste une colonne lisible */
    display: block;
  }
  .games-menu li { display: block; margin: 0; }
  .games-menu a {
    padding: 14px 14px;
    font-size: 12px;
    letter-spacing: 0.18em;
    gap: 14px;
    border-radius: 6px;
    /* Cible tactile minimale (44px) — l'élément `a` fait 44+ avec padding */
    min-height: 44px;
    align-items: center;
  }
  .games-menu .menu-num { font-size: 10px; min-width: 24px; }
  .games-menu .menu-name { letter-spacing: 0.10em; font-size: 13px; }
  .games-menu .menu-dot { width: 8px; height: 8px; }

  /* Bouton fermer (×) injecté en CSS via ::before sur le menu */
  .games-menu::before {
    content: "×";
    position: absolute;
    top: 14px; right: 16px;
    width: 36px; height: 36px;
    display: flex; align-items: center; justify-content: center;
    font-family: var(--serif);
    font-size: 28px; font-style: italic;
    color: var(--ink-mid);
    pointer-events: none; /* le clic est géré sur le menu lui-même via tap-outside */
  }

  /* Compteur ouvert : on inverse le caret en croix tactile */
  .games-counter[aria-expanded="true"] .games-counter-caret { display: none; }
}

/* World — 2D infinite canvas. Cards positioned via JS-set transform. */
#world {
  position: fixed;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  cursor: grab;
}
#world.dragging { cursor: grabbing; }

.game-card {
  position: absolute;
  top: 0; left: 0;
  width: var(--card-w, 280px);
  display: block;
  text-decoration: none;
  color: inherit;
  transform: translate3d(0, 0, 0);
  will-change: transform;
  /* Cartes = <a> avec href. Sans ces règles, presser sur une carte
     déclenche le drag-image natif (desktop) ou le tap-navigate (touch)
     avant que notre handler de pan ait pu prendre la main. */
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.game-card * { -webkit-user-drag: none; }
.card-figure {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 4px;
  overflow: hidden;
  background: rgba(8, 8, 14, 0.65);
  border: 1px solid var(--line);
  /* Halos plus serrés : avec 30 cartes ils se chevauchaient en moiré quand on bougeait */
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.04),
    0 20px 50px hsla(var(--hue), 70%, 50%, 0.20),
    0 0 90px hsla(var(--hue), 80%, 50%, 0.08);
  filter: url(#warp);
  will-change: filter;
}
/* Mobile : warp SVG + box-shadow lourde = gros lag GPU. On simplifie.
   Pour garder un peu de vie au drag, scale léger piloté par --warp (set par JS). */
@media (max-width: 900px), (hover: none) and (pointer: coarse) {
  .card-figure {
    filter: none;
    box-shadow:
      0 0 0 1px rgba(255,255,255,0.04),
      0 12px 30px hsla(var(--hue), 70%, 50%, 0.20);
    will-change: transform;
    transition: none;
    /* Scale léger inversement proportionnel à la vélocité de la cam.
       --warp est mis à jour chaque frame par world.js (0 → ~1). */
    transform: scale(calc(1 - var(--warp, 0) * 0.05));
  }
  #world:not(.moving) .game-card:hover .card-figure {
    box-shadow:
      0 0 0 1px rgba(255,255,255,0.06),
      0 16px 40px hsla(var(--hue), 70%, 50%, 0.28);
  }
  .games-menu {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(8, 8, 14, 0.92);
  }
}
.card-figure::before {
  content: "";
  position: absolute; inset: -1px;
  border-radius: 5px;
  padding: 1px;
  background: linear-gradient(135deg,
    hsla(var(--hue), 90%, 70%, 0.45),
    hsla(calc(var(--hue) + 60), 90%, 60%, 0.22),
    hsla(var(--hue), 90%, 50%, 0.35));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
  opacity: 0.65;
  z-index: 2;
}
.card-figure canvas {
  width: 100%; height: 100%;
  display: block;
}

.card-cap {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.card-line {
  display: flex;
  align-items: baseline;
  gap: 14px;
}
.card-num {
  font-size: 10px;
  letter-spacing: 0.4em;
  color: var(--ink-low);
  font-variant-numeric: tabular-nums;
}
.card-title {
  font-family: var(--serif);
  font-style: italic;
  font-weight: 400;
  font-size: clamp(28px, 3.5vw, 52px);
  line-height: 0.94;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg,
    hsl(var(--hue), 90%, 80%) 0%,
    hsl(calc(var(--hue) + 30), 90%, 68%) 50%,
    hsl(calc(var(--hue) + 70), 90%, 72%) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.card-tag {
  font-size: 10px;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  color: var(--ink-mid);
}
/* Hover gated sur #world non-en-mouvement : sinon, les cartes qui passent
   sous le curseur pendant un scroll s'allument une à une et créent un flash. */
#world:not(.moving) .game-card:hover .card-figure {
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.10),
    0 28px 70px hsla(var(--hue), 80%, 50%, 0.32),
    0 0 130px hsla(var(--hue), 90%, 50%, 0.14);
}
#world:not(.moving) .game-card:hover .card-tag { color: var(--ink); }

/* Mobile : la largeur card est gérée par --card-w (JS adapte au viewport).
   On ajuste juste typographie et chrome pour les petits écrans. */
@media (max-width: 760px) {
  .hud { font-size: 8px; }
  .hud .brand { font-size: 24px; }
  .card-title { font-size: clamp(20px, 6vw, 36px); }
  .card-num   { font-size: 9px; letter-spacing: 0.32em; }
  .card-tag   { font-size: 9px; letter-spacing: 0.25em; }
  .card-cap   { margin-top: 10px; gap: 4px; }
  .card-line  { gap: 10px; }
}
@media (max-width: 480px) {
  .card-title { font-size: clamp(18px, 6vw, 28px); }
  .card-num   { font-size: 8px; }
  .card-tag   { font-size: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  .live-dot { animation: none; }
  .card-figure { filter: none; }
}

/* Visually-hidden index — accessible to screen readers and crawlers, invisible to users */
.sr-index {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
