/* ============ Theme variables ============ */
:root {
  --bg: #f3f5f9;
  --bg-2: #ffffff;
  --bg-3: #eef1f6;
  --text: #1f2937;
  --text-soft: #475569;
  --text-mute: #94a3b8;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --purple: #7c3aed;
  --purple-soft: #ede9fe;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 14px 40px rgba(15, 23, 42, 0.12);
  --radius: 14px;
  --radius-sm: 10px;
  --transition: 200ms ease;
}

[data-theme="dark"] {
  --bg: #0b1220;
  --bg-2: #111a2c;
  --bg-3: #16223a;
  --text: #e2e8f0;
  --text-soft: #b6c1d3;
  --text-mute: #788397;
  --border: #243149;
  --border-strong: #30425e;
  --primary: #60a5fa;
  --primary-hover: #93c5fd;
  --primary-soft: #1e3a8a;
  --success: #4ade80;
  --success-soft: #052e1a;
  --danger: #f87171;
  --danger-soft: #3a0f12;
  --warning: #fbbf24;
  --warning-soft: #3a2a08;
  --purple: #a78bfa;
  --purple-soft: #2c1f5b;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 14px 40px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: "Segoe UI", "Noto Sans Devanagari", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; cursor: pointer; }

/* ============ Screens ============ */
.screen { display: none; min-height: 100vh; }
.screen.active { display: block; }

/* ============ Start Screen ============ */
#screen-start {
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 32px 16px;
}
#screen-start.active { display: flex; }

.start-card {
  width: 100%;
  max-width: 880px;
  background: var(--bg-2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 32px;
  border: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.brand-mark {
  width: 56px; height: 56px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--purple));
  color: white;
  display: grid; place-items: center;
  font-weight: 800; letter-spacing: 1px;
  font-size: 18px;
  box-shadow: var(--shadow);
}
.brand-text h1 { margin: 0; font-size: 22px; }
.brand-text .muted { margin: 2px 0 0; color: var(--text-mute); font-size: 13px; }

.icon-btn {
  margin-left: auto;
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-3);
  color: var(--text);
  font-size: 18px;
  display: grid; place-items: center;
  transition: background var(--transition);
}
.icon-btn:hover { background: var(--primary-soft); }

.instructions h2 { margin: 12px 0 8px; font-size: 17px; }
.instructions ul { margin: 0; padding-left: 20px; color: var(--text-soft); }
.instructions li { margin: 6px 0; }

.legend-row {
  display: flex; flex-wrap: wrap; gap: 10px;
  margin-top: 8px;
}
.legend {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px;
  background: var(--bg-3);
  border-radius: 999px;
  font-size: 12px;
  color: var(--text-soft);
}
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.dot-na { background: #cbd5e1; }
.dot-na2 { background: #ef4444; }
.dot-ans { background: #16a34a; }
.dot-rev { background: #7c3aed; }
.dot-ansrev { background: linear-gradient(135deg, #16a34a 0%, #16a34a 50%, #7c3aed 50%, #7c3aed 100%); }

.candidate-row {
  margin: 16px 0 8px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.candidate-row label {
  font-weight: 600; min-width: 130px;
}
.candidate-row input {
  flex: 1; min-width: 200px;
  padding: 10px 12px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
}
.candidate-row input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }

.agree-row { margin: 12px 0; }
.checkbox {
  display: flex; align-items: flex-start; gap: 8px;
  cursor: pointer; color: var(--text-soft);
}
.checkbox input { margin-top: 4px; transform: scale(1.15); accent-color: var(--primary); }

.actions-row {
  display: flex; justify-content: flex-end; gap: 10px;
  margin-top: 16px;
}

/* ============ Buttons ============ */
.btn {
  padding: 10px 18px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  background: var(--bg-2);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  transition: all var(--transition);
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }

.btn-primary {
  background: var(--primary); border-color: var(--primary); color: white;
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-success {
  background: var(--success); border-color: var(--success); color: white;
}
.btn-success:hover { filter: brightness(1.05); }
.btn-ghost { background: var(--bg-3); }

/* ============ Exam topbar ============ */
.exam-topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
  width: 42px; height: 42px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--purple));
  color: white; font-weight: 800;
  display: grid; place-items: center;
}
.title-block { min-width: 0; }
.title-main { font-weight: 700; font-size: 15px; line-height: 1.1; }
.title-sub { color: var(--text-mute); font-size: 12px; line-height: 1.4; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 38vw; }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.candidate-chip {
  padding: 6px 12px;
  background: var(--bg-3);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-soft);
  white-space: nowrap;
}
.timer-box {
  background: var(--primary-soft);
  color: var(--primary);
  padding: 6px 14px;
  border-radius: 10px;
  text-align: center;
  font-weight: 700;
  border: 1px solid var(--primary);
}
[data-theme="dark"] .timer-box { color: white; }
.timer-box.warning { background: var(--warning-soft); color: var(--warning); border-color: var(--warning); }
.timer-box.danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: 0.6; } }
.timer-label { display: block; font-size: 10px; line-height: 1; opacity: 0.85; }
.timer-value { font-size: 17px; line-height: 1.2; letter-spacing: 0.5px; }

/* Progress bar */
.exam-progress-wrap {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.exam-progress-bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--bg-3);
  overflow: hidden;
}
.exam-progress-bar span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--purple));
  transition: width 250ms ease;
}
.exam-progress-text { font-size: 12px; color: var(--text-mute); white-space: nowrap; }

