body {
  -webkit-touch-callout: none !important;
}
a {
  user-select: none !important;
  -webkit-user-select: none !important;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #111;
  color: #fff;
  /* The visible height: 100dvh leaves out the browser's bars, which 100vh
     includes on phones; browsers without dvh take the 100vh */
  max-height: 100vh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  position: fixed;
  width: 100%;
}

#waiting-screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  padding: 20px;
  text-align: center;
}

#waiting-screen h3 {
  font-size: 1.5em;
  margin-bottom: 20px;
}

#waiting-screen h1 {
  font-size: 3em;
  margin-bottom: 4px;
}

#waiting-screen p {
  font-size: 1.2em;
  color: #aaa;
  margin: 10px 0;
}

/* Warning and note styles */
#waiting-screen .warning-text,
.connection-lost .warning-text {
  color: #ff6b6b;
  font-weight: 600;
  font-size: 1.3em;
  margin-top: 25px;
  text-shadow: 0 0 10px rgba(255, 107, 107, 0.3);
}

#waiting-screen .note-text {
  color: #aaa;
  font-size: 1.1em;
  margin: 10px 0 20px;
}

#waiting-screen .position {
  font-size: 2em;
  color: #fff;
  margin: 20px 0;
}

/* A dropped connection during the turn: over the countdown or the game and
   the rules, under game over and quit (3000) */
.connection-lost {
  position: fixed;
  inset: 0;
  z-index: 2600;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  background: rgba(0, 0, 0, 0.9);
}

.connection-lost h3 {
  font-size: 1.5em;
}

.connection-lost p {
  font-size: 1.2em;
  color: #ccc;
}

.connection-lost .warning-text {
  margin-top: 10px;
}

.spinner {
  width: 36px;
  height: 36px;
  margin-bottom: 6px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-duration: 3s;
  }
}

/* Both are shown and hidden with the hidden attribute */
.connection-lost[hidden],
.modal[hidden] {
  display: none;
}

.game-info {
  text-align: center;
  background: rgba(0, 0, 0, 0.95);
  padding: 8px;
  border-radius: 8px;
  border: 1px solid #333;
  z-index: 100;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.info-item {
  text-align: center;
  background: rgba(0, 0, 0, 0.7);
  padding: 6px;
  border-radius: 4px;
  border: 1px solid #444;
}

.info-value {
  font-size: 1.4em;
  font-weight: bold;
  margin-top: 5px;
}

.info-level {
  font-size: 2.5em;
  font-weight: bold;
  margin-bottom: 5px;
}

/* The card gives way first when the screen is short, so the pad never does */
.next-piece-preview {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 0;
  max-height: 100%;
  background: rgba(0, 0, 0, 0.95);
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #333;
  text-align: center;
}

.next-piece-preview h3 {
  flex: none;
  margin-bottom: 10px;
  color: #fff;
}

/* Drawn at 120 px; shrinks with the card and keeps its square */
canvas.next-piece {
  flex: 0 1 auto;
  min-height: 0;
  height: 120px;
  width: auto;
  max-width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  border: 1px solid #333;
  background: #111;
}

.controls {
  flex: none;
  width: 100%;
  /* Clear of the home bar on phones that have one */
  padding: 16px max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(0, 0, 0, 0.9);
  border-top: 1px solid #333;
  transition: opacity 0.3s ease;
}

.keyboard-controls {
  display: none;
  color: #fff;
  text-align: center;
}

.container {
  padding: 16px 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto auto;
  gap: 8px;
  /* Thumb-sized on a tablet too */
  max-width: 520px;
  margin: 0 auto;
}

.btn {
  background: #222;
  color: #fff;
  padding: 25px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: bold;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
  position: relative;
  transition: background 0.1s, transform 0.1s, border-color 0.1s;
  border: 1px solid #444;
  /* Holding a button repeats the move: a long press must not select text,
     open the callout or start a drag instead */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* The arrows are pictures: big enough to see at night, and never the target
   of a long press (iOS would offer to save the image instead of repeating) */
.btn img {
  width: 28px;
  height: 28px;
  pointer-events: none;
  -webkit-user-drag: none;
}

.btn:active {
  background: #3a3a3a;
  border-color: #888;
  transform: scale(0.96);
}

.btn-left {
  grid-column: 1;
  grid-row: span 2;
}
.btn-right {
  grid-column: 3;
  grid-row: span 2;
}
.btn-down {
  grid-column: 2;
  grid-row: 2;
}
.btn-rotate {
  grid-column: 2;
  grid-row: 1;
}
.btn-drop {
  grid-column: 1 / span 3;
  grid-row: 3;
  padding: 18px;
}

/* A mouse and a keyboard: the keys play, so a hint takes the pad's place.
   Every touch screen keeps the pad, whatever its size or orientation. */
@media (hover: hover) and (pointer: fine) {
  .keyboard-controls {
    display: block;
  }
  .keyboard-controls h2 {
    margin-bottom: 10px;
  }
  .keyboard-controls ul {
    list-style: none;
  }
  .keyboard-controls li {
    margin: 5px 0;
  }
  .buttons {
    display: none;
  }
}

.hidden {
  display: none !important;
}

/* The playing screen fills what is visible, the pad at the bottom */
.game-elements {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;

  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-top: 16px;
  overflow: hidden;
}

.game-elements > .info-item,
.game-info {
  flex: none;
}

/* The card takes the room left between the scores and the pad */
.game-elements .next-piece-preview {
  flex: 0 1 auto;
  margin: auto 0;
}

/* ---- One screen at a time (body data-screen, set by controllerDisplay) ----
   connecting, waiting, countdown, game, over. The others are out of the way
   entirely, and the one shown fades in, so nothing flashes between them. */

.connecting-screen {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: #111;
  text-align: center;
}

.connecting-screen p {
  font-size: 1.2em;
  color: #aaa;
}

.connecting-screen h1 {
  font-size: 4em;
  line-height: 1;
  margin-bottom: 16px;
}

body:not([data-screen="connecting"]) .connecting-screen,
body:not([data-screen="waiting"]) #waiting-screen {
  display: none;
}

body:not([data-screen="countdown"]) .canvas-block {
  visibility: hidden;
}

body[data-screen="game"] .game-elements {
  opacity: 1;
  pointer-events: auto;
}

body:not([data-screen="game"]) .game-elements {
  visibility: hidden;
}

body[data-screen="connecting"] .connecting-screen,
body[data-screen="waiting"] #waiting-screen,
body[data-screen="countdown"] .canvas-block {
  animation: screen-in 0.25s ease both;
}

@keyframes screen-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  body[data-screen] * {
    animation-duration: 0.01ms !important;
  }
}

