:root {
  --bg: #f6efe4;
  --bg2: #efe3d1;
  --glow: rgba(233, 170, 110, 0.28);
  --ink: #3b2f28;
  --ink-soft: #7a6a5c;
  --card: #fffaf2;
  --line: #e2d4c0;
  --tile-empty: #d9cbb8;
  --tile-filled: #9c8b7a;
  --tile-bg: #fffaf3;
  --key: #e8dccb;
  --key-ink: #3b2f28;
  --correct: #6f9a5b;
  --present: #d9a441;
  --absent: #918478;
  --accent: #c8643b;
  --confetti: #e8b4a0;
  --on-state: #fffaf2;
  --shadow: 0 10px 30px rgba(80, 50, 30, 0.14);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, ui-serif, serif;
  --sans: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --tile: 62px;
  --key-h: 56px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #1f1a17;
  --bg2: #171310;
  --glow: rgba(200, 100, 59, 0.16);
  --ink: #f3e9dc;
  --ink-soft: #b3a392;
  --card: #2a231f;
  --line: #3f362f;
  --tile-empty: #3f362f;
  --tile-filled: #7a6a5c;
  --tile-bg: #241e1a;
  --key: #3d342e;
  --key-ink: #f3e9dc;
  --correct: #6a9a5a;
  --present: #c99a3c;
  --absent: #4d443d;
  --accent: #e07b4c;
  --confetti: #f1c7a8;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

:root[data-cb="1"] {
  --correct: #e07b39;
  --present: #5aa3d8;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background:
    radial-gradient(ellipse 80% 55% at 50% -5%, var(--glow), transparent 70%),
    linear-gradient(180deg, var(--bg), var(--bg2));
  background-color: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

/* ---------- header ---------- */
.top {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px 6px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.top .grow { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.brand { font-family: var(--serif); font-weight: 700; font-size: 24px; letter-spacing: 0.02em; line-height: 1.05; }
.brand em { font-style: italic; color: var(--accent); }
.mode { font-size: 12px; color: var(--ink-soft); letter-spacing: 0.08em; text-transform: uppercase; display: flex; gap: 8px; }
.mode .timer { font-variant-numeric: tabular-nums; }
.mode .hard { color: var(--accent); font-weight: 700; }
.icon {
  width: 38px; height: 38px; border: 0; border-radius: 10px; background: transparent;
  display: grid; place-items: center; color: var(--ink); flex: none;
  transition: background 0.15s;
}
.icon:hover { background: color-mix(in srgb, var(--key) 70%, transparent); }
.icon svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- board ---------- */
#board-wrap { flex: 1; display: grid; place-items: center; width: 100%; min-height: 0; padding: 8px 0; }
#board { display: grid; grid-template-rows: repeat(6, var(--tile)); gap: calc(var(--tile) * 0.09); }
.row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: calc(var(--tile) * 0.09); }
.row.shake { animation: shake 0.5s; }
.tile {
  width: var(--tile); height: var(--tile);
  border: 2px solid var(--tile-empty);
  border-radius: calc(var(--tile) * 0.14);
  background: var(--tile-bg);
  display: grid; place-items: center;
  font-weight: 800; font-size: calc(var(--tile) * 0.52);
  text-transform: uppercase; line-height: 1;
  color: var(--ink);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.03);
}
.tile.filled { border-color: var(--tile-filled); animation: pop 0.12s ease-out; }
.tile[data-state] {
  border-color: transparent; color: var(--on-state);
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12), 0 4px 10px rgba(60, 35, 20, 0.12);
}
.tile[data-state="correct"] { background: linear-gradient(160deg, color-mix(in srgb, var(--correct) 82%, #fff), var(--correct)); }
.tile[data-state="present"] { background: linear-gradient(160deg, color-mix(in srgb, var(--present) 82%, #fff), var(--present)); }
.tile[data-state="absent"] { background: var(--absent); }
.tile.flip { animation: flip 0.5s ease-in-out both; }
.tile.bounce { animation: bounce 0.6s cubic-bezier(.3,1.6,.5,1) both; }
.tile.hint { outline: 2px dashed var(--accent); outline-offset: 2px; }

@keyframes pop { 0% { transform: scale(0.85); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(-90deg); } 100% { transform: rotateX(0); } }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-38%); } 70% { transform: translateY(6%); } }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); } 20%, 80% { transform: translateX(5px); }
  30%, 50%, 70% { transform: translateX(-8px); } 40%, 60% { transform: translateX(8px); }
}

/* ---------- keyboard ---------- */
#keyboard { width: 100%; padding: 0 8px 10px; flex: none; display: flex; flex-direction: column; gap: 7px; }
.krow { display: flex; gap: 6px; justify-content: center; }
.key {
  flex: 1; max-width: 46px; height: var(--key-h);
  border: 0; border-radius: 9px;
  background: var(--key); color: var(--key-ink);
  font-weight: 700; font-size: 17px; text-transform: uppercase;
  display: grid; place-items: center;
  box-shadow: 0 2px 0 rgba(60, 35, 20, 0.15);
  transition: background 0.25s, transform 0.06s, color 0.25s;
  touch-action: manipulation;
}
.key.wide { flex: 1.6; max-width: 72px; font-size: 12px; letter-spacing: 0.04em; }
.key svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.key.down, .key:active { transform: translateY(2px); box-shadow: none; }
.key[data-state] { color: var(--on-state); }
.key[data-state="correct"] { background: var(--correct); }
.key[data-state="present"] { background: var(--present); }
.key[data-state="absent"] { background: var(--absent); opacity: 0.85; }
.krow .spacer { flex: 0.5; max-width: 22px; }

/* ---------- overlays ---------- */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 30; }
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  transition: opacity 0.25s;
}
.overlay.hidden { opacity: 0; pointer-events: none; }
.overlay.solid { background: linear-gradient(180deg, var(--bg), var(--bg2)); }
.card {
  position: relative;
  width: min(440px, 100%); max-height: calc(100vh - 32px); overflow: auto;
  background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); padding: 24px 22px;
  animation: rise 0.3s cubic-bezier(.2,.9,.3,1.2);
}
.overlay.solid .card { width: min(500px, 100%); background: transparent; border: 0; box-shadow: none; text-align: center; }
@keyframes rise { from { transform: translateY(16px) scale(0.98); opacity: 0; } }
.card h2 { font-family: var(--serif); margin: 0 0 12px; font-size: 26px; }
.card h3 { margin: 18px 0 8px; font-size: 13px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
.card p { margin: 8px 0; line-height: 1.45; }
.card .close { position: absolute; top: 12px; right: 12px; }
.muted { color: var(--ink-soft); font-size: 14px; }

.logo { display: flex; justify-content: center; gap: 5px; margin: 4px 0 10px; }
.logo .tile { --tile: clamp(28px, calc((100vw - 100px) / 8), 50px); animation: flip 0.6s ease-in-out both; }
.tagline { font-family: var(--serif); font-style: italic; font-size: 18px; color: var(--ink-soft); margin: 4px 0 20px; }
.btns { display: flex; flex-direction: column; gap: 10px; align-items: stretch; width: min(320px, 100%); margin: 0 auto; }
.btn {
  border: 0; border-radius: 14px; padding: 14px 18px;
  font-weight: 700; font-size: 17px;
  background: var(--key); color: var(--key-ink);
  box-shadow: 0 3px 0 rgba(60, 35, 20, 0.15);
  transition: transform 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.04); }
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn.primary { background: linear-gradient(160deg, color-mix(in srgb, var(--accent) 80%, #fff), var(--accent)); color: #fffaf2; }
.btn.good { background: linear-gradient(160deg, color-mix(in srgb, var(--correct) 80%, #fff), var(--correct)); color: #fffaf2; }
.btn small { display: block; font-weight: 500; font-size: 12px; opacity: 0.85; margin-top: 2px; }
.btn-row { display: flex; gap: 10px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; min-width: 120px; padding: 12px; font-size: 15px; }
.link-row { display: flex; gap: 6px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.link-row .icon { width: auto; padding: 0 12px; gap: 6px; display: flex; font-size: 14px; font-weight: 600; }
.controls { margin-top: 18px; font-size: 13px; color: var(--ink-soft); line-height: 1.6; }
kbd { font-family: var(--sans); font-size: 12px; background: var(--key); border-radius: 5px; padding: 1px 6px; box-shadow: 0 1px 0 rgba(0,0,0,0.15); }

.example { display: flex; gap: 5px; margin: 8px 0 4px; }
.example .tile { --tile: 38px; }

.stats-nums { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; gap: 4px; }
.stats-nums b { display: block; font-size: 30px; font-family: var(--serif); }
.stats-nums span { font-size: 11px; color: var(--ink-soft); line-height: 1.2; display: block; }
.dist { display: flex; flex-direction: column; gap: 5px; }
.dist .bar-row { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 700; }
.dist .bar {
  background: var(--absent); color: #fffaf2; border-radius: 5px; padding: 3px 8px;
  min-width: 26px; text-align: right; font-size: 13px;
  transition: width 0.6s cubic-bezier(.2,.9,.3,1);
}
.dist .bar.hi { background: var(--correct); }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; background: var(--key); padding: 4px; border-radius: 12px; }
.tabs button { flex: 1; border: 0; border-radius: 9px; padding: 8px; background: transparent; font-weight: 700; }
.tabs button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }

.result-head { text-align: center; }
.result-head .big { font-family: var(--serif); font-size: 34px; margin: 0; }
.answer-word { display: flex; gap: 5px; justify-content: center; margin: 12px 0; }
.answer-word .tile { --tile: 44px; }
.countdown { text-align: center; margin-top: 14px; }
.countdown b { font-size: 26px; font-variant-numeric: tabular-nums; font-family: var(--serif); }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: 0; }
.setting .lbl b { display: block; }
.setting .lbl span { font-size: 13px; color: var(--ink-soft); }
.switch { width: 50px; height: 30px; border-radius: 99px; border: 0; background: var(--tile-empty); position: relative; flex: none; transition: background 0.2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.25); transition: transform 0.2s; }
.switch.on { background: var(--correct); }
.switch.on::after { transform: translateX(20px); }
.switch:disabled { opacity: 0.45; cursor: not-allowed; }
.seg { display: flex; background: var(--key); border-radius: 10px; padding: 3px; flex: none; }
.seg button { border: 0; background: transparent; border-radius: 8px; padding: 6px 10px; font-size: 13px; font-weight: 700; }
.seg button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }

/* ---------- toasts ---------- */
#toasts { position: fixed; top: 64px; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 40; pointer-events: none; }
.toast {
  background: var(--ink); color: var(--bg); font-weight: 700; padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); animation: toast-in 0.2s ease-out; transition: opacity 0.3s, transform 0.3s;
}
.toast.out { opacity: 0; transform: translateY(-6px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px) scale(0.96); } }

@media (prefers-reduced-motion: reduce) {
  .tile.flip, .tile.bounce, .row.shake, .tile.filled, .card, .logo .tile { animation: none !important; }
  .row.shake .tile { outline: 2px solid var(--accent); }
}

:root[data-theme="dark"] .key[data-state="absent"] { background: #2a2420; color: #85776b; opacity: 1; }
