/* ═══════════════════════════════════════════════
   MégaQuiz — thème violet / bleu
   Fond violet rayé, cartes blanches, bords noirs,
   ombres dures sur les cartes (pas sur les textes).
   Type : Titan One (logo/titres) + Fredoka (tout le reste)
   ═══════════════════════════════════════════════ */

:root {
  --bg: #6f5ff0;
  --bg-stripe: #6656e8;
  --ink: #1b1535;
  --paper: #ffffff;
  --primary: #3d7bfd;
  --violet: #8f6ff5;
  --urgent: #ff5470;
  --green: #17a06b;
  --border: 3px solid var(--ink);
  --shadow: 5px 6px 0 var(--ink);
  --shadow-sm: 3px 4px 0 var(--ink);
  --radius: 22px;
  --display: "Titan One", cursive;
  --body: "Fredoka", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--body);
  font-size: 17px;
  color: var(--ink);
  min-height: 100vh;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: repeating-linear-gradient(
    -45deg,
    var(--bg) 0 46px,
    var(--bg-stripe) 46px 92px
  );
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  touch-action: manipulation;
}

input, textarea, select, button {
  user-select: auto;
  -webkit-user-select: auto;
  -moz-user-select: auto;
  -ms-user-select: auto;
}

img {
  -webkit-user-drag: none;
  user-drag: none;
  pointer-events: none;
}

.screen { display: none; min-height: 100vh; }
.screen.active { display: block; }
.hidden { display: none !important; }

/* ── composants de base ── */
.card {
  background: var(--paper);
  border: var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.btn {
  font-family: var(--body);
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: 0.3px;
  color: #fff;
  background: var(--primary);
  border: var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
  padding: 12px 26px;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:hover { transform: translate(-1px, -1px); box-shadow: 4px 5px 0 var(--ink); }
.btn:active { transform: translate(2px, 3px); box-shadow: 1px 1px 0 var(--ink); }
.btn:disabled {
  background: #b9b3d6;
  cursor: not-allowed;
  transform: none;
  box-shadow: var(--shadow-sm);
}
.btn.small { font-size: 0.95rem; padding: 10px 20px; }

.chip {
  display: inline-block;
  font-family: var(--body);
  font-weight: 700;
  font-size: 1rem;
  color: #fff;
  border: var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
  padding: 8px 16px;
}
.chip.blue { background: var(--primary); }
.chip.red { background: var(--violet); }
.chip.green { background: var(--green); }

/* ═════════ ACCUEIL ═════════ */
#home.active {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)) 16px;
}
.home-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  width: 100%;
  max-width: 480px;
}
.home-head { text-align: center; }

.logo {
  font-family: var(--display);
  font-size: clamp(3.2rem, 9vw, 5.5rem);
  color: #fff;
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 6px 7px 0 var(--ink), 0 0 1px rgba(0, 0, 0, 0.12);
  letter-spacing: 2px;
}
.tagline {
  margin-top: 10px;
  font-size: 1.3rem;
  font-weight: 600;
  font-style: italic;
  color: #fff;
  opacity: 0.95;
  line-height: 1.4;
}

