/* The game board: index cards on a tray. */

.board {
  --gap: clamp(4px, 1.1cqmin, 12px);
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: var(--gap);
  width: min(100cqw, 100cqh * var(--cols) / var(--rows));
  aspect-ratio: var(--cols) / var(--rows);
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  container-type: size;
}

.tile {
  padding: 0;
  border: 0;
  background: none;
  border-radius: 10px;
  perspective: 700px;
  cursor: pointer;
}

.tile:focus-visible {
  outline: 3px solid var(--md-primary);
  outline-offset: 2px;
}

.tile[aria-disabled="true"] {
  cursor: default;
}

.tile-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 420ms var(--ease-emphasized-decel);
}

.tile.is-open .tile-inner {
  transform: rotateY(180deg);
}

.face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: clamp(6px, 9cqmin, 14px);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: var(--elev-1);
}

/* Back: cobalt with a scatter of letters and a thin inset frame. */
.back {
  background-color: var(--tile-back);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='48' height='48' viewBox='0 0 48 48'%3E%3Cg fill='%23ffffff' fill-opacity='.2' font-family='Georgia,serif' font-size='15'%3E%3Ctext x='5' y='18'%3Ea%3C/text%3E%3Ctext x='29' y='18' font-style='italic'%3EB%3C/text%3E%3Ctext x='17' y='42'%3Ec%3C/text%3E%3Ctext x='39' y='42' font-style='italic'%3Ed%3C/text%3E%3C/g%3E%3C/svg%3E");
  background-size: 50% 50%;
  transition: background-color 150ms;
}

.back::after {
  content: "";
  position: absolute;
  inset: 7cqmin;
  border: max(1.5px, 1.5cqmin) solid rgb(255 255 255 / 0.5);
  border-radius: clamp(3px, 5cqmin, 9px);
}

.tile:not([aria-disabled="true"]):hover .back {
  background-color: var(--tile-back-hover);
}

/* Front: a ruled index card. Content sits below the red line. */
.front {
  transform: rotateY(180deg);
  display: grid;
  place-items: center;
  padding: 24cqh 7cqw 5cqh;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--paper-margin), var(--paper-margin)),
    repeating-linear-gradient(to bottom, transparent 0 calc(14cqh - 1px), var(--paper-rule) calc(14cqh - 1px) 14cqh);
  background-size: 100% max(1px, 1.4cqh), 100% 100%;
  background-position: 0 20cqh, 0 20cqh;
  background-repeat: no-repeat;
  color: var(--paper-ink);
  text-align: center;
}

.tile-art.emoji-art {
  width: min(58cqw, 64cqh);
  height: min(58cqw, 64cqh);
}

.tile-art.emoji-text,
.tile-emoji {
  font-family: var(--font-emoji);
  font-size: min(50cqw, 60cqh);
  line-height: 1;
}

.tile-img {
  position: absolute;
  top: 24cqh;
  left: 6cqw;
  width: 88cqw;
  height: 70cqh;
  object-fit: contain;
}

.tile-text {
  font-family: var(--font-learn);
  font-weight: 700;
  line-height: 1.12;
  font-size: min(28cqmin, 122cqw / var(--len, 5), 60cqh / var(--lines, 1));
  overflow-wrap: anywhere;
}

.tile-word {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2cqh;
  max-width: 100%;
}

/* How the word sounds, under it. Leaves the word a little less room. */
.tile-phonetic {
  font-family: var(--font-learn);
  font-size: min(11cqh, 9cqw, 150cqw / var(--plen, 8));
  line-height: 1.1;
  color: var(--paper-ink-soft);
  white-space: nowrap;
}

.front:has(.tile-phonetic) .tile-text {
  font-size: min(24cqmin, 122cqw / var(--len, 5), 46cqh / var(--lines, 1));
}

.front-meaning .tile-text {
  color: var(--paper-ink-soft);
  font-weight: 400;
}

/* Wrong pair: red edge and a small nudge before turning back. */
.tile.is-wrong .front {
  box-shadow: inset 0 0 0 max(2px, 4cqmin) var(--wrong);
}

.tile.is-wrong .tile-inner {
  animation: nudge 320ms ease-in-out 320ms;
}

@keyframes nudge {
  0%, 100% { translate: 0; }
  25% { translate: -4% 0; }
  75% { translate: 4% 0; }
}

/* Matched: a green check stamp in the strip above the red line. */
.tile.is-matched .front {
  box-shadow: inset 0 0 0 max(2px, 2.5cqmin) var(--stamp);
}

.tile.is-matched .front::after {
  content: "✓";
  position: absolute;
  top: 2cqh;
  right: 4cqw;
  display: grid;
  place-items: center;
  width: 16cqh;
  height: 16cqh;
  border: max(1.5px, 1.6cqh) solid var(--stamp);
  border-radius: 50%;
  color: var(--stamp);
  font: 700 11cqh/1 var(--font-ui);
  rotate: -12deg;
  animation: stamp 300ms cubic-bezier(0.2, 1.6, 0.4, 1) both;
}

@keyframes stamp {
  from { scale: 1.8; opacity: 0; }
}

/* The free centre cell on odd boards. */
.free {
  display: grid;
  place-items: center;
  border: 1.5px dashed var(--md-outline-variant);
  border-radius: clamp(6px, 9cqmin, 14px);
}

.free-star {
  font-size: 38cqmin;
  line-height: 1;
  color: var(--md-outline-variant);
}

/* Dense boards (over 7×7): drop the rules and the pronunciation so small cards stay readable. */
.board[data-density="dense"] .tile-phonetic {
  display: none;
}

.board[data-density="dense"] .front .tile-text {
  font-size: min(28cqmin, 122cqw / var(--len, 5), 60cqh / var(--lines, 1));
}

.board[data-density="dense"] .front {
  background-image: linear-gradient(var(--paper-margin), var(--paper-margin));
  background-size: 100% max(1px, 2cqh);
  background-position: 0 17cqh;
  padding: 21cqh 7cqw 4cqh;
}

.board[data-density="dense"] .tile.is-matched .front::after {
  top: 1cqh;
  width: 14cqh;
  height: 14cqh;
  border-width: max(1px, 1.4cqh);
  font-size: 10cqh;
}
