:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-secondary: #4b5563;
  --text-dim: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --ok: #22c55e;
  --ok-bg: #f0fdf4;
  --warn: #f59e0b;
  --warn-bg: #fffbeb;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.04);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.06), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -2px rgba(0, 0, 0, 0.02);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --topbar-h: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
.screen { min-height: 100vh; }
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }

/* ---------- 按钮 ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:hover:not(:disabled) { background: #f9fafb; border-color: #9ca3af; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--primary-light); color: var(--primary); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-block { width: 100%; }

/* ---------- 登录 ---------- */
.auth-screen { display: flex; align-items: center; justify-content: center; padding: 20px; background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); }
.auth-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 32px; width: 100%; max-width: 380px; }
.auth-title { margin: 0 0 4px; font-size: 24px; font-weight: 700; color: var(--primary); }
.auth-sub { margin: 0 0 24px; color: var(--text-dim); font-size: 13px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--text-secondary); font-weight: 500; }
.field input, .field textarea, .field select {
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); padding: 9px 12px;
  font-size: 14px; font-family: inherit; background: var(--surface); color: var(--text);
}
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.field textarea { resize: vertical; min-height: 80px; }
.auth-actions { display: flex; gap: 10px; margin-top: 4px; }
.auth-actions .btn { flex: 1; }
.auth-error { color: var(--danger); font-size: 13px; margin: 0; }

