/* Mise en page téléphone : l'écran de jeu occupe tout l'écran, le jeu est mis à l'échelle par fit() (app.js) */
.rotate-hint { display: none; }

@media (max-width: 700px), (max-height: 600px) and (pointer: coarse) {
  body:has(#game:not([hidden])) { overflow: hidden; }
  #game:not([hidden]) { position: fixed; inset: 0; z-index: 5; display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
  #game header { flex: none; display: flex; flex-wrap: nowrap; align-items: center; gap: 8px; min-height: 48px; padding: 6px 56px 6px 8px; }
  #game header .btn { flex: none; padding: 8px 12px; font-size: 13px; white-space: nowrap; }
  #game h2 { flex: 0 1 auto; min-width: 0; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .game-players { order: 0; flex: 1 1 0; min-width: 0; max-width: none; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #gameHelp, #fsBtn { display: none; }
  #stage { flex: 1 1 0; min-height: 0; height: auto !important; padding: 6px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
  #stage > * { flex: none; }

  /* bouton son unique, dans l'en-tête */
  body:has(#game:not([hidden])) .snd { top: 6px; right: 8px; bottom: auto; }
  body:has(#game:not([hidden])) .snd button { width: 40px; height: 40px; }
  body:has(#game:not([hidden])) .snd button:not(:first-child) { display: none; }
  body:has(#game:not([hidden])) .snd-toast { top: 54px; bottom: auto; }
  body:has(#game:not([hidden])) .snd-panel { top: 54px; bottom: auto; }

  .snake-wrap { width: min(630px, calc(100vw - 12px)); }
  .snake-wrap canvas { width: 100%; height: auto; }
  /* Bandeau de score sur une seule ligne (le pseudo est déjà dans l'en-tête) */
  .snake-wrap .scorebar, .nb-wrap .scorebar { flex-wrap: nowrap; gap: 12px; font-size: 14px; white-space: nowrap; }
  .snake-wrap .scorebar .snake-player, .nb-wrap .scorebar .snake-player { display: none; }
  .nb-wrap .nb-pause { min-width: 0; min-height: 0; padding: 4px 10px; }
  .sw-levels { background: radial-gradient(circle at 30% 0%, #2a1c52, #0a0716); overflow-y: auto; padding: 16px 14px; }
  .tetris-wrap { width: min(410px, calc(100vw - 12px)); }
  .mines-wrap { gap: 8px; width: calc(100vw - 12px); }
  .mines-wrap .row { flex-wrap: wrap; justify-content: center; gap: 6px; }
  .mines-wrap .row .btn { padding: 8px 12px; font-size: 13px; }

  /* Patience */
  .sol { --gap: 4px; --cw: calc((100vw - 12px - 6 * var(--gap)) / 7); width: calc(100vw - 12px); }
  .sol .bar { gap: 6px; }

  /* Petits Chevaux */
  .horses-wrap { width: calc(100vw - 12px); min-width: 0; flex-direction: column; align-items: stretch; gap: 8px; }
  .horses-wrap.is-setup { width: min(420px, calc(100vw - 24px)); }
  .horses-wrap canvas { width: 100%; height: auto; aspect-ratio: 1; }
  .hp-board { width: 100%; height: auto; aspect-ratio: 1; }
  .hp-panel { width: 100%; flex: none; height: auto; display: grid; grid-template-columns: 1fr 120px; gap: 8px; padding: 10px; }
  .hp-turn { grid-column: 1; grid-row: 1; padding: 8px 10px; }
  .hp-msg { grid-column: 1; grid-row: 2; min-height: 46px; }
  .hp-panel .dice-stage { grid-column: 2; grid-row: 1 / span 2; height: 100px; }
  .hp-roll { grid-column: 1 / -1; grid-row: 3; }
  .hp-list { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .hp-tools { grid-column: 1 / -1; margin-top: 0; }
  .hp-pl { gap: 6px; padding: 6px 8px; }
  .hp-av { width: 30px; height: 30px; font-size: 19px; }
  .hp-nm b { white-space: normal; overflow: visible; text-overflow: clip; font-size: 12px; line-height: 1.1; overflow-wrap: anywhere; }
  .hp-sc { font-size: 11px; }
  #game h2 { white-space: normal; overflow: visible; text-overflow: clip; line-height: 1.1; }
  .game-players { white-space: normal; overflow: visible; text-overflow: clip; line-height: 1.1; }

  /* Nuée Stellaire */
  .swarm-wrap { width: calc(100vw - 12px); }
  .swarm-wrap canvas { width: 100%; height: auto; }
  .swarm-hud { flex-wrap: wrap; justify-content: center; padding: 4px 6px; }
  .swarm-wrap:has(.sw-levels:not([hidden])) { min-height: min(480px, calc(100dvh - 150px)); max-height: calc(100dvh - 130px); }
  .sw-levels { padding: 12px 10px; overflow-y: auto; gap: 8px; }
  .sw-levels h3 { font-size: 24px; }
  .sw-row { flex-wrap: wrap; gap: 8px; padding: 8px 10px; }
  .sw-gal { width: 100%; }
  .sw-cells { flex-wrap: wrap; gap: 8px; }
  .lv { width: 46px; height: 46px; font-size: 17px; }
}

@media (max-width: 700px) and (orientation: portrait) {
  body:has(.online-chat) #game { padding-bottom: 100px; }
  .tetris-wrap.is-versus:has(.tetris-touch-player:only-child) { align-items: flex-start; justify-content: center; }
  .tetris-wrap.is-versus:has(.tetris-touch-player:only-child) > canvas:first-of-type { width: calc((100vw - 44px) * 0.63); flex: none; }
  .tetris-wrap.is-versus:has(.tetris-touch-player:only-child) > canvas:nth-of-type(2) { width: calc((100vw - 44px) * 0.37); flex: none; }
  .tetris-wrap.is-versus:has(.tetris-touch-player:only-child) .tetris-touch-area { grid-template-columns: 1fr; }
  .tetris-wrap.is-versus:has(.tetris-touch-player:only-child) .tetris-touch-controls .btn { min-height: 54px; }
}

@media (max-width: 700px) and (orientation: portrait) {
  .rotate-hint { display: block; padding: 6px 10px; border-radius: 10px; background: #ffc75f22; border: 1px solid #ffc75f55; color: #ffe29a; font-size: 12px; text-align: center; }
  .snake-touch-controls { grid-template-columns: repeat(3, 76px); gap: 8px; }
  .snake-touch-controls .btn { min-height: 60px; font-size: 24px; }
  /* Menu des niveaux de Nuée Stellaire : occupe toute la hauteur de l'écran */
  .swarm-wrap:has(> .sw-levels:not([hidden])) { height: calc(100dvh - 64px); }
  .swarm-wrap:has(> .sw-levels:not([hidden])) > :not(.sw-levels) { visibility: hidden; }
}

/* Téléphone en paysage : plateau à gauche, panneau à droite */
@media (max-height: 600px) and (pointer: coarse) and (orientation: landscape) {
  #game header { min-height: 40px; padding-top: 3px; padding-bottom: 3px; }
  #stage { padding: 4px; }
  .snake-wrap { width: min(630px, calc(100dvh - 84px)); gap: 4px; }
  /* Snake : plateau à gauche, flèches à droite */
  .snake-wrap:has(.snake-touch-controls) { width: auto; display: grid; grid-template-columns: auto auto; grid-template-areas: "bar ." "board ctrl"; align-items: center; column-gap: 18px; }
  .snake-wrap:has(.snake-touch-controls) > .scorebar { grid-area: bar; justify-self: center; }
  .snake-wrap:has(.snake-touch-controls) > canvas { grid-area: board; width: calc(100dvh - 76px); height: auto; }
  .snake-wrap .snake-touch-controls { grid-area: ctrl; grid-template-columns: repeat(3, 64px); gap: 8px; }
  .snake-wrap .snake-touch-controls .btn { min-height: 58px; font-size: 22px; }
  .scorebar { gap: 12px; font-size: 13px; }
  .scorebar b { font-size: 17px; }
  .mines-wrap { width: calc(100vw - 12px); gap: 6px; }
  .mgrid { --cs: min(26px, calc((100dvh - 100px) / 16)) !important; }
  .sol { --cw: min(92px, calc((100dvh - 130px) / 3.3)); --gap: 4px; width: max-content; }
  /* Patience : pioche à gauche, fondations à droite, colonnes au centre */
  .sol-board { --sp: .75; grid-template-columns: calc(var(--cw) * 1.5) repeat(8, var(--cw)); }
  .sol-board > div:nth-child(3) { display: none; }
  .sol-board > [data-zone="stock"] { grid-column: 1; grid-row: 1; }
  .sol-board > [data-zone="waste"] { grid-column: 1; grid-row: 2; }
  .sol-board > [data-zone="found"] { grid-column: 9; }
  .sol-board > [data-zone="found"][data-i="0"] { grid-row: 1; }
  .sol-board > [data-zone="found"][data-i="1"] { grid-row: 2; }
  .sol-board > [data-zone="found"][data-i="2"] { grid-row: 3; }
  .sol-board > [data-zone="found"][data-i="3"] { grid-row: 4; }
  .sol-board > .tab { grid-row: 1 / span 4; height: calc(var(--cw) * 5.05); }
  .sol-board > .tab[data-i="0"] { grid-column: 2; } .sol-board > .tab[data-i="1"] { grid-column: 3; }
  .sol-board > .tab[data-i="2"] { grid-column: 4; } .sol-board > .tab[data-i="3"] { grid-column: 5; }
  .sol-board > .tab[data-i="4"] { grid-column: 6; } .sol-board > .tab[data-i="5"] { grid-column: 7; }
  .sol-board > .tab[data-i="6"] { grid-column: 8; }
  /* Petits Chevaux : réglages sur deux colonnes */
  .horses-wrap.is-setup .hs-card { width: 100%; margin: 0; padding: 16px 20px; display: grid; grid-template-columns: 1fr 1fr; column-gap: 22px; row-gap: 8px; align-content: center; }
  .hs-card > h3 { grid-column: 1; grid-row: 1; font-size: 24px; }
  .hs-card > .rules { grid-column: 1; grid-row: 2 / span 5; align-self: start; }
  .hs-card > :not(h3):not(.rules) { grid-column: 2; }
  .hs-card .hs-bots { margin: 0; justify-content: center; }
  .horses-wrap { width: auto; flex-direction: row; align-items: center; gap: 10px; }
  .horses-wrap.is-setup { width: min(820px, calc(100vw - 24px)); flex-direction: column; }
  .horses-wrap canvas, .hp-board { width: calc(100vh - 70px); width: calc(100dvh - 70px); height: auto; }
  .hp-panel { width: 290px; }
  .hp-panel .dice-stage { height: 90px; }
  .swarm-wrap { width: min(960px, calc((100dvh - 90px) * 1.6), calc(100vw - 12px)); }
  /* Nuée Stellaire : commandes en colonne à gauche, carte sur toute la hauteur */
  .swarm-wrap { width: auto; flex-direction: row; align-items: stretch; gap: 8px; }
  .swarm-wrap canvas { width: min(calc((100dvh - 56px) * 1.6), calc(100vw - 200px)); height: auto; }
  .swarm-hud { flex-direction: column; flex-wrap: nowrap; justify-content: center; align-self: stretch; width: 172px; gap: 6px; padding: 8px; }
  .swarm-hud .sep { display: none; }
  .swarm-hud .sw-title { text-align: center; font-size: 13px; }
  .swarm-hud .sw-legend { margin: 0; flex-wrap: wrap; justify-content: center; gap: 6px; }
  .swarm-hud .sw-clock { margin: 0; }
  .swarm-hud .btn { width: 100%; }
  .tetris-wrap:has(.tetris-touch-player:only-child) { width: calc(100vw - 28px); flex-direction: row; flex-wrap: nowrap; align-items: center; justify-content: center; gap: 10px; }
  .tetris-wrap:has(.tetris-touch-player:only-child) > canvas { width: calc((100vh - 56px) * 0.733); width: calc((100dvh - 56px) * 0.733); max-width: calc((100vw - 190px) / 2); flex: none; height: auto; }
  .tetris-wrap:has(.tetris-touch-player:only-child):not(.is-versus) > canvas { max-width: calc(100vw - 190px); }
  .tetris-wrap:has(.tetris-touch-player:only-child) .tetris-touch-area { width: 150px; flex: none; }
  .tetris-wrap:has(.tetris-touch-player:only-child) .tetris-touch-controls { grid-template-columns: repeat(2, 1fr); gap: 6px; width: 150px; }
  .tetris-wrap:has(.tetris-touch-player:only-child) .tetris-touch-controls .btn { min-height: 40px; padding: 6px 0; }
  body .online-chat { bottom: 8px; right: 8px; }
  body .chat-feed { bottom: 8px; }
  .chat-panel { max-height: 50dvh; }
  .snd-toast { top: 46px; }
}
