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

:root {
  --bg: #fdf8ff;
  --surface: #ffffff;
  --card: #f3e8ff;
  --border: rgba(139, 92, 246, 0.15);
  --a: #f43f5e;
  --b: #7c3aed;
  --c: #0ea5e9;
  --d: #10b981;
  --text: #1e1b4b;
  --muted: #7c6fa0;
  --accent: #8b5cf6;
  --radius: 10px;
  --radius-lg: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* ── Typography ── */
h1 { font-size: 2rem; font-weight: 800; }
h2 { font-size: 1.5rem; font-weight: 700; }
h3 { font-size: 1.05rem; font-weight: 600; }

.logo {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: -1px;
  background: linear-gradient(135deg, #f43f5e, #8b5cf6, #06b6d4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ── Layout ── */
.page {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.5rem;
}

.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 1rem;
  text-align: center;
  padding: 1.5rem;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius);
  border: none;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  white-space: nowrap;
  text-decoration: none;
}

.btn:hover { opacity: 0.85; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-success { background: #10b981; color: #fff; }
.btn-danger  { background: #e74c3c; color: #fff; }
.btn-ghost   { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-sm      { padding: 0.35rem 0.75rem; font-size: 0.8rem; }
.btn-lg      { padding: 0.85rem 2rem; font-size: 1rem; border-radius: var(--radius-lg); }
.btn-full    { width: 100%; justify-content: center; }

/* ── Forms ── */
input[type="text"], input[type="number"], input[type="password"],
textarea, select {
  width: 100%;
  padding: 0.65rem 0.9rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}

input:focus, textarea:focus, select:focus { border-color: var(--accent); }
input::placeholder, textarea::placeholder { color: var(--muted); }
textarea { resize: vertical; min-height: 70px; }
select option { background: var(--card); }

.form-group { margin-bottom: 0.85rem; }
.form-group label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

/* ── Answer Color Classes ── */
.answer-a { --clr: var(--a); }
.answer-b { --clr: var(--b); }
.answer-c { --clr: var(--c); }
.answer-d { --clr: var(--d); }

/* ── Student Answer Buttons ── */
.answer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  width: 100%;
}

.answer-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  border-radius: var(--radius-lg);
  border: none;
  background: var(--clr);
  color: #fff;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s, filter 0.2s;
  min-height: 96px;
}

.answer-btn:hover:not(:disabled) { transform: scale(1.02); }
.answer-btn:active:not(:disabled) { transform: scale(0.98); }
.answer-btn:disabled { cursor: not-allowed; }

.answer-btn .opt-lbl {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  background: rgba(0,0,0,0.25);
  border-radius: 50%;
  font-size: 1.6rem;
  font-weight: 900;
  flex-shrink: 0;
}

.answer-btn.selected  { outline: 3px solid #fff; }
.answer-btn.dimmed    { opacity: 0.3; filter: saturate(0.2); }
.answer-btn.correct   { outline: 4px solid #fff; box-shadow: 0 0 0 4px rgba(255,255,255,0.3); }
.answer-btn.wrong-sel { opacity: 0.45; }

/* ── Result Bars ── */
.result-bar + .result-bar { margin-top: 0.55rem; }

.result-bar-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.result-bar-lbl {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  background: var(--clr);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
  flex-shrink: 0;
}

.result-bar-track {
  flex: 1;
  height: 26px;
  background: var(--card);
  border-radius: 6px;
  overflow: hidden;
}

.result-bar-fill {
  height: 100%;
  background: var(--clr);
  border-radius: 6px;
  transition: width 0.35s ease;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 7px;
  font-size: 0.75rem;
  font-weight: 700;
}

.result-bar-count {
  width: 28px;
  text-align: right;
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.result-bar-tick { font-size: 0.85rem; color: #10b981; font-weight: 700; width: 16px; }

/* ── PIN Display ── */
.pin-display {
  font-size: 2.8rem;
  font-weight: 900;
  letter-spacing: 0.2em;
  font-family: 'Courier New', monospace;
  color: var(--accent);
}

.pin-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--muted);
  margin-bottom: 0.2rem;
}

/* ── Auth Overlay ── */
#auth-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-box { width: 100%; max-width: 360px; text-align: center; }
.auth-box h2 { margin-bottom: 0.4rem; }
.auth-box .sub { color: var(--muted); font-size: 0.9rem; margin-bottom: 1.5rem; }

/* ── Quiz List ── */
.quiz-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem 1.25rem;
  margin-bottom: 0.65rem;
}

.quiz-info { flex: 1; }
.quiz-title { font-weight: 700; font-size: 1rem; }
.quiz-meta  { color: var(--muted); font-size: 0.8rem; margin-top: 0.2rem; }
.quiz-actions { display: flex; gap: 0.4rem; flex-shrink: 0; }

/* ── Question Items in Editor ── */
.q-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin-bottom: 0.45rem;
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
}

.q-num {
  width: 26px;
  height: 26px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 1px;
}

.q-body { flex: 1; min-width: 0; }
.q-text  { font-weight: 600; margin-bottom: 0.4rem; font-size: 0.95rem; }
.q-opts  { display: flex; flex-wrap: wrap; gap: 0.3rem; }

.opt-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.15rem 0.55rem;
  border-radius: 20px;
  font-size: 0.75rem;
  background: var(--surface);
  color: var(--muted);
}
.opt-chip.correct { background: #10b981; color: #fff; font-weight: 700; }

/* ── Session Layout ── */
.session-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 1.25rem;
  padding: 1.25rem;
  max-width: 1080px;
  margin: 0 auto;
}

.sess-controls { display: flex; flex-direction: column; gap: 0.6rem; margin-top: 0.75rem; }

/* ── Present View ── */
#present-view {
  position: fixed;
  inset: 0;
  background: #120a2e;
  color: #ffffff;
  z-index: 200;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

#present-view.active { display: flex; }

.pv-question {
  flex: 0 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem 4rem;
  font-size: clamp(2rem, 5vw, 4.5rem);
  font-weight: 800;
  text-align: center;
  min-height: 20%;
  max-height: 38%;
  overflow: hidden;
}

.pv-options {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.pv-opt {
  background: var(--clr);
  display: flex;
  align-items: center;
  padding: 1.75rem 2rem;
  gap: 1rem;
  font-size: clamp(1.6rem, 3.8vw, 3rem);
  font-weight: 700;
  position: relative;
  overflow: hidden;
  transition: opacity 0.3s, filter 0.3s;
}

.pv-opt-lbl {
  width: 40px;
  height: 40px;
  background: rgba(0,0,0,0.25);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.pv-opt-text { flex: 1; }

.pv-opt-count {
  font-size: 1.6rem;
  font-weight: 900;
  opacity: 0.85;
  flex-shrink: 0;
}

.pv-opt-bar {
  position: absolute;
  bottom: 0; left: 0;
  height: 5px;
  background: rgba(255,255,255,0.45);
  transition: width 0.35s ease;
}

.pv-opt.reveal-correct { outline: 5px solid #fff; z-index: 1; }
.pv-opt.reveal-dim     { opacity: 0.35; filter: saturate(0.15); }

.pv-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 1.25rem;
  background: #0d0720;
  border-top: 1px solid rgba(255,255,255,0.08);
  gap: 1rem;
  flex-shrink: 0;
}

#present-view .btn-ghost {
  color: rgba(255,255,255,0.7);
  border-color: rgba(255,255,255,0.2);
}

.pv-progress { font-weight: 700; font-size: 0.9rem; }
.pv-ctrl     { display: flex; gap: 0.5rem; }

/* ── Play Page ── */
.play-wrap {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.play-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.play-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 0.75rem;
}

.q-display {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  font-size: clamp(1.4rem, 3.5vw, 2rem);
  font-weight: 700;
  text-align: center;
  line-height: 1.4;
}

.play-status {
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
  min-height: 1.4em;
}

/* ── Toggle Group ── */
.toggle-grp {
  display: flex;
  background: var(--card);
  border-radius: var(--radius);
  padding: 3px;
  gap: 3px;
}

.toggle-grp input[type="radio"] { display: none; }

.toggle-grp label {
  flex: 1;
  text-align: center;
  padding: 0.45rem;
  border-radius: calc(var(--radius) - 2px);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
  transition: background 0.15s, color 0.15s;
  margin: 0;
}

.toggle-grp input[type="radio"]:checked + label {
  background: var(--accent);
  color: #fff;
}

/* ── CSV Drop ── */
.csv-drop {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 1.75rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s;
}

.csv-drop:hover, .csv-drop.dragover { border-color: var(--accent); }

.csv-err-item {
  padding: 0.35rem 0.65rem;
  background: rgba(244,63,94,0.08);
  border-radius: 6px;
  font-size: 0.78rem;
  color: #be123c;
  margin-bottom: 0.25rem;
}

/* ── Utilities ── */
.hidden       { display: none !important; }
.flex         { display: flex; }
.flex-col     { display: flex; flex-direction: column; }
.gap-sm       { gap: 0.5rem; }
.gap-md       { gap: 0.85rem; }
.mt-1         { margin-top: 0.5rem; }
.mt-2         { margin-top: 1rem; }
.mt-3         { margin-top: 1.5rem; }
.mb-1         { margin-bottom: 0.5rem; }
.mb-2         { margin-bottom: 1rem; }
.mb-3         { margin-bottom: 1.5rem; }
.text-muted   { color: var(--muted); }
.text-center  { text-align: center; }
.text-sm      { font-size: 0.85rem; }
.text-xs      { font-size: 0.75rem; }
.text-success { color: #059669; }
.text-danger  { color: #dc2626; }
.text-accent  { color: var(--accent); }
.bold         { font-weight: 700; }
.w-full       { width: 100%; }

.divider { border: none; border-top: 1px solid var(--border); margin: 1.25rem 0; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  background: var(--card);
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 600;
}

.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--card);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 0 auto;
}

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

.waiting-dots::after {
  content: '';
  animation: dots 1.5s steps(4, end) infinite;
}
@keyframes dots { 0%{content:''} 25%{content:'.'} 50%{content:'..'} 75%{content:'...'} }

.msg {
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
}
.msg-error   { background: rgba(244,63,94,0.08);  border: 1px solid rgba(244,63,94,0.2);  color: #be123c; }
.msg-success { background: rgba(16,185,129,0.08); border: 1px solid rgba(16,185,129,0.2); color: #047857; }
.msg-info    { background: rgba(139,92,246,0.08); border: 1px solid rgba(139,92,246,0.2); color: #6d28d9; }

/* ── Responsive ── */
@media (max-width: 720px) {
  .session-layout { grid-template-columns: 1fr; padding: 1rem; }

  .pv-question { padding: 1.5rem; min-height: 25%; }
  .pv-opt { padding: 0.9rem 1.1rem; gap: 0.65rem; }
  .pv-opt-lbl { width: 32px; height: 32px; }

  .form-row { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
  .pin-display { font-size: 2.2rem; }
  .answer-btn  { min-height: 80px; }
  .answer-btn .opt-lbl { width: 44px; height: 44px; font-size: 1.2rem; }
}

/* ── Tabs (create page) ── */
.tab-bar {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 1.25rem;
}

.tab-btn {
  flex: 1;
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tab-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ── Timer ── */
.timer-display {
  font-size: 2.2rem;
  font-weight: 900;
  font-family: 'Courier New', monospace;
  letter-spacing: 0.05em;
  color: var(--text);
}

.timer-display.urgent { color: #e74c3c; }

.pv-timer {
  position: absolute;
  top: 1rem;
  right: 1.5rem;
  font-size: clamp(3rem, 8vw, 7rem);
  font-weight: 900;
  font-family: 'Courier New', monospace;
  opacity: 0.9;
  line-height: 1;
  transition: color 0.3s;
  z-index: 3;
}

.pv-timer.urgent { color: #e74c3c; }

/* ── Rankings (teacher view) ── */
.rank-item {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.6rem 0.85rem;
  background: var(--card);
  border-radius: var(--radius);
  margin-bottom: 0.4rem;
}

.rank-item-me { outline: 2px solid var(--accent); }

.rank-pos {
  width: 28px;
  font-weight: 900;
  font-size: 0.9rem;
  text-align: center;
  flex-shrink: 0;
  color: var(--muted);
}

.rank-name { flex: 1; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.rank-score { font-weight: 700; font-size: 1rem; flex-shrink: 0; }

.rank-delta {
  font-size: 0.78rem;
  color: #10b981;
  min-width: 50px;
  text-align: right;
  flex-shrink: 0;
}

/* ── Present view rankings overlay ── */
#pv-rankings {
  display: none;
  position: absolute;
  inset: 0 0 60px 0;
  background: #120a2e;
  z-index: 5;
  padding: 2rem 3rem;
  gap: 2.5rem;
  align-items: center;
}

#pv-rankings.active { display: grid; grid-template-columns: 1fr 1fr; }

.pv-rank-col { display: flex; flex-direction: column; }

.pv-chart-col {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.5rem;
}

.pv-rank-title {
  font-size: clamp(1.5rem, 4vw, 3rem);
  font-weight: 900;
  margin-bottom: 1.5rem;
  background: linear-gradient(135deg, #f43f5e, #8b5cf6, #06b6d4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.pv-rank-list { width: 100%; }

.pv-rank-item {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0.85rem 1.25rem;
  background: rgba(255,255,255,0.1);
  border-radius: var(--radius-lg);
  margin-bottom: 0.65rem;
  font-size: clamp(1rem, 2.5vw, 1.6rem);
  font-weight: 700;
  color: #ffffff;
  will-change: transform;
}

.pv-rank-pos { width: 2em; text-align: center; font-weight: 900; }
.pv-rank-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pv-rank-score { font-weight: 900; flex-shrink: 0; }
.pv-rank-delta { font-size: 0.65em; color: #10b981; flex-shrink: 0; }

/* ── Answer distribution chart ── */
.ans-chart-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  opacity: 0.6;
}
.ans-chart-correct { opacity: 1; }
.ans-chart-lbl {
  width: 3em;
  font-weight: 700;
  flex-shrink: 0;
}
.ans-chart-track {
  flex: 1;
  height: 28px;
  background: rgba(255,255,255,0.12);
  border-radius: 6px;
  overflow: hidden;
}
.ans-chart-fill {
  height: 100%;
  border-radius: 6px;
  transition: width 0.5s ease;
  min-width: 2px;
}
.ans-chart-count {
  width: 2.2em;
  text-align: right;
  font-weight: 700;
  flex-shrink: 0;
}

/* Present view chart text colours */
#pv-chart .ans-chart-lbl   { font-size: clamp(1rem, 2.2vw, 1.5rem); color: #fff; }
#pv-chart .ans-chart-track { background: rgba(255,255,255,0.12); height: clamp(24px, 4vh, 40px); }
#pv-chart .ans-chart-count { font-size: clamp(0.9rem, 1.8vw, 1.2rem); color: rgba(255,255,255,0.85); }

/* Teacher view chart text colours */
#rankings-chart .ans-chart-lbl   { font-size: 0.85rem; color: var(--text); }
#rankings-chart .ans-chart-track { background: var(--card); height: 22px; }
#rankings-chart .ans-chart-count { font-size: 0.82rem; color: var(--muted); }
#rankings-chart .ans-chart-row   { opacity: 0.7; }
#rankings-chart .ans-chart-correct { opacity: 1; }

/* ── Avatar picker ── */
.avatar-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.35rem;
  width: 100%;
  max-width: 360px;
}

.avatar-btn {
  font-size: 1.55rem;
  line-height: 1;
  padding: 0.3rem 0;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--card);
  cursor: pointer;
  aspect-ratio: 1;
  transition: transform 0.1s, border-color 0.15s, opacity 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-btn:hover:not(.avatar-taken) {
  border-color: var(--accent);
  transform: scale(1.15);
}

.avatar-btn:active:not(.avatar-taken) { transform: scale(0.95); }

.avatar-btn.avatar-taken {
  opacity: 0.2;
  cursor: not-allowed;
  pointer-events: none;
  filter: grayscale(0.5);
}

/* ── Joined students list ── */
.joined-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  justify-content: center;
}

.joined-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
}

.joined-avatar { font-size: 1.9rem; line-height: 1; }

.joined-name {
  font-size: 0.65rem;
  color: var(--muted);
  max-width: 52px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Avatar in rankings ── */
.rank-avatar {
  font-size: 1.2rem;
  line-height: 1;
  flex-shrink: 0;
  width: 1.6em;
  text-align: center;
}

.pv-rank-avatar {
  font-size: clamp(1.2rem, 2.5vw, 2rem);
  line-height: 1;
  flex-shrink: 0;
  width: 1.4em;
  text-align: center;
}
