/* ===================================================================
   SparkQuiz — design tokens
   Light: warm violet-on-white, soft lilac surfaces.
   Dark:  deep indigo-black, glowing violet accent.
   =================================================================== */

:root {
  --bg: #F6F5FC;
  --surface: #FFFFFF;
  --surface-2: #EFEDFA;
  --text: #1B1B2F;
  --text-soft: #63637A;
  --border: #E3E0F4;

  --primary: #6C5CE7;
  --primary-dark: #5647c9;
  --primary-tint: #EDE9FE;
  --accent: #FDCB6E;
  --accent-dark: #E8A93C;
  --correct: #00B894;
  --correct-tint: #DAF7EE;
  --wrong: #FF5252;
  --wrong-tint: #FFE3E3;

  --shadow: 0 10px 30px -12px rgba(108, 92, 231, 0.28);
  --shadow-sm: 0 4px 14px -6px rgba(27, 27, 47, 0.12);
  --shadow-lg: 0 20px 50px -20px rgba(27, 27, 47, 0.25);
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;

  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #100F1B;
  --surface: #1B1A2C;
  --surface-2: #242338;
  --text: #EDECF7;
  --text-soft: #9A98B5;
  --border: #302F47;

  --primary: #8C7CF0;
  --primary-dark: #A79BFF;
  --primary-tint: #2A2650;
  --accent: #FDCB6E;
  --accent-dark: #FFD98A;
  --correct: #35D399;
  --correct-tint: #123527;
  --wrong: #FF6B6B;
  --wrong-tint: #3A1A1F;

  --shadow: 0 10px 34px -10px rgba(0, 0, 0, 0.55);
  --shadow-sm: 0 4px 16px -6px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 24px 60px -20px rgba(0, 0, 0, 0.65);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 88px;
  scroll-padding-bottom: 96px;
}

/* The top bar collapses away during a quiz attempt, so there's no longer
   anything up there to clear space for. */
body:has(#view-quiz:not([hidden])) {
  scroll-padding-top: 20px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease, color 0.25s ease;
}

button { font-family: inherit; }

/* Gentle fade whenever a view becomes visible — replaces the old hard cut */
.view:not([hidden]) {
  animation: viewFade 0.28s ease;
}
@keyframes viewFade {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ------------------------------ Top bar ------------------------------ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px clamp(20px, 4vw, 64px);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--border);
  max-height: 88px;
  opacity: 1;
  overflow: hidden;
  transition: max-height 0.32s ease, opacity 0.22s ease, padding 0.32s ease, border-color 0.32s ease;
}

/* While a quiz is being attempted, tuck the brand bar away entirely so the
   question gets the full screen — the in-quiz header still has its own
   "← Exit" control, so nothing is lost. */
body:has(#view-quiz:not([hidden])) .topbar {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  opacity: 0;
  border-bottom-color: transparent;
  pointer-events: none;
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 0;
}

.topbar__mark {
  font-size: 22px;
  filter: drop-shadow(0 2px 6px rgba(253, 203, 110, 0.5));
}

.topbar__name {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text);
}

.topbar__actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--primary); transform: translateY(-1px); }
.icon-btn:active { transform: translateY(0); }

/* ------------------------------ Layout ------------------------------ */

main#app {
  max-width: 1520px;
  margin: 0 auto;
  padding: 28px clamp(20px, 4vw, 64px) 60px;
}

.view[hidden] { display: none; }

.hero { margin-bottom: 32px; max-width: 60ch; }
.hero--compact { margin-bottom: 24px; }

.crumb-row { margin-bottom: 10px; }
.crumb-row .text-btn { padding-left: 0; }

.hero h1 {
  font-size: clamp(1.6rem, 3.4vw, 2.2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
}

.hero p {
  color: var(--text-soft);
  margin: 0;
  font-size: 1rem;
}

/* ------------------------------ Subject cards ------------------------------ */

.subject-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 18px;
}

.subject-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  cursor: pointer;
  text-align: left;
  overflow: hidden;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.subject-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow);
  border-color: color-mix(in srgb, var(--card-color, var(--primary)) 45%, var(--border));
}

.subject-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--card-color, var(--primary));
  opacity: 0.08;
}

.subject-card__icon {
  font-size: 30px;
  display: inline-block;
  margin-bottom: 14px;
}

.subject-card__name {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0 0 6px;
  position: relative;
}

.subject-card__desc {
  font-size: 0.88rem;
  color: var(--text-soft);
  margin: 0 0 14px;
  position: relative;
  min-height: 2.6em;
}

.subject-card__count {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--card-color, var(--primary));
  position: relative;
}

.skeleton-card {
  height: 168px;
  border-radius: var(--radius-lg);
  background: linear-gradient(100deg, var(--surface) 30%, var(--surface-2) 50%, var(--surface) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-soft);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
}

