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

:root {
  --primary: #6c5ce7;
  --primary-dark: #5a4bd1;
  --danger: #e74c3c;
  --danger-dark: #c0392b;
  --bg: #0f0e17;
  --card-bg: #1a1a2e;
  --surface: #16213e;
  --text: #fffffe;
  --text-muted: #a7a9be;
  --correct: #00c853;
  --incorrect: #ff1744;
  --border: #0f3460;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: clamp(0.75rem, 4vw, 2rem) clamp(0.5rem, 3vw, 1rem);
}

.container {
  width: 100%;
  max-width: min(900px, 100%);
}

header {
  text-align: center;
  margin-bottom: clamp(1rem, 3vw, 2rem);
}

header h1 {
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), #a29bfe);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: clamp(0.75rem, 2vw, 1.5rem);
  line-height: 1.2;
  word-break: break-word;
}

nav {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  background: var(--card-bg);
  padding: 0.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  flex-wrap: wrap;
}

.nav-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 0.6rem clamp(0.8rem, 3vw, 1.5rem);
  border-radius: 8px;
  cursor: pointer;
  font-size: clamp(0.8rem, 2.5vw, 0.95rem);
  font-weight: 600;
  transition: all 0.2s;
  white-space: nowrap;
  flex: 0 1 auto;
}

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

.nav-btn.active {
  color: var(--text);
  background: var(--primary);
}

.nav-btn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.view { display: none; }
.view.active { display: block; }

.hidden { display: none !important; }

.btn-primary {
  display: inline-block;
  background: var(--primary);
  color: var(--text);
  border: none;
  padding: clamp(0.7rem, 2.5vw, 0.9rem) clamp(1.2rem, 4vw, 2rem);
  border-radius: var(--radius);
  font-size: clamp(0.95rem, 2.5vw, 1.1rem);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:active { transform: scale(0.98); }

.quiz-active {
  display: flex;
  flex-direction: column;
}

#btnNext {
  align-self: flex-end;
  width: auto;
}

.btn-danger {
  display: inline-block;
  background: var(--danger);
  color: var(--text);
  border: none;
  padding: 0.6rem 1.5rem;
  border-radius: 8px;
  font-size: clamp(0.8rem, 2.5vw, 0.9rem);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn-danger:hover { background: var(--danger-dark); }

/* Quiz Start */
.quiz-start {
  text-align: center;
  background: var(--card-bg);
  padding: clamp(1.5rem, 6vw, 3rem) clamp(1rem, 4vw, 2rem);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.quiz-start h2 {
  font-size: clamp(1.3rem, 5vw, 1.8rem);
  margin-bottom: 0.75rem;
}

.quiz-start p {
  color: var(--text-muted);
  margin-bottom: clamp(1.2rem, 4vw, 2rem);
  line-height: 1.6;
  font-size: clamp(0.9rem, 2.5vw, 1rem);
}

/* Quiz Active */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--card-bg);
  padding: clamp(0.6rem, 2vw, 1rem) clamp(0.8rem, 3vw, 1.5rem);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  font-weight: 600;
  gap: 0.5rem;
}

.progress {
  color: var(--text-muted);
  font-size: clamp(0.8rem, 2.5vw, 1rem);
  flex-shrink: 0;
}

.timer-wrap {
  position: relative;
  width: clamp(64px, 16vw, 100px);
  height: 28px;
  background: var(--surface);
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 1px solid var(--border);
}

.timer-bar {
  position: absolute;
  inset: 0;
  background: var(--primary);
  border-radius: 16px;
  transition: width 0.4s linear, background 0.3s;
  width: 100%;
}