/* ============ Exam body layout ============ */
.exam-body {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 16px;
  padding: 16px;
  max-width: 1400px;
  margin: 0 auto;
}
.exam-main-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

/* Section tabs */
.section-tabs {
  display: flex; gap: 6px; flex-wrap: wrap;
  padding: 8px;
  background: var(--bg-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.exam-actions-top {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  margin: 10px 0 4px;
  text-align: right;
}
.exam-actions-top .btn {
  min-width: 120px;
  margin-left: auto;
}
.section-tab {
  padding: 6px 12px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}
.section-tab:hover { background: var(--bg-3); }
.section-tab.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary);
}
[data-theme="dark"] .section-tab.active { color: white; }

/* Question panel */
.question-panel {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: var(--shadow);
}
.question-meta {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px; flex-wrap: wrap; gap: 8px;
}
.question-num { font-weight: 700; font-size: 15px; }
.question-num .q-of { color: var(--text-mute); font-weight: 500; }
.question-marks { display: flex; gap: 6px; }
.badge {
  padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700;
}
.badge-pos { background: var(--success-soft); color: var(--success); }
.badge-neg { background: var(--danger-soft); color: var(--danger); }

.question-text {
  font-size: 16px;
  line-height: 1.7;
  margin: 12px 0 22px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.options-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex; flex-direction: column; gap: 10px;
}
.option-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 14px;
  background: var(--bg-3);
  border: 2px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  transition: all var(--transition);
}
.option-item:hover { background: var(--primary-soft); border-color: var(--primary); }
[data-theme="dark"] .option-item:hover { background: var(--bg-3); border-color: var(--primary); }
.option-item.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.option-radio {
  flex: 0 0 22px;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  display: grid; place-items: center;
  background: var(--bg-2);
  margin-top: 1px;
}
.option-item.selected .option-radio {
  border-color: var(--primary);
  background: var(--primary);
}
.option-item.selected .option-radio::after {
  content: ""; width: 8px; height: 8px; border-radius: 50%; background: white;
}
.option-letter { font-weight: 700; min-width: 18px; }
.option-text { flex: 1; word-wrap: break-word; }

/* Footer buttons */
.exam-footer {
  display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  position: sticky; bottom: 0;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.04);
}
.footer-left, .footer-right { display: flex; gap: 8px; flex-wrap: wrap; }

