:root {
  --bg: #fff;
  --fg: #111;
  --muted: #6b6b6b;
  --line: #e9e9e9;

  --user: #111;
  --user-fg: #fff;

  --asst: #f3f3f3;
  --asst-fg: #111;

  --danger: #b00020;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--fg);
}

.btn,
.input,
.select,
.textarea,
textarea {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--fg);
  outline: none;
}

.btn {
  height: 34px;
  padding: 6px 10px;
  cursor: pointer;
  border-color: #111;
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.btn.secondary {
  border-color: var(--line);
}

.btn.danger {
  border-color: #ffd6db;
  color: var(--danger);
}

.btn:disabled {
  opacity: .6;
  cursor: not-allowed;
}

.input {
  height: 34px;
  padding: 6px 10px;
  width: 100%;
}

.select {
  height: 34px;
  padding: 6px 10px;
  width: 100%;
}

.textarea {
  width: 100%;
  min-height: 130px;
  max-height: 280px;
  resize: vertical;
  padding: 10px 12px;
  font: inherit;
  line-height: 1.4;
}

label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 0 0 6px;
}

/* Layout */
.app {
  height: 100%;
  display: flex;
}

/* Sidebar (desktop default) */
.sidebar {
  width: 300px;
  border-right: 1px solid var(--line);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #fff;
}

.side-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.side-title {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: .2px;
}

.session-list {
  overflow: auto;
  padding-right: 4px;
  flex: 1;
}

.session-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 10px;
  cursor: pointer;
  background: #fff;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.session-item:hover {
  border-color: #dcdcdc;
}

.session-item.active {
  border-color: #111;
}

.session-title {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-preview {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.backdrop {
  display: none;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* Topbar: left / center status / right */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.top-left-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.iconbtn {
  display: none;
  height: 34px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  white-space: nowrap;
}

.top-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.chat-title {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-sub {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-status {
  justify-self: center;
  color: var(--muted);
  font-size: 12px;
  max-width: 40vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  justify-self: end;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.chat {
  flex: 1;
  overflow: auto;
  padding: 14px 12px 6px;
  background: #fff;
}

.wrap {
  max-width: 980px;
  margin: 0 auto;
}

.msg {
  display: flex;
  margin: 10px 0;
}

.msg.user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(78ch, 88%);
  padding: 10px 12px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid transparent;
}

/* markdown 渲染后的元素别强制 pre-wrap（否则表格/代码布局会怪），在 md 根节点里恢复正常排版 */
.bubble .md {
  white-space: normal;
}

.user .bubble {
  background: var(--user);
  color: var(--user-fg);
  border-top-right-radius: 6px;
}

.assistant .bubble {
  background: var(--asst);
  color: var(--asst-fg);
  border: 1px solid var(--line);
  border-top-left-radius: 6px;
}

.composer {
  border-top: 1px solid var(--line);
  padding: 10px 12px;
  background: #fff;
}

.composer-wrap {
  display: flex;
  gap: 10px;
  align-items: stretch;
  flex-wrap: wrap;
  /* 新增：允许换行 */
}

#composerInput {
  flex: 1;
  height: 42px;
  min-height: 42px;
  max-height: 180px;
  resize: vertical;
  padding: 10px 12px;
  font: inherit;
  line-height: 1.4;
}

.sendcol {
  display: flex;
  align-items: stretch;
  min-width: 86px;
}

#btnSend {
  height: 100%;
  width: 100%;
  justify-content: center;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;

  z-index: 200;
  /* 新增：确保在复制按钮等之上 */
}


.modal.open {
  display: flex;
}

.panel {
  width: min(920px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
  overflow: hidden;
  position: relative;
  /* 可选 */
  z-index: 201;
  /* 可选 */
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.panel-head .h {
  font-weight: 600;
  font-size: 14px;
}

.panel-actions {
  display: flex;
  gap: 8px;
}

.panel-body {
  padding: 12px;
  max-height: 72vh;
  overflow: auto;
}



.section {
  padding: 10px 0 14px;
  border-bottom: 1px solid var(--line);
}

.section:last-child {
  border-bottom: none;
}

.section-title {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 10px;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
}

.row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 10px;
}

/* ===== Markdown / 富文本 样式（完整版） ===== */
.bubble .md> :first-child {
  margin-top: 0;
}

.bubble .md> :last-child {
  margin-bottom: 0;
}

.bubble .md p {
  margin: 8px 0;
}

.bubble .md a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bubble .md img {
  max-width: 100%;
  height: auto;
  display: block;
  border-radius: 10px;
  margin: 8px 0;
}

.bubble .md hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 12px 0;
}

.bubble .md blockquote {
  margin: 10px 0;
  padding: 8px 10px;
  border-left: 3px solid #cfcfcf;
  background: rgba(0, 0, 0, .03);
  border-radius: 10px;
}

.bubble .md h1,
.bubble .md h2,
.bubble .md h3 {
  margin: 12px 0 8px;
  line-height: 1.25;
}

.bubble .md ul,
.bubble .md ol {
  margin: 8px 0;
  padding-left: 22px;
}

.bubble .md table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  overflow: auto;
  display: block;
}

.bubble .md th,
.bubble .md td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}

.bubble .md thead th {
  background: rgba(0, 0, 0, .03);
}

.bubble .md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px;
}

