/* ==========================================================================
   🎨 CORE DESIGN SYSTEM & VARIABLES
   ========================================================================== */
:root {
  /* Palette Crepuscolare / Tramonto in Montagna */
  --bg-twilight-dark: #0b0c16;
  --bg-twilight-mid: #1d1a39;
  --bg-twilight-light: #451e3e;
  --accent-sunset: #ff8a5c;
  --accent-gold: #ffd700;
  --accent-rose: #ff85a2;
  --text-light: #f3f4f6;
  --text-muted: #b3b4c2;
  --glass-bg: rgba(255, 255, 255, 0.05);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-shadow: rgba(0, 0, 0, 0.4);

  /* Tipografia */
  --font-sans: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-script: 'Dancing Script', cursive;
}

/* Reset Base e Prevenzione Scrollbar Globali Antiestetiche */
html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  background: linear-gradient(to bottom, var(--bg-twilight-dark) 0%, var(--bg-twilight-mid) 50%, var(--bg-twilight-light) 100%);
  font-family: var(--font-sans);
  color: var(--text-light);
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

/* Background Stars & Particles */
#stars-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.star {
  position: absolute;
  background: white;
  border-radius: 50%;
  opacity: 0;
  animation: twinkle var(--duration) ease-in-out infinite;
}

@keyframes twinkle {

  0%,
  100% {
    opacity: 0;
  }

  50% {
    opacity: var(--opacity);
  }
}

/* Particle Explosion */
#explosion-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 999;
}

.particle {
  position: absolute;
  left: 50%;
  top: 50%;
  pointer-events: none;
  opacity: 0;
  animation: explode var(--duration) cubic-bezier(0.1, 0.85, 0.25, 1) forwards;
}

@keyframes explode {
  0% {
    transform: translate(-50%, -50%) scale(0) rotate(0deg);
    opacity: 1;
  }

  100% {
    transform: translate(var(--tx), var(--ty)) scale(var(--scale)) rotate(var(--rot));
    opacity: 0;
  }
}

/* ==========================================================================
   1️⃣ SCHERMATA DI INTRODUZIONE
   ========================================================================== */
#intro-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 100;
  background: radial-gradient(circle at center, var(--bg-twilight-mid) 0%, var(--bg-twilight-dark) 100%);
  transition: opacity 1s ease, visibility 1s;
  padding: 20px;
}

#intro-screen.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.intro-card {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 40px;
  border-radius: 24px;
  text-align: center;
  box-shadow: 0 20px 50px var(--glass-shadow);
  max-width: 500px;
  width: 100%;
  transform: translateY(0);
  animation: float-slow 4s ease-in-out infinite;
}

@keyframes float-slow {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}

.intro-title {
  font-size: 2.8rem;
  font-family: var(--font-script);
  color: var(--accent-rose);
  margin-bottom: 10px;
  text-shadow: 0 0 10px rgba(255, 133, 162, 0.3);
}

.intro-subtitle {
  font-size: 1.1rem;
  font-weight: 300;
  color: var(--text-muted);
  margin-bottom: 30px;
  line-height: 1.6;
}

#start-btn {
  background: linear-gradient(135deg, var(--accent-rose), var(--accent-sunset));
  border: none;
  outline: none;
  cursor: pointer;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
  box-shadow: 0 0 25px rgba(255, 133, 162, 0.4);
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.3s;
  position: relative;
}

#start-btn::before {
  content: '';
  position: absolute;
  inset: -8px;
  border: 2px dashed var(--accent-sunset);
  border-radius: 50%;
  animation: rotate-slow 15s linear infinite;
}

#start-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 0 35px rgba(255, 133, 162, 0.6);
}

#start-btn:active {
  transform: scale(0.95);
}

.start-btn-icon {
  font-size: 2.5rem;
  color: white;
  transition: transform 0.3s;
}

#start-btn:hover .start-btn-icon {
  transform: rotate(15deg);
}

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

.start-prompt {
  margin-top: 25px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--text-muted);
}

/* ==========================================================================
   2️⃣ SCHERMATA PRINCIPALE (Cruciverba)
   ========================================================================== */
#main-screen {
  width: 100%;
  max-width: 1200px;
  padding: 30px 16px;
  z-index: 2;
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 1.2s cubic-bezier(0.16, 1, 0.3, 1), transform 1.2s cubic-bezier(0.16, 1, 0.3, 1);
  overflow-x: hidden;
}

