/* ==========================================================================
   TABELLINE SPRINT - DESIGN SYSTEM & STYLESHEET
   ========================================================================== */

:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #e0e7ff;
  --secondary: #06b6d4;
  --accent: #f59e0b;
  --accent-pink: #ec4899;
  --success: #10b981;
  --danger: #ef4444;
  --bg-main: #0f172a;
  --bg-card: rgba(30, 41, 59, 0.85);
  --bg-card-hover: rgba(51, 65, 85, 0.95);
  --bg-card-light: #1e293b;
  --border-color: rgba(255, 255, 255, 0.1);
  --border-highlight: rgba(99, 102, 241, 0.4);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-inverse: #0f172a;
  
  --font-display: 'Fredoka', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-body: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;

  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --radius-full: 9999px;

  --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
  --shadow-glow: 0 0 25px rgba(99, 102, 241, 0.4);
  --shadow-glow-success: 0 0 25px rgba(16, 185, 129, 0.5);
  --shadow-glow-danger: 0 0 25px rgba(239, 68, 68, 0.5);

  --transition-fast: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-bounce: 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Base Reset & Box Sizing */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html, body {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: var(--bg-main);
  background-image: 
    radial-gradient(at 0% 0%, rgba(79, 70, 229, 0.18) 0px, transparent 50%),
    radial-gradient(at 100% 100%, rgba(236, 72, 153, 0.15) 0px, transparent 50%),
    radial-gradient(at 50% 50%, rgba(6, 182, 212, 0.08) 0px, transparent 60%);
  background-attachment: fixed;
  color: var(--text-main);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  overflow-x: hidden;
}

/* App Container */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 580px;
  margin: 0 auto;
  padding: 12px 16px 24px;
  position: relative;
}

/* Top Nav Bar */
.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0 16px;
}

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

.brand-icon {
  font-size: 1.7rem;
  filter: drop-shadow(0 0 10px rgba(245, 158, 11, 0.6));
}

.brand-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text-main);
}

.brand-title .highlight {
  color: #38bdf8;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-icon {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 8px 12px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 600;
  transition: all var(--transition-fast);
  backdrop-filter: blur(10px);
}

.btn-icon:hover, .btn-icon:active {
  background: var(--bg-card-hover);
  border-color: var(--border-highlight);
  transform: translateY(-2px);
}

.badge {
  background: var(--primary);
  color: #ffffff;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 700;
}

/* Screen Management */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex-direction: column;
  animation: screenFadeIn 0.25s ease-out forwards;
}

.screen.active {
  display: flex;
}

@keyframes screenFadeIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.99);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ==========================================================================
   HOME SCREEN
   ========================================================================== */

.btn-profile-badge {
  background: rgba(99, 102, 241, 0.15);
  border: 1px solid rgba(99, 102, 241, 0.4);
  color: #c7d2fe;
  padding: 6px 14px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.88rem;
  font-weight: 700;
  transition: all var(--transition-fast);
}

.btn-profile-badge:hover {
  background: rgba(99, 102, 241, 0.3);
  border-color: #818cf8;
  color: #ffffff;
  transform: translateY(-2px);
}

.hero-top-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.user-active-pill {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 4px 12px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 700;
  transition: all var(--transition-fast);
}

.user-active-pill:hover {
  background: var(--bg-card-hover);
  border-color: var(--border-highlight);
}

.user-active-pill .u-switch {
  color: #38bdf8;
  font-size: 0.75rem;
}

.hero-badge {
  display: inline-block;
  background: rgba(99, 102, 241, 0.2);
  color: #a5b4fc;
  border: 1px solid rgba(99, 102, 241, 0.3);
  padding: 4px 14px;
  border-radius: var(--radius-full);
  font-size: 0.85rem;
  font-weight: 600;
}

.hero-title {
  font-family: var(--font-display);
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.2;
  margin-bottom: 8px;
}

.gradient-text {
  background: linear-gradient(135deg, #38bdf8 0%, #a855f7 50%, #ec4899 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.hero-subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
}

/* Game Mode Cards Grid */
.game-modes-grid {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 24px;
}

.mode-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  transition: all var(--transition-fast);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-md);
}