/* ---------- 顶栏 ---------- */
.app-screen { display: flex; flex-direction: column; }
.topbar {
  height: var(--topbar-h); flex-shrink: 0; background: var(--surface);
  border-bottom: 1px solid var(--border); display: flex; align-items: center;
  padding: 0 16px; gap: 16px; position: sticky; top: 0; z-index: 100; box-shadow: var(--shadow-sm);
}
.brand { font-weight: 700; font-size: 16px; color: var(--primary); white-space: nowrap; }
.topbar-nav { display: flex; gap: 4px; flex: 1; }
.topbar-nav a {
  padding: 7px 14px; border-radius: var(--radius-sm); color: var(--text-secondary);
  font-size: 14px; font-weight: 500; transition: all 0.15s;
}
.topbar-nav a:hover { background: var(--primary-light); color: var(--primary); }
.topbar-nav a.active { background: var(--primary); color: #fff; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-name { color: var(--text-dim); font-size: 13px; }

/* ---------- 视图 ---------- */
.view { flex: 1; padding: 20px; max-width: 960px; margin: 0 auto; width: 100%; }
.page-title { margin: 0 0 16px; font-size: 20px; font-weight: 700; }
.section-title { margin: 24px 0 12px; font-size: 15px; font-weight: 600; color: var(--text-secondary); }

/* ---------- 卡片 ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow-sm); }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.entry-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 24px; }
.entry-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; cursor: pointer; transition: all 0.15s; box-shadow: var(--shadow-sm); text-align: left; }
.entry-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }
.entry-card .ic-icon { font-size: 28px; margin-bottom: 8px; }
.entry-card .ic-title { font-weight: 600; font-size: 15px; margin-bottom: 4px; }
.entry-card .ic-desc { font-size: 13px; color: var(--text-dim); }

/* ---------- 录题 ---------- */
.capture-zone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 28px; text-align: center; color: var(--text-dim); background: var(--surface); cursor: pointer; transition: all 0.15s; }
.capture-zone:hover, .capture-zone.dragover { border-color: var(--primary); background: var(--primary-light); color: var(--primary); }
.capture-zone .cz-icon { font-size: 36px; margin-bottom: 8px; }
.capture-zone .cz-hint { font-size: 13px; }
.image-strip { display: flex; flex-wrap: wrap; gap: 10px; margin: 14px 0; }
.image-thumb {
  position: relative; width: 120px; height: 120px; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: #f9fafb;
}
.image-thumb img { width: 100%; height: 100%; object-fit: cover; }
.image-thumb .img-actions { position: absolute; bottom: 0; left: 0; right: 0; display: flex; background: rgba(0,0,0,0.55); color: #fff; font-size: 11px; }
.image-thumb .img-actions button { flex: 1; background: transparent; border: 0; color: #fff; padding: 4px; cursor: pointer; }
.image-thumb .img-actions button:hover { background: rgba(255,255,255,0.2); }
.image-thumb .img-order { position: absolute; top: 4px; left: 4px; background: rgba(0,0,0,0.65); color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 7px; }

/* ---------- 题目列表 ---------- */
.q-list { display: flex; flex-direction: column; gap: 10px; }
.q-item { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: flex; gap: 12px; cursor: pointer; transition: all 0.15s; box-shadow: var(--shadow-sm); }
.q-item:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.q-thumb { width: 64px; height: 64px; border-radius: var(--radius-sm); overflow: hidden; background: var(--bg); flex-shrink: 0; }
.q-thumb img { width: 100%; height: 100%; object-fit: cover; }
.q-thumb.empty { display: flex; align-items: center; justify-content: center; color: var(--text-dim); font-size: 11px; }
.q-body { flex: 1; min-width: 0; }
.q-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.q-meta { display: flex; flex-wrap: wrap; gap: 6px; font-size: 12px; color: var(--text-dim); align-items: center; }
.q-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

/* ---------- 徽章 ---------- */
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 500; background: var(--primary-light); color: var(--primary); }
.badge-subject { background: #eef2ff; color: #4338ca; }
.badge-math { background: #ecfdf5; color: #047857; }
.badge-logic { background: #fef3c7; color: #b45309; }
.badge-writing { background: #fce7f3; color: #be185d; }
.badge-mastery-unlearned { background: #f3f4f6; color: #6b7280; }
.badge-mastery-learning { background: var(--warn-bg); color: var(--warn); }
.badge-mastery-mastered { background: var(--ok-bg); color: var(--ok); }
.badge-tag { background: #f3f4f6; color: var(--text-secondary); }
.badge-error { background: var(--danger-bg); color: var(--danger); }

/* ---------- 题目详情 ---------- */
.qd-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.qd-images { display: flex; flex-direction: column; gap: 10px; }
.qd-image { width: 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: auto; background: #f9fafb; max-height: 480px; }
.qd-image img { width: 100%; display: block; }
.qd-stem { font-size: 14px; line-height: 1.7; }
.qd-stem .q-stem-markdown { background: #f9fafb; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; white-space: pre-wrap; word-break: break-word; }
.qd-options { list-style: none; padding: 0; margin: 8px 0; display: flex; flex-direction: column; gap: 6px; }
.qd-options li { padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); }
.qd-options li.correct { border-color: var(--ok); background: var(--ok-bg); }
.qd-options li.wrong { border-color: var(--danger); background: var(--danger-bg); }

/* ---------- AI 老师对话 ---------- */
.tutor-box { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.tutor-msg { padding: 12px 14px; border-radius: var(--radius); max-width: 100%; font-size: 14px; line-height: 1.7; word-break: break-word; }
.tutor-msg.user { background: var(--primary-light); border: 1px solid #c7d2fe; }
.tutor-msg.ai { background: var(--surface); border: 1px solid var(--border); }
.tutor-msg .msg-role { font-size: 12px; color: var(--text-dim); margin-bottom: 4px; font-weight: 600; }
.tutor-msg.ai .md-body { }
.tutor-msg.ai .md-body p:first-child { margin-top: 0; }
.tutor-msg.ai .md-body p:last-child { margin-bottom: 0; }
.tutor-msg.ai .md-body pre { background: #1e293b; color: #e2e8f0; padding: 10px; border-radius: var(--radius-sm); overflow-x: auto; font-size: 13px; }
.tutor-msg.ai .md-body code { font-family: var(--mono); }
.tutor-msg.ai .md-body :not(pre) > code { background: #f1f5f9; padding: 1px 4px; border-radius: 4px; font-size: 13px; }
.tutor-input { display: flex; gap: 8px; margin-top: 10px; }
.tutor-input textarea { flex: 1; resize: vertical; min-height: 60px; }

/* ---------- 重做 ---------- */
.redo-cover { background: var(--warn-bg); border: 1px solid var(--warn); border-radius: var(--radius); padding: 14px; text-align: center; color: #92400e; margin-bottom: 12px; }
.redo-result { padding: 12px; border-radius: var(--radius-sm); font-weight: 600; }
.redo-result.correct { background: var(--ok-bg); color: var(--ok); }
.redo-result.wrong { background: var(--danger-bg); color: var(--danger); }

/* ---------- 筛选栏 ---------- */
.filter-bar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; align-items: center; }
.filter-bar select, .filter-bar input { padding: 6px 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); font-size: 13px; background: var(--surface); }
.filter-bar input[type=search] { flex: 1; min-width: 140px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border-radius: 12px; border: 1px solid var(--border-strong); background: var(--surface); font-size: 12px; cursor: pointer; color: var(--text-secondary); }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- 统计 ---------- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; text-align: center; box-shadow: var(--shadow-sm); }
.stat-card .stat-num { font-size: 26px; font-weight: 700; color: var(--primary); }
.stat-card .stat-label { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

/* ---------- 空态 ---------- */
.empty { text-align: center; padding: 40px 20px; color: var(--text-dim); }
.empty .em-icon { font-size: 40px; margin-bottom: 8px; }
.empty .em-text { font-size: 14px; }

/* ---------- 弹窗 ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.45); display: flex; align-items: center; justify-content: center; z-index: 200; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg); max-width: 520px; width: 100%; max-height: 88vh; overflow: auto; }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: 18px; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-end; }

/* ---------- 加载/提示 ---------- */
.loading { text-align: center; padding: 30px; color: var(--text-dim); }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1e293b; color: #fff; padding: 10px 18px; border-radius: var(--radius); box-shadow: var(--shadow-lg); z-index: 300; font-size: 14px; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.7s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 响应式 ---------- */
@media (max-width: 640px) {
  .topbar { padding: 0 10px; gap: 8px; }
  .brand { font-size: 14px; }
  .topbar-nav { overflow-x: auto; }
  .topbar-nav a { padding: 6px 10px; font-size: 13px; white-space: nowrap; }
  .user-name { display: none; }
  .view { padding: 12px; }
  .qd-layout { grid-template-columns: 1fr; }
  .q-item { padding: 12px; }
  .q-thumb { width: 52px; height: 52px; }
}
