/* The results screen: how the game ended, what everyone has to show for it, and the way into
   the next one. Three bands: the final score (with the winner's moment), every player's words
   (the one band that scrolls, if four players' words need it), and the buttons, which are on
   screen from the first instant and stay where the thumb is.

   The winner's score sits on a torch-gold coin under a crown; everyone else's in a well sunk
   into the stage. Words are drawn as they lay on the table: tiles tinted with the colour of
   whoever held them at the end. A word that was stolen carries a tag with the mark of the
   player it was taken from, so the to and fro of the game can be read off the final table.

   Rematch is the one gold button. The level offer's Accept is a tile, not gold.

   Motion: the scores count up, the coin and the crown land, the words follow. views/results.js
   sets --results-count (how long the count takes) and finishes everything at once on a tap.
   Every colour, radius, shadow, type size and timing is a token from tokens.css. */

.results {
  --coin: 60px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-3);
}

/* --- The final score ---------------------------------------------------------------------- */

.results__head {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  gap: var(--space-1);
  text-align: center;
}

.final {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: var(--space-2);
  width: 100%;
  margin-bottom: var(--space-2);
  list-style: none;
}

.final__seat {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  flex: 0 1 136px;
  gap: var(--space-2);
  min-width: 0;
}

.final__who {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  min-width: 0;
  min-height: 28px;   /* a face's height, so names line up with or without one */
  color: var(--text-soft);
  font-size: var(--text-s);
  font-weight: var(--weight-label);
  line-height: var(--leading-snug);
}

.final__who .player-badge {
  width: 20px;
  height: 20px;
  border-radius: calc(var(--radius-s) * 0.6);
}

.final__who .player-badge .player-mark {
  width: 10px;
  height: 10px;
}

.final__who .portrait {
  width: 28px;
  height: 28px;
}

.final__name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* The score, in a well sunk into the stage. The winner's is on a coin instead: a torch-coloured
   disc with a lip, like every solid piece here. The coin is drawn behind the figure so it can
   land after the count. */
.final__coin {
  position: relative;
  isolation: isolate;
  display: grid;
  place-items: center;
  width: var(--coin);
  height: var(--coin);
  margin-top: 14px;   /* the crown's room, kept for every player so the figures line up */
  margin-bottom: var(--lip-m);
  color: var(--text);
  font-size: var(--text-2xl);
  font-weight: var(--weight-display);
  font-variant-numeric: tabular-nums;
  line-height: var(--leading-flat);
}

.final__coin::before,
.final__coin::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: var(--radius-round);
}

.final__coin::before {
  background: var(--well);
}

.final__seat.is-winner .final__coin {
  color: var(--ink);
}

.final__seat.is-winner .final__coin::after {
  background: var(--torch);
  box-shadow: 0 var(--lip-m) 0 var(--torch-rim), var(--shadow-soft);
}

.final__crown {
  position: absolute;
  bottom: 100%;
  left: 50%;
  width: 32px;
  height: 24px;
  margin: 0 0 -7px -16px;
  color: var(--sun);
}

.final__seat.is-gone {
  opacity: 0.55;
}

/* More than two players: the same pieces, closer together. */
.final--many {
  gap: var(--space-2);
}

.final--many .final__seat {
  flex: 1 1 0;
}

.final--many .final__who {
  font-size: var(--text-xs);
}

.final--many .final__who .portrait {
  display: none;
}

.results__title {
  font-size: var(--text-xl);
  font-weight: var(--weight-display);
  line-height: var(--leading-tight);
  text-wrap: balance;
}

.results__note {
  max-width: 34ch;
  color: var(--text-soft);
  font-size: var(--text-s);
  line-height: var(--leading-snug);
  text-wrap: balance;
}

/* --- Every player's words ----------------------------------------------------------------- */

/* The words scroll here only when they must (views/results.js fitBody drops the "Best steal"
   lines first). A region that scrolls ends in a rule, a clear edge, instead of a faded line. */
.results__body {
  margin-inline: calc(var(--gutter) * -1);
  padding: var(--space-2) var(--gutter) var(--space-4);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.results__body.is-tight .seat__pride,
.results__body.is-tighter .results__legend {
  display: none;
}

.results__body.is-scrolling {
  background: linear-gradient(var(--panel), var(--panel)) no-repeat bottom center / calc(100% - var(--gutter) * 2) var(--rule);
}

.seats {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  list-style: none;
}

.seat {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-2);
  padding-block: var(--space-3);
  border-top: var(--rule) solid var(--panel);
}

