:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --sidebar-bg: #f3f2f5;
  --sidebar-active: #e1e1f1;
  --line: #e5e5e5;
  --text: #333238;
  --muted: #666666;
  --primary: #1f75cb;
  --primary-dark: #1068bf;
  --green: #108548;
  --green-bg: #ecf4ee;
  --orange: #ab6100;
  --orange-bg: #fdf1dd;
  --red: #dd2b0e;
  --red-bg: #fdf1f1;
  --blue-bg: #e9f3fc;
  --purple: #6f42c1;
  --radius: 4px;
  --font: "GitLab Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
}

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

h1 {
  font-size: 23px;
  font-weight: 600;
  margin: 0 0 16px;
  color: var(--text);
}

h2 {
  font-size: 17px;
  font-weight: 600;
  margin: 0 0 12px;
  color: var(--text);
}

.app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 256px 1fr;
  background: var(--bg);
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  color: var(--text);
  padding: 0;
  border-right: 1px solid var(--line);
}

.sidebar-header {
  padding: 18px 16px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.brand-title {
  font-size: 17px;
  font-weight: 700;
  line-height: 1.25;
  color: var(--text);
  letter-spacing: -0.02em;
}

.brand-portal {
  display: inline-flex;
  align-self: flex-start;
  align-items: center;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  border-radius: var(--radius);
  border: 1px solid transparent;
}

.brand-portal-teacher {
  color: var(--primary);
  background: var(--blue-bg);
  border-color: #b8d9f5;
}

.brand-portal-student {
  color: var(--green);
  background: var(--green-bg);
  border-color: #b8dfc4;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px 20px;
  overflow-y: auto;
}

.nav-section {
  margin: 0 0 8px;
  padding: 0 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.sidebar a { color: var(--text); }

.nav-link {
  display: block;
  padding: 8px 12px 8px 14px;
  margin: 2px 0;
  border-left: 3px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  line-height: 1.4;
  text-decoration: none;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.nav-link:hover {
  background: rgba(225, 225, 241, 0.65);
  text-decoration: none;
}

.nav-link.active {
  background: var(--sidebar-active);
  border-left-color: var(--primary);
  color: var(--text);
  font-weight: 600;
}

.main {
  min-width: 0;
  background: var(--bg);
}

.topbar {
  height: 48px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.topbar-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
}

.topbar-meta {
  color: var(--muted);
  font-size: 13px;
  max-width: 280px;
  text-align: right;
  line-height: 1.4;
}

.account-menu {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  white-space: nowrap;
}

.account-name {
  font-weight: 600;
  color: var(--text);
  padding-right: 4px;
}

.account-link {
  color: var(--muted);
  font-weight: 500;
  text-decoration: none;
}
.account-link:hover { color: var(--primary); text-decoration: none; }
.account-link.active { color: var(--primary); font-weight: 600; }

.account-sep {
  color: var(--line);
  user-select: none;
}

.content { padding: 20px 24px 32px; }

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.cols-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 20px;
}

.card h2,
.card h3 { margin: 0 0 12px; }
.metric {
  font-size: 28px;
  font-weight: 600;
  margin: 6px 0;
  color: var(--text);
  line-height: 1.2;
}
.muted { color: var(--muted); }

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: var(--radius);
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
  background: var(--sidebar-bg);
  color: var(--muted);
  border: 1px solid var(--line);
}

.badge.green { background: var(--green-bg); color: var(--green); border-color: #b8dfc4; }
.badge.orange { background: var(--orange-bg); color: var(--orange); border-color: #f0d4a8; }
.badge.red { background: var(--red-bg); color: var(--red); border-color: #f5c4bc; }
.badge.blue { background: var(--blue-bg); color: var(--primary); border-color: #b8d9f5; }
.badge.purple { background: #f0e9ff; color: var(--purple); border-color: #d8c8f5; }

.btn {
  display: inline-block;
  border: 1px solid var(--line);
  background: #fff;
  padding: 7px 12px;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 14px;
  line-height: 1.25;
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
  text-align: center;
}

.btn:hover {
  background: var(--sidebar-bg);
  text-decoration: none;
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  color: #fff;
}
.btn.danger {
  border-color: var(--red);
  color: var(--red);
  background: #fff;
}
.btn.danger:hover { background: var(--red-bg); }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
}

.table th,
.table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  font-size: 14px;
  vertical-align: middle;
}

.table th {
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  border-bottom: 1px solid var(--line);
}
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafafa; }

.form-row { margin-bottom: 16px; }
label {
  display: block;
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 6px;
  color: var(--text);
}
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  font: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary);
}
textarea { min-height: 128px; resize: vertical; }

textarea.textarea-compact {
  min-height: 0;
  height: auto;
  max-height: 72px;
  font-size: 13px;
  color: var(--muted);
  resize: vertical;
}

.prompt-advanced {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.prompt-advanced summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  user-select: none;
  list-style: none;
}

.prompt-advanced summary::-webkit-details-marker { display: none; }

.prompt-advanced summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s ease;
}