#main-screen.visible {
  opacity: 1;
  transform: translateY(0);
}

/* Header e Navigazione */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 16px;
  width: 100%;
  flex-wrap: wrap;
  gap: 12px;
}

.back-btn {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color 0.3s, transform 0.3s;
  font-weight: 300;
  min-height: 44px;
  padding: 0 8px;
}

.back-btn:hover {
  color: var(--accent-rose);
  transform: translateX(-3px);
}

.header-titles {
  text-align: right;
}

.game-title {
  font-family: var(--font-script);
  font-size: 2.5rem;
  color: var(--accent-rose);
  text-shadow: 0 0 10px rgba(255, 133, 162, 0.2);
}

.game-subtitle {
  font-size: 0.9rem;
  font-weight: 300;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

/* Grids Container - Flex Container */
.grids-wrapper {
  display: flex;
  gap: 30px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 40px;
  width: 100%;
  max-width: 100%;
}

.puzzle-card {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border-radius: 20px;
  padding: 24px 20px;
  box-shadow: 0 15px 35px var(--glass-shadow);
  flex: 1 1 420px;
  min-width: 0;
  max-width: 580px;
  width: 100%;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s, box-shadow 0.3s, border-color 0.3s;
}

.puzzle-card:hover {
  border-color: rgba(255, 255, 255, 0.15);
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.5);
}

.puzzle-card.completed {
  border-color: var(--accent-gold);
  box-shadow: 0 0 30px rgba(255, 215, 0, 0.15);
}

.card-title {
  font-family: var(--font-script);
  font-size: 2.2rem;
  color: var(--accent-sunset);
  margin-bottom: 20px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.card-title .status-icon {
  font-size: 1.4rem;
  opacity: 0;
  transform: scale(0.5);
  transition: opacity 0.5s, transform 0.5s;
}

.puzzle-card.completed .card-title .status-icon {
  opacity: 1;
  transform: scale(1);
  color: var(--accent-gold);
}

/* Cruciverba Grids - Visibilità Verticale Totale al 100% */
.crossword-grid {
  display: flex;
  flex-direction: column;
  gap: var(--cell-gap, 6px);
  margin: 0 auto 24px auto;
  align-items: flex-start;
  width: 100%;
  max-width: 100%;
  flex-shrink: 0; /* IMPEDISCE LO SCHIACCIAMENTO VERTICALE */
  overflow-x: auto;
  overflow-y: visible; /* TUTTE LE RIGHE SONO SEMPRE VISIBILI */
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding: 10px 10px 10px 24px;
}

.crossword-grid::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.grid-row {
  display: flex;
  gap: var(--cell-gap, 6px);
  align-items: center;
  position: relative;
  flex-shrink: 0;
}

/* Spacer INVISIBILI per l'allineamento verticale della parola chiave */
.cell-spacer {
  width: var(--cell-size, 34px);
  height: var(--cell-size, 34px);
  flex-shrink: 0;
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  visibility: hidden;
  pointer-events: none;
}

/* Etichetta Numero Riga */
.row-number {
  position: absolute;
  left: calc(var(--offset-left, 0px) - 20px);
  width: 16px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent-rose);
  text-align: right;
  user-select: none;
}

/* Cella di input - Comoda e Toccabile */
.grid-cell {
  width: var(--cell-size, 34px);
  height: var(--cell-size, 34px);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 6px;
  color: white;
  text-align: center;
  font-family: var(--font-sans);
  font-size: 16px; /* Evita zoom automatizzato su iOS */
  font-weight: 600;
  text-transform: uppercase;
  outline: none;
  transition: background-color 0.3s, border-color 0.3s, box-shadow 0.3s, transform 0.2s;
  caret-color: transparent;
  flex-shrink: 0;
  padding: 0;
}

.grid-cell:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.4);
}

.grid-cell:focus {
  background: rgba(255, 255, 255, 0.18);
  border-color: var(--accent-rose);
  box-shadow: 0 0 12px rgba(255, 133, 162, 0.6);
  transform: scale(1.06);
  z-index: 10;
}

/* Cella evidenziata che compone la parola verticale */
.grid-cell.target-cell {
  border: 2px solid rgba(255, 176, 133, 0.8);
  background: rgba(255, 176, 133, 0.18);
  box-shadow: inset 0 0 8px rgba(255, 176, 133, 0.3);
}

