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

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #0f3460;
  --accent: #e94560;
  --accent-hover: #c73652;
  --success: #4ade80;
  --error: #f87171;
  --text-primary: #eaeaea;
  --text-secondary: #a0aec0;
  --border: #2d4a7a;
  --option-bg: #1e3a5f;
  --option-hover: #2a4f80;
  --option-selected: #2d5986;
}

html {
  height: 100%;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

#app {
  width: 100%;
  max-width: 680px;
}

/* ===== Screens ===== */
.screen {
  display: none;
  animation: fadeIn 0.3s ease;
}

.screen.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Card ===== */
.card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

/* ===== Header ===== */
.app-header {
  text-align: center;
  margin-bottom: 2rem;
}

.app-header .logo {
  font-size: 3rem;
  margin-bottom: 0.5rem;
}

.app-header h1 {
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.app-header p {
  color: var(--text-secondary);
  margin-top: 0.5rem;
}

/* ===== Section Labels ===== */
.section-label {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-bottom: 0.75rem;
}

/* ===== Quiz List ===== */
.quiz-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1.5rem;
}

.quiz-item {
  background: var(--bg-card);
  border: 2px solid transparent;
  border-radius: 0.75rem;
  padding: 1rem 1.25rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.quiz-item:hover {
  background: var(--option-hover);
  transform: translateX(4px);
}

.quiz-item.selected {
  border-color: var(--accent);
  background: var(--option-selected);
}

.quiz-item .quiz-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.quiz-item .quiz-info {
  flex: 1;
  min-width: 0;
}

.quiz-item .quiz-title {
  font-weight: 600;
  font-size: 0.95rem;
}

.quiz-item .quiz-desc {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 0.2rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.quiz-item .quiz-count {
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 600;
  flex-shrink: 0;
}

/* ===== Divider ===== */
.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1.25rem 0;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ===== Upload Area ===== */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: 0.75rem;
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  margin-bottom: 1.5rem;
}

.upload-area:hover,
.upload-area.drag-over {
  border-color: var(--accent);
  background: rgba(233, 69, 96, 0.05);
}

.upload-area .upload-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.upload-area p {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.upload-area .upload-filename {
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 600;
  margin-top: 0.4rem;
}

#file-input {
  display: none;
}

/* ===== Count Selector ===== */
.count-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.count-row label {
  color: var(--text-secondary);
  font-size: 0.9rem;
  white-space: nowrap;
}

.count-input-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.count-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-primary);
  width: 2rem;
  height: 2rem;
  border-radius: 0.4rem;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.count-btn:hover {
  background: var(--option-hover);
}

#question-count {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-primary);
  width: 4rem;
  height: 2rem;
  border-radius: 0.4rem;
  text-align: center;
  font-size: 1rem;
}

#question-count:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.count-hint {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-left: auto;
}

/* ===== Input Mode Toggle ===== */
#input-mode-wrap {
  margin-bottom: 1.5rem;
}

.input-mode-options {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.input-mode-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  padding: 0.6rem 1rem;
  border: 2px solid var(--border);
  border-radius: 0.625rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
  transition: border-color 0.15s, color 0.15s, background 0.15s;
  user-select: none;
}

.input-mode-label:has(input:checked) {
  border-color: var(--accent);
  color: var(--text-primary);
  background: rgba(233, 69, 96, 0.08);
}

.input-mode-label input[type="radio"] {
  accent-color: var(--accent);
}

/* ===== Buttons ===== */
.btn-primary {
  display: block;
  width: 100%;
  padding: 0.875rem 1.5rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 0.75rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  letter-spacing: 0.02em;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1.25rem;
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 0.625rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.btn-secondary:hover {
  background: var(--bg-card);
  color: var(--text-primary);
}

/* ===== Quiz Screen ===== */
.btn-back {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 0.4rem;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
  white-space: nowrap;
}

.btn-back:hover {
  color: var(--text-primary);
  background: var(--bg-card);
}

.quiz-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 1rem;
}

