/* ═══════════════════════════════════════════════════
   数学 AI 教学工作台 · 样式
   配色：深绿主色（沉稳、教育场景）+ 中性灰
   ═══════════════════════════════════════════════════ */

:root {
  --brand: #064E3B;
  --brand-light: #047857;
  --brand-pale: #ECFDF5;
  --ink: #1F2937;
  --ink-2: #4B5563;
  --ink-3: #9CA3AF;
  --line: #E5E7EB;
  --bg: #F7F8FA;
  --panel: #FFFFFF;
  --warn: #B45309;
  --err: #DC2626;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 12px rgba(0,0,0,.05);
}

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

body {
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ── 顶栏 ── */
.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
  height: 60px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 26px; }
.brand h1 { font-size: 16px; font-weight: 700; letter-spacing: -.2px; }
.brand .sub { font-size: 12px; color: var(--ink-3); margin-top: 1px; }

.tabs { display: flex; gap: 4px; margin-left: auto; }
.tab {
  padding: 8px 16px;
  border: none;
  background: transparent;
  color: var(--ink-2);
  font-size: 14px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.tab:hover { background: var(--bg); color: var(--ink); }
.tab.active { background: var(--brand); color: #fff; font-weight: 600; }

.status {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--ink-3);
  margin-left: 8px;
  flex-shrink: 0;
}
.status.ok { background: #10B981; box-shadow: 0 0 0 3px #D1FAE5; }
.status.bad { background: var(--err); box-shadow: 0 0 0 3px #FEE2E2; }

/* ── 布局 ── */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 640px;
  min-height: 0;
  overflow: hidden;
}
.layout.collapsed { grid-template-columns: 1fr 0; }

.panel-left { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.view { display: none; flex: 1; flex-direction: column; min-height: 0; }
.view.active { display: flex; }

/* ── 快捷指令 ── */
.quickbar {
  display: flex; gap: 8px; padding: 12px 20px; flex-wrap: wrap;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.chip {
  padding: 6px 13px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 999px; font-size: 13px; color: var(--ink-2); cursor: pointer;
  font-family: inherit; transition: all .15s;
}
.chip:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-pale); }

.settings {
  display: flex; gap: 18px; padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.settings label { font-size: 13px; color: var(--ink-2); display: flex; align-items: center; gap: 6px; }
.settings select {
  padding: 5px 9px; border: 1px solid var(--line); border-radius: 6px;
  font-size: 13px; font-family: inherit; background: var(--panel); color: var(--ink);
}

/* ── 对话区 ── */
.chatlog {
  flex: 1; overflow-y: auto; padding: 24px 20px;
  display: flex; flex-direction: column; gap: 16px;
}
.welcome { max-width: 560px; margin: 40px auto; color: var(--ink-2); }
.welcome h2 { font-size: 20px; color: var(--ink); margin-bottom: 12px; }
.welcome ul { margin: 12px 0 12px 20px; font-size: 14px; line-height: 2; }
.welcome .tip {
  margin-top: 16px; padding: 10px 14px; background: var(--brand-pale);
  border-radius: 8px; font-size: 13px; color: var(--brand);
}

.msg { display: flex; gap: 12px; max-width: 100%; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
  background: var(--bg);
}
.msg.user .avatar { background: #DBEAFE; }
.msg.assistant .avatar { background: var(--brand-pale); }
.msg .body {
  flex: 1; min-width: 0; padding: 12px 16px; border-radius: 12px;
  background: var(--panel); box-shadow: var(--shadow); font-size: 14px; line-height: 1.75;
}
.msg.user .body { background: #EFF6FF; }
.msg .body.error { color: var(--err); background: #FEF2F2; }

/* Markdown 渲染 */
.md h1 { font-size: 19px; margin: 18px 0 10px; }
.md h2 { font-size: 17px; margin: 16px 0 8px; padding-bottom: 5px; border-bottom: 1px solid var(--line); }
.md h3 { font-size: 15px; margin: 14px 0 6px; }
.md p { margin: 8px 0; }
.md ol, .md ul { margin: 8px 0 8px 22px; }
.md li { margin: 3px 0; }
.md code {
  background: var(--bg); padding: 2px 6px; border-radius: 4px;
  font-family: "Consolas", monospace; font-size: 13px;
}
.md pre {
  background: var(--bg); padding: 12px; border-radius: 8px;
  overflow-x: auto; margin: 10px 0; font-size: 13px;
}
.md blockquote {
  border-left: 3px solid var(--line); padding-left: 12px;
  color: var(--ink-3); margin: 10px 0;
}
.md table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 13px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md th { background: var(--bg); font-weight: 600; }
.md hr { border: none; border-top: 1px solid var(--line); margin: 16px 0; }
.md strong { font-weight: 700; }

/* 试卷模式：紧凑排版 */
.md.exam h1 { text-align: center; font-size: 20px; margin-bottom: 4px; }
.md.exam h2 {
  text-align: center; font-size: 15px; font-weight: 500;
  border: none; margin: 6px 0 14px; color: var(--ink-2);
}

/* 选项横排 */
.opts { display: flex; flex-wrap: wrap; gap: 4px 20px; margin: 4px 0 10px 22px; }
.opts span { white-space: nowrap; }
.opts-vert { display: block; }
.opts-vert > div { margin: 2px 0; }

.thinking { display: flex; align-items: center; gap: 10px; color: var(--ink-3); font-size: 14px; }
.thinking .dots { display: flex; gap: 3px; }
.thinking .dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--brand);
  animation: bounce 1.2s infinite;
}
.thinking .dots i:nth-child(2) { animation-delay: .15s; }
.thinking .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

.doc-actions {
  margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line);
  display: flex; gap: 8px; flex-wrap: wrap;
}

/* ── 输入区 ── */
.composer {
  padding: 12px 20px 16px; background: var(--panel);
  border-top: 1px solid var(--line); flex-shrink: 0;
}
.composer textarea {
  width: 100%; padding: 11px 14px; border: 1px solid var(--line);
  border-radius: var(--radius); font-size: 14px; font-family: inherit;
  resize: vertical; min-height: 64px; max-height: 160px; line-height: 1.6;
  background: var(--panel); color: var(--ink);
}
.composer textarea:focus { outline: none; border-color: var(--brand); }
.composer-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 9px; }

/* ── 按钮 ── */
.btn {
  padding: 9px 18px; border: none; border-radius: 8px; font-size: 14px;
  font-family: inherit; cursor: pointer; transition: all .15s; font-weight: 500;
}
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-light); }
.btn.primary:disabled { background: var(--ink-3); cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--ink-2); border: 1px solid var(--line); }
.btn.ghost:hover { background: var(--bg); color: var(--ink); }
.btn.sm { padding: 6px 13px; font-size: 13px; }
.icon-btn {
  padding: 5px 9px; border: none; background: transparent; cursor: pointer;
  border-radius: 6px; color: var(--ink-3); font-size: 15px;
}
.icon-btn:hover { background: var(--bg); color: var(--ink); }

