:root {
  --bg: #f6f1e7;
  --surface: #fffaf2;
  --ink: #2b2420;
  --muted: #7a6f66;
  --line: #ddd3c5;
  --filled: #a3968a;
  --key: #e7ded1;
  --hit: #138a72;
  --near: #df6449;
  --miss: #8f8579;
  --on-color: #ffffff;
  --shadow: 0 10px 40px rgb(43 36 32 / 0.18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1916;
    --surface: #26221e;
    --ink: #f3ece2;
    --muted: #a99e92;
    --line: #3d3731;
    --filled: #6f665d;
    --key: #3a342f;
    --hit: #1fa386;
    --near: #e8714f;
    --miss: #4b453f;
    --shadow: 0 10px 40px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Fredoka", ui-rounded, system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* header */
.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}
.brand { text-align: center; line-height: 1; }
.brand h1 { margin: 0; font-size: clamp(20px, 6.4vw, 30px); font-weight: 700; letter-spacing: 0.01em; white-space: nowrap; }
.brand h1 span { color: var(--near); }
.brand h1 .logo { width: 1.1em; height: 1.1em; vertical-align: -0.2em; margin-right: 0.25em; }
.brand p { margin: 4px 0 0; font-size: 13px; color: var(--muted); font-weight: 500; }
.icon-btn {
  justify-self: start;
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: none;
  font-weight: 700;
}
.text-btn {
  justify-self: end;
  border: 2px solid var(--line);
  background: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

/* board */
main {
  flex: 1;
  display: grid;
  place-items: center;
  position: relative;
  padding: 12px 16px;
  min-height: 0;
}
#board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 7px;
  width: min(340px, 100%, 55dvh * 5 / 6);
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 7px; }
.row.shake { animation: shake 400ms; }

.tile {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 14px;
  border: 2px solid var(--line);
  font-size: clamp(22px, 7vw, 32px);
  font-weight: 600;
  text-transform: uppercase;
  background: transparent;
}
.tile[data-state="filled"] { border-color: var(--filled); }
.tile.pop { animation: pop 110ms ease-out; }
.tile[data-state="hit"]  { --c: var(--hit); }
.tile[data-state="near"] { --c: var(--near); }
.tile[data-state="miss"] { --c: var(--miss); }
.tile[data-state="hit"], .tile[data-state="near"], .tile[data-state="miss"] {
  background: var(--c);
  border-color: var(--c);
  color: var(--on-color);
}
.tile.animate { animation: reveal 460ms var(--d, 0ms) both cubic-bezier(.3, 1.4, .5, 1); }
.row.win .tile { animation: hop 520ms var(--d, 0ms) ease-in-out; }

@keyframes reveal {
  0%   { background: transparent; border-color: var(--filled); color: var(--ink); transform: scale(1); }
  45%  { transform: translateY(-14%) scale(1.08); }
  50%  { background: var(--c); border-color: var(--c); color: var(--on-color); }
  100% { background: var(--c); border-color: var(--c); color: var(--on-color); transform: none; }
}
@keyframes pop   { 50% { transform: scale(1.1); } }
@keyframes hop   { 30% { transform: translateY(-28%) rotate(-6deg); } 60% { transform: translateY(4%); } }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }

/* toast */
#toast {
  position: absolute;
  top: 10px; left: 50%;
  transform: translate(-50%, -6px);
  background: var(--ink);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms, transform 160ms;
  z-index: 2;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* keyboard */
#keyboard {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.krow { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1 1 0;
  height: 54px;
  max-width: 44px;
  border: 0;
  border-radius: 10px;
  background: var(--key);
  font-size: 18px;
  font-weight: 600;
  text-transform: uppercase;
  transition: background 200ms, transform 80ms;
}
.key:active { transform: scale(0.94); }
.key.wide { max-width: 68px; flex-grow: 1.5; font-size: 13px; }
.key[data-state="hit"]  { background: var(--hit);  color: var(--on-color); }
.key[data-state="near"] { background: var(--near); color: var(--on-color); }
.key[data-state="miss"], .key:disabled { background: var(--miss); color: var(--on-color); opacity: 0.35; cursor: default; }
.key:disabled:active { transform: none; }

/* footer */
.credit {
  text-align: center;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  padding: 0 16px calc(8px + env(safe-area-inset-bottom));
}
.credit a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.credit a:hover { color: var(--ink); }

/* dialogs */
dialog {
  border: 0;
  border-radius: 22px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  width: min(380px, calc(100vw - 32px));
  padding: 24px;
  text-align: center;
}
dialog::backdrop { background: rgb(20 16 12 / 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 26px; }
dialog p { line-height: 1.45; margin: 0 0 12px; font-weight: 500; }
dialog .fine { color: var(--muted); font-size: 14px; }
.example { display: grid; grid-template-columns: repeat(5, 48px); gap: 6px; justify-content: center; margin: 16px 0; }
.example .tile { font-size: 22px; border-radius: 11px; }
.legend { list-style: none; padding: 0; margin: 0 0 14px; text-align: left; font-size: 15px; display: grid; gap: 8px; }
.legend li { display: flex; gap: 10px; align-items: center; }
.swatch { flex: none; width: 18px; height: 18px; border-radius: 6px; }
.swatch.hit { background: var(--hit); } .swatch.near { background: var(--near); } .swatch.miss { background: var(--miss); }
.reveal { font-size: 17px; }
#end-word {
  display: block;
  margin-top: 6px;
  font-size: 40px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--hit);
}
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 18px 0 14px; }
.stats div { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); font-weight: 500; }
.stats b { font-size: 26px; color: var(--ink); font-weight: 600; }
.stats:empty { display: none; }
.share { display: block; width: 100%; padding: 12px 20px; font-size: 17px; }
.actions { display: flex; gap: 10px; justify-content: center; margin-top: 8px; }
.actions form { margin: 0; }
.primary, .ghost {
  border-radius: 999px;
  padding: 10px 20px;
  font-weight: 600;
  font-size: 16px;
}
.primary { background: var(--hit); color: var(--on-color); border: 0; }
.ghost { background: none; border: 2px solid var(--line); }

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