:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --line: #e3e8ef;
  --text: #1d2433;
  --sub: #667085;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --ok: #059669;
  --warn: #b45309;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 8px 24px rgba(16,24,40,.06);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 -apple-system, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  -webkit-text-size-adjust: 100%;
}

header.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.logo { font-weight: 700; font-size: 16px; letter-spacing: .3px; }
.logo span { color: var(--primary); }
nav.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
nav.tabs button {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 8px;
  color: var(--sub); cursor: pointer; font-size: 14px; font-weight: 500;
}
nav.tabs button.active { background: #eef2ff; color: var(--primary-dark); font-weight: 600; }
.sysinfo { color: var(--sub); font-size: 12px; }

main { padding: 16px; max-width: 1560px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px;
}
.panel + .panel { margin-top: 12px; }
.panel h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.panel h3 small { color: var(--sub); font-weight: 400; font-size: 12px; }

.btn {
  border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 500;
  transition: .15s;
}
.btn:hover { border-color: #c7d2e3; background: #fafbfe; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #fbd5d5; }
.btn.tiny { padding: 4px 9px; font-size: 12px; border-radius: 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

input[type=text], input[type=password], textarea, select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  font: inherit; color: var(--text); background: #fff; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: #a9c1f5; box-shadow: 0 0 0 3px rgba(37,99,235,.08); }
textarea { resize: vertical; line-height: 1.65; }
label.fld { display: block; font-size: 12px; color: var(--sub); margin-bottom: 4px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- 录题工作台 ---------- */
.capture-layout { display: grid; grid-template-columns: minmax(380px, 1fr) minmax(420px, 1.15fr); gap: 12px; }
@media (max-width: 1080px) { .capture-layout { grid-template-columns: 1fr; } }

.dropzone {
  border: 2px dashed #c9d6ea; border-radius: var(--radius); background: #fbfcff;
  padding: 26px 14px; text-align: center; color: var(--sub); cursor: pointer;
}
.dropzone.drag { border-color: var(--primary); background: #eff5ff; color: var(--primary); }
.dropzone b { color: var(--text); display: block; font-size: 15px; margin-bottom: 4px; }

.img-wrap { position: relative; margin-top: 10px; border-radius: 8px; overflow: hidden; background: #eceff5; }
.img-wrap img { display: block; width: 100%; user-select: none; }
.img-wrap .box-layer { position: absolute; inset: 0; }
.crop-rect { position: absolute; border: 2px solid var(--primary); background: rgba(37,99,235,.12); display: none; pointer-events: none; }
.crop-rect.pick { border: 2px dashed #f59e0b; background: rgba(245,158,11,.14); }
.ocr-box { position: absolute; border: 1px solid rgba(37,99,235,.45); background: rgba(37,99,235,.06); pointer-events: none; }
.ocr-box.hl { background: rgba(37,99,235,.28); border-color: var(--primary); }

.crop-hint { color: var(--sub); font-size: 12px; margin-top: 6px; }

.ocr-para {
  margin-top: 6px; min-height: 110px; line-height: 1.75; font-size: 15px;
  background: #fbfcff; border-color: #d8e3f5;
}
#btnToggleLines.on { border-color: #a9c1f5; background: #eff5ff; color: var(--primary-dark); }

.ocr-list { max-height: 260px; overflow: auto; margin-top: 8px; border: 1px solid var(--line); border-radius: 8px; }
.ocr-line { padding: 7px 10px; border-bottom: 1px dashed var(--line); cursor: pointer; font-size: 13px; }
.ocr-line:hover { background: #f5f8ff; }
.ocr-line:last-child { border-bottom: 0; }

.toolbar { display: flex; gap: 4px; flex-wrap: wrap; padding: 6px; background: #f7f9fc; border: 1px solid var(--line); border-radius: 8px 8px 0 0; border-bottom: 0; }
.toolbar button { border: 0; background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 4px 9px; cursor: pointer; font-size: 12px; }
.toolbar button:hover { border-color: #b9c9e6; }
.editor-area { border-radius: 0 0 8px 8px; min-height: 160px; font-size: 15px; }

.preview {
  margin-top: 10px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; min-height: 56px; font-size: 15px;
}
.preview .ph { color: #aab2c0; }
.preview img.qimg { max-width: 100%; display: block; margin: 8px auto; }

/* 公式预览样式 */
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; font-size: .82em; margin: 0 2px; transform: translateY(-.12em); }
.frac .num { border-bottom: 1px solid currentColor; padding: 0 3px 1px; }
.frac .den { padding: 1px 3px 0; }
.sqrt { display: inline-flex; align-items: flex-start; vertical-align: middle; }
.sqrt .rad { font-size: 1.05em; line-height: 1; transform: translateY(.12em); }
.sqrt .radicand { border-top: 1px solid currentColor; padding: 1px 2px 0; }

.assets-strip { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }.asset-card { border: 1px solid var(--line); border-radius: 8px; padding: 6px; background: #fff; text-align: center; }
.asset-card img { max-width: 130px; max-height: 90px; display: block; margin-bottom: 5px; }
.asset-card .cap { font-size: 11px; color: var(--sub); }

/* ---------- 题库 ---------- */
.bank-list { display: grid; gap: 8px; margin-top: 12px; }
.q-card { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: #fff; }
.q-card.sel { border-color: var(--primary); background: #f7faff; }
.q-head { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--sub); }
.q-head .id { font-weight: 700; color: var(--primary-dark); }
.q-body { margin-top: 6px; font-size: 14px; max-height: 120px; overflow: hidden; position: relative; }
.q-body img.qimg { max-height: 100px; vertical-align: middle; }
/* 带图形的题：文字在左、图形在右，图形等比例缩小做预览 */
.q-body.split { display: flex; align-items: flex-start; gap: 10px; }
.q-body.split .q-text { flex: 1 1 auto; min-width: 0; overflow: hidden; }
.q-figs { flex: 0 0 auto; display: flex; flex-wrap: wrap; gap: 6px; max-width: 40%; justify-content: flex-end; align-items: flex-start; }
.q-figs img {
  height: 88px; width: auto; max-width: 100%; object-fit: contain;
  border: 1px solid var(--line); border-radius: 4px; background: #fff;
}
.q-actions { margin-top: 8px; display: flex; gap: 6px; }

.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: #f1f4f9; color: var(--sub); font-size: 11px; margin-right: 5px; }

/* ---------- 导出 ---------- */
.export-picked { display: grid; gap: 6px; max-height: 320px; overflow: auto; }
.picked-item { display: flex; gap: 8px; align-items: baseline; font-size: 13px; padding: 6px 8px; border-bottom: 1px dashed var(--line); }
.picked-item .n { color: var(--primary-dark); font-weight: 700; min-width: 20px; }

/* ---------- 提示 ---------- */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 13px; opacity: 0; pointer-events: none; transition: .25s; z-index: 100;
  max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; bottom: 40px; }
.toast.err { background: #b91c1c; }
.toast.ok { background: #047857; }

.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid #d7deea; border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 切题 ---------- */
.split-strip { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 8px; }
.split-card {
  width: 152px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 8px;
  padding: 6px; background: #fff; cursor: pointer; transition: .15s;
}
.split-card:hover { border-color: #b9c9e6; box-shadow: var(--shadow); }
.split-card.on { border-color: var(--primary); background: #f5f9ff; }
.split-card img { width: 100%; height: 104px; object-fit: contain; background: #f7f9fc; display: block; margin-bottom: 5px; }
.split-card .cap { font-size: 12px; font-weight: 600; color: var(--text); }
.split-card .pv { font-size: 11px; color: var(--sub); max-height: 30px; overflow: hidden; line-height: 1.4; }

.split-box {
  position: absolute; border: 2px dashed rgba(37,99,235,.5);
  background: rgba(37,99,235,.04); pointer-events: none;
}
.split-box:hover { background: rgba(37,99,235,.2); border-color: var(--primary); }
.split-box .no {
  position: absolute; top: -2px; left: -2px; background: var(--primary); color: #fff;
  font-size: 11px; line-height: 16px; padding: 0 5px; border-radius: 4px 0 4px 0;
  pointer-events: auto; cursor: pointer;
}

/* 手动分界线 */
.split-cut { position: absolute; height: 0; border-top: 2px dashed #f59e0b; pointer-events: none; }
.split-cut .tag {
  position: absolute; right: 0; top: -9px; background: #f59e0b; color: #fff;
  font-size: 10px; line-height: 15px; padding: 0 5px; border-radius: 3px; white-space: nowrap;
}

/* 自动切题的原始接缝：灰点线，双击即可合并 */
.split-seam { position: absolute; height: 0; border-top: 1px dotted rgba(100,116,139,.85); pointer-events: none; }

@media (max-width: 1080px) {
  .capture-layout { grid-template-columns: 1fr; }
}

/* ---------- 手机适配 ---------- */
@media (max-width: 760px) {
  /* 题库里带图的题：手机上图形再小一点，文字仍占主位 */
  .q-body.split { gap: 8px; }
  .q-figs { max-width: 44%; gap: 4px; }
  .q-figs img { height: 68px; }

  main { padding: 10px 10px calc(12px + env(safe-area-inset-bottom)); }

  header.topbar { padding: 8px 10px; gap: 8px; }
  .logo { font-size: 15px; white-space: nowrap; }
  nav.tabs {
    flex: 1; flex-wrap: nowrap; overflow-x: auto; gap: 2px;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs button { padding: 9px 12px; font-size: 14px; white-space: nowrap; }
  .sysinfo { display: none; }

  .panel { padding: 12px; border-radius: 12px; }
  .panel h3 { font-size: 15px; display: block; }
  .panel h3 small { display: block; margin-top: 3px; font-size: 12px; line-height: 1.45; }

  .btn { min-height: 42px; padding: 10px 14px; font-size: 14px; }
  .btn.tiny { min-height: 32px; padding: 5px 10px; font-size: 12px; }
  input[type=text], input[type=password], textarea, select { font-size: 16px; padding: 10px; }

  .grid2 { grid-template-columns: 1fr; }
  .grid3, .grid4 { grid-template-columns: 1fr 1fr; }

  .dropzone { padding: 30px 14px; }
  .dropzone b { font-size: 16px; }

  .img-wrap img, .img-wrap .box-layer { touch-action: none; }

  .toolbar { gap: 6px; }
  .toolbar button { min-height: 36px; padding: 6px 11px; font-size: 13px; }
  .editor-area { font-size: 16px; min-height: 130px; }
  .ocr-list { max-height: 38vh; }
  .ocr-line { padding: 10px 12px; font-size: 14px; }
  .preview { font-size: 16px; }

  .split-card { width: 46vw; }
  .split-card img { height: 122px; }
  .asset-card img { max-width: 44vw; max-height: 130px; }

  .q-card { padding: 12px; }
  .q-head { flex-wrap: wrap; row-gap: 6px; }
  .q-head input[type=checkbox] { width: 22px; height: 22px; }
  .q-body { max-height: 46vh; }
  .picked-item { font-size: 14px; }

  .toast { bottom: 72px; font-size: 14px; padding: 12px 20px; }

  /* 保存操作固定在屏幕底部，手机上一只手就够得着 */
  .sticky-actions {
    position: sticky; bottom: 0; z-index: 15;
    margin-top: 12px; border-radius: 12px;
    box-shadow: 0 -8px 18px rgba(16,24,40,.10);
  }
  .sticky-actions .row { gap: 10px; }
  .sticky-actions .row .btn { flex: 1 1 auto; justify-content: center; }
}

@media (max-width: 420px) {
  .grid3, .grid4 { grid-template-columns: 1fr; }
}


/* 空白/过窄的分块：提醒拖动分界线 */
.split-card.warn { border-color: #f04438; background: #fff6f5; }
.split-card.warn .cap { color: #d92d20; }

/* ---------- AI 改题弹窗 ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(16,24,40,.45); z-index: 90;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-box {
  background: #fff; border-radius: 12px; width: min(760px, 100%);
  max-height: 92vh; overflow: auto; padding: 14px 16px 16px;
  box-shadow: 0 18px 50px rgba(16,24,40,.22);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.modal-head strong { font-size: 15px; }
.modal-body label.fld { display: block; font-size: 12px; color: #667085; margin-bottom: 4px; }
.var-src {
  font-size: 12px; line-height: 1.5; color: #475467; background: #f8fafc;
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
  max-height: 120px; overflow: auto;
}
#varContent, #varAnswer, #varAnalysis { width: 100%; font-size: 14px; font-family: inherit; }
@media (max-width: 760px) {
  .modal-box { padding: 12px; max-height: 94vh; }
  .modal-mask { align-items: flex-end; padding: 0; }
  .modal-box { border-radius: 14px 14px 0 0; width: 100%; max-height: 88vh; }
}
