:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1c2430;
  --muted: #6b7480;
  --border: #e2e5ea;
  --brand: #2456d9;
  --brand-dark: #1a3fa8;
  --good: #1f9254;
  --good-bg: #e8f7ee;
  --bad: #c23b3b;
  --bad-bg: #fceaea;
  --warn: #b9770e;
  --warn-bg: #fdf3e0;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

.hidden { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--brand);
  color: white;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}
.topbar-inner {
  max-width: 900px; margin: 0 auto;
  padding: 12px 16px;
  display: flex; align-items: center; gap: 12px;
}
.brand { font-weight: 700; font-size: 1.05rem; cursor: pointer; }
.donate-badge {
  background: rgba(255,255,255,0.15); color: white;
  border: 1px solid rgba(255,255,255,0.5); border-radius: 20px;
  padding: 4px 12px; font-size: 0.82rem; font-weight: 600; cursor: pointer;
}
.donate-badge:hover { background: rgba(255,255,255,0.28); }
.timer {
  font-weight: 700; font-size: 1.1rem;
  background: rgba(255,255,255,0.15);
  padding: 4px 12px; border-radius: 20px;
}
.timer.low { background: #c23b3b; }

.topbar-right { display: flex; align-items: center; gap: 12px; margin-left: auto; }
.user-box { position: relative; font-size: 0.9rem; }
.btn.small { padding: 5px 10px; font-size: 0.82rem; }
.btn.ghost { color: white; border-color: rgba(255,255,255,0.5); }

.user-menu-toggle {
  display: flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,0.15); color: white;
  border: 1px solid rgba(255,255,255,0.5); border-radius: 20px;
  padding: 5px 12px; font-size: 0.85rem; font-weight: 600; cursor: pointer;
}
.user-menu-toggle:hover { background: rgba(255,255,255,0.28); }
.user-menu-toggle .caret { font-size: 0.7rem; opacity: 0.85; }

.user-menu {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 20;
  background: var(--card); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.15);
  min-width: 190px; padding: 6px; overflow: hidden;
}
.user-menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  background: none; border: none; text-align: left;
  padding: 9px 10px; border-radius: 6px; font-size: 0.9rem;
  color: var(--ink); cursor: pointer;
}
.user-menu-item:hover { background: var(--bg); }
.user-menu-item.danger { color: var(--bad); }
.user-menu-icon { width: 16px; text-align: center; }

.auth-card {
  max-width: 420px; margin: 40px auto; background: var(--card);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px 28px; box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.auth-card h1 { font-size: 1.3rem; margin-bottom: 6px; }
.auth-tabs { display: flex; gap: 8px; margin: 18px 0 16px; border-bottom: 1px solid var(--border); }
.auth-tab {
  flex: 1; padding: 10px; background: none; border: none; cursor: pointer;
  font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent;
}
.auth-tab.active { color: var(--brand); border-bottom-color: var(--brand); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.88rem; font-weight: 600; color: var(--ink); }
.auth-form input {
  padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.95rem; font-weight: 400;
}
.auth-error { color: var(--bad); font-size: 0.85rem; margin: 0; }
.muted.small { font-size: 0.8rem; margin: -6px 0 0; }

.profile-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; margin: 18px 0; display: flex; gap: 18px; flex-wrap: wrap; font-size: 0.9rem;
}
.profile-card .stat strong { display: block; font-size: 1.1rem; color: var(--brand); }

#app { max-width: 900px; margin: 0 auto; padding: 20px 16px 60px; }

.hero h1 { font-size: 1.5rem; margin-bottom: 4px; }
.muted { color: var(--muted); }

.filter-bar {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin: 18px 0 0;
}
.filter-bar label {
  display: flex; flex-direction: column; gap: 4px; font-size: 0.82rem; font-weight: 600; color: var(--muted);
}
.filter-bar select {
  padding: 7px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem;
  color: var(--ink); min-width: 120px;
}
.filter-bar .btn.small { margin-left: 0; }
.filter-count { color: var(--muted); font-size: 0.85rem; margin: 10px 0 -8px; }

.paper-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
  margin: 20px 0 32px;
}
@media (max-width: 640px) { .paper-grid { grid-template-columns: 1fr; } }

.paper-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.paper-card h3 { margin: 0 0 6px; }
.exam-type-badge {
  display: inline-block; background: var(--good-bg); color: var(--good);
  font-size: 0.78rem; font-weight: 700; padding: 3px 9px; border-radius: 20px;
  margin-bottom: 10px;
}
.paper-card .meta { color: var(--muted); font-size: 0.9rem; margin-bottom: 14px; }
.paper-card .meta span { display: inline-block; margin-right: 10px; }

