:root {
  --bg: #f4efe6;
  --surface: #fffaf2;
  --ink: #1f2a37;
  --muted: #5d6673;
  --line: #d6cdbd;
  --line-strong: #8f8676;
  --key: #e3dccf;
  --key-ink: #1f2a37;
  --s0: #7d8590;   /* not in the word */
  --s1: #d27a24;   /* elsewhere in the word */
  --s2: #1f7a72;   /* right place */
  --on-s: #ffffff;
  --shadow: 0 10px 40px rgba(31, 42, 55, 0.18);
  color-scheme: light;
}
:root.contrast { --s1: #e8890c; --s2: #2459c7; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141a22;
    --surface: #1c2430;
    --ink: #ece6dc;
    --muted: #9aa3ae;
    --line: #36404d;
    --line-strong: #6b7584;
    --key: #566170;
    --key-ink: #ece6dc;
    --s0: #2a313b;
    --s1: #c06e22;
    --s2: #23897f;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
  :root.contrast:not([data-theme="light"]) { --s1: #e8890c; --s2: #3b6fe0; }
}

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

/* header */
.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
}
.bar .right { justify-self: end; display: flex; gap: 4px; }
.mark {
  margin: 0;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.mark::first-letter { color: var(--s2); }
.icon {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: none; border: 0; border-radius: 10px; cursor: pointer;
}
.icon:hover { background: var(--key); }
.icon svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon svg .dot { fill: currentColor; }

/* board */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  position: relative;
  min-height: 0;
}
.puzzle-no { margin: 0 0 10px; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  width: min(330px, 100%, 52dvh);
  aspect-ratio: 5 / 6;
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tile {
  display: grid; place-items: center;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-size: clamp(22px, 7vw, 32px);
  font-weight: 750;
  text-transform: uppercase;
  user-select: none;
  aspect-ratio: 1;
}
.tile.filled { border-color: var(--line-strong); animation: pop 0.1s ease-out; }
.tile.s0, .tile.s1, .tile.s2 { color: var(--on-s); border-color: transparent; }
.tile.s0 { background: var(--s0); }
.tile.s1 { background: var(--s1); }
.tile.s2 { background: var(--s2); }
.tile.reveal { animation: drop 0.42s ease-out both; }
.row.shake { animation: shake 0.4s; }
.row.win .tile { animation: hop 0.5s ease-out both; }

@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes drop {
  0% { transform: translateY(-14px); opacity: 0.2; }
  60% { transform: translateY(3px); opacity: 1; }
  100% { transform: none; }
}
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@keyframes hop { 40% { transform: translateY(-16px); } 70% { transform: translateY(2px); } }
@media (prefers-reduced-motion: reduce) {
  .tile, .row { animation: none !important; }
}

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

/* keyboard */
.keyboard {
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px calc(10px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 7px;
  user-select: none;
}
.krow { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1;
  height: 54px;
  min-width: 0;
  border: 0; border-radius: 8px;
  background: var(--key); color: var(--key-ink);
  font-weight: 700; font-size: 15px; text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
}
.key.wide { flex: 1.6; font-size: 12px; letter-spacing: 0.04em; }
.key svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; vertical-align: middle; }
.krow .gap { flex: 0.5; }
.key.s0, .key.s1, .key.s2 { color: var(--on-s); }
.key.s0 { background: var(--s0); }
.key.s1 { background: var(--s1); }
.key.s2 { background: var(--s2); }
.key:active { filter: brightness(0.92); }

/* dialogs */
dialog {
  border: 0; border-radius: 16px;
  background: var(--surface); color: var(--ink);
  padding: 22px 22px 20px;
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(20, 26, 34, 0.45); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog h3 { margin: 18px 0 8px; font-size: 15px; }
dialog p { margin: 0 0 10px; line-height: 1.45; }
.close {
  position: absolute; top: 10px; right: 12px;
  width: 36px; height: 36px; border: 0; border-radius: 8px;
  background: none; font-size: 26px; line-height: 1; cursor: pointer;
}
.close:focus:not(:focus-visible) { outline: none; }
.close:hover { background: var(--key); }
.example { display: grid; grid-template-columns: repeat(5, 40px); gap: 5px; margin: 6px 0 6px; }
.example .tile { font-size: 20px; border-radius: 8px; }
.k0 { color: var(--s0); } .k1 { color: var(--s1); } .k2 { color: var(--s2); }
.small, small { color: var(--muted); font-size: 13px; }

.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; gap: 4px; }
.stat-row b { display: block; font-size: 28px; font-variant-numeric: tabular-nums; }
.stat-row span { font-size: 12px; color: var(--muted); }
.dist { display: grid; gap: 4px; }
.dist .d { display: grid; grid-template-columns: 14px 1fr; gap: 6px; align-items: center; font-size: 14px; font-variant-numeric: tabular-nums; }
.dist .bar-fill {
  background: var(--s0); color: var(--on-s);
  min-width: 26px; padding: 2px 7px; border-radius: 5px;
  text-align: right; font-weight: 700;
}
.dist .bar-fill.today { background: var(--s2); }
.after { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.next span { display: block; font-size: 13px; color: var(--muted); }
.next strong { font-size: 24px; font-variant-numeric: tabular-nums; }
.share {
  border: 0; border-radius: 10px;
  background: var(--s2); color: var(--on-s);
  padding: 12px 26px; font-weight: 700; font-size: 16px; cursor: pointer;
}

.setting { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.setting input { width: 22px; height: 22px; accent-color: var(--s2); flex: none; }
.setting + p { margin-top: 16px; }

.noscript { text-align: center; padding: 20px; }

@media (max-height: 640px) {
  .key { height: 46px; }
  .mark { font-size: 24px; }
}
