:root {
  --color-primary:   #9900FF;
  --color-accent-1:  #429DFF;
  --color-accent-2:  #F3A200;
  --color-accent-3:  #ACFF00;
  --color-accent-4:  #ED00A6;
  --color-error:     #E74C3C;
  --color-dark:      #222B34;
  --color-light:     #F5FAFD;
  --color-white:     #FFFFFF;

  --cell-bg: var(--color-white);
  --cell-bg-hint: #F1E0FF;
  --letter-color:  #000000;
  --letter-correct: #1E9E46;
  --letter-wrong:  var(--color-error);

  --font-title: 'Muli', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-body:  'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: var(--font-body);
  background: linear-gradient(135deg, #9900FF 0%, #F1E0FF 100%);
  background-attachment: fixed;
  color: var(--color-dark);
}

h1, h2, h3, h4 { font-family: var(--font-title); margin: 0 0 8px; }

button { font-family: var(--font-body); cursor: pointer; }

/* ---------- Layout de pantallas ---------- */
.screen {
  display: none;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.screen.active { display: flex; }

.card {
  background: var(--color-white);
  border-radius: 18px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.25);
  padding: 28px 26px;
  max-width: 480px;
  width: 100%;
  text-align: center;
}
.card--menu { max-width: 520px; }
.card--modal { max-width: 560px; max-height: 85vh; overflow-y: auto; text-align: left; }

.title { font-size: 1.8rem; color: var(--color-dark); }
.title--sm { font-size: 1.4rem; text-align: center; }
.subtitle { margin: 0 0 20px; color: #5a6472; }
.section-label { font-size: 1rem; color: var(--color-accent-4); margin-bottom: 10px; }

/* ---------- Botones ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 13px 18px;
  margin: 8px 0;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: linear-gradient(135deg, var(--color-primary), var(--color-accent-4));
  color: var(--color-white);
  box-shadow: 0 6px 14px rgba(153, 0, 255, 0.35);
}
.btn-ghost {
  background: var(--color-light);
  color: var(--color-dark);
}
.stack-buttons { display: flex; flex-direction: column; }

.icon-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: var(--color-white);
  font-size: 1.3rem;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  line-height: 1;
}
.icon-btn--inline {
  width: 30px;
  height: 30px;
  font-size: 1rem;
  background: rgba(153, 0, 255, 0.12);
  color: var(--color-primary);
  vertical-align: middle;
  margin: 0 2px;
}

/* ---------- Selección de puzzle (menú) ---------- */
.level-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}
.level-btn {
  border: 3px solid transparent;
  border-radius: 14px;
  background: var(--color-light);
  padding: 16px 10px;
  text-align: center;
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-dark);
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.level-btn.selected {
  border-color: var(--color-primary);
  transform: translateY(-2px);
  background: #f7ecff;
}

/* ---------- Ajustes ---------- */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 16px 0;
  text-align: left;
}
.setting-row label { font-weight: 700; }
.setting-row input[type="range"] { flex: 1; accent-color: var(--color-primary); }
.switch { width: 20px; height: 20px; accent-color: var(--color-primary); }

/* ---------- Pantalla de juego ---------- */
#screen-game { align-items: stretch; padding: 0; }
#screen-game.active { display: flex; flex-direction: column; }

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  margin-bottom: 12px;
  color: var(--color-white);
}
.game-info {
  display: flex;
  gap: 16px;
  align-items: center;
  background: rgba(255,255,255,0.15);
  padding: 8px 18px;
  border-radius: 20px;
  font-weight: 700;
  font-size: 0.9rem;
}
.game-level-name {
  background: rgba(255,255,255,0.25);
  padding: 2px 10px;
  border-radius: 12px;
}
.game-body {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 22px;
  align-items: stretch;
  justify-content: center;
  padding: 0 18px 18px;
}

/* ---------- Cuadrícula del crucigrama ---------- */
.cw-grid-wrap {
  overflow: auto;
  max-width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: rgba(255,255,255,0.1);
  border-radius: 18px;
}

.cw-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size, 42px));
  grid-template-rows: repeat(var(--rows), var(--cell-size, 42px));
  gap: 4px;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.cw-cell {
  position: relative;
  width: var(--cell-size, 42px);
  height: var(--cell-size, 42px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--cell-bg);
  box-shadow: 2px 2px 6px rgba(0,0,0,0.22), 0 1px 2px rgba(0,0,0,0.12);
  cursor: pointer;
  transition: box-shadow 0.15s ease, background 0.15s ease;
}
.cw-cell--blocked {
  background: transparent;
  box-shadow: none;
  cursor: default;
}
.cw-number {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 0.6rem;
  font-weight: 700;
  color: #8a8f99;
}
.cw-letter {
  color: var(--letter-color);
  font-weight: 800;
  font-size: calc(var(--cell-size, 42px) * 0.42);
  font-family: var(--font-title);
}
.cw-cell--hint {
  box-shadow: 0 0 0 3px var(--color-primary) inset, 2px 2px 6px rgba(0,0,0,0.22);
  background: var(--cell-bg-hint);
}
.cw-cell--hint-start {
  background: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary) inset, 0 0 10px rgba(153,0,255,0.55), 2px 2px 6px rgba(0,0,0,0.22);
}
.cw-cell--hint-start .cw-letter,
.cw-cell--hint-start .cw-number { color: var(--color-white); }
.cw-cell--wrong {
  box-shadow: 0 0 0 3px var(--color-error) inset, 2px 2px 6px rgba(0,0,0,0.22);
  animation: cw-shake 0.4s;
}
.cw-cell--wrong .cw-letter { color: var(--letter-wrong); }
@keyframes cw-shake {
  20%, 60% { transform: translateX(-4px); }
  40%, 80% { transform: translateX(4px); }
}
.cw-cell--target {
  box-shadow: 0 0 0 3px var(--letter-correct) inset, 2px 2px 6px rgba(0,0,0,0.22);
  background: #e3fbe9;
}
.cw-cell--target .cw-letter { color: var(--letter-correct); }