.btn {
  display: inline-block; padding: 10px 18px; border-radius: 8px; border: none;
  font-size: 0.95rem; font-weight: 600; cursor: pointer; text-align: center;
}
.btn.primary { background: var(--brand); color: white; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); margin-left: 10px; }
.btn.full { width: 100%; }

.history-section h2 { font-size: 1.15rem; margin-bottom: 10px; }
.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-item {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 14px; display: flex; justify-content: space-between; font-size: 0.92rem;
}
.history-item .score { font-weight: 700; }

.exam-header { margin-bottom: 18px; }
.exam-header h2 { margin-bottom: 4px; }

.section-title {
  font-size: 1.1rem; font-weight: 700; margin: 28px 0 6px;
  padding-bottom: 6px; border-bottom: 2px solid var(--brand);
}
.section-instructions { color: var(--muted); font-size: 0.9rem; margin-bottom: 14px; }

.q-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 14px;
}
.q-head { display: flex; gap: 10px; margin-bottom: 8px; }
.q-num { font-weight: 700; color: var(--brand); min-width: 28px; }
.q-text { white-space: pre-line; flex: 1; }
.q-marks { color: var(--muted); font-size: 0.82rem; white-space: nowrap; }

.q-diagram { margin: 10px 0; }
.q-diagram img { max-width: 100%; border: 1px solid var(--border); border-radius: 6px; display: block; }

.q-options { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.q-option {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 6px; cursor: pointer;
}
.q-option:hover { background: #f0f3fb; }
.q-option input { margin: 0; }

.q-answer-row { margin-top: 10px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.q-answer-row input[type="text"], .q-answer-row input[type="number"], .q-answer-row select {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.95rem;
  min-width: 140px;
}
.q-unit { color: var(--muted); font-size: 0.9rem; }

.part-block { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--border); }
.part-block:first-of-type { border-top: none; padding-top: 0; }
.part-label { font-weight: 700; margin-right: 6px; }

.tf-table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.9rem; }
.tf-table td { padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.tf-table select { padding: 6px 8px; border-radius: 6px; border: 1px solid var(--border); }

.manual-note {
  margin-top: 8px; padding: 8px 10px; background: var(--warn-bg); color: var(--warn);
  border-radius: 6px; font-size: 0.85rem;
}

.submit-bar { margin-top: 24px; text-align: center; }

.results-summary {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 22px; text-align: center; margin-bottom: 24px;
}
.results-summary .big-score { font-size: 2.6rem; font-weight: 800; color: var(--brand); }
.results-summary .sub-score { color: var(--muted); margin-top: 4px; }
.results-summary .manual-flag { margin-top: 10px; color: var(--warn); font-size: 0.9rem; }

.result-section-title {
  font-size: 1.05rem; font-weight: 700; margin: 24px 0 10px;
}

.result-q {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 10px;
}
.result-q.correct { border-left: 4px solid var(--good); }
.result-q.wrong { border-left: 4px solid var(--bad); }
.result-q.manual { border-left: 4px solid var(--warn); }
.result-q .rq-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.result-q .rq-marks { font-weight: 700; font-size: 0.9rem; }
.result-q .rq-marks.correct { color: var(--good); }
.result-q .rq-marks.wrong { color: var(--bad); }
.result-q .rq-marks.manual { color: var(--warn); }
.result-q .rq-text { white-space: pre-line; margin: 4px 0 8px; }
.result-q .rq-your, .result-q .rq-correct { font-size: 0.9rem; margin-top: 2px; }
.result-q .rq-your .val { font-weight: 600; }
.result-q .rq-correct .val { font-weight: 600; color: var(--good); }

.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-card {
  position: relative;
  background: var(--card); border-radius: var(--radius);
  padding: 28px 24px 24px; max-width: 320px; width: 100%;
  text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.modal-card h3 { margin: 0 0 6px; }
.modal-close {
  position: absolute; top: 8px; right: 10px;
  background: none; border: none; font-size: 1.4rem; line-height: 1;
  cursor: pointer; color: var(--muted);
}
.donate-qr { width: 100%; max-width: 240px; margin-top: 14px; border-radius: 6px; }

#changePasswordModal .modal-card { max-width: 360px; text-align: left; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