/* inline code */
.bubble .md :not(pre)>code {
  padding: 1px 6px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, .08);
  background: rgba(0, 0, 0, .04);
}

.bubble .md pre {
  position: relative;
  margin: 10px 0;
  padding: 12px 12px;
  border-radius: 12px;
  overflow: auto;
  border: 1px solid rgba(255, 255, 255, .2);
  background: #f6f8fa;
}

.user .bubble .md pre {
  background: rgba(0, 0, 0, .25);
  border-color: rgba(255, 255, 255, .3);
}

.user .bubble .md :not(pre)>code {
  background: rgba(255, 255, 255, .15);
  border-color: rgba(255, 255, 255, .2);
  color: #fff;
}

.bubble .md pre code {
  background: transparent;
  padding: 0;
  border: 0;
  white-space: pre;
}


.code-copy-btn:active {
  transform: translateY(1px);
}

.code-block-wrap {
  position: relative;
  display: block;
  width: 100%;
  margin: 10px 0;
}

.code-block-wrap pre {
  margin: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding-top: 36px;
}

.code-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 20;
  padding: 4px 10px;
  font-size: 12px;
  line-height: 1;
  color: #fff;
  background: rgba(0, 0, 0, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  cursor: pointer;
}

.code-copy-btn:hover {
  background: rgba(0, 0, 0, 0.8);
}

/* ===== Code block collapse ===== */
.code-toggle-btn {
  position: absolute;
  top: 8px;
  right: 56px;
  z-index: 21;
  padding: 4px 10px;
  font-size: 12px;
  line-height: 1;
  color: #fff;
  background: rgba(0, 0, 0, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  cursor: pointer;
}

.code-toggle-btn:hover {
  background: rgba(0, 0, 0, 0.8);
}

.code-block-wrap.collapsed pre {
  max-height: 220px;
  overflow: hidden;
}

.code-block-wrap.collapsed::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 54px;
  background: linear-gradient(to bottom, rgba(246, 248, 250, 0), rgba(246, 248, 250, 1));
  pointer-events: none;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}


/* Mermaid 容器 */
.mermaid {
  margin: 10px 0;
  padding: 10px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, .08);
  background: #fff;
  overflow: auto;
}

pre {
  overflow-x: auto;
  overflow-y: hidden;
}

/* KaTeX 在气泡里适配换行 */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 6px 0;
}

/* ===== Responsive ===== */
@media (max-width: 860px) {
  .grid {
    grid-template-columns: 1fr;
  }

  .iconbtn {
    display: inline-flex;
    align-items: center;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 320px);
    z-index: 50;
    transform: translateX(-105%);
    transition: transform .2s ease;
    border-right: 1px solid var(--line);
    box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .35);
    z-index: 40;
    display: none;
  }

  .backdrop.open {
    display: block;
  }

  .top-status {
    max-width: 46vw;
  }
}

/* Toast 气泡提示 */
.toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  max-width: min(92vw, 420px);
  padding: 10px 14px;
  border-radius: 12px;
  background: rgba(17, 17, 17, 0.92);
  color: #fff;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 1000;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.success {
  background: rgba(22, 163, 74, 0.95);
}

.toast.error {
  background: rgba(176, 0, 32, 0.95);
}

.toast.info {
  background: rgba(17, 17, 17, 0.92);
}

.msg-col {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* user 气泡右对齐、assistant 左对齐 */
.msg.user .msg-col {
  align-items: flex-end;
}

.msg.assistant .msg-col {
  align-items: flex-start;
}

.msg-actions {
  display: flex;
  gap: 6px;
}

.msg-action-btn {
  height: 26px;
  padding: 0 10px;
  font-size: 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  cursor: pointer;
}

.msg-action-btn:disabled {
  opacity: .6;
  cursor: not-allowed;
}

/* 放在黑色 user 气泡下更协调一点 */
.msg.user .msg-action-btn {
  background: rgba(255, 255, 255, .08);
  border-color: rgba(255, 255, 255, .22);
  color: rgba(255, 255, 255, .92);
}

/* Attach button */
#btnAttach {
  white-space: nowrap;
}

/* 手机上空间紧张时：让“选择文件”变小一点 */
@media (max-width: 860px) {
  #btnAttach {
    padding: 6px 8px;
    height: 34px;
  }
}

.attachcol {
  display: flex;
  align-items: stretch;
}

#btnAttach {
  height: 100%;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

/* Drag & Drop overlay */
.drop-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .35);
  z-index: 1200;
  padding: 18px;
}

.drop-overlay.open {
  display: flex;
}

.drop-box {
  width: min(520px, 92vw);
  background: #fff;
  border: 2px dashed #111;
  border-radius: 16px;
  padding: 18px 16px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
}

.drop-title {
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 6px;
}

.drop-sub {
  color: var(--muted);
  font-size: 13px;
}