.identity {
  width: 100%;
  padding: 26px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.pseudo-hint {
  font-size: 0.92rem;
  font-weight: 600;
  font-style: italic;
  color: #8a82b8;
  margin-top: -6px;
  text-align: center;
}
/* LE bouton : énorme, impossible à rater */
.play-btn {
  appearance: none;
  -webkit-appearance: none;
  font-family: var(--display);
  font-size: clamp(1.7rem, 4.5vh, 2.3rem);
  letter-spacing: 1px;
  line-height: 1.15;
  color: #fff;
  background: var(--green);
  border: 4px solid var(--ink);
  border-radius: 20px;
  box-shadow: 0 8px 0 var(--ink);
  padding: 18px 20px;
  width: 100%;
  cursor: pointer;
  outline: none;
  transition: filter 0.12s ease, transform 0.08s ease, box-shadow 0.08s ease;
}
.play-btn:hover { filter: brightness(1.08); }
.play-btn:active { transform: translateY(6px); box-shadow: 0 2px 0 var(--ink); }
.play-btn:focus-visible { outline: 3px dashed #fff; outline-offset: 4px; }
.online-count {
  font-weight: 700;
  color: #6e66a3;
  font-size: 0.95rem;
  text-align: center;
  min-height: 1.2em;
}
#pseudoInput {
  width: 100%;
  font-family: var(--body);
  font-size: 1.3rem;
  font-weight: 700;
  text-align: center;
  color: var(--ink);
  background: #f2f0fd;
  border: var(--border);
  border-radius: 14px;
  padding: 15px 18px;
  outline: none;
}
#pseudoInput:focus { border-color: var(--primary); }


.hall { width: 100%; padding: 20px 26px; }
.hall h3 { font-family: var(--display); font-size: 1.35rem; margin-bottom: 14px; }
.hall ol { list-style: none; counter-reset: rank; }
.hall li {
  counter-increment: rank;
  display: flex; align-items: center; gap: 12px;
  padding: 9px 0;
  border-bottom: 2px dashed #e4e0f7;
  font-weight: 600;
  font-size: 1.08rem;
}
.hall li::before {
  content: counter(rank);
  font-family: var(--display);
  color: var(--primary);
  width: 26px;
}
.hall li img { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--ink); }
.hall li .pts { margin-left: auto; color: var(--violet); font-weight: 700; }

/* ═════════ PLATEAU ═════════ */
/* Comme un jeu flash : tout tient dans la hauteur de l'écran, aucun scroll de page. */
#game.active {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
.game-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 18px 16px;
}

.game-top {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 12px;
}
.room-title {
  font-family: var(--display);
  font-size: clamp(1.9rem, 5vh, 2.9rem);
  letter-spacing: 1px;
  color: #fff;
  text-shadow: 5px 6px 0 var(--ink);
}
.top-spacer { width: 90px; }

/* ── bulle du présentateur ── */
.bubble {
  flex: 0 0 auto;
  position: relative;
  min-height: clamp(120px, 22vh, 170px);
  height: auto;
  padding: 10px 18px 18px;
  margin-bottom: 22px;
  display: flex;
  flex-direction: column;
}
.bubble::before {
  content: "";
  position: absolute;
  left: 220px; bottom: -28px;
  border: 14px solid transparent;
  border-top: 14px solid var(--ink);
}
.bubble::after {
  content: "";
  position: absolute;
  left: 224px; bottom: -19px;
  border: 10px solid transparent;
  border-top: 10px solid var(--paper);
}
.bubble.hot { animation: hotShake 0.4s ease infinite; }
@keyframes hotShake { 50% { transform: translateX(2px); } }

.q-meta { flex: 0 0 auto; display: flex; justify-content: space-between; align-items: center; }
.q-timer { font-variant-numeric: tabular-nums; min-width: 74px; text-align: center; }
.q-timer.urgent { background: var(--urgent); }

.bubble-row {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
}
.bubble-text {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  font-size: clamp(1.15rem, 2.4vh, 1.75rem);
  font-weight: 700;
  line-height: 1.35;
  overflow: hidden;
}
.bubble-text > span { display: inline; }
.bubble-text .caret {
  display: inline-block; width: 4px; height: 1.1em;
  background: var(--primary); vertical-align: -0.15em;
  animation: blink 0.8s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0.3; } }
.bubble-text .answer-hl { color: var(--green); }

/* drapeau à deviner : dans le flux, à la suite de la question */
.q-flag {
  flex: 0 0 auto;
  height: clamp(56px, 11vh, 88px);
  border-radius: 6px;
  margin-right: 5px;
}

.timer-bar {
  position: absolute; left: 22px; right: 22px; bottom: 10px;
  height: 10px;
  background: #eceafb;
  border: 2px solid var(--ink);
  border-radius: 999px;
  overflow: hidden;
}
#timerFill {
  height: 100%; width: 100%;
  background: var(--primary);
  transform-origin: left;
  transition: transform 0.2s linear, background 0.3s;
}
.bubble.hot #timerFill { background: var(--urgent); }

/* ── corps : 40% présentateur / 60% chat sur widescreen ── */
.game-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 2fr 3fr;
  gap: 18px;
  align-items: stretch;
}

