:root {
  --paper: #fbfaf7;
  --ink: #4a453c;
  --muted: #8a8172;
  --grid: #cfc8ba;
  --outline: #b7ae9c;
  --track: #e8e3d8;
  --accent: #5cc98a;
  --card: #ffffff;
  --hover: #efeae0;
}

/* Thème sombre : soit choisi explicitement, soit le réglage système quand le
   joueur laisse « auto » (pas d'attribut data-theme). Les couleurs des pièces
   restent vives ; seuls le fond, le texte et la grille changent. */
:root[data-theme="dark"] {
  --paper: #1c1b18;
  --ink: #ece7dd;
  --muted: #9a9284;
  --grid: #3a382f;
  --outline: #55503f;
  --track: #2e2c26;
  --card: #262420;
  --hover: #322f28;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1c1b18;
    --ink: #ece7dd;
    --muted: #9a9284;
    --grid: #3a382f;
    --outline: #55503f;
    --track: #2e2c26;
    --card: #262420;
    --hover: #322f28;
  }
}

* { box-sizing: border-box; }

/* Une règle de classe qui pose `display` écrase le `display:none` par défaut de
   l'attribut `hidden`. Sans ce garde, `.victory` (display:flex) reste visible
   même masqué et voile toute la page. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 4px 4px;
  gap: 8px;
}

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }

.logo { width: 30px; height: 27px; flex: 0 0 auto; }

.brand-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }

.brand-name {
  font-weight: 800;
  font-size: 17px;
  letter-spacing: 0.02em;
  color: var(--ink);
}

.level { font-size: 12px; font-weight: 600; color: var(--muted); }

.solutions { font-size: 11px; font-weight: 600; color: var(--accent); }

.actions { display: flex; gap: 2px; flex: 0 0 auto; }

.actions button {
  border: 0;
  background: transparent;
  font-size: 20px;
  line-height: 1;
  padding: 6px 8px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--ink);
}

.actions button:hover { background: var(--track); }
.actions button:disabled { opacity: 0.3; cursor: default; background: transparent; }

/* Compteur d'indices, en pastille sur le bouton 💡. */
#hint { position: relative; }
#hint::after {
  content: attr(data-count);
  position: absolute;
  top: 0;
  right: 0;
  min-width: 15px;
  height: 15px;
  padding: 0 3px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 15px;
  text-align: center;
}
#hint.empty::after { background: var(--muted); }

.progress { padding: 4px 4px 10px; }

.progress-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}

.progress-track {
  position: relative;
  height: 6px;
  border-radius: 99px;
  background: var(--track);
}

.progress-fill {
  height: 100%;
  width: 0;
  border-radius: 99px;
  background: linear-gradient(90deg, #7ad4a0, var(--accent));
  transition: width 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}

/* Le marqueur suit le remplissage et change de visage : le plateau prend vie. */
.progress-marker {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translate(-50%, -50%);
  font-size: 17px;
  line-height: 1;
  transition: left 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.15));
  pointer-events: none;
}

.board-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 4px 0;
}

#board { width: 100%; height: 100%; touch-action: none; }

.cell { fill: none; stroke: var(--grid); stroke-width: 1; }
.board-outline { fill: none; stroke: var(--outline); stroke-width: 2.5; stroke-linejoin: round; }

/* Le liseré entre triangles d'une même pièce est de la couleur du fond : les
   triangles se lisent comme un bloc tout en gardant leur découpe visible. */
.piece-cell { stroke: var(--paper); stroke-width: 3; stroke-linejoin: round; }

/* Contour de chaque pièce : la délimite nettement de ses voisines. */
.piece-outline {
  fill: none;
  stroke: rgba(24, 23, 20, 0.8);
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
:root[data-theme="dark"] .piece-outline { stroke: rgba(0, 0, 0, 0.78); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .piece-outline { stroke: rgba(0, 0, 0, 0.78); }
}

.piece-symbol {
  fill: rgba(28, 27, 24, 0.72);
  text-anchor: middle;
  dominant-baseline: central;
  font-weight: 800;
  font-family: system-ui, sans-serif;
  pointer-events: none;
}

.ghost { opacity: 0.35; pointer-events: none; }

.placed { cursor: grab; }

/* Petit « pop » élastique sur la pièce qu'on vient de poser : le geste est
   confirmé physiquement, pas seulement visuellement. */
@keyframes pop {
  0% { transform: scale(0.62); opacity: 0.4; }
  60% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); }
}

