:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #d97706;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

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

.container { max-width: 760px; margin: 0 auto; padding: 24px 16px; }
.container.wide { max-width: 1200px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 20px;
}

h1 { font-size: 1.6rem; margin: 0 0 8px; }
h2 { font-size: 1.2rem; margin: 0 0 16px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  transition: background 0.15s;
}
.btn:hover:not(:disabled) { background: var(--primary-dark); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn.secondary:hover:not(:disabled) { background: #f3f4f6; }
.btn.danger { background: #fff; color: var(--danger); border-color: var(--danger-bg); }
.btn.small { padding: 4px 10px; font-size: 0.85rem; }

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 12px 14px;
  font-size: 1.1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15); }

.form-row { display: flex; gap: 10px; margin-top: 16px; }
.login-form { display: grid; gap: 6px; margin-top: 18px; }
.login-form label { font-weight: 600; font-size: 0.9rem; margin-top: 6px; }
.login-form .btn { margin-top: 12px; padding: 13px; }
.code-input { text-transform: uppercase; letter-spacing: 0.15em; font-weight: 700; }
.code-input::placeholder { text-transform: none; letter-spacing: normal; font-weight: 400; }
.form-row input { flex: 1; }

.alert {
  padding: 12px 14px;
  border-radius: 8px;
  margin-top: 14px;
  white-space: pre-line;
}
.alert.error { background: var(--danger-bg); color: var(--danger); }
.alert.success { background: var(--success-bg); color: var(--success); }

.info-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 20px 0 4px; }
.info-grid div { background: var(--bg); border-radius: 8px; padding: 12px; text-align: center; }
.info-grid strong { display: block; font-size: 1.3rem; }
.info-grid span { font-size: 0.85rem; color: var(--muted); }

/* Quiz page */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.4rem;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 8px;
  background: var(--bg);
}
.timer.low { color: var(--danger); background: var(--danger-bg); }

.progress-wrap { margin-top: 16px; }
.progress-meta { display: flex; justify-content: space-between; font-size: 0.9rem; color: var(--muted); margin-bottom: 6px; }
.progress { height: 12px; background: var(--border); border-radius: 99px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--primary), #60a5fa); transition: width 0.4s; }

/* Timer and progress stay visible while scrolling */
.quiz-top { position: sticky; top: 0; z-index: 5; }

