/* 数学讲解 · 三栏同步界面 */
:root {
  --ink: #2d2a24;
  --ink-soft: #6b645a;
  --line: #e6e1d8;
  --bg: #f7f5f1;
  --card: #ffffff;
  --accent: #c0392b;
  --accent-soft: #fdf0ee;
  --blue: #2c7be5;
  --gold: #b98a2f;
  --paper: #fdfaf1;
}

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

html, body { height: 100%; }
body {
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- 顶部输入栏 ---------- */
.topbar {
  display: flex;
  align-items: stretch;
  gap: 14px;
  padding: 12px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-size: 22px;
  font-weight: 700;
  white-space: nowrap;
  display: flex;
  align-items: center;
  color: var(--ink);
}
.input-wrap { display: flex; gap: 10px; flex: 1; }
#question-input {
  flex: 1;
  resize: none;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  outline: none;
  transition: border-color .2s;
}
#question-input:focus { border-color: var(--blue); }
#question-input.drag {
  border-color: var(--blue);
  background: #f0f6ff;
  box-shadow: 0 0 0 3px rgba(44, 123, 229, .15);
}
.input-actions { display: flex; flex-direction: column; gap: 6px; justify-content: center; }

/* ---------- 图片预览条 ---------- */
.image-preview {
  position: absolute;
  top: calc(100% + 6px);
  right: 18px;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  box-shadow: 0 4px 18px rgba(45, 42, 36, .12);
  max-width: 380px;
}
.image-preview img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #fff;
}
.preview-info { display: flex; align-items: center; gap: 8px; }
#preview-name {
  font-size: 13px;
  color: var(--ink-soft);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn.small { padding: 2px 8px; font-size: 12px; line-height: 1.4; }
.topbar { position: relative; }

/* ---------- 用户区 / 登录弹窗 ---------- */
.user-box { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.user-name { font-size: 13px; color: var(--ink-soft); }
.link { color: var(--blue); font-size: 13px; text-decoration: none; }
.link:hover { text-decoration: underline; }

.auth-modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(45,42,36,.45);
  display: flex; align-items: center; justify-content: center;
}
.auth-card {
  position: relative;
  width: min(400px, 92vw);
  background: var(--card);
  border-radius: 14px;
  padding: 26px 28px 24px;
  box-shadow: 0 10px 40px rgba(0,0,0,.22);
}
.auth-tabs { display: flex; gap: 8px; margin-bottom: 18px; }
.auth-tab {
  flex: 1; padding: 9px 0; border: 1.5px solid var(--line); border-radius: 8px;
  background: #fff; font-size: 15px; cursor: pointer; color: var(--ink-soft);
}
.auth-tab.active { border-color: var(--blue); color: var(--blue); font-weight: 700; background: #f0f6ff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-soft); }
.auth-form input {
  border: 1.5px solid var(--line); border-radius: 8px; padding: 9px 11px;
  font-size: 15px; outline: none; transition: border-color .2s;
}
.auth-form input:focus { border-color: var(--blue); }
.auth-hint { font-size: 12px; color: var(--gold); }
.auth-err { font-size: 13px; color: var(--accent); min-height: 1em; }
.btn.wide { width: 100%; padding: 10px 0; font-size: 15px; }
.auth-close {
  position: absolute; top: 10px; right: 14px; border: none; background: none;
  font-size: 18px; color: var(--ink-soft); cursor: pointer;
}

/* ---------- 截图裁剪层 ---------- */
#crop-overlay {
  position: fixed; inset: 0; z-index: 120;
  background: rgba(20, 22, 28, .82);
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 16px;
}
#crop-overlay .crop-hint {
  color: #e8e6e1; font-size: 14px; background: rgba(255,255,255,.08);
  padding: 6px 14px; border-radius: 8px;
}
#crop-overlay .crop-stage {
  position: relative;
  flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  max-width: 96vw; overflow: hidden;
  cursor: crosshair;
}
#crop-overlay .crop-stage img {
  max-width: 100%; max-height: 100%;
  user-select: none; -webkit-user-drag: none;
  box-shadow: 0 4px 30px rgba(0,0,0,.5);
}
#crop-box {
  position: absolute; display: none;
  border: 2px solid #4da3ff;
  background: rgba(77, 163, 255, .14);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .35);
  pointer-events: none;
}
#crop-overlay .crop-actions { display: flex; gap: 10px; }

