:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --primary: #1b365d;
  --primary-hover: #142947;
  --accent: #2563eb;
  --green: #16a34a;
  --green-bg: #ecfdf5;
  --red: #dc2626;
  --red-bg: #fef2f2;
  --amber: #d97706;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', 'Noto Sans JP', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

.view { display: none; }
.view.active { display: block; }
.screen { display: none; }
.screen.active { display: block; }

/* LOGIN */
#login-view.active {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  background: linear-gradient(135deg, #1b365d 0%, #2b5b84 100%);
}
.login-card {
  background: var(--surface);
  width: 100%;
  max-width: 440px;
  padding: 36px 32px;
  border-radius: 18px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  text-align: center;
}
.brand-badge {
  display: inline-block;
  background: #e0ecff;
  color: var(--primary);
  font-weight: 700;
  font-size: 13px;
  padding: 5px 14px;
  border-radius: 999px;
  margin-bottom: 12px;
}
.login-card h1 { font-size: 26px; color: var(--primary); margin-bottom: 8px; }
.subtitle { color: var(--muted); font-size: 14px; margin-bottom: 24px; }
.form-group { text-align: left; margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text); }
.form-group input, textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.2s;
}
.form-group input:focus, textarea:focus { outline: none; border-color: var(--accent); }
.admin-toggle-row { text-align: left; margin-bottom: 18px; }
.checkbox-label, .radio-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
}
.error-box {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid #fecaca;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
  text-align: left;
}
.info-box {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid #bbf7d0;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-top: 12px;
}
.login-footnote { margin-top: 18px; font-size: 12px; color: var(--muted); }