.prompt-advanced[open] summary::before {
  transform: rotate(90deg);
}

.prompt-advanced[open] summary {
  margin-bottom: 12px;
  color: var(--text);
}

.upload-box {
  border: 1px dashed #c4c4c4;
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  background: #fafafa;
}
.upload-box.drag-over {
  border-color: var(--primary);
  background: var(--blue-bg);
}
.file-input-native {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.upload-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 16px;
  flex-wrap: wrap;
}
.upload-actions label.btn {
  cursor: pointer;
  margin: 0;
}
.picked-files {
  list-style: none;
  padding: 0;
  margin: 14px 0 0;
  text-align: left;
  font-size: 13px;
  color: var(--muted);
}
.picked-files li {
  padding: 4px 0;
  border-bottom: 1px dashed var(--line);
}
.submit-final-form {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.progress {
  height: 6px;
  background: var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.progress span { display: block; height: 100%; background: var(--primary); border-radius: var(--radius); }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { border-left: 3px solid var(--line); padding: 0 0 16px 12px; margin-bottom: 4px; }
.timeline li.active { border-left-color: var(--primary); }

.split {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 16px;
}

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

.flash-list { margin-bottom: 16px; }
.flash {
  padding: 10px 14px;
  border-radius: var(--radius);
  margin-bottom: 8px;
  font-size: 14px;
  border: 1px solid transparent;
}
.flash.success {
  background: var(--green-bg);
  color: var(--green);
  border-color: #b8dfc4;
}
.flash.error {
  background: var(--red-bg);
  color: var(--red);
  border-color: #f5c4bc;
}
.flash.info {
  background: var(--blue-bg);
  color: var(--primary);
  border-color: #b8d9f5;
}

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--sidebar-bg);
}

.login-card {
  width: 100%;
  max-width: 400px;
  box-shadow: 0 2px 8px rgba(51, 50, 56, 0.08);
}
.login-card h1 {
  font-size: 21px;
  margin-bottom: 8px;
}
.login-card .btn.primary {
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
}

.guide-content {
  line-height: 1.75;
  font-size: 15px;
  color: var(--text);
  word-break: break-word;
}

.guide-article > h1:first-child {
  display: none;
}

.guide-content h1 {
  font-size: 1.5rem;
  font-weight: 600;
  margin: 0 0 16px;
  color: var(--text);
}

.guide-content h2 {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 28px 0 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
  color: var(--text);
}

.guide-content h3 {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 20px 0 8px;
  color: var(--text);
}

.guide-content h4 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 16px 0 6px;
  color: var(--text);
}

.guide-content p {
  margin: 0 0 12px;
}

.guide-content ul,
.guide-content ol {
  margin: 0 0 16px;
  padding-left: 1.6em;
}

.guide-content li {
  margin: 6px 0;
}

.guide-content li > p {
  margin: 0;
}

.guide-content ul ul,
.guide-content ol ol,
.guide-content ul ol,
.guide-content ol ul {
  margin: 6px 0 0;
}

.guide-content strong {
  font-weight: 600;
  color: var(--text);
}

.guide-content em {
  font-style: italic;
}

.guide-content code {
  background: var(--sidebar-bg);
  padding: 2px 6px;
  border-radius: var(--radius);
  font-family: "GitLab Mono", Consolas, "Courier New", monospace;
  font-size: 0.88em;
  color: var(--text);
}