.seat__head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.seat__head .player-badge {
  width: 20px;
  height: 20px;
  border-radius: calc(var(--radius-s) * 0.6);
}

.seat__head .player-badge .player-mark {
  width: 10px;
  height: 10px;
}

.seat__head .portrait {
  width: 24px;
  height: 24px;
}

.seat__name {
  min-width: 0;
  overflow: hidden;
  font-weight: var(--weight-label);
  line-height: var(--leading-snug);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.seat__tag {
  flex: none;
  padding: 1px var(--space-2);
  border-radius: var(--radius-pill);
  background: var(--panel);
  color: var(--text-soft);
  font-size: var(--text-xs);
  font-weight: var(--weight-label);
}

.seat__score {
  flex: none;
  margin-left: auto;
  color: var(--text-soft);
  font-size: var(--text-s);
  font-variant-numeric: tabular-nums;
}

.seat.is-gone .seat__head,
.seat.is-gone .seat__words {
  opacity: 0.55;
}

.seat__words {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  list-style: none;
}

/* One word: its tiles in its holder's colour, as on the table. Never broken across lines. */
.rw {
  display: flex;
  flex: none;
  gap: 2px;
  max-width: 100%;
}

.rw .tile {
  --tile-size: 24px;
  background: var(--player-face);
  box-shadow: 0 var(--tile-depth) 0 var(--player-strong);
}

/* The longest words there can be would not fit a phone's width at that size. */
.rw[data-long] .tile {
  --tile-size: 20px;
}

/* Stolen: a tag on the front of the word in the colour, and with the mark, of the player it
   was taken from. */
.rw__from {
  display: grid;
  place-items: center;
  flex: none;
  width: 16px;
  height: 24px;
  margin-right: 1px;
  border-radius: calc(var(--radius-s) * 0.6) calc(var(--radius-s) * 0.2) calc(var(--radius-s) * 0.2) calc(var(--radius-s) * 0.6);
  background: var(--player);
}

.rw__from .player-mark {
  width: 9px;
  height: 9px;
  background: var(--ink);
}

.seat__none {
  color: var(--text-faint);
  font-size: var(--text-s);
}

/* Something to be proud of: a quiet label and the words themselves. */
.seat__pride {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 var(--space-2);
  color: var(--text-soft);
  font-size: var(--text-s);
  line-height: var(--leading-snug);
}

.seat__pride-value {
  color: var(--text);
  font-weight: var(--weight-display);
  letter-spacing: 0.03em;   /* words from the game, set as the event line sets them */
  overflow-wrap: anywhere;
}

.results__legend {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding-block: var(--space-2) var(--space-3);
  border-top: var(--rule) solid var(--panel);
  color: var(--text-faint);
  font-size: var(--text-xs);
  line-height: var(--leading-snug);
}

.results__legend .rw__from {
  height: 18px;
}

/* --- What next ---------------------------------------------------------------------------- */

.results__actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-2);
}

.results__go {
  min-height: 64px;
}

/* Asked, and waiting for the other player: the button stays down. */
.results__go.is-waiting {
  transform: translateY(calc(var(--btn-depth) - var(--lip-down)));
  box-shadow: 0 var(--lip-down) 0 var(--btn-lip);
  cursor: default;
}

/* With a second line under it ("Waiting… for Mo", "New opponent · Mo left", "Play Nib ·
   Medium") the label steps down a size, so the two lines sit apart in the same button. */
.results__go:has(.btn__sub:not([hidden])) .btn__label {
  font-size: var(--text-xl);
}

.results__exits {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: var(--space-3);
}

.results__exits .btn {
  min-height: 48px;
  font-size: var(--text-m);
}

/* The level offer: who is next, and two answers. */
.offer {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-1) var(--space-2) var(--space-2);
  border-radius: var(--radius-m);
  background: var(--panel);
}

.offer .portrait {
  width: 36px;
  height: 36px;
}

.offer__text {
  display: grid;
  flex: 1 1 0;
  min-width: 0;
}

.offer__title {
  font-size: var(--text-m);
  font-weight: var(--weight-label);
  line-height: var(--leading-tight);
  text-wrap: balance;
}