.canvas-block {
  padding: 20px 0px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  width: 100%;
  height: 100%;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* The countdown fits a short screen too */
#gameBoard {
  max-height: calc(100vh - 80px);
  max-height: calc(100dvh - 80px);
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 3000;
  padding: 20px;
  text-align: center;
}

/* Above everything: the page can't play */
.setup-error {
  z-index: 4000;
}

.modal-content {
  width: 100%;
  max-width: 360px;
  background: #1a1a1a;
  padding: 24px 20px;
  border-radius: 16px;
  border: 1px solid #333;
}

.modal-content p {
  margin-bottom: 8px;
}

.modal-content h2 {
  margin-bottom: 16px;
}

.menu-button {
  background: #2a2a2a;
  border: 1px solid #444;
  color: #fff;
  padding: 15px 30px;
  border-radius: 12px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 20px;
  touch-action: manipulation;
}

/* Game over: the score big, the rest small, then what to do next */
.game-over .modal-content {
  padding: 28px 20px 20px;
}

.game-over-reason {
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.55);
}

.game-over h2 {
  font-size: 28px;
  margin-bottom: 18px;
}

.final-score span {
  display: block;
  font-size: 56px;
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.final-score small,
.final-stats small {
  display: block;
  margin-top: 4px;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.55);
}

.final-stats {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin: 20px 0 24px;
}

.final-stats span {
  font-size: 24px;
  font-weight: 700;
}

.game-over .modal-actions .menu-button {
  width: 100%;
  margin-top: 0;
}

.game-over-note {
  margin: 14px 0 0;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.45);
}

/* The quit and stop-waiting questions rise from the bottom, where the thumb
   already is, over the game or the queue instead of blacking it out */
.sheet {
  justify-content: flex-end;
  padding: 0;
  background: rgba(0, 0, 0, 0.6);
}