.timer-bar.warning { background: #f39c12; }
.timer-bar.danger { background: var(--danger); animation: pulse 0.5s infinite; }

.timer-text {
  position: relative;
  z-index: 1;
  color: var(--text);
  font-size: clamp(0.85rem, 2.5vw, 1rem);
  font-weight: 800;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

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

.score {
  color: var(--text-muted);
  font-size: clamp(0.8rem, 2.5vw, 1rem);
  flex-shrink: 0;
}

.question-card {
  background: var(--card-bg);
  padding: clamp(1rem, 4vw, 2rem);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

.question-text {
  font-size: clamp(1.05rem, 3.5vw, 1.3rem);
  font-weight: 600;
  margin-bottom: clamp(1rem, 3vw, 1.5rem);
  line-height: 1.5;
  word-break: break-word;
}

.choices {
  display: flex;
  flex-direction: column;
  gap: clamp(0.5rem, 2vw, 0.8rem);
}

.choice-btn {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 2px solid transparent;
  padding: clamp(0.7rem, 2.5vw, 1rem) clamp(0.8rem, 3vw, 1.2rem);
  border-radius: 10px;
  font-size: clamp(0.9rem, 2.5vw, 1rem);
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.choice-btn:hover {
  border-color: var(--primary);
  background: rgba(108, 92, 231, 0.1);
}

.choice-btn.correct {
  border-color: var(--correct);
  background: rgba(0, 200, 83, 0.15);
}

.choice-btn.incorrect {
  border-color: var(--incorrect);
  background: rgba(255, 23, 68, 0.15);
}

.choice-btn:disabled {
  cursor: not-allowed;
  opacity: 0.8;
}

.choice-letter {
  display: inline-block;
  width: clamp(24px, 6vw, 28px);
  height: clamp(24px, 6vw, 28px);
  line-height: clamp(24px, 6vw, 28px);
  text-align: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  margin-right: clamp(0.5rem, 2vw, 0.8rem);
  font-weight: 700;
  font-size: clamp(0.75rem, 2vw, 0.85rem);
  flex-shrink: 0;
}

.choice-btn.correct .choice-letter { background: var(--correct); color: #fff; }
.choice-btn.incorrect .choice-letter { background: var(--incorrect); color: #fff; }

.name-form {
  margin-bottom: 1.5rem;
}

.name-form label {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
  font-size: clamp(0.85rem, 2.5vw, 0.95rem);
}

.name-form-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.name-form-row input {
  flex: 1 1 140px;
  min-width: 0;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

.name-form-row input:focus {
  border-color: var(--primary);
}

.name-form-row .btn-primary {
  padding: 0.8rem 1.5rem;
  font-size: 0.95rem;
  flex: 0 0 auto;
}

/* Quiz Result */
.quiz-result {
  text-align: center;
  background: var(--card-bg);
  padding: clamp(1.5rem, 6vw, 3rem) clamp(1rem, 4vw, 2rem);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.quiz-result h2 {
  font-size: clamp(1.4rem, 5vw, 2rem);
  margin-bottom: clamp(0.75rem, 3vw, 1.5rem);
}

.result-card {
  margin-bottom: clamp(1rem, 4vw, 2rem);
}

.result-score {
  font-size: clamp(1.1rem, 3.5vw, 1.4rem);
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.result-score span {
  color: var(--text);
  font-weight: 800;
}

.result-percent {
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), #a29bfe);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* Leaderboard */
#viewLeaderboard {
  background: var(--card-bg);
  padding: clamp(1rem, 4vw, 2rem);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

#viewLeaderboard h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  font-size: clamp(1.2rem, 4vw, 1.5rem);
}

.leaderboard-list {
  margin-bottom: 1.5rem;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem;
  font-size: clamp(0.85rem, 2.5vw, 1rem);
}

.leaderboard-item {
  display: flex;
  align-items: center;
  padding: 0.8rem 1rem;
  border-radius: 8px;
  background: var(--surface);
  margin-bottom: 0.5rem;
  gap: clamp(0.5rem, 2vw, 1rem);
  flex-wrap: wrap;
}

.leaderboard-rank {
  font-weight: 800;
  font-size: clamp(1rem, 3vw, 1.1rem);
  color: var(--primary);
  min-width: clamp(24px, 6vw, 30px);
}

.leaderboard-name {
  flex: 1 1 80px;
  min-width: 0;
  font-weight: 600;
  font-size: clamp(0.85rem, 2.5vw, 1rem);
  word-break: break-word;
}

.leaderboard-score {
  font-weight: 700;
  color: var(--text-muted);
  font-size: clamp(0.8rem, 2.5vw, 0.95rem);
  white-space: nowrap;
}

.leaderboard-date {
  font-size: clamp(0.7rem, 2vw, 0.8rem);
  color: var(--text-muted);
  margin-left: auto;
}

/* Creator */
#viewCreator {
  background: var(--card-bg);
  padding: clamp(1rem, 4vw, 2rem);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

#viewCreator h2 {
  text-align: center;
  margin-bottom: 0.5rem;
  font-size: clamp(1.2rem, 4vw, 1.5rem);
}

.creator-info {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  font-size: clamp(0.85rem, 2.5vw, 0.95rem);
}

.creator-upload {
  text-align: center;
  margin-bottom: 1.5rem;
}

.upload-label {
  display: inline-block;
  background: var(--surface);
  color: var(--primary);
  padding: 0.8rem clamp(1rem, 4vw, 2rem);
  border-radius: 8px;
  border: 2px dashed var(--primary);
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  font-size: clamp(0.85rem, 2.5vw, 0.95rem);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  width: 100%;
  max-width: 300px;
}

.upload-label:hover {
  background: rgba(108, 92, 231, 0.1);
}

.upload-label input[type="file"] {
  display: none;
}

.creator-editor textarea {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: clamp(0.75rem, 2.5vw, 0.9rem);
  resize: vertical;
  margin-bottom: 1rem;
  min-height: 150px;
}

.creator-editor textarea:focus {
  outline: none;
  border-color: var(--primary);
}

#viewCreator .btn-primary {
  width: 100%;
}

/* Small phones (320px - 400px) */
@media (max-width: 400px) {
  body {
    padding: 0.1rem 0.4rem;
  }

  .container {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
  }

  main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  #viewQuiz.view.active {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
    margin-bottom: 0;
  }

  header h1 {
    margin-bottom: 0.15rem;
  }

  nav {
    gap: 0.3rem;
    padding: 0.4rem;
    margin-top: 0.5rem;
  }

  .nav-btn {
    padding: 0.5rem 0.7rem;
    font-size: 0.78rem;
  }

  .quiz-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    justify-content: unset;
    padding: 0.4rem 0.5rem;
    gap: 0.3rem;
  }

  .progress { text-align: left; }
  .timer-wrap { justify-self: center; }
  .score { text-align: right; }

  .leaderboard-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.3rem;
  }

  .leaderboard-date {
    margin-left: 0;
  }

  .name-form-row {
    flex-direction: column;
  }

  .name-form-row input {
    flex: 1 1 auto;
    width: 100%;
  }

  .name-form-row .btn-primary {
    width: 100%;
  }
}

/* Medium phones (400px - 480px) */
@media (min-width: 401px) and (max-width: 480px) {
  body {
    padding: 0.1rem 0.5rem;
  }

  .container {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
  }

  main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  #viewQuiz.view.active {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
    margin-bottom: 0;
  }

  header h1 {
    margin-bottom: 0.2rem;
  }

  nav {
    margin-top: 0.5rem;
  }

  .quiz-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    justify-content: unset;
    gap: 0.3rem;
  }

  .progress { text-align: left; }
  .timer-wrap { justify-self: center; }
  .score { text-align: right; }

  .leaderboard-item {
    gap: 0.5rem;
  }

  .name-form-row {
    flex-wrap: nowrap;
  }
}

/* Large phones / small tablets (481px - 600px) */
@media (min-width: 481px) and (max-width: 600px) {
  body {
    padding: 0.2rem 0.75rem;
  }

  .container {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
  }

  main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  #viewQuiz.view.active {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
    margin-bottom: 0;
  }

  header h1 {
    margin-bottom: 0.3rem;
  }

  nav {
    margin-top: 0.5rem;
  }

  .quiz-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    justify-content: unset;
  }

  .progress { text-align: left; }
  .timer-wrap { justify-self: center; }
  .score { text-align: right; }
}

/* Tablets (601px - 768px) */
@media (min-width: 601px) and (max-width: 768px) {
  .container {
    max-width: 700px;
  }
}

/* Large tablets and desktops (769px+) */
@media (min-width: 769px) {
  .container {
    max-width: 900px;
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  .nav-btn,
  .choice-btn,
  .btn-primary,
  .btn-danger,
  .upload-label {
    cursor: default;
  }

  .choice-btn {
    padding-top: clamp(0.8rem, 3vw, 1rem);
    padding-bottom: clamp(0.8rem, 3vw, 1rem);
  }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Landscape mobile optimization */
@media (max-height: 500px) and (orientation: landscape) {
  body {
    padding: 0.4rem;
  }

  header {
    margin-bottom: 0.5rem;
  }

  header h1 {
    margin-bottom: 0.5rem;
    font-size: 1.2rem;
  }

  nav {
    padding: 0.3rem;
  }

  .quiz-start {
    padding: 1rem;
  }

  .quiz-header {
    padding: 0.4rem 0.6rem;
    margin-bottom: 0.5rem;
  }

  .question-card {
    padding: 0.8rem;
    margin-bottom: 0.5rem;
  }

  .choice-btn {
    padding: 0.5rem 0.8rem;
  }
}