/* ============ Palette ============ */
.palette-col {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  height: fit-content;
  position: sticky; top: 80px;
  box-shadow: var(--shadow);
}
.palette-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.palette-title { font-weight: 700; font-size: 14px; }
.icon-btn-mini {
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--bg-3); border: 1px solid var(--border); color: var(--text-soft);
  display: none; place-items: center;
}
.legend-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  font-size: 11px;
  margin-bottom: 10px;
}
.legend-grid .legend { background: transparent; padding: 0; font-size: 11px; }
.pal-tile {
  width: 18px; height: 18px; border-radius: 5px;
  display: inline-block;
}
.pal-na { background: #e2e8f0; border: 1px solid #cbd5e1; }
[data-theme="dark"] .pal-na { background: #334155; border-color: #475569; }
.pal-na2 { background: #ef4444; }
.pal-ans { background: #16a34a; }
.pal-rev { background: #7c3aed; }
.pal-ansrev { background: linear-gradient(135deg, #7c3aed 50%, #16a34a 50%); }

.palette-section-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-soft);
  margin: 8px 0 6px;
  padding: 4px 6px;
  background: var(--bg-3);
  border-radius: 6px;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  max-height: 380px;
  overflow-y: auto;
  padding: 2px;
}
.palette-grid::-webkit-scrollbar { width: 6px; }
.palette-grid::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }

.pal-q {
  aspect-ratio: 1;
  display: grid; place-items: center;
  background: #e2e8f0;
  color: #1e293b;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform var(--transition);
  user-select: none;
}
[data-theme="dark"] .pal-q { background: #334155; color: #e2e8f0; }
.pal-q:hover { transform: scale(1.08); }
.pal-q.current { outline: 2px solid var(--primary); outline-offset: 1px; }
.pal-q.answered { background: #16a34a; color: white; }
.pal-q.notanswered { background: #ef4444; color: white; }
.pal-q.review { background: #7c3aed; color: white; }
.pal-q.ansreview {
  background: linear-gradient(135deg, #7c3aed 50%, #16a34a 50%);
  color: white;
  position: relative;
}

.palette-summary {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-soft);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
}
.palette-summary div { display: flex; justify-content: space-between; }
.palette-summary b { color: var(--text); }

.palette-show {
  display: none;
  position: fixed; right: 14px; top: 96px;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--primary); color: white; border: none;
  font-size: 18px;
  z-index: 15;
  box-shadow: var(--shadow-lg);
}

/* ============ Modal ============ */
.modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: grid; place-items: center;
  z-index: 100;
  padding: 16px;
  animation: fadein 200ms;
}
.modal.hidden { display: none; }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  background: var(--bg-2);
  border-radius: var(--radius);
  padding: 24px 28px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  animation: pop 220ms;
}
@keyframes pop { from { transform: scale(0.92); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-card h3 { margin: 0 0 8px; font-size: 19px; }
.modal-card.warn { border-top: 4px solid var(--warning); }
.modal-card p { color: var(--text-soft); margin: 0 0 12px; }
.submit-summary {
  background: var(--bg-3);
  padding: 12px 14px;
  border-radius: 10px;
  margin: 12px 0;
  font-size: 13px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
}
.submit-summary b { color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* ============ Result screen ============ */
.result-body {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px;
}

.result-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.stat-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}
.stat-label { font-size: 12px; color: var(--text-mute); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 28px; font-weight: 800; margin-top: 6px; }
.stat-value.score { color: var(--primary); }
.stat-value.correct { color: var(--success); }
.stat-value.wrong { color: var(--danger); }
.stat-value.unattempted { color: var(--text-mute); }
.stat-value.percent { color: var(--purple); }

.status-banner {
  grid-column: 1 / -1;
  padding: 18px 22px;
  border-radius: var(--radius);
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  border: 2px solid;
}
.status-banner.pass { background: var(--success-soft); color: var(--success); border-color: var(--success); }
.status-banner.fail { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }

.result-actions {
  display: flex; gap: 10px; margin-bottom: 18px; flex-wrap: wrap;
}

.review-section { margin-top: 16px; }
.review-section h2 { margin-top: 0; }
.review-section.hidden { display: none; }

.review-section-title {
  margin: 18px 0 8px;
  font-size: 15px;
  font-weight: 700;
  color: var(--primary);
  padding-bottom: 4px;
  border-bottom: 2px solid var(--primary-soft);
}

.review-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.review-card .review-q-num {
  font-size: 12px; font-weight: 700; color: var(--text-mute);
  display: inline-block;
  margin-right: 10px;
}
.review-card .review-q-status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
  display: inline-block;
}
.review-q-status.correct { background: var(--success-soft); color: var(--success); }
.review-q-status.wrong { background: var(--danger-soft); color: var(--danger); }
.review-q-status.skip { background: var(--bg-3); color: var(--text-mute); }
.review-q-text { margin: 8px 0 10px; line-height: 1.6; }
.review-options { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 5px; }
.review-options li {
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg-3);
  border: 1px solid transparent;
  font-size: 14px;
  display: flex; gap: 8px; align-items: flex-start;
}
.review-options li.right {
  background: var(--success-soft);
  border-color: var(--success);
  color: var(--success);
  font-weight: 600;
}
.review-options li.user-wrong {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
  font-weight: 600;
  text-decoration: line-through;
  text-decoration-color: rgba(220, 38, 38, 0.4);
}
.review-options li .ropt-letter { font-weight: 700; min-width: 18px; }
.review-options li .ropt-tag { margin-left: auto; font-size: 11px; opacity: 0.85; }

/* ============ Responsive ============ */
@media (max-width: 920px) {
  .exam-body { grid-template-columns: 1fr; }
  .palette-col {
    position: fixed;
    top: 0; right: 0; bottom: 0; left: auto;
    width: 88vw; max-width: 320px;
    height: 100vh;
    border-radius: 0;
    z-index: 30;
    transform: translateX(100%);
    transition: transform 250ms ease;
    overflow-y: auto;
    box-shadow: -4px 0 20px rgba(0,0,0,0.25);
    padding-top: 18px;
  }
  .palette-col.open { transform: translateX(0); }
  /* Backdrop overlay when palette is open */
  body:has(.palette-col.open)::before {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 25;
  }
  .palette-show {
    display: grid; place-items: center;
    top: 88px;
    right: 12px;
    bottom: auto;
    width: 40px; height: 40px;
    font-size: 16px;
    z-index: 15;
  }
  /* Hide floating palette button when palette is open */
  .palette-col.open ~ .palette-show { display: none; }
  body:has(.palette-col.open) .palette-show { display: none; }
  .icon-btn-mini { display: grid; }
  .topbar-right { gap: 6px; }
  .candidate-chip { display: none; }
  .start-card { padding: 18px; }
  .question-panel { padding: 16px; }
  .exam-footer { padding: 8px 10px; gap: 8px; }
  .footer-left, .footer-right { flex: 1; }
  .footer-left .btn, .footer-right .btn { flex: 1; min-width: 0; font-size: 13px; padding: 8px 6px; }
  .exam-actions-top .btn { width: auto; min-width: 100px; }
}
@media (max-width: 480px) {
  .brand-text h1 { font-size: 18px; }
  .timer-value { font-size: 14px; }
  .question-text { font-size: 15px; }
  .palette-grid { grid-template-columns: repeat(5, 1fr); }
  .stat-value { font-size: 22px; }
}
