/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: #f0f2f5;
  color: #1a1a2e;
  min-height: 100vh;
}
h1,h2,h3 { margin-bottom: .5em; }
input, textarea, select {
  font: inherit;
  padding: 10px 14px;
  border: 1px solid #d0d5dd;
  border-radius: 6px;
  width: 100%;
  transition: border .15s;
}
input:focus, textarea:focus { border-color: #2196F3; outline: none; }
label { display: block; font-weight: 600; margin: 8px 0 4px; font-size: .92em; }

/* ── Pages ────────────────────────────────────────────────────────────────── */
.page { display: none; padding: 20px; animation: fadeIn .25s; }
.page.active { display: block; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-block;
  padding: 10px 22px;
  border: none;
  border-radius: 6px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.btn:active { transform: scale(.97); }
.btn[hidden] { display: none; }
.btn-primary   { background: #2196F3; color: #fff; }
.btn-primary:hover { background: #1976D2; }
.btn-secondary { background: #fff; color: #2196F3; border: 2px solid #2196F3; }
.btn-secondary:hover { background: #e3f2fd; }
.btn-danger    { background: #e53935; color: #fff; }
.btn-danger:hover { background: #c62828; }
.btn-sm  { padding: 6px 14px; font-size: .88em; }
.btn-lg  { padding: 14px 36px; font-size: 1.12em; }
.btn-link { background: none; color: #666; text-decoration: underline; padding: 8px; }
.btn-tab {
  background: #e8eaed; color: #333; margin-right: 4px; border-radius: 6px 6px 0 0;
}
.btn-tab.active { background: #2196F3; color: #fff; }

/* ── Hero (home page) ────────────────────────────────────────────────────── */
.hero {
  text-align: center;
  padding: 15vh 20px 0;
}
.hero h1 { font-size: 2.6em; color: #1565C0; }
.subtitle { color: #666; font-size: 1.15em; margin: 8px 0 32px; }
.hero-buttons { display: flex; gap: 20px; justify-content: center; flex-wrap: wrap; }

/* ── Card ─────────────────────────────────────────────────────────────────── */
.card {
  background: #fff;
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 2px 12px rgba(0,0,0,.08);
}
.card-narrow { max-width: 380px; }
.centered { margin: 40px auto; }

/* ── Error ────────────────────────────────────────────────────────────────── */
.error-msg { color: #e53935; font-size: .9em; margin: 8px 0; }

/* ── Admin ────────────────────────────────────────────────────────────────── */
.admin-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: #1565C0; color: #fff;
  border-radius: 10px; margin-bottom: 16px;
}
.admin-header h2 { margin: 0; font-size: 1.2em; }
.admin-tab { display: none; }
.admin-tab.active { display: block; }
.admin-grid {
  display: grid;
  grid-template-columns: 220px 1fr 280px;
  gap: 16px;
}
@media (max-width: 960px) {
  .admin-grid { grid-template-columns: 1fr; }
}
.panel {
  background: #fff;
  border-radius: 10px;
  padding: 18px;
  box-shadow: 0 1px 6px rgba(0,0,0,.06);
}
.panel-wide { min-height: 400px; }
.text-muted { color: #999; font-size: .85em; font-weight: normal; }

/* ── Folders ──────────────────────────────────────────────────────────────── */
.folder-actions { display: flex; gap: 6px; margin-bottom: 10px; }
.folder-actions input { flex: 1; padding: 7px 10px; font-size: .9em; }
.folder-list { list-style: none; }
.folder-list li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
  transition: background .12s;
}
.folder-list li:hover { background: #e3f2fd; }
.folder-list li.active { background: #2196F3; color: #fff; }
.folder-list li .del-folder {
  background: none; border: none; cursor: pointer; opacity: .5; font-size: 1.1em;
}
.folder-list li.active .del-folder { color: #fff; opacity: .8; }

/* ── Drop zone ────────────────────────────────────────────────────────────── */
.drop-zone {
  border: 2px dashed #bbb;
  border-radius: 10px;
  padding: 40px 20px;
  text-align: center;
  color: #999;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  min-height: 200px;
  display: flex; align-items: center; justify-content: center;
  flex-direction: column;
  position: relative;
}
.drop-zone.drag-over { border-color: #2196F3; background: #e3f2fd; }
.drop-zone img {
  max-width: 100%; max-height: 300px; object-fit: contain;
  border-radius: 6px;
}
.input-area { margin: 12px 0; }

/* ── KaTeX preview ────────────────────────────────────────────────────────── */
.katex-preview {
  background: #fafafa;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 16px;
  margin-top: 8px;
  min-height: 50px;
  font-size: 1.15em;
  overflow-x: auto;
}

/* ── Question list (admin) ────────────────────────────────────────────────── */
.question-list { max-height: 500px; overflow-y: auto; }
.q-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px; border-bottom: 1px solid #eee;
  cursor: pointer; transition: background .12s;
}
.q-item:hover { background: #f5f5f5; }
.q-item .q-thumb { width: 50px; height: 40px; object-fit: cover; border-radius: 4px; }
.q-item .q-info { flex: 1; font-size: .88em; }
.q-item .q-del { background: none; border: none; cursor: pointer; color: #e53935; font-size: 1.1em; }

/* ── Mode toggle ──────────────────────────────────────────────────────────── */
.mode-toggle { display: flex; gap: 4px; margin: 10px 0; }

/* ── Student setup ────────────────────────────────────────────────────────── */
.form-row { display: flex; gap: 16px; margin: 8px 0; }
.form-row > div { flex: 1; }
.folder-checkboxes { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 16px; }
.folder-checkboxes label {
  display: flex; align-items: center; gap: 6px;
  background: #f5f5f5; padding: 8px 14px;
  border-radius: 6px; font-weight: normal; cursor: pointer;
}
.folder-checkboxes label:hover { background: #e3f2fd; }

/* ── Folder quota rows (config tab) ─────────────────────────────────────── */
.folder-quotas { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 4px; }
.folder-quota-row {
  display: flex; align-items: center; gap: 10px;
  background: #f5f5f5; padding: 8px 14px; border-radius: 6px;
}
.folder-quota-label {
  display: flex; align-items: center; gap: 6px;
  font-weight: normal; cursor: pointer; flex: 1;
}
.folder-quota-row .cfg-quota-input {
  width: 72px; padding: 5px 8px; font-size: .9em;
  border: 1px solid #d0d5dd; border-radius: 6px;
  text-align: center; flex-shrink: 0;
}

/* ── Folder order list ───────────────────────────────────────────────────── */
.order-list { display: flex; flex-direction: column; gap: 6px; }
.order-item {
  display: flex; align-items: center; justify-content: space-between;
  background: #e8f0fe; border: 1px solid #c5d4f8;
  border-radius: 8px; padding: 10px 14px;
}
.order-item-name { flex: 1; font-size: .95em; }
.order-item-btns { display: flex; gap: 4px; }
.order-btn {
  background: #fff; border: 1px solid #d0d5dd;
  border-radius: 4px; padding: 3px 10px; font-size: .95em;
  cursor: pointer; line-height: 1.4;
}
.order-btn:hover:not(:disabled) { background: #e3f2fd; }
.order-btn:disabled { opacity: .35; cursor: default; }

/* ── Test page ────────────────────────────────────────────────────────────── */
.test-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 24px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  margin-bottom: 12px;
  font-weight: 700;
}
.timer { font-size: 1.5em; color: #2196F3; }
.timer.warning { color: #e53935; }
.test-body {
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  min-height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  overflow: auto;
}
.test-question {
  width: 100%;
  text-align: center;
}
.test-question img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: 8px;
}
.test-question .katex-block {
  font-size: 1.3em;
  text-align: left;
  padding: 20px;
}
.test-answer-row {
  display: flex; align-items: center; gap: 12px;
  background: #fff; border-radius: 10px; padding: 14px 20px;
  margin: 12px 0; box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.test-answer-row label { margin: 0; white-space: nowrap; }
.test-answer-row input { flex: 1; font-size: 1.1em; }
.test-nav {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0;
}

/* ── Results ──────────────────────────────────────────────────────────────── */
#result-summary { font-size: 1.15em; text-align: center; margin: 16px 0; }
#result-grade {
  text-align: center;
  font-size: 3em;
  font-weight: 900;
  margin: 10px 0 24px;
}
.detail-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; margin: 4px 0; border-radius: 8px;
}
.detail-item.correct { background: #e8f5e9; }
.detail-item.wrong   { background: #ffebee; }
.detail-icon { font-size: 1.3em; font-weight: bold; width: 28px; text-align: center; }
.detail-text { flex: 1; font-size: .92em; }

/* ── History table ────────────────────────────────────────────────────────── */
.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9em;
}
.history-table th, .history-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid #eee;
}
.history-table th { background: #f5f5f5; font-weight: 700; position: sticky; top: 0; }
.history-table tr:hover td { background: #fafafa; }
.grade-5 { color: #2e7d32; font-weight: bold; }
.grade-4 { color: #558b2f; font-weight: bold; }
.grade-3 { color: #ef6c00; font-weight: bold; }
.grade-2 { color: #c62828; font-weight: bold; }

.history-detail {
  display: none;
  background: #fafafa;
  padding: 12px;
  border-radius: 8px;
  margin: 4px 0 8px;
}
.history-detail.open { display: block; }

/* ── History detail items with question content ──────────────────────────── */
.detail-item-full {
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 14px;
}
.detail-header {
  display: flex; align-items: center; gap: 8px;
  font-size: .95em;
}
.hist-q-img-wrap {
  width: 100%;
  text-align: center;
  background: #fff;
  border-radius: 6px;
  padding: 6px;
}
.hist-q-img {
  max-width: 100%;
  max-height: 300px;
  object-fit: contain;
  border-radius: 4px;
  cursor: pointer;
}
.hist-q-img:hover { opacity: .85; }
.hist-q-text {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 10px 14px;
  width: 100%;
  font-size: 1.05em;
  overflow-x: auto;
}
.detail-answers {
  display: flex; flex-wrap: wrap; gap: 12px;
  font-size: .9em;
}
.detail-user-ans  { color: #333; }
.detail-correct-ans { color: #2e7d32; font-weight: 600; }

/* ── Question preview modal ──────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: pointer;
}
.modal-overlay[hidden] { display: none; }
.modal-content {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
  cursor: default;
  text-align: center;
}
.modal-content img {
  max-width: 100%;
  max-height: 75vh;
  object-fit: contain;
  border-radius: 8px;
}
.preview-answer {
  margin-top: 16px;
  padding: 12px 16px;
  background: #f5f5f5;
  border-radius: 8px;
  font-size: .95em;
  text-align: left;
}

/* ── Student test info box ────────────────────────────────────────────────── */
.test-info-box { margin: 16px 0; }
.info-active {
  background: #e8f5e9; border: 1px solid #a5d6a7;
  padding: 16px 20px; border-radius: 8px; color: #2e7d32;
}
.info-inactive {
  background: #fff3e0; border: 1px solid #ffcc80;
  padding: 16px 20px; border-radius: 8px; color: #e65100;
}