.grid-cell.target-cell:focus {
  border-color: var(--accent-sunset);
  box-shadow: 0 0 14px rgba(255, 138, 92, 0.7), inset 0 0 8px rgba(255, 176, 133, 0.3);
}

/* Riga corretta */
.grid-row.row-correct .grid-cell {
  border-color: rgba(72, 187, 120, 0.5);
  background: rgba(72, 187, 120, 0.05);
}

.grid-row.row-correct .grid-cell.target-cell {
  border-color: var(--accent-gold);
  background: rgba(255, 215, 0, 0.12);
  color: var(--accent-gold);
  text-shadow: 0 0 5px rgba(255, 215, 0, 0.6);
}

/* Sezione Definizioni (Indizi) */
.clues-section {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  padding-top: 20px;
  flex-grow: 1;
  width: 100%;
}

.clues-title {
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--accent-sunset);
  margin-bottom: 14px;
  font-weight: 500;
}

.clues-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  list-style-type: none;
  width: 100%;
}

.clue-item {
  font-size: 0.92rem;
  line-height: 1.45;
  color: var(--text-muted);
  padding: 10px 12px;
  border-radius: 8px;
  transition: background-color 0.3s, color 0.3s;
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  word-break: break-word;
}

.clue-item:hover {
  background: rgba(255, 255, 255, 0.03);
  color: var(--text-light);
}

/* Definizioni evidenziate in base al focus */
.clue-item.active {
  background: rgba(255, 133, 162, 0.12);
  color: white;
  border-left: 3px solid var(--accent-rose);
  padding-left: 9px;
}

.clue-item.completed {
  text-decoration: line-through;
  opacity: 0.4;
}

.clue-number {
  font-weight: 600;
  color: var(--accent-rose);
  margin-right: 6px;
  flex-shrink: 0;
}

/* Istruzioni e Tasto Footer */
.instructions-footer {
  text-align: center;
  margin-top: 30px;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
}

.footer-actions {
  margin-top: 8px;
  width: 100%;
  display: flex;
  justify-content: center;
}

.skip-letter-btn {
  background: linear-gradient(135deg, rgba(255, 133, 162, 0.2), rgba(255, 138, 92, 0.25));
  border: 1px solid var(--accent-rose);
  color: var(--text-light);
  font-family: var(--font-sans);
  font-size: 0.95rem;
  font-weight: 500;
  padding: 12px 24px;
  border-radius: 30px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 4px 15px rgba(255, 133, 162, 0.2);
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  min-height: 48px;
  max-width: 100%;
}

.skip-letter-btn:hover {
  background: linear-gradient(135deg, var(--accent-rose), var(--accent-sunset));
  color: white;
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 8px 25px rgba(255, 133, 162, 0.4);
}

.skip-letter-btn:active {
  transform: translateY(1px) scale(0.98);
}

.skip-letter-btn .btn-icon {
  font-size: 1.2rem;
  transition: transform 0.3s;
}

.skip-letter-btn:hover .btn-icon {
  transform: rotate(12deg) scale(1.15);
}

/* ==========================================================================
   3️⃣ MODALE FINALE / LETTERA D'AMORE
   ========================================================================== */
#success-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(11, 12, 22, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 1s ease, visibility 1s;
  padding: 16px;
  overflow-x: hidden;
}

#success-overlay.visible {
  opacity: 1;
  visibility: visible;
}

.letter-container {
  background: #fffcf5;
  color: #3b302c;
  border-radius: 20px;
  max-width: 580px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.5), inset 0 0 100px rgba(139, 69, 19, 0.05);
  border: 1px solid #e6dec9;
  transform: scale(0.9);
  transition: transform 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  display: flex;
  flex-direction: column;
}

#success-overlay.visible .letter-container {
  transform: scale(1);
}

.letter-header {
  padding: 24px 30px 10px 30px;
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}

.wax-seal {
  width: 70px;
  height: 70px;
  position: relative;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.15));
}

.letter-body {
  padding: 16px 30px;
  font-family: var(--font-sans);
  font-size: 1.05rem;
  line-height: 1.8;
  font-weight: 350;
  min-height: 180px;
  white-space: normal;
  word-break: break-word;
  overflow-wrap: break-word;
  color: #4a3e3b;
  flex-shrink: 0;
}

