/* Jeu pensé pour le portrait mobile : tout tient dans la hauteur de l'écran,
   sans scroll, et le plateau occupe la largeur disponible. */

* { box-sizing: border-box; }

:root {
  --bg: #1e1b2e;
  --panel: #2a2640;
  --text: #f3f0ff;
  --muted: #a79fc9;
  --accent: #ffb454;
  --grid-line: #2a2640;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  gap: 12px;
  /* pas de zoom accidentel ni de sélection au double tap */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  /* L'accueil peut dépasser sur petit écran : on autorise le défilement
     vertical, jamais l'horizontal. */
  overflow-x: hidden;
  overflow-y: auto;
}

h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0.5px;
}

/* --- Écrans : un seul visible à la fois, pas de rechargement --- */

.screen {
  display: none;
  width: 100%;
  max-width: 520px;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.screen.active {
  display: flex;
}

/* --- Chargement --- */

.loader {
  margin-top: 30vh;
  text-align: center;
  color: var(--muted);
}

.loader-cat {
  font-size: 3rem;
  display: block;
  animation: hint-pulse 1.4s ease-in-out infinite;
}

/* --- Connexion --- */

.login-box {
  margin-top: 18vh;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-intro {
  color: var(--muted);
  margin: 0;
  font-size: 0.9rem;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#login-code {
  text-align: center;
  font-size: 1.3rem;
  letter-spacing: 3px;
  padding: 14px;
}

.login-error {
  color: #ff7a8a;
  font-size: 0.85rem;
  min-height: 1.2rem;
  margin: 0;
}

/* --- Accueil --- */

.home-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.greeting {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 6px;
}

.stats-row {
  width: 100%;
  display: flex;
  gap: 8px;
}

.stat {
  flex: 1;
  background: var(--panel);
  border-radius: 12px;
  padding: 12px 6px;
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
}

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* Boutons de mode : la cible principale, donc généreuse au doigt. */
.mode-btn {
  width: 100%;
  background: var(--panel);
  border: 1px solid #3b3557;
  border-radius: 16px;
  padding: 18px;
  color: var(--text);
  cursor: pointer;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 2px 14px;
  align-items: center;
  text-align: left;
}

.mode-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.mode-icon {
  grid-row: 1 / 3;
  font-size: 2rem;
}

.mode-title {
  font-size: 1.05rem;
  font-weight: 700;
}

.mode-sub {
  font-size: 0.8rem;
  color: var(--muted);
}

.mode-daily:not(:disabled) {
  border-color: var(--accent);
}

.home-note {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
}

/* --- En-tête de partie --- */

.game-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
}

.game-title {
  font-size: 0.9rem;
  color: var(--muted);
}

.game-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.score-summary {
  margin: 0;
  text-align: center;
  color: var(--accent);
  font-weight: 700;
}

.hud {
  width: 100%;
  max-width: 520px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 14px;
}

.hud-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.hud-value {
  font-size: 1rem;
  color: var(--text);
  letter-spacing: 0;
  text-transform: none;
}

#lives { font-size: 1.05rem; letter-spacing: 1px; }

#seed {
  cursor: pointer;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  color: var(--accent);
  position: relative;
}

.hud-sub {
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.5px;
}

#seed.copied::after {
  content: 'copié !';
  position: absolute;
  left: 0;
  top: 110%;
  font-size: 0.7rem;
  color: var(--muted);
}

/* --- Plateau --- */

#board {
  --size: 8;
  /* --cell (largeur réelle d'une case, en px) est posé par le JS après
     la mise en page : les tailles de texte en dépendent, et un calc()
     à base de 100% ne se résout pas de façon fiable pour font-size. */
  --cell: 40px;
  /* la marge réservée tient compte du HUD, du message d'indice
     (qui peut faire deux lignes), des boutons et de l'aide */
  width: min(100%, 520px, calc(100vh - 330px));
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  grid-template-rows: repeat(var(--size), 1fr);
  gap: 0;
  background: var(--grid-line);
  border: 3px solid var(--grid-line);
  border-radius: 10px;
  overflow: hidden;
  /* le glissement peint des X : il ne doit pas faire défiler la page */
  touch-action: none;
}

.cell {
  border: 1px solid rgba(0, 0, 0, 0.18);
  padding: 0;
  margin: 0;
  /* --cell est la largeur réelle d'une case : le texte suit donc la
     taille du plateau, qu'il soit en 8x8 ou en 10x10. */
  font-size: calc(var(--cell) * 0.62);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #2b2440;
  font-weight: 700;
  transition: filter 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}