.dots-label { margin-top: 20px; font-size: 0.85rem; font-weight: 600; }
/* Scrolls instead of growing when a quiz has many questions */
.dots { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; max-height: 168px; overflow-y: auto; padding: 3px; position: relative; }
.dot {
  flex: none;
  min-width: 36px; height: 36px;
  padding: 0 4px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  font-weight: 600;
  cursor: pointer;
}
.dot.current { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary); }
.dot.correct { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.dot.partial { background: #fef3c7; color: var(--warning); border-color: var(--warning); }
.dot.wrong { background: var(--danger-bg); color: var(--danger); border-color: var(--danger); }

.question-number { color: var(--muted); font-weight: 600; }
.question-text { font-size: 1.2rem; font-weight: 600; margin: 6px 0 18px; }

.options { display: grid; gap: 10px; }
.option {
  display: flex;
  gap: 12px;
  align-items: center;
  text-align: left;
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
  color: var(--text);
}
.option:hover:not(:disabled) { border-color: var(--primary); transform: translateX(3px); }
.option:disabled { cursor: default; }
.option .letter {
  flex: none;
  width: 30px; height: 30px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--bg);
  font-weight: 700;
}
.option.correct { border-color: var(--success); background: var(--success-bg); }
.option.correct .letter { background: var(--success); color: #fff; }
.option.wrong { border-color: var(--danger); background: var(--danger-bg); }
.option.wrong .letter { background: var(--danger); color: #fff; }
.option.shake { animation: shake 0.35s; }
@keyframes shake {
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

.feedback { margin-top: 16px; font-weight: 600; min-height: 1.5em; }
.feedback.good { color: var(--success); }
.feedback.bad { color: var(--danger); }
.feedback.warn { color: var(--warning); }

.nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.btn, .option, .dot { touch-action: manipulation; }

.result-score { font-size: 3rem; font-weight: 800; color: var(--primary); margin: 10px 0; }

.rules { margin: 12px 0 0; padding-left: 18px; color: var(--muted); font-size: 0.95rem; }

/* Anti-cheating */
.no-copy { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.violations { color: var(--danger); font-weight: 600; }
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.6);
  display: grid; place-items: center;
  padding: 16px;
  z-index: 10;
}
.modal { background: var(--card); border-radius: var(--radius); padding: 24px; max-width: 440px; box-shadow: var(--shadow); text-align: center; }
.modal h2 { color: var(--danger); }
.badge.cheating { background: var(--danger-bg); color: var(--danger); }

/* Admin */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.toolbar select { padding: 9px; border-radius: 8px; border: 1px solid var(--border); font-size: 1rem; max-width: 100%; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--bg); border-radius: 10px; padding: 14px; }
.stat strong { display: block; font-size: 1.5rem; }
.stat span { font-size: 0.85rem; color: var(--muted); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { background: var(--bg); position: sticky; top: 0; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 0.8rem; font-weight: 600; background: var(--bg); }
.badge.finished { background: var(--success-bg); color: var(--success); }
.badge.in_progress { background: #dbeafe; color: var(--primary); }
.badge.expired { background: #fef3c7; color: var(--warning); }
.badge.test { background: #ede9fe; color: #6d28d9; }
.chart-scroll { overflow-x: auto; }
.chart-box { position: relative; height: 380px; min-width: 100%; }
.time-form { display: inline-flex; gap: 6px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.time-form input { width: 90px; padding: 6px 8px; font-size: 1rem; border: 1px solid var(--border); border-radius: 8px; }
.upload-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.upload-grid .card { margin: 0; }
input[type="file"] { margin: 10px 0; max-width: 100%; }

@media (max-width: 560px) {
  .container { padding: 12px; }
  .card { padding: 16px; margin-bottom: 12px; }
  h1 { font-size: 1.25rem; }
  .info-grid { gap: 8px; }
  .info-grid div { padding: 10px 4px; }
  .form-row { flex-direction: column; }
  .form-row .btn { padding: 14px; }

  /* Compact sticky header: title + timer on one line */
  .quiz-top { border-radius: 0 0 var(--radius) var(--radius); margin: -12px -12px 12px; padding: 12px 16px; }
  .quiz-header { flex-wrap: nowrap; }
  .quiz-header h1 { font-size: 1rem; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .quiz-header > div:first-child { min-width: 0; }
  #student { font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .timer { font-size: 1.15rem; padding: 4px 10px; flex: none; }
  .progress-wrap { margin-top: 10px; }
  .progress-meta { font-size: 0.8rem; gap: 8px; }

  .question-text { font-size: 1.05rem; }
  .option { padding: 14px 12px; }
  .option:hover:not(:disabled) { transform: none; }

  /* Prev / Next side by side, Finish full width below */
  .nav { display: grid; grid-template-columns: 1fr 1fr; }
  #prev-btn { order: 1; }
  #next-btn { order: 2; }
  #finish-btn { order: 3; grid-column: 1 / -1; }
  .nav .btn { padding: 13px 10px; }

  .result-score { font-size: 2.4rem; }
  #result-card .btn { width: 100%; margin-top: 8px; }

  .chart-box { height: 300px; }
  .toolbar > * { flex: 1 1 100%; min-width: 0; }
}

/* Staff pages */
.card.narrow { max-width: 460px; margin: 40px auto; }
.topbar, .section-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar h1 { margin: 0; }
.section-head { margin-bottom: 8px; }
.section-head h2 { margin: 0; }
h3 { font-size: 1.05rem; margin: 0 0 6px; }
.small { font-size: 0.8rem; }
td.wrap { white-space: normal; min-width: 180px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; min-width: 260px; }
tr.selected td { background: #eff6ff; }
.code { font-family: ui-monospace, Menlo, monospace; font-weight: 700; letter-spacing: 0.08em; background: var(--bg); padding: 3px 8px; border-radius: 6px; margin-right: 6px; }
.upload-box { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.add-row input[type="text"], .search { width: auto; flex: 1 1 160px; padding: 9px 12px; font-size: 1rem; }
.upload-row { margin-top: 14px; }
.search { width: 100%; margin: 16px 0 8px; border: 1px solid var(--border); border-radius: 8px; }
.students-table { max-height: 420px; overflow-y: auto; }
.login-form label input { display: block; width: 100%; margin-top: 4px; }

/* On/off switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.slider { position: relative; width: 42px; height: 24px; background: #cbd5e1; border-radius: 99px; transition: background 0.2s; flex: none; }
.slider::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform 0.2s; }
.switch input:checked + .slider { background: var(--success); }
.switch input:checked + .slider::after { transform: translateX(18px); }
.switch input:focus-visible + .slider { box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.3); }
.switch-label { font-weight: 600; font-size: 0.9rem; }

.dialog { border: none; border-radius: var(--radius); padding: 24px; width: min(420px, calc(100vw - 32px)); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(15, 23, 42, 0.5); }
.dialog h2 { margin-bottom: 4px; }

/* Question analysis page */
.field-label { display: block; font-weight: 600; margin-bottom: 6px; }
.quiz-select { flex: 1 1 320px; padding: 10px; font-size: 1rem; border: 1px solid var(--border); border-radius: 8px; background: #fff; max-width: 100%; }
#quiz-meta { margin-top: 8px; }
.question-cell { min-width: 260px; max-width: 480px; }
.rate { font-weight: 700; padding: 2px 8px; border-radius: 99px; }
.rate.easy { background: var(--success-bg); color: var(--success); }
.rate.medium { background: #fef3c7; color: var(--warning); }
.rate.hard { background: var(--danger-bg); color: var(--danger); }
