:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --text: #111827;
  --text-secondary: #4b5563;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --success: #059669;
  --success-soft: #ecfdf5;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

button, input, select {
  font-family: inherit;
}

.hidden {
  display: none !important;
}

/* Loading */
.loading-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  gap: 12px;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Layout */
.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0 16px;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
  color: var(--text);
}

.brand-badge {
  background: var(--primary);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 8px;
  letter-spacing: 0.5px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  background: var(--surface);
  color: var(--text);
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
  padding: 7px 12px;
  font-size: 13px;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface);
  color: var(--text);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger-soft {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}

.btn-success-soft {
  background: var(--success-soft);
  color: var(--success);
  border-color: #a7f3d0;
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.btn-block {
  width: 100%;
  padding: 13px 16px;
  font-size: 15px;
}

/* Login View */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 26px;
  box-shadow: var(--shadow-lg);
}

.login-title {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
}

.login-sub {
  margin: 0 0 24px;
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
}

.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.input {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: var(--surface-soft);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

.input:focus {
  border-color: var(--primary);
  background: #fff;
}

.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 16px;
}

.alert-error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #fecaca;
}

.alert-warn {
  background: var(--warn-soft);
  color: #92400e;
  border: 1px solid #fde68a;
}

/* Dashboard Countdown & Total Reviews Banner */
.exam-banner {
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
  color: #fff;
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.22);
}

.exam-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.exam-label {
  font-size: 12px;
  opacity: 0.88;
  letter-spacing: 0.3px;
}

.exam-days-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.exam-days-num {
  font-size: 30px;
  font-weight: 800;
  line-height: 1.1;
}

.exam-days-unit {
  font-size: 13px;
  opacity: 0.92;
}

.exam-right {
  text-align: right;
  background: rgba(255, 255, 255, 0.14);
  padding: 8px 14px;
  border-radius: 12px;
  backdrop-filter: blur(4px);
}

.exam-total-num {
  font-size: 20px;
  font-weight: 800;
  line-height: 1.15;
}

.exam-total-label {
  font-size: 11px;
  opacity: 0.88;
}

/* Dashboard Hero */
.hero-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}

.hero-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.hero-greeting {
  font-size: 15px;
  font-weight: 600;
}

.hero-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}

.stat-box {
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 10px;
  text-align: center;
}

.stat-num {
  font-size: 24px;
  font-weight: 800;
  line-height: 1.2;
}

.stat-num.due { color: var(--primary); }
.stat-num.new { color: var(--warn); }
.stat-num.mastered { color: var(--success); }

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.hero-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.limit-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--border);
  font-size: 13px;
  color: var(--text-secondary);
}

.limit-select {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-soft);
  font-size: 13px;
  color: var(--text);
}

/* Filter & Chapters */
.section-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-secondary);
  margin: 20px 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.star-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.pill {
  padding: 7px 13px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.pill:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.pill-highlight {
  background: var(--primary-soft);
  border-color: #bfdbfe;
  color: var(--primary);
}

.chapter-list {
  display: grid;
  gap: 10px;
}

.chapter-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chapter-item:hover {
  border-color: #bfdbfe;
  box-shadow: var(--shadow);
}

.chapter-info {
  flex: 1;
  min-width: 0;
}

.chapter-name {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
}

.chapter-bar {
  height: 6px;
  background: #edf0f5;
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 4px;
}

.chapter-bar-fill {
  height: 100%;
  background: var(--success);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.chapter-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.chapter-due-badge {
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

/* Study View (Flashcard) */
.study-wrap {
  max-width: 680px;
  margin: 0 auto;
  padding: 12px 16px 120px;
}

.study-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 10px;
}

.study-progress-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.study-progress-bar {
  height: 5px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 16px;
}

.study-progress-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.25s ease;
}

.flashcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 24px 22px;
  box-shadow: var(--shadow);
  min-height: 280px;
  display: flex;
  flex-direction: column;
}

.card-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.card-tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-soft);
  padding: 3px 10px;
  border-radius: 999px;
}

.card-stars {
  font-size: 13px;
  color: #f59e0b;
  letter-spacing: 1px;
}

.card-topic {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.card-question {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.55;
  margin: 0 0 14px;
}

.card-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0 6px;
}

.card-img-thumb {
  max-width: 100%;
  max-height: 220px;
  object-fit: contain;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  padding: 4px;
  cursor: zoom-in;
}

.card-img-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.answer-divider {
  border: none;
  border-top: 1px dashed var(--border);
  margin: 18px 0 16px;
}

.answer-box {
  animation: fadeIn 0.18s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.answer-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.answer-list li {
  position: relative;
  padding-left: 16px;
  font-size: 15px;
  line-height: 1.65;
  color: #1f2937;
}

.answer-list li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--primary);
}

.answer-list strong {
  color: #0f172a;
}

.katex {
  font-size: 1.04em;
}

/* Bottom fixed action bar */
.study-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(245, 247, 251, 0.94);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  z-index: 20;
}

.study-footer-inner {
  max-width: 680px;
  margin: 0 auto;
}

.rating-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.rate-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 11px 8px;
  border-radius: 12px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.12s, filter 0.12s;
}

.rate-btn:active {
  transform: scale(0.97);
}

.rate-btn .rate-label {
  font-size: 15px;
  font-weight: 700;
}

.rate-btn .rate-sub {
  font-size: 11px;
  margin-top: 2px;
  opacity: 0.85;
}

.rate-forgot {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #fecaca;
}

.rate-fuzzy {
  background: var(--warn-soft);
  color: var(--warn);
  border-color: #fde68a;
}

.rate-familiar {
  background: var(--success-soft);
  color: var(--success);
  border-color: #a7f3d0;
}

/* Complete Screen */
.complete-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 24px;
  text-align: center;
  box-shadow: var(--shadow);
  margin-top: 24px;
}

.complete-icon {
  font-size: 44px;
  margin-bottom: 10px;
}

/* Admin Panel */
.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.admin-card h3 {
  margin: 0 0 12px;
  font-size: 16px;
}

.site-switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.status-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-dot.on { background: var(--success); }
.status-dot.off { background: var(--danger); }

.inline-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  align-items: end;
}

.user-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.user-row {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--surface-soft);
}

.user-row.disabled-user {
  opacity: 0.65;
  background: #fafafa;
}

.user-info {
  min-width: 180px;
}

.user-name {
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.badge-role {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
}

.badge-off {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--danger-soft);
  color: var(--danger);
}

.user-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.user-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.lightbox img {
  max-width: 96vw;
  max-height: 90vh;
  background: #fff;
  border-radius: 10px;
  padding: 6px;
}

.lightbox-close {
  position: fixed;
  top: 16px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 200;
}

@media (max-width: 480px) {
  .flashcard {
    padding: 20px 16px;
  }
  .card-question {
    font-size: 17px;
  }
  .hero-actions {
    grid-template-columns: 1fr;
  }
}
