:root {
  --bg: #ffffff;
  --fg: #1a1a1b;
  --muted: #6e6e73;
  --line: #d3d6da;
  --line-strong: #878a8c;
  --key: #d3d6da;
  --key-fg: #1a1a1b;
  --absent: #787c7e;
  --present: #c9b458;
  --correct: #6aaa64;
  --on-color: #ffffff;
  --toast-bg: #1a1a1b;
  --toast-fg: #ffffff;
  --dialog-bg: #ffffff;
  --accent: #0057b7;
  --radius: 6px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --fg: #f2f2f2;
    --muted: #9a9aa0;
    --line: #3a3a3c;
    --line-strong: #565758;
    --key: #818384;
    --key-fg: #ffffff;
    --absent: #3a3a3c;
    --present: #b59f3b;
    --correct: #538d4e;
    --toast-bg: #f2f2f2;
    --toast-fg: #121213;
    --dialog-bg: #1d1d1f;
    --accent: #4d8fe0;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button { font: inherit; color: inherit; }

.banner {
  flex: none;
  padding: calc(7px + env(safe-area-inset-top)) 12px 7px;
  text-align: center;
  color: #fff;
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.03em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
  background: linear-gradient(90deg, #0057b7, #1a6fd1 50%, #0057b7);
  border-bottom: 4px solid #ffd700;
}
.bar {
  flex: none;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  max-width: 500px;
  width: 100%;
  margin: 0 auto;
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
}
.bar h1 {
  margin: 0;
  text-align: center;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}
.icon-btn {
  width: 40px; height: 40px;
  border: 0; border-radius: 50%;
  background: transparent;
  font-size: 1.35rem; font-weight: 700;
  cursor: pointer;
}
.icon-btn:hover { background: color-mix(in srgb, var(--fg) 8%, transparent); }

main {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
}
.board-wrap {
  height: 100%;
  max-height: 420px;
  aspect-ratio: 5 / 6;
  max-width: min(350px, 100%);
  display: flex;
  align-items: center;
}
.board {
  width: 100%;
  aspect-ratio: 5 / 6;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  container-type: inline-size;
}
.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
}
.tile {
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  font-size: 10.5cqi;
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
}
.tile[data-filled] { border-color: var(--line-strong); }
.tile[data-state] { border-color: transparent; color: var(--on-color); }
.tile[data-state="absent"], .mini[data-state="absent"], .key[data-state="absent"] { background: var(--absent); }
.tile[data-state="present"], .mini[data-state="present"], .key[data-state="present"] { background: var(--present); }
.tile[data-state="correct"], .mini[data-state="correct"], .key[data-state="correct"] { background: var(--correct); }

.tile.pop { animation: pop 0.1s ease-out; }
.tile.flip { animation: flip 0.5s ease-in-out both; }
.row.shake { animation: shake 0.45s; }
.tile.bounce { animation: bounce 0.6s ease both; }
@keyframes pop { 50% { transform: scale(1.12); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(-90deg); } 100% { transform: rotateX(0); } }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}
@keyframes bounce { 0%, 20% { transform: translateY(0); } 40% { transform: translateY(-28%); } 50% { transform: translateY(4%); } 60% { transform: translateY(-12%); } 80% { transform: translateY(1%); } 100% { transform: translateY(0); } }

.toasts {
  position: absolute;
  top: 8px; left: 0; right: 0;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none;
  z-index: 5;
}
.toast {
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 14px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.95rem;
  transition: opacity 0.3s;
}
.toast.fade { opacity: 0; }

.keyboard {
  flex: none;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 6px calc(8px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 7px;
  user-select: none;
}
.krow { display: flex; gap: 4px; }
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 54px;
  border: 0;
  border-radius: 5px;
  background: var(--key);
  color: var(--key-fg);
  font-size: clamp(0.9rem, 3.6vw, 1.15rem);
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.2s;
}
.key.wide { flex-grow: 1.6; font-size: clamp(1rem, 4vw, 1.3rem); }
.key[data-state] { color: var(--on-color); }
.key:active { filter: brightness(0.9); }
@media (max-height: 640px) { .key { height: 46px; } }

dialog {
  border: 0;
  border-radius: 12px;
  padding: 24px 22px 20px;
  max-width: min(420px, calc(100vw - 32px));
  background: var(--dialog-bg);
  color: var(--fg);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 1.35rem; text-align: center; }
dialog p { margin: 0 0 12px; line-height: 1.45; }
.muted { color: var(--muted); font-size: 0.92rem; }
.legend { list-style: none; padding: 0; margin: 0 0 14px; display: grid; gap: 8px; }
.legend li { display: flex; align-items: center; gap: 10px; }
.mini {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; flex: none;
  border-radius: 5px;
  color: var(--on-color);
  font-weight: 700; text-transform: uppercase;
}
.answer { display: flex; justify-content: center; gap: 5px; margin: 4px 0 14px; }
.answer .mini { width: 44px; height: 44px; font-size: 1.4rem; }
.definition { font-size: 0.98rem; max-height: 38dvh; overflow-y: auto; overscroll-behavior: contain; }
.definition b { font-weight: 700; }
.source { color: var(--muted); font-size: 0.8rem; text-align: right; margin-top: -6px; }
.primary, .secondary {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  border: 0;
  border-radius: 8px;
  background: var(--correct);
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
}
.secondary { background: var(--accent); }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 6px; }
.actions button { margin-top: 0; }

@media (prefers-reduced-motion: reduce) {
  .tile.pop, .tile.flip, .row.shake, .tile.bounce { animation: none; }
}
