/* ---------- Design Tokens ---------- */
:root {
  --bg: #f7f7fb;
  --bg-card: #ffffff;
  --bg-soft: #f1f2f7;
  --text: #0f172a;
  --text-soft: #475569;
  --text-muted: #94a3b8;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef2ff;
  --accent: #06b6d4;
  --success: #10b981;
  --success-soft: #d1fae5;
  --warning: #f59e0b;
  --error: #ef4444;
  --error-soft: #fee2e2;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 3px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 6px -1px rgba(15, 23, 42, 0.06), 0 2px 4px -2px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 20px 25px -5px rgba(15, 23, 42, 0.08), 0 8px 10px -6px rgba(15, 23, 42, 0.05);
  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

h1, h2, h3, h4 { color: var(--text); margin-top: 0; line-height: 1.25; }
h1 { font-size: 2rem; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 1.4rem; font-weight: 700; letter-spacing: -0.01em; }
h3 { font-size: 1.1rem; font-weight: 600; }

p { margin: 0 0 1rem; }
.muted { color: var(--text-muted); font-size: 0.9rem; }

/* ---------- Layout ---------- */
.main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}
.main.narrow { max-width: 720px; }
.main.center {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

/* ---------- Top nav ---------- */
.topnav {
  background: white;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}
.topnav-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  letter-spacing: -0.01em;
}
.brand:hover { color: var(--text); }
.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  box-shadow: 0 0 0 4px var(--primary-soft);
}
.nav-links {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 4px;
}
.nav-link {
  padding: 8px 14px;
  border-radius: 999px;
  font-weight: 500;
  color: var(--text-soft);
  font-size: 0.92rem;
  transition: all 0.15s ease;
}
.nav-link:hover {
  background: var(--bg-soft);
  color: var(--text);
}
.nav-link.active {
  background: var(--primary-soft);
  color: var(--primary-dark);
}
.nav-user {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: 8px;
  padding-left: 16px;
  border-left: 1px solid var(--border);
}
.user-pill {
  font-size: 0.85rem;
  color: var(--text-muted);
}
.nav-link.logout { color: var(--text-muted); }

/* ---------- Cards ---------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 20px; }
.card-title {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* ---------- Hero / login ---------- */
.hero {
  text-align: center;
  margin-bottom: 32px;
}
.hero .eyebrow {
  display: inline-block;
  padding: 6px 14px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-bottom: 16px;
}
.hero h1 {
  font-size: 2.4rem;
  margin-bottom: 12px;
  background: linear-gradient(135deg, #0f172a 0%, #4338ca 60%, #0891b2 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.hero p { color: var(--text-soft); max-width: 540px; margin: 0 auto 4px; }

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px 32px;
  box-shadow: var(--shadow-lg);
}
.auth-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 28px;
  font-weight: 700;
  font-size: 1.1rem;
}

/* ---------- Forms ---------- */
.form-group {
  margin-bottom: 18px;
}
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-soft);
  margin-bottom: 6px;
}
input[type="text"], input[type="password"], input[type="number"], select {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: white;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--primary-soft);
}
select {
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%23475569' d='M6 8L0 0h12z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 22px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s ease;
}
.btn:hover { background: #e5e7eb; }

.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.3);
}
.btn-primary:hover {
  background: var(--primary-dark);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}
.btn-primary:active { transform: translateY(0); }

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

.btn-ghost {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text-soft);
}
.btn-ghost:hover { background: var(--bg-soft); }

.btn-block { width: 100%; }
.btn-lg { padding: 14px 28px; font-size: 1rem; }
.btn-xl { padding: 18px 36px; font-size: 1.1rem; border-radius: var(--radius); }

