/* Visio Chine - feuille de style unique, sans dependance externe.
   Ecrite pour le telephone d'abord : c'est l'appareil sur lequel l'appel a
   lieu, tenu a une main, souvent dans une piece mal eclairee. Le bureau n'est
   qu'un cas particulier plus confortable. */

:root {
  --bg: #0b0e14;
  --bg-soft: #141924;
  --bg-card: #171d2a;
  --line: #263041;
  --text: #e6ebf4;
  --text-dim: #8f9bb3;
  --accent: #3ba3ff;
  --accent-ink: #04121f;
  --good: #35d07f;
  --warn: #f2b544;
  --bad: #ff5a5a;
  --radius: 14px;

  /* Les commandes suivent la largeur de l'ecran : cinq boutons ronds doivent
     tenir sur une rangee depuis un 320 px jusqu'au bureau, sans jamais deborder. */
  --ctrl: clamp(46px, 13.5vw, 58px);
  --ctrl-gap: clamp(6px, 2.6vw, 14px);

  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);

  /* Hauteur de la barre de commandes, reutilisee pour poser les panneaux. */
  --bar-h: calc(var(--ctrl) + 24px + var(--safe-b));

  /* Vignettes de la vue « un grand + les autres ». Assez petites pour que cinq
     tiennent empilees sur un telephone sans mordre sur les commandes. */
  --mini-w: clamp(64px, 20vw, 116px);
  --mini-h: clamp(85px, 26.6vw, 155px);
}

* { box-sizing: border-box; }

/* Un `display` declare par l'auteur bat la regle `display:none` que le
   navigateur applique a [hidden]. Sans ce garde-fou, l'ecran d'appel reste
   dans le flux et pousse l'accueil hors de l'ecran sur mobile. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

/* Pendant l'appel, la page elle-meme ne defile jamais : seuls les panneaux le
   font. Evite la barre d'URL qui monte et descend au moindre glissement. */
body.in-call { overflow: hidden; position: fixed; inset: 0; width: 100%; }

button {
  font: inherit;
  cursor: pointer;
  touch-action: manipulation; /* supprime le delai de double-tap sur mobile */
  -webkit-user-select: none;
  user-select: none;
}
input, select { font: inherit; }

/* Sous 16 px, iOS Safari zoome tout seul a la mise au point d'un champ et
   l'utilisateur se retrouve avec une page decalee qu'il doit repincer. */
input, select, textarea { font-size: 16px; }

/* ------------------------------ ACCUEIL ------------------------------ */
.lobby {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  /* `align-items: center` seul rogne le haut d'une carte plus grande que
     l'ecran ; `auto` sur les marges recentre sans jamais couper. */
  padding: max(16px, var(--safe-t)) max(14px, var(--safe-l)) max(16px, var(--safe-b)) max(14px, var(--safe-r));
  background:
    radial-gradient(900px 500px at 15% -10%, #1b3557 0%, transparent 60%),
    radial-gradient(800px 500px at 95% 110%, #3a1f3d 0%, transparent 60%),
    var(--bg);
}

.lobby-card {
  width: min(440px, 100%);
  margin: auto;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: clamp(18px, 5vw, 26px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
}

.lobby-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 16px;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: .2px; }
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 0 4px rgba(53, 208, 127, .16);
}

.lang-switch { display: flex; gap: 2px; }
.lang-switch button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text-dim);
  border-radius: 9px;
  /* Cible tactile de 44 px de haut, obtenue sans grossir le cadre visible. */
  min-width: 46px;
  min-height: 44px;
  padding: 0 8px;
  font-size: 13px;
}
.lang-switch button.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

h1 { font-size: clamp(19px, 5.4vw, 22px); margin: 0 0 6px; line-height: 1.3; }
.sub { margin: 0 0 18px; color: var(--text-dim); font-size: 14px; }

.field { display: block; margin-bottom: 13px; }
.field > span { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }

.field input, #opt-quality {
  width: 100%;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 12px;
  min-height: 48px;
  outline: none;
}
.field input:focus, #opt-quality:focus { border-color: var(--accent); }

.room-row { display: flex; gap: 8px; }
.icon-btn {
  flex: 0 0 auto;
  width: 48px;
  min-height: 48px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font-size: 18px;
}

.prejoin { position: relative; margin: 4px 0 16px; }
#preview {
  display: block;
  margin: 0 auto;
  width: 100%;
  max-width: 100%;
  /* Format provisoire, tant que la camera n'a pas repondu. */
  aspect-ratio: 4 / 3;
  object-fit: contain;
  /* Sur un ecran court (petit telephone, ou clavier ouvert), la previsualisation
     cede la place aux champs plutot que de pousser le bouton hors de vue. */
  max-height: min(38vh, 260px);
  background: #000;
  border-radius: var(--radius);
  transform: scaleX(-1);
  border: 1px solid var(--line);
}
/* Des que la camera a repondu, le cadre epouse le format reel de l'image : ni
   rognage, ni bandes noires. On voit exactement ce que l'autre recevra. */
