/* ---------- Theme ---------- */
:root {
  --bg: #ffffff;
  --text: #000000;
  --muted: #787c7e;
  --line: #d3d6da;
  --tile-empty: #d3d6da;
  --tile-filled: #878a8c;
  --tile-text: #000000;
  --key-bg: #d3d6da;
  --key-text: #000000;
  --modal-bg: #ffffff;
  --shadow: 0 4px 23px 0 rgba(0, 0, 0, 0.2);
  --toast-bg: #000000;
  --toast-text: #ffffff;
  --correct: #6aaa64;
  --present: #c9b458;
  --absent: #787c7e;
  --absent-key: #787c7e;
  --switch-off: #878a8c;
  --font: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  --font-title: "Karnak Condensed", Georgia, "Times New Roman", serif;
}
.dark {
  --bg: #121213;
  --text: #ffffff;
  --muted: #818384;
  --line: #3a3a3c;
  --tile-empty: #3a3a3c;
  --tile-filled: #565758;
  --tile-text: #ffffff;
  --key-bg: #818384;
  --key-text: #ffffff;
  --modal-bg: #121213;
  --shadow: 0 4px 23px 0 rgba(0, 0, 0, 0.6);
  --toast-bg: #ffffff;
  --toast-text: #000000;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --absent-key: #3a3a3c;
  --switch-off: #565758;
}
.contrast { --correct: #f5793a; --present: #85c0f9; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button { font-family: inherit; color: inherit; }
svg { fill: currentColor; }

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  max-width: 500px;
  margin: 0 auto;
}

/* ---------- Header ---------- */
.header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;  /* title stays centred whatever the icons need */
  align-items: center;
  height: 65px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  position: relative;
}
.app::before { /* full-width header rule on wide screens */
  content: "";
  position: absolute;
  left: 0; right: 0; top: 65px;
  border-bottom: 1px solid var(--line);
  pointer-events: none;
}
.header-side { display: flex; gap: 4px; }
.header-side.right { justify-content: flex-end; }
.title {
  margin: 0;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 36px;
  letter-spacing: 0.01em;
  text-align: center;
  pointer-events: none;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: none;
  cursor: pointer;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:hover { background: var(--line); }

/* ---------- Board ---------- */
.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 10px;
}
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  width: min(350px, calc((100dvh - 65px - 200px - 20px) * 5 / 6));
  aspect-ratio: 5 / 6;
}
.row { display: grid; grid-template-columns: repeat(var(--cols, 5), 1fr); gap: 5px; }
.tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  aspect-ratio: 1;
  border: 2px solid var(--tile-empty);
  font-size: 2rem;
  line-height: 1;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--tile-text);
  user-select: none;
}
.tile[data-state="tbd"] { border-color: var(--tile-filled); }
.tile[data-state="correct"],
.tile[data-state="present"],
.tile[data-state="absent"] { border: 0; color: #fff; }
.tile[data-state="correct"] { background: var(--correct); }
.tile[data-state="present"] { background: var(--present); }
.tile[data-state="absent"] { background: var(--absent); }

/* typing pop */
.tile[data-anim="pop"] { animation: pop 100ms; }
@keyframes pop {
  from { transform: scale(0.8); opacity: 0; }
  40% { transform: scale(1.1); opacity: 1; }
}
/* reveal flip: colour switches at the halfway point (set from JS) */
.tile[data-anim="flip-in"] { animation: flip-in 250ms ease-in forwards; }
.tile[data-anim="flip-out"] { animation: flip-out 250ms ease-in; }
@keyframes flip-in { from { transform: rotateX(0); } to { transform: rotateX(-90deg); } }
@keyframes flip-out { from { transform: rotateX(-90deg); } to { transform: rotateX(0); } }
/* wrong word */
.row[data-anim="shake"] { animation: shake 600ms; }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
/* win */
.tile[data-anim="bounce"] { animation: bounce 1000ms; }
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-30px); }
  50% { transform: translateY(5px); }
  60% { transform: translateY(-15px); }
  80% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}

