:root {
  --bg: #ffffff;
  --text: #121212;
  --muted: #5a5a5a;
  --border: #e0e0e0;
  --tile: #efefe6;
  --tile-selected: #5a594e;
  --tile-text-selected: #ffffff;
  --yellow: #f9df6d;
  --green: #a0c35a;
  --blue: #b0c4ef;
  --purple: #ba81c5;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --radius: 8px;
  --font: "Libre Franklin", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1.5rem 1rem 3rem;
}

.app {
  width: 100%;
  max-width: 620px;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

header {
  text-align: center;
}

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--muted);
  font-size: 0.95rem;
  margin-top: 0.25rem;
}

/* Solved groups */
.solved-groups {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.solved-group {
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  text-align: center;
  color: #121212;
  animation: pop-in 0.35s ease;
}

.solved-group.yellow { background: var(--yellow); }
.solved-group.green  { background: var(--green); }
.solved-group.blue   { background: var(--blue); }
.solved-group.purple { background: var(--purple); }

.solved-group .title {
  font-weight: 700;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.solved-group .words {
  font-size: 0.9rem;
  margin-top: 0.2rem;
  opacity: 0.9;
}

/* Board */
.board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  min-height: 280px;
}

.tile {
  background: var(--tile);
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 1.1rem 0.4rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.1s;
  word-break: break-word;
  line-height: 1.2;
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  user-select: none;
}

.tile:hover:not(.selected):not(:disabled) {
  background: #e4e4d9;
}

.tile.selected {
  background: var(--tile-selected);
  color: var(--tile-text-selected);
}

.tile:active:not(:disabled) {
  transform: scale(0.97);
}

.tile:disabled {
  opacity: 0;
  pointer-events: none;
}

/* Status */
.status {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  min-height: 2.5rem;
}

.mistakes {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.mistake-dots {
  display: flex;
  gap: 0.35rem;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #5a594e;
  transition: background 0.2s, opacity 0.2s;
}

.dot.used {
  background: transparent;
  border: 2px solid #ccc;
}

.message {
  font-weight: 600;
  font-size: 0.95rem;
  min-height: 1.3em;
  text-align: center;
}

.message.wrong { color: #c0392b; }
.message.one-away { color: #d35400; }
.message.correct { color: #27ae60; }

/* Controls */
.controls {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 999px;
  padding: 0.65rem 1.4rem;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn.primary {
  background: #121212;
  color: #fff;
}

.btn.primary:hover:not(:disabled) {
  background: #333;
}

.btn.primary:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.btn.secondary {
  background: transparent;
  border-color: #121212;
  color: #121212;
}

.btn.secondary:hover {
  background: #f5f5f0;
}

.btn.link {
  background: none;
  border: none;
  color: var(--muted);
  text-decoration: underline;
  padding: 0.4rem;
  font-weight: 500;
}

.btn.link:hover {
  color: var(--text);
}

/* End screen */
.end-screen {
  text-align: center;
  padding: 1.5rem;
  background: #f8f8f4;
  border-radius: var(--radius);
  animation: pop-in 0.4s ease;
}

.end-screen h2 {
  font-size: 1.5rem;
  margin-bottom: 0.4rem;
}

.end-screen p {
  color: var(--muted);
  margin-bottom: 1.2rem;
}

.hidden {
  display: none !important;
}

/* Footer */
footer {
  text-align: center;
  margin-top: 0.5rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.4rem;
}

/* Modal / Editor */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}

.modal-content {
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-content h2 {
  margin-bottom: 0.5rem;
}

.editor-help {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
  line-height: 1.4;
}

.editor-help code {
  background: #f0f0e8;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

#puzzle-json {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  line-height: 1.4;
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.error {
  color: #c0392b;
  font-size: 0.85rem;
  margin-top: 0.6rem;
  min-height: 1.2em;
}

/* Animations */
@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.shake {
  animation: shake 0.4s ease;
}

/* Responsive */
@media (max-width: 480px) {
  .container { padding: 1rem 0.5rem 2rem; gap: 0.9rem; }
  .board { gap: 0.4rem; }
  .tile {
    font-size: 0.75rem;
    padding: 0.5rem 0.2rem;
    min-height: 60px;
    letter-spacing: 0;
  }
  .btn {
    padding: 0.55rem 1rem;
    font-size: 0.85rem;
  }
}

@media (max-width: 360px) {
  .board { gap: 0.3rem; }
  .tile {
    font-size: 0.68rem;
    padding: 0.4rem 0.15rem;
    min-height: 54px;
  }
}