/* =====================================================================
   Quiz view — two-column shell: question column + palette sidebar
   ===================================================================== */

.quiz-shell {
  max-width: 100%;
}

.quiz-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  align-items: start;
  gap: 36px;
}

.quiz-column {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* ------------------------------ Quiz header ------------------------------ */

.quiz-header {
  margin-bottom: 16px;
}

.quiz-header__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.quiz-header__top h2 {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
  text-align: center;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 8px;
}

.text-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background 0.15s ease;
  white-space: nowrap;
}
.text-btn:hover { background: var(--primary-tint); }

/* On desktop the palette is always visible as a sidebar, so the header toggle
   button is redundant there — it only earns its place back on small screens. */
.palette-toggle { display: none; }

.progress-track {
  height: 8px;
  border-radius: 99px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 10%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 99px;
  transition: width 0.35s ease;
}

.quiz-header__meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.82rem;
  color: var(--text-soft);
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
}

/* ------------------------------ Question card ------------------------------ */

.question-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 26px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 16px;
}

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 99px;
  background: var(--primary-tint);
  color: var(--primary);
  margin-bottom: 12px;
}

.question-text {
  font-size: 1.15rem;
  line-height: 1.6;
  font-weight: 500;
  margin: 0 0 18px;
  white-space: pre-line;
  max-width: 80ch;
}

.table-wrap {
  overflow-x: auto;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.table-wrap table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.92rem;
}

.table-wrap th, .table-wrap td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.table-wrap th {
  background: var(--surface-2);
  font-weight: 700;
}

.table-wrap tr:last-child td { border-bottom: none; }

.options-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

/* Two columns once there's enough room, so four short options take half
   the vertical space instead of stacking the whole way down the screen. */
@media (min-width: 760px) {
  .options-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

.option-btn {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  text-align: left;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  line-height: 1.55;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}

.option-btn:hover:not(:disabled) {
  border-color: var(--primary);
  background: var(--primary-tint);
  box-shadow: var(--shadow-sm);
}

.option-btn:active:not(:disabled) { transform: scale(0.99); }

.option-btn__marker {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  display: grid;
  place-items: center;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-soft);
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

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

.option-btn.is-correct {
  border-color: var(--correct);
  background: var(--correct-tint);
}
.option-btn.is-correct .option-btn__marker {
  border-color: var(--correct);
  background: var(--correct);
  color: #fff;
}

.option-btn.is-wrong {
  border-color: var(--wrong);
  background: var(--wrong-tint);
}
.option-btn.is-wrong .option-btn__marker {
  border-color: var(--wrong);
  background: var(--wrong);
  color: #fff;
}

.option-btn.is-dimmed { opacity: 0.55; }

.solution-box {
  margin-top: 20px;
  padding: 16px 18px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
  border-left: 4px solid var(--primary);
  animation: reveal 0.25s ease;
}

@keyframes reveal {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.solution-box__label {
  font-weight: 800;
  font-size: 0.85rem;
  margin-bottom: 6px;
  letter-spacing: -0.01em;
}
.solution-box__label.is-correct { color: var(--correct); }
.solution-box__label.is-wrong { color: var(--wrong); }

.solution-box p {
  margin: 0;
  color: var(--text-soft);
  font-size: 0.95rem;
  line-height: 1.65;
}

/* ------------------------------ Nav bar ------------------------------ */

.quiz-nav {
  position: sticky;
  bottom: 0;
  z-index: 5;
  display: flex;
  gap: 12px;
  padding: 16px 0;
  margin-top: 4px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  box-shadow: 0 -8px 20px -12px rgba(0, 0, 0, 0.12);
}

.btn {
  flex: 1;
  padding: 14px 18px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 0.98rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease, box-shadow 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--primary-dark); box-shadow: var(--shadow-sm); }

.btn--accent { background: var(--accent); color: #241a00; }
.btn--accent:hover:not(:disabled) { background: var(--accent-dark); box-shadow: var(--shadow-sm); }

.btn--ghost {
  background: var(--surface);
  color: var(--text);
  border: 1.5px solid var(--border);
}
.btn--ghost:hover:not(:disabled) { background: var(--surface-2); }

/* ------------------------------ Result view ------------------------------ */

.result-card {
  max-width: 420px;
  margin: 30px auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px 28px;
  text-align: center;
  box-shadow: var(--shadow-sm);
}

.result-ring {
  width: 132px;
  height: 132px;
  margin: 0 auto 20px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: conic-gradient(var(--primary) var(--pct, 70%), var(--surface-2) 0);
}

.result-ring span {
  display: grid;
  place-items: center;
  width: 104px;
  height: 104px;
  border-radius: 50%;
  background: var(--surface);
  font-size: 1.3rem;
  font-weight: 800;
}

.result-card h2 {
  margin: 0 0 20px;
  font-size: 1.3rem;
}

.result-stats {
  display: flex;
  justify-content: center;
  gap: 22px;
  margin-bottom: 26px;
}

.stat { display: flex; flex-direction: column; align-items: center; }
.stat span { font-size: 1.3rem; font-weight: 800; }
.stat small { color: var(--text-soft); font-size: 0.78rem; }
.stat--correct span { color: var(--correct); }
.stat--wrong span { color: var(--wrong); }
.stat--skip span { color: var(--text-soft); }

.result-actions { display: flex; gap: 12px; }

/* ------------------------------ History ------------------------------ */

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.history-item:hover { border-color: var(--primary); }

.history-item__icon {
  font-size: 22px;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--primary-tint);
  display: grid;
  place-items: center;
  flex: none;
}

.history-item__body { flex: 1; min-width: 0; }
.history-item__title { font-weight: 700; margin: 0 0 3px; }
.history-item__meta { font-size: 0.82rem; color: var(--text-soft); margin: 0; }

.history-item__score {
  font-weight: 800;
  font-size: 1.05rem;
  flex: none;
}

/* ------------------------------ Palette panel ------------------------------
   Desktop: a persistent, always-visible sidebar card next to the question.
   Mobile:  reverts to a full-screen dimmed overlay with a slide-up drawer,
            toggled by the "Palette" button in the quiz header.
   ---------------------------------------------------------------------- */

.palette-panel {
  position: sticky;
  top: 24px;
}

/* Ignore the `hidden` attribute on desktop — the sidebar always shows */
.palette-panel[hidden] {
  display: block;
}

.palette-drawer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  max-height: calc(100vh - 56px);
  overflow-y: auto;
}

