:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --text: #1f1f1d;
  --muted: #64625c;
  --border: #d4d1c7;
  --accent: #3c5bd6;
  --board: #17171c;
  --gap: 0.75rem;
  /* The room the board leaves: for the stats row and the pad in portrait, for each side column in landscape. */
  --stats: 3.75rem;
  --pad: 3rem;
  --side: 8.5rem;
  --vh: 100vh;
  color-scheme: light dark;
}

/* On a phone vh counts the space under the address bar too, and the pad would fall below the screen. */
@supports (height: 100dvh) {
  :root { --vh: 100dvh; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111113;
    --surface: #1f1f23;
    --text: #ecebe6;
    --muted: #a3a19a;
    --border: #3a3a40;
  }
}

* { box-sizing: border-box; }

/* Otherwise a rule that sets display, like .overlay's, shows what is hidden. */
[hidden] { display: none !important; }

html, body { overscroll-behavior: none; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.game {
  display: grid;
  grid-template-areas: "stats" "board" "pad";
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  align-content: center;
  gap: var(--gap);
  min-height: var(--vh);
  padding: var(--gap);
}

.stats {
  grid-area: stats;
  display: flex;
  justify-content: center;
  gap: min(0.75rem, 2.5vw);
  height: var(--stats);
  margin: 0;
  text-align: center;
}

dt {
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

dd {
  margin: 0;
  font-size: min(1.25rem, 5vw);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.next canvas {
  display: block;
  width: 3.5rem;
  height: 2.5rem;
  border-radius: 4px;
  background: var(--board);
}

.board {
  grid-area: board;
  position: relative;
  /* The height the stats, the pad and the gaps leave, as long as the width fits the screen. */
  --board-h: min(var(--vh) - var(--stats) - var(--pad) - 4 * var(--gap), (100vw - 2 * var(--gap)) * 1.8, 42rem);
  width: calc(var(--board-h) / 1.8);
  height: var(--board-h);
}

.board canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  background: var(--board);
}

.overlay {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  border-radius: 6px;
  background: rgba(10, 10, 14, 0.72);
  color: #f4f3ef;
  text-align: center;
}

.overlay h2 { margin: 0 0 0.5rem; font-size: 1.4rem; }
.overlay p { margin: 0 0 1rem; }

.overlay button {
  min-height: 2.75rem;
  padding: 0.5rem 1rem;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #ffffff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.keys { display: none; }

.pad {
  grid-area: pad;
  display: grid;
  grid-template-columns: repeat(6, minmax(2.75rem, 1fr));
  gap: 0.375rem;
  width: min(100%, 26rem);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.pad button {
  height: var(--pad);
  min-width: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1.25rem;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.pad [data-action="drop"], .pad [data-action="pause"] { font-size: 0.875rem; }
.pad button:active { background: var(--border); }

/* Only a mouse and keyboard can do without the pad; a device that may be a phone keeps it. */
@media (hover: hover) and (pointer: fine) {
  :root { --pad: 0rem; }
  .pad { display: none; }
  .keys { display: block; }
}

@media (orientation: landscape) {
  .game {
    grid-template-areas: "stats board pad";
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    column-gap: 1.5rem;
  }

  .stats {
    display: grid;
    grid-template-columns: repeat(2, auto);
    gap: 0.5rem 1.25rem;
    justify-self: end;
    height: auto;
    text-align: left;
  }

  .next { grid-column: 1 / -1; }
  .next canvas { width: 5rem; height: 3.5rem; }

  .board {
    --board-h: min(var(--vh) - 2 * var(--gap), (100vw - 2 * var(--side) - 2 * 1.5rem - 2 * var(--gap)) * 1.8, 42rem);
  }

  .pad {
    grid-template-columns: repeat(2, 3.5rem);
    justify-self: start;
    width: auto;
  }
}