#preview.ready {
  width: auto;
  height: auto;
  aspect-ratio: auto;
}
.prejoin-controls {
  position: absolute;
  left: 50%; bottom: 10px;
  transform: translateX(-50%);
  display: flex; gap: 12px;
}
.chip {
  width: 46px; height: 46px;
  border-radius: 50%;
  border: none;
  background: rgba(20, 25, 36, .82);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  color: var(--text);
  font-size: 18px;
}
.chip:not(.on) { background: var(--bad); }
.prejoin-msg {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  text-align: center; padding: 20px;
  font-size: 13px; color: var(--text-dim);
  pointer-events: none;
}

.primary {
  width: 100%;
  border: none;
  border-radius: 12px;
  padding: 15px;
  min-height: 52px;
  font-weight: 650;
  font-size: 16px;
  background: var(--accent);
  color: var(--accent-ink);
}
.primary:disabled { opacity: .5; }

.hint { color: var(--text-dim); font-size: 12.5px; margin: 14px 0 0; text-align: center; }
#browser-warning { color: var(--warn); }

/* Ecran court : le bouton « Rejoindre » doit rester visible sans faire defiler.
   Ce qu'on sacrifie en premier, c'est le texte d'accroche et la taille de la
   previsualisation - pas les champs ni les commandes micro/camera. */
@media (max-height: 700px) {
  .sub { display: none; }
  .lobby-card { padding: 16px; }
  h1 { margin-bottom: 12px; }
  #preview { max-height: 24vh; }
  .hint { margin-top: 10px; font-size: 12px; }
}

@media (max-height: 560px) {
  #preview { max-height: 20vh; }
  .prejoin { margin-bottom: 12px; }
  .field { margin-bottom: 10px; }
}

/* ------------------------------- APPEL ------------------------------- */
.call {
  position: relative;
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  /* Le decrochement de l'ecran va en haut, pas en bas : la version precedente
     ajoutait l'encoche au mauvais bord et laissait la barre sous le capteur. */
  padding: calc(8px + var(--safe-t)) max(10px, var(--safe-r)) 8px max(10px, var(--safe-l));
  border-bottom: 1px solid var(--line);
  background: var(--bg-soft);
  flex: 0 0 auto;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 6px; min-width: 0; }
.topbar-left { flex: 1 1 auto; }
.topbar-right { flex: 0 0 auto; }

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text-dim);
  border-radius: 9px;
  min-height: 40px;
  padding: 0 12px;
  font-size: 13px;
  white-space: nowrap;
}
.ghost:hover { color: var(--text); border-color: var(--accent); }
#btn-stats { min-width: 44px; padding: 0 10px; font-size: 16px; }

.room-label {
  font-size: 13px; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  min-width: 0;
}
.clock { font-size: 11.5px; color: var(--text-dim); font-variant-numeric: tabular-nums; white-space: nowrap; }

.banner {
  padding: 9px 14px;
  font-size: 13px;
  text-align: center;
  background: rgba(242, 181, 68, .14);
  color: var(--warn);
  border-bottom: 1px solid rgba(242, 181, 68, .25);
  flex: 0 0 auto;
}
.banner.bad { background: rgba(255, 90, 90, .14); color: var(--bad); border-color: rgba(255, 90, 90, .3); }
.banner.ok { background: rgba(53, 208, 127, .12); color: var(--good); border-color: rgba(53, 208, 127, .25); }

.grid {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  gap: 6px;
  padding: 6px max(6px, var(--safe-l)) 6px max(6px, var(--safe-r));
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, 1fr);
}
.grid.two { grid-template-rows: 1fr 1fr; }
.grid.many { grid-template-columns: 1fr 1fr; }

.tile {
  position: relative;
  min-height: 0;
  min-width: 0;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
}
.tile video {
  width: 100%; height: 100%;
  /* `contain` et non `cover` : une tuile n'a presque jamais le format exact de
     la camera d'en face, et remplir le cadre revenait a rogner la moitie de
     l'image - le visage arrivait en gros plan, coupe. Mieux vaut deux bandes
     noires qu'un cadrage ampute. Vaut aussi pour l'ecran partage, qui etait
     rogne sur les bords. */
  object-fit: contain;
  display: block;
  background: #000;
}
.tile.self video { transform: scaleX(-1); }
.tile.self.sharing video { transform: none; }
.tile.novideo video { opacity: 0; }

