/* The part every game shares: the page, top bar, players and score, status line, buttons and the
   options sheet. A game page is one fixed frame exactly as tall as the visible screen and it never
   scrolls: the board takes whatever room the rest leaves, so a browser bar makes the board
   smaller instead of covering the buttons. Each game adds its own board stylesheet, which sizes
   the board from the space it is given (.stage). The page that lists the games is the only one
   that scrolls (see picker.css). */
:root {
  --bg: #0c1020;
  --panel: #171b2b;
  --line: #2a3048;
  --ink: #f4ede6;
  --dim: #a9adc0;
  --p1: #acf8b4;     /* mint: player 1 */
  --p2: #ffa470;     /* warm orange: player 2 */
  --focus: #ffffff;
  --gap: clamp(8px, 2.2vw, 14px);
}

* { box-sizing: border-box; }

html { height: 100%; overflow: hidden; color-scheme: dark; -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: rgba(172, 248, 180, .2); }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.skip { position: absolute; left: 12px; top: -80px; z-index: 5; background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 8px; }
.skip:focus { top: 12px; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

/* top bar */
.top {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 16px;
  border-bottom: 1px solid var(--line);
}
.home { display: inline-flex; align-items: center; min-height: 44px; color: var(--ink); text-decoration: none; font-weight: 700; }
.home b { color: var(--p1); }
.home:hover { text-decoration: underline; text-underline-offset: 4px; }
h1 { margin: 0; font-size: 1.05rem; font-weight: 600; color: var(--dim); }

/* The frame under the top bar: players, status, the board's stage (the row that stretches) and
   the buttons. */
main {
  flex: 1;
  min-height: 0;
  width: 100%;
  margin: 0 auto;
  padding: 10px 16px 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "players" "status" "stage" "actions";
  justify-items: center;
  gap: 10px;
}
.players { grid-area: players; }
.status { grid-area: status; }
.stage { grid-area: stage; }
.actions { grid-area: actions; }
.players, .actions { width: min(100%, 560px); }

/* The board's stage is a size container: the board stylesheets size the board in cqw and cqh, the
   stage's own width and height, so the board always fits the room that is left. */
.stage {
  container-type: size;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.board { flex: none; }

/* players and score: one compact row, each player's piece, name and wins on one line */
.players { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); gap: 8px; }
.player, .draws {
  height: 56px;
  min-width: 0;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 14px;
}
.player {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 6px;
  padding: 0 10px 0 8px;
}
.player.p1.is-turn { border-color: var(--p1); }
.player.p2.is-turn { border-color: var(--p2); }
.draws { display: grid; align-content: center; justify-items: center; padding: 0 10px; }
.draws-label { color: var(--dim); font-size: .75rem; line-height: 1.2; }

.mark { display: block; width: 22px; height: 22px; position: relative; }

/* The newer games show a running count under each player's piece (pieces left, boxes, pairs). */
.tally { display: grid; justify-items: center; row-gap: 3px; min-width: 22px; }
.tally .mark { width: 20px; height: 20px; }
.count { font-size: .85rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.p1 .count { color: var(--p1); }
.p2 .count { color: var(--p2); }

/* The two kinds of pieces in the newer games: player 1 is a solid mint piece, player 2 is orange
   with a ring in it, so the two can be told apart without relying on color. The ring is sized
   from the piece, so it works at any size. */
.chip-1, .chip-2 { position: relative; border-radius: 50%; }
.chip-1 { background: var(--p1); }
.chip-2 { background: var(--p2); }
.chip-2::before, .chip-2::after { content: ""; position: absolute; border-radius: 50%; }
.chip-2::before { inset: 10%; background: #8a4a22; }
.chip-2::after { inset: 21%; background: var(--p2); }

.name {
  width: 100%;
  min-width: 0;
  height: 44px;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 6px;
  color: var(--ink);
  font: inherit;
  font-size: .95rem;
  font-weight: 600;
  text-align: left;
  text-overflow: ellipsis;
  padding: 4px 2px;
}
.name:hover { border-bottom-color: var(--line); }
.name:focus { border-bottom-color: var(--dim); }

.score { font-size: 1.4rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.p1 .score { color: var(--p1); }
.p2 .score { color: var(--p2); }
.draws .score { color: var(--dim); font-size: 1.2rem; }

/* The status line always has room for two lines, so a long message never moves the board. */
.status {
  margin: 0;
  width: 100%;
  height: 2.7em;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.35;
  text-wrap: balance;
}

/* buttons: undo, the main one, options */
.actions { display: grid; grid-template-columns: 1fr 1.4fr 1fr; gap: 8px; }
.actions-two { grid-template-columns: 1.6fr 1fr; }   /* a game with nothing to undo */
.btn {
  appearance: none;
  min-width: 0;
  min-height: 52px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  padding: 0 8px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover:not(:disabled) { border-color: var(--dim); }
.btn:disabled { color: #6b7088; cursor: default; }
.btn-main { background: var(--p1); border-color: var(--p1); color: #0c1020; }
.btn-main:hover:not(:disabled) { background: #c5fbcb; border-color: #c5fbcb; }
.btn-quiet { min-height: 44px; background: transparent; color: var(--dim); font-weight: 500; }

/* The options sheet: who is playing, the game's own choices, reset, and how to win. It stays
   inside the safe area and scrolls inside itself if the screen is shorter than it is. */
.sheet {
  inset: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  margin: auto;
  width: min(100vw - 32px - env(safe-area-inset-left) - env(safe-area-inset-right), 440px);
  max-width: none;
  max-height: calc(100vh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow: auto;
  padding: 0;
  background: var(--panel);
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: 18px;
}
.sheet::backdrop { background: rgba(5, 7, 16, .72); }
.sheet-body { display: grid; gap: 12px; padding: 16px; }
.sheet h2 { margin: 0; font-size: 1.15rem; font-weight: 700; }
.how { margin: 0; color: var(--dim); font-size: .95rem; line-height: 1.45; }
.mode { margin: 0; min-width: 0; padding: 6px 12px 8px; border: 2px solid var(--line); border-radius: 14px; display: flex; flex-wrap: wrap; gap: 0 16px; }
.mode legend { padding: 0 6px; color: var(--dim); font-size: .9rem; }
.mode label { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.mode input { width: 20px; height: 20px; accent-color: var(--p1); margin: 0; }

.foot { padding: 10px 16px 18px; text-align: center; color: var(--dim); font-size: .9rem; }
.foot p { margin: 0; }

.btn, .name, .mode label { -webkit-user-select: none; user-select: none; }
.name { -webkit-user-select: text; user-select: text; }

/* A short, narrow screen (a folded phone with the browser's bars showing): trim the chrome so the
   board gets the room. */
@media (max-height: 640px) and (max-width: 719px) {
  .top { padding-block: 0; }
  main { gap: 8px; padding-block: 8px 10px; }
  .btn { min-height: 46px; }
}
/* a very narrow phone: the names get every pixel */
@media (max-width: 359px) {
  .player { padding: 0 6px 0 5px; column-gap: 4px; }
  .draws { padding: 0 6px; }
  .name { font-size: .88rem; }
}

/* A wide screen (an unfolded phone, a tablet on its side, a desktop): the board's stage takes the
   left half at full height and everything else sits in the right half, so on a folding phone the
   board is on one screen and the controls on the other. Still one frame, no scrolling. */
@media (min-width: 720px) and (min-aspect-ratio: 11/10) {
  main {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto auto auto minmax(0, 1fr);
    grid-template-areas:
      "stage ."
      "stage players"
      "stage status"
      "stage actions"
      "stage .";
    column-gap: 24px;
    row-gap: 12px;
    padding: 12px 24px;
  }
  .players, .status, .actions { width: min(100%, 440px); }
  /* the roomier players panel: piece, name and wins stacked */
  .player, .draws { height: auto; border-radius: 16px; }
  .player { grid-template-columns: minmax(0, 1fr); justify-items: center; padding: 8px 8px 6px; }
  .draws { padding-inline: 14px; }
  .draws-label { font-size: .85rem; }
  .tally { display: inline-flex; align-items: center; gap: 8px; height: 22px; }
  .tally .mark { width: 22px; height: 22px; }
  .count { font-size: 1rem; }
  .name { text-align: center; font-size: 1rem; padding: 4px 6px; }
  .score, .draws .score { font-size: 1.7rem; }
  .status { font-size: 1.2rem; }
}

/* Two screens side by side (a Surface Duo spanned, a fold held open): the board is centered on
   the first screen and the controls on the second, with nothing under the hinge. The columns are
   the two screens and the hinge between them, as the browser reports them. Not seen on a real
   device yet; browsers that report no segments never get here. */
@media (horizontal-viewport-segments: 2) {
  body { padding-left: 0; padding-right: 0; }
  main {
    grid-template-columns: env(viewport-segment-width 0 0) calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0)) env(viewport-segment-width 1 0);
    grid-template-rows: minmax(0, 1fr) auto auto auto minmax(0, 1fr);
    grid-template-areas:
      "stage . ."
      "stage . players"
      "stage . status"
      "stage . actions"
      "stage . .";
    column-gap: 0;
    row-gap: 12px;
    padding: 12px 0;
  }
  .stage { padding: 0 16px; }
  .players, .status, .actions { width: min(100% - 32px, 440px); }
  .player, .draws { height: auto; border-radius: 16px; }
  .player { grid-template-columns: minmax(0, 1fr); justify-items: center; padding: 8px 8px 6px; }
  .draws { padding-inline: 14px; }
  .tally { display: inline-flex; align-items: center; gap: 8px; height: 22px; }
  .name { text-align: center; font-size: 1rem; }
  .score, .draws .score { font-size: 1.7rem; }
  /* the options sheet opens on the second screen */
  .sheet { left: env(viewport-segment-left 1 0); width: min(env(viewport-segment-width 1 0) - 32px, 440px); }
}

/* Two screens one above the other (a fold held like a laptop, a Duo turned): the top bar and the
   board on the top screen, the controls on the bottom one. Not seen on a real device yet. */
@media (vertical-viewport-segments: 2) {
  .top { height: 54px; padding-block: 0; }
  main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: calc(env(viewport-segment-height 0 0) - 54px - 20px) calc(env(viewport-segment-top 0 1) - env(viewport-segment-bottom 0 0)) auto auto auto;
    grid-template-areas: "stage" "." "players" "status" "actions";
    align-content: start;
    gap: 10px;
    padding: 10px 16px;
  }
  .sheet { top: env(viewport-segment-top 0 1); }
}