.palette-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.palette-drawer__header h3 { margin: 0; font-size: 1.02rem; }

/* The close button only makes sense inside the mobile drawer */
#closePaletteBtn { display: none; }

.palette-legend {
  display: flex;
  gap: 14px;
  font-size: 0.78rem;
  color: var(--text-soft);
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.palette-legend span { display: flex; align-items: center; gap: 6px; }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot--answered { background: var(--correct); }
.dot--current { background: var(--primary); }
.dot--unanswered { background: var(--border); }

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

.palette-cell {
  aspect-ratio: 1;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.palette-cell:hover { border-color: var(--primary); transform: translateY(-1px); }

.palette-cell.is-answered { background: var(--correct-tint); border-color: var(--correct); color: var(--correct); }
.palette-cell.is-current { background: var(--primary); border-color: var(--primary); color: #fff; }

/* ------------------------------ Subject choice modal ------------------------------ */

.choice-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 8, 30, 0.45);
  z-index: 45;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.choice-overlay[hidden] { display: none; }

.choice-modal {
  width: min(400px, 100%);
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow-lg);
  animation: popIn 0.18s ease;
}

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

.choice-modal h3 {
  margin: 0 0 4px;
  font-size: 1.15rem;
  font-weight: 800;
}

.choice-modal__sub {
  margin: 0 0 18px;
  color: var(--text-soft);
  font-size: 0.9rem;
}

.choice-option {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  text-align: left;
  padding: 14px 16px;
  margin-bottom: 10px;
  border-radius: var(--radius-md);
  border: 1.5px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}
.choice-option:hover { border-color: var(--primary); background: var(--primary-tint); }
.choice-option:active { transform: scale(0.99); }

.choice-option__icon {
  font-size: 22px;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
}

.choice-option__text { display: flex; flex-direction: column; color: var(--text); }
.choice-option__text strong { font-size: 0.96rem; font-weight: 700; }
.choice-option__text small { color: var(--text-soft); font-size: 0.8rem; margin-top: 2px; }

#closeChoiceBtn { display: block; margin: 4px auto 0; }

/* Small "revision" tag on history items */
.history-item__tag {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 99px;
  background: var(--accent);
  color: #241a00;
  flex: none;
}

/* ------------------------------ Responsive ------------------------------ */

/* Below this width there isn't room for a 296px sidebar alongside a
   comfortable reading column, so the palette becomes a full-screen
   drawer instead, opened via the header's "Palette" button. */
@media (max-width: 900px) {
  .quiz-layout {
    display: block;
  }

  .palette-toggle { display: inline-flex; }

  .palette-panel {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: rgba(10, 8, 30, 0.45);
  }
  .palette-panel[hidden] { display: none; }

  .palette-drawer {
    width: 100%;
    max-width: 480px;
    max-height: 78vh;
    border-radius: 20px 20px 0 0;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.22s ease;
  }
  @keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  #closePaletteBtn { display: grid; }
}

@media (max-width: 640px) {
  main#app { padding: 24px 16px 88px; }
  .topbar { padding: 14px 18px; }
  .question-card { padding: 20px; }
  .question-text { font-size: 1.1rem; }
  .result-stats { gap: 14px; }
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* Visible keyboard focus */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