/* Mise en avant : un visage occupe tout le cadre, les autres se rangent en
   vignettes sur le bord. C'est l'etat par defaut a deux sur un telephone tenu
   droit, et celui qu'on obtient en tapant sur quelqu'un. */
.tile { cursor: pointer; }

.grid.spotlight { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.grid.spotlight > .tile { grid-area: 1 / 1; }
.grid.spotlight > .tile.mini {
  position: absolute;
  right: max(10px, var(--safe-r));
  /* Les vignettes s'empilent selon leur rang, calcule en JS. */
  top: calc(10px + var(--mini-rank, 0) * (var(--mini-h) + 8px));
  width: var(--mini-w);
  height: var(--mini-h);
  z-index: 15;
  border-radius: 12px;
  border-color: rgba(255, 255, 255, .22);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .55);
}
/* Une vignette est trop petite pour se permettre des bandes noires : elle
   remplit, quitte a rogner les bords. On y reconnait la personne, c'est son
   seul role. */
.grid.spotlight > .tile.mini video { object-fit: cover; }

.grid.spotlight.one-mini > .tile.mini {
  width: clamp(86px, 26vw, 140px);
  height: clamp(115px, 34.6vw, 186px);
}
.grid.spotlight > .tile.mini .tile-name { display: none; }
.grid.spotlight > .tile.mini .tile-avatar { font-size: 22px; }
.grid.spotlight > .tile.mini .tile-muted { width: 20px; height: 20px; font-size: 10px; right: 4px; top: 4px; }

.tile-name {
  position: absolute; left: 8px; bottom: 8px;
  background: rgba(0, 0, 0, .58);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 12.5px;
  display: flex; align-items: center; gap: 6px;
  max-width: calc(100% - 16px);
}
.tile-name .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-name .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-dim); flex: 0 0 auto; }
.tile-name .dot.good { background: var(--good); }
.tile-name .dot.warn { background: var(--warn); }
.tile-name .dot.bad { background: var(--bad); }

.tile-avatar {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  font-size: clamp(28px, 9vw, 56px);
  font-weight: 600;
  color: var(--text-dim);
  background: linear-gradient(160deg, #1b2331, #10151f);
}
.tile:not(.novideo) .tile-avatar { display: none; }

.tile-muted {
  position: absolute; right: 8px; top: 8px;
  background: rgba(255, 90, 90, .9);
  border-radius: 50%;
  width: 26px; height: 26px;
  display: grid; place-items: center;
  font-size: 13px;
}

/* --------------------------- PANNEAUX --------------------------- */
/* Voile : sur mobile, un panneau ouvert doit se fermer d'une tape a cote,
   sinon il faut viser un petit bouton pour revenir a l'appel. */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 19;
}

.panel {
  position: fixed;
  z-index: 20;
  background: var(--bg-card);
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  box-shadow: 0 -12px 40px rgba(0, 0, 0, .55);

  /* Mobile : feuille ancree en bas, sur toute la largeur, pouce a portee. */
  left: 0; right: 0; bottom: 0;
  border-radius: 18px 18px 0 0;
  border-bottom: none;
  padding: 14px max(14px, var(--safe-l)) calc(14px + var(--safe-b)) max(14px, var(--safe-r));
  max-height: 72dvh;
}

/* Poignee de feuille : indique que le panneau se tire vers le bas. */
.panel::before {
  content: '';
  display: block;
  width: 38px; height: 4px;
  margin: -4px auto 10px;
  border-radius: 99px;
  background: var(--line);
  flex: 0 0 auto;
}

.panel h3 { margin: 0 0 10px; font-size: 15px; flex: 0 0 auto; }
.panel-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-top: 12px; font-size: 14px; color: var(--text-dim);
  flex: 0 0 auto;
}
.panel-row select { width: auto; flex: 1; min-height: 44px; }
.panel-row .ghost { width: 100%; min-height: 46px; }
.switch { display: flex; align-items: center; gap: 10px; color: var(--text-dim); font-size: 14px; min-height: 44px; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: 0 0 auto; }

.stats-panel { font-size: 13px; }
#stats-body { overflow-y: auto; -webkit-overflow-scrolling: touch; }
#stats-body table { width: 100%; border-collapse: collapse; }
#stats-body td { padding: 4px 0; color: var(--text-dim); vertical-align: top; }
#stats-body td:last-child { color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }
#stats-body h4 { margin: 10px 0 4px; font-size: 13px; color: var(--text); }

/* Hauteur stable : sans plancher, la feuille grandit a chaque message recu et
   les boutons se derobent sous le pouce. */