.quiz-title-bar {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.progress-text {
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}

.progress-bar-wrap {
  width: 100%;
  height: 6px;
  background: var(--bg-card);
  border-radius: 99px;
  margin-bottom: 1.5rem;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.3s ease;
}

.question-text {
  font-size: 1.15rem;
  font-weight: 600;
  line-height: 1.5;
  margin-bottom: 1.5rem;
  color: var(--text-primary);
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.option-btn {
  background: var(--option-bg);
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  padding: 0.875rem 1.25rem;
  text-align: left;
  color: var(--text-primary);
  font-size: 0.95rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.option-btn:hover:not(:disabled) {
  background: var(--option-hover);
  border-color: var(--accent);
  transform: translateX(4px);
}

.option-btn:disabled {
  cursor: default;
}

.option-letter {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--accent);
  min-width: 1.2rem;
}

.option-btn.correct {
  background: rgba(74, 222, 128, 0.15);
  border-color: var(--success);
}

.option-btn.correct .option-letter {
  color: var(--success);
}

.option-btn.wrong {
  background: rgba(248, 113, 113, 0.15);
  border-color: var(--error);
}

.option-btn.wrong .option-letter {
  color: var(--error);
}

/* ===== Feedback Area ===== */
.feedback-area {
  margin-top: 1.25rem;
  padding: 1rem 1.25rem;
  border-radius: 0.75rem;
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  animation: fadeIn 0.2s ease;
}

.feedback-area.correct-feedback {
  background: rgba(74, 222, 128, 0.1);
  border: 1px solid rgba(74, 222, 128, 0.3);
}

.feedback-area.wrong-feedback {
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.3);
}

.feedback-icon {
  font-size: 1.25rem;
  flex-shrink: 0;
}

.feedback-text {
  flex: 1;
}

.feedback-text strong {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 0.2rem;
}

.feedback-text .correct-answer {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.next-btn-wrap {
  margin-top: 1.25rem;
  text-align: right;
}

/* ===== Free Text Input (quiz screen) ===== */
.text-input-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.answer-input {
  width: 10rem;
  background: var(--option-bg);
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  padding: 0.875rem 1.25rem;
  color: var(--text-primary);
  font-size: 1.4rem;
  font-weight: 600;
  text-align: center;
  transition: border-color 0.15s;
}

.answer-input:focus {
  outline: none;
  border-color: var(--accent);
}

.answer-input:disabled {
  cursor: default;
}

.answer-input.correct {
  background: rgba(74, 222, 128, 0.15);
  border-color: var(--success);
}

.answer-input.wrong {
  background: rgba(248, 113, 113, 0.15);
  border-color: var(--error);
}

.text-input-wrap .btn-primary {
  width: auto;
  min-width: 10rem;
}

/* ===== Result Screen ===== */
.result-header {
  text-align: center;
  margin-bottom: 2rem;
}

.result-emoji {
  font-size: 4rem;
  display: block;
  margin-bottom: 0.75rem;
}

.result-header h2 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--accent);
}

.result-header p {
  color: var(--text-secondary);
  margin-top: 0.4rem;
}

.score-circle-wrap {
  display: flex;
  justify-content: center;
  margin: 1.5rem 0;
}

.score-circle {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--pct) * 1%), var(--bg-card) 0);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.score-circle::before {
  content: '';
  position: absolute;
  width: 110px;
  height: 110px;
  border-radius: 50%;
  background: var(--bg-secondary);
}

.score-text {
  position: relative;
  z-index: 1;
  text-align: center;
}

.score-text .score-num {
  display: block;
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1;
}

.score-text .score-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin: 1.5rem 0;
}

.stat-card {
  background: var(--bg-card);
  border-radius: 0.75rem;
  padding: 1rem;
  text-align: center;
}

.stat-card .stat-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
}

.stat-card .stat-key {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 0.2rem;
}

.stat-card.correct .stat-value { color: var(--success); }
.stat-card.wrong   .stat-value { color: var(--error); }
.stat-card.total   .stat-value { color: var(--accent); }

.result-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

.result-actions .btn-primary {
  flex: 1;
}

/* ===== Error Banner ===== */
.error-banner {
  background: rgba(248, 113, 113, 0.15);
  border: 1px solid rgba(248, 113, 113, 0.4);
  border-radius: 0.6rem;
  padding: 0.75rem 1rem;
  font-size: 0.875rem;
  color: var(--error);
  margin-bottom: 1rem;
  display: none;
}

.error-banner.visible {
  display: block;
  animation: fadeIn 0.2s ease;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .card {
    padding: 1.25rem;
  }
  .app-header h1 {
    font-size: 1.5rem;
  }
  .stats-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
  }
  .stat-card {
    padding: 0.75rem 0.5rem;
  }
  .result-actions {
    flex-direction: column;
  }
}

/* ===== Melody Quiz ===== */
.melody-container {
  margin-bottom: 1.5rem;
}

.melody-staff {
  background: #0b1524;
  border-radius: 0.75rem;
  padding: 12px 8px;
  padding-inline-end: 20px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.melody-staff svg {
  display: block;
  width: 100%;
  height: auto;
  min-width: 400px;
}

.melody-play-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin: 12px auto 0;
  padding: 0.65rem 1.5rem;
  background: var(--bg-card);
  color: var(--text-primary);
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  min-height: 44px;
  min-width: 44px;
}

.melody-play-btn:hover {
  border-color: var(--accent);
  background: var(--option-hover);
}

.melody-play-btn.playing {
  border-color: var(--accent);
  background: rgba(233, 69, 96, 0.15);
  color: var(--accent);
}
