:root {
  --bg: #fffdf7;
  --fg: #1f1b16;
  --muted: #6b6258;
  --tile: #f0e9db;
  --tile-selected: #5a4a3a;
  --c0: #f9df6d;
  --c1: #a0c35a;
  --c2: #b0c4ef;
  --c3: #ba81c5;
  --accent: #d9622b;
  --gap: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a17;
    --fg: #f3ece0;
    --muted: #a99e8f;
    --tile: #3a342c;
    --tile-selected: #e8dcc5;
  }
  .tile.selected { color: #1c1a17; }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

header {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid color-mix(in srgb, var(--fg) 15%, transparent);
}

h1 { font-size: 1.4rem; margin: 0; }

.header-buttons { display: flex; gap: 8px; }

.icon-btn {
  background: none;
  color: var(--fg);
  border: 1px solid color-mix(in srgb, var(--fg) 30%, transparent);
  border-radius: 999px;
  padding: 6px 14px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}

main {
  width: 100%;
  max-width: 560px;
  padding: 12px 16px 32px;
  position: relative;
}

.subtitle { text-align: center; margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; }
.instructions { text-align: center; margin: 4px 0 12px; font-weight: 600; }

#toast {
  position: absolute;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 10;
  white-space: nowrap;
}
#toast.show { opacity: 1; }

#board { display: flex; flex-direction: column; gap: var(--gap); }

.solved {
  border-radius: 8px;
  padding: 12px 8px;
  text-align: center;
  min-height: 72px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: #1f1b16;
  animation: reveal 0.45s ease-out;
}
.solved .title { font-weight: 800; text-transform: uppercase; font-size: 0.95rem; }
.solved .words { font-size: 0.9rem; margin-top: 2px; }
.c0 { background: var(--c0); }
.c1 { background: var(--c1); }
.c2 { background: var(--c2); }
.c3 { background: var(--c3); }

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.tile {
  background: var(--tile);
  color: var(--fg);
  border: 0;
  border-radius: 8px;
  min-height: 72px;
  padding: 4px;
  font: inherit;
  font-weight: 800;
  font-size: clamp(0.62rem, 2.6vw, 0.95rem);
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  hyphens: auto;
  overflow-wrap: anywhere;
  transition: background 0.12s, color 0.12s, transform 0.12s;
}
.tile:hover { filter: brightness(0.96); }
.tile:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.tile.selected { background: var(--tile-selected); color: #fffdf7; transform: scale(0.96); }
.tile.shake { animation: shake 0.5s; }
.tile.hop { animation: hop 0.45s; }

.mistakes {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 16px 0;
  color: var(--muted);
}
#mistakes { display: inline-flex; gap: 6px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: var(--tile-selected); transition: transform 0.3s, opacity 0.3s; }
.dot.lost { transform: scale(0); opacity: 0; }

.controls { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.pill {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--fg);
  border-radius: 999px;
  padding: 12px 20px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.pill:disabled { opacity: 0.35; cursor: not-allowed; }
.pill.primary { background: var(--fg); color: var(--bg); }

dialog {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid color-mix(in srgb, var(--fg) 25%, transparent);
  border-radius: 12px;
  max-width: min(440px, 92vw);
  padding: 20px 24px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h2 { margin-top: 0; }
.dialog-buttons { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 16px; }
.muted { color: var(--muted); font-size: 0.9rem; }

#result-grid { font-size: 1.4rem; line-height: 1.25; text-align: center; margin: 12px 0; }
.stats { display: flex; justify-content: space-around; text-align: center; }
.stats b { display: block; font-size: 1.6rem; }
.stats span { font-size: 0.75rem; color: var(--muted); }

#puzzle-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; max-height: 50vh; overflow: auto; }
#puzzle-list button {
  background: var(--tile);
  color: var(--fg);
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 10px 0;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}
#puzzle-list button.today { border-color: var(--accent); }
#puzzle-list button.won::after { content: " ✓"; }
#puzzle-list button.lost::after { content: " ✗"; }

.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.swatch { color: #1f1b16; padding: 8px; border-radius: 6px; text-align: center; font-weight: 700; font-size: 0.85rem; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@keyframes hop {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-10px); }
}
@keyframes reveal {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: scale(1); }
}

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