.cell:active { filter: brightness(0.9); }

/* Frontières de zone : trait épais et sombre */
.cell.edge-top    { border-top-color: #1b1830;    border-top-width: 3px; }
.cell.edge-bottom { border-bottom-color: #1b1830; border-bottom-width: 3px; }
.cell.edge-left   { border-left-color: #1b1830;   border-left-width: 3px; }
.cell.edge-right  { border-right-color: #1b1830;  border-right-width: 3px; }

/* X d'aide : bien lisible sur les pastels, mais clairement distinct
   du X rouge d'erreur (couleur + épaisseur). */
/* Les croix sont dessinées en texte simple : épaisseur obtenue par la
   taille du glyphe, sans text-stroke (rendu trop variable selon la
   police qui fournit réellement le caractère). */
.cell.is-marked {
  color: #14101f;
  font-size: calc(var(--cell) * 0.66);
  font-weight: 900;
  opacity: 0.9;
}

.cell.is-wrong {
  color: #b8071c;
  font-size: calc(var(--cell) * 0.72);
  font-weight: 900;
  text-shadow:
     0 1px 0 rgba(255, 255, 255, 0.8),
     0 0 3px rgba(255, 255, 255, 0.9);
}

.cell.is-cat, .cell.revealed { filter: saturate(1.15); }
.cell.revealed { opacity: 0.75; }

body.finished .cell { cursor: default; }

/* Génération en cours : vérifier l'unicité prend un court instant. */
body.loading #board { opacity: 0.35; }
body.loading .message { color: var(--muted); }

/* --- Animations de retour --- */

@keyframes pop {
  0%   { transform: scale(0.5); }
  60%  { transform: scale(1.18); }
  100% { transform: scale(1); }
}
.cell.pop { animation: pop 0.28s ease; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-3px); }
  75%      { transform: translateX(3px); }
}
.cell.shake { animation: shake 0.22s ease; }

/* --- Bas de page --- */

.controls {
  width: 100%;
  max-width: 520px;
  display: flex;
  gap: 8px;
}

input[type="text"] {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid #3b3557;
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-family: ui-monospace, Consolas, monospace;
}

input[type="text"]::placeholder { color: var(--muted); }

button.action {
  background: var(--accent);
  color: #2b2440;
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

button.action.secondary {
  background: var(--panel);
  color: var(--text);
  border: 1px solid #3b3557;
}

/* Case désignée par un indice : elle pulse jusqu'à ce qu'on y touche. */
.cell.hinted {
  box-shadow: inset 0 0 0 3px var(--accent);
  animation: hint-pulse 1.1s ease-in-out infinite;
}

@keyframes hint-pulse {
  0%, 100% { box-shadow: inset 0 0 0 3px var(--accent); }
  50%      { box-shadow: inset 0 0 0 5px #fff0c9; }
}

.hint-btn {
  width: 100%;
  max-width: 520px;
  background: var(--panel);
  color: var(--accent);
  border: 1px solid var(--accent);
}

.hint-btn:disabled {
  opacity: 0.4;
  cursor: default;
  border-color: #3b3557;
  color: var(--muted);
}

.message.hint-text {
  color: var(--accent);
  font-weight: 600;
  line-height: 1.35;
}

.message {
  min-height: 1.2rem;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  max-width: 520px;
  margin: 0;
}
.message.win  { color: #7ee08a; }
.message.lose { color: #ff7a8a; }

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  max-width: 520px;
  line-height: 1.4;
}

/* =====================================================================
   Classement
   ===================================================================== */

.link-btn.wide {
  width: 100%;
  padding: 12px;
  border: 1px solid #3b3557;
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
}

/* --- Podium --- */

.podium {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 8px;
  margin: 8px 0 4px;
  min-height: 190px;
}

.podium-step {
  flex: 1;
  max-width: 108px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  animation: podium-rise 0.5s cubic-bezier(0.2, 1.1, 0.4, 1) both;
  animation-delay: var(--delay, 0s);
}

@keyframes podium-rise {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

.podium-medal { font-size: 1.7rem; line-height: 1; }

.podium-name {
  font-size: 0.85rem;
  font-weight: 700;
  text-align: center;
  /* les prénoms longs ne doivent pas déformer le podium */
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.podium-score {
  font-size: 0.75rem;
  color: var(--accent);
  margin-bottom: 4px;
}

/* La marche : sa hauteur porte le classement, d'où les trois tailles. */
.podium-block {
  width: 100%;
  border-radius: 10px 10px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 800;
  color: #2b2440;
}

.place-1 .podium-block { height: 96px; background: linear-gradient(#ffd479, #f0a93c); }
.place-2 .podium-block { height: 68px; background: linear-gradient(#dfe3ec, #aeb4c4); }
.place-3 .podium-block { height: 52px; background: linear-gradient(#e0a877, #c07f4a); }

.podium-step.is-me .podium-name { color: var(--accent); }
.podium-step.is-me .podium-block {
  box-shadow: inset 0 0 0 3px var(--text);
}

/* --- Suite du classement --- */

.ranking {
  width: 100%;
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rank-row {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  gap: 0 10px;
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 12px;
}

.rank-position {
  grid-row: 1 / 3;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
}

.rank-name { font-size: 0.95rem; font-weight: 600; }

.rank-detail {
  grid-column: 2;
  font-size: 0.72rem;
  color: var(--muted);
}

.rank-score {
  grid-row: 1 / 3;
  font-weight: 700;
  color: var(--accent);
  font-size: 0.9rem;
}

/* Sa propre ligne, mise en évidence : c'est la première qu'on cherche. */
.rank-row.is-me {
  background: #332c52;
  box-shadow: inset 0 0 0 1px var(--accent);
}
.rank-row.is-me .rank-name { color: var(--accent); }

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

/* =====================================================================
   Écran de fin de partie
   ===================================================================== */

.end-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(18, 15, 30, 0.82);
  backdrop-filter: blur(3px);
  opacity: 0;
  transition: opacity 0.25s ease;
}

.end-overlay.visible { opacity: 1; }

.end-card {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 340px;
  background: var(--panel);
  border-radius: 20px;
  padding: 24px 20px 20px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  transform: translateY(14px) scale(0.94);
  transition: transform 0.35s cubic-bezier(0.2, 1.1, 0.35, 1);
}

.end-overlay.visible .end-card {
  transform: translateY(0) scale(1);
}

.end-title {
  margin: 10px 0 0;
  font-size: 1.25rem;
}

.end-detail {
  margin: 8px 0 0;
  color: var(--accent);
  font-weight: 700;
  font-size: 0.95rem;
}

/* Les boutons n'apparaissent qu'après la célébration : cliquer trop tôt
   donnerait l'impression d'avoir raté l'animation. */
.end-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 18px;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}

.end-overlay.actions-ready .end-actions {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/* --- Le chat --- */

.end-cat {
  display: flex;
  justify-content: center;
}

.cat-svg {
  width: 130px;
  height: 130px;
}

/* Les yeux et la bouche tombent sur le masque BLANC du museau : le trait
   doit donc être sombre, sinon il disparaît. */
.cat-eye-line, .cat-mouth {
  fill: none;
  stroke: #26232b;
  stroke-width: 3;
  stroke-linecap: round;
}

.cat-brow {
  fill: none;
  stroke: #26232b;
  stroke-width: 2;
  stroke-linecap: round;
  opacity: 0.75;
}

/* La larme perle, glisse le long de la joue, puis se reforme. */
.cat-tear {
  animation: tear-fall 3.4s ease-in 1s infinite;
  transform-origin: 43px 57px;
}

@keyframes tear-fall {
  0%       { transform: translateY(0) scale(0); opacity: 0; }
  12%      { transform: translateY(0) scale(1); opacity: 0.95; }
  20%      { transform: translateY(2px) scale(1); opacity: 0.95; }
  55%      { transform: translateY(16px) scale(0.9); opacity: 0.9; }
  75%      { transform: translateY(26px) scale(0.6); opacity: 0; }
  100%     { transform: translateY(26px) scale(0); opacity: 0; }
}

/* Victoire : le chat surgit, puis respire. */
.cat-happy {
  animation: cat-pop 0.5s cubic-bezier(0.2, 1.4, 0.4, 1) both,
             cat-breathe 2.6s ease-in-out 0.5s infinite;
  transform-origin: 50% 88%;
}

/* Défaite : arrivée discrète, affaissement. */
.cat-sad {
  animation: cat-slump 0.6s ease-out both;
  transform-origin: 50% 88%;
}

@keyframes cat-pop {
  0%   { transform: scale(0.2) translateY(30px) rotate(-8deg); opacity: 0; }
  55%  { transform: scale(1.14) translateY(-7px) rotate(3deg); opacity: 1; }
  75%  { transform: scale(0.97) translateY(2px) rotate(-1deg); }
  100% { transform: scale(1) translateY(0) rotate(0); }
}

/* Respiration : un très léger balancement, pas un simple zoom —
   un chat assis ne grossit pas, il oscille. */
@keyframes cat-breathe {
  0%, 100% { transform: scale(1) rotate(0deg); }
  30%      { transform: scale(1.025) rotate(-0.8deg); }
  70%      { transform: scale(1.015) rotate(0.8deg); }
}

@keyframes cat-slump {
  0%   { transform: scale(0.8) translateY(-10px); opacity: 0; }
  70%  { transform: scale(1.02) translateY(3px); opacity: 1; }
  100% { transform: scale(1) translateY(0); }
}

/* --- La queue --- */

/* Battement irrégulier : une queue de chat ne fait pas un métronome.
   Les paliers inégaux donnent ce côté vivant. */
.cat-happy .cat-tail {
  transform-origin: 86px 96px;
  animation: tail-wag 1.5s ease-in-out infinite;
}

@keyframes tail-wag {
  0%   { transform: rotate(-14deg); }
  18%  { transform: rotate(10deg); }
  32%  { transform: rotate(-6deg); }
  50%  { transform: rotate(16deg); }
  68%  { transform: rotate(-12deg); }
  84%  { transform: rotate(6deg); }
  100% { transform: rotate(-14deg); }
}

/* Défaite : la queue pend, avec un balancement lent. */
.cat-sad .cat-tail {
  transform-origin: 86px 96px;
  animation: tail-droop 3.2s ease-in-out infinite;
}

@keyframes tail-droop {
  0%, 100% { transform: rotate(6deg); }
  50%      { transform: rotate(-4deg); }
}

/* --- Détails vivants --- */

.cat-happy .cat-whiskers {
  animation: whiskers 2.2s ease-in-out infinite;
  transform-origin: 56px 60px;
}

@keyframes whiskers {
  0%, 100% { transform: rotate(0deg) scaleX(1); }
  40%      { transform: rotate(1.5deg) scaleX(1.03); }
  70%      { transform: rotate(-1deg) scaleX(0.99); }
}

/* Les oreilles frémissent, par à-coups brefs et espacés. */
.cat-svg .cat-ear-l,
.cat-svg .cat-ear-r {
  animation: ear-twitch 4.5s ease-in-out infinite;
}
.cat-svg .cat-ear-l { transform-origin: 38px 33px; }
.cat-svg .cat-ear-r { transform-origin: 74px 33px; animation-delay: 0.4s; }

@keyframes ear-twitch {
  0%, 88%, 100% { transform: rotate(0deg); }
  91%           { transform: rotate(-7deg); }
  94%           { transform: rotate(4deg); }
  97%           { transform: rotate(-2deg); }
}

/* Clignement : rare et rapide, comme un vrai chat. Le groupe entier est
   écrasé verticalement — paupières et sourcils compris, ce qui donne un
   vrai battement plutôt que des iris qui rétrécissent. */
.cat-sad .cat-eyes {
  animation: blink 5.5s ease-in-out infinite;
  transform-origin: 56px 47px;
}

@keyframes blink {
  0%, 90%, 100% { transform: scaleY(1); }
  93%           { transform: scaleY(0.15); }
  96%           { transform: scaleY(1); }
}

/* --- Feux d'artifice --- */

/* Au-dessus de la carte (z-index 2) : sinon les particules explosent
   derrière un panneau opaque et restent invisibles. */
.fireworks {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
}

.firework-particle {
  position: absolute;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  /* halo : les particules restent lisibles même sur la carte claire */
  box-shadow: 0 0 8px 2px currentColor;
  /* --dx / --dy : trajectoire, posée en JS */
  animation: firework 1.1s ease-out forwards;
}

@keyframes firework {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }
  100% {
    /* la gravité fait retomber légèrement les particules */
    transform: translate(var(--dx), calc(var(--dy) + 30px)) scale(0.3);
    opacity: 0;
  }
}

/* Certaines personnes règlent leur système pour éviter les animations :
   on affiche alors le résultat sans mouvement. */
@media (prefers-reduced-motion: reduce) {
  .cat-happy, .cat-sad,
  .cat-happy .cat-tail, .cat-sad .cat-tail,
  .cat-happy .cat-whiskers,
  .cat-svg .cat-ear-l, .cat-svg .cat-ear-r,
  .cat-sad .cat-eyes,
  .firework-particle {
    animation: none;
  }
  .end-card { transition: none; transform: none; }
}
