:root {
  --bg: #0b0d11;
  --panel: #12151c;
  --panel-2: #171b24;
  --line: #232936;
  --text: #e7eaf0;
  --muted: #8b93a5;
  --accent: #7c6cff;
  --accent-2: #5b8cff;
  --danger: #ff5f6d;
  --ok: #35d6a4;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- 侧边栏 ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 12px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 8px; }
.brand-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 18px rgba(124, 108, 255, .7);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text small { color: var(--muted); font-size: 11px; }

.side-label {
  display: flex; justify-content: space-between;
  font-size: 12px; color: var(--muted);
  padding: 6px 6px 0;
}

.session-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; min-height: 0; }

.session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: 10px;
  border: 1px solid transparent;
  color: #c9cfdd; cursor: pointer;
}
.session-item:hover { background: var(--panel-2); }
.session-item.active { background: rgba(124, 108, 255, .12); border-color: rgba(124, 108, 255, .35); color: #fff; }
.session-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .ops { display: flex; gap: 2px; opacity: 0; }
.session-item:hover .ops { opacity: 1; }
.mini-btn {
  border: 0; background: transparent; color: var(--muted);
  padding: 2px 4px; border-radius: 6px; font-size: 12px;
}
.mini-btn:hover { background: #222838; color: #fff; }

.sidebar-footer { display: flex; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; }
.ghost-btn {
  flex: 1; background: transparent; border: 1px solid var(--line);
  color: #c3c9d8; padding: 8px; border-radius: 10px; font-size: 13px;
}
.ghost-btn:hover { background: var(--panel-2); }

/* ---------- 主区域 ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 18px; border-bottom: 1px solid var(--line);
  background: rgba(13, 15, 20, .8); backdrop-filter: blur(8px);
}
.topbar-title {
  flex: 1; font-weight: 600; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; cursor: text;
}
.model-select {
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 9px; padding: 7px 10px; max-width: 240px;
}
.model-select:focus { outline: none; border-color: var(--accent); }

.messages { flex: 1; overflow-y: auto; padding: 22px 18px 8px; scroll-behavior: smooth; }
.msg { max-width: 860px; margin: 0 auto 18px; display: flex; gap: 12px; }
.msg .avatar {
  width: 28px; height: 28px; border-radius: 8px; flex: none;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: var(--panel-2); border: 1px solid var(--line);
}
.msg.user .avatar { background: rgba(124, 108, 255, .18); border-color: rgba(124, 108, 255, .4); }
.msg .bubble { min-width: 0; flex: 1; }
.msg .role { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg .content { white-space: normal; word-break: break-word; }
.msg.error .content { color: var(--danger); }
.msg-usage { margin-top: 6px; font-size: 11px; color: var(--muted); }

.content pre {
  background: #0d1016; border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 14px; overflow-x: auto; position: relative; margin: 10px 0;
}
.content pre code { background: none; padding: 0; border: 0; font-size: 13px; }
.content code {
  background: #1a1f2b; border: 1px solid var(--line); border-radius: 6px;
  padding: 1px 5px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px;
}
.content h1, .content h2, .content h3 { margin: 14px 0 8px; line-height: 1.3; }
.content h1 { font-size: 20px; } .content h2 { font-size: 17px; } .content h3 { font-size: 15px; }
.content ul, .content ol { padding-left: 22px; margin: 8px 0; }
.content a { color: var(--accent-2); }
.content blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.content table { border-collapse: collapse; margin: 10px 0; width: 100%; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }

.msg-tools { margin-top: 8px; display: flex; gap: 8px; }
.msg-tools button {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  border-radius: 7px; padding: 3px 8px; font-size: 12px;
}
.msg-tools button:hover { color: #fff; border-color: #39415a; }

.empty { max-width: 720px; margin: 8vh auto; text-align: center; color: var(--muted); }
.empty h1 { color: var(--text); font-size: 22px; margin-bottom: 6px; }
.empty .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; margin-top: 22px; text-align: left; }
.empty .card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; }
.empty .card strong { display: block; color: var(--text); margin-bottom: 4px; font-size: 13px; }

/* ---------- 输入区 ---------- */
.composer { padding: 10px 18px 16px; }
.composer-box {
  max-width: 860px; margin: 0 auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 10px 12px;
}
.composer-box:focus-within { border-color: #3b4266; }
#input {
  width: 100%; resize: none; border: 0; background: transparent;
  max-height: 200px; min-height: 26px; outline: none; line-height: 1.6;
}
.composer-actions { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.composer-actions .hint { flex: 1; font-size: 11px; color: var(--muted); }

.btn {
  border: 1px solid var(--line); background: var(--panel-2);
  border-radius: 9px; padding: 8px 14px; color: var(--text);
}
.btn:hover { border-color: #3a4258; }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border: 0; color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { border-color: rgba(255, 95, 109, .4); color: var(--danger); background: rgba(255, 95, 109, .08); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn {
  border: 1px solid var(--line); background: transparent; color: #c3c9d8;
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--panel-2); }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(4, 6, 10, .66); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 18px;
}
.modal-panel {
  width: min(560px, 100%); max-height: 88dvh; overflow: hidden;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; display: flex; flex-direction: column;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .5);
}
.modal-panel.wide { width: min(920px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-body.split { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; overflow: hidden; }

.list-pane { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.model-list { display: flex; flex-direction: column; gap: 6px; max-height: 56dvh; overflow-y: auto; }
.model-item { border: 1px solid var(--line); border-radius: 10px; padding: 10px; cursor: pointer; background: var(--panel-2); }
.model-item:hover { border-color: #39415a; }
.model-item.active { border-color: var(--accent); background: rgba(124, 108, 255, .1); }
.model-item .name { font-weight: 600; font-size: 13px; }
.model-item .sub { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.form-pane { display: flex; flex-direction: column; gap: 12px; min-width: 0; max-height: 56dvh; overflow-y: auto; padding-right: 4px; }
.form-pane label, .modal-body > label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.form-pane input, .form-pane textarea, .modal-body input, .modal-body textarea {
  background: #0e1117; border: 1px solid var(--line); border-radius: 9px;
  padding: 9px 11px; color: var(--text); outline: none;
}
.form-pane input:focus, .modal-body input:focus, .modal-body textarea:focus { border-color: var(--accent); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { flex-direction: row !important; align-items: center; gap: 8px !important; }
.check input { width: 16px; height: 16px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; position: sticky; bottom: 0; background: var(--panel); padding: 8px 0 2px; }
.test-output {
  background: #0c0f14; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; font-size: 12px; white-space: pre-wrap; margin: 0; max-height: 180px; overflow: auto;
}
.test-output .ok { color: var(--ok); }
.test-output .fail { color: var(--danger); }

/* ---------- Toast ---------- */
.toast-wrap { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #1b2130; border: 1px solid var(--line); border-left: 3px solid var(--accent);
  padding: 10px 14px; border-radius: 9px; font-size: 13px; max-width: 360px;
  animation: slidein .18s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.only-mobile { display: none; }
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 60;
    transform: translateX(-102%); transition: transform .2s ease;
  }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .6); }
  .only-mobile { display: grid; }
  .modal-body.split { grid-template-columns: 1fr; }
  .model-list { max-height: 220px; }
  .model-select { max-width: 130px; }
}