.sheet .modal-content {
  max-width: 520px;
  padding: 24px 20px calc(20px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  border-bottom: none;
  animation: sheet-up 0.18s ease-out;
}

.sheet h2 {
  font-size: 22px;
  margin-bottom: 8px;
}

.sheet p {
  margin-bottom: 20px;
  color: rgba(255, 255, 255, 0.7);
}

/* Under "How to play" on the waiting screen: there, but quieter */
.leave-queue {
  margin-top: 14px;
  padding: 10px 18px;
  font-size: 15px;
  color: rgba(255, 255, 255, 0.6);
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 22px;
  cursor: pointer;
  touch-action: manipulation;
}

.leave-queue:active {
  background: rgba(255, 255, 255, 0.08);
}

@keyframes sheet-up {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

/* Quitting: a small button out of the way of the controls, then a question
   where staying is the big easy answer and quitting the small one */
.quit-button,
.help-button {
  position: fixed;
  top: 16px;
  z-index: 10;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.55);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  cursor: pointer;
}

.quit-button {
  left: 16px;
  padding: 8px 14px;
}

/* The rules during a game: the quit button's round mirror, top right */
.help-button {
  right: 16px;
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 16px;
  font-weight: bold;
  border-radius: 50%;
}

/* A larger target than it looks, so it is easy to hit without aiming */
.help-button::after {
  content: "";
  position: absolute;
  inset: -6px;
}

/* How to play, under the queue position */
.rules-open {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 18px;
  padding: 12px 22px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 24px;
  cursor: pointer;
}

.rules-open::before {
  content: "?";
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  font-size: 13px;
  border: 1px solid currentColor;
  border-radius: 50%;
}

/* Small phones: the waiting screen and its button still fit without
   scrolling, and the scores, the next piece and the whole pad fit in a game */
@media (max-height: 640px) {
  #waiting-screen h3 {
    margin-bottom: 8px;
  }
  #waiting-screen h1 {
    font-size: 2.4em;
  }
  #waiting-screen p {
    margin: 6px 0;
    font-size: 1.05em;
  }
  #waiting-screen .warning-text {
    margin-top: 12px;
    font-size: 1.15em;
  }
  #waiting-screen .note-text {
    margin-bottom: 4px;
  }
  .rules-open {
    margin-top: 10px;
  }

  .game-elements {
    gap: 6px;
    padding-top: 10px;
  }
  .game-elements > .info-item {
    padding: 2px 10px;
  }
  .info-level {
    font-size: 1.9em;
    margin-bottom: 0;
  }
  .game-info {
    padding: 6px;
  }
  .info-item {
    padding: 4px 6px;
  }
  .info-value {
    font-size: 1.2em;
    margin-top: 2px;
  }
  .next-piece-preview {
    padding: 8px 12px;
  }
  .next-piece-preview h3 {
    font-size: 15px;
    margin-bottom: 6px;
  }
  .controls {
    padding-top: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .btn {
    padding: 14px;
  }
  .btn-drop {
    padding: 13px;
  }
}

/* A phone on its side: the scores and the next piece on the left, the pad
   on the right, below the ? button */
@media (orientation: landscape) and (max-height: 500px) {
  #waiting-screen {
    padding: 10px;
  }
  #waiting-screen h3 {
    font-size: 1.25em;
    margin-bottom: 2px;
  }
  #waiting-screen h1 {
    font-size: 1.9em;
    margin-bottom: 0;
  }
  #waiting-screen p {
    margin: 3px 0;
    font-size: 1em;
  }
  #waiting-screen .warning-text {
    margin-top: 6px;
    font-size: 1.05em;
  }
  #waiting-screen .note-text {
    margin: 3px 0 0;
    font-size: 0.95em;
  }
  .rules-open {
    margin-top: 8px;
    padding: 8px 18px;
  }

  .game-elements {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "window pad"
      "info pad"
      "next pad";
    justify-items: center;
    column-gap: 16px;
    row-gap: 6px;
    padding: 10px max(16px, env(safe-area-inset-right)) 0
      max(16px, env(safe-area-inset-left));
  }
  .game-elements > .info-item {
    grid-area: window;
  }
  .game-info {
    grid-area: info;
  }
  .game-elements .next-piece-preview {
    grid-area: next;
    align-self: center;
    margin: 0;
    padding: 6px 12px;
  }
  .next-piece-preview h3 {
    font-size: 14px;
    margin-bottom: 4px;
  }
  .controls {
    grid-area: pad;
    align-self: stretch;
    display: flex;
    flex-direction: column;
    justify-content: center;
    /* Starts below the ? button at the top right */
    padding: 48px 0 max(10px, env(safe-area-inset-bottom));
    background: none;
    border-top: none;
  }
  /* Fills the column for big thumb targets, but not taller than a portrait pad */
  .buttons {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    max-height: min(100%, 340px);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr) auto;
    gap: 6px;
  }
  .btn {
    padding: 10px;
  }
  .btn-drop {
    padding: 12px;
  }
}

/* Two full-width buttons: staying is the bright one on top, quitting the quiet
   one under it, far enough apart that a slip doesn't hit the wrong one */
.modal-actions {
  display: grid;
  gap: 12px;
}

.menu-button.keep-playing {
  width: 100%;
  margin-top: 0;
  padding: 16px;
  border: none;
  background: #fff;
  color: #000;
  font-size: 17px;
  font-weight: bold;
}

.quit-game {
  width: 100%;
  padding: 14px;
  font-size: 16px;
  color: #ff7070;
  background: transparent;
  border: 1px solid rgba(255, 112, 112, 0.6);
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
}

.quit-game:active {
  background: rgba(255, 112, 112, 0.12);
}
