@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

/* ── Design tokens ────────────────────────────────────── */
:root {
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

  --bg:          #F7F8FA;
  --surface:     #FFFFFF;
  --surface-2:   #F3F4F6;
  --border:      #E5E7EB;
  --border-subtle: #F0F1F3;

  --text-1:      #111827;
  --text-2:      #374151;
  --text-3:      #6B7280;
  --text-4:      #9CA3AF;

  --accent:      #4F46E5;
  --accent-dark: #4338CA;
  --accent-bg:   #EEF2FF;

  --cell-word:   #DBEAFE;
  --cell-active: #3B82F6;
  --cell-blocked:#111827;
  --cell-correct:#D1FAE5;
  --cell-wrong:  #FEE2E2;
  --cell-hint:   #FEF3C7;

  --green:       #059669;
  --red:         #DC2626;

  --radius-sm:   6px;
  --radius-md:   10px;
  --radius-lg:   16px;

  --shadow-sm:   0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md:   0 4px 12px rgba(0,0,0,.08), 0 2px 4px rgba(0,0,0,.04);

  --cell-size:   40px;
}

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

/* ── Base ─────────────────────────────────────────────── */
html { font-size: 16px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-1);
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ───────────────────────────────────────────── */
.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

/* ── Header ───────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0 24px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 32px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-icon {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
  flex-shrink: 0;
}

.header h1 {
  font-size: 1.125rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-1);
}

.header p {
  font-size: 0.8125rem;
  color: var(--text-3);
  margin-top: 1px;
}

/* ── Theme input section ──────────────────────────────── */
.theme-input-section {
  max-width: 560px;
  margin: 0 auto 40px;
  text-align: center;
}

.theme-input-section > label {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text-1);
  margin-bottom: 6px;
}

.theme-input-section > .theme-subtitle {
  font-size: 0.9375rem;
  color: var(--text-3);
  margin-bottom: 20px;
}

.input-container {
  display: flex;
  gap: 10px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input-container:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,70,229,.12), var(--shadow-sm);
}

label[for="theme-input"] {
  display: none;
}

input[type="text"]#theme-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--font);
  font-size: 1rem;
  color: var(--text-1);
  outline: none;
  min-width: 0;
}

input[type="text"]#theme-input::placeholder {
  color: var(--text-4);
}

select#difficulty-select {
  border: none;
  background: transparent;
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--text-2);
  outline: none;
  cursor: pointer;
  padding-right: 4px;
  border-left: 1px solid var(--border-subtle);
  padding-left: 12px;
}

select#difficulty-select:focus {
  color: var(--text-1);
}

#generate-btn {
  flex-shrink: 0;
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}

#generate-btn:hover  { background: var(--accent-dark); }
#generate-btn:active { transform: scale(0.97); }

#generate-btn:disabled {
  background: var(--text-4);
  cursor: not-allowed;
  transform: none;
}

/* ── Loading ──────────────────────────────────────────── */
.loading-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
  color: var(--text-3);
  font-size: 0.9375rem;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Game container ───────────────────────────────────── */
.game-container {
  display: grid;
  grid-template-columns: 1fr 300px;
  grid-template-rows: auto auto;
  grid-template-areas:
    "grid  clues"
    "ctrl  clues";
  gap: 20px;
  align-items: start;
}

/* ── Crossword grid wrapper ───────────────────────────── */
.crossword-grid {
  grid-area: grid;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 320px;
}

/* ── Game controls ────────────────────────────────────── */
.game-controls {
  grid-area: ctrl;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: flex-start;
}

.game-controls button {
  padding: 8px 16px;
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  white-space: nowrap;
}

.game-controls button:hover {
  background: var(--surface-2);
  border-color: #D1D5DB;
}

#new-puzzle-btn {
  color: var(--accent);
  border-color: var(--accent);
}

#new-puzzle-btn:hover {
  background: var(--accent-bg);
  border-color: var(--accent-dark);
}

/* ── Clues panel ──────────────────────────────────────── */
.clues-panel {
  grid-area: clues;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  max-height: 600px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.clues-section {
  padding: 16px 16px 8px;
}

.clues-section + .clues-section {
  border-top: 1px solid var(--border-subtle);
}

.clues-section h3 {
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-4);
  margin-bottom: 6px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border-subtle);
}

.clues-list {
  display: flex;
  flex-direction: column;
}

.clue-item {
  display: flex;
  gap: 8px;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.8125rem;
  color: var(--text-2);
  line-height: 1.45;
  transition: background 0.1s;
  border-left: 2px solid transparent;
}

.clue-item:hover { background: var(--surface-2); }

.clue-item.active {
  background: var(--accent-bg);
  border-left-color: var(--accent);
  color: var(--text-1);
}

.clue-number {
  font-weight: 600;
  color: var(--text-3);
  flex-shrink: 0;
  min-width: 22px;
  font-variant-numeric: tabular-nums;
}

.clue-item.active .clue-number { color: var(--accent); }

/* ── Toasts ───────────────────────────────────────────── */
.error-message,
.success-message {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  opacity: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 18px;
  font-size: 0.9375rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 100;
  white-space: nowrap;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

.error-message[style*="block"],
.success-message[style*="block"] {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.error-message   { border-left: 3px solid var(--red);   color: var(--red); }
.success-message { border-left: 3px solid var(--green); color: var(--green); }

/* ── Accessibility ────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Update notification ──────────────────────────────── */
.update-notification {
  position: fixed;
  top: 20px; right: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  box-shadow: var(--shadow-md);
  z-index: 200;
  max-width: 280px;
  animation: slideIn 0.25s ease-out;
}

.update-notification p {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--text-1);
  margin-bottom: 12px;
}

.update-notification button {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface);
  color: var(--text-2);
  margin-right: 8px;
  transition: background 0.12s;
}

.update-notification button:first-of-type {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.update-notification button:first-of-type:hover { background: var(--accent-dark); }

@keyframes slideIn {
  from { transform: translateX(110%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

/* ── Timer display ────────────────────────────────────── */
.timer-display {
  display: flex;
  align-items: center;
  padding: 8px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.03em;
  min-width: 54px;
  justify-content: center;
  user-select: none;
}

/* ── Completion overlay ───────────────────────────────── */
.completion-overlay {
  position: fixed;
  inset: 0;
  background: rgba(17,24,39,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.completion-modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 44px 40px 40px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  max-width: 360px;
  width: 90%;
  animation: slideUp 0.3s cubic-bezier(0.34,1.56,0.64,1);
}

@keyframes slideUp {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.completion-icon {
  font-size: 2.5rem;
  color: var(--accent);
  margin-bottom: 12px;
  line-height: 1;
}

.completion-modal h2 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text-1);
  margin-bottom: 16px;
}

.completion-time-label {
  font-size: 0.8125rem;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  margin-bottom: 4px;
}

.completion-time {
  font-size: 2.25rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  margin-bottom: 28px;
}

.completion-new-btn {
  padding: 12px 28px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-family: var(--font);
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.completion-new-btn:hover  { background: var(--accent-dark); }
.completion-new-btn:active { transform: scale(0.97); }

/* ── Confetti ─────────────────────────────────────────── */
.confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 600;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  top: -16px;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  to {
    transform: translateY(105vh) rotate(720deg);
    opacity: 0;
  }
}

/* ── Reduced motion ───────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ── Print ────────────────────────────────────────────── */
@media print {
  .game-controls, .update-notification, .header p { display: none !important; }
  .game-container { grid-template-columns: 1fr; }
  .clues-panel { max-height: none; box-shadow: none; border: 1px solid #ccc; }
}