.chat-panel { min-height: min(46dvh, 340px); }

.chat-log {
  flex: 1 1 auto;
  min-height: 90px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 8px;
  padding-right: 4px;
}
.msg { font-size: 14.5px; }
.msg .who { color: var(--accent); font-weight: 600; margin-right: 6px; }
.msg.self .who { color: var(--good); }
.msg .txt { color: var(--text); overflow-wrap: anywhere; }
.msg.sys { color: var(--text-dim); font-style: italic; font-size: 13px; }

#chat-form { display: flex; gap: 8px; margin-top: 10px; flex: 0 0 auto; }
#chat-form input {
  flex: 1 1 auto;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 12px;
  padding: 12px;
  min-height: 48px;
  outline: none;
  min-width: 0;
}
#chat-form button {
  flex: 0 0 auto;
  border: none; border-radius: 12px;
  width: 48px; min-height: 48px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 18px;
}

/* Bureau et tablette : de la largeur disponible, donc des visages cote a cote
   plutot que deux bandeaux ecrases l'un sur l'autre. */
@media (min-width: 760px) {
  .grid.two { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
  /* Au-dela de deux, le nombre de colonnes suit la largeur : deux sur une
     tablette, trois ou quatre sur un grand ecran. */
  .grid.many { grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr)); }

  /* Le panneau redevient une carte flottante a droite. */
  .scrim { display: none; }
  .panel {
    left: auto;
    right: 12px;
    bottom: calc(var(--bar-h) + 12px);
    width: 350px;
    max-height: min(60vh, 460px);
    border-radius: var(--radius);
    border-bottom: 1px solid var(--line);
    padding: 14px;
  }
  .panel::before { display: none; }
}

/* --------------------------- CONTROLES --------------------------- */
.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--ctrl-gap);
  padding: 12px max(8px, var(--safe-l)) calc(12px + var(--safe-b)) max(8px, var(--safe-r));
  background: var(--bg-soft);
  border-top: 1px solid var(--line);
  flex: 0 0 auto;
}
.ctrl {
  width: var(--ctrl); height: var(--ctrl);
  flex: 0 0 auto;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-card);
  color: var(--text);
  font-size: calc(var(--ctrl) * .38);
  position: relative;
  display: grid; place-items: center;
  transition: background .12s, transform .12s;
}
.ctrl:active { transform: scale(.94); }
.ctrl.on { background: #24304a; border-color: #33456a; }
.ctrl.off { background: var(--bad); border-color: var(--bad); }
.ctrl.danger { background: var(--bad); border-color: var(--bad); }
.ctrl .badge {
  position: absolute; top: 4px; right: 4px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
}

/* Telephone etroit : les horloges cedent la place au nom de salle. */
@media (max-width: 400px) {
  .clock { display: none; }
  #btn-copy { padding: 0 10px; }
}

/* Telephone couche : la hauteur est la ressource rare. Les commandes passent
   par-dessus la video en colonne sur le cote, et la video occupe tout. */
@media (max-height: 500px) and (orientation: landscape) {
  /* Ici c'est la hauteur qui manque, pas la largeur : les boutons se calibrent
     donc sur la hauteur d'ecran, sinon la colonne depasse en haut et en bas. */
  :root {
    --ctrl: clamp(40px, 11vh, 50px);
    --ctrl-gap: clamp(5px, 1.7vh, 10px);
  }

  .topbar { padding-top: calc(4px + var(--safe-t)); padding-bottom: 4px; }
  .ghost { min-height: 34px; font-size: 12px; }
  .banner { padding: 5px 12px; font-size: 12px; }
  .grid { padding: 4px calc(var(--ctrl) + 18px) 4px max(4px, var(--safe-l)); }
  /* Le positionnement absolu ignore le padding : les vignettes doivent etre
     decalees a la main pour ne pas passer sous la colonne de commandes. */
  .grid.spotlight > .tile.mini { right: calc(var(--ctrl) + 24px); }
  .grid.two { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
  .controls {
    position: absolute;
    right: max(6px, var(--safe-r));
    top: 50%;
    transform: translateY(-50%);
    flex-direction: column;
    padding: 8px 6px;
    border: 1px solid var(--line);
    border-radius: 99px;
    background: rgba(20, 25, 36, .82);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    z-index: 18;
  }
  .panel { max-height: 88dvh; }
  /* Plus de barre en bas dans cette orientation : le message remonte contre le bord. */
  .toast { bottom: calc(10px + var(--safe-b)); }
}

/* ----------------------------- TOAST ----------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + 14px);
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  text-align: center;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 13.5px;
  z-index: 60;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
}

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