/* ------------------------------------------------------------------
   Theme tokens
   ------------------------------------------------------------------ */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1a1d23;
  --muted: #6b7280;
  --tile-bg: #ffffff;
  --tile-border: #d3d6dc;
  --key-bg: #dfe2e7;
  --key-text: #1a1d23;
  --hit: #3d9a62;
  --miss: #868b94;
  --reveal: #d0574f;
  --warn: #b7791f;
  --pip: #e0566a;
  --shadow: 0 10px 30px rgba(16, 24, 40, 0.18);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121316;
    --surface: #1d1f24;
    --text: #eceef1;
    --muted: #9aa0aa;
    --tile-bg: #121316;
    --tile-border: #3a3d44;
    --key-bg: #3a3e46;
    --key-text: #eceef1;
    --hit: #4aa871;
    --miss: #2e3036;
    --reveal: #c2554d;
    --warn: #e0b04a;
    --pip: #ef6a7c;
    --shadow: 0 12px 34px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

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

/* ------------------------------------------------------------------
   Layout
   ------------------------------------------------------------------ */
.app {
  display: flex;
  flex-direction: column;
  max-width: 560px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 0 16px calc(12px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--tile-border);
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 150ms, color 150ms;
}

.icon-btn:hover { color: var(--text); background: color-mix(in srgb, var(--key-bg) 60%, transparent); }

.icon-btn svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn svg .dot { fill: currentColor; }

.play {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: clamp(16px, 6vh, 56px);
}

/* ------------------------------------------------------------------
   Board / tiles
   ------------------------------------------------------------------ */
.board {
  --len: 6;
  --gap: 8px;
  --size: min(60px, calc((100vw - 32px - (var(--len) - 1) * var(--gap)) / var(--len)));
  display: grid;
  grid-template-columns: repeat(var(--len), var(--size));
  gap: var(--gap);
  min-height: var(--size);
  perspective: 700px;
}

.tile {
  display: grid;
  place-items: center;
  width: var(--size);
  height: var(--size);
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  background: var(--tile-bg);
  font-size: calc(var(--size) * 0.55);
  font-weight: 800;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
}

/* Empty tiles show a small underscore so the word length is obvious. */
.tile:empty::after {
  content: "";
  width: 40%;
  height: 3px;
  margin-top: 45%;
  border-radius: 2px;
  background: var(--tile-border);
}

