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

:root {
  --bg: #FFF8F0;
  --text: #1A1A2E;
  --primary: #2D6A4F;
  --primary-light: #40916C;
  --accent: #E07A5F;
  --accent-light: #F4A261;
  --card-bg: #FFFFFF;
  --border: #D4A373;
  --shadow: rgba(0, 0, 0, 0.08);
  --font-en: 'Segoe UI', 'Arial', sans-serif;
  --font-ar: 'Segoe UI', 'Arial', sans-serif;
  --radius: 16px;
  --radius-sm: 10px;
}

html {
  font-size: 18px;
  scroll-behavior: smooth;
}

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

body.arabic {
  font-family: var(--font-ar);
  direction: rtl;
  text-align: right;
}

.container {
  max-width: 720px;
  margin: 0 auto;
}

header {
  text-align: center;
  padding: 1.5rem 1rem;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.2;
}

.subtitle {
  font-size: 1rem;
  color: #5A5A7A;
  margin-top: 0.5rem;
}

.lang-btn {
  background: var(--card-bg);
  border: 2px solid var(--primary);
  color: var(--primary);
  padding: 0.4rem 1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.2s;
}

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

.controls {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: 0 2px 12px var(--shadow);
  margin-bottom: 1.5rem;
}

.control-group {
  margin-bottom: 1.25rem;
}

.control-group:last-of-type {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 0.5rem;
}

select {
  width: 100%;
  padding: 0.8rem 1rem;
  font-size: 1.1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: white;
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.2s;
  appearance: auto;
}

select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(45, 106, 79, 0.15);
}

.difficulty-buttons {
  display: flex;
  gap: 0.75rem;
}

.diff-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.8rem 0.5rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: white;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 1rem;
}

.diff-btn:hover {
  border-color: var(--primary-light);
  background: #F0F7F4;
}