/* ---------- Stats grid ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
}
.stat-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.stat-value {
  font-size: 2rem;
  font-weight: 700;
  margin-top: 8px;
  letter-spacing: -0.02em;
}
.stat-value .small { font-size: 1rem; color: var(--text-muted); font-weight: 500; }

/* ---------- Tables ---------- */
.table-wrap {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
thead {
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
}
th {
  text-align: left;
  padding: 12px 16px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
tr:last-child td { border-bottom: none; }
tr.you { background: var(--primary-soft); }
tr.you td { font-weight: 600; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-admin { background: #fde68a; color: #92400e; }
.badge-student { background: var(--primary-soft); color: var(--primary-dark); }
.badge-success { background: var(--success-soft); color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-empty { background: var(--bg-soft); color: var(--text-muted); }
.badge-rank { background: linear-gradient(135deg, #fbbf24, #f59e0b); color: white; }

/* ---------- Flashes ---------- */
.flashes { margin-bottom: 20px; }
.flash {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  font-size: 0.92rem;
  font-weight: 500;
}
.flash-success { background: var(--success-soft); color: #065f46; }
.flash-error { background: var(--error-soft); color: #991b1b; }
.flash-message, .flash-info { background: var(--primary-soft); color: var(--primary-dark); }

/* ---------- Inline form / row ---------- */
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  flex-wrap: wrap;
  gap: 12px;
}
.section-header h2 { margin: 0; }

/* ---------- Picture grid (admin) ---------- */
.picture-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 18px;
}
.picture-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.picture-thumb {
  aspect-ratio: 1 / 1;
  background: var(--bg-soft);
  overflow: hidden;
}
.picture-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.picture-meta {
  padding: 14px;
  border-top: 1px solid var(--border);
}
.picture-name {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 8px;
  word-break: break-all;
}

/* ---------- Welcome / start screen ---------- */
.welcome-card {
  max-width: 640px;
  margin: 0 auto;
}
.start-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  margin-top: 28px;
}
.toggle {
  display: inline-flex;
  background: var(--bg-soft);
  border-radius: 999px;
  padding: 4px;
  border: 1px solid var(--border);
}
.toggle input { display: none; }
.toggle label {
  padding: 10px 22px;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--text-soft);
  transition: all 0.15s ease;
  user-select: none;
}
.toggle input:checked + label {
  background: white;
  color: var(--primary-dark);
  box-shadow: var(--shadow-sm);
}

.instruction-list {
  list-style: none;
  padding: 0;
  margin: 24px 0 0;
}
.instruction-list li {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 14px;
  align-items: flex-start;
}
.instruction-list li:last-child { border-bottom: none; }
.instruction-list .num {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary-dark);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
}

/* ---------- Test screen ---------- */
.test-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 18px;
  padding: 32px 24px 48px;
  background: linear-gradient(180deg, #f7f7fb 0%, #e7e9f3 100%);
}
.test-progress {
  background: white;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-soft);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.image-stage {
  width: min(80vw, 600px);
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-lg);
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  position: relative;
}
.image-stage .loading {
  color: var(--text-muted);
  font-size: 0.95rem;
}
.image-stage .stage-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  /* No transition: opacity changes must be instantaneous for stimulus timing. */
  transition: none;
  user-select: none;
  -webkit-user-drag: none;
}
.image-stage .stage-img.visible {
  opacity: 1;
}

.answer-form {
  width: min(90vw, 620px);
  background: white;
  border-radius: var(--radius-lg);
  padding: 32px;
  box-shadow: var(--shadow-lg);
  /* Visible from the start so the layout doesn't shift. The form is
     made interactive only once the emotion image has been shown
     (see `.is-active` on the form, plus disabled radios in HTML). */
  opacity: 0.55;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
.answer-form.is-active {
  opacity: 1;
  pointer-events: auto;
}
.answer-form h2 { margin-bottom: 20px; text-align: center; }

.emotion-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 24px;
}
.emotion-option {
  display: block;
  position: relative;
  cursor: pointer;
}
.emotion-option input { position: absolute; opacity: 0; pointer-events: none; }
.emotion-option span {
  display: block;
  text-align: center;
  padding: 14px 12px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text-soft);
  transition: all 0.15s ease;
  background: white;
}
.emotion-option:hover span { border-color: var(--primary); color: var(--primary-dark); }
.emotion-option input:checked + span {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-dark);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* ---------- Results screen ---------- */
.results-hero {
  text-align: center;
  padding: 32px 0 24px;
}
.score-ring {
  width: 180px;
  height: 180px;
  margin: 0 auto 20px;
  border-radius: 50%;
  background:
    conic-gradient(var(--primary) calc(var(--p) * 1%), var(--bg-soft) 0);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.score-ring::before {
  content: "";
  position: absolute;
  inset: 12px;
  border-radius: 50%;
  background: var(--bg-card);
}
.score-ring .score {
  position: relative;
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.02em;
}
.score-ring .score small { font-size: 1rem; color: var(--text-muted); font-weight: 500; }

.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 24px 0;
}
.metric {
  background: var(--bg-soft);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
}
.metric .v { font-size: 1.4rem; font-weight: 700; }
.metric .l { font-size: 0.78rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }

/* ---------- Misc ---------- */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}
.empty-state .icon { font-size: 2.4rem; margin-bottom: 12px; }
.danger-zone {
  border: 1px solid #fecaca;
  background: #fff8f8;
}
hr.divider { border: none; border-top: 1px solid var(--border); margin: 24px 0; }

.kbd {
  display: inline-block;
  padding: 2px 8px;
  background: var(--bg-soft);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 0.85rem;
}

@media (max-width: 720px) {
  .topnav-inner { flex-wrap: wrap; }
  .nav-links { width: 100%; flex-wrap: wrap; }
  .hero h1 { font-size: 1.8rem; }
  .nav-user { padding-left: 0; border-left: none; margin-left: 0; }
}

/* ---------- Mode selection ---------- */
.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}
.mode-card {
  display: block;
  text-decoration: none;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  color: var(--text);
  position: relative;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: var(--shadow-sm);
}
.mode-card:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  color: var(--text);
}
.mode-card .mode-num {
  position: absolute;
  top: 18px;
  right: 18px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: white;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
  box-shadow: 0 4px 10px rgba(79, 70, 229, 0.25);
}
.mode-card h3 {
  margin: 8px 0 8px;
  font-size: 1.25rem;
  letter-spacing: -0.01em;
}
.mode-card p {
  margin: 0 0 16px;
  font-size: 0.92rem;
  line-height: 1.55;
}
.mode-card .mode-cta {
  display: inline-block;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--primary-dark);
}
.mode-card:hover .mode-cta {
  color: var(--primary);
}

/* Mode badges */
.badge-mode1 {
  background: #ecfeff;
  color: #0e7490;
}
.badge-mode2 {
  background: #f5f3ff;
  color: #6d28d9;
}