.tile.hit { --to-bg: var(--hit); background: var(--hit); border-color: var(--hit); color: #fff; }
.tile.reveal { --to-bg: var(--reveal); background: var(--reveal); border-color: var(--reveal); color: #fff; }

/* New word: tiles drop in one by one (--i is set per tile in script.js).
   A flip/bounce on the same tile always wins over the entry animation. */
.board.enter { animation: none-marker 700ms; } /* gives the board its own animationend */
.board.enter .tile { animation: tile-in 360ms var(--ease) calc(var(--i, 0) * 40ms) both; }

.board .tile.flip { animation: flip 520ms ease-in-out var(--delay, 0ms) both; }
.board .tile.bounce { animation: bounce 600ms var(--ease) var(--delay, 0ms) both; }

.board.shake { animation: shake 420ms var(--ease); }
.board.sink { animation: sink 700ms var(--ease); }

/* ------------------------------------------------------------------
   Lives, message, actions
   ------------------------------------------------------------------ */
.lives {
  display: flex;
  gap: 6px;
  margin-top: 24px;
}

.pip {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--pip);
  transition: background-color 250ms, transform 250ms, box-shadow 250ms;
}

.pip.lost { background: transparent; box-shadow: inset 0 0 0 2px var(--tile-border); transform: scale(0.8); }
.pip.popping { animation: pip-pop 450ms var(--ease); }
.lives.danger .pip:not(.lost) { animation: pulse 1.1s ease-in-out infinite; }

.lives-text {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.message {
  min-height: 1.5em;
  margin: 14px 0 0;
  font-weight: 600;
  text-align: center;
}

.message[data-tone="good"] { color: var(--hit); }
.message[data-tone="bad"], .message[data-tone="lose"] { color: var(--reveal); }
.message[data-tone="warn"] { color: var(--warn); }
.message[data-tone="win"] { color: var(--hit); font-size: 1.1rem; }

.actions { margin: 14px 0 0; }

.btn {
  min-height: 44px;
  padding: 10px 22px;
  border: 2px solid var(--tile-border);
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
  transition: background-color 150ms, border-color 150ms, transform 100ms;
}

.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { border-color: var(--hit); background: var(--hit); color: #fff; }
.btn.primary:hover:not(:disabled) { border-color: var(--hit); filter: brightness(1.08); }

/* ------------------------------------------------------------------
   On-screen keyboard
   ------------------------------------------------------------------ */
.keyboard {
  --gap: 6px;
  width: 100%;
  margin-top: auto;
  padding-top: 24px;
  user-select: none;
}

.key-row {
  display: flex;
  justify-content: center;
  gap: var(--gap);
  margin-bottom: 8px;
}

.key {
  flex: 1 1 0;
  max-width: calc((100% - 9 * var(--gap)) / 10); /* all keys as wide as in the 10-key row */
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation; /* no double-tap zoom */
  transition: background-color 250ms, color 250ms, opacity 250ms, transform 80ms;
}

.key:active { transform: scale(0.94); }
.key.hit { background: var(--hit); color: #fff; }
.key.miss { background: var(--miss); color: #fff; opacity: 0.75; }
.key.press { animation: key-press 260ms var(--ease); }
.key.nudge { animation: shake 320ms var(--ease); }

@media (hover: hover) {
  .key:hover:not(.hit):not(.miss) { filter: brightness(0.95); }
}

/* ------------------------------------------------------------------
   Dialogs
   ------------------------------------------------------------------ */
dialog {
  width: min(420px, calc(100% - 32px));
  max-height: calc(100% - 32px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog[open] { animation: dialog-in 260ms var(--ease); }
dialog::backdrop { background: rgba(10, 12, 16, 0.5); }

.dialog-inner { position: relative; padding: 24px 22px 22px; }
.dialog-inner .close { position: absolute; top: 8px; right: 8px; }

dialog h2 { margin: 0 0 12px; font-size: 1.3rem; }
dialog h3 { margin: 20px 0 8px; font-size: 0.95rem; color: var(--muted); font-weight: 600; }
dialog ul { padding-left: 1.2em; }
dialog li { margin: 4px 0; }
.small { color: var(--muted); font-size: 0.85rem; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
}

.stat-value { display: block; font-size: 1.8rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-label { display: block; color: var(--muted); font-size: 0.75rem; line-height: 1.2; }

.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
}

.dist-label { width: 1.6em; text-align: right; font-variant-numeric: tabular-nums; }

.dist-bar {
  width: var(--w, 8%);
  min-width: 1.8em;
  padding: 1px 8px;
  border-radius: 4px;
  background: var(--miss);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  text-align: right;
  animation: grow 500ms var(--ease) both;
}

.dist-bar.current { background: var(--hit); }

#stats-new { display: block; margin: 18px auto 0; border-color: var(--hit); background: var(--hit); color: #fff; }
#stats-new[hidden] { display: none; }

.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 50;
}

/* ------------------------------------------------------------------
   Small / short screens
   ------------------------------------------------------------------ */
@media (max-width: 400px) {
  .board { --gap: 6px; }
  .keyboard { --gap: 4px; }
  .key { height: 52px; font-size: 1rem; }
}

@media (max-height: 640px) {
  .play { padding-top: 12px; }
  .lives { margin-top: 14px; }
  .key { height: 46px; }
}

/* ------------------------------------------------------------------
   Keyframes
   ------------------------------------------------------------------ */
@keyframes flip {
  0% { transform: rotateX(0); background: var(--tile-bg); border-color: var(--tile-border); color: transparent; }
  50% { transform: rotateX(-90deg); background: var(--tile-bg); border-color: var(--tile-border); color: transparent; }
  50.1% { transform: rotateX(-90deg); background: var(--to-bg); border-color: var(--to-bg); color: #fff; }
  100% { transform: rotateX(0); background: var(--to-bg); border-color: var(--to-bg); color: #fff; }
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  35% { transform: translateY(-35%); }
  60% { transform: translateY(4%); }
  80% { transform: translateY(-6%); }
}

@keyframes tile-in {
  from { opacity: 0; transform: translateY(-12px) scale(0.9); }
  to { opacity: 1; transform: none; }
}

@keyframes none-marker { from { opacity: 1; } to { opacity: 1; } }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-8px); }
  35% { transform: translateX(7px); }
  55% { transform: translateX(-5px); }
  75% { transform: translateX(3px); }
}

@keyframes sink {
  0% { transform: none; }
  30% { transform: translateY(10px) rotate(-1.5deg); }
  60% { transform: translateY(4px) rotate(1deg); }
  100% { transform: none; }
}

@keyframes pip-pop {
  0% { transform: scale(1); box-shadow: 0 0 0 0 var(--pip); }
  40% { transform: scale(1.5); background: var(--pip); }
  100% { transform: scale(0.8); box-shadow: 0 0 0 8px transparent; }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.25); }
}

@keyframes key-press {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(16px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

@keyframes grow {
  from { width: 8%; }
}

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