/* ---------- Panel de palabras (agrupado por nº de letras) ---------- */
.word-panel {
  background: rgba(255,255,255,0.95);
  border-radius: 16px;
  padding: 16px 20px;
  min-width: 200px;
  overflow-y: auto;
  box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}
.word-group { margin-bottom: 14px; }
.word-group:last-child { margin-bottom: 0; }
.word-group-title {
  font-size: 0.85rem;
  color: var(--color-primary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 0 0 6px;
}
.word-list { list-style: none; margin: 0; padding: 0; }
.word-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 7px 8px;
  margin: 2px 0;
  border-radius: 8px;
  color: var(--color-dark);
  transition: background 0.15s ease, color 0.2s ease;
}
.word-item:not(.placed) { cursor: pointer; }
.word-item:not(.placed):hover { background: var(--color-light); }
.word-item.selected { background: var(--cell-bg-hint); }
.word-item.placed {
  color: var(--letter-correct);
  cursor: pointer;
}
.word-item.placed span:first-child {
  text-decoration: line-through;
  text-decoration-color: var(--letter-correct);
}
.word-check {
  font-size: 0.9rem;
  color: var(--letter-correct);
  font-weight: 800;
}
.word-undo {
  font-size: 0.8rem;
  color: var(--color-error);
  opacity: 0.7;
}
.word-speak {
  border: none;
  background: transparent;
  color: var(--color-primary);
  font-size: 1rem;
  padding: 2px 4px;
  line-height: 1;
  opacity: 0.75;
}
.word-speak:hover { opacity: 1; }
.word-panel--locked {
  opacity: 0.65;
  pointer-events: none;
}

/* ---------- Overlays (instrucciones / pausa / significado / victoria) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(34, 43, 52, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.overlay.hidden { display: none; }

/* El fondo del popup de significados es más claro que el resto de overlays
   para que la casilla resaltada en verde siga viéndose detrás. */
#overlay-meaning {
  background: rgba(34, 43, 52, 0.35);
}

.instructions-list { padding-left: 20px; margin: 0 0 18px; }
.instructions-list li { margin-bottom: 10px; line-height: 1.4; border-radius: 8px; transition: background 0.2s ease; }
.instructions-subtitle { text-align: left; margin-top: 6px; border-radius: 8px; transition: background 0.2s ease; }
.reading-active {
  background: var(--cell-bg-hint);
  box-shadow: 0 0 0 2px var(--color-primary);
  padding: 2px 6px;
  margin-left: -6px;
}
#btn-read-instructions { margin-bottom: 14px; }

.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 8px;
  font-weight: 700;
  color: var(--color-white);
}
.tag-green { background: var(--letter-correct); }
.tag-red { background: var(--color-error); }

.pause-progress { color: #5a6472; margin-bottom: 16px; }

/* ---------- Significados (Fase 2) ---------- */
.meaning-options { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.meaning-option-row { display: flex; align-items: stretch; gap: 6px; }
.meaning-option {
  flex: 1;
  text-align: left;
  background: var(--color-light);
  border: 2px solid transparent;
  margin: 0;
}
.meaning-option:disabled { cursor: default; }
.meaning-speak {
  flex: 0 0 auto;
  width: 42px;
  border: none;
  border-radius: 12px;
  background: var(--color-light);
  color: var(--color-primary);
  font-size: 1.1rem;
}
.meaning-option.correct {
  background: #e3fbe9;
  border-color: var(--letter-correct);
  color: #146328;
}
.meaning-option.wrong {
  background: #fdeceb;
  border-color: var(--color-error);
  color: #96261b;
  text-decoration: line-through;
}
#meaning-word { color: var(--color-primary); }

.victory-stats { text-align: left; margin: 10px 0 18px; }
.victory-stats p { display: flex; justify-content: space-between; margin: 6px 0; }
.record-badge {
  text-align: center;
  background: var(--color-accent-2);
  color: var(--color-dark);
  font-weight: 800;
  padding: 8px;
  border-radius: 10px;
  margin-top: 10px;
}

/* Confeti sencillo */
.confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}
.confetti span {
  position: absolute;
  top: -10px;
  width: 8px;
  height: 14px;
  opacity: 0.9;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to { transform: translateY(110vh) rotate(600deg); opacity: 0.3; }
}

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .game-body { flex-direction: column; align-items: center; }
  .word-panel { width: 100%; max-width: 340px; }
  .card { padding: 22px 18px; }
}
@media (max-width: 480px) {
  .level-grid { grid-template-columns: 1fr; }
  .game-info { flex-wrap: wrap; justify-content: center; gap: 6px 10px; font-size: 0.75rem; padding: 6px 10px; }
}
