:root {
  --bg: #f6f2ea;
  --surface: #fffdf8;
  --ink: #1f1c18;
  --muted: #6f685e;
  --line: #ddd5c7;
  --key: #e9e2d5;
  --key-ink: #1f1c18;
  --accent: #d9602a;
  --accent-soft: #f7dccd;
  --accent-ink: #fff;
  --good: #3f9a5c;
  --bad: #c2413b;
  --shadow: 0 1px 0 rgb(0 0 0 / .06), 0 6px 20px rgb(60 40 20 / .08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16151a;
    --surface: #211f26;
    --ink: #eeeae3;
    --muted: #a09a90;
    --line: #36333d;
    --key: #2e2c35;
    --key-ink: #eeeae3;
    --accent: #ff8547;
    --accent-soft: #4a2c1f;
    --accent-ink: #1a0f08;
    --good: #5cbf7c;
    --bad: #ff6b63;
    --shadow: 0 1px 0 rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}

.bar-right { justify-self: end; display: flex; gap: 2px; }

.brand { text-align: center; }
.brand h1 { margin: 0; font-size: 1.5rem; letter-spacing: .02em; font-weight: 800; line-height: 1.1; }
.brand p { margin: 0; font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.icon-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 10px;
  background: transparent;
  display: grid; place-items: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--key); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; }

