:root {
  --bg: #f6f4ff;
  --card-bg: #ffffff;
  --ink: #2c2a3a;
  --ink-soft: #6b6880;
  --primary: #7c5cff;
  --primary-dark: #6a49f2;
  --success: #2f9e6e;
  --error: #d64545;
  --warn: #ffb703;
  --border: #e6e2f5;
  --radius: 16px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 28px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  font-size: 1.3rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-nav a {
  margin-left: 18px;
  color: var(--ink-soft);
  text-decoration: none;
  font-weight: 600;
}

.topbar-nav a:hover { color: var(--primary); }

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 28px 80px;
}

.page-title { margin-top: 0; }

/* Flash messages */
.flash-stack { margin-bottom: 16px; }
.flash {
  padding: 10px 16px;
  border-radius: 10px;
  margin-bottom: 8px;
  font-weight: 600;
}
.flash-success { background: #e2f7ec; color: var(--success); }
.flash-error { background: #fbe6e6; color: var(--error); }

/* Buttons */
.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.95rem;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:disabled { background: #c8c2e8; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-small { padding: 6px 12px; font-size: 0.85rem; border-radius: 8px; }
.btn-muted { background: #f0eefa; color: var(--ink-soft); }
.btn-muted:hover { background: #e6e2f5; }

/* Login */
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
}
.login-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 40px;
  width: 340px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(90, 70, 200, 0.12);
}
.login-emoji { font-size: 2.5rem; }
.login-card form { display: flex; flex-direction: column; gap: 6px; margin-top: 20px; text-align: left; }
.login-card label { font-size: 0.85rem; font-weight: 600; color: var(--ink-soft); margin-top: 8px; }
.login-card input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 1rem;
}
.login-card button { margin-top: 16px; }

/* Dashboard toolbar */
.dashboard-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}
.toolbar-left { display: flex; align-items: center; gap: 12px; }
.select-all-label { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.selection-count { color: var(--ink-soft); font-size: 0.9rem; }

.student-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
}

.student-card {
  position: relative;
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px 12px 14px;
  text-align: center;
  box-shadow: 0 4px 14px rgba(90, 70, 200, 0.08);
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.1s ease, border-color 0.15s ease;
}
.student-card:hover { transform: translateY(-2px); }
.student-card.selected { border-color: var(--primary); }
.student-card.must-spend { border-color: var(--warn); }

.student-select {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 18px;
  height: 18px;
}

.must-spend-ribbon {
  position: absolute;
  top: 8px;
  right: -4px;
  background: var(--warn);
  color: #4a3400;
  font-size: 0.65rem;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 6px;
  text-transform: uppercase;
}

.avatar-wrap img { width: 64px; height: 64px; margin-top: 8px; }
.student-name { font-weight: 700; margin-top: 6px; }
.level-badge {
  display: inline-block;
  background: #f0eefa;
  color: var(--primary-dark);
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 12px;
  margin-top: 4px;
}
.progress-bar {
  background: #eee9fb;
  border-radius: 6px;
  height: 8px;
  margin-top: 10px;
  overflow: hidden;
}
.progress-fill {
  background: linear-gradient(90deg, var(--primary), #a78bfa);
  height: 100%;
  transition: width 0.4s ease;
}
.progress-label { font-size: 0.7rem; color: var(--ink-soft); margin-top: 4px; }
.gold-coins { margin-top: 8px; font-weight: 700; }
.coin-icon { margin-right: 4px; }

/* Audit log */
.audit-list { list-style: none; padding: 0; margin: 0; max-height: 420px; overflow-y: auto; }
.audit-entry {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}
.audit-time { display: block; color: var(--ink-soft); font-size: 0.72rem; }
.audit-reward .audit-text strong:last-of-type { color: var(--success); }
.audit-spend .audit-text strong:last-of-type { color: var(--primary-dark); }

.empty-state { color: var(--ink-soft); font-style: italic; }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(30, 20, 60, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: white;
  border-radius: var(--radius);
  padding: 24px;
  width: 380px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  border: none;
  background: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--ink-soft);
}
.modal-tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.tab-btn {
  flex: 1;
  padding: 8px;
  border: none;
  background: #f0eefa;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 700;
  color: var(--ink-soft);
}
.tab-btn.active { background: var(--primary); color: white; }
.tab-panel.hidden { display: none; }
.modal-hint { color: var(--ink-soft); font-size: 0.9rem; }

.option-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.option-btn {
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fafaff;
  cursor: pointer;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}
.option-btn:hover:not(:disabled) { border-color: var(--primary); background: #f0eefa; }
.option-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Level up */
.level-up-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(30, 20, 60, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.level-up-backdrop.hidden { display: none; }
.level-up-card {
  background: white;
  border-radius: 20px;
  padding: 40px 50px;
  text-align: center;
  position: relative;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.level-up-emoji { font-size: 3.5rem; }
.level-up-text { font-size: 1.4rem; font-weight: 800; margin: 12px 0 20px; color: var(--primary-dark); }

.confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(circle, #ffb703 4px, transparent 4px),
    radial-gradient(circle, #7c5cff 4px, transparent 4px),
    radial-gradient(circle, #2f9e6e 4px, transparent 4px),
    radial-gradient(circle, #d64545 4px, transparent 4px);
  background-size: 60px 60px;
  animation: confetti-fall 1.6s linear infinite;
  opacity: 0.7;
}
@keyframes confetti-fall {
  from { background-position: 0 -100px, 20px -140px, 40px -60px, 10px -180px; }
  to { background-position: 0 400px, 20px 400px, 40px 400px, 10px 400px; }
}

/* Settings */
.settings-section {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 20px 24px;
  margin-bottom: 20px;
}
.settings-section h2 { margin-top: 0; }
.section-hint { color: var(--ink-soft); font-size: 0.85rem; margin-top: -8px; }

.inline-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.inline-form label { display: flex; flex-direction: column; font-size: 0.8rem; font-weight: 600; color: var(--ink-soft); gap: 4px; }
.inline-form input, .inline-form select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 0.9rem;
}
.add-form { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--border); }

.settings-list { list-style: none; padding: 0; margin: 0; }
.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.settings-row.archived { opacity: 0.5; }
.row-avatar { width: 32px; height: 32px; }
.row-name { font-weight: 700; flex-shrink: 0; }
.row-value { color: var(--ink-soft); font-weight: 600; }
.row-edit { margin-left: auto; }
.row-edit summary { cursor: pointer; font-size: 0.85rem; color: var(--primary-dark); font-weight: 600; }
.row-edit form { margin-top: 8px; }
.badge {
  background: #f0eefa;
  color: var(--ink-soft);
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 8px;
  font-weight: 700;
}
