:root {
  --bg: #f4f6fb;
  --bg-grad: linear-gradient(180deg, #f4f6fb 0%, #eef2f9 100%);
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --soft: #f1f5f9;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef2ff;
  --success: #10b981;
  --success-soft: #d1fae5;
  --warning: #f59e0b;
  --warning-soft: #fef3c7;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --border: #e2e8f0;
  --border-soft: #f1f5f9;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow: 0 2px 6px rgba(15, 23, 42, 0.05), 0 8px 24px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 8px 32px rgba(15, 23, 42, 0.12);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 8px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-grad);
  min-height: 100vh;
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter,
    "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wrap {
  max-width: 760px;
  margin: 0 auto;
  padding: 32px 16px 80px;
}

.wrap.narrow { max-width: 460px; }
.wrap.wide { max-width: 1100px; }

h1 {
  font-size: 1.75rem;
  margin: 0 0 8px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

h2 { font-size: 1.25rem; margin: 0 0 8px; }
h3 { font-size: 1rem; margin: 0 0 8px; color: var(--muted); font-weight: 600; }

.hint {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0 0 24px;
}

.hero {
  text-align: center;
  margin-bottom: 24px;
}

.hero .badge {
  display: inline-block;
  padding: 4px 12px;
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 99px;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 12px;
}

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

.card.success {
  border-left: 4px solid var(--success);
  background: linear-gradient(135deg, #ffffff 0%, #f0fdf4 100%);
}

label {
  display: block;
  margin-bottom: 16px;
}

label > span {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

input[type="text"],
input[type="password"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-xs);
  font-size: 1rem;
  font-family: inherit;
  background: white;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

textarea { min-height: 100px; resize: vertical; }

input:hover, select:hover, textarea:hover { border-color: #cbd5e1; }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

button {
  appearance: none;
  border: 1.5px solid var(--border);
  background: white;
  color: var(--text);
  padding: 10px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: var(--radius-xs);
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

button:hover { background: var(--soft); border-color: #cbd5e1; }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

button.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
button.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
}

button.big { padding: 14px 32px; font-size: 1.05rem; width: 100%; }

button.danger {
  background: white;
  color: var(--danger);
  border-color: var(--danger-soft);
}
button.danger:hover { background: var(--danger-soft); }

.error {
  color: var(--danger);
  font-size: 0.9rem;
  margin: 12px 0 0;
  padding: 10px 12px;
  background: var(--danger-soft);
  border-radius: var(--radius-xs);
}

.footnote {
  text-align: center;
  margin-top: 32px;
  color: var(--muted);
  font-size: 0.85rem;
}
.footnote a { color: var(--primary); font-weight: 600; text-decoration: none; }
.footnote a:hover { text-decoration: underline; }

/* === Top bar (test) === */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(160%) blur(8px);
  -webkit-backdrop-filter: saturate(160%) blur(8px);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

#studentInfo { font-size: 0.9rem; color: var(--muted); }
#studentInfo b { color: var(--text); }

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 8px 14px;
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: var(--radius-xs);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.timer::before { content: "⏱"; font-size: 1rem; }
.timer.warn { background: var(--warning-soft); color: #92400e; }
.timer.danger {
  background: var(--danger-soft);
  color: #b91c1c;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* === Progress bar === */
.progress-bar {
  position: sticky;
  top: 56px;
  z-index: 9;
  background: var(--border-soft);
  height: 3px;
  width: 100%;
}
.progress-bar .fill {
  height: 100%;
  background: var(--primary);
  width: 0%;
  transition: width 0.3s;
}

/* === Group cards === */
.group {
  margin-bottom: 28px;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  background: var(--card);
}

.group-header {
  background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  color: white;
  padding: 18px 22px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.group-header .code {
  font-size: 0.75rem;
  background: rgba(255, 255, 255, 0.22);
  padding: 4px 10px;
  border-radius: 99px;
  font-weight: 700;
  letter-spacing: 0.02em;
  flex-shrink: 0;
}

.group-header .name {
  font-weight: 600;
  font-size: 1rem;
  line-height: 1.35;
}

.group-body { padding: 0; }

/* === Question === */
.question {
  padding: 20px 22px;
  border-bottom: 1px solid var(--border-soft);
  transition: background 0.15s, border-color 0.15s;
}
.question:last-child { border-bottom: none; }

.question.flagged {
  background: var(--danger-soft);
  border-left: 3px solid var(--danger);
}
.question.answered {
  background: linear-gradient(90deg, #f0fdf4 0%, transparent 40%);
  border-left: 3px solid var(--success);
}

.q-text {
  font-weight: 500;
  margin-bottom: 14px;
  font-size: 1rem;
  line-height: 1.45;
}

.q-num {
  display: inline-block;
  min-width: 24px;
  height: 24px;
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 700;
  text-align: center;
  line-height: 24px;
  margin-right: 8px;
}

.q-text .points {
  display: inline-block;
  background: var(--soft);
  color: var(--muted);
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 600;
  margin-left: 8px;
  vertical-align: middle;
}

/* === Options === */
.options { margin: 0; }
.options label {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-xs);
  margin-bottom: 6px;
  cursor: pointer;
  border: 1.5px solid var(--border);
  background: white;
  transition: all 0.12s;
}
.options label:last-child { margin-bottom: 0; }
.options label:hover { border-color: #cbd5e1; background: var(--soft); }
.options label.selected {
  background: var(--primary-soft);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.08);
}
.options input { margin-top: 3px; flex-shrink: 0; accent-color: var(--primary); }
.options label > span { font-size: 0.95rem; line-height: 1.45; color: var(--text); flex: 1; }

/* === Match / Order / Fill === */
.match-row, .order-row, .fill-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.match-row > span, .order-row > span {
  flex: 1;
  min-width: 200px;
  font-size: 0.95rem;
}
.match-row select, .order-row select, .fill-row select {
  min-width: 90px;
  max-width: 100%;
  width: auto;
  padding: 8px 10px;
  font-size: 0.92rem;
}
.fill-row { padding: 4px 0; }
.fill-row > span { font-weight: 500; font-size: 0.95rem; }

/* === Submit === */
.actions {
  margin-top: 32px;
  display: flex;
  justify-content: center;
}

/* === Modal === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fadeIn 0.15s ease-out;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: white;
  border-radius: var(--radius);
  padding: 28px;
  max-width: 520px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.18s ease-out;
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.modal-content h3 {
  font-size: 1.1rem;
  color: var(--text);
  margin-bottom: 14px;
}

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

/* === Result summary === */
#autoResult {
  background: var(--soft);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin: 16px 0;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.92rem;
}
#autoResult .row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
}
#autoResult .total {
  border-top: 1px solid var(--border);
  margin-top: 8px;
  padding-top: 10px;
  font-size: 1rem;
}

/* === Admin table === */
table.admin {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  background: white;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
table.admin th, table.admin td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--border-soft);
}
table.admin th {
  background: var(--soft);
  font-weight: 700;
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
table.admin tr:hover td { background: var(--soft); }
table.admin tr:last-child td { border-bottom: none; }

.tag {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.tag.pending { background: var(--warning-soft); color: #92400e; }
.tag.done { background: var(--success-soft); color: #065f46; }

.admin-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: flex-end;
}
.admin-bar label { margin: 0; }
.admin-bar > .spacer { flex: 1; }

.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--muted);
}
.empty-state .emoji { font-size: 2.5rem; margin-bottom: 12px; }

/* === Tabs === */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--soft);
  padding: 4px;
  border-radius: var(--radius-xs);
  width: fit-content;
}
.tab {
  background: transparent;
  border: none;
  padding: 8px 16px;
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}
.tab:hover { background: rgba(255, 255, 255, 0.5); color: var(--text); }
.tab.active {
  background: white;
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

/* === Access code cards === */
.code-card {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  box-shadow: var(--shadow-sm);
}
.code-card.expired, .code-card.exhausted {
  opacity: 0.55;
  background: var(--soft);
}
.code-card .code-value {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--primary);
  padding: 8px 14px;
  background: var(--primary-soft);
  border-radius: var(--radius-xs);
  flex-shrink: 0;
}
.code-card .code-meta { flex: 1; min-width: 200px; }
.code-card .code-meta .label { font-weight: 600; margin-bottom: 4px; }
.code-card .code-meta .sub { color: var(--muted); font-size: 0.85rem; }
.code-card .code-actions { display: flex; gap: 8px; }
.code-card { cursor: default; }
.code-card.expandable { cursor: pointer; transition: box-shadow 0.15s; }
.code-card.expandable:hover { box-shadow: var(--shadow); }
.code-card .expand-arrow {
  font-size: 0.85rem;
  color: var(--muted);
  transition: transform 0.2s;
}
.code-card.expanded .expand-arrow { transform: rotate(180deg); }

.students-list {
  width: 100%;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-soft);
}
.students-list .empty {
  color: var(--muted);
  font-size: 0.88rem;
  padding: 8px 0;
}
.student-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 0.9rem;
}
.student-row:last-child { border-bottom: none; }
.student-row .name { flex: 1; font-weight: 500; }
.student-row .class {
  background: var(--soft);
  color: var(--muted);
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 600;
}
.student-row .time { font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.student-row .score {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-weight: 600;
  font-size: 0.85rem;
}
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 700;
}
.status-badge.in_progress { background: var(--warning-soft); color: #92400e; }
.status-badge.in_progress::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; animation: pulse 1.5s infinite; }
.status-badge.submitted { background: var(--success-soft); color: #065f46; }
.status-badge.finalized { background: var(--primary-soft); color: var(--primary); }
.code-card .usage-bar {
  height: 4px;
  background: var(--border-soft);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 6px;
}
.code-card .usage-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.3s;
}

.big-code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 3.5rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 24px;
  border-radius: var(--radius);
  margin: 16px 0;
  font-variant-numeric: tabular-nums;
}

/* === Open-answer card in admin === */
.open-card {
  background: var(--soft);
  padding: 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  border: 1px solid var(--border);
}
.open-card .group-label {
  display: inline-block;
  background: var(--primary-soft);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 700;
  margin-bottom: 8px;
}
.open-card .answer-box {
  background: white;
  padding: 12px;
  border-radius: var(--radius-xs);
  border: 1px solid var(--border);
  margin: 10px 0 12px;
  font-style: italic;
  color: var(--text);
  white-space: pre-wrap;
}
.open-card .answer-box.empty { color: var(--muted); font-style: normal; }
.open-card .grade-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.open-card .grade-input-row input { width: 80px; }

@media (max-width: 600px) {
  .wrap { padding: 20px 12px 60px; }
  h1 { font-size: 1.4rem; }
  .topbar { padding: 10px 14px; }
  .group-header { padding: 14px 16px; }
  .group-header .name { font-size: 0.92rem; }
  .question { padding: 16px; }
  table.admin { font-size: 0.78rem; }
  table.admin th, table.admin td { padding: 8px 6px; }
  .modal-content { padding: 22px; }
  .admin-bar { flex-direction: column; align-items: stretch; }
  .admin-bar > .spacer { display: none; }
}
