/* ============================================
   Quiz Formativo — Kahoot Style
   ============================================ */

/* --- Design System Tokens --- */
:root, [data-theme='light'] {
  --font-display: 'Chillax', 'Arial', sans-serif;
  --font-body: 'Nunito', 'Helvetica Neue', sans-serif;

  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
  --text-2xl: clamp(2rem, 1.2rem + 2.5vw, 3.5rem);

  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem;
  --space-4: 1rem; --space-5: 1.25rem; --space-6: 1.5rem;
  --space-8: 2rem; --space-10: 2.5rem; --space-12: 3rem;
  --space-16: 4rem; --space-20: 5rem;

  --color-bg: #f5f7fa;
  --color-surface: #ffffff;
  --color-surface-2: #f0f4f8;
  --color-border: #d0d7e0;
  --color-divider: #e2e8f0;
  --color-text: #1a2a3a;
  --color-text-muted: #5a6a7a;
  --color-text-faint: #94a3b8;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-light: #dbeafe;
  --color-primary-dark: #1e40af;
  --color-success: #16a34a;
  --color-success-light: #dcfce7;
  --color-error: #dc2626;
  --color-error-light: #fee2e2;
  --color-warning: #d97706;
  --color-warning-light: #fef3c7;
  --color-accent: #0d9488;

  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-2xl: 1.25rem;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.12);

  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);

  /* Kahoot-specific */
  --kahoot-bg: #46178f;
  --kahoot-bg-light: #f0ebf8;
  --answer-red: #ef4444;
  --answer-blue: #3b82f6;
  --answer-yellow: #eab308;
  --answer-green: #22c55e;
}

[data-theme='dark'] {
  --color-bg: #0f172a;
  --color-surface: #1e293b;
  --color-surface-2: #283548;
  --color-border: #334155;
  --color-divider: #1e293b;
  --color-text: #e2e8f0;
  --color-text-muted: #94a3b8;
  --color-text-faint: #64748b;
  --color-primary: #60a5fa;
  --color-primary-hover: #3b82f6;
  --color-primary-light: #1e3a5f;
  --color-primary-dark: #93c5fd;
  --color-success: #4ade80;
  --color-success-light: #14532d;
  --color-error: #f87171;
  --color-error-light: #450a0a;
  --color-warning: #fbbf24;
  --color-warning-light: #451a03;
  --color-accent: #2dd4bf;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.5);

  --kahoot-bg: #1a0a30;
  --kahoot-bg-light: #1e293b;
}

/* --- Reset --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; scroll-behavior: smooth; }
body {
  min-height: 100dvh;
  line-height: 1.6;
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  overflow: hidden;
}
img, svg { display: block; max-width: 100%; height: auto; }
button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }
a, button, [role='button'] { transition: color var(--transition-interactive), background var(--transition-interactive); }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; border-radius: var(--radius-sm); }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }

/* --- Theme Toggle --- */
.theme-toggle {
  position: fixed;
  top: var(--space-3);
  right: var(--space-3);
  z-index: 100;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  transition: background var(--transition-fast), color var(--transition-fast);
}
.theme-toggle:hover { background: var(--color-surface-2); color: var(--color-text); }

/* --- Screens --- */
.screen {
  display: none;
  min-height: 100dvh;
  width: 100%;
}
.screen.active { display: flex; }

/* ========================================
   START SCREEN
   ======================================== */
#screen-start {
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  padding: var(--space-6);
}

.start-container {
  max-width: 440px;
  width: 100%;
  text-align: center;
  animation: fadeSlideUp 0.6s ease-out;
}

.start-logo {
  margin: 0 auto var(--space-6);
  animation: pulse 2s ease-in-out infinite;
  display: flex;
  justify-content: center;
}

.start-title {
  font-family: var(--font-display);
  font-size: var(--text-2xl);
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.1;
  margin-bottom: var(--space-2);
}