.guide-content pre {
  background: var(--sidebar-bg);
  border: 1px solid var(--line);
  padding: 14px 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 12px 0 16px;
  line-height: 1.55;
}

.guide-content pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  color: var(--text);
}

.guide-content .highlight {
  background: #f6f8fa;
}

.guide-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: 14px;
}

.guide-content th,
.guide-content td {
  border: 1px solid var(--line);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
}

.guide-content th {
  background: var(--sidebar-bg);
  font-weight: 600;
}

.guide-content blockquote {
  margin: 12px 0;
  padding: 10px 16px;
  border-left: 3px solid var(--primary);
  background: var(--sidebar-bg);
  color: var(--muted);
}

.guide-content hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 24px 0;
}

.guide-content a {
  color: var(--primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.quiz-block {
  background: var(--sidebar-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.quiz-option {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin: 8px 0;
  font-weight: 400;
}
.quiz-option input { width: auto; margin-top: 4px; }

.timer {
  font-size: 14px;
  font-weight: 700;
  color: var(--orange);
}

.open-chapter-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 12px 0 20px;
}
.open-chapter-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fafafa;
}
.open-chapter-item:hover { background: var(--sidebar-bg); }

.chapter-select-row {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.chapter-select-row label {
  margin-bottom: 8px;
  font-size: 14px;
}
.chapter-select-wrap {
  display: flex;
  gap: 8px;
  align-items: center;
}
.chapter-select-wrap select {
  flex: 1;
  max-width: 560px;
}

.class-toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.class-stats {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
}

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(51, 50, 56, 0.4);
  z-index: 1000;
  align-items: flex-start;
  justify-content: center;
  padding: 16px 24px;
  overflow-y: auto;
}
.modal-overlay.open { display: flex; }

.modal-dialog {
  background: var(--panel);
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: 0 4px 24px rgba(51, 50, 56, 0.12);
  width: 100%;
  max-width: 520px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin: auto 0;
}
.modal-dialog-wide { max-width: 960px; }

.quiz-dialog {
  max-height: calc(100vh - 32px);
}

.quiz-form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  max-height: calc(100vh - 32px);
}

.quiz-header {
  flex-shrink: 0;
  align-items: flex-start;
  gap: 16px;
}

.quiz-header-main {
  flex: 1;
  min-width: 0;
}

.quiz-header-main h2 {
  margin: 0 0 6px;
  font-size: 18px;
}

.quiz-header .timer {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--orange);
}

.quiz-submit-top {
  flex-shrink: 0;
  white-space: nowrap;
}

.quiz-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-close {
  border: 0;
  background: none;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  padding: 0 4px;
}
.modal-close:hover { color: var(--text); }

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--line);
}

/* Markdown editor */
.md-editor {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fff;
}
.md-editor-toolbar {
  display: flex;
  gap: 4px;
  padding: 8px 10px;
  background: var(--sidebar-bg);
  border-bottom: 1px solid var(--line);
}
.md-editor-tab {
  border: 1px solid transparent;
  background: none;
  padding: 5px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  color: var(--muted);
}
.md-editor-tab.active {
  background: #fff;
  border-color: var(--line);
  color: var(--primary);
  font-weight: 600;
}
.md-editor-panes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 320px;
}
.md-editor-input {
  border: 0;
  border-radius: 0;
  min-height: 320px;
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
  line-height: 1.55;
  padding: 14px;
  border-right: 1px solid var(--line);
}
.md-editor-preview {
  padding: 14px 16px;
  overflow-y: auto;
  max-height: 480px;
  background: #fafafa;
}

.md-editor.mode-edit .md-editor-panes { grid-template-columns: 1fr; }
.md-editor.mode-edit .md-editor-preview { display: none; }
.md-editor.mode-preview .md-editor-panes { grid-template-columns: 1fr; }
.md-editor.mode-preview .md-editor-input { display: none; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4, .grid.cols-5, .split { grid-template-columns: 1fr; }
  .modal-dialog-wide { max-width: 100%; }
  .md-editor-panes { grid-template-columns: 1fr; }
  .md-editor.mode-split .md-editor-preview { display: none; }
  .quiz-header {
    flex-direction: column;
    align-items: stretch;
  }
  .quiz-submit-top { width: 100%; }
}