main {
  flex: 1;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.board { padding-top: 4vh; container-type: inline-size; }

.word {
  --n: 3;
  --gap: 10px;
  --tile: min(56px, calc((100cqi - (var(--n) + 1) * var(--gap)) / var(--n)));
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 72px;
  touch-action: manipulation;
  user-select: none;
  cursor: pointer;
}
.word > * { flex: none; }

.tile {
  width: var(--tile);
  height: calc(var(--tile) * 1.18);
  display: grid; place-items: center;
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: 8px;
  font-weight: 800;
  font-size: calc(var(--tile) * .56);
  text-transform: uppercase;
  box-shadow: var(--shadow);
}
.tile.pending { border-style: dashed; border-color: var(--accent); color: var(--accent); }
.tile.slot { background: var(--accent-soft); }
.tile.slot::after { content: ""; width: 45%; height: 3px; border-radius: 2px; background: var(--accent); transform: translateY(calc(var(--tile) * .3)); animation: blink 1.1s steps(1) infinite; }
.tile.new { border-color: var(--accent); background: var(--accent-soft); animation: pop .28s ease-out; }

.caret {
  width: var(--gap);
  height: calc(var(--tile) * 1.18 + 12px);
  border: 0; padding: 0; background: none;
  display: grid; place-items: center;
  cursor: pointer;
}
.caret::before {
  content: "";
  width: 3px; height: 45%;
  border-radius: 2px;
  background: var(--line);
  transition: height .15s, background .15s;
}
.caret:hover::before { background: var(--muted); }
.caret.active::before { height: 100%; background: var(--accent); animation: blink 1.1s steps(1) infinite; }
.caret:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
.done .caret::before, .done .caret.active::before { background: transparent; animation: none; }
.done .word { cursor: default; }

.message { min-height: 1.45em; margin: 10px 0 0; text-align: center; color: var(--muted); font-size: .92rem; }
.message.bad { color: var(--bad); }

.shake { animation: shake .36s; }

.chain {
  list-style: none; margin: 0; padding: 0;
  display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 4px;
  font-size: .9rem;
}
.chain li { color: var(--muted); }
.chain li + li::before { content: "→"; margin-right: 4px; color: var(--line); }
.chain li.dbl::before { content: "⇉"; color: var(--accent); }
.chain li:last-child { color: var(--ink); font-weight: 700; }

.btn {
  height: 44px; padding: 0 18px;
  border: 0; border-radius: 10px;
  background: var(--key); color: var(--key-ink);
  font-weight: 700; cursor: pointer;
}
.btn:hover { filter: brightness(.97); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }

.keyboard { display: flex; flex-direction: column; gap: 7px; margin-top: auto; }
.krow { display: flex; justify-content: center; gap: 5px; }
.key {
  flex: 1; max-width: 44px; height: 52px;
  border: 0; border-radius: 7px;
  background: var(--key); color: var(--key-ink);
  font-weight: 700; font-size: 1.05rem; text-transform: uppercase;
  cursor: pointer; touch-action: manipulation;
}
.key:active { background: var(--accent); color: var(--accent-ink); }
.krow.mid { padding: 0 5%; }

.actions { display: flex; justify-content: center; gap: 10px; }
.actions .btn { flex: 1; max-width: 240px; }
.btn-double.armed { background: var(--accent); color: var(--accent-ink); }
.btn-done.confirm { background: var(--accent); color: var(--accent-ink); }

.done .keyboard { opacity: .4; pointer-events: none; }

.modal {
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  border: 0; border-radius: 16px;
  padding: 22px 22px 20px;
  background: var(--surface); color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / .3);
}
.modal::backdrop { background: rgb(10 8 6 / .45); }
.modal h2 { margin: 0 0 8px; font-size: 1.35rem; outline: none; }
.modal h3 { margin: 18px 0 6px; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.modal p { margin: 8px 0; }
.close {
  position: absolute; top: 8px; right: 8px;
  width: 40px; height: 40px;
  border: 0; border-radius: 8px; background: none;
  font-size: 1.6rem; line-height: 1; color: var(--muted); cursor: pointer;
}
.close:hover { background: var(--key); }

.muted { color: var(--muted); font-size: .9rem; }
.center { text-align: center; }

.example { display: flex; flex-wrap: wrap; gap: 4px 6px; margin: 12px 0; font-weight: 700; }
.example span { background: var(--key); border-radius: 6px; padding: 2px 8px; }
.example b { color: var(--accent); }

.kicker { margin: 0 !important; color: var(--muted); font-size: .85rem; }
.score { margin: 4px 0 8px !important; font-size: 2.6rem; font-weight: 800; line-height: 1; }
.score .of { font-size: 1rem; font-weight: 600; color: var(--muted); margin-left: 6px; }
.squares { display: flex; gap: 4px; }
.squares i { width: 18px; height: 18px; border-radius: 4px; background: var(--good); }
.squares i.miss { background: var(--line); }
.insight { background: var(--key); border-radius: 10px; padding: 10px 12px; font-size: .95rem; }
.insight:empty { display: none; }
.path { font-weight: 600; line-height: 1.6; }
.path + .path { margin-top: 6px; }
.path .arrow { color: var(--muted); font-weight: 400; }
.path .arrow.dbl { color: var(--accent); }
.path .on, .legend .on { color: var(--good); }
.path .miss, .legend .miss { color: var(--bad); }
.legend { margin-top: 0 !important; font-size: .82rem; }
.end-actions { display: flex; gap: 10px; margin-top: 18px; }
.end-actions .btn { flex: 1; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; margin: 12px 0; }
.stat-row strong { display: block; font-size: 1.8rem; font-variant-numeric: tabular-nums; }
.stat-row span { font-size: .75rem; color: var(--muted); }
.dist { display: grid; grid-template-columns: auto 1fr; gap: 5px 8px; align-items: center; font-size: .85rem; }
.dist .bar { height: 22px; min-width: 24px; border-radius: 4px; background: var(--key); color: var(--ink); text-align: right; padding: 0 7px; font-weight: 700; line-height: 22px; }
.dist .bar.hot { background: var(--accent); color: var(--accent-ink); }

.toast {
  position: fixed; left: 50%; top: 72px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 9px 16px; border-radius: 10px; font-weight: 600; font-size: .92rem;
  box-shadow: var(--shadow); z-index: 10;
}

@keyframes pop { 0% { transform: scale(.6); } 70% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }
@keyframes blink { 50% { opacity: .35; } }

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

@media (max-height: 640px) {
  .key { height: 44px; }
  main { gap: 10px; }
}