.diff-btn.active {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.diff-icon {
  font-size: 1.5rem;
}

.diff-text {
  font-weight: 600;
  font-size: 0.95rem;
}

.generate-btn {
  width: 100%;
  padding: 1rem;
  font-size: 1.2rem;
  font-weight: 700;
  color: white;
  background: var(--primary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.generate-btn:hover {
  background: var(--primary-light);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(45, 106, 79, 0.3);
}

.generate-btn:active {
  transform: translateY(0);
}

.generate-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-spinner {
  width: 1.2rem;
  height: 1.2rem;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

.hidden {
  display: none !important;
}

.puzzle-display {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 2rem 1.5rem;
  box-shadow: 0 2px 12px var(--shadow);
  margin-bottom: 1.5rem;
  animation: fadeIn 0.4s ease;
}

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

.puzzle-content {
  font-size: 1.3rem;
  line-height: 1.8;
  text-align: center;
}

.puzzle-content .puzzle-question {
  font-size: 1.4rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: var(--text);
}

.puzzle-content .puzzle-options {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-top: 1rem;
}

.puzzle-content .puzzle-option {
  padding: 0.8rem 1.2rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.2s;
  background: white;
}

.puzzle-content .puzzle-option:hover {
  border-color: var(--primary-light);
  background: #F0F7F4;
}

.puzzle-content .puzzle-option.selected {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.puzzle-content .puzzle-option.correct {
  border-color: #2D6A4F;
  background: #D8F3DC;
  color: #1B4332;
}

.puzzle-content .puzzle-option.wrong {
  border-color: #D62828;
  background: #FFE5E5;
  color: #9B2226;
}

.puzzle-content .word-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  margin: 1rem 0;
}

.puzzle-content .word-grid .letter-box {
  width: 2.5rem;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 700;
  background: #F0F7F4;
  border: 2px solid var(--border);
  border-radius: 6px;
  text-transform: uppercase;
}

.puzzle-content .hint-text {
  font-size: 1rem;
  color: #5A5A7A;
  font-style: italic;
  margin-top: 0.8rem;
}

.answer-input-wrapper {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1.25rem;
  align-items: center;
  justify-content: center;
}

.puzzle-input {
  flex: 1 1 200px;
  padding: 0.8rem 1rem;
  font-size: 1.15rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  background: white;
  color: var(--text);
  transition: border-color 0.2s;
  min-width: 140px;
}

.puzzle-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(45, 106, 79, 0.15);
}

.puzzle-input:disabled {
  background: #f5f5f5;
  opacity: 0.7;
}

.submit-btn {
  padding: 0.8rem 1.5rem;
  font-size: 1.1rem;
  font-weight: 600;
  color: white;
  background: var(--primary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.submit-btn:hover {
  background: var(--primary-light);
}

.submit-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.text-feedback {
  width: 100%;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 0.4rem 0;
  border-radius: var(--radius-sm);
  animation: fadeIn 0.2s ease;
}

.text-feedback.correct {
  color: #2D6A4F;
  background: #D8F3DC;
}

.text-feedback.wrong {
  color: #9B2226;
  background: #FFE5E5;
}

.ws-grid-wrapper {
  display: flex;
  justify-content: center;
  margin: 1rem 0;
  overflow-x: auto;
}

.ws-grid {
  display: grid;
  gap: 2px;
  background: var(--border);
  border: 2px solid var(--border);
  border-radius: 6px;
  max-width: 100%;
}

.ws-cell {
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 700;
  background: white;
  text-transform: uppercase;
  user-select: none;
}

.ws-cell.selected {
  background: #FFE5D9;
  color: #9B2226;
  font-weight: 700;
  cursor: pointer;
}

.ws-cell.highlighted {
  background: #D8F3DC;
  color: #1B4332;
  font-weight: 800;
}

.ws-cell {
  cursor: pointer;
}

.ws-word-list {
  margin-top: 1rem;
  text-align: center;
}

.ws-word-list .puzzle-question {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.ws-words {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.ws-word {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  font-size: 1.05rem;
  font-weight: 600;
  background: #F0F7F4;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  transition: all 0.3s;
}

.ws-word.found {
  background: #D8F3DC;
  border-color: #2D6A4F;
  color: #1B4332;
  text-decoration: line-through;
  opacity: 0.7;
}

.answer-section {
  margin-top: 1rem;
  animation: fadeIn 0.3s ease;
}

.answer-divider {
  height: 2px;
  background: var(--border);
  margin: 1rem 0;
}

.answer-content {
  font-size: 1.3rem;
  text-align: center;
  padding: 1rem;
  background: #F0F7F4;
  border-radius: var(--radius-sm);
}

.answer-content .answer-label {
  font-weight: 600;
  color: var(--primary);
}

.answer-content .answer-text {
  font-weight: 700;
  font-size: 1.5rem;
  color: var(--primary);
  margin-top: 0.3rem;
}

.answer-content .answer-explanation {
  font-size: 1rem;
  color: #5A5A7A;
  margin-top: 0.5rem;
  font-style: italic;
}

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

.action-btn {
  flex: 1;
  padding: 0.8rem;
  font-size: 1.1rem;
  font-weight: 600;
  border: 2px solid var(--primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  background: var(--primary);
  color: white;
}

.action-btn:hover {
  background: var(--primary-light);
  border-color: var(--primary-light);
}

.action-btn.secondary {
  background: white;
  color: var(--primary);
}

.action-btn.secondary:hover {
  background: #F0F7F4;
}

.info-section {
  margin-bottom: 1.5rem;
}

.info-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: 0 2px 12px var(--shadow);
  margin-bottom: 1rem;
}

.info-card h3 {
  font-size: 1.3rem;
  color: var(--primary);
  margin-bottom: 0.5rem;
}

.info-card p {
  font-size: 1rem;
  color: #3A3A5A;
  line-height: 1.7;
}

.info-cards-row {
  display: flex;
  gap: 0.75rem;
}

.info-card.small {
  flex: 1;
  padding: 1rem;
  margin-bottom: 0;
}

.info-card.small h4 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 0.4rem;
}

.info-card.small p {
  font-size: 0.85rem;
}

footer {
  text-align: center;
  padding: 1.5rem;
  color: #5A5A7A;
  font-size: 0.9rem;
}

.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: #D62828;
  color: white;
  padding: 1rem 2rem;
  border-radius: var(--radius-sm);
  font-size: 1.1rem;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(214, 40, 40, 0.3);
  z-index: 1000;
  animation: fadeIn 0.3s ease;
  max-width: 90%;
  text-align: center;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 248, 240, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 999;
  animation: fadeIn 0.2s ease;
}

.loading-spinner {
  width: 3rem;
  height: 3rem;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 1rem;
}

#loadingText {
  font-size: 1.2rem;
  color: var(--primary);
  font-weight: 600;
}

/* responsive */
@media (max-width: 600px) {
  html { font-size: 16px; }

  .container { padding: 0; }

  h1 { font-size: 1.6rem; }

  .puzzle-content { font-size: 1.1rem; }

  .difficulty-buttons { flex-direction: column; }

  .info-cards-row { flex-direction: column; }

  .info-card.small { margin-bottom: 0.5rem; }

  .puzzle-actions { flex-direction: column; }

  .puzzle-content .word-grid .letter-box {
    width: 2rem;
    height: 2rem;
    font-size: 1.2rem;
  }

  .ws-cell {
    width: 1.5rem;
    height: 1.5rem;
    font-size: 0.8rem;
  }

  .answer-input-wrapper {
    flex-direction: column;
  }

  .puzzle-input {
    flex: 1 1 auto;
    width: 100%;
  }

  .submit-btn {
    width: 100%;
  }
}

/* print */
@media print {
  .controls, .puzzle-actions, .lang-btn, footer { display: none; }
  .puzzle-display { box-shadow: none; border: 1px solid #ccc; }
}
