/*
 * Sizes are in rem throughout. The root font size is set from JavaScript and grows with the
 * magnification level, so the whole interface scales with the tiles.
 */

:root {
  --felt-hi: #1d6158;
  --felt-mid: #114440;
  --felt-lo: #082a28;
  --ink: #f7f3e6;
  --ink-soft: #bcd3cc;
  --paper: #fbf7ea;
  --paper-ink: #142927;
  --paper-soft: #47605c;
  --accent: #14645a;
  --tile-face-hi: #fffdf4;
  --tile-face-lo: #efe6cc;
  --tile-edge: #cdc19b;
  --tile-back: #1c7a5b;
  --gold: #ffb300;
  --hint: #0a84ff;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--felt-lo);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.35;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

:focus-visible {
  outline: 0.2rem solid var(--gold);
  outline-offset: 0.12rem;
}

[hidden] {
  display: none !important;
}

/* ---------- frame ---------- */

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template: "top" auto "play" 1fr "controls" auto / 1fr;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: radial-gradient(ellipse at 50% 35%, var(--felt-hi), var(--felt-mid) 55%, var(--felt-lo));
}

/* The clock, then the board label, and the tile count at the far end. */
.topbar {
  grid-area: top;
  /* Long wording is cut short inside the bar; it never widens the page. */
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: min(1rem, 2.5vw);
  padding: 0.45rem min(0.9rem, 3vw) 0.2rem;
  white-space: nowrap;
}

/*
 * Should the label ever be wider than the room it has, it is cut short and the clock and the
 * count stay in view. On a narrow screen its letters are a little smaller than its line, which
 * keeps the height the board is sized around while leaving room for the clock.
 */
.topbar h1 {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: min(1.3rem, 6.2vw);
  line-height: calc(1.35 * min(1.3rem, 7vw));
  font-weight: 800;
}

/* Digits of one width, and room for 59:59 from the start, so the label beside it stays put. */
#boardTime {
  flex: none;
  min-width: 4.6ch;
  font-variant-numeric: tabular-nums;
}

#tilesLeft {
  flex: none;
  margin-left: auto;
}

/* The bar keeps its height before the label is written, since the board is sized around it. */
.topbar h1:empty::before {
  content: "\200b";
}

/* Never seen: it measures how wide the tile count would be when spelled out in full. */
.topbar .probe {
  position: absolute;
  visibility: hidden;
  pointer-events: none;
}

.topbar p {
  margin: 0;
  font-size: min(1.1rem, 5.6vw);
  font-weight: 700;
  color: var(--ink-soft);
}

#play {
  grid-area: play;
  position: relative;
  min-height: 0;
  min-width: 0;
}

/*
 * The run of cleared boards, printed on the felt. It comes before the board in the page, so the
 * tiles lie over it and it shows more as they come off, and it takes no taps. Its size comes
 * from the screen alone, so it moves nothing and the board is dealt the same with or without it.
 */
.streak {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.5vmin;
  margin: 0;
  color: rgb(255 255 255 / 0.13);
  font-weight: 800;
  line-height: 1;
  pointer-events: none;
  user-select: none;
}

.streak span {
  font-size: 7vmin;
  letter-spacing: 0.3em;
  /* The spacing after the last letter would push the word off centre. */
  margin-right: -0.3em;
  text-transform: uppercase;
}

.streak #streakCount {
  font-size: min(36vmin, 72vmin / var(--figures, 1));
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
  margin-right: 0;
}

.bottom {
  grid-area: controls;
  position: relative;
}

.bottombar {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.35rem;
  padding: 0.35rem 0.5rem 0.45rem;
}

.bar-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.1rem;
  min-width: 0;
  min-height: 3.4rem;
  padding: 0.3rem 0.1rem;
  border: 0.125rem solid rgb(255 255 255 / 0.24);
  border-radius: 0.8rem;
  background: rgb(255 255 255 / 0.1);
  font-size: min(0.85rem, 3.5vw);
  font-weight: 700;
}

