:root {
  --bg: #f4f9ff;
  --card: #ffffff;
  --primary: #1e3a8a;
  --accent: #0284c7;
  --text: #1f2937;
  --muted: #6b7280;
  --correct: #166534;
  --wrong: #b91c1c;
  --correct-bg: #dcfce7;
  --wrong-bg: #fee2e2;
  --quiz-answer-height: 76px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top, #dbeafe 0%, var(--bg) 45%, #eef2ff 100%);
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.ambient-layer {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--ambient-background, transparent);
  pointer-events: none;
  z-index: 0;
  --ambient-fade-ms: 1200ms;
  --ambient-saturation: 1;
  --ambient-glow-px: 0px;
  --ambient-glow-alpha: 0;
  --ambient-circle-color: rgba(2, 132, 199, 0.34);
  --ambient-square-color: rgba(96, 165, 250, 0.28);
  --ambient-diamond-color: rgba(147, 197, 253, 0.28);
  --ambient-ring-color: rgba(2, 132, 199, 0.34);
  --ambient-triangle-color: rgba(125, 211, 252, 0.32);
}

.ambient-shape {
  position: absolute;
  width: var(--size);
  height: var(--size);
  left: var(--x);
  top: var(--y);
  --start-rot: 0deg;
  --end-rot: 360deg;
  opacity: var(--opacity);
  transition: opacity var(--ambient-fade-ms) ease-in-out;
  animation: ambient-float var(--duration) ease-in-out var(--delay) infinite alternate;
}

.ambient-shape.is-faded {
  opacity: 0;
}

.ambient-shape.circle {
  border-radius: 999px;
  background: var(--ambient-circle-color);
}

.ambient-shape.square {
  border-radius: 8px;
  background: var(--ambient-square-color);
}

.ambient-shape.diamond {
  --start-rot: 45deg;
  --end-rot: 405deg;
  border-radius: 8px;
  background: var(--ambient-diamond-color);
}

.ambient-shape.ring {
  border-radius: 999px;
  border: max(1px, calc(var(--size) * 0.08)) solid var(--ambient-ring-color);
}

.ambient-shape.triangle {
  width: 0;
  height: 0;
  border-left: calc(var(--size) * 0.52) solid transparent;
  border-right: calc(var(--size) * 0.52) solid transparent;
  border-bottom: var(--size) solid var(--ambient-triangle-color);
}

body[data-view] .ambient-layer .ambient-shape {
  filter: saturate(var(--ambient-saturation))
    drop-shadow(0 0 var(--ambient-glow-px) rgba(56, 189, 248, var(--ambient-glow-alpha)));
}

@keyframes ambient-float {
  from {
    transform: translate(0, 0) rotate(var(--start-rot));
  }
  to {
    transform: translate(var(--drift-x), var(--drift-y)) rotate(var(--end-rot));
  }
}

.fireworks-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

.fireworks-layer canvas {
  display: block;
}

.app {
  width: min(760px, 100%);
  position: relative;
  z-index: 2;
}

body[data-view="selection"] .app {
  width: min(1120px, 100%);
}

.app-header {
  text-align: center;
  margin-bottom: 14px;
}

.app-header h1 {
  margin: 0;
  color: var(--primary);
  font-size: clamp(1.7rem, 3.5vw, 2.7rem);
}

.app-header p {
  margin: 6px 0 0;
  color: var(--muted);
}

.toolbar-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

body[data-view="quiz"] .toolbar-row {
  margin-bottom: 10px;
}

.top-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  flex-wrap: wrap;
}

.feedback-language-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.category-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.card-type-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.difficulty-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.question-count-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

body[data-view="quiz"] .category-controls {
  display: none;
}

body[data-view="quiz"] .card-type-controls {
  display: none;
}

body[data-view="quiz"] .difficulty-controls {
  display: none;
}