.letter-body .highlight-words {
  font-family: var(--font-script);
  font-size: 2.1rem;
  color: #e27396;
  display: inline;
  vertical-align: baseline;
  line-height: 1.3;
  font-weight: 700;
  padding: 0 3px;
  text-shadow: 1px 1px 0 rgba(226, 115, 150, 0.15);
}

.letter-signature {
  padding: 10px 30px 24px 30px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-family: var(--font-script);
  font-size: 1.8rem;
  color: #e27396;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 1s ease 0.5s, transform 1s ease 0.5s;
  flex-shrink: 0;
}

.letter-signature.visible {
  opacity: 1;
  transform: translateY(0);
}

.letter-signature span {
  display: block;
}

.sig-heart {
  font-size: 1.6rem;
  margin-top: 5px;
  animation: heart-beat 1.5s ease infinite;
  display: inline-block;
}

@keyframes heart-beat {

  0%,
  100% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.18);
  }
}

.btn-container {
  padding: 0 30px 30px 30px;
  display: flex;
  justify-content: center;
  opacity: 0;
  transform: translateY(15px);
  transition: opacity 1s ease 1s, transform 1s ease 1s;
  flex-shrink: 0;
}

.btn-container.visible {
  opacity: 1;
  transform: translateY(0);
}

.home-btn {
  background: linear-gradient(135deg, #e27396, #ea9ab2);
  color: white;
  text-decoration: none;
  padding: 14px 36px;
  border-radius: 30px;
  font-weight: 500;
  font-size: 0.95rem;
  box-shadow: 0 10px 20px rgba(226, 115, 150, 0.35);
  transition: transform 0.3s, box-shadow 0.3s;
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.home-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 25px rgba(226, 115, 150, 0.5);
}

.home-btn:active {
  transform: translateY(1px);
}

.typing::after {
  content: '|';
  animation: blink 0.8s infinite;
  display: inline-block;
  font-family: var(--font-sans);
  font-weight: 300;
  margin-left: 2px;
  color: #e27396;
}

.highlight-words.typing::after {
  font-size: 1.4rem;
  vertical-align: baseline;
}

@keyframes blink {

  0%,
  100% {
    opacity: 0;
  }

  50% {
    opacity: 1;
  }
}

/* ==========================================================================
   📱 ADATTAMENTI MOBILE NATIVE
   ========================================================================== */
@media (max-width: 1024px) {
  #main-screen {
    padding: 20px 12px;
  }

  .grids-wrapper {
    flex-direction: column;
    align-items: center;
    gap: 24px;
  }

  .puzzle-card {
    width: 100%;
    max-width: 580px;
  }
}

@media (max-width: 600px) {
  #main-screen {
    padding: 16px 8px;
  }

  .game-header {
    margin-bottom: 16px;
    padding-bottom: 10px;
  }

  .game-title {
    font-size: 1.7rem;
  }

  .game-subtitle {
    font-size: 0.8rem;
  }

  .puzzle-card {
    padding: 16px 12px;
    border-radius: 16px;
  }

  .card-title {
    font-size: 1.6rem;
    margin-bottom: 14px;
  }

  .crossword-grid {
    padding: 8px 6px 8px 22px;
    margin-bottom: 18px;
  }

  .clues-section {
    padding-top: 14px;
  }

  .clue-item {
    font-size: 0.88rem;
    padding: 8px 10px;
  }

  .skip-letter-btn {
    width: 100%;
    padding: 12px 16px;
  }

  #success-overlay {
    padding: 12px 10px;
    align-items: flex-start;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .letter-container {
    max-width: 100%;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    margin: auto 0;
    border-radius: 18px;
  }

  .letter-header {
    padding: 16px 18px 4px 18px;
  }

  .wax-seal {
    width: 56px;
    height: 56px;
  }

  .letter-body {
    padding: 12px 16px;
    font-size: 0.95rem;
    line-height: 1.65;
  }

  .letter-body .highlight-words {
    font-size: 1.55rem;
    line-height: 1.25;
    padding: 0 2px;
  }

  .letter-signature {
    padding: 4px 16px 14px 16px;
    font-size: 1.6rem;
  }

  .btn-container {
    padding: 0 16px 18px 16px;
  }

  .home-btn {
    width: 100%;
    padding: 12px 20px;
  }
}