.start-subtitle {
  font-size: var(--text-lg);
  color: var(--color-text-muted);
  font-weight: 500;
  margin-bottom: var(--space-2);
}

.start-info {
  font-size: var(--text-sm);
  color: var(--color-text-faint);
  margin-bottom: var(--space-6);
}

.start-subjects {
  display: flex;
  justify-content: center;
  gap: var(--space-2);
  margin-bottom: var(--space-8);
  flex-wrap: wrap;
}

.subject-pill {
  display: inline-flex;
  align-items: center;
  padding: var(--space-1) var(--space-4);
  background: color-mix(in srgb, var(--pill-color) 12%, transparent);
  color: var(--pill-color);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 600;
  border: 1.5px solid color-mix(in srgb, var(--pill-color) 30%, transparent);
}

.name-input-group {
  margin-bottom: var(--space-6);
  text-align: left;
}

.name-label {
  display: block;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.name-input {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-xl);
  border: 2px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-base);
  font-family: var(--font-body);
  text-align: center;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.name-input::placeholder { color: var(--color-text-faint); }
.name-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 4px var(--color-primary-light);
}

.btn-start {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-10);
  background: var(--color-primary);
  color: #fff;
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 700;
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-md), 0 4px 0 0 var(--color-primary-dark);
  transition: transform var(--transition-interactive), box-shadow var(--transition-interactive), opacity var(--transition-fast);
  min-height: 56px;
}
.btn-start:not(:disabled):hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg), 0 6px 0 0 var(--color-primary-dark);
}
.btn-start:not(:disabled):active {
  transform: translateY(2px);
  box-shadow: var(--shadow-sm), 0 1px 0 0 var(--color-primary-dark);
}
.btn-start:disabled { opacity: 0.4; cursor: not-allowed; }

/* ========================================
   QUESTION SCREEN
   ======================================== */
#screen-question {
  flex-direction: column;
  background: var(--kahoot-bg);
  position: relative;
}

.question-layout {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: var(--space-3);
  position: relative;
}

/* Top Bar */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-2) 0;
  flex-shrink: 0;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.q-counter {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-lg);
  color: #fff;
  opacity: 0.9;
}