/* How many shuffles are left, on the corner of the Shuffle button where they are counted. */

.bar-count {
  position: absolute;
  top: -0.3rem;
  right: -0.2rem;
  min-width: 1.5rem;
  padding: 0 0.3rem;
  border-radius: 1rem;
  background: var(--gold);
  color: #2b1a00;
  font-size: 0.95rem;
  font-weight: 800;
  line-height: 1.5rem;
  text-align: center;
}

.bar-btn svg {
  width: min(1.75rem, 9vw);
  height: min(1.75rem, 9vw);
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.bar-btn:active:not(:disabled),
.bar-btn[aria-expanded="true"] {
  background: rgb(255 255 255 / 0.3);
}

.bar-btn:disabled {
  opacity: 0.38;
  cursor: default;
}

/*
 * Undo pulses while the board can no longer be cleared, to say the last match should come back:
 * it turns gold, swells, and sends out a gold ring. Neither the swelling nor the ring takes up
 * room, so the bar and the board keep their sizes. With reduced motion the button stays gold and
 * does not move.
 */
.bar-btn.nudge {
  /* Above its neighbours, which the ring passes over. */
  z-index: 1;
  border-color: var(--gold);
  background: rgb(255 179 0 / 0.28);
  animation:
    nudge-ring 1.4s ease-out infinite,
    nudge-swell 1.4s ease-in-out infinite;
}

@keyframes nudge-ring {
  0% { box-shadow: 0 0 0 0 rgb(255 179 0 / 0.85); }
  70%,
  100% { box-shadow: 0 0 0 1rem rgb(255 179 0 / 0); }
}

@keyframes nudge-swell {
  0%,
  100% { transform: scale(1); }
  35% { transform: scale(1.09); }
}

/* From 2× up, the labels would be too small to help; the icons take the room instead. */
.big-ui .bar-btn span {
  display: none;
}

.big-ui .bar-btn svg {
  width: min(2.3rem, 11vw);
  height: min(2.3rem, 11vw);
  stroke-width: 2.4;
}

/* A phone on its side has no height to spare, so the controls move to the right edge. */
@media (orientation: landscape) and (max-height: 540px) {
  #app {
    grid-template: "top controls" auto "play controls" 1fr / 1fr auto;
  }

  .bottombar {
    grid-template-columns: none;
    grid-template-rows: repeat(5, 1fr);
    height: 100%;
    /* As wide as the longest button name needs, which depends on the language. */
    width: auto;
    min-width: 4.4rem;
    padding: 0.4rem;
  }

  .bar-btn {
    min-height: 0;
  }
}

/* ---------- board ---------- */

.board {
  position: absolute;
  inset: 0;
  isolation: isolate;
}

/*
 * A flyer is a matched tile on its way off the board. A shard is one piece of it: a holder the
 * size of the tile, with a chip inside that shows only that piece of the tile's face.
 */
.tile,
.flyer,
.shard {
  position: absolute;
  width: var(--tw);
  height: var(--th);
  margin: 0;
  padding: 0;
}

.tile,
.flyer,
.chip {
  border: 1px solid #a99e7c;
  border-radius: calc(var(--tw) * 0.13);
  background: linear-gradient(150deg, var(--tile-face-hi), var(--tile-face-lo));
}

.chip {
  position: absolute;
  inset: 0;
}