.just-placed { animation: pop 0.34s cubic-bezier(0.34, 1.56, 0.64, 1); }

@media (prefers-reduced-motion: reduce) {
  .just-placed { animation: none; }
}

.message {
  margin: 0 4px 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: #fdf0ee;
  color: #a4453f;
  font-size: 13px;
  text-align: center;
}

/* Une astuce n'est pas une alerte : même gabarit, couleurs neutres. */
.message.tip { background: #eef6f0; color: #2f6b45; }
.message.tip button { background: #2f6b45; }

.message button {
  border: 0;
  background: #a4453f;
  color: #fff;
  border-radius: 8px;
  padding: 4px 10px;
  margin-left: 8px;
  font-size: 13px;
  cursor: pointer;
}

.tray {
  display: grid;
  gap: 8px;
  align-items: center;
  justify-items: center;
  padding: 10px 4px 14px;
  border-top: 1px solid var(--track);
}

.tray-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 54px;
  width: 100%;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.tray-slot svg { max-width: 100%; max-height: 54px; overflow: visible; }

/* Le liseré de 3px est calibré pour le plateau (arête ~120px). À l'échelle de la
   réserve (arête 26px) il mangerait un cinquième de chaque triangle et la pièce
   se lirait comme des éclats séparés. */
.tray-slot .piece-cell { stroke-width: 1.1; }
.tray-slot.dragging { opacity: 0.25; }

.drag-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.drag-layer svg {
  position: absolute;
  overflow: visible;
  filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.18));
}

/* Confettis : au-dessus de tout, jamais cliquables. */
.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 30;
}

.victory {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  z-index: 20;
  animation: fade-in 0.25s ease;
}

.victory-card {
  background: #fff;
  border-radius: 22px;
  padding: 30px 36px;
  text-align: center;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.14);
  animation: card-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.victory-face { font-size: 54px; line-height: 1; margin-bottom: 8px; }

.victory-card h2 { margin: 0 0 4px; font-size: 26px; }
.victory-card p { margin: 0 0 20px; color: var(--muted); }

.victory-card button {
  border: 0;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  padding: 13px 28px;
  border-radius: 13px;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}

