/* Puzzdle : jeu de société à la campagne, fin d'après-midi. */

:root {
  --wood: #643d22;
  --wood-light: #9a6337;
  --wood-dark: #4d2d16;
  --felt: #2f6b46;
  --felt-light: #3c8156;
  --felt-dark: #1d4630;
  --cream: #f6ead2;
  --paper: #fbf4e4;
  --card: #f1e3c4;
  --ink: #3b2a1a;
  --ink-soft: #6b5640;
  --honey: #e3a14f;
  --honey-dark: #b5652a;
  --sage: #6f8f5c;
  --photo-color: #8a6a4a;

  --font-title: 'Fraunces', Georgia, 'Times New Roman', serif;
  --font-body: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;

  /* Veinage du bois et grain du feutre, générés en SVG. */
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='640' height='640'><filter id='g'><feTurbulence type='fractalNoise' baseFrequency='0.005 0.11' numOctaves='3' seed='7' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.2  0 0 0 0 0.1  0 0 0 0 0.03  1.3 0 0 0 -0.42'/></filter><rect width='100%' height='100%' filter='url(%23g)'/></svg>");
  --fuzz: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0.05  0 0 0 0 0  0 0 0 0.5 -0.1'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  /* Grain fin : poussière qui accroche la lumière. */
  --dust: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='240' height='240'><filter id='d'><feTurbulence type='fractalNoise' baseFrequency='1.1' numOctaves='1' seed='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 0.84  0 0 0 0 0.6  0 0 0 1.7 -1.08'/></filter><rect width='100%' height='100%' filter='url(%23d)'/></svg>");

  /* Bougies hors champ, tout autour de la table : un groupe par côté, pour vaciller séparément. */
  --candles-west:
    radial-gradient(ellipse 48% 42% at -6% 22%, rgba(255, 160, 70, 0.6), transparent 70%),
    radial-gradient(ellipse 50% 46% at -4% 94%, rgba(255, 146, 58, 0.5), transparent 70%),
    radial-gradient(ellipse 54% 30% at 30% 110%, rgba(255, 172, 90, 0.38), transparent 70%);
  --candles-east:
    radial-gradient(ellipse 46% 40% at 106% 14%, rgba(255, 152, 64, 0.52), transparent 70%),
    radial-gradient(ellipse 52% 46% at 104% 88%, rgba(255, 166, 80, 0.58), transparent 70%),
    radial-gradient(ellipse 54% 28% at 70% -10%, rgba(255, 180, 100, 0.34), transparent 70%);
  /* Pénombre de la pièce, avec un léger halo au centre de la table. */
  --candle-room:
    radial-gradient(ellipse 70% 60% at 50% 46%, rgba(255, 196, 130, 0.1), transparent 65%),
    radial-gradient(ellipse 110% 95% at 50% 48%, transparent 30%, rgba(10, 4, 0, 0.72));

  --piece-grid: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><path d='M0 99 H38 C30 84 70 84 62 99 H100 M99 0 V38 C84 30 84 70 99 62 V100' fill='none' stroke='white' stroke-width='2.5' stroke-opacity='.7'/></svg>");
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; }

body,
.game {
  --table:
    repeating-linear-gradient(180deg, transparent 0 170px, rgba(20, 9, 2, 0.55) 170px 172px, rgba(255, 200, 150, 0.07) 172px 173px),
    repeating-linear-gradient(180deg, rgba(255, 235, 205, 0.04) 0 173px, rgba(0, 0, 0, 0.07) 173px 346px, rgba(120, 60, 20, 0.08) 346px 519px),
    var(--grain);
  background-color: var(--wood);
  background-image: var(--table);
}

/* Sur le plateau, la lumière reste derrière le cadre : elle ne teinte jamais la photo. */
.game { background-image: var(--dust), var(--candle-room), var(--table); }

