:root {
  --bg0: #1a1030;
  --bg1: #2d1b4e;
  --card: #fff7ed;
  --ink: #2a1840;
  --muted: #6b5a7a;
  --accent: #ff8c42;
  --accent2: #7c5cff;
  --ok: #22c55e;
  --panel: rgba(255, 247, 237, 0.94);
  --shadow: 0 10px 30px rgba(20, 8, 40, 0.35);
  --radius: 18px;
  --focus: #ffe566;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(1200px 800px at 20% 0%, #3b2466 0%, var(--bg0) 55%, #120a22 100%);
  color: #fff7ed;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; cursor: pointer; }
button:focus-visible, .card:focus-visible, .theme:focus-visible, .lvl:focus-visible {
  outline: 3px solid var(--focus); outline-offset: 2px;
}
#app {
  position: relative; height: 100%; width: 100%;
  padding: calc(12px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  display: flex; flex-direction: column; gap: 10px;
}
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; animation: fade .28s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .screen, .card, .overlay .panel, .star { animation: none !important; transition: none !important; }
  .card.flip-anim { transition: none !important; }
}

/* ---- Menu */
.hero {
  text-align: center; padding: 8px 8px 4px;
}
.hero h1 {
  margin: 0; font-size: clamp(1.6rem, 5vw, 2.4rem); letter-spacing: .02em;
  background: linear-gradient(90deg, #ffe566, #ff8c42, #c084fc);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 2px 0 rgba(0,0,0,.25));
}
.hero p { margin: .35rem 0 0; color: #e9d5ff; opacity: .9; font-size: .95rem; }
.toolbar {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center;
}
.chip, .icon-btn, .btn {
  border: 0; border-radius: 999px; padding: .55rem 1rem;
  background: rgba(255,255,255,.12); color: #fff7ed;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.12);
}
.btn.primary {
  background: linear-gradient(135deg, var(--accent), #ff5e7a);
  font-weight: 700; box-shadow: var(--shadow);
}
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 2px rgba(255,255,255,.25); }
.btn:disabled, .lvl:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn { width: 42px; height: 42px; padding: 0; display: grid; place-items: center; border-radius: 14px; }
.modes { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.modes .chip.on { background: rgba(124, 92, 255, .45); box-shadow: inset 0 0 0 2px #c4b5fd; }

.themes {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px;
  margin: 8px 0; max-width: 720px; width: 100%; align-self: center;
  overflow: auto; padding: 4px;
}
.theme {
  appearance: none; border: 0; text-align: left;
  background: var(--panel); color: var(--ink);
  border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: 64px 1fr; gap: 10px; align-items: center;
  transition: transform .15s ease;
}
.theme:hover { transform: translateY(-2px); }
.theme.on { box-shadow: 0 0 0 3px var(--accent2), var(--shadow); }
.theme .art {
  width: 64px; height: 64px; border-radius: 16px;
  background: linear-gradient(160deg, #fff, #ffe8d2);
  display: grid; place-items: center; overflow: hidden;
}
.theme .art svg { width: 52px; height: 52px; }
.theme h3 { margin: 0; font-size: 1.05rem; }
.theme small { color: var(--muted); display: block; margin-top: 2px; }

.levels-wrap {
  background: rgba(0,0,0,.22); border-radius: var(--radius); padding: 12px;
  max-width: 720px; width: 100%; align-self: center; flex: 1; min-height: 0;
  display: flex; flex-direction: column; gap: 8px;
}
.levels-wrap h2 { margin: 0; font-size: 1rem; font-weight: 600; color: #fde68a; }
.levels {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px;
  overflow: auto;
}
.lvl {
  appearance: none; border: 0; border-radius: 14px; padding: 10px 6px;
  background: rgba(255,255,255,.1); color: #fff; display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.lvl .n { font-weight: 800; font-size: 1.1rem; }
.lvl .stars { letter-spacing: 1px; font-size: .85rem; color: #fde047; min-height: 1.1em; }
.lvl.lock { filter: grayscale(.4); }
.lvl.sel { box-shadow: 0 0 0 3px #ffe566; background: rgba(124,92,255,.35); }

.foot {
  display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; padding-bottom: 4px;
}

/* ---- Jeu */
.hud {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.hud .stat {
  background: rgba(255,255,255,.12); border-radius: 999px; padding: .35rem .75rem;
  font-variant-numeric: tabular-nums; font-size: .92rem;
}
.hud .sp { flex: 1; }
.board-wrap {
  flex: 1; min-height: 0; display: grid; place-items: center;
  padding: 4px; touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.board {
  width: min(100%, 560px); max-height: min(100%, 70vh);
  display: grid; gap: clamp(6px, 1.6vw, 12px);
  grid-auto-rows: minmax(0, 1fr);
  align-content: center;
}
.card {
  position: relative; border: 0; border-radius: 14px; padding: 0;
  background: transparent; perspective: 800px;
  min-height: 0; aspect-ratio: 1; width: 100%; height: auto;
  max-height: 100%;
  -webkit-user-select: none; user-select: none;
}
.card .face {
  position: absolute; inset: 0; border-radius: 14px;
  display: grid; place-items: center;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  transition: transform .28s ease;
  box-shadow: 0 6px 16px rgba(0,0,0,.28);
}
.card .back {
  background: linear-gradient(145deg, #7c5cff, #ff8c42 70%);
  transform: rotateY(0deg);
}
.card .back::after {
  content: ""; width: 42%; height: 42%; border-radius: 12px;
  background: rgba(255,255,255,.22);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.35);
}
.card .front {
  background: var(--card); transform: rotateY(180deg);
  color: var(--ink);
}
.card .front svg { width: 72%; height: 72%; }
.card.up .back { transform: rotateY(180deg); }
.card.up .front { transform: rotateY(0deg); }
.card.matched .front {
  box-shadow: 0 0 0 3px var(--ok), 0 6px 16px rgba(0,0,0,.2);
  filter: saturate(1.1);
}
.card.matched { pointer-events: none; }
.card:disabled { cursor: default; }
body.fast .card .face { transition: none; }

.overlay {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(12, 6, 24, .62); backdrop-filter: blur(4px);
  padding: 16px; z-index: 20;
}
.overlay .panel {
  background: var(--panel); color: var(--ink); border-radius: 22px;
  padding: 22px 20px; width: min(420px, 100%); text-align: center;
  box-shadow: var(--shadow); animation: pop .28s ease;
}
@keyframes pop { from { transform: scale(.94); opacity: 0; } to { transform: none; opacity: 1; } }
.overlay h2 { margin: 0 0 8px; font-size: 1.45rem; }
.overlay p { margin: 0 0 14px; color: var(--muted); }
.stars-big { font-size: 1.8rem; color: #eab308; letter-spacing: 4px; margin-bottom: 8px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.overlay .btn { color: #fff; }
.overlay .btn.primary { color: #fff; }
.overlay .btn.ghost { color: var(--ink); box-shadow: inset 0 0 0 2px rgba(42,24,64,.2); }

.toast {
  position: absolute; left: 50%; bottom: calc(18px + var(--safe-b));
  transform: translateX(-50%); background: rgba(0,0,0,.75); color: #fff;
  padding: .5rem 1rem; border-radius: 999px; z-index: 30; font-size: .9rem;
  pointer-events: none;
}

@media (max-width: 520px) {
  .themes { grid-template-columns: 1fr; }
  .levels { grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .hero h1 { font-size: 1.55rem; }
  .hud .stat { font-size: .8rem; padding: .3rem .55rem; }
}
@media (min-width: 900px) {
  .themes { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* Affinage visuel cartes + HUD mobile */
.card .back {
  background: linear-gradient(145deg, #9b7bff 0%, #ff9a5c 55%, #ff6b8a 100%);
}
.card .back::after {
  width: 46%; height: 46%; border-radius: 14px;
  background: rgba(255,255,255,.28);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.45), 0 2px 8px rgba(0,0,0,.15);
}
.card .front {
  background: linear-gradient(180deg, #fffdf8, #ffe8cc);
}
@media (max-width: 520px) {
  .hud { gap: 6px; }
  .hud .stat { font-size: .75rem; padding: .28rem .5rem; }
  .icon-btn { width: 38px; height: 38px; border-radius: 12px; }
  .board { height: min(100%, 62vh); gap: 8px; }
}

/* ---- Décors de thème (SVG derrière plateau / menu) */
.screen { position: relative; z-index: 0; }
.scene {
  position: absolute; inset: 0;
  z-index: 0; pointer-events: none; overflow: hidden;
  border-radius: 0;
}
.scene-svg { width: 100%; height: 100%; display: block; }
.screen > :not(.scene) { position: relative; z-index: 1; }
.scene-soft { opacity: .42; }
.screen.menu .scene-soft { mask-image: linear-gradient(180deg, #000 0%, #000 55%, transparent 100%);
  -webkit-mask-image: linear-gradient(180deg, #000 0%, #000 55%, transparent 100%); }

.screen.game[data-theme="animaux"] .hud .stat,
.screen.game[data-theme="animaux"] .icon-btn {
  background: rgba(40, 22, 12, .55);
  box-shadow: inset 0 0 0 1px rgba(255, 220, 170, .25);
}
.screen.game[data-theme="nature"] .hud .stat,
.screen.game[data-theme="nature"] .icon-btn {
  background: rgba(12, 40, 28, .5);
  box-shadow: inset 0 0 0 1px rgba(200, 255, 220, .22);
}
.screen.game[data-theme="espace"] .hud .stat,
.screen.game[data-theme="espace"] .icon-btn {
  background: rgba(10, 8, 30, .55);
  box-shadow: inset 0 0 0 1px rgba(180, 170, 255, .28);
}
.screen.game[data-theme="gaming"] .hud .stat,
.screen.game[data-theme="gaming"] .icon-btn {
  background: rgba(18, 10, 36, .55);
  box-shadow: inset 0 0 0 1px rgba(120, 230, 255, .25);
}

.board-wrap {
  padding: 8px;
}
.board-stage {
  width: min(100%, 600px);
  max-height: 100%;
  padding: clamp(10px, 2.2vw, 18px);
  border-radius: 22px;
  background: rgba(12, 8, 24, .28);
  box-shadow:
    inset 0 0 0 1px rgba(255,255,255,.16),
    0 12px 36px rgba(8, 4, 20, .35);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
}
.screen.game[data-theme="animaux"] .board-stage {
  background: rgba(55, 28, 12, .32);
  box-shadow: inset 0 0 0 1px rgba(255, 210, 140, .22), 0 12px 36px rgba(40, 18, 8, .35);
}
.screen.game[data-theme="nature"] .board-stage {
  background: rgba(10, 40, 28, .30);
  box-shadow: inset 0 0 0 1px rgba(180, 255, 200, .2), 0 12px 36px rgba(8, 30, 18, .32);
}
.screen.game[data-theme="espace"] .board-stage {
  background: rgba(8, 6, 28, .38);
  box-shadow: inset 0 0 0 1px rgba(160, 150, 255, .25), 0 12px 40px rgba(0, 0, 20, .45);
}
.screen.game[data-theme="gaming"] .board-stage {
  background: rgba(16, 8, 36, .36);
  box-shadow: inset 0 0 0 1px rgba(80, 220, 255, .22), 0 12px 36px rgba(10, 0, 30, .4);
}

/* Dos de cartes légèrement teintés selon le thème (sans toucher aux faces SVG) */
.screen.game[data-theme="animaux"] .card .back {
  background: linear-gradient(145deg, #e0a15a 0%, #d97706 45%, #b45309 100%);
}
.screen.game[data-theme="nature"] .card .back {
  background: linear-gradient(145deg, #4ade80 0%, #16a34a 50%, #166534 100%);
}
.screen.game[data-theme="espace"] .card .back {
  background: linear-gradient(145deg, #818cf8 0%, #6366f1 45%, #312e81 100%);
}
.screen.game[data-theme="gaming"] .card .back {
  background: linear-gradient(145deg, #22d3ee 0%, #a855f7 50%, #ec4899 100%);
}

.screen.menu[data-theme="animaux"] { --menu-glow: rgba(255, 170, 80, .12); }
.screen.menu[data-theme="nature"] { --menu-glow: rgba(80, 200, 120, .12); }
.screen.menu[data-theme="espace"] { --menu-glow: rgba(120, 110, 255, .14); }
.screen.menu[data-theme="gaming"] { --menu-glow: rgba(80, 220, 255, .12); }
.screen.menu {
  background: radial-gradient(900px 500px at 50% -10%, var(--menu-glow, transparent), transparent 70%);
}

@media (prefers-reduced-motion: reduce) {
  .scene, .board-stage { transition: none !important; }
  .board-stage { backdrop-filter: none; }
}
@media (max-width: 520px) {
  .board-stage { padding: 8px; border-radius: 16px; }
  .board-wrap { padding: 4px; }
}
