/* Brand colors are fixed; background and text follow Telegram's theme when
   running inside Telegram, and the system theme otherwise. app.js sets
   data-theme on <html>. */
:root {
  --blue: #0267e9;
  --amber: #f5a524;
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #101828);
  --muted: color-mix(in srgb, var(--text) 50%, var(--bg));
  --surface: color-mix(in srgb, var(--text) 6%, var(--bg));
  --surface-2: color-mix(in srgb, var(--text) 11%, var(--bg));
  --absent: #98a2b3;
  --radius: 12px;
  font-family: "Nunito", system-ui, -apple-system, sans-serif;
}
:root[data-theme="dark"] {
  --bg: var(--tg-theme-bg-color, #0f1216);
  --text: var(--tg-theme-text-color, #f2f4f7);
  /* Light enough to stand apart from empty tiles, dark enough for white text. */
  --absent: #5b6472;
}

* { box-sizing: border-box; margin: 0; }
/* Elements with their own display (grid/flex) must still honour [hidden]. */
[hidden] { display: none !important; }

html, body { height: 100%; background: var(--bg); color: var(--text); }

body {
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  padding: 0 12px env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
/* Dialogs focus their first button on open; don't ring it for touch users. */
.sheet .close:focus-visible { outline: none; }
svg { width: 20px; height: 20px; stroke-width: 2; }

/* ---------- header ---------- */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0 4px;
}
.logo { height: 28px; }
:root[data-theme="dark"] .logo { content: url("assets/logo-white.png"); }

.icon-btn {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 50%;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.icon-btn:active { background: var(--surface); color: var(--text); }

/* ---------- board ---------- */

main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  position: relative;
}

.caption { font-size: 14px; font-weight: 700; color: var(--muted); }

#board {
  --cols: 6;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  width: min(360px, 100%, calc((100dvh - 406px) * var(--cols) / 6));
  aspect-ratio: var(--cols) / 6;
}
.row { display: grid; grid-template-columns: repeat(var(--cols), 1fr); gap: 6px; }

.tile {
  display: grid; place-items: center;
  border-radius: var(--radius);
  background: var(--surface);
  font-size: clamp(20px, 6.5vw, 28px);
  font-weight: 800;
  line-height: 1;
  transition: background 0.1s;
}
.tile.filled { background: var(--bg); box-shadow: inset 0 0 0 2px var(--surface-2); animation: pop 0.12s ease-out; }
.row.current .tile { cursor: pointer; }
.tile.active { background: var(--bg); box-shadow: inset 0 0 0 2.5px var(--blue); }
.tile.correct, .tile.present, .tile.absent { color: #fff; box-shadow: none; }
.tile.correct { background: var(--blue); }
.tile.present { background: var(--amber); }
.tile.absent { background: var(--absent); }
.tile.flip { animation: flip 0.4s ease-in-out; }

.row.shake { animation: shake 0.4s; }
.row.win .tile { animation: jump 0.45s ease; }
.row.win .tile:nth-child(2) { animation-delay: 0.07s; }
.row.win .tile:nth-child(3) { animation-delay: 0.14s; }
.row.win .tile:nth-child(4) { animation-delay: 0.21s; }
.row.win .tile:nth-child(5) { animation-delay: 0.28s; }

#toast {
  position: absolute; top: 0; left: 50%;
  transform: translate(-50%, -8px);
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--text); color: var(--bg);
  font-size: 14px; font-weight: 700;
  white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 5;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#tg-only { max-width: 320px; text-align: center; display: grid; gap: 14px; }
#tg-only[hidden] { display: none; }
#tg-only h2 { font-size: 22px; font-weight: 800; }
#tg-only p { color: var(--muted); font-size: 15px; line-height: 1.45; }
#tg-only .primary { text-decoration: none; }

/* ---------- keyboard ---------- */

#keyboard { padding: 12px 0 10px; }
.krow { display: grid; grid-template-columns: repeat(11, 1fr); gap: 5px; margin-bottom: 6px; }
.key {
  min-width: 0;
  height: 50px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: var(--surface);
  font-size: clamp(14px, 4.2vw, 18px);
  font-weight: 700;
  touch-action: manipulation;
  transition: transform 0.06s, background 0.15s;
}
.key:active { transform: scale(0.94); }
.key.wide { grid-column: span 2; }
.key.space { grid-column: span 7; }
.key.enter { background: var(--blue); color: #fff; }
.key.correct, .key.present, .key.absent { color: #fff; }
.key.correct { background: var(--blue); }
.key.present { background: var(--amber); }
.key.absent { background: var(--absent); color: color-mix(in srgb, #fff 70%, var(--absent)); }
/* Dark theme: a used-up letter fades out rather than lighting up. */
:root[data-theme="dark"] .key.absent {
  background: color-mix(in srgb, var(--text) 2%, var(--bg));
  color: color-mix(in srgb, var(--text) 30%, var(--bg));
}

/* ---------- bottom sheets ---------- */

.sheet {
  position: fixed;
  inset: auto 0 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 88dvh;
  margin: 0 auto;
  padding: 10px 20px calc(20px + env(safe-area-inset-bottom));
  border: 0;
  border-radius: 24px 24px 0 0;
  background: var(--bg);
  color: var(--text);
  overflow-y: auto;
  user-select: text;
}
.sheet[open] { animation: slide-up 0.28s cubic-bezier(0.2, 0.9, 0.3, 1); }
.sheet::backdrop { background: rgb(0 0 0 / 0.35); animation: fade 0.2s; }
.handle { width: 36px; height: 4px; border-radius: 2px; background: var(--surface-2); margin: 0 auto 12px; }
.sheet .close { position: absolute; top: 10px; right: 10px; }
.sheet h2 { font-size: 22px; font-weight: 800; text-align: center; margin: 8px 0 16px; }

.lead { font-size: 16px; font-weight: 700; text-align: center; margin-bottom: 18px; }
.rule { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.rule p { font-size: 15px; color: var(--muted); }
.rule b { color: var(--text); }
.note { font-size: 14px; color: var(--muted); margin: 18px 0 20px; line-height: 1.45; }

.mini-row { display: flex; gap: 4px; }
.mini-row i, .mini-row span {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  border-radius: 8px;
  background: var(--surface);
  font-style: normal; font-size: 15px; font-weight: 800;
}
.mini-row .correct { background: var(--blue); color: #fff; }
.mini-row .present { background: var(--amber); color: #fff; }
.mini-row .absent { background: var(--absent); color: #fff; }

#answer-box { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 20px; }
#answer-box[hidden] { display: none; }
#answer-word span { width: 38px; height: 38px; font-size: 20px; border-radius: 10px; background: var(--blue); color: #fff; }
#answer-word.lost span { background: var(--text); color: var(--bg); }
#answer-def { font-size: 15px; color: var(--muted); text-align: center; }
#answer-def a { color: var(--blue); text-decoration: none; font-weight: 700; }
#answer-def:empty { display: none; }
.dict-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--blue) 10%, var(--bg));
  color: var(--blue);
  font-size: 14px; font-weight: 800;
  text-decoration: none;
}
.dict-link[hidden] { display: none; }
.dict-link svg { width: 16px; height: 16px; }

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 14px 0;
  border-radius: 16px;
  background: var(--surface);
  text-align: center;
}
.stats-row b { display: block; font-size: 24px; font-weight: 800; }
.stats-row span { font-size: 12px; font-weight: 700; color: var(--muted); }

#distribution { display: grid; gap: 6px; margin: 18px 0 4px; }
.bar-row { display: grid; grid-template-columns: 14px 1fr; align-items: center; gap: 8px; font-size: 13px; font-weight: 700; color: var(--muted); }
.bar {
  height: 22px;
  min-width: 26px;
  padding: 0 8px;
  display: flex; align-items: center; justify-content: flex-end;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  transition: width 0.5s ease;
}
.bar.today { background: var(--blue); color: #fff; }

#finished-box[hidden] { display: none; }
.next { display: flex; justify-content: space-between; align-items: baseline; margin: 18px 2px 14px; font-weight: 700; color: var(--muted); }
.next b { font-size: 20px; color: var(--text); font-variant-numeric: tabular-nums; }

.primary {
  width: 100%; height: 52px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  border-radius: 14px;
  background: var(--blue); color: #fff;
  font-size: 17px; font-weight: 800;
  transition: transform 0.06s, filter 0.15s;
}
.primary:active { transform: scale(0.98); filter: brightness(0.95); }
.text-btn {
  width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  margin-top: 10px; padding: 8px;
  font-size: 14px; font-weight: 700; color: var(--muted);
}
.text-btn svg { width: 16px; height: 16px; }
a.text-btn { text-decoration: none; color: var(--blue); }

/* ---------- motion ---------- */

@keyframes pop { 40% { transform: scale(1.08); } }
@keyframes flip { 50% { transform: scaleY(0); } }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@keyframes jump { 40% { transform: translateY(-14px); } 70% { transform: translateY(2px); } }
@keyframes slide-up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::backdrop { animation: none !important; transition: none !important; }
}

@media (max-height: 640px) {
  .key { height: 44px; }
  header { padding-top: 6px; }
}
.note + .note { margin-top: -10px; }