.question-count-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.category-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.card-type-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.difficulty-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.question-count-select {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 4px 7px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.category-select {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 4px 7px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.card-type-select {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 4px 7px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.difficulty-select {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 4px 7px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.question-count-select:focus {
  outline: 2px solid #bfdbfe;
  outline-offset: 1px;
}

.category-select:focus {
  outline: 2px solid #bfdbfe;
  outline-offset: 1px;
}

.card-type-select:focus {
  outline: 2px solid #bfdbfe;
  outline-offset: 1px;
}

.difficulty-select:focus {
  outline: 2px solid #bfdbfe;
  outline-offset: 1px;
}

.feedback-language-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}

.feedback-language-select {
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 4px 7px;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.feedback-language-select:focus {
  outline: 2px solid #bfdbfe;
  outline-offset: 1px;
}

.refresh-selection-btn {
  border: 1px solid #bae6fd;
  border-radius: 999px;
  background: linear-gradient(180deg, #f0f9ff 0%, #e0f2fe 100%);
  color: #0369a1;
  width: 28px;
  height: 28px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}

.refresh-selection-btn svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.refresh-selection-btn:hover:not(:disabled) {
  border-color: #38bdf8;
  background: linear-gradient(180deg, #e0f2fe 0%, #bae6fd 100%);
}

.refresh-selection-btn:disabled {
  cursor: default;
  opacity: 0.7;
}

.refresh-selection-btn.is-hidden {
  display: none;
}

.quiz-card {
  background: var(--card);
  border-radius: 16px;
  padding: 22px;
  box-shadow: 0 16px 40px rgba(30, 58, 138, 0.15);
}

body[data-view="quiz"] .quiz-card {
  padding: 18px;
}

body[data-view="quiz"] .quiz-card.is-result-view {
  padding: 22px;
}

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

#progress,
#score {
  color: var(--muted);
  font-weight: 700;
}

.score-actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.question {
  margin: 0 0 16px;
  min-height: 3em;
  line-height: 1.4;
  font-size: clamp(1.1rem, 2.5vw, 1.4rem);
  text-align: center;
}

body[data-view="quiz"] .question {
  margin-bottom: 12px;
  min-height: 2.8em;
}

.answers {
  display: grid;
  gap: 12px;
  padding: 4px 2px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

body[data-view="selection"] .answers {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

body[data-view="quiz"] .answers {
  gap: 10px;
}

.loading-state {
  grid-column: 1 / -1;
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 22px;
  border: 1px solid rgba(30, 58, 138, 0.12);
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(239, 246, 255, 0.82) 0%, rgba(255, 255, 255, 0.92) 100%);
}

.loading-spinner {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  align-self: center;
  border-radius: 999px;
  border: 4px solid #bfdbfe;
  border-top-color: var(--accent);
  box-shadow: 0 0 18px rgba(2, 132, 199, 0.18);
  animation: loading-spin 0.8s linear infinite;
}

.loading-copy {
  width: min(560px, 72vw);
  min-width: 0;
  display: grid;
  gap: 5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.92rem;
  color: #334155;
}

.loading-line {
  line-height: 1.35;
  white-space: nowrap;
}

.loading-file {
  width: 100%;
  min-height: 28px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  padding: 5px 9px;
  border-radius: 999px;
  color: #0f172a;
  background: rgba(219, 234, 254, 0.72);
  border: 1px solid rgba(59, 130, 246, 0.18);
}

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

.answer-btn {
  border: 2px solid #bfdbfe;
  background: #eff6ff;
  color: var(--text);
  font-size: 1rem;
  padding: 12px 14px;
  border-radius: 12px;
  text-align: left;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease, box-shadow 0.15s ease;
}

body[data-view="quiz"] .answer-btn {
  height: var(--quiz-answer-height);
  display: flex;
  align-items: center;
}

body[data-view="quiz"] .answer-btn__label {
  width: 100%;
  text-align: left;
  line-height: 1.28;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
}

@media (hover: hover) {
  .answer-btn:hover:not(:disabled) {
    border-color: var(--accent);
    background: #e0f2fe;
  }
}

.answer-btn:disabled {
  cursor: default;
  opacity: 0.92;
}

.answer-btn.correct {
  border-color: #15803d;
  background: linear-gradient(180deg, #ecfdf3 0%, #dcfce7 100%);
  box-shadow: 0 0 0 2px rgba(22, 101, 52, 0.13), 0 2px 6px rgba(22, 101, 52, 0.06);
}

.answer-btn.wrong {
  border-color: #dc2626;
  background: linear-gradient(180deg, #fff5f5 0%, #fee2e2 100%);
  box-shadow: 0 0 0 2px rgba(185, 28, 28, 0.12), 0 2px 6px rgba(185, 28, 28, 0.06);
}

.answer-btn.is-subdued {
  opacity: 0.46;
  filter: grayscale(0.35) saturate(0.82);
}

.quiz-select-btn {
  text-align: left;
  display: grid;
  gap: 4px;
  position: relative;
  border-left-width: 5px;
  padding-right: 52px;
}

body[data-view="selection"] .quiz-select-btn {
  height: 100%;
  align-content: start;
}

.quiz-select-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.quiz-select-badges {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.quiz-select-title {
  font-weight: 700;
  color: var(--primary);
}

.quiz-level-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.quiz-level-default {
  border-left-color: #93c5fd;
}

.quiz-level-easy {
  border-left-color: #5cc8ae;
  background: linear-gradient(180deg, #f2fffa 0%, #ecfeff 100%);
}

.quiz-level-medium {
  border-left-color: #f59e7a;
  background: linear-gradient(180deg, #fff7ed 0%, #fffaf0 100%);
}

.quiz-level-hard {
  border-left-color: #d977a9;
  background: linear-gradient(180deg, #fdf2f8 0%, #fff7fb 100%);
}

.quiz-level-badge-easy {
  color: #0f766e;
  background: #ccfbf1;
}

.quiz-level-badge-medium {
  color: #9a3412;
  background: #ffedd5;
}

.quiz-level-badge-hard {
  color: #9d174d;
  background: #fce7f3;
}

.quiz-level-badge-group {
  color: #334155;
  background: #e2e8f0;
}

.quiz-select-description {
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.35;
  min-height: 2.7em;
}

.quiz-question-count-pill {
  position: absolute;
  right: 10px;
  top: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 24px;
  padding: 0;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  line-height: 1;
}

.quiz-question-count-pill.is-wide-count {
  width: auto;
  min-width: 32px;
  padding: 0 8px;
}

.quiz-question-count-pill-default {
  color: #1e293b;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.45);
}

.quiz-question-count-pill-easy {
  color: #0f766e;
  background: rgba(204, 251, 241, 0.92);
  border: 1px solid rgba(15, 118, 110, 0.28);
}

.quiz-question-count-pill-medium {
  color: #9a3412;
  background: rgba(255, 237, 213, 0.92);
  border: 1px solid rgba(154, 52, 18, 0.26);
}

.quiz-question-count-pill-hard {
  color: #9d174d;
  background: rgba(252, 231, 243, 0.92);
  border: 1px solid rgba(157, 23, 77, 0.24);
}

.feedback {
  min-height: 1.5em;
  margin: 20px 0 24px;
  font-weight: 700;
  text-align: center;
}

.feedback.correct {
  color: #166534;
}

.feedback.wrong {
  color: #b91c1c;
}

.feedback.is-chip-feedback {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

body[data-view="quiz"] .feedback {
  margin: 16px 0 10px;
}

body[data-view="quiz"] .quiz-card.is-result-view .feedback {
  margin: 16px 0 18px;
}

body[data-view="quiz"] .feedback.is-chip-feedback {
  height: 32px;
}

.feedback.is-chip-feedback .feedback-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid transparent;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: transparent;
  background: transparent;
  box-shadow: none;
}

.feedback.is-chip-feedback.correct .feedback-chip {
  color: #166534;
  border: 1px solid rgba(22, 101, 52, 0.22);
  background: linear-gradient(180deg, #f0fdf4 0%, #dcfce7 100%);
  box-shadow: 0 6px 12px rgba(22, 101, 52, 0.09);
}

.feedback.is-chip-feedback.wrong .feedback-chip {
  color: #b91c1c;
  border: 1px solid rgba(185, 28, 28, 0.22);
  background: linear-gradient(180deg, #fff5f5 0%, #fee2e2 100%);
  box-shadow: 0 6px 12px rgba(185, 28, 28, 0.09);
}

.feedback.is-chip-feedback.correct .feedback-chip::before {
  content: "✓";
  font-size: 0.88rem;
}

.feedback.is-chip-feedback.wrong .feedback-chip::before {
  content: "✕";
  font-size: 0.85rem;
}

@media (prefers-reduced-motion: reduce) {
  .answer-btn,
  .feedback {
    transition: none;
  }

  .loading-spinner {
    animation: none;
  }
}

.restart-btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 1rem;
  font-weight: 700;
  background: var(--primary);
  color: white;
  cursor: pointer;
  min-width: 110px;
}

.restart-btn:hover {
  background: #1d4ed8;
}

.secondary-btn {
  background: #0f766e;
}

.secondary-btn:hover {
  background: #0d9488;
}

.icon-btn {
  min-width: 42px;
  width: 42px;
  height: 42px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.actions {
  min-height: 42px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
}

body[data-view="quiz"] .actions {
  min-height: 36px;
}

body[data-view="quiz"] .quiz-card.is-result-view .actions {
  min-height: 42px;
  margin-top: 6px;
}

.is-hidden {
  visibility: hidden;
  pointer-events: none;
}

@media (max-width: 560px) {
  body {
    padding: 0;
  }

  .app {
    width: 100%;
  }

  .quiz-card {
    width: 100%;
    margin: 0;
    padding: 16px;
  }

  .toolbar-row {
    align-items: center;
  }

  #progress,
  #score {
    font-size: 0.92rem;
  }

  .feedback-language-controls {
    margin-left: auto;
  }

  .category-controls {
    margin-left: auto;
  }

  .card-type-controls {
    margin-left: auto;
  }

  .difficulty-controls {
    margin-left: auto;
  }

  .top-controls {
    width: 100%;
    justify-content: flex-end;
    gap: 8px;
  }

  .loading-state {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }

  .loading-copy {
    width: 100%;
  }

  .feedback-language-select {
    max-width: 150px;
  }

  .category-select {
    max-width: 160px;
  }

  .card-type-select {
    max-width: 132px;
  }

  .difficulty-select {
    max-width: 180px;
  }

  .question-count-select {
    max-width: 72px;
  }

  .answers {
    grid-template-columns: 1fr;
  }

  .answer-btn {
    font-size: 0.98rem;
    padding: 11px 12px;
  }

  .quiz-select-btn {
    padding-right: 40px;
  }

  body[data-view="quiz"] .answer-btn {
    height: 74px;
  }

  body[data-view="quiz"] .feedback.is-chip-feedback.wrong {
    height: auto;
    min-height: 32px;
  }

  body[data-view="quiz"] .feedback.is-chip-feedback.wrong .feedback-chip {
    white-space: normal;
    overflow-wrap: anywhere;
    overflow: visible;
    text-overflow: clip;
  }

  .app-header h1 {
    font-size: clamp(1.55rem, 3.1vw, 2.45rem);
    line-height: 1.15;
    overflow-wrap: anywhere;
    text-wrap: balance;
  }

  .quiz-select-title {
    flex: none;
    min-width: 0;
    overflow-wrap: normal;
    word-break: normal;
    hyphens: none;
  }

  .quiz-select-heading {
    display: block;
    min-width: 0;
  }

  .quiz-select-badges {
    margin-top: 4px;
  }

  .quiz-select-description {
    font-size: 0.88rem;
    line-height: 1.3;
    overflow-wrap: break-word;
    hyphens: auto;
  }

  .quiz-question-count-pill {
    width: 27px;
    height: 20px;
    top: -4px;
    right: -4px;
    font-size: 0.62rem;
  }

  .quiz-question-count-pill.is-wide-count {
    min-width: 27px;
    padding: 0 6px;
  }

  .quiz-level-badge {
    font-size: 0.7rem;
    flex-shrink: 0;
    white-space: nowrap;
  }

  .quiz-level-badge-easy,
  .quiz-level-badge-medium,
  .quiz-level-badge-hard {
    min-width: 58px;
    justify-content: center;
  }
}

@media (min-width: 980px) {
  body[data-view="selection"] .answers {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