/* ── 右侧 A4 面板 ── */
.panel-right {
  background: #E8EAED; border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-width: 0; overflow: hidden;
}
.layout.collapsed .panel-right { display: none; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.ph-title { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.panel-foot {
  padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--line);
  display: flex; gap: 8px; flex-shrink: 0;
}

.a4-page-wrap { flex: 1; overflow-y: auto; padding: 20px; }

/* A4 纸张：尺寸与打印一致 */
.a4-page {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  padding: 20mm;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0,0,0,.12);
  font-family: "SimSun", "宋体", serif;
  font-size: 10.5pt;
  line-height: 1.75;
  color: #1a1a1a;
  transform-origin: top center;
}
.a4-preview .a4-page { transform: scale(.82); margin-bottom: -60px; }
.empty-hint { color: var(--ink-3); text-align: center; padding-top: 120px; font-size: 13px; font-family: system-ui, sans-serif; }

/* A4 内的排版规则（与 Word 导出一致） */
.a4-page h1 { text-align: center; font-size: 16pt; margin: 0 0 6px; font-family: "SimHei", "黑体", sans-serif; }
.a4-page h2 { text-align: center; font-size: 12pt; font-weight: 400; color: var(--ink-2); margin: 0 0 12px; }
.a4-page h3 { font-size: 12pt; margin: 14px 0 8px; padding-bottom: 3px; border-bottom: 1px solid #eee; font-family: "SimHei", sans-serif; }
.a4-page p { margin: 5px 0; }
.a4-page ol, .a4-page ul { margin: 5px 0 5px 20px; }
.a4-page ol { list-style: decimal; }
.a4-page ul { list-style: disc; }
.a4-page li { margin: 2px 0; }
.a4-page hr { border: none; border-top: 1px solid #ddd; margin: 14px 0; }
.a4-page table { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 9.5pt; }
.a4-page th, .a4-page td { border: 1px solid #ddd; padding: 4px 7px; text-align: left; }
.a4-page th { background: #f8f8f8; }
.a4-page blockquote {
  border-left: 3px solid #999; padding-left: 10px; color: #666; margin: 8px 0;
}
.a4-page pre {
  background: #f5f5f5; padding: 8px; border-radius: 4px; font-size: 9pt;
  overflow-x: auto; font-family: Consolas, monospace;
}
.a4-page strong { font-weight: 700; }
.a4-page .ans { font-family: "KaiTi", "楷体", serif; }
.a4-page .namefield {
  margin: 8px 0 14px; font-size: 10.5pt;
}
.a4-page .namefield span { margin-right: 22px; }
.a4-page .opts-row { display: flex; flex-wrap: wrap; gap: 0 18px; margin: 3px 0 3px 20px; }
.a4-page .opts-row > span { white-space: nowrap; }
.a4-page .opts-vert > div { margin: 2px 0 2px 20px; }
.a4-page .tikz-block { text-align: center; margin: 10px 0; }
.a4-page .tikz-block svg { max-width: 100%; height: auto; }

/* ── 文档编辑视图 ── */
.doc-tools {
  display: flex; gap: 10px; padding: 12px 20px; flex-wrap: wrap; align-items: center;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.doc-tools input[type="text"] {
  padding: 7px 11px; border: 1px solid var(--line); border-radius: 7px;
  font-size: 14px; font-family: inherit; background: var(--panel); color: var(--ink);
}
.doc-tools input[type="text"] { flex: 1; min-width: 160px; }
.doc-tools input:focus { outline: none; border-color: var(--brand); }
.doc-opts { display: flex; gap: 12px; }
.doc-opts label { font-size: 13px; color: var(--ink-2); display: flex; align-items: center; gap: 4px; cursor: pointer; }

.doc-editor { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); min-height: 0; }
.pane { display: flex; flex-direction: column; background: var(--panel); min-width: 0; min-height: 0; }
.pane-head {
  padding: 8px 14px; font-size: 12px; font-weight: 600; color: var(--ink-2);
  border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.pane textarea {
  flex: 1; padding: 14px; border: none; font-family: "Consolas", "Monaco", monospace;
  font-size: 13px; line-height: 1.75; resize: none; background: var(--panel); color: var(--ink);
}
.pane textarea:focus { outline: none; }
.a4-preview { flex: 1; overflow: auto; padding: 16px; background: #E8EAED; }

/* ── 绘图视图 ── */
.draw-tools {
  display: flex; gap: 10px; padding: 12px 20px; align-items: center;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.draw-tools input[type="text"] {
  flex: 1; padding: 8px 12px; border: 1px solid var(--line); border-radius: 7px;
  font-size: 14px; font-family: inherit; background: var(--panel); color: var(--ink);
}
.draw-tools input:focus { outline: none; border-color: var(--brand); }
.draw-area { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); min-height: 0; }
.tikz-input { display: flex; flex-direction: column; background: var(--panel); min-width: 0; min-height: 0; }
.tikz-input textarea {
  flex: 1; padding: 14px; border: none; font-family: Consolas, monospace;
  font-size: 13px; line-height: 1.7; resize: none; background: var(--panel); color: var(--ink);
}
.tikz-input textarea:focus { outline: none; }
.tikz-preview { display: flex; flex-direction: column; background: var(--panel); min-width: 0; min-height: 0; }
.svg-host { flex: 1; overflow: auto; padding: 20px; display: flex; align-items: center; justify-content: center; }
.svg-host svg { max-width: 100%; height: auto; }

/* ── 遮罩 & 提示 ── */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: none; align-items: center; justify-content: center; gap: 14px;
  flex-direction: column; color: #fff; font-size: 14px; z-index: 100;
}
.overlay.show { display: flex; }
.spinner {
  width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.25);
  border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--ink); color: #fff; padding: 11px 20px; border-radius: 8px;
  font-size: 14px; opacity: 0; transition: all .3s; z-index: 200; pointer-events: none;
  max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--err); }

/* ── 响应式 ── */
@media (max-width: 1200px) {
  .layout { grid-template-columns: 1fr 480px; }
  .a4-page { transform: scale(.68); transform-origin: top center; }
  .a4-preview .a4-page { transform: scale(.6); margin-bottom: -110px; }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .panel-right { display: none; }
  .doc-editor, .draw-area { grid-template-columns: 1fr; }
}

/* ── 打印：只输出 A4 内容 ── */
@media print {
  body { overflow: visible; height: auto; background: #fff; }
  .topbar, .panel-left, .panel-foot, .panel-head, .overlay, .toast { display: none !important; }
  .layout { display: block; }
  .panel-right { display: block !important; border: none; background: #fff; }
  .a4-page-wrap { overflow: visible; padding: 0; }
  .a4-page {
    width: auto; min-height: auto; box-shadow: none; padding: 0;
    transform: none !important; margin: 0 !important;
  }
  body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  @page { size: A4; margin: 20mm; }
}

/* 隐藏 KaTeX 的 MathML 无障碍层，避免打印时公式重影 */
.katex .katex-mathml {
  position: absolute; clip: rect(1px,1px,1px,1px);
  padding: 0; border: 0; height: 1px; width: 1px; overflow: hidden;
}

/* ══════════════════════════════════════════════
   matplotlib 配图引擎
   ══════════════════════════════════════════════ */
.engine-tabs {
  display: flex; gap: 6px; padding: 0 16px 10px;
  border-bottom: 1px solid #E5E7EB;
}
.engine-tab {
  padding: 7px 15px; font-size: 13px; border-radius: 7px 7px 0 0;
  border: 1px solid transparent; border-bottom: none;
  background: transparent; color: #6B7280; cursor: pointer;
  font-family: inherit; transition: all .15s;
}
.engine-tab:hover { color: #064E3B; background: #F3F6F4; }
.engine-tab.active {
  background: #fff; border-color: #E5E7EB; color: #064E3B; font-weight: 600;
  box-shadow: 0 -1px 0 #fff inset;
}

.mpl-side { display: flex; flex-direction: column; min-width: 0; }
.mpl-presets {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px;
  border-bottom: 1px solid #F0F0F0;
}
.preset-chip {
  padding: 5px 11px; font-size: 12.5px; border-radius: 999px;
  border: 1px solid #D6DBD8; background: #fff; color: #4B5563;
  cursor: pointer; font-family: inherit; transition: all .15s;
}
.preset-chip:hover { border-color: #064E3B; color: #064E3B; }
.preset-chip.active {
  background: #064E3B; border-color: #064E3B; color: #fff; font-weight: 500;
}

#mplOpts {
  flex: 1; min-height: 220px; border: none; outline: none; resize: none;
  padding: 12px 14px; font-family: 'SF Mono', Consolas, 'Courier New', monospace;
  font-size: 12.5px; line-height: 1.65; color: #1F2937; background: #FCFDFC;
  tab-size: 2;
}
.ph-text { color: #9CA3AF; font-size: 13px; padding: 24px 12px; display: block; text-align: center; }
.ph-text.err { color: #C0392B; }

.figure-block svg { max-width: 100%; height: auto; }
.figure-ok { background: #fff; }
