:root {
  --bg: #0f4d3a;
  --bg-soft: #12593f;
  --surface: #ffffff;
  --surface-alt: #f4f6f5;
  --ink: #17251f;
  --ink-soft: #5b6b62;
  --accent: #0f9d58;
  --accent-dark: #0b7a44;
  --danger: #d64545;
  --border: #e1e6e3;
  --radius: 14px;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  color-scheme: light;
}

* { box-sizing: border-box; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(160deg, var(--bg), var(--bg-soft));
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: contain;
}

button {
  font-family: inherit;
}

.hidden { display: none !important; }

/* ---------- Écrans génériques ---------- */
.screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}

#screen-home, #screen-lobby {
  justify-content: center;
}

.card-panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px;
  width: 100%;
  max-width: 420px;
  margin: 16px 0;
}

h1 {
  margin: 0 0 4px;
  font-size: 1.7rem;
  text-align: center;
}

.subtitle {
  text-align: center;
  color: var(--ink-soft);
  margin: 0 0 24px;
  font-size: 0.95rem;
}

h2 { margin-top: 0; text-align: center; }
h3 { margin-bottom: 8px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.field input {
  font-size: 1.05rem;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  color: var(--ink);
  background: var(--surface-alt);
}

.field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Case à cocher inline (Mode Bénéficiaire AAH, voir
   /public/shared/AAH_MODE.md — motif dupliqué depuis les autres jeux de la
   plateforme, chacun restant autonome pour son propre CSS). */
.field-inline { display: flex; align-items: center; gap: 10px; font-size: 0.85rem; color: var(--ink-soft); margin: 12px 0; }
.field-inline input { width: 20px; height: 20px; flex-shrink: 0; }

#input-code { text-transform: uppercase; letter-spacing: 3px; font-weight: 600; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: 10px;
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: transform 0.05s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--surface-alt); color: var(--ink); border: 1.5px solid var(--border); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-small { padding: 9px 14px; font-size: 0.85rem; width: auto; }

.home-actions { display: flex; flex-direction: column; gap: 10px; }
.join-panel { margin-top: 16px; display: flex; flex-direction: column; gap: 10px; }

.error-text { color: var(--danger); font-size: 0.9rem; margin-top: 14px; text-align: center; }
.hint-text { color: var(--ink-soft); font-size: 0.85rem; text-align: center; min-height: 1.2em; }

/* ---------- Salon ---------- */
.room-code-label { text-align: center; color: var(--ink-soft); margin: 6px 0 2px; font-size: 0.85rem; }
.room-code {
  text-align: center;
  font-size: 2.4rem;
  font-weight: 700;
  letter-spacing: 8px;
  color: var(--accent-dark);
  margin-bottom: 14px;
}
.players-title { margin-top: 20px; }
.players-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.players-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface-alt);
  border-radius: 10px;
  font-size: 0.95rem;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); flex: none; }
.dot.off { background: #c9c9c9; }
.tag-host { font-size: 0.7rem; background: var(--accent); color: white; padding: 2px 7px; border-radius: 20px; margin-left: auto; }
.lobby-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 22px; }

/* ---------- Jeu ---------- */
#screen-game {
  padding: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: stretch; /* .screen impose align-items:center, il faut l'annuler ici :
    sinon tous les enfants (dont la main) se réduisent à leur contenu et se
    centrent au lieu de remplir toute la largeur de l'écran. */
  color: white;
}

