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

:root {
  --color-bg: #f8f9fa;
  --color-surface: #ffffff;
  --color-text: #212529;
  --color-muted: #6c757d;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-error: #dc2626;
  --color-border: #dee2e6;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Courier New", Courier, monospace;
  --max-width: 640px;
}

body {
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.6;
  min-height: 100vh;
}

.container {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 2rem 1rem;
}

h1 {
  text-align: center;
  font-size: 1.75rem;
  margin-bottom: 2rem;
}

/* --- Form --- */

.puzzle-form {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 1.5rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.form-group .optional,
.form-group .hint {
  font-weight: 400;
  color: var(--color-muted);
  font-size: 0.875rem;
}

.form-group input[type="text"],
.form-group textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-family: var(--font-sans);
  font-size: 1rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: var(--color-bg);
  color: var(--color-text);
}

.form-group textarea {
  resize: vertical;
  font-family: var(--font-mono);
}

.form-group input:focus,
.form-group textarea:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: -1px;
  border-color: var(--color-primary);
}

.btn-generate {
  display: block;
  width: 100%;
  padding: 0.75rem;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  background: var(--color-primary);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn-generate:hover {
  background: var(--color-primary-hover);
}

/* --- Error --- */

.error-message {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  color: var(--color-error);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 4px;
  font-size: 0.875rem;
}

/* --- Puzzle output --- */

.puzzle-output {
  margin-top: 2rem;
}

.puzzle-title {
  text-align: center;
  font-size: 1.35rem;
  margin-bottom: 1rem;
}

.puzzle-grid {
  border-collapse: collapse;
  margin: 0 auto;
}

.puzzle-cell {
  width: 2rem;
  height: 2rem;
  text-align: center;
  vertical-align: middle;
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 600;
  line-height: 2rem;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  user-select: none;
}

.word-bank {
  margin-top: 1.5rem;
  text-align: center;
}

.word-bank h3 {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.word-bank ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem 1rem;
}

.word-bank li {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  padding: 0.25rem 0.5rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 4px;
}

/* --- Share button --- */

.btn-share {
  padding: 0.5rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-primary);
  background: var(--color-surface);
  border: 2px solid var(--color-primary);
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.btn-share:hover {
  background: var(--color-primary);
  color: #fff;
}

.puzzle-actions {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

/* --- Solve mode --- */

.puzzle-cell {
  cursor: pointer;
  transition: background 0.1s ease;
}

.puzzle-cell:hover {
  background: #e2e8f0;
}

.puzzle-cell:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
}

.puzzle-cell.selected {
  background: #bfdbfe;
}

.puzzle-cell.found {
  background: #bbf7d0;
}

.puzzle-cell.found:hover {
  background: #86efac;
}

.puzzle-cell.solution {
  background: #fef08a;
}

.word-bank li.word-found {
  text-decoration: line-through;
  color: var(--color-muted);
  background: transparent;
  border-color: transparent;
}

.solve-progress {
  text-align: center;
  margin-top: 1rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-muted);
}

/* --- Congrats banner --- */

.congrats-banner {
  text-align: center;
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  font-size: 1.1rem;
  font-weight: 700;
  color: #166534;
  background: #dcfce7;
  border: 1px solid #bbf7d0;
  border-radius: 6px;
}

/* --- Responsive --- */

@media (max-width: 480px) {
  .container {
    padding: 1rem 0.5rem;
  }

  h1 {
    font-size: 1.35rem;
    margin-bottom: 1.25rem;
  }

  .puzzle-form {
    padding: 1rem;
  }

  .grid-scroll-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .puzzle-cell {
    width: 1.6rem;
    height: 1.6rem;
    font-size: 0.8rem;
    line-height: 1.6rem;
  }

  .puzzle-actions {
    flex-wrap: wrap;
  }

  .btn-share {
    font-size: 0.85rem;
    padding: 0.4rem 0.75rem;
  }
}

/* --- Print --- */

@media print {
  /* Hide everything except the puzzle output */
  body {
    background: #fff;
  }

  .puzzle-form,
  .error-message,
  .puzzle-actions,
  .solve-progress,
  .congrats-banner,
  h1 {
    display: none !important;
  }

  .container {
    max-width: 100%;
    padding: 0;
    margin: 0;
  }

  .puzzle-output {
    margin-top: 0;
  }

  .puzzle-title {
    font-size: 1.5rem;
    margin-bottom: 0.75rem;
  }

  .puzzle-grid {
    border-collapse: collapse;
    margin: 0 auto;
    page-break-inside: avoid;
  }

  .puzzle-cell {
    width: 1.6rem;
    height: 1.6rem;
    font-size: 0.85rem;
    line-height: 1.6rem;
    border: 1px solid #000;
    background: #fff;
  }

  .puzzle-cell.solution {
    background: #fff;
  }

  .puzzle-cell.found {
    background: #fff;
  }

  .word-bank {
    margin-top: 1rem;
    page-break-inside: avoid;
  }

  .word-bank li {
    border: none;
    background: none;
    padding: 0.15rem 0.4rem;
    font-size: 0.85rem;
  }
}
