/* 浅色主题 · 三栏布局 */
* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: #f5f6f8;
  color: #2c3e50;
  overflow: hidden;
}

#layout {
  display: flex;
  height: 100vh;
}

/* ===== 通用面板 ===== */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: #ffffff;
  border-bottom: 1px solid #e4e7ed;
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.panel-body {
  flex: 1;
  overflow: auto;
  min-height: 0;
}

.hidden { display: none !important; }

/* ===== 拖拽分隔条 ===== */
.divider {
  width: 5px;
  cursor: col-resize;
  background: #e4e7ed;
  flex-shrink: 0;
  transition: background .15s;
}
.divider:hover, .divider.dragging { background: #1a56db; }
body.col-resizing { cursor: col-resize; user-select: none; }
body.col-resizing iframe, body.col-resizing canvas { pointer-events: none; }

/* ===== 左栏 ===== */
#sidebar {
  width: 240px;
  min-width: 240px;
  display: flex;
  flex-direction: column;
  background: #fafbfc;
  border-right: 1px solid #e4e7ed;
}

#btn-refresh {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 16px;
  color: #606266;
  padding: 2px 6px;
  border-radius: 4px;
}
#btn-refresh:hover { background: #ecf0f3; }

#file-tree { padding: 8px 0; font-size: 13px; }
#file-tree ul { list-style: none; padding-left: 16px; }
#file-tree > ul { padding-left: 0; }
#file-tree li { margin: 1px 0; }

.tree-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  cursor: pointer;
  border-radius: 4px;
  margin: 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-item:hover { background: #ecf0f3; }
.tree-item.active { background: #d6e4ff; color: #1a56db; }
.tree-item .icon { flex-shrink: 0; }

.folder-children.collapsed { display: none; }
.folder-arrow { transition: transform .15s; font-size: 10px; color: #909399; }
.folder-arrow.open { transform: rotate(90deg); }

/* ===== 中栏 ===== */
#viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: #ffffff;
}

#viewer-tools { display: flex; align-items: center; gap: 10px; font-weight: 400; }
#btn-copy, #btn-download {
  border: 1px solid #dcdfe6;
  background: #fff;
  border-radius: 4px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
  color: #606266;
}
#btn-copy:hover, #btn-download:hover { color: #1a56db; border-color: #1a56db; }

#pdf-zoom { display: inline-flex; align-items: center; gap: 4px; }
#pdf-zoom button {
  border: 1px solid #dcdfe6;
  background: #fff;
  border-radius: 4px;
  width: 24px;
  height: 24px;
  cursor: pointer;
  color: #606266;
  line-height: 1;
}
#pdf-zoom button:hover { color: #1a56db; border-color: #1a56db; }
#zoom-label { font-size: 12px; color: #606266; min-width: 40px; text-align: center; }

#welcome { display: flex; align-items: center; justify-content: center; background: #fafbfc; }
.welcome-inner { text-align: center; color: #909399; line-height: 2.2; }
.welcome-inner h2 { color: #606266; margin-bottom: 8px; }

/* PDF 连续滚动 */
#pdf-viewer { background: #e8eaed; padding: 16px 0; }
.pdf-page {
  position: relative;
  margin: 0 auto 14px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.15);
  width: fit-content;
}
.pdf-page canvas { display: block; }
.pdf-page .page-num {
  position: absolute;
  top: 6px;
  right: 8px;
  background: rgba(0,0,0,.45);
  color: #fff;
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 8px;
  pointer-events: none;
}
.pdf-page.placeholder { display: flex; align-items: center; justify-content: center; color: #c0c4cc; font-size: 13px; }

/* 代码编辑区 */
#html-viewer { display: flex; }
#html-frame { flex: 1; border: none; background: #fff; }

#code-viewer { display: flex; flex-direction: column; }
#code-viewer .CodeMirror {
  flex: 1;
  height: auto;
  font-size: 13.5px;
  font-family: "SF Mono", Consolas, "Courier New", monospace;
  background: #fdfdfd;
}

/* ===== 右栏 ===== */
#chat {
  width: 360px;
  min-width: 360px;
  display: flex;
  flex-direction: column;
  background: #fafbfc;
  border-left: 1px solid #e4e7ed;
}

