/* Kalimat: stitched tiles on fabric. Tokens, pieces and keyframes come from base.css (docs/GAMES-THEME.md). */

.kalimat-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  padding: 16px 8px 32px;
  max-width: 500px;
  margin: 0 auto;
}

.board {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 350px;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.row.shake {
  animation: shake 300ms;
}

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-8px); }
  40%, 60% { transform: translateX(8px); }
}

/* ---------- tiles ---------- */
/* Each tile is a cloth square with a running stitch just inside its edge. --tile-edge and --tile-stitch
   carry the state's border and stitch colours. */
.tile {
  --tile-edge: var(--border);
  --tile-stitch: var(--stitch-color);
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--tile-edge);
  border-radius: var(--radius-sm);
  outline: 1.5px dashed var(--tile-stitch);
  outline-offset: -6px;
  font-size: 2rem;
  line-height: 1;
  text-transform: uppercase;
  color: var(--fg);
  background-color: var(--card-bg);
  user-select: none;
}

.tile .glyph {
  display: inline-block;
}

.tile.filled {
  --tile-edge: var(--control-border);
  --tile-stitch: var(--muted);
}

/* The result threads; --fill is the colour thread-fill sews in. */
.tile.correct { --fill: var(--tile-correct); }
.tile.present { --fill: var(--tile-present); }
.tile.absent { --fill: var(--tile-absent); }

.tile.revealed.correct {
  --tile-edge: var(--tile-correct);
  --tile-stitch: color-mix(in srgb, var(--tile-correct-fg) 45%, transparent);
  background-color: var(--tile-correct);
  color: var(--tile-correct-fg);
}

.tile.revealed.present {
  --tile-edge: var(--tile-present);
  --tile-stitch: color-mix(in srgb, var(--tile-present-fg) 40%, transparent);
  background-color: var(--tile-present);
  color: var(--tile-present-fg);
}

.tile.revealed.absent {
  --tile-edge: var(--tile-absent);
  --tile-stitch: color-mix(in srgb, var(--tile-absent-fg) 40%, transparent);
  background-color: var(--tile-absent);
  color: var(--tile-absent-fg);
}

/* Dark letters on a light thread take a light shade, so the embroidery shadow does not muddy them. */
.tile.revealed.present .glyph {
  --yarn-shade: color-mix(in srgb, var(--tile-present) 55%, #FFFDF8);
}

/* While a tile is being filled it keeps its typed look: the old letter colour stays below the thread
   line (an overlay clipped in step with thread-fill) and the edge and stitch switch when it ends.
   Under reduced motion none of this applies and the tile shows its result at once. */
@media (prefers-reduced-motion: no-preference) {
  .tile.revealed.thread-fill {
    --tile-edge: var(--control-border);
    --tile-stitch: var(--muted);
  }

  .tile.thread-fill::before {
    content: attr(data-letter) / "";
    position: absolute;
    z-index: 1;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--fg);
    font-family: var(--font-yarn);
    font-weight: 800;
    letter-spacing: .03em;
    text-shadow: 1px 1px 0 color-mix(in srgb, var(--fg) 55%, rgba(0, 0, 0, .55));
    pointer-events: none;
    animation: tile-unfill var(--fill-dur, 300ms) var(--ease-thread) var(--fill-delay, 0ms) both;
  }
}

@keyframes tile-unfill {
  from { clip-path: inset(0 0 0 0); }
  to   { clip-path: inset(100% 0 0 0); }
}

/* Without motion, thread-fill stays on a revealed tile (animationend never fires); drop its thread lines so
   the tile is the same solid thread as a restored one. */
@media (prefers-reduced-motion: reduce) {
  .tile.thread-fill {
    background-image: none;
  }
}

/* ---------- keyboard ---------- */
.keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 500px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

/* Keys are small cloth labels with an inner running stitch; a status key takes that thread's colour. */
.key {
  flex: 1;
  min-width: 28px;
  max-width: 44px;
  height: 46px;
  padding: 0;
  border: 1px solid color-mix(in srgb, var(--fg) 12%, transparent);
  border-radius: var(--radius-sm);
  background: var(--muted-bg);
  color: var(--fg);
  font-family: var(--font-yarn);
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: .02em;
  display: flex;
  align-items: center;
  justify-content: center;
  outline: 1px dashed color-mix(in srgb, currentColor 32%, transparent);
  outline-offset: -4px;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
}

@media (hover: hover) {
  .key:hover {
    outline-color: color-mix(in srgb, currentColor 60%, transparent);
  }
}

.key:active {
  translate: 0 1px;
}

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

.key.key-wide {
  flex: 1.6;
  max-width: 70px;
  font-size: 0.75rem;
}

.key.correct {
  background: var(--tile-correct);
  border-color: var(--tile-correct);
  color: var(--tile-correct-fg);
}

.key.present {
  background: var(--tile-present);
  border-color: var(--tile-present);
  color: var(--tile-present-fg);
}

.key.absent {
  background: var(--tile-absent);
  border-color: var(--tile-absent);
  color: var(--tile-absent-fg);
}

/* ---------- result modal ---------- */
.modal-answer {
  font-size: 1.1rem;
}

.modal-answer strong {
  font-family: var(--font-yarn);
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--link);
}

.stats-summary {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 16px 0;
}

/* Each figure is a small sand patch with a yarn number, like the shared stats block. */
.stat {
  flex: 1 1 0;
  min-width: 0;
  padding: 10px 4px 8px;
  border-radius: var(--radius);
  background: var(--sand);
  outline: 1.5px dashed var(--stitch-color);
  outline-offset: -5px;
  text-align: center;
}

.stat-value {
  font-family: var(--font-yarn);
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1.2;
  color: var(--fg);
  text-shadow: 1px 1px 0 color-mix(in srgb, currentColor 30%, transparent);
}

.stat-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  text-transform: uppercase;
}

.stats-dist {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 16px;
}

.stats-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.stats-label {
  width: 12px;
  font-weight: 700;
  color: var(--muted);
}

/* Distribution bars are lengths of thread: taupe, and olive for this game's row. */
.stats-bar {
  background: var(--tile-absent);
  color: var(--tile-absent-fg);
  text-align: right;
  padding: 2px 8px;
  min-width: 24px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  outline: 1px dashed color-mix(in srgb, currentColor 35%, transparent);
  outline-offset: -3px;
}

.stats-bar.stats-bar-current {
  background: var(--tile-correct);
  color: var(--tile-correct-fg);
}

#btn-see-results[hidden] {
  display: none;
}

@media (max-width: 400px) {
  .tile {
    font-size: 1.6rem;
    outline-offset: -5px;
  }
  .key {
    height: 42px;
    font-size: 0.85rem;
  }
  .key.key-wide {
    font-size: 0.72rem;
  }
}