.float-nav {
  position: fixed;
  right: 16px;
  bottom: calc(var(--float-safe-bottom, 16px) + env(safe-area-inset-bottom, 0px));
  z-index: 1600;
  display: flex;
  flex-direction: column;
  gap: 10px;

  /* 拖动相关 */
  touch-action: none;
  /* 移动端允许拖动，不触发页面滚动手势 */
  user-select: none;
}

@media (max-width: 860px) {
  .float-nav {
    right: 12px;
  }
}

.float-nav.dragging {
  cursor: grabbing;
}

.float-nav.dragging .float-btn {
  pointer-events: none;
  /* 拖动时避免误点上下按钮 */
}

/* 手机端把悬浮按钮整体上移，避免遮挡底部发送区 */
@media (max-width: 860px) {
  .float-nav {
    right: 12px;
    bottom: calc(92px + env(safe-area-inset-bottom, 0px));
  }
}

.float-nav.hidden {
  display: none;
}

.float-btn {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  color: var(--fg);
  cursor: pointer;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.12);
  backdrop-filter: blur(6px);
}

.float-btn:hover {
  border-color: #dcdcdc;
}

.float-btn.hidden {
  display: none;
}

.attach-preview {
  order: 1;
  flex: 0 0 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 0 0 8px;

  /* 新增：限制高度，超出滚动 */
  max-height: 140px;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 2px 2px;
  /* 可选：让滚动时不贴边 */
}

/* 手机上再矮一点 */
@media (max-width: 860px) {
  .attach-preview {
    max-height: 96px;
  }
}

.attach-preview.hidden {
  display: none;
}

.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  max-width: 100%;
}

.attach-chip-name {
  font-size: 12px;
  color: var(--fg);
  max-width: 52vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attach-chip-meta {
  font-size: 12px;
  color: var(--muted);
}

.attach-chip-btn {
  height: 22px;
  padding: 0 8px;
  font-size: 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  color: var(--muted);
}

/* 流式阶段：气泡里只显示一行实时文本，并横向滚动到最新 */
.bubble.streaming .stream-live {
  display: block;
  max-width: 100%;
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  line-height: 1.4;
  /* 可选：让它更像“实时输出条” */
  opacity: 0.95;
}

/* 可选：隐藏滚动条（仍可滚动） */
.bubble.streaming .stream-live::-webkit-scrollbar {
  display: none;
}

.bubble.streaming .stream-live {
  scrollbar-width: none;
  /* Firefox */
}

.code-preview-btn {
  position: absolute;
  top: 8px;
  right: 104px;
  /* 预览在最左，展开中间，复制最右 */
  z-index: 21;
  padding: 4px 10px;
  font-size: 12px;
  line-height: 1;
  color: #fff;
  background: rgba(0, 0, 0, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  cursor: pointer;
}

.code-preview-btn:hover {
  background: rgba(0, 0, 0, 0.8);
}

.code-preview {
  margin-top: 8px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  resize: vertical;
  /* 可拖拽 */
  min-height: 260px;
  max-height: 80vh;
}

.code-preview-frame {
  width: 100%;
  height: 60vh;
  /* 默认更高 */
  min-height: 260px;
  border: 0;
  background: #fff;
}

.code-preview.hidden {
  display: none;
}

.preview-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  padding: 18px;
}

.preview-modal.open {
  display: flex;
}

.preview-panel {
  width: min(1200px, 96vw);
  height: min(90vh, 900px);
  background: #fff;
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  box-shadow: 0 12px 30px rgba(0, 0, 0, .25);
}

.preview-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.preview-body {
  flex: 1;
}

.preview-frame {
  width: 100%;
  height: 100%;
  border: 0;
}

.preview-actions {
  display: flex;
  gap: 8px;
}

.preview-panel.fullscreen {
  width: 100vw;
  height: 100vh;
  border-radius: 0;
}


.model-picker {
  position: relative;
}

/* 让模型下拉不被 panel-body 的 overflow 裁剪 */
.model-dropdown {
  position: fixed;
  /* 关键：脱离 panel-body 裁剪 */
  left: 0;
  top: 0;
  width: 200px;

  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  max-height: 45vh;
  overflow: auto;
  z-index: 99999;
  box-shadow: 0 10px 24px rgba(0, 0, 0, .08);
}


.model-dropdown.hidden {
  display: none;
}

.model-opt {
  padding: 8px 10px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-opt:hover {
  background: #f3f3f3;
}

.model-opt.muted {
  color: var(--muted);
  cursor: default;
}

/* [MODEL_CLEAR_X_STYLE] */
.model-picker {
  position: relative;
}

.clear-x {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);

  width: 42px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);

  display: inline-flex;
  align-items: center;
  justify-content: center;

  cursor: pointer;
  line-height: 1;
  padding: 0;
}

.clear-x:hover {
  color: #111;
  border-color: #cfcfcf;
}

.chat {
  contain: layout style paint;
  will-change: scroll-position;
}

.msg {
  contain: layout style paint;
}

.bubble {
  contain: content;
}