.btn {
  border: 1.5px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 10px;
  padding: 0 18px;
  font-size: 15px;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.btn:hover { border-color: #c9c2b5; background: #faf8f4; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn.primary:hover { background: #a93226; }
.btn.small { padding: 4px 12px; font-size: 13px; }

/* ---------- 示例条 ---------- */
.presets-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  color: var(--ink-soft);
  flex-wrap: wrap;
}
.chip {
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 13px;
  cursor: pointer;
  color: var(--ink);
}
.chip:hover { border-color: var(--gold); color: var(--gold); }
.history-wrap { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 13px;
  background: var(--card);
  max-width: 220px;
}
.llm-status { font-size: 12px; }
.llm-status.ok { color: #27ae60; }
.llm-status.err { color: var(--accent); }

/* ---------- 三栏工作区 ---------- */
.workspace {
  flex: 1;
  display: grid;
  /* 列 1/3/5 为面板，列 2/4 为 JS 插入的拖拽分隔条（宽 10px 负 margin 不占位） */
  grid-template-columns: 1.3fr 10px 1.15fr 10px 0.9fr;
  gap: 12px;
  padding: 12px 18px;
  min-height: 0;
}
.hidden { display: none !important; }

.pane {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* ---------- 栏宽拖拽分隔条 ---------- */
.pane-resizer {
  width: 10px;
  margin: 0 -5px;               /* 侵入 gap 中线两侧，不占额外空间 */
  cursor: col-resize;
  position: relative;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  touch-action: none;
}
.pane-resizer-dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #cfc8ba;
  transition: background .15s, transform .15s;
}
.pane-resizer:hover .pane-resizer-dot,
.workspace.resizing .pane-resizer-dot {
  background: var(--blue);
  transform: scale(1.25);
}
.workspace.resizing { cursor: col-resize; }
.workspace.resizing .pane { pointer-events: none; } /* 拖动中防误触 */
.pane-head {
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-soft);
  border-bottom: 1px solid var(--line);
  background: #fbfaf7;
  letter-spacing: 1px;
}

/* 左栏：图形 */
.jxgbox {
  flex: 1;
  min-height: 0;
  margin: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

/* 中栏：板书（纸张质感） */
.board {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px 40px;
  background:
    repeating-linear-gradient(to bottom, transparent 0 34px, rgba(185,138,47,.10) 34px 35px),
    var(--paper);
}
.board-item {
  position: relative;
  margin-bottom: 14px;
  min-height: 35px;
  line-height: 35px;
  font-family: "KaiTi", "STKaiti", "楷体", "Segoe UI", serif;
  font-size: 21px;
  color: var(--ink);
  overflow-x: auto;              /* 长公式横向滑动，不换行不错位 */
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}
.board-item.board-title {
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  border-bottom: 2px solid rgba(44,42,36,.25);
  margin-bottom: 20px;
  letter-spacing: 2px;
}
.board-layer { display: inline-block; white-space: nowrap; }   /* 底稿与揭示同规则：永不换行 */
.board-base { opacity: .28; }
.board-reveal {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: bottom;
  font-weight: 600;
}
.board-item.board-title .board-reveal { left: 50%; transform: translateX(-50%); }

/* 右栏：讲解 + 控制 */
.steps-view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 10px;
}
.step {
  display: flex;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
  margin-bottom: 5px;
  transition: background .2s;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--ink-soft);
}
.step .step-idx {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #eee9df;
  color: var(--ink-soft);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
}
.step.current {
  background: var(--accent-soft);
  color: var(--ink);
}
.step.current .step-idx { background: var(--accent); color: #fff; }
.step.done { color: #9a938a; }
.step .tok { border-radius: 3px; }
.step .tok.active { background: #ffd66b; color: #5c4700; }
.step .tok.past { color: #b8b1a6; }

.controls {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
  background: #fbfaf7;
  flex-wrap: wrap;
}
/* 右栏收窄：控制条按钮紧凑化 */
.controls .btn { padding: 0 10px; font-size: 13px; }
.controls .select { padding: 3px 4px; font-size: 12.5px; max-width: 110px; }
.step-progress {
  flex: 1;
  display: flex;
  gap: 4px;
  min-width: 120px;
}
.seg {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: #e8e3d9;
  cursor: pointer;
  transition: background .2s;
}
.seg.done { background: var(--gold); }
.seg.current { background: var(--accent); }

/* ---------- 占位与状态 ---------- */
.placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
  font-size: 16px;
  gap: 6px;
}
.placeholder-icon { font-size: 56px; margin-bottom: 10px; }

.status-bar {
  min-height: 28px;
  padding: 4px 18px;
  font-size: 13px;
  color: var(--ink-soft);
  background: var(--card);
  border-top: 1px solid var(--line);
}
.status-bar.error { color: var(--accent); }
.status-bar .spinner {
  display: inline-block;
  width: 12px; height: 12px;
  border: 2px solid #d8d2c6;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 滚动条 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #d8d2c6; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

/* ================= 移动端适配（≤820px：三栏 → 上中下三层） ================= */
@media (max-width: 820px) {
  body { overflow: auto; }               /* 允许整页滚动 */
  .topbar { flex-wrap: wrap; padding: 10px 12px; gap: 8px; }
  .brand { font-size: 19px; }
  .input-wrap { order: 3; width: 100%; flex-direction: column; }
  .input-actions { flex-direction: row; justify-content: flex-end; }
  .input-actions .btn { flex: 1; padding: 9px 6px; font-size: 14px; }
  .user-box { margin-left: auto; }

  .presets-bar { padding: 6px 12px; font-size: 13px; gap: 6px; }
  .history-wrap { margin-left: 0; }
  .select { max-width: 150px; }

  /* 工作区：纵向三层（图形 / 板书 / 讲解），等分高度 */
  .workspace {
    display: flex !important;             /* 覆盖桌面端 JS 写入的内联 grid 模板 */
    flex-direction: column;
    grid-template-columns: none !important;
    gap: 10px;
    padding: 10px 12px 80px;             /* 底部留控制条空间 */
    min-height: 0;
    flex: 1;
  }
  .pane { min-height: 30vh; }            /* 每层至少 30% 视口高，可滚动浏览 */

  /* 禁用桌面端拖拽分隔条（三层布局无横向栏宽概念） */
  .pane-resizer { display: none !important; }

  /* 图形区适配 */
  .jxgbox { margin: 6px; }

  /* 板书移动端专项：字号收紧、间距压缩、题目条目仍居中 */
  .board { padding: 12px 14px 30px; }
  .board-item {
    font-size: 17px;
    line-height: 30px;
    min-height: 30px;
    margin-bottom: 10px;
  }
  .board-item.board-title { font-size: 19px; letter-spacing: 1px; }

  /* 控制条：吸附视口底部，双行防溢出 */
  .pane-narration .controls {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    background: #fbfaf7;
    box-shadow: 0 -3px 14px rgba(45,42,36,.12);
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
  }
  .controls .btn { padding: 8px 10px; font-size: 14px; }
  .step-progress { min-width: 100%; order: 9; }

  .status-bar { padding: 4px 12px; }

  /* 截图裁剪层按钮加大触控面积 */
  #crop-overlay .crop-actions .btn { padding: 10px 18px; font-size: 15px; }
  #crop-overlay .crop-hint { font-size: 13px; text-align: center; }

  /* 登录弹窗全屏化 */
  .auth-card { width: 92vw; padding: 20px 18px; }
  .auth-form input { font-size: 16px; }  /* ≥16px 防 iOS 聚焦自动放大 */
}