.offer__sub {
  color: var(--text-soft);
  font-size: var(--text-xs);
  line-height: var(--leading-snug);
}

.offer .btn {
  flex: none;
  min-height: var(--tap);
  font-size: var(--text-m);
}

.offer__yes {
  --btn-bg: var(--tile-face);
  --btn-lip: var(--tile-lip);
  --btn-ink: var(--tile-ink);
  --btn-depth: var(--lip-s);
  padding-inline: var(--space-3);
}

.offer .btn--text {
  padding-inline: var(--space-2);
  font-size: var(--text-s);
  white-space: nowrap;
}

/* --- The moment ---------------------------------------------------------------------------
   The figures count up for --results-count; then the coin lands under the winner's, the crown
   drops onto it, the line says how it ended and the words follow. Nothing here delays a
   button: they are not part of it. */

.results.is-arriving .final__seat.is-winner .final__coin {
  animation: coin-ink var(--motion-quick) linear var(--results-count, 0ms) backwards;
}

.results.is-arriving .final__seat.is-winner .final__coin::after {
  animation: coin-land var(--motion-settle) var(--ease-pop) var(--results-count, 0ms) backwards;
}

.results.is-arriving .final__crown {
  animation: crown-drop var(--motion-settle) var(--ease-pop) calc(var(--results-count, 0ms) + var(--motion-stagger)) backwards;
}

.results.is-arriving .results__title,
.results.is-arriving .results__note {
  animation: results-rise var(--motion-settle) var(--ease-out) var(--results-count, 0ms) backwards;
}

.results.is-arriving .seat,
.results.is-arriving .results__legend {
  animation: results-rise var(--motion-settle) var(--ease-out) calc(var(--results-count, 0ms) + var(--motion-stagger) * (1 + var(--nth, 0))) backwards;
}

@keyframes coin-ink {
  from { color: var(--text); }
}

@keyframes coin-land {
  from { opacity: 0; transform: scale(calc(1 - 0.5 * var(--travel))); }
}

@keyframes crown-drop {
  from { opacity: 0; transform: translateY(calc(var(--travel) * -18px)) rotate(calc(var(--travel) * -14deg)); }
}

@keyframes results-rise {
  from { opacity: 0; transform: translateY(calc(var(--travel) * 10px)); }
}

/* --- Short screens: the browser's bars leave an iPhone SE about 550 px -------------------- */

@media (max-height: 600px) {
  .results {
    --coin: 50px;
    gap: var(--space-2);
  }

  .final {
    margin-bottom: var(--space-1);
  }

  .final__seat {
    gap: var(--space-1);
  }

  .final__coin {
    font-size: var(--text-xl);
  }

  .final__crown {
    width: 28px;
    height: 21px;
    margin: 0 0 -6px -14px;
  }

  .seat {
    padding-block: var(--space-2);
  }

  .results__go {
    min-height: 56px;
  }
}

/* --- Tall phones: the spare height goes into the score ------------------------------------ */

@media (min-height: 760px) {
  .results {
    --coin: 72px;
    gap: var(--space-4);
  }

  .rw .tile {
    --tile-size: 28px;
  }

  .rw__from {
    height: 28px;
  }

  .seat {
    gap: var(--space-3);
    padding-block: var(--space-4);
  }

  .results__head {
    gap: var(--space-2);
    padding-top: var(--space-4);
  }

  .final {
    margin-bottom: var(--space-3);
  }

  .results__actions {
    gap: var(--space-3);
  }

  .results__go {
    min-height: 78px;
  }

  .results__exits .btn {
    min-height: 52px;
  }
}

/* --- A desk: the column is wider, and the words, the names and the score grow with it ------- */

@media (pointer: fine) and (min-width: 700px) {
  .results {
    --coin: 84px;
    gap: var(--space-4);
  }

  .rw .tile {
    --tile-size: 38px;
  }

  .rw[data-long] .tile {
    --tile-size: 32px;
  }

  .rw__from {
    width: 22px;
    height: 38px;
  }

  .rw__from .player-mark {
    width: 12px;
  }

  .seat__name {
    font-size: var(--text-m);
  }

  .seat__score,
  .seat__pride {
    font-size: var(--text-m);
  }

  .results__note {
    font-size: var(--text-m);
  }
}
