/* Soft, low-glare palette. State colors stay clearly distinct but muted. */
:root {
  --bg: #f6f4ef;
  --surface: #fbfaf7;
  --fg: #2e3036;
  --muted: #7d7f86;
  --line: #e3dfd6;
  --tile-border: #dedad1;
  --tile-border-filled: #a8a59d;
  --tile-empty: #fbfaf7;
  --key: #e5e1d8;
  --key-fg: #3a3c42;
  --correct: #6f9e7c;
  --present: #d1ac5f;
  --absent: #9b9da3;
  --on-color: #fdfcf9;
  --seg-bg: #ebe7df;
  --seg-active: #fdfcf9;
  --toast-bg: #3a3c42;
  --toast-fg: #f6f4ef;
  --shadow: 0 1px 2px rgb(60 50 30 / 0.08), 0 6px 20px rgb(60 50 30 / 0.06);
  --tw: 58px;
  --th: 58px;
  --gap: 5px;
  --radius: 6px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
.dark {
  --bg: #17191e;
  --surface: #1d2026;
  --fg: #e4e3df;
  --muted: #8b8e96;
  --line: #2b2e35;
  --tile-border: #30333b;
  --tile-border-filled: #5a5e68;
  --tile-empty: #1b1d22;
  --key: #5d626d;
  --key-fg: #f0efeb;
  --correct: #5a8a69;
  --present: #b6974f;
  --absent: #33363e;
  --on-color: #f2f1ed;
  --seg-bg: #23262d;
  --seg-active: #363a43;
  --toast-bg: #e4e3df;
  --toast-fg: #17191e;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 28px rgb(0 0 0 / 0.35);
  color-scheme: dark;
}
.contrast { --correct: #d98a5f; --present: #6fa3cc; }
.dark.contrast { --correct: #c47a52; --present: #5a8db5; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  transition: background-color 300ms ease, color 300ms ease;
}
button { font: inherit; color: inherit; }
button:focus-visible { outline: 2px solid var(--correct); outline-offset: 2px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* Header: icons + mode switch on one row */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  padding: calc(6px + env(safe-area-inset-top)) 8px 6px;
  flex-shrink: 0;
}
.topbar-side { display: flex; }
.topbar-side.right { justify-content: flex-end; }
.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border: 0; background: none; border-radius: 10px;
  cursor: pointer; color: var(--muted);
  transition: background-color 150ms ease, color 150ms ease, transform 150ms var(--ease-out);
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn:hover { color: var(--fg); background: color-mix(in srgb, var(--fg) 6%, transparent); }
.icon-btn:active { transform: scale(0.92); }

.seg {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--seg-bg);
}
.seg button {
  border: 0; background: none; cursor: pointer;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 14px; font-weight: 650;
  color: var(--muted);
  transition: background-color 200ms ease, color 200ms ease, box-shadow 200ms ease;
}
.seg button[aria-selected="true"] {
  background: var(--seg-active);
  color: var(--fg);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.08);
}
.seg.small button { padding: 4px 11px; font-size: 12.5px; }

.subbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  width: 100%; max-width: 560px; margin: 0 auto;
  padding: 0 12px 4px;
  flex-shrink: 0;
}
.subline {
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.subline b { color: var(--fg); font-weight: 650; }

.game {
  flex: 1;
  display: flex; flex-direction: column;
  min-height: 0;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

/* Boards */
.board-area {
  flex: 1;
  min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 4px 6px;
}
.boards { display: grid; gap: calc(var(--gap) * 2.4); }
.boards.n2, .boards.n4 { grid-template-columns: repeat(2, auto); }
.boards.enter { animation: boards-in 320ms var(--ease-out); }
@keyframes boards-in { from { opacity: 0; transform: translateY(6px); } }
.board { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: grid; grid-template-columns: repeat(5, var(--tw)); gap: var(--gap); }

.tile {
  width: var(--tw); height: var(--th);
  display: grid; place-items: center;
  border: 2px solid var(--tile-border);
  border-radius: var(--radius);
  background: var(--tile-empty);
  font-size: calc(min(var(--tw), var(--th)) * 0.5);
  font-weight: 700;
  text-transform: uppercase;
  line-height: 1;
  user-select: none;
  color: var(--fg);
  transition: border-color 120ms ease, opacity 300ms ease;
}
.tile[data-filled] { border-color: var(--tile-border-filled); animation: pop 140ms var(--ease-out); }
.tile[data-state] { border-color: transparent; color: var(--on-color); }
.tile[data-state="correct"] { background: var(--correct); }
.tile[data-state="present"] { background: var(--present); }
.tile[data-state="absent"] { background: var(--absent); }
.board.solved .tile:not([data-state]) { opacity: 0.3; }

.tile.flip-in { animation: flip-in 230ms cubic-bezier(0.55, 0, 0.75, 0.5) forwards; }
.tile.flip-out { animation: flip-out 230ms cubic-bezier(0.25, 0.5, 0.45, 1) forwards; }
.tile.flip-in, .tile.flip-out, .tile.bounce { will-change: transform; }
.tile.bounce { animation: bounce 900ms var(--ease-out); }
.row-shake { animation: shake 500ms ease; }

@keyframes pop {
  from { transform: scale(0.9); }
  50% { transform: scale(1.06); }
  to { transform: scale(1); }
}
@keyframes flip-in { from { transform: perspective(500px) rotateX(0); } to { transform: perspective(500px) rotateX(-90deg); } }
@keyframes flip-out { from { transform: perspective(500px) rotateX(-90deg); } to { transform: perspective(500px) rotateX(0); } }
@keyframes shake {
  15% { transform: translateX(-6px); }
  30% { transform: translateX(5px); }
  45% { transform: translateX(-4px); }
  60% { transform: translateX(3px); }
  75% { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}
@keyframes bounce {
  0% { transform: translateY(0); }
  30% { transform: translateY(-38%); }
  50% { transform: translateY(4%); }
  68% { transform: translateY(-12%); }
  85% { transform: translateY(1%); }
  100% { transform: translateY(0); }
}

/* Keyboard */
.keyboard {
  flex-shrink: 0;
  padding: 4px 5px calc(6px + env(safe-area-inset-bottom));
  user-select: none;
}
.kb-row { display: flex; gap: 5px; margin-bottom: 7px; }
.kb-row:last-child { margin-bottom: 0; }
.kb-row .spacer { flex: 0.5; }
.key {
  flex: 1;
  min-width: 0;
  height: clamp(44px, 6.6dvh, 56px);
  border: 0; border-radius: 7px;
  background: var(--key);
  color: var(--key-fg);
  font-weight: 650; font-size: 14px;
  text-transform: uppercase;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  display: grid; place-items: center;
  padding: 0;
  transition: background-color 250ms ease, color 250ms ease, opacity 250ms ease, transform 120ms var(--ease-out);
}
.key.wide { flex: 1.5; font-size: 11.5px; letter-spacing: 0.02em; }
.key svg { width: 22px; height: 22px; fill: currentColor; }
.key:active { transform: scale(0.94); }
.key > span { position: relative; z-index: 1; }
.key[data-state="correct"] { background: var(--correct); color: var(--on-color); }
.key[data-state="present"] { background: var(--present); color: var(--on-color); }
.key[data-state="absent"] { background: var(--absent); color: var(--on-color); }
.key.dead { opacity: 0.4; }

.key .segs { position: absolute; inset: 0; display: grid; }
.key .segs.n2 { grid-template-columns: 1fr 1fr; }
.key .segs.n4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.key .segs i { display: block; transition: background-color 250ms ease; }
.key .segs i[data-state="correct"] { background: var(--correct); }
.key .segs i[data-state="present"] { background: var(--present); }
.key .segs i[data-state="absent"] { background: var(--absent); }
.key.colored { color: var(--on-color); text-shadow: 0 1px 2px rgb(0 0 0 / 0.25); }

/* Toasts */
.toasts {
  position: fixed; top: calc(92px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 100; pointer-events: none;
}
.toast {
  background: var(--toast-bg); color: var(--toast-fg);
  padding: 10px 16px; border-radius: 10px;
  font-weight: 650; font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in 220ms var(--ease-out);
  transition: opacity 250ms ease, transform 250ms ease;
  white-space: nowrap;
}
.toast.hide { opacity: 0; transform: translateY(-6px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px) scale(0.96); } }

/* Modals */
.modal {
  border: 0;
  border-radius: 16px;
  padding: 22px 18px 18px;
  width: min(480px, calc(100vw - 20px));
  max-height: calc(100dvh - 24px);
  background: var(--surface);
  color: var(--fg);
  box-shadow: var(--shadow);
  overflow-y: auto;
  overscroll-behavior: contain;
  opacity: 0;
  transform: translateY(16px) scale(0.98);
  transition: opacity 220ms var(--ease-out), transform 260ms var(--ease-out), overlay 260ms allow-discrete, display 260ms allow-discrete;
}
.modal[open] { opacity: 1; transform: none; }
@starting-style { .modal[open] { opacity: 0; transform: translateY(16px) scale(0.98); } }
.modal:focus { outline: none; }
.modal::backdrop {
  background: rgb(20 20 24 / 0);
  backdrop-filter: blur(0);
  transition: background-color 260ms ease, backdrop-filter 260ms ease, overlay 260ms allow-discrete, display 260ms allow-discrete;
}
.modal[open]::backdrop { background: rgb(20 20 24 / 0.38); backdrop-filter: blur(2px); }
@starting-style { .modal[open]::backdrop { background: rgb(20 20 24 / 0); backdrop-filter: blur(0); } }
.dark .modal { border: 1px solid var(--line); }
.modal h2 { margin: 0 0 12px; font-size: 15px; text-transform: uppercase; letter-spacing: 0.06em; text-align: center; font-weight: 700; }
.modal h3 { margin: 18px 0 10px; font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.modal .close {
  position: absolute; top: 10px; right: 10px;
  width: 34px; height: 34px; border: 0; background: none; border-radius: 10px;
  font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted);
}
.modal .close:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
#modal-help h2, #modal-settings h2 { text-align: left; }
.lead { font-size: 17px; margin: 0 0 8px; }
.rules { padding-left: 20px; margin: 0; line-height: 1.6; font-size: 15px; }
.muted { color: var(--muted); font-size: 14px; line-height: 1.45; }
.example { margin-bottom: 10px; }
.example p { margin: 6px 0 0; font-size: 14px; }
.ex-row { display: flex; gap: 4px; --tw: 34px; --th: 34px; }
.ex-row .tile { font-size: 18px; }

.setting {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  padding: 14px 0; border-bottom: 1px solid var(--line);
}
.setting:last-child { border-bottom: 0; }
.setting-name { font-size: 16px; }
.setting-desc { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.switch {
  flex-shrink: 0;
  width: 40px; height: 24px; border-radius: 999px; border: 0;
  background: var(--key); position: relative; cursor: pointer;
  transition: background-color 200ms ease;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.2);
  transition: transform 220ms var(--ease-out);
}
.switch[aria-checked="true"] { background: var(--correct); }
.switch[aria-checked="true"]::after { transform: translateX(16px); }

/* Stats */
.stats { display: flex; justify-content: center; gap: 6px; margin-bottom: 4px; }
.stat { flex: 1; max-width: 84px; text-align: center; }
.stat-num { font-size: 30px; font-weight: 300; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 11.5px; color: var(--muted); line-height: 1.25; }
.dist-title { text-align: center; }
.dist { display: flex; flex-direction: column; gap: 4px; padding: 0 6px; }
.dist-row { display: flex; align-items: center; gap: 6px; font-size: 13px; font-variant-numeric: tabular-nums; }
.dist-row .label { width: 16px; text-align: right; color: var(--muted); }
.dist-row .bar-wrap { flex: 1; }
.dist-row .bar {
  min-width: 26px;
  border-radius: 4px;
  background: var(--absent); color: var(--on-color);
  text-align: right; padding: 2px 8px; font-weight: 650;
  transform-origin: left;
  animation: grow 500ms var(--ease-out) backwards;
}
.dist-row .bar.hl { background: var(--correct); }
@keyframes grow { from { transform: scaleX(0); } }
.dist-empty { text-align: center; color: var(--muted); font-size: 14px; }

.result {
  text-align: center;
  padding-bottom: 14px; margin-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.result-head { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.result-sub { color: var(--muted); font-size: 14px; }

.share-block { margin-top: 18px; }
.next { text-align: center; margin-bottom: 12px; }
.next-label { font-size: 11.5px; text-transform: uppercase; font-weight: 700; letter-spacing: 0.06em; color: var(--muted); }
.next-time { font-size: 28px; font-weight: 300; font-variant-numeric: tabular-nums; }
.share-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.btn {
  border: 1px solid var(--line);
  background: var(--bg); color: var(--fg);
  border-radius: 12px;
  padding: 11px 6px;
  font-weight: 650; font-size: 13.5px;
  white-space: nowrap;
  cursor: pointer;
  transition: transform 120ms var(--ease-out), filter 150ms ease, background-color 150ms ease;
}
.btn.primary { background: var(--correct); border-color: transparent; color: var(--on-color); }
.btn:hover { filter: brightness(1.04); }
.btn:active { transform: scale(0.97); }

.meanings { margin-top: 18px; display: flex; flex-direction: column; gap: 10px; }
.meaning { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; background: var(--bg); }
.meaning-word { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.meaning-word b { font-size: 17px; letter-spacing: 0.08em; text-transform: uppercase; }
.meaning-word .phon { color: var(--muted); font-size: 14px; }
.meaning-word .audio { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; font-size: 16px; }
.meaning ol { margin: 6px 0 0; padding-left: 20px; font-size: 14px; line-height: 1.5; }
.meaning .pos { font-style: italic; color: var(--muted); font-size: 13px; margin-top: 6px; }
.meaning .src { font-size: 12px; margin-top: 6px; }
.meaning .src a { color: var(--muted); }
.practice-cta { margin-top: 16px; display: flex; justify-content: center; }

@media (max-width: 380px) {
  .seg button { padding: 6px 10px; font-size: 13px; }
  .icon-btn { width: 34px; height: 34px; }
  .kb-row { gap: 4px; }
  .key { font-size: 13px; }
}
@media (min-width: 700px) {
  .topbar { padding-top: 12px; padding-bottom: 10px; }
  .key { height: 56px; }
}
@media (prefers-reduced-motion: reduce) {
  .tile[data-filled], .tile.bounce, .row-shake, .boards.enter, .toast, .dist-row .bar { animation: none; }
  .tile.flip-in, .tile.flip-out { animation-duration: 1ms; }
  .modal, .modal::backdrop { transition-duration: 1ms; }
}