body {
  font-family: var(--font-body);
  color: var(--cream);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body.in-game { overflow: hidden; }

a { color: inherit; text-underline-offset: 2px; }
a:hover { color: #fff; }

button { font: inherit; }

:focus-visible { outline: 3px solid var(--honey); outline-offset: 3px; }

/* Lumière de bougies invisibles, disposées autour de la table. */
.light {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: var(--dust), var(--candle-room);
}
.light::before,
.light::after,
.game::before,
.game::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  mix-blend-mode: screen;
}
.light::before,
.game::before {
  background: var(--candles-west);
  animation: flicker-a 4.7s ease-in-out infinite;
}
.light::after,
.game::after {
  background: var(--candles-east);
  animation: flicker-b 6.3s ease-in-out infinite;
}

.screen { position: relative; z-index: 1; }

/* --- Boutons ---------------------------------------------------------------- */

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: var(--cream);
  background: rgba(30, 15, 5, 0.35);
  box-shadow: inset 0 1px 0 rgba(255, 230, 200, 0.15), 0 2px 6px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
.icon-btn:hover { background: rgba(30, 15, 5, 0.55); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn .spk { fill: currentColor; stroke-width: 1.4; }
.sound-toggle .off,
.sound-toggle.is-muted .on { display: none; }
.sound-toggle.is-muted .off { display: inline; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4em;
  min-height: 44px;
  padding: 0.55em 1.2em;
  border: 0;
  border-radius: 999px;
  font-weight: 700;
  color: var(--ink);
  background: #e9dcc1;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12), 0 2px 6px rgba(60, 30, 10, 0.2);
  cursor: pointer;
  transition: transform 0.15s, background 0.2s;
}
.btn:hover { background: #e2d2b2; }
.btn:active { transform: translateY(1px); }
.btn-primary { color: #fff8ec; background: var(--honey-dark); }
.btn-primary:hover { background: #a2581f; }
.btn-small { min-height: 34px; padding: 0.3em 0.9em; font-size: 0.9rem; }

/* --- Accueil ---------------------------------------------------------------- */

.home {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(28px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  overflow: hidden;
}

.home-sound { position: absolute; top: max(16px, env(safe-area-inset-top)); right: 16px; z-index: 3; }

.decor { position: absolute; inset: 0; z-index: 0; pointer-events: none; }
.decor img { position: absolute; display: block; user-select: none; }
.decor-tea { top: 84px; right: -36px; width: 250px; transform: rotate(-12deg); }
.decor-crumbs { top: 330px; right: 150px; width: 110px; opacity: 0.95; }
.decor-box { bottom: -30px; left: -70px; width: 360px; }
.decor-pieces { bottom: 40px; right: 30px; width: 170px; }

.brand {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  margin-top: clamp(4px, 3vh, 36px);
}
.logo {
  width: 84px;
  height: 84px;
  filter: drop-shadow(0 6px 8px rgba(20, 8, 0, 0.45));
  animation: settle 1.2s cubic-bezier(.2, .9, .3, 1.2) both;
}
.brand h1 {
  margin: 4px 0 0;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: clamp(2.8rem, 8vw, 4.4rem);
  line-height: 1;
  letter-spacing: -0.01em;
  color: var(--paper);
  text-shadow: 0 2px 0 rgba(60, 30, 10, 0.5), 0 8px 24px rgba(20, 8, 0, 0.45), 0 0 34px rgba(255, 170, 90, 0.3);
}
.tagline { margin: 10px 0 12px; font-size: 1.05rem; color: rgba(246, 234, 210, 0.85); }
.date-pill {
  display: inline-block;
  padding: 4px 14px;
  border-radius: 999px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: var(--paper);
  box-shadow: 0 3px 10px rgba(20, 8, 0, 0.35);
  transform: rotate(-1deg);
}

.levels {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(16px, 3vw, 32px);
  width: min(920px, 100%);
  margin: clamp(24px, 5vh, 48px) 0 16px;
}

/* Une card = une boîte de puzzle posée sur la table. */
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 0;
  border: 0;
  border-radius: 12px;
  overflow: hidden;
  text-align: left;
  color: var(--ink);
  background: var(--card);
  box-shadow:
    inset 0 0 0 1px rgba(120, 80, 40, 0.25),
    inset 0 1px 0 rgba(255, 236, 200, 0.6),
    0 3px 0 #c8b185,
    0 0 26px -8px rgba(255, 166, 80, 0.35),
    0 18px 28px -12px rgba(15, 6, 0, 0.7);
  transform: rotate(var(--tilt, 0deg));
  cursor: pointer;
  transition: transform 0.3s cubic-bezier(.2, .8, .3, 1.2), box-shadow 0.3s;
}
.card:hover:not(:disabled),
.card:focus-visible {
  transform: rotate(0deg) translateY(-6px);
  box-shadow: inset 0 0 0 1px rgba(120, 80, 40, 0.25), 0 3px 0 #c8b185, 0 26px 34px -12px rgba(15, 6, 0, 0.75);
}
.card:disabled { cursor: progress; }

.card-art {
  position: relative;
  display: block;
  aspect-ratio: 3 / 2;
  overflow: hidden;
  background: var(--photo-color);
}
.card-photo {
  position: absolute;
  inset: -12px;
  background: var(--thumb, linear-gradient(135deg, #c99a63, #7d9a6b)) center / cover;
  filter: blur(5px) saturate(0.85) sepia(0.2);
  transition: filter 0.4s;
}
.card:hover .card-photo { filter: blur(3px) saturate(0.95) sepia(0.1); }
.card-grid {
  position: absolute;
  inset: 0;
  background: var(--piece-grid) 0 0 / var(--grid) auto repeat;
  mix-blend-mode: soft-light;
  opacity: 0.9;
}
.card-body { display: grid; gap: 2px; padding: 12px 16px 14px; }
.card-title { font-family: var(--font-title); font-weight: 700; font-size: 1.55rem; line-height: 1.15; }
.card-meta { display: flex; justify-content: space-between; align-items: center; color: var(--ink-soft); font-size: 0.95rem; }
.card-dots { color: var(--honey-dark); letter-spacing: 2px; font-size: 0.8rem; }
.card-status {
  justify-self: start;
  margin-top: 8px;
  padding: 3px 12px;
  border-radius: 999px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #fff8ec;
  background: var(--honey-dark);
}
.card.is-done .card-status { background: var(--sage); }
.card.is-loading .card-status { background: var(--ink-soft); }

.home-status { position: relative; z-index: 1; min-height: 1.5em; margin: 0 0 8px; text-align: center; }

.credits {
  position: relative;
  z-index: 1;
  margin-top: auto;
  padding-top: 16px;
  max-width: 640px;
  text-align: center;
  font-size: 0.85rem;
  color: rgba(246, 234, 210, 0.78);
}
.credits p { margin: 4px 0; }
.credits summary { cursor: pointer; }

/* --- Partie ----------------------------------------------------------------- */

.game {
  position: fixed;
  inset: 0;
  z-index: 2;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  transform-origin: 0 0;
  overflow: hidden;
}

.hud,
.frame-wood,
.game-credit { position: relative; z-index: 1; }

.hud {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
}
.hud-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 1.3rem;
}
.hud-timer {
  margin-left: auto;
  padding: 4px 14px;
  border-radius: 999px;
  font-size: 1.35rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  background: rgba(30, 15, 5, 0.38);
  box-shadow: inset 0 1px 0 rgba(255, 230, 200, 0.15);
}

.frame-wood {
  width: 100%;
  max-width: 1440px;
  min-height: 0;
  margin: 0 auto;
  padding: clamp(8px, 1.4vw, 18px);
  border-radius: 14px;
  background:
    radial-gradient(ellipse 35% 45% at 0% 20%, rgba(255, 176, 96, 0.28), transparent 70%),
    radial-gradient(ellipse 35% 45% at 100% 85%, rgba(255, 170, 90, 0.26), transparent 70%),
    var(--grain),
    linear-gradient(135deg, var(--wood-light), #6b3f20 55%, #8a5530);
  box-shadow:
    inset 0 1px 0 rgba(255, 210, 160, 0.4),
    inset 0 -2px 0 rgba(0, 0, 0, 0.35),
    0 0 40px -6px rgba(255, 160, 70, 0.18),
    0 22px 40px -14px rgba(10, 4, 0, 0.8);
}

.felt {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  overflow: hidden;
  background:
    var(--fuzz),
    radial-gradient(ellipse 40% 50% at 0% 25%, rgba(255, 170, 80, 0.2), transparent 70%),
    radial-gradient(ellipse 40% 50% at 100% 80%, rgba(255, 170, 80, 0.2), transparent 70%),
    radial-gradient(ellipse 90% 80% at 50% 45%, var(--felt-light), var(--felt) 55%, var(--felt-dark));
  box-shadow: inset 0 3px 14px rgba(0, 0, 0, 0.55), inset 0 0 0 1px rgba(0, 0, 0, 0.45);
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.countdown {
  position: absolute;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 128px;
  height: 128px;
  margin: -64px 0 0 -64px;
  border-radius: 50%;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 4.5rem;
  line-height: 1;
  color: var(--paper);
  background: rgba(30, 15, 5, 0.55);
  box-shadow: 0 0 0 6px rgba(246, 234, 210, 0.15), 0 12px 30px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
.countdown.pop { animation: pop 0.9s ease-out both; }

.game-credit { margin: 0; text-align: center; font-size: 0.8rem; color: rgba(246, 234, 210, 0.75); }

/* --- Victoire --------------------------------------------------------------- */

.win {
  width: min(420px, calc(100vw - 32px));
  padding: 20px 22px 22px;
  border: 0;
  border-radius: 18px;
  color: var(--ink);
  text-align: center;
  background:
    radial-gradient(ellipse at 50% 0%, #fffaf0, transparent 70%),
    var(--paper);
  box-shadow: 0 30px 60px -20px rgba(10, 4, 0, 0.8), inset 0 0 0 1px rgba(120, 80, 40, 0.2);
}
.win[open] { animation: rise 0.45s cubic-bezier(.2, .9, .3, 1.15) both; }
.win::backdrop { background: rgba(25, 12, 2, 0.55); backdrop-filter: blur(2px); }
.win-photo {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  border-radius: 10px;
  box-shadow: 0 6px 16px rgba(40, 20, 5, 0.3);
  transform: rotate(-1.2deg);
  background: var(--photo-color);
}
.win h2 { margin: 16px 0 4px; font-family: var(--font-title); font-size: 2rem; line-height: 1.1; }
.win p { margin: 6px 0; }
.win-lead strong { font-variant-numeric: tabular-nums; font-size: 1.15em; }
.win-note { font-size: 0.9rem; color: var(--ink-soft); }
.win-word strong {
  display: inline-block;
  padding: 0 10px;
  border-radius: 6px;
  font-family: var(--font-title);
  font-size: 1.2em;
  background: rgba(227, 161, 79, 0.25);
}
.win-credit { font-size: 0.85rem; color: var(--ink-soft); }
.win-other {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed rgba(120, 80, 40, 0.3);
}
.win-other p { font-size: 0.95rem; font-weight: 600; }
.win-levels { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 8px; }
.btn-level {
  background: transparent;
  box-shadow: inset 0 0 0 2px rgba(181, 101, 42, 0.55);
}
.btn-level:hover { background: rgba(227, 161, 79, 0.18); }
.win-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 16px; }

/* --- Publicité ------------------------------------------------------------- */

.ad {
  width: min(400px, calc(100vw - 32px));
  padding: 18px 20px 20px;
  border: 0;
  border-radius: 18px;
  color: var(--ink);
  text-align: center;
  background: var(--paper);
  box-shadow: 0 30px 60px -20px rgba(10, 4, 0, 0.8), inset 0 0 0 1px rgba(120, 80, 40, 0.2);
}
.ad[open] { animation: rise 0.35s cubic-bezier(.2, .9, .3, 1.15) both; }
.ad:focus { outline: none; }
.ad::backdrop { background: rgba(25, 12, 2, 0.6); backdrop-filter: blur(2px); }
.ad-title { margin: 0 0 12px; font-family: var(--font-title); font-weight: 700; font-size: 1.15rem; }
.ad-slot {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 250px;
  overflow: hidden;
  border-radius: 10px;
}
.ad-slot.is-placeholder {
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  background: repeating-linear-gradient(135deg, rgba(120, 80, 40, 0.05) 0 12px, transparent 12px 24px);
  box-shadow: inset 0 0 0 2px rgba(120, 80, 40, 0.18);
}
.ad-progress {
  height: 4px;
  margin: 14px 0;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(120, 80, 40, 0.15);
}
.ad-progress span { display: block; width: 0; height: 100%; background: var(--honey-dark); }
.ad-progress span.is-running { animation: ad-fill linear forwards; }
.ad button:disabled { opacity: 0.6; cursor: default; font-variant-numeric: tabular-nums; }

@keyframes ad-fill { to { width: 100%; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 10;
  max-width: calc(100vw - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  color: var(--paper);
  background: rgba(30, 15, 5, 0.9);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

/* --- Animations ------------------------------------------------------------- */

@keyframes pop {
  0% { transform: scale(0.4); opacity: 0; }
  25% { transform: scale(1.08); opacity: 1; }
  40% { transform: scale(1); }
  85% { opacity: 1; }
  100% { transform: scale(0.92); opacity: 0; }
}
@keyframes rise {
  from { transform: translateY(24px) scale(0.96); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes settle {
  from { transform: translateY(-18px) rotate(-25deg); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* Vacillement des flammes : irrégulier, jamais en cadence entre les deux côtés. */
@keyframes flicker-a {
  0%, 100% { opacity: 0.86; }
  13% { opacity: 0.96; }
  21% { opacity: 0.88; }
  38% { opacity: 1; }
  52% { opacity: 0.9; }
  64% { opacity: 0.97; }
  79% { opacity: 0.85; }
  90% { opacity: 0.94; }
}
@keyframes flicker-b {
  0%, 100% { opacity: 0.92; }
  9% { opacity: 0.84; }
  27% { opacity: 0.98; }
  41% { opacity: 0.89; }
  58% { opacity: 1; }
  71% { opacity: 0.87; }
  86% { opacity: 0.95; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* --- Responsive ------------------------------------------------------------- */

@media (max-width: 900px) {
  .decor-tea { width: 190px; right: -50px; top: 80px; }
  .decor-crumbs { top: 270px; right: 40px; width: 80px; }
  .decor-box { width: 280px; left: -90px; }
  .decor-pieces { width: 130px; }
}

@media (max-width: 680px) {
  .home { padding-top: max(20px, env(safe-area-inset-top)); }
  .decor-tea { width: 150px; top: auto; right: -64px; bottom: 90px; opacity: 0.9; }
  .decor .decor-crumbs, .decor .decor-pieces { display: none; }
  .decor-box { width: 220px; left: -100px; bottom: -40px; opacity: 0.85; }
  .logo { width: 64px; height: 64px; }
  .levels { grid-template-columns: 1fr; gap: 14px; margin-top: 22px; max-width: 460px; }
  .card { flex-direction: row; min-height: 112px; }
  .card-art { aspect-ratio: auto; width: 38%; flex: none; }
  .card-body { flex: 1; align-content: center; padding: 10px 14px; }
  .card-title { font-size: 1.3rem; }
  .hud { gap: 8px; }
  .hud-brand { font-size: 1.1rem; }
  .hud-timer { font-size: 1.15rem; }
}