.game-top {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 8px;
  font-size: 0.9rem;
}
.game-room-code { font-weight: 700; letter-spacing: 2px; opacity: 0.85; }
.game-direction { font-size: 1.2rem; }
.game-turn-indicator {
  margin-left: auto;
  background: rgba(255,255,255,0.15);
  padding: 6px 12px;
  border-radius: 20px;
  font-weight: 600;
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.game-turn-indicator.my-turn { background: #ffd54a; color: #3a2c00; }

.icon-btn {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.15);
  color: white;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.icon-btn:active { background: rgba(255,255,255,0.3); }
.icon-btn.active { background: #ffd54a; }

/* Bouton retour du salon d'attente : sur une carte claire, pas le fond vert
   du bandeau en jeu, donc contraste propre à ce contexte. */
.card-panel .topbar {
  display: flex;
  align-items: center;
  margin: -6px -4px 4px;
}
.card-panel .topbar .icon-btn {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  color: var(--ink);
  text-decoration: none;
}
.card-panel .topbar .icon-btn:active { background: var(--border); }

.banner-pending {
  margin: 0 14px 8px;
  background: rgba(255,255,255,0.92);
  color: #7a2b00;
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
}

.opponents-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 14px 10px;
  -webkit-overflow-scrolling: touch;
}
.opponent-chip {
  flex: none;
  background: rgba(255,255,255,0.12);
  border-radius: 12px;
  padding: 8px 12px;
  min-width: 84px;
  text-align: center;
  font-size: 0.8rem;
  border: 2px solid transparent;
}
.opponent-chip.is-turn { border-color: #ffd54a; background: rgba(255,213,74,0.18); }
.opponent-chip.disconnected { opacity: 0.45; }
.opponent-name { display: block; font-weight: 600; margin-bottom: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.opponent-hand-visual { display: flex; align-items: center; justify-content: center; gap: 3px; margin-top: 2px; }
.mini-hand { display: inline-flex; }
.mini-back {
  width: 10px;
  height: 15px;
  border-radius: 2px;
  background: repeating-linear-gradient(45deg, #2f6f52, #2f6f52 2px, #245840 2px, #245840 4px);
  border: 1px solid rgba(255,255,255,0.55);
  margin-left: -5px;
}
.mini-back:first-child { margin-left: 0; }
.mini-extra { font-size: 0.62rem; opacity: 0.85; margin-left: 2px; }
.opponent-count-num { font-size: 0.72rem; font-weight: 700; opacity: 0.9; }

.table-area {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 28px;
  padding: 10px;
  min-height: 190px;
  border-radius: 18px;
  transition: background 0.15s ease, box-shadow 0.15s ease;
}
.table-area.drop-target-active {
  background: rgba(255, 213, 74, 0.16);
  box-shadow: inset 0 0 0 3px #ffd54a;
}

.pile {
  width: 100px;
  height: 138px;
  border-radius: 10px;
  border: 2px dashed rgba(255,255,255,0.55);
  background: rgba(255,255,255,0.08);
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
}
.pile-count { font-size: 1.4rem; font-weight: 700; }
.pile-label { font-size: 0.75rem; opacity: 0.85; }

.discard-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; }

/* Conteneur positionné : la pile d'historique (.discard-history) et la carte
   active (#discard-top) se superposent dedans, chacune en position absolue. */
.discard-stack {
  position: relative;
  width: 100px;
  height: 138px;
  /* Marge basse : les 3 cartes d'historique dépassent visuellement du bas de
     cette boîte (voir .history-card) sans agrandir sa taille de mise en page
     (transform ne compte pas dans le flux) — il faut réserver la place à la
     main pour que "Couleur active" ne chevauche pas leur liseré. */
  margin-bottom: 54px;
}

.card {
  width: 100px;
  height: 138px;
  border-radius: 10px;
  background: white;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 2rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.25);
  position: relative;
  user-select: none;
}
.card.suit-red { color: #d21f3c; }
.card.suit-black { color: #1a1a1a; }
.card.card-joker { color: #7b2fd1; font-size: 2.6rem; }
#discard-top {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
}
#discard-top.card-played-anim { animation: cardPlayed 0.42s cubic-bezier(0.22, 1, 0.36, 1); }
@keyframes cardPlayed {
  0% { transform: translateY(-46px) scale(0.72) rotate(-10deg); opacity: 0; }
  60% { transform: translateY(5px) scale(1.05) rotate(3deg); opacity: 1; }
  100% { transform: translateY(0) scale(1) rotate(0deg); opacity: 1; }
}

/* Les 3 cartes jouées juste avant : empilées derrière la carte active, chacune
   décalée un peu plus bas et avec un z-index un peu plus faible, de sorte que
   seul un fin liseré du bas de chaque carte (avec son numéro et sa couleur)
   dépasse de celle posée juste après elle. */
.discard-history {
  position: absolute;
  inset: 0;
  z-index: 0;
}
.history-card {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 3px 8px rgba(0,0,0,0.2);
}
/* Décalages égaux (16px) : chaque carte plus ancienne ne dépasse donc que de
   la bande laissée libre par celle posée juste après elle, jamais moins —
   sinon son numéro/couleur n'aurait pas la place de rester lisible. */
.history-card:nth-child(1) { transform: translateY(16px); opacity: 0.85; z-index: 3; }
.history-card:nth-child(2) { transform: translateY(32px); opacity: 0.65; z-index: 2; }
.history-card:nth-child(3) { transform: translateY(48px); opacity: 0.48; z-index: 1; }
.history-corner {
  position: absolute;
  bottom: 3px;
  left: 8px;
  font-size: 0.72rem;
  line-height: 1;
  font-weight: 700;
}
.history-card.suit-red .history-corner { color: #d21f3c; }
.history-card.suit-black .history-corner { color: #1a1a1a; }
.history-card.card-joker .history-corner { color: #7b2fd1; }

.card .rank-corner {
  position: absolute;
  top: 6px;
  left: 8px;
  /* em plutôt que rem : suit la taille (dynamique, voir layoutHand) de la
     carte elle-même quand la main se resserre. */
  font-size: 0.57em;
  font-weight: 700;
}
.card-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #2c2c2c;
  color: white;
  font-size: 0.4em;
  font-weight: 700;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 20px;
  border: 2px solid var(--surface);
  white-space: nowrap;
}
.hand-card .card-badge { padding: 2px 4px; }

.active-suit { font-size: 0.85rem; background: rgba(255,255,255,0.9); color: var(--ink); padding: 4px 12px; border-radius: 20px; font-weight: 600; }

.log-panel {
  background: rgba(0,0,0,0.22);
  margin: 0 14px 8px;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.78rem;
  max-height: 64px;
  overflow-y: auto;
  line-height: 1.5;
}
.log-entry { opacity: 0.9; }
.log-entry:last-child { opacity: 1; font-weight: 600; }
.log-entry-enter { animation: logIn 0.32s cubic-bezier(0.22, 1, 0.36, 1); }
@keyframes logIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.hand-wrap {
  background: rgba(0,0,0,0.18);
  padding: 6px 10px calc(env(safe-area-inset-bottom) + 12px);
}
.hand-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px 6px;
  gap: 8px;
}
.hand-hint {
  color: rgba(255,255,255,0.6);
  font-size: 0.72rem;
}
.btn-link {
  background: none;
  border: none;
  color: #ffd54a;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 4px 6px;
  cursor: pointer;
}
/* Volontairement neutre : ce bouton ne doit jamais être surligné ou mis en
   avant, même quand il serait "pertinent" de l'utiliser — l'oubli fait
   partie du jeu. */
.btn-catch {
  background: rgba(255,255,255,0.14);
  border: 1px solid rgba(255,255,255,0.35);
  color: white;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
}
.btn-catch:active { background: rgba(255,255,255,0.24); }
.hand-row {
  display: flex;
  overflow-x: auto;
  /* padding-top généreux : les cartes jouables se soulèvent (translateY) et
     leur badge d'effet déborde encore un peu plus haut (voir .card-badge) —
     il faut que cette marge absorbe les deux sans jamais chevaucher la barre
     d'outils au-dessus (sinon elle intercepte les clics sur "Trier"). */
  padding: 26px 4px 8px;
  -webkit-overflow-scrolling: touch;
  min-height: 96px;
}
.hand-card {
  flex: none;
  /* Largeur/hauteur/taille de police par défaut ; recalculées dynamiquement
     par layoutHand() en JS pour que toute la main tienne toujours à l'écran. */
  width: 70px;
  height: 99px;
  font-size: 1.4rem;
  cursor: grab;
  touch-action: none;
  transition: transform 0.16s ease, margin-left 0.16s ease, width 0.16s ease, height 0.16s ease, font-size 0.16s ease, opacity 0.15s ease;
}
.hand-card.playable { transform: translateY(-10px); box-shadow: 0 0 0 3px #ffd54a, 0 4px 10px rgba(0,0,0,0.25); }
.hand-card.not-playable { opacity: 0.55; }
.hand-card:active.playable { transform: translateY(-6px) scale(0.97); }
.hand-card.dragging {
  cursor: grabbing;
  z-index: 40;
  transition: none;
  box-shadow: 0 12px 26px rgba(0,0,0,0.45);
}

/* ---------- Modales ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.modal-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.modal-box-wide { max-width: 420px; text-align: left; max-height: 84vh; overflow-y: auto; }
.modal-box-wide h3 { text-align: center; }
.legend-list { list-style: none; margin: 14px 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.legend-list li { display: flex; align-items: flex-start; gap: 10px; font-size: 0.88rem; line-height: 1.4; }
.legend-list strong { color: var(--accent-dark); }
.legend-badge {
  flex: none;
  min-width: 34px;
  text-align: center;
  background: #2c2c2c;
  color: white;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 4px 6px;
  border-radius: 20px;
  margin-top: 2px;
}
.legend-subtitle { margin: 4px 0 6px; font-size: 1rem; }
.legend-text { font-size: 0.85rem; line-height: 1.5; color: var(--ink-soft); margin: 0 0 16px; }
.modal-box-wide .btn { display: block; margin: 0 auto; }

.suit-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 18px 0;
}
.suit-btn {
  border: 1.5px solid var(--border);
  background: var(--surface-alt);
  border-radius: 12px;
  padding: 18px 8px;
  font-size: 2.2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}
.suit-btn span { font-size: 0.8rem; color: var(--ink-soft); font-weight: 600; }
.suit-red { color: #d21f3c; }
.suit-black { color: #1a1a1a; }

.gameover-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }

/* ---------- Divers ---------- */
.conn-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  background: #c98a00;
  color: white;
  text-align: center;
  font-size: 0.85rem;
  padding: 6px 10px;
  padding-top: calc(env(safe-area-inset-top) + 6px);
  z-index: 100;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translateX(-50%);
  background: #2c2c2c;
  color: white;
  padding: 10px 18px;
  border-radius: 30px;
  font-size: 0.85rem;
  z-index: 200;
  max-width: 88vw;
  text-align: center;
  box-shadow: var(--shadow);
}

@media (min-width: 640px) {
  .table-area { gap: 40px; }
}

/* ---------- Réactions emoji ---------- */
.reaction-picker {
  position: fixed;
  left: 50%;
  top: 56px;
  transform: translateX(-50%);
  background: rgba(20, 30, 25, 0.92);
  border-radius: 22px;
  padding: 8px 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  max-width: min(88vw, 280px);
  z-index: 60;
  box-shadow: var(--shadow);
}
.reaction-choice {
  background: none;
  border: none;
  font-size: 1.5rem;
  padding: 4px;
  cursor: pointer;
  border-radius: 50%;
  line-height: 1;
}
.reaction-choice:active { background: rgba(255,255,255,0.18); transform: scale(1.15); }

.reactions-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 90;
  overflow: hidden;
}
.reaction-bubble {
  position: absolute;
  bottom: 130px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 2.1rem;
  animation: reactionFloat 2.6s ease-out forwards;
  text-shadow: 0 3px 8px rgba(0,0,0,0.35);
}
.reaction-bubble .reaction-name {
  font-size: 0.62rem;
  font-weight: 600;
  color: white;
  background: rgba(0,0,0,0.45);
  padding: 2px 8px;
  border-radius: 10px;
  text-shadow: none;
}
@keyframes reactionFloat {
  0% { opacity: 0; transform: translateY(0) scale(0.5); }
  12% { opacity: 1; transform: translateY(-14px) scale(1.15); }
  25% { transform: translateY(-34px) scale(1); }
  100% { opacity: 0; transform: translateY(-220px) scale(1); }
}

/* ---------- Historique / face-à-face ---------- */
.btn-link-center {
  display: block;
  width: 100%;
  text-align: center;
  margin-top: 18px;
  background: none;
  border: none;
  color: var(--accent-dark);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 6px;
}
.history-list { list-style: none; margin: 10px 0 18px; padding: 0; counter-reset: rank; }
.history-list li {
  counter-increment: rank;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.history-list li::before {
  content: counter(rank);
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--surface-alt);
  color: var(--ink-soft);
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.history-name { flex: 1; font-weight: 600; }
.history-score { color: var(--ink-soft); font-size: 0.8rem; }

.h2h-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.h2h-list li {
  background: var(--surface-alt);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
  text-align: center;
  color: var(--ink-soft);
}
.h2h-list strong { color: var(--ink); }