/* ---------- Keyboard ---------- */
.keyboard { padding: 0 8px 8px; user-select: none; }
.kb-row { display: flex; gap: 6px; margin-bottom: 8px; touch-action: manipulation; }
.kb-spacer { flex: 0.5; }
.key {
  flex: 1;
  height: 58px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 1.25em;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.1s;
}
.key.wide { flex: 1.5; font-size: 12px; }
.key svg { width: 24px; height: 24px; }
.key:active { opacity: 0.8; }
.key[data-state="correct"] { background: var(--correct); color: #fff; }
.key[data-state="present"] { background: var(--present); color: #fff; }
.key[data-state="absent"] { background: var(--absent-key); color: #fff; }

/* ---------- Toasts ---------- */
.toasts {
  position: absolute;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}
.toast {
  margin: 16px;
  padding: 16px;
  border-radius: 4px;
  background: var(--toast-bg);
  color: var(--toast-text);
  font-weight: 700;
  font-size: 14px;
  opacity: 1;
  transition: opacity 300ms cubic-bezier(0.645, 0.045, 0.355, 1);
  white-space: nowrap;
}
.toast.fade { opacity: 0; }

/* ---------- Modals ---------- */
.modal {
  width: min(500px, calc(100% - 32px));
  max-height: calc(100% - 64px);
  padding: 16px 32px 32px;
  border: 0;
  border-radius: 8px;
  background: var(--modal-bg);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow-y: auto;
}
.modal[open] { animation: slide-in 200ms; }
@keyframes slide-in { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal::backdrop { background: rgba(255, 255, 255, 0.5); }
.dark .modal::backdrop { background: rgba(0, 0, 0, 0.5); }
.modal .close {
  position: absolute;
  right: 16px;
  top: 16px;
  border: 0;
  background: none;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
}
.modal h2 {
  margin: 24px 0 12px;
  font-size: 16px;
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 0.02em;
}
.modal .modal-title-left {
  text-transform: none;
  text-align: left;
  font-family: var(--font-title);
  font-size: 28px;
  margin: 24px 0 4px;
}
.modal p, .modal li { font-size: 16px; line-height: 1.4; }
.lead { font-size: 20px !important; margin: 0 0 8px; }
.rules { padding-left: 20px; margin: 0 0 12px; }
.bold { font-weight: 700; }
.example { margin-bottom: 18px; }
.example p { margin: 6px 0 0; }
.row.mini { display: inline-grid; gap: 4px; grid-template-columns: repeat(5, 32px); }
.row.mini .tile { font-size: 1.3rem; }
.modal hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.inline-icon { font-weight: 700; }

/* stats */
.stats { display: flex; justify-content: center; text-align: center; margin-bottom: 10px; }
.stats > div { flex: 1; max-width: 80px; }
.stat-num { display: block; font-size: 36px; font-variant-numeric: proportional-nums; letter-spacing: 0.05em; }
.stat-label { display: block; font-size: 12px; }
.dist { width: 80%; margin: 0 auto 8px; }
.dist-row { display: flex; align-items: center; height: 20px; margin-bottom: 4px; font-size: 14px; }
.dist-row .n { width: 14px; }
.dist-row .bar {
  min-width: 7%;
  height: 100%;
  padding: 0 8px;
  background: var(--absent);
  color: #fff;
  font-weight: 700;
  text-align: right;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  transition: width 400ms;
}
.dist-row .bar.now { background: var(--correct); }
.dist-empty { text-align: center; color: var(--muted); }
.stats-footer { display: flex; gap: 12px; justify-content: center; margin-top: 18px; }
.stats-footer[hidden] { display: none; }
.modal:focus { outline: none; }
.btn {
  flex: 1;
  max-width: 180px;
  height: 52px;
  border: 0;
  border-radius: 4px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 18px;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn.share { background: var(--correct); color: #fff; }
.btn svg { width: 22px; height: 22px; }
.btn:active { opacity: 0.85; }

/* settings */
.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}
.setting-name { font-size: 18px; }
.setting-desc { font-size: 12px; color: var(--muted); margin-top: 2px; }
.switch {
  flex: none;
  width: 32px;
  height: 20px;
  padding: 2px;
  border: 0;
  border-radius: 999px;
  background: var(--switch-off);
  cursor: pointer;
  transition: background 0.2s;
}
.switch span {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.switch[aria-checked="true"] { background: var(--correct); }
.switch[aria-checked="true"] span { transform: translateX(12px); }

/* ---------- Small screens ---------- */
@media (max-height: 640px) {
  .header { height: 50px; }
  .app::before { top: 50px; }
  .title { font-size: 28px; }
  .key { height: 48px; }
  .board { width: min(300px, calc((100dvh - 50px - 170px - 20px) * 5 / 6)); }
}
@media (max-width: 420px) {
  .header { padding: 0 8px; }
  .header-side { gap: 0; }
  .title { font-size: 30px; }
  .icon-btn { width: 32px; }
}
@media (max-width: 360px) {
  .title { font-size: 26px; }
  .icon-btn { width: 28px; }
  .modal { padding: 16px 20px 24px; }
}