.victory-card button:hover { background: #4bb87a; transform: translateY(-1px); }
.victory-card button:active { transform: translateY(1px); }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@keyframes card-in {
  from { transform: scale(0.8) translateY(10px); opacity: 0; }
  to { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.celebrate { animation: pulse 0.6s ease; transform-origin: center; }

@media (prefers-reduced-motion: reduce) {
  .victory, .victory-card, .celebrate { animation: none; }
}

@media (max-height: 700px) {
  .tray { gap: 6px; padding: 6px 4px 10px; }
  .tray-slot { min-height: 42px; }
  .tray-slot svg { max-height: 42px; }
  .progress { padding: 2px 4px 6px; }
}

/* ==================== Écrans & navigation ==================== */

.screen {
  min-height: 100vh;
  min-height: 100dvh;
}

/* Le jeu occupe exactement l'écran et ne défile pas ; l'accueil et les niveaux
   peuvent défiler si besoin. */
.screen.game {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.screen-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 8px;
  max-width: 560px;
  margin: 0 auto;
}

.screen-title { font-weight: 800; font-size: 18px; }
.header-spacer { flex: 1; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: 24px;
  line-height: 1;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }

/* Boutons génériques */
.btn-primary, .btn-secondary, .btn-danger {
  border: 0;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 700;
  padding: 14px 22px;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #4bb87a; transform: translateY(-1px); }
.btn-secondary { background: var(--track); color: var(--ink); }
.btn-secondary:hover { background: var(--hover); }
.btn-danger { background: #f2726f; color: #fff; }
.btn-primary:active, .btn-secondary:active, .btn-danger:active { transform: translateY(1px); }

/* ==================== Accueil ==================== */

.home { display: flex; align-items: center; justify-content: center; padding: 24px; }
.home-inner { display: flex; flex-direction: column; align-items: center; text-align: center; width: 100%; max-width: 320px; }
.home-logo { width: 84px; height: 76px; margin-bottom: 14px; }
.home-title { font-size: 42px; font-weight: 800; letter-spacing: 0.02em; margin: 0; }
.home-tagline { color: var(--muted); margin: 6px 0 32px; }
.home-actions { display: flex; flex-direction: column; gap: 12px; width: 100%; }
.home-actions .btn-primary { font-size: 18px; padding: 16px; }

/* ==================== Sélection des niveaux ==================== */

.levels-scroll {
  max-width: 560px;
  margin: 0 auto;
  padding: 4px 14px calc(env(safe-area-inset-bottom) + 24px);
}

.chapter { margin-bottom: 22px; }
.chapter-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 6px 2px 10px;
}
.chapter-name { font-weight: 800; font-size: 17px; }
.chapter-count { font-size: 13px; font-weight: 600; color: var(--muted); }

.level-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 9px;
}
@media (max-width: 380px) { .level-grid { grid-template-columns: repeat(5, 1fr); } }

.level-tile {
  aspect-ratio: 1;
  border: 2px solid var(--track);
  background: var(--card);
  color: var(--ink);
  border-radius: 14px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, border-color 0.1s ease;
}
.level-tile:hover:not(.locked) { transform: translateY(-2px); border-color: var(--accent); }
.level-tile.done { background: var(--accent); border-color: var(--accent); color: #fff; }
.level-tile.locked { opacity: 0.45; cursor: default; font-size: 14px; }

/* ==================== Panneau réglages ==================== */

.sheet {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  z-index: 40;
}
.sheet-card {
  background: var(--card);
  width: 100%;
  max-width: 460px;
  border-radius: 20px 20px 0 0;
  padding: 18px 20px calc(env(safe-area-inset-bottom) + 20px);
  animation: sheet-up 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.sheet-head h2 { margin: 0; font-size: 20px; }

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px;
  border-bottom: 1px solid var(--track);
  font-weight: 600;
  cursor: pointer;
}
.setting-row input { width: 20px; height: 20px; accent-color: var(--accent); cursor: pointer; }
.sheet-card .btn-danger { width: 100%; margin-top: 18px; }

/* Victoire : deux boutons empilés */
.victory-actions { display: flex; flex-direction: column; gap: 10px; }
.victory-card { background: var(--card); }
.victory-card h2, .victory-card p { color: var(--ink); }
.victory-card p { color: var(--muted); }

@media (prefers-reduced-motion: reduce) { .sheet-card { animation: none; } }

/* Étoiles sur les tuiles de niveaux */
.level-tile { flex-direction: column; gap: 2px; padding: 4px; }
.tile-num { font-size: 15px; font-weight: 700; }
.tile-stars { display: flex; gap: 0; line-height: 1; }
.tile-stars .pip { font-size: 8px; }
.tile-stars .pip.on { color: #f0a500; }
.tile-stars .pip.off { color: var(--muted); opacity: 0.4; }
.level-tile.done .tile-stars .pip.on { color: #ffd66b; }
.level-tile.done .tile-stars .pip.off { color: rgba(255, 255, 255, 0.55); opacity: 1; }

/* Le compteur d'étoiles ouvre la galerie */
.solutions { cursor: pointer; user-select: none; }

/* Galerie des solutions */
.gallery-hint { color: var(--muted); font-size: 13px; margin: 2px 4px 12px; }
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 12px;
  max-height: 52vh;
  overflow-y: auto;
  padding: 2px;
}
.gallery-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  border-radius: 12px;
  background: var(--track);
}
.gallery-cell.found { background: color-mix(in srgb, var(--accent) 16%, var(--card)); }
.mini-board { width: 100%; height: auto; }
.mini-board .cell { stroke: var(--grid); stroke-width: 0.8; }
.mini-board.locked { opacity: 0.5; }
.mini-board .piece-cell { stroke: var(--card); stroke-width: 1; }
.gallery-badge {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 12px;
}
.gallery-cell.found .gallery-badge { color: #f0a500; }
.gallery-stars { font-size: 15px; font-weight: 700; color: var(--muted); margin-left: 4px; }