.tile {
  box-shadow: var(--tile-shadow);
  -webkit-appearance: none;
  appearance: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.tile img,
.flyer img,
.chip img {
  position: absolute;
  inset: 5%;
  width: 90%;
  height: 90%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* The classic faces are drawn right to the edge of their canvas, so they get a wider margin. */
.board[data-tiles="classic"] .tile img,
.board[data-tiles="classic"] .flyer img,
.board[data-tiles="classic"] .chip img {
  inset: 8% 9%;
  width: 82%;
  height: 84%;
}

.tile::before,
.tile::after {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: inherit;
  pointer-events: none;
}

.board.dim-blocked .tile.blocked::after {
  background: rgb(30 24 8 / 0.38);
}

.tile.selected {
  background: linear-gradient(150deg, #fff6c7, #ffd95a);
}

.tile.selected::before {
  box-shadow:
    inset 0 0 0 max(3px, calc(var(--tw) * 0.055)) var(--gold),
    inset 0 0 0 max(5px, calc(var(--tw) * 0.085)) #4a2600;
}

/* A hinted tile stays tinted and ringed for the whole pulse, so it reads at any moment. */
.tile.hint {
  background: linear-gradient(150deg, #e8f3ff, #a9d2ff);
}

.tile.hint::before {
  box-shadow:
    inset 0 0 0 max(4px, calc(var(--tw) * 0.07)) var(--hint),
    inset 0 0 0 max(6px, calc(var(--tw) * 0.1)) #00224d;
  animation: hint-pulse 0.7s ease-in-out infinite alternate;
}

@keyframes hint-pulse {
  from { opacity: 0.55; }
  to { opacity: 1; }
}

.tile.gone {
  opacity: 0;
  transform: scale(0.72);
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease, visibility 0s 0.18s;
}

/* A matched tile goes at once: its flyer is already where it was. */
.tile.gone.flown {
  transition: none;
}

/* Above every tile, and out of the way of taps, which go to the tiles underneath. */
.flyer,
.shard,
.spark {
  z-index: 100000;
  pointer-events: none;
  will-change: transform, opacity;
}

/* Off the board a tile keeps its thickness and casts a wider, softer shadow than one lying down. */
.flyer {
  box-shadow: var(--tile-shadow), 0 calc(var(--tw) * 0.16) calc(var(--tw) * 0.3) rgb(0 0 0 / 0.35);
}

/* The shadow is cast by the holder, since a chip is cut to its shape and its own would be cut off. */
.shard {
  filter: drop-shadow(0 calc(var(--tw) * 0.04) calc(var(--tw) * 0.05) rgb(0 0 0 / 0.5));
}

/* The flash where two tiles hit. */
.spark {
  position: absolute;
  width: var(--tw);
  height: var(--tw);
  margin: calc(var(--tw) / -2) 0 0 calc(var(--tw) / -2);
  border-radius: 50%;
  background: radial-gradient(closest-side, rgb(255 255 255 / 0.95), rgb(255 240 190 / 0.55) 45%, rgb(255 240 190 / 0) 100%);
}

.tile.refused {
  animation: refuse 0.3s ease;
}

@keyframes refuse {
  20% { transform: translateX(-7%); }
  50% { transform: translateX(6%); }
  80% { transform: translateX(-3%); }
}

.tile:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 2px;
}

/* ---------- floating panels ---------- */

.banner,
.size-panel {
  background: var(--paper);
  color: var(--paper-ink);
  border-radius: 1rem;
  box-shadow: 0 0.5rem 1.6rem rgb(0 0 0 / 0.5);
}

.banner {
  position: absolute;
  left: 50%;
  bottom: 0.6rem;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.7rem;
  width: max-content;
  max-width: calc(100% - 1rem);
  padding: 0.5rem 0.6rem 0.5rem 1rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.banner .btn {
  min-height: 2.6rem;
  padding: 0.3rem 1rem;
  white-space: nowrap;
}

.size-panel {
  position: absolute;
  left: 0.5rem;
  right: 0.5rem;
  bottom: calc(100% + 0.4rem);
  max-width: 24rem;
  margin: 0 auto;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem;
  z-index: 2;
}

.size-panel button,
.start-size button {
  width: 3.6rem;
  height: 3.6rem;
  border: 0;
  border-radius: 0.9rem;
  background: var(--accent);
  color: #fff;
  font-size: 2.3rem;
  font-weight: 800;
  line-height: 1;
}

.size-panel button:disabled,
.start-size button:disabled {
  opacity: 0.3;
  cursor: default;
}

/* With the controls down the right edge, the panel opens beside them, over the board's corner. */
@media (orientation: landscape) and (max-height: 540px) {
  .size-panel {
    left: auto;
    right: calc(100% + 0.5rem);
    bottom: 0.5rem;
    width: min(19rem, 100vw - 6rem);
    margin: 0;
  }
}

.size-readout {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--paper-soft);
}

.size-pending {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.2rem 0.3rem 0.1rem;
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
}

.size-panel .size-pending .btn {
  width: 100%;
  height: auto;
  min-height: 3rem;
  font-size: 1.1rem;
  font-weight: 800;
  line-height: 1.2;
}

.size-readout strong {
  font-size: 1.7rem;
  line-height: 1.1;
  color: var(--paper-ink);
}

/* ---------- dialogs ---------- */

dialog {
  width: min(92vw, 30rem);
  max-height: 92dvh;
  padding: 1.4rem;
  border: 0;
  border-radius: 1.2rem;
  background: var(--paper);
  color: var(--paper-ink);
  box-shadow: 0 1rem 3rem rgb(0 0 0 / 0.55);
  overflow-y: auto;
}

dialog::backdrop {
  background: rgb(3 18 17 / 0.74);
}

dialog h2 {
  margin: 0 0 0.6rem;
  font-size: 1.55rem;
  line-height: 1.2;
}

dialog h3 {
  margin: 1.3rem 0 0.2rem;
  font-size: 0.9rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--paper-soft);
}

dialog p {
  margin: 0 0 0.7rem;
  font-size: 1.1rem;
}

dialog .note {
  margin-bottom: 0.2rem;
  font-size: 0.95rem;
  color: var(--paper-soft);
}

/*
 * Auto on a row of its own, then the levels five to a row. The columns sit close together so that
 * a level is still 40px wide in the menu of a 320px phone at the largest text size.
 */
.level-choices {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.35rem 0.2rem;
  margin: 0.45rem 0 0.5rem;
}

.level-auto {
  grid-column: 1 / -1;
}

/* The languages: the same pills as the levels, two to a row. */
.lang-choices {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
  margin: 0.45rem 0 0.5rem;
}

.lang-choices .level-choice span {
  min-height: 3.2rem;
  font-size: 1.25rem;
}

.level-choice {
  position: relative;
  cursor: pointer;
}

.level-choice input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.level-choice span {
  display: grid;
  place-items: center;
  min-height: 2.9rem;
  border: 0.19rem solid #cfc7ad;
  border-radius: 0.8rem;
  font-size: 1.15rem;
  font-weight: 700;
}

.level-choice input:checked + span {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.level-choice input:focus-visible + span {
  outline: 0.19rem solid var(--hint);
  outline-offset: 0.12rem;
}

/* ---------- the first dialog: language and tile size ---------- */

#startSizeTitle {
  margin-top: 1.1rem;
}

.start-size {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.6rem;
}

.start-size .size-readout {
  text-align: center;
}

/* One tile at the size the board has it. */
.start-sample {
  display: flex;
  justify-content: center;
  margin: 0.9rem 0 0.8rem;
}

.start-sample .sample-tile {
  max-width: 60%;
}

.start-sample[data-tiles="classic"] .sample-tile img {
  inset: 8% 9%;
  width: 82%;
  height: 84%;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 3.2rem;
  padding: 0.5rem 1.2rem;
  border: 0.125rem solid var(--accent);
  border-radius: 0.9rem;
  background: var(--accent);
  color: #fff;
  font-size: 1.15rem;
  font-weight: 800;
  text-decoration: none;
}

.btn.secondary {
  background: transparent;
  color: var(--accent);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

/* ---------- tile set picker ---------- */

.tile-choices {
  display: grid;
  gap: 0.7rem;
  margin: 0.2rem 0 0.8rem;
}

.tile-choice {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.2rem 0.8rem;
  padding: 0.8rem 0.9rem;
  border: 0.19rem solid #cfc7ad;
  border-radius: 1rem;
  cursor: pointer;
}

.tile-choice.chosen {
  border-color: var(--accent);
  background: #e6f1ec;
}

.tile-choice input {
  -webkit-appearance: none;
  appearance: none;
  width: 1.7rem;
  height: 1.7rem;
  margin: 0;
  border: 0.19rem solid var(--paper-soft);
  border-radius: 50%;
  background: #fff;
}

.tile-choice input:checked {
  border-color: var(--accent);
  background: radial-gradient(circle, var(--accent) 0 44%, #fff 50%);
}

.tile-choice-text {
  display: flex;
  flex-direction: column;
  font-size: 1rem;
  color: var(--paper-soft);
}

.tile-choice-text strong {
  font-size: 1.3rem;
  color: var(--paper-ink);
}

.tile-choice-faces {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.sample-tile {
  position: relative;
  aspect-ratio: 1 / 1.3;
  border: 1px solid #a99e7c;
  border-radius: 13%/10%;
  background: linear-gradient(150deg, var(--tile-face-hi), var(--tile-face-lo));
  box-shadow: 0.12rem 0.12rem 0 var(--tile-edge), 0.24rem 0.24rem 0 var(--tile-back);
}

.sample-tile img {
  position: absolute;
  inset: 5%;
  width: 90%;
  height: 90%;
  object-fit: contain;
}

.tile-choice[data-tiles="classic"] .sample-tile img {
  inset: 8% 9%;
  width: 82%;
  height: 84%;
}

/* A phone on its side shows the sets side by side, so the button stays in view. */
@media (orientation: landscape) and (max-height: 540px) {
  #dlgTiles {
    width: min(94vw, 46rem);
  }

  .tile-choices {
    grid-template-columns: 1fr 1fr;
  }
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin: 1.1rem 0 0;
}

.btn-row .btn {
  flex: 1 1 9rem;
}

#btnTiles,
#btnSounds {
  width: 100%;
  margin: 0.4rem 0 0.3rem;
}

#soundsTitle {
  margin-top: 1.1rem;
}

/* These can be taller than a small screen, so the button stays in view while the rest scrolls. */
#dlgStart .btn-row,
#dlgTiles .btn-row {
  position: sticky;
  bottom: -1.4rem;
  margin: 0.6rem -1.4rem -1.4rem;
  padding: 0.7rem 1.4rem 1.4rem;
  background: var(--paper);
  box-shadow: 0 -0.5rem 0.6rem -0.4rem rgb(0 0 0 / 0.18);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.5rem 0;
  font-size: 1.08rem;
  font-weight: 600;
  cursor: pointer;
}

.toggle input {
  flex: none;
  position: relative;
  width: 3.2rem;
  height: 1.9rem;
  margin: 0;
  border-radius: 1rem;
  background: #aaa390;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  transition: background 0.15s;
}

.toggle input::after {
  content: "";
  position: absolute;
  top: 0.2rem;
  left: 0.2rem;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}

.toggle input:checked {
  background: var(--accent);
}

.toggle input:checked::after {
  transform: translateX(1.3rem);
}

.toggle small {
  display: block;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--paper-soft);
}

.toggle small:empty {
  display: none;
}

#dlgCheckin,
#dlgWin {
  text-align: center;
}

.checkin-art svg {
  width: 6rem;
  height: 6rem;
}

dialog .checkin-kicker {
  margin: 0.2rem 0 0.3rem;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--paper-soft);
}

dialog .checkin-text {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1.25;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .tile.hint::before {
    opacity: 1;
  }
}