.left-col {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* le présentateur prend toute la hauteur restante de la colonne */
.presenter {
  position: relative;
  overflow: hidden;
  flex: 1;
  min-height: 0;
  display: flex; align-items: center; justify-content: center;
  background: #f2f0fd;
}
.presenter img { width: 100%; height: 100%; object-fit: cover; display: block; }
.presenter.noimg img { display: none; }
.presenter-fallback {
  display: none;
  font-family: var(--display);
  font-size: 1.4rem;
  text-align: center;
  color: #c9c2ea;
}
.presenter.noimg .presenter-fallback { display: block; }

.applaud-btn { flex: 0 0 auto; width: 100%; font-size: 1.05rem; padding: 12px 10px; border-radius: 16px; }

/* scoreboard : panneau violet, 8 places fixes,
   colonne gauche = 1er a 4e, colonne droite = 5e a 8e (classement general).
   Taille constante : rien ne bouge entre les questions. */
.players {
  flex: 0 0 auto;
  padding: 12px;
  background: #5747e6;
}
#playerList {
  list-style: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(4, 1fr);
  grid-auto-flow: column;
  gap: 7px;
}
#playerList li {
  display: flex; align-items: center; gap: 9px;
  cursor: default;
  height: 40px;
  padding: 0 8px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: #6f5ff0;
  color: #fff;
  font-weight: 600;
  font-size: 0.93rem;
  min-width: 0;
}
#playerList li .pl-name {
  flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: #fff;
}
#playerList li .pl-score {
  flex: 0 0 auto;
  font-weight: 700;
  font-size: 1.05rem;
  color: #ffd36e;
  min-width: 30px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
#playerList li.clickable { cursor: pointer; border-color: rgba(0, 0, 0, 0.4); transition: transform 0.08s, background 0.08s; }
#playerList li.clickable:hover { background: #7d6ffa; transform: translateY(-1px); }
#playerList li.clickable:active { transform: translateY(1px); }
#playerList li.empty { opacity: 0.4; }
#playerList li.empty .pl-name { font-style: italic; font-weight: 500; }

/* ── grand chat : remplit la hauteur, scroll interne uniquement ── */
.chat {
  min-height: 0;
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
}
.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 7px;
  font-size: 1.04rem;
  font-weight: 600;
  padding-right: 6px;
  scroll-behavior: smooth;
}
.chat-log .msg.sys { color: #6e66a3; }
.chat-log .msg.found-msg { color: var(--green); }
.chat-log .msg.me-found {
  color: var(--green);
  text-align: center;
  background: #e7f6ef;
  border: 2px solid var(--green);
  border-radius: 10px;
  padding: 7px;
}
.chat-log .msg.qdiv {
  display: flex; align-items: center; gap: 12px;
  margin: 10px 0 4px;
  font-weight: 700;
  color: var(--violet);
  text-transform: uppercase;
  letter-spacing: 1px;
  font-size: 0.85rem;
}
.chat-log .msg.qdiv::before,
.chat-log .msg.qdiv::after {
  content: "";
  flex: 1;
  height: 2px;
  background: #e4e0f7;
  border-radius: 2px;
}

.chat-form { flex: 0 0 auto; display: flex; gap: 10px; margin-top: 10px; }
#chatInput {
  flex: 1;
  font-family: var(--body);
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--ink);
  border: var(--border);
  border-radius: 12px;
  padding: 11px 14px;
  outline: none;
}
#chatInput:focus { border-color: var(--primary); }