#api-key-bar { padding: 10px 12px; background: #fff; border-bottom: 1px solid #e4e7ed; }
#api-key {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
}
#api-key:focus { border-color: #1a56db; }

#btn-clear {
  border: 1px solid #dcdfe6;
  background: #fff;
  border-radius: 4px;
  padding: 2px 10px;
  font-size: 12px;
  cursor: pointer;
  color: #606266;
}
#btn-clear:hover { color: #c45656; border-color: #c45656; }

#chat-history { padding: 12px; display: flex; flex-direction: column; gap: 10px; }

.msg {
  max-width: 92%;
  min-width: 0;
  flex-shrink: 0;   /* 气泡保持内容自然高度，超出由 chat-history 滚动 */
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13.5px;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: anywhere;   /* 长单词/长 URL 放大字体后也能折行 */
}
.msg.user, .msg.error { white-space: pre-wrap; }
.msg.user {
  align-self: flex-end;
  background: #d6e4ff;
  color: #1a3a8f;
  border-bottom-right-radius: 2px;
}
.msg.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid #e4e7ed;
  border-bottom-left-radius: 2px;
  overflow: hidden;   /* Markdown 内容再宽也不溢出气泡 */
}
.msg.assistant p { margin: 4px 0; }
.msg.assistant code {
  background: #f0f2f5;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12.5px;
  overflow-wrap: anywhere;
}
.msg.assistant pre {
  background: #f0f2f5;
  padding: 8px 10px;
  border-radius: 6px;
  overflow-x: auto;
  max-width: 100%;
  margin: 6px 0;
}
.msg.assistant pre code { background: none; padding: 0; overflow-wrap: normal; }
.msg.assistant img { max-width: 100%; }
.msg.assistant table { display: block; overflow-x: auto; max-width: 100%; border-collapse: collapse; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.4em; }
.msg.error { background: #fef0f0; border-color: #fbc4c4; color: #c45656; }
.msg .cursor { display: inline-block; width: 7px; height: 14px; background: #1a56db; vertical-align: -2px; animation: blink .8s infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* 单次调用的 token 详情（缓存命中等） */
.usage-detail {
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px dashed #e4e7ed;
  font-size: 12px;
  color: #909399;
}
.usage-detail summary { cursor: pointer; user-select: none; color: #b0b4bb; }
.usage-detail summary:hover { color: #1a56db; }
.usage-detail[open] summary { color: #1a56db; }
.usage-body { padding: 4px 0 2px; line-height: 1.8; }
.usage-body .hit { color: #2ba471; font-weight: 600; }
.usage-body .miss { color: #909399; }

#context-bar {
  padding: 6px 12px;
  font-size: 12px;
  color: #606266;
  background: #f0f4ff;
  border-top: 1px solid #e4e7ed;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}

#input-bar {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: #fff;
  border-top: 1px solid #e4e7ed;
  flex-shrink: 0;
}
#question {
  flex: 1;
  resize: none;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}
#question:focus { border-color: #1a56db; }
#btn-send {
  border: none;
  background: #1a56db;
  color: #fff;
  border-radius: 6px;
  padding: 0 16px;
  font-size: 13px;
  cursor: pointer;
}
#btn-send:hover { background: #1648c0; }
#token-bar {
  padding: 5px 12px;
  font-size: 11.5px;
  color: #909399;
  background: #fff;
  border-top: 1px solid #f0f2f5;
  flex-shrink: 0;
}

#btn-send:disabled { background: #a0b7ea; cursor: not-allowed; }
