:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1b1c1f;
  --muted: #62656b;
  --line: #b9b6ad;
  --line-strong: #8d8f94;
  --key: #e2e0d9;
  --key-text: #1b1c1f;
  --correct: #2b7a49;
  --present: #946f08;
  --absent: #6b6e74;
  --on-tile: #ffffff;
  --accent: #2b7a49;
  --key-used: #6b6e74;
  --key-used-text: #ffffff;
  --shadow: 0 10px 40px rgba(20, 20, 20, .18);
  --tile: min(62px, calc((100vw - 64px) / 5), calc((100dvh - 360px) / 6));
}
/* The result block needs room too; shrink the board instead of pushing the keyboard off screen. */
body.is-done { --tile: min(62px, calc((100vw - 64px) / 5), calc((100dvh - 480px) / 6)); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --surface: #202226; --text: #ececec; --muted: #a2a5ab;
    --line: #55585f; --line-strong: #7d8087; --key: #3a3c42; --key-text: #ececec;
    --correct: #2b7a49; --present: #946f08; --absent: #3a3c42; --accent: #4fae73;
    --key-used: #242529; --key-used-text: #9a9da3; --shadow: 0 10px 40px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --surface: #202226; --text: #ececec; --muted: #a2a5ab;
  --line: #55585f; --line-strong: #7d8087; --key: #3a3c42; --key-text: #ececec;
  --correct: #2b7a49; --present: #946f08; --absent: #3a3c42; --accent: #4fae73;
  --key-used: #242529; --key-used-text: #9a9da3; --shadow: 0 10px 40px rgba(0,0,0,.5);
}
:root[data-contrast="high"] { --correct: #b8560c; --present: #2a6fb0; --accent: #b8560c; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  display: flex; flex-direction: column; min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.bar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 8px 16px; border-bottom: 1px solid var(--line);
}
.bar h1 { margin: 0; font-size: 22px; letter-spacing: .02em; font-weight: 800; }
.bar-right { justify-self: end; display: flex; gap: 4px; }
.icon {
  width: 44px; height: 44px; border: 0; background: none; border-radius: 10px;
  display: grid; place-items: center; font-size: 20px; font-weight: 700;
}
.icon:hover { background: var(--key); }
.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.modes { display: flex; justify-content: center; align-items: center; gap: 6px; padding: 10px 16px 0; }
.modes button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; font-size: 14px; font-weight: 600; min-height: 36px;
}
.modes button[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.hard-badge { font-size: 12px; font-weight: 700; color: var(--accent); border: 1px solid currentColor; border-radius: 6px; padding: 2px 6px; }

main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 12px 16px; gap: 14px; }
.board { display: grid; grid-template-rows: repeat(6, var(--tile)); gap: 6px; }
.row-g { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: 6px; }
.tile {
  border: 2px solid var(--line); display: grid; place-items: center;
  font-size: calc(var(--tile) * .52); font-weight: 800; text-transform: uppercase;
  background: var(--surface); user-select: none;
}
.tile.filled { border-color: var(--line-strong); animation: pop .1s ease-out; }
.tile.correct, .tile.present, .tile.absent { color: var(--on-tile); border-color: transparent; }
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }
.tile.flip { animation: flip .5s ease forwards; }
.row-g.shake { animation: shake .4s; }
.row-g.win .tile { animation: bounce .5s ease; }
.tile.mini { width: 30px; height: 30px; font-size: 16px; display: inline-grid; margin-right: 8px; vertical-align: middle; }

@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
@keyframes bounce { 40% { transform: translateY(-14px); } 70% { transform: translateY(3px); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

.after { text-align: center; }
.after p { margin: 0 0 8px; }
.after-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.primary, .secondary { border-radius: 10px; padding: 10px 18px; font-weight: 700; min-height: 44px; border: 0; }
.primary { background: var(--correct); color: #fff; }
.secondary { background: var(--key); color: var(--key-text); }

.keyboard { padding: 0 8px calc(10px + env(safe-area-inset-bottom)); max-width: 520px; width: 100%; margin: 0 auto; }
.kb-row { display: flex; gap: 6px; justify-content: center; margin-top: 7px; }
.key {
  flex: 1; height: clamp(40px, 7dvh, 54px); border: 0; border-radius: 6px; background: var(--key); color: var(--key-text);
  font-weight: 700; font-size: 15px; text-transform: uppercase; padding: 0; touch-action: manipulation;
}
.key.wide { flex: 1.5; font-size: 12px; }
.key.correct { background: var(--correct); color: var(--on-tile); }
.key.present { background: var(--present); color: var(--on-tile); }
.key.absent { background: var(--key-used); color: var(--key-used-text); }
.kb-spacer { flex: .5; }

.toast {
  position: fixed; top: 110px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; }

dialog {
  border: 0; border-radius: 14px; background: var(--surface); color: var(--text);
  width: min(440px, calc(100vw - 32px)); padding: 22px 22px 18px; box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog h3 { margin: 18px 0 8px; font-size: 15px; }
.close { position: absolute; top: 8px; right: 8px; width: 40px; height: 40px; border: 0; background: none; font-size: 26px; border-radius: 8px; }
.close:hover { background: var(--key); }
.legend { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 6px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.row small { display: block; color: var(--muted); font-size: 13px; }
.row input { width: 44px; height: 24px; accent-color: var(--accent); flex: none; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; gap: 4px; }
.stats b { display: block; font-size: 28px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 12px; color: var(--muted); }
.dist { display: grid; gap: 4px; }
.dist-row { display: grid; grid-template-columns: 14px 1fr; gap: 6px; align-items: center; font-size: 14px; font-variant-numeric: tabular-nums; }
.dist-bar { background: var(--absent); color: #fff; text-align: right; padding: 1px 6px; min-width: 22px; font-weight: 700; }
.dist-bar.hit { background: var(--correct); }