.subject-badge {
  display: inline-flex;
  align-items: center;
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: rgba(255,255,255,0.2);
  color: #fff;
}
.subject-badge[data-subject="TTRG"] { background: #2563eb; }
.subject-badge[data-subject="Inglese Tecnico"] { background: #16a34a; }
.subject-badge[data-subject="Ed. Civica"] { background: #d97706; }

.score-display {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-lg);
  color: #fbbf24;
}
.score-display svg { flex-shrink: 0; }

/* Timer Bar */
.timer-bar-container {
  position: relative;
  width: 100%;
  height: 24px;
  background: rgba(255,255,255,0.15);
  border-radius: var(--radius-full);
  margin: var(--space-2) 0 var(--space-4);
  flex-shrink: 0;
  overflow: hidden;
}

.timer-bar {
  height: 100%;
  width: 100%;
  border-radius: var(--radius-full);
  background: linear-gradient(90deg, #22c55e, #eab308, #ef4444);
  transition: width 0.1s linear;
  transform-origin: left;
}

.timer-text {
  position: absolute;
  right: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-xs);
  color: #fff;
  opacity: 0.9;
  background: rgba(0,0,0,0.3);
  padding: 0 var(--space-2);
  border-radius: var(--radius-full);
  line-height: 1.6;
}

/* Question Card */
.question-card {
  background: rgba(255,255,255,0.12);
  border-radius: var(--radius-2xl);
  padding: var(--space-6) var(--space-5);
  margin-bottom: var(--space-4);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
  animation: fadeIn 0.3s ease-out;
  min-height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.question-text {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 600;
  color: #fff;
  text-align: center;
  line-height: 1.4;
}

/* Answer Grid */
.answer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  flex: 1;
  min-height: 0;
  padding-bottom: var(--space-3);
}

.answer-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border-radius: var(--radius-xl);
  padding: var(--space-3);
  color: #fff;
  font-weight: 700;
  font-size: var(--text-base);
  text-align: center;
  position: relative;
  overflow: hidden;
  transition: transform var(--transition-interactive), opacity var(--transition-fast), filter var(--transition-fast);
  min-height: 80px;
  box-shadow: 0 4px 0 0 rgba(0,0,0,0.2);
  line-height: 1.3;
}

.answer-btn:hover:not(:disabled) { transform: scale(1.03); }
.answer-btn:active:not(:disabled) {
  transform: scale(0.97) translateY(2px);
  box-shadow: 0 1px 0 0 rgba(0,0,0,0.2);
}
.answer-btn:disabled { cursor: default; }

.answer-icon {
  opacity: 0.5;
  flex-shrink: 0;
}

.answer-red    { background: var(--answer-red);    box-shadow: 0 4px 0 0 #b91c1c; }
.answer-blue   { background: var(--answer-blue);   box-shadow: 0 4px 0 0 #1d4ed8; }
.answer-yellow { background: var(--answer-yellow); box-shadow: 0 4px 0 0 #a16207; color: #1a2a3a; }
.answer-yellow .answer-icon { opacity: 0.4; }
.answer-green  { background: var(--answer-green);  box-shadow: 0 4px 0 0 #15803d; }

/* Answer States */
.answer-btn.dimmed { opacity: 0.3; filter: grayscale(0.5); transform: scale(0.95); }
.answer-btn.correct-highlight {
  animation: correctPulse 0.6s ease;
  box-shadow: 0 0 0 4px #fff, 0 4px 0 0 rgba(0,0,0,0.2);
  transform: scale(1.05);
}
.answer-btn.wrong-highlight {
  animation: shake 0.4s ease;
}

/* Feedback Overlay */
.feedback-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 20;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease-out;
}
.feedback-overlay.show { display: flex; }

.feedback-content {
  text-align: center;
  padding: var(--space-6);
  animation: popIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.feedback-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto var(--space-4);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}
.feedback-icon.correct { background: #22c55e; }
.feedback-icon.wrong { background: #ef4444; }

.feedback-text {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: #fff;
  margin-bottom: var(--space-2);
}

.feedback-correct {
  font-size: var(--text-sm);
  color: rgba(255,255,255,0.8);
  max-width: 300px;
  margin: 0 auto;
}

/* Score Popup */
.score-popup {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: #fbbf24;
  pointer-events: none;
  opacity: 0;
  z-index: 30;
  text-shadow: 0 2px 8px rgba(0,0,0,0.4);
}
.score-popup.animate {
  animation: scoreFloat 1s ease-out forwards;
}

/* ========================================
   RESULTS SCREEN
   ======================================== */
#screen-results {
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  padding: var(--space-6);
  overflow-y: auto;
}

.results-container {
  max-width: 480px;
  width: 100%;
  text-align: center;
  animation: fadeSlideUp 0.6s ease-out;
  padding: var(--space-4) 0;
}

.results-stars {
  font-size: 40px;
  margin-bottom: var(--space-4);
  animation: popIn 0.5s 0.2s ease-out both;
}

.results-title {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-text);
  margin-bottom: var(--space-1);
}

.results-name {
  font-size: var(--text-base);
  color: var(--color-text-muted);
  margin-bottom: var(--space-6);
}

/* Score Ring */
.results-score-ring {
  position: relative;
  width: 140px;
  height: 140px;
  margin: 0 auto var(--space-6);
}

.score-ring-svg { width: 100%; height: 100%; }

.score-ring-inner {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.score-ring-number {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-text);
  line-height: 1;
}

.score-ring-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: 600;
}

/* Breakdown */
.results-breakdown {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-bottom: var(--space-6);
  text-align: left;
}

.breakdown-row {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
}

.breakdown-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-2);
}

.breakdown-subject {
  font-weight: 700;
  font-size: var(--text-sm);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.breakdown-dot {
  width: 10px;
  height: 10px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
}

.breakdown-score {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.breakdown-bar-bg {
  width: 100%;
  height: 8px;
  background: var(--color-surface-2);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.breakdown-bar-fill {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width 1s ease-out 0.5s;
  width: 0;
}

.results-message {
  font-size: var(--text-base);
  color: var(--color-text-muted);
  margin-bottom: var(--space-8);
  line-height: 1.6;
  padding: 0 var(--space-4);
}

/* Result Buttons */
.results-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  align-items: center;
}

.btn-restart {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-8);
  background: var(--color-primary);
  color: #fff;
  font-family: var(--font-display);
  font-size: var(--text-base);
  font-weight: 700;
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-md);
  min-height: 48px;
}
.btn-restart:hover { background: var(--color-primary-hover); transform: translateY(-1px); }

.btn-hub {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6);
  background: var(--color-surface);
  color: var(--color-text);
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 600;
  border-radius: var(--radius-full);
  border: 1.5px solid var(--color-border);
  text-decoration: none;
  min-height: 44px;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.btn-hub:hover { background: var(--color-surface-2); border-color: var(--color-text-faint); }

/* ========================================
   ANIMATIONS
   ======================================== */
@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(24px); }
  to   { opacity: 1; transform: translateY(0); }
}

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

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.06); }
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.7); }
  to   { opacity: 1; transform: scale(1); }
}