/* volume */
.volume {
  position: fixed;
  bottom: 14px; left: 14px;
  z-index: 99;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-radius: 999px;
}
.volume button {
  font-family: var(--body);
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--ink);
  background: none;
  border: none;
  cursor: pointer;
}
.volume button.muted { color: #b9b3d6; text-decoration: line-through; }
#volumeSlider { width: 110px; accent-color: var(--primary); cursor: pointer; }

/* footer */
.made-by {
  position: fixed;
  bottom: 12px; right: 14px;
  z-index: 98;
  font-family: var(--body);
  font-weight: 600;
  font-size: 0.8rem;
  color: #fff;
  opacity: 0.75;
  text-decoration: none;
}
.made-by:hover { opacity: 1; text-decoration: underline; }

/* ═════════ MODE APPLICATION MOBILE ═════════
   Sous 980px, le plateau devient une vraie appli qui tient entièrement dans
   l'écran, sans aucun scroll de page : en-tête compact, réponse tout en
   haut sous la question, présentateur + applaudissements + classement
   complet (8 places, sans scroll) regroupés en un seul bloc compact, et le
   fil de chat qui absorbe le reste de l'espace. */
@media (max-width: 980px) {
  #game.active {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .game-wrap {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto auto auto auto 1fr auto;
    grid-template-areas:
      "header  header"
      "bubble  bubble"
      "answer  answer"
      "roster  roster"
      "mute    mute"
      "chat    chat"
      "toolbar credit";
    gap: 7px;
    height: 100%;
    overflow: hidden;
    padding: calc(8px + var(--safe-top)) 12px calc(8px + var(--safe-bottom)) 12px;
  }

  /* ces conteneurs n'ont plus de rôle visuel propre : leurs enfants
     deviennent directement des cases de la grille, quel que soit
     l'endroit où ils sont imbriqués dans le HTML */
  .game-body { display: contents; }
  .chat { display: contents; }

  .game-top { grid-area: header; margin-bottom: 0; }

  .bubble {
    grid-area: bubble;
    margin-bottom: 0;
    min-height: clamp(58px, 11vh, 92px);
    padding: 5px 16px 10px;
  }
  .bubble::before { left: 26px; }
  .bubble::after { left: 30px; }

  /* ── la réponse tout en haut, juste sous la question : c'est ce qu'on utilise le plus ── */
  .chat-form { grid-area: answer; margin-top: 0; }

  /* ── bloc compact : présentateur + applaudir sur une ligne, classement complet juste en dessous ── */
  .left-col {
    grid-area: roster;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
  }

  .presenter {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
  .presenter-fallback { font-size: 0.4rem; line-height: 1.1; padding: 1px; }

  .applaud-btn {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
    padding: 8px 10px;
    font-size: 0.72rem;
    line-height: 1.2;
    border-radius: 14px;
  }

  /* force le classement à passer sur sa propre ligne, sous présentateur + applaudir */
  .players {
    flex: 1 1 100%;
    min-width: 0;
    padding: 6px;
  }
  #playerList {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(2, 1fr);
    grid-auto-flow: row;
    gap: 5px;
  }
  #playerList li {
    height: auto;
    min-height: 30px;
    padding: 0 6px;
    gap: 4px;
    font-size: 0.64rem;
  }
  #playerList li .pl-score { font-size: 0.75rem; min-width: 16px; }

  .mute-toggle { grid-area: mute; margin: 0; padding: 0 4px; }

  /* ── le chat absorbe le reste de l'espace, sans jamais pousser la page à scroller ── */
  .chat-log {
    grid-area: chat;
    min-height: 0;
    background: var(--paper);
    border: var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 10px 12px;
  }

  /* ── barre d'outils fine (son + crédit), toujours visible en bas ── */
  .volume {
    grid-area: toolbar;
    position: static;
    width: auto;
    margin: 0;
    left: auto;
    bottom: auto;
    right: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 7px 14px;
  }
  #volumeSlider { flex: 1; width: auto; }

  .made-by {
    grid-area: credit;
    position: static;
    display: flex;
    align-items: center;
    margin: 0;
    right: auto;
    left: auto;
    bottom: auto;
  }

  /* Sur mobile, -webkit-text-stroke associé à text-shadow fait disparaître
     l'ombre (bug Safari/Chrome iOS et Android). On reprend donc la même
     technique que le titre du plateau (ombre seule, sans contour), avec
     une distance plus courte pour rester discrète à cette taille. */
  .logo {
    -webkit-text-stroke: 0;
    paint-order: normal;
    text-shadow: 4px 5px 0 var(--ink);
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ═════════ CONFETTIS ═════════ */
#confetti {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 60;
}

/* ═════════ MUTE DES AUTRES JOUEURS ═════════ */
.mute-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #8a82b8;
  padding: 4px 6px 0;
  cursor: pointer;
  user-select: none;
}
.mute-toggle input { accent-color: var(--violet); cursor: pointer; }
body.mute-others .chat-log .msg.user-msg:not(.mine) { display: none; }