.mode-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 5px;
  height: 100%;
}

.card-quiz::before { background: linear-gradient(to bottom, #f59e0b, #ef4444); }
.card-keypad::before { background: linear-gradient(to bottom, #ec4899, #8b5cf6); }
.card-memory::before { background: linear-gradient(to bottom, #06b6d4, #3b82f6); }
.card-brainwash::before { background: linear-gradient(to bottom, #a855f7, #ec4899); }

.mode-card:hover {
  transform: translateY(-4px) scale(1.01);
  border-color: rgba(255, 255, 255, 0.25);
  box-shadow: var(--shadow-glow);
}

.mode-card:active {
  transform: scale(0.98);
}

.mode-card-badge {
  align-self: flex-start;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  margin-bottom: 12px;
}

.card-quiz .mode-card-badge { background: rgba(245, 158, 11, 0.2); color: #fbbf24; }
.card-keypad .mode-card-badge { background: rgba(236, 72, 153, 0.2); color: #f472b6; }
.card-memory .mode-card-badge { background: rgba(6, 182, 212, 0.2); color: #67e8f9; }
.card-brainwash .mode-card-badge { background: rgba(168, 85, 247, 0.2); color: #c084fc; }

.mode-footer-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
}

.level-indicator {
  font-size: 0.8rem;
  font-weight: 700;
  color: #38bdf8;
  background: rgba(56, 189, 248, 0.12);
  padding: 3px 10px;
  border-radius: var(--radius-full);
  border: 1px solid rgba(56, 189, 248, 0.25);
}

.mode-icon-wrapper {
  font-size: 2.3rem;
  margin-bottom: 6px;
}

.mode-info h2 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.mode-info p {
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.4;
  margin-bottom: 14px;
}

.btn-play {
  align-self: flex-end;
  background: var(--bg-card-light);
  border: 1px solid var(--border-color);
  color: #ffffff;
  padding: 6px 14px;
  border-radius: var(--radius-full);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

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

/* Stats Banner */
.stats-overview-card {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 14px 18px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-icon {
  font-size: 1.4rem;
}

.stat-details {
  display: flex;
  flex-direction: column;
}

.stat-val {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  color: #38bdf8;
}

.stat-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.stat-divider {
  width: 1px;
  height: 28px;
  background: var(--border-color);
}

/* ==========================================================================
   GAME HEADERS & CONTROLS (SHARED)
   ========================================================================== */

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 8px;
}

.btn-back {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 8px 14px;
  border-radius: var(--radius-full);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-back:hover, .btn-back:active {
  background: var(--bg-card-hover);
  border-color: var(--border-highlight);
}

.btn-icon-refresh {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 8px 12px;
  border-radius: var(--radius-full);
  font-size: 1rem;
  cursor: pointer;
}

/* ==========================================================================
   1. QUIZ & SFIDA TASTIERINO (A LIVELLI)
   ========================================================================== */

.level-header-badge {
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.15);
  padding: 5px 12px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow-sm);
}

.level-header-badge .lvl-icon {
  font-size: 1.3rem;
  filter: drop-shadow(0 0 6px rgba(245, 158, 11, 0.6));
}

.level-header-badge .lvl-texts {
  display: flex;
  flex-direction: column;
}

.level-header-badge .lvl-num {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  color: #38bdf8;
  letter-spacing: 0.5px;
  line-height: 1.1;
}

.level-header-badge .lvl-name {
  font-family: var(--font-display);
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.1;
}

.streak-pill {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  font-size: 0.9rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 4px;
}

.streak-pill .fire {
  animation: pulseFire 1s infinite alternate;
}

@keyframes pulseFire {
  0% { transform: scale(1); filter: drop-shadow(0 0 2px #f59e0b); }
  100% { transform: scale(1.2); filter: drop-shadow(0 0 8px #ef4444); }
}

/* Level Progress Bar */
.level-progress-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
  background: rgba(15, 23, 42, 0.5);
  border: 1px solid var(--border-color);
  padding: 8px 14px;
  border-radius: var(--radius-md);
}

.level-progress-labels {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  font-weight: 600;
}

.lp-rule {
  color: var(--text-muted);
}

.lp-points {
  color: #38bdf8;
  font-family: var(--font-display);
  font-size: 0.9rem;
  font-weight: 700;
}

.level-progress-track {
  width: 100%;
  height: 8px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.level-progress-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(to right, #38bdf8, #818cf8, #ec4899);
  border-radius: var(--radius-full);
  transition: width 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.quiz-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 4px;
}

.quiz-card {
  background: linear-gradient(145deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.95));
  border: 2px solid var(--border-highlight);
  border-radius: var(--radius-lg);
  padding: 26px 20px;
  text-align: center;
  box-shadow: var(--shadow-md), var(--shadow-glow);
  position: relative;
}

.quiz-table-tag {
  position: absolute;
  top: 10px;
  right: 14px;
  background: rgba(99, 102, 241, 0.25);
  color: #a5b4fc;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: var(--radius-full);
}

.quiz-equation {
  font-family: var(--font-display);
  font-size: 3rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  letter-spacing: 1px;
}

.quiz-equation .op {
  color: #f59e0b;
}

.quiz-equation .equals {
  color: var(--text-muted);
}

.target-input-slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 54px;
  padding: 0 10px;
  height: 60px;
  background: rgba(56, 189, 248, 0.15);
  border: 2px dashed #38bdf8;
  border-radius: var(--radius-sm);
  color: #38bdf8;
  font-size: 2.8rem;
  animation: targetSlotGlow 1.5s infinite alternate;
}

@keyframes targetSlotGlow {
  0% { border-color: rgba(56, 189, 248, 0.4); box-shadow: 0 0 5px rgba(56, 189, 248, 0.2); }
  100% { border-color: #38bdf8; box-shadow: 0 0 15px rgba(56, 189, 248, 0.5); }
}

.quiz-options-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: 100%;
}

/* Griglia per 3 opzioni: 3 su 1 riga */
.quiz-options-grid.options-3 {
  grid-template-columns: repeat(3, 1fr);
}

/* Griglia per 4 opzioni: 2x2 */
.quiz-options-grid.options-4 {
  grid-template-columns: repeat(2, 1fr);
}

/* Griglia per 5 opzioni: 3 sopra e 2 sotto centrate */
.quiz-options-grid.options-5 {
  grid-template-columns: repeat(6, 1fr);
}

.quiz-options-grid.options-5 .quiz-option-btn:nth-child(1),
.quiz-options-grid.options-5 .quiz-option-btn:nth-child(2),
.quiz-options-grid.options-5 .quiz-option-btn:nth-child(3) {
  grid-column: span 2;
}

.quiz-options-grid.options-5 .quiz-option-btn:nth-child(4),
.quiz-options-grid.options-5 .quiz-option-btn:nth-child(5) {
  grid-column: span 3;
}

/* Griglia per 6 opzioni: 3 sopra e 3 sotto (3x2) */
.quiz-options-grid.options-6 {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
}

.quiz-option-btn {
  background: var(--bg-card);
  border: 2px solid var(--border-color);
  color: var(--text-main);
  padding: 12px 10px;
  border-radius: var(--radius-md);
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-bounce);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60px;
}

.quiz-option-btn:hover {
  background: var(--bg-card-hover);
  border-color: rgba(255, 255, 255, 0.4);
  transform: translateY(-2px);
}

.quiz-option-btn:active {
  transform: scale(0.97);
}

/* ==========================================================================
   VIRTUAL KEYPAD (TASTIERINO NUMERICO)
   ========================================================================== */

.keypad-input-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 14px;
}

.keypad-display-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-card-light);
  border: 2px solid var(--border-highlight);
  border-radius: var(--radius-md);
  padding: 8px 16px;
}

.keypad-display-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 600;
}

.keypad-display-value {
  font-family: var(--font-display);
  font-size: 2rem;
  font-weight: 700;
  color: #34d399;
  min-width: 45px;
  text-align: right;
  letter-spacing: 2px;
}

.virtual-keypad {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
}

.keypad-num-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}

.keypad-actions-row {
  display: grid;
  grid-template-columns: 1fr 1.3fr;
  gap: 10px;
  margin-top: 2px;
}

.key-btn {
  background: var(--bg-card);
  border: 1.5px solid var(--border-color);
  color: var(--text-main);
  padding: 8px 4px;
  border-radius: var(--radius-md);
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25);
  aspect-ratio: 1.05 / 1;
  min-height: 46px;
}

.key-btn:hover {
  background: var(--bg-card-hover);
  border-color: rgba(255, 255, 255, 0.35);
  transform: translateY(-2px);
}

.key-btn:active {
  transform: scale(0.92);
  background: #334155;
}

.keypad-actions-row .key-btn {
  aspect-ratio: auto;
  min-height: 46px;
  font-size: 1.05rem;
  font-family: var(--font-body);
  font-weight: 700;
  padding: 10px 14px;
  gap: 6px;
}

.key-backspace {
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.35);
}

.key-backspace:hover {
  background: rgba(239, 68, 68, 0.28);
  color: #ffffff;
}

.key-submit {
  background: linear-gradient(135deg, #10b981, #059669);
  color: #ffffff;
  border: none;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}

.key-submit:hover {
  filter: brightness(1.1);
}

/* Feedback states */
.quiz-option-btn.correct, .keypad-display-wrapper.correct {
  background: #059669 !important;
  border-color: #34d399 !important;
  color: #ffffff !important;
  box-shadow: var(--shadow-glow-success) !important;
  animation: correctPop 0.4s ease-out;
}

.quiz-option-btn.wrong, .keypad-display-wrapper.wrong {
  background: #dc2626 !important;
  border-color: #f87171 !important;
  color: #ffffff !important;
  box-shadow: var(--shadow-glow-danger) !important;
  animation: wrongShake 0.4s ease-in-out;
}

.quiz-option-btn.disabled {
  pointer-events: none;
  opacity: 0.7;
}

@keyframes correctPop {
  0% { transform: scale(0.95); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

.quiz-feedback-banner {
  min-height: 32px;
  text-align: center;
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 600;
}

.quiz-feedback-banner.success { color: #34d399; }
.quiz-feedback-banner.error { color: #f87171; }

/* ==========================================================================
   2. MEMORY A LIVELLI (2x2, 4x3, 4x4, 4x5, 5x6)
   ========================================================================== */

.memory-status-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.stat-chip {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 6px 10px;
  border-radius: var(--radius-full);
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-main);
}

/* Selettore Livelli Memory */
.memory-level-selector-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(15, 23, 42, 0.5);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: var(--radius-md);
  margin-bottom: 8px;
}

.mem-lvl-label {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
}

.memory-level-pills-bar {
  display: flex;
  gap: 6px;
  flex: 1;
  overflow-x: auto;
}

.mem-lvl-btn {
  background: var(--bg-card-light);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.mem-lvl-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text-main);
}

.mem-lvl-btn.active {
  background: var(--primary);
  border-color: var(--primary-light);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.4);
}

.memory-board-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin-top: 4px;
}

/* Base Memory Grid */
.memory-grid {
  display: grid;
  gap: 8px;
  width: 100%;
  perspective: 1000px;
}

/* 5 Grid Variants (Tutte impaginate in verticale se non quadrate) */
.grid-2x2 {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  max-width: 280px;
  aspect-ratio: 1 / 1;
}

.grid-3x4, .grid-4x3 {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(4, 1fr);
  max-width: 360px;
  aspect-ratio: 3 / 4;
}

.grid-4x4 {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  max-width: 440px;
  aspect-ratio: 1 / 1;
}

.grid-4x5 {
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  max-width: 400px;
  aspect-ratio: 4 / 5;
  gap: 6px;
}

.grid-5x6 {
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(6, 1fr);
  max-width: 440px;
  aspect-ratio: 5 / 6;
  gap: 5px;
}

.memory-card {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: var(--radius-md);
}

.memory-card.flipped {
  transform: rotateY(180deg);
}

.memory-card.matched {
  transform: rotateY(180deg);
  opacity: 1;
  pointer-events: none;
  cursor: default;
  animation: matchBounce 0.4s ease-out;
}

@keyframes matchBounce {
  0% { transform: rotateY(180deg) scale(0.95); }
  50% { transform: rotateY(180deg) scale(1.08); }
  100% { transform: rotateY(180deg) scale(1); }
}

.card-face {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: var(--shadow-sm);
  font-family: var(--font-display);
}

/* Card Back (hidden state) */
.card-back {
  background: linear-gradient(135deg, #1e293b, #334155);
  border: 2px solid rgba(255, 255, 255, 0.12);
  color: #38bdf8;
  font-size: 1.6rem;
  transition: all var(--transition-fast);
}

.memory-card:hover .card-back {
  border-color: rgba(56, 189, 248, 0.5);
  background: linear-gradient(135deg, #334155, #475569);
}

/* Card Front (revealed state) */
.card-front {
  transform: rotateY(180deg);
  border: 2px solid var(--border-highlight);
  font-weight: 700;
  padding: 6px;
  text-align: center;
}

/* Operation card vs Result card visual distinction */
.card-front.is-op {
  background: linear-gradient(145deg, #312e81, #4338ca);
  color: #ffffff;
  font-size: 1.15rem;
  letter-spacing: -0.5px;
}

.card-front.is-res {
  background: linear-gradient(145deg, #0e7490, #0891b2);
  color: #ffffff;
  font-size: 1.5rem;
}

/* Stile per carte correttamente accoppiate: restano completamente visibili, luminose e nitide */
.memory-card.matched .card-front.is-op {
  background: linear-gradient(145deg, #1e3a5f, #065f46) !important;
  border: 2.5px solid #10b981 !important;
  box-shadow: 0 0 16px rgba(16, 185, 129, 0.45);
}

.memory-card.matched .card-front.is-res {
  background: linear-gradient(145deg, #064e3b, #059669) !important;
  border: 2.5px solid #34d399 !important;
  box-shadow: 0 0 16px rgba(52, 211, 153, 0.45);
}

/* Adattamento dimensioni testi per griglie dense */
.grid-2x2 .card-front.is-op { font-size: 1.6rem; }
.grid-2x2 .card-front.is-res { font-size: 2.2rem; }
.grid-2x2 .card-back { font-size: 2.4rem; }

.grid-4x5 .card-front.is-op { font-size: 0.95rem; }
.grid-4x5 .card-front.is-res { font-size: 1.3rem; }
.grid-4x5 .card-back { font-size: 1.4rem; }

.grid-5x6 .card-front.is-op { font-size: 0.8rem; padding: 2px; }
.grid-5x6 .card-front.is-res { font-size: 1.1rem; padding: 2px; }
.grid-5x6 .card-back { font-size: 1.15rem; }

.btn-modal-tertiary {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  padding: 10px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-modal-tertiary:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
}

/* ==========================================================================
   3. LAVAGGIO DEL CERVELLO (BRAINWASH) SCREEN
   ========================================================================== */

.brainwash-badge {
  font-family: var(--font-display);
  font-size: 0.9rem;
  font-weight: 600;
  color: #ec4899;
  background: rgba(236, 72, 153, 0.15);
  padding: 4px 12px;
  border-radius: var(--radius-full);
}

.btn-voice-toggle {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-voice-toggle.off {
  opacity: 0.6;
  text-decoration: line-through;
}

.brainwash-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* The Giant Flashcard */
.brainwash-card {
  background: linear-gradient(145deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.98));
  border: 2px solid rgba(236, 72, 153, 0.4);
  border-radius: var(--radius-lg);
  padding: 36px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 30px rgba(236, 72, 153, 0.15);
  position: relative;
  min-height: 250px;
  transition: all 0.25s ease-out;
}

.brainwash-card.animate-pulse {
  animation: cardFlash 0.3s ease-out;
}

@keyframes cardFlash {
  0% { transform: scale(0.97); filter: brightness(1.2); }
  100% { transform: scale(1); filter: brightness(1); }
}

.bw-category {
  position: absolute;
  top: 14px;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: #f472b6;
  background: rgba(236, 72, 153, 0.15);
  padding: 3px 12px;
  border-radius: var(--radius-full);
}

.bw-expression {
  font-family: var(--font-display);
  font-size: 3.5rem;
  font-weight: 700;
  color: #ffffff;
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 10px;
}

.bw-times {
  color: #f59e0b;
}

.bw-divider {
  width: 80px;
  height: 3px;
  background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.3), transparent);
  margin: 10px 0;
}

.bw-result-box {
  display: flex;
  align-items: center;
  gap: 12px;
}

.bw-equals {
  font-family: var(--font-display);
  font-size: 3rem;
  color: var(--text-muted);
}

.bw-result {
  font-family: var(--font-display);
  font-size: 4rem;
  font-weight: 700;
  color: #34d399;
  text-shadow: 0 0 20px rgba(52, 211, 153, 0.4);
}

/* Brainwash Settings & Controls */
.bw-controls-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.bw-settings-row {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setting-pill-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.setting-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-right: 4px;
}

.pill-btn {
  background: var(--bg-card-light);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  padding: 5px 12px;
  border-radius: var(--radius-full);
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.pill-btn.active {
  background: var(--primary);
  border-color: var(--primary-light);
  color: #ffffff;
}

.bw-actions-row {
  display: flex;
  gap: 8px;
}

.btn-bw-action {
  flex: 1;
  background: var(--bg-card-light);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 12px 10px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.btn-bw-action:hover {
  background: var(--bg-card-hover);
}

.btn-bw-play {
  flex: 1.4;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  border: none;
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
}

.btn-bw-play:hover {
  filter: brightness(1.1);
}

/* ==========================================================================
   MODALS
   ========================================================================== */

.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(8px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-overlay.open {
  display: flex;
  animation: modalFadeIn 0.2s ease-out;
}

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

.modal-card {
  background: var(--bg-card-light);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-md);
  animation: modalSlideUp 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes modalSlideUp {
  from { transform: translateY(20px) scale(0.96); }
  to { transform: translateY(0) scale(1); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.modal-header h3 {
  font-family: var(--font-display);
  font-size: 1.3rem;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
}

.modal-desc {
  color: var(--text-muted);
  font-size: 0.88rem;
  margin-bottom: 16px;
}

.tables-quick-selection {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.btn-chip {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.tables-toggle-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.table-toggle-btn {
  background: var(--bg-card);
  border: 2px solid var(--border-color);
  color: var(--text-muted);
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-md);
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.table-toggle-btn.selected {
  background: var(--primary);
  border-color: #818cf8;
  color: #ffffff;
  box-shadow: 0 4px 10px rgba(79, 70, 229, 0.4);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
}

.btn-modal-primary {
  width: 100%;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #ffffff;
  border: none;
  padding: 14px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.btn-modal-secondary {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 14px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

/* ==========================================================================
   USER PROFILES & RESET MODAL
   ========================================================================== */

.users-card {
  max-width: 460px;
  max-height: 88vh;
  overflow-y: auto;
}

.active-user-banner {
  display: flex;
  align-items: center;
  gap: 14px;
  background: linear-gradient(135deg, rgba(79, 70, 229, 0.2), rgba(124, 58, 237, 0.25));
  border: 1px solid rgba(124, 58, 237, 0.4);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  margin-bottom: 14px;
}

.active-user-avatar {
  font-size: 2.2rem;
  background: rgba(255, 255, 255, 0.08);
  width: 52px;
  height: 52px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
}

.active-user-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  font-weight: 700;
  color: #a5b4fc;
  letter-spacing: 0.5px;
}

.active-user-title {
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 700;
  color: #ffffff;
  margin: 2px 0;
}

.active-user-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.user-section {
  border-top: 1px solid var(--border-color);
  padding-top: 12px;
  margin-top: 12px;
}

.user-section-title {
  font-family: var(--font-display);
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 8px;
}

.user-section-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 10px;
  line-height: 1.3;
}

.users-list-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 140px;
  overflow-y: auto;
  padding-right: 4px;
}

.user-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  transition: all var(--transition-fast);
}

.user-item-row.is-current {
  border-color: #38bdf8;
  background: rgba(56, 189, 248, 0.1);
}

.user-item-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-item-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.user-badge-current {
  background: #38bdf8;
  color: #0f172a;
  font-size: 0.68rem;
  font-weight: 800;
  padding: 2px 6px;
  border-radius: var(--radius-full);
}

.user-item-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.btn-load-user {
  background: var(--primary);
  border: none;
  color: #ffffff;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-load-user:hover {
  filter: brightness(1.15);
}

.btn-delete-user {
  background: transparent;
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #f87171;
  padding: 4px 8px;
  border-radius: var(--radius-full);
  font-size: 0.78rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-delete-user:hover {
  background: rgba(239, 68, 68, 0.25);
}

.new-user-form {
  display: flex;
  gap: 8px;
}

.input-user {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 0.9rem;
}

.input-user:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.3);
}

.btn-add-user {
  background: linear-gradient(135deg, #10b981, #059669);
  border: none;
  color: #ffffff;
  padding: 10px 16px;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 0.88rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-add-user:hover {
  filter: brightness(1.1);
}

.reset-buttons-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.btn-reset-single {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  color: #fca5a5;
  padding: 8px 10px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-reset-single:hover {
  background: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
  color: #ffffff;
}

.reset-badge {
  background: rgba(239, 68, 68, 0.3);
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: var(--radius-full);
}

.btn-reset-all {
  grid-column: span 2;
  background: linear-gradient(135deg, rgba(220, 38, 38, 0.25), rgba(185, 28, 28, 0.35));
  border: 1px solid #ef4444;
  color: #ffffff;
  padding: 10px;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-reset-all:hover {
  background: #dc2626;
  box-shadow: var(--shadow-glow-danger);
}

/* Level Up Modal Specifics */
.levelup-card {
  text-align: center;
  border: 2px solid #f59e0b;
  box-shadow: 0 0 35px rgba(245, 158, 11, 0.35);
}

.levelup-icon {
  font-size: 4rem;
  margin-bottom: 6px;
  animation: bounceQ 1.2s infinite ease-in-out;
  filter: drop-shadow(0 0 16px rgba(245, 158, 11, 0.8));
}

.levelup-subtitle {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #fbbf24;
  margin-bottom: 4px;
}

.levelup-title {
  font-family: var(--font-display);
  font-size: 1.8rem;
  font-weight: 700;
  color: #ffffff;
  margin-bottom: 8px;
}

.levelup-rank-badge {
  display: inline-block;
  background: linear-gradient(135deg, #f59e0b, #ef4444);
  color: #ffffff;
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  padding: 6px 18px;
  border-radius: var(--radius-full);
  margin-bottom: 12px;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.4);
}

.levelup-desc {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 22px;
  line-height: 1.4;
}

.levelup-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Victory Modal Specifics */
.victory-card {
  text-align: center;
}

.victory-icon {
  font-size: 3.5rem;
  margin-bottom: 8px;
}

.victory-title {
  font-family: var(--font-display);
  font-size: 2rem;
  color: #34d399;
  margin-bottom: 4px;
}

.victory-msg {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 20px;
}

.victory-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-bottom: 24px;
  background: rgba(15, 23, 42, 0.6);
  padding: 14px;
  border-radius: var(--radius-md);
}

.v-stat {
  display: flex;
  flex-direction: column;
}

.v-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

.v-val {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 700;
  color: #38bdf8;
}

.victory-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ==========================================================================
   CONFETTI CANVAS
   ========================================================================== */

#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 2000;
}

/* ==========================================================================
   MOBILE FINE-TUNING
   ========================================================================== */

@media (max-width: 400px) {
  .hero-title {
    font-size: 1.7rem;
  }
  
  .quiz-equation {
    font-size: 2.6rem;
  }
  
  .quiz-option-btn {
    font-size: 1.5rem;
    padding: 14px;
  }

  .card-front.is-op {
    font-size: 0.95rem;
  }

  .card-front.is-res {
    font-size: 1.3rem;
  }

  .bw-expression {
    font-size: 2.8rem;
  }

  .bw-result {
    font-size: 3.2rem;
  }
}