/* TOPBAR */
.topbar {
  background: var(--primary);
  color: #fff;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}
.logo-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-badge {
  background: rgba(255,255,255,0.16);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
}

/* BUTTONS */
.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: #e0ecff; color: var(--primary); }
.btn-secondary:hover { background: #cde0ff; }
.btn-outline { background: transparent; border: 1.5px solid var(--border); color: var(--text); }
.topbar .btn-outline { border-color: rgba(255,255,255,0.4); color: #fff; }
.btn-ghost { background: transparent; color: var(--muted); }
.topbar .btn-ghost { color: rgba(255,255,255,0.85); }
.btn-danger { background: var(--red-bg); color: var(--red); padding: 6px 12px; font-size: 13px; }
.btn-sm { padding: 7px 12px; font-size: 13px; }
.btn-xs { padding: 4px 10px; font-size: 12px; }
.btn-full { width: 100%; padding: 13px; font-size: 15px; }

/* LAYOUT */
.container { max-width: 1180px; margin: 0 auto; padding: 24px 16px 60px; }
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  padding: 18px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.stat-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat-val { font-size: 28px; font-weight: 800; margin: 4px 0; }
.stat-sub { font-size: 12px; color: var(--muted); }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-blue { color: var(--accent); }

/* QUICK ACTIONS */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 24px;
}
.action-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  text-align: left;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.15s, border-color 0.15s;
}
.action-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.action-icon { font-size: 28px; flex-shrink: 0; }
.action-body { flex: 1; }
.action-body h3 { font-size: 15px; color: var(--primary); margin-bottom: 3px; }
.action-body p { font-size: 12.5px; color: var(--muted); line-height: 1.4; }
.action-pill {
  background: #eef2ff;
  color: var(--primary);
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.action-review .action-pill { background: var(--red-bg); color: var(--red); }

/* TABS */
.tabs-bar {
  display: flex;
  gap: 8px;
  border-bottom: 2px solid var(--border);
  margin-bottom: 18px;
}
.tab-btn {
  background: none;
  border: none;
  padding: 12px 20px;
  font-size: 15px;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
}
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; }
.tab-content.active { display: block; }

.filter-bar {
  background: var(--surface);
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.group-heading { font-size: 16px; color: var(--primary); margin: 18px 0 12px; }

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 14px;
}
.item-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.item-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.item-title { font-size: 15px; font-weight: 700; color: var(--primary); margin-bottom: 4px; }
.item-jp { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.item-footer { display: flex; justify-content: space-between; align-items: center; font-size: 12.5px; color: var(--muted); }
.progress-bar { height: 6px; background: #e2e8f0; border-radius: 99px; overflow: hidden; margin: 8px 0; }
.progress-fill { height: 100%; background: var(--green); transition: width 0.3s; }

/* QUIZ SCREEN */
.quiz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.quiz-header-left, .quiz-header-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.quiz-title { font-size: 18px; color: var(--primary); }
.timer-pill {
  background: #fef3c7;
  color: #92400e;
  font-weight: 800;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 14px;
}
.progress-pill {
  background: #e0ecff;
  color: var(--primary);
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
}

.result-banner {
  padding: 18px 22px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}
.result-banner.pass { background: var(--green-bg); border: 1.5px solid #86efac; color: #166534; }
.result-banner.fail { background: var(--red-bg); border: 1.5px solid #fca5a5; color: #991b1b; }

.quiz-layout { display: grid; grid-template-columns: 1fr 270px; gap: 18px; align-items: start; }
.question-card, .explanation-card, .sidebar-card, .card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px 24px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.q-meta-row { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.q-badge {
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 12.5px;
  padding: 4px 10px;
  border-radius: 6px;
}
.q-source { font-size: 12.5px; color: var(--muted); flex: 1; }
.q-text {
  font-size: 17px;
  font-weight: 700;
  color: #0f172a;
  margin-bottom: 20px;
  line-height: 1.75;
  white-space: pre-wrap;
}

.options-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
.opt-btn {
  text-align: left;
  background: #f8fafc;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.6;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text);
}
.opt-btn:hover:not(:disabled) { border-color: var(--accent); background: #eff6ff; }
.opt-btn.selected { border-color: var(--accent); background: #dbeafe; font-weight: 600; }
.opt-btn.correct { border-color: var(--green); background: var(--green-bg); color: #14532d; font-weight: 600; }
.opt-btn.wrong { border-color: var(--red); background: var(--red-bg); color: #7f1d1d; }

.q-nav-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }

/* EXPLANATION */
.explanation-card { margin-top: 16px; border-left: 5px solid var(--primary); }
.exp-header { font-size: 16px; font-weight: 800; color: var(--primary); margin-bottom: 14px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.exp-body h2 { font-size: 15px; color: #1d4ed8; margin: 16px 0 8px; }
.exp-body h3 { font-size: 14.5px; color: var(--primary); margin: 14px 0 6px; background: #f1f5f9; padding: 6px 10px; border-radius: 6px; border-left: 3px solid var(--primary); }
.exp-body p { margin: 6px 0; font-size: 14.5px; }
.exp-body ul { margin: 4px 0 10px 20px; font-size: 14px; }
.exp-body li { margin: 4px 0; }
.exp-answer-box {
  background: var(--green-bg);
  border: 1.5px solid #86efac;
  color: #166534;
  padding: 10px 14px;
  border-radius: 8px;
  font-weight: 800;
  margin-top: 14px;
}
.tag-dung { color: var(--green); font-weight: 800; }
.tag-sai { color: var(--red); font-weight: 800; }

/* SIDEBAR GRID */
.sidebar-title { font-size: 14px; font-weight: 700; color: var(--primary); margin-bottom: 10px; }
.q-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 12px; max-height: 420px; overflow-y: auto; padding-right: 4px; }
.q-cell {
  border: 1px solid var(--border);
  background: #f8fafc;
  border-radius: 6px;
  padding: 7px 0;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  position: relative;
}
.q-cell.current { border-color: var(--primary); background: var(--primary); color: #fff; }
.q-cell.answered { background: #dbeafe; border-color: #93c5fd; color: #1e40af; }
.q-cell.correct { background: var(--green-bg); border-color: #86efac; color: #166534; }
.q-cell.wrong { background: var(--red-bg); border-color: #fca5a5; color: #991b1b; }
.q-cell.flagged::after { content: "🚩"; position: absolute; top: -4px; right: -2px; font-size: 9px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 11.5px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }
.dot-current { background: var(--primary); }
.dot-correct { background: var(--green); }
.dot-wrong { background: var(--red); }
.dot-answered { background: #93c5fd; }

/* ADMIN */
.admin-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.mt-12 { margin-top: 12px; }
.muted { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.email-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; max-height: 440px; overflow-y: auto; }
.email-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}

/* LOADING */
.loading-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,0.5);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: #fff; z-index: 100; gap: 12px;
}
.spinner {
  width: 40px; height: 40px; border: 4px solid rgba(255,255,255,0.3);
  border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .quick-actions { grid-template-columns: 1fr; }
  .quiz-layout { grid-template-columns: 1fr; }
  .admin-grid { grid-template-columns: 1fr; }
}