@keyframes scoreFloat {
  0%   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -150%) scale(1.4); }
}

@keyframes correctPulse {
  0%   { transform: scale(1); }
  30%  { transform: scale(1.08); }
  60%  { transform: scale(0.98); }
  100% { transform: scale(1.05); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-8px); }
  40%      { transform: translateX(8px); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(4px); }
}

@keyframes timerPulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

/* Timer bar low-time warning */
.timer-bar.warning {
  animation: timerPulse 0.5s ease-in-out infinite;
}

/* Correct / Wrong flash on question screen */
#screen-question.flash-correct { animation: flashGreen 0.4s ease; }
#screen-question.flash-wrong   { animation: flashRed 0.4s ease; }

@keyframes flashGreen {
  0%   { background-color: var(--kahoot-bg); }
  50%  { background-color: #166534; }
  100% { background-color: var(--kahoot-bg); }
}

@keyframes flashRed {
  0%   { background-color: var(--kahoot-bg); }
  50%  { background-color: #7f1d1d; }
  100% { background-color: var(--kahoot-bg); }
}

/* Star bounce */
.star { display: inline-block; animation: starBounce 0.4s ease-out both; }
.star:nth-child(1) { animation-delay: 0.3s; }
.star:nth-child(2) { animation-delay: 0.5s; }
.star:nth-child(3) { animation-delay: 0.7s; }

@keyframes starBounce {
  0%   { opacity: 0; transform: scale(0) rotate(-20deg); }
  60%  { transform: scale(1.3) rotate(5deg); }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}

/* ========================================
   RESPONSIVE
   ======================================== */
@media (max-width: 600px) {
  .question-layout { padding: var(--space-2); }

  .answer-btn {
    font-size: var(--text-sm);
    padding: var(--space-2);
    min-height: 64px;
    gap: var(--space-1);
  }

  .answer-icon svg { width: 16px; height: 16px; }

  .question-card {
    padding: var(--space-4) var(--space-3);
    min-height: 80px;
  }

  .question-text { font-size: var(--text-base); }

  .answer-grid { gap: var(--space-2); }

  .results-score-ring { width: 120px; height: 120px; }
}

@media (max-width: 380px) {
  .start-title { font-size: clamp(1.5rem, 6vw, 2rem); }
  .answer-btn { font-size: var(--text-xs); min-height: 56px; }
}
