:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --line: #e5e7eb;
  --line-strong: #d7dbe0;
  --text: #14161a;
  --muted: #6b7280;
  --muted-2: #9aa1ab;
  --accent: #2f6bff;
  --accent-weak: #eef3ff;
  --green: #18a058;
  --amber: #d99a00;
  --red: #d84a4a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }

button, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
textarea { resize: none; }

/* ---------- shared ---------- */
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px; display: inline-block;
  background: linear-gradient(140deg, #3b74ff, #1f4fd8);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.18);
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 7px; border-radius: 4px;
  border: 1.6px solid rgba(255,255,255,.9); border-bottom-color: rgba(255,255,255,.25);
}
.brand-mark.small { width: 22px; height: 22px; border-radius: 7px; }
.brand-mark.large { width: 44px; height: 44px; border-radius: 13px; }
.brand-name { font-weight: 600; font-size: 15px; letter-spacing: .2px; }

.btn {
  border: 1px solid var(--line-strong);
  background: #fff;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  line-height: 1.2;
}
.btn:hover { background: #f7f8fa; }
.btn:active { background: #f0f2f5; }
.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #fff;
}
.btn-primary:hover { background: #255ce6; }
.btn-primary:disabled { opacity: .55; cursor: not-allowed; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { background: #f0f2f5; color: var(--text); }
.btn-block { width: 100%; padding: 11px 16px; }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 7px; }

.icon-btn {
  border: 1px solid transparent; background: transparent; color: var(--muted);
  width: 34px; height: 34px; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: #f0f2f5; color: var(--text); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12.5px; color: var(--muted); }
.field input, .field textarea {
  border: 1px solid var(--line-strong); background: #fff; border-radius: 8px;
  padding: 10px 12px; outline: none; transition: border-color .15s, box-shadow .15s;
  width: 100%;
}
.field input:focus, .field textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak);
}
.error-text { color: var(--red); font-size: 13px; min-height: 18px; margin: 0; }
.error-text.small { font-size: 12px; padding: 0 16px 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted-2); display: inline-block; flex: none; }
.dot.online { background: var(--green); box-shadow: 0 0 0 3px rgba(24,160,88,.12); }
.dot.connecting { background: var(--amber); }
.dot.error { background: var(--red); }

.pill {
  font-size: 12px; color: var(--muted); background: var(--panel-2);
  border: 1px solid var(--line); padding: 3px 9px; border-radius: 999px;
}
.pill.online { color: var(--green); border-color: #cdebd9; background: #f2fbf6; }
.pill.error { color: var(--red); border-color: #f3d3d3; background: #fdf5f5; }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1200px 500px at 50% -10%, #eaf0ff 0%, rgba(234,240,255,0) 60%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 14px;
  padding: 30px 28px 26px; box-shadow: var(--shadow);
}
.login-desc { color: var(--muted); margin: 10px 0 22px; font-size: 13.5px; }
.login-card form { display: flex; flex-direction: column; gap: 6px; }
.login-card .btn { margin-top: 6px; }

/* ---------- app shell ---------- */
.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  height: 58px; flex: none; background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; gap: 12px; position: relative; z-index: 20;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; }
.topbar-title { font-weight: 600; letter-spacing: .2px; }
.topbar-center { flex: 1; display: flex; justify-content: center; min-width: 0; }

.instance-switcher { position: relative; max-width: 100%; }
.instance-btn {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line-strong); background: #fff;
  border-radius: 999px; padding: 7px 13px; cursor: pointer;
  max-width: 320px; color: var(--text);
}
.instance-btn:hover { background: #fafbfc; }
.instance-btn span:nth-child(2) {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px;
}
.instance-menu {
  position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  min-width: 260px; max-width: 340px; background: #fff; border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); padding: 6px; z-index: 40;
}
.instance-item {
  display: flex; align-items: center; gap: 9px; padding: 9px 10px;
  border-radius: 7px; cursor: pointer;
}
.instance-item:hover { background: #f5f7fa; }
.instance-item.active { background: var(--accent-weak); }
.instance-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.instance-item .meta { font-size: 12px; color: var(--muted-2); }
.instance-menu .menu-foot {
  border-top: 1px solid var(--line); margin-top: 6px; padding: 8px 10px 4px;
}
.instance-menu .menu-foot button {
  border: 0; background: none; color: var(--accent); cursor: pointer; padding: 4px 0;
  font-size: 13px;
}

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 328px; flex: none; background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { padding: 12px; border-bottom: 1px solid var(--line); }
.search {
  width: 100%; border: 1px solid var(--line-strong); background: var(--panel-2);
  border-radius: 8px; padding: 9px 12px; outline: none;
}
.search:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-weak); }

.session-list { flex: 1; overflow-y: auto; padding: 6px; }
.session-item {
  display: flex; gap: 11px; padding: 11px 12px; border-radius: 9px; cursor: pointer;
  border: 1px solid transparent;
}
.session-item:hover { background: #f7f8fa; }
.session-item.active { background: var(--accent-weak); border-color: #dbe5ff; }
.avatar {
  width: 38px; height: 38px; border-radius: 10px; flex: none;
  background: #eef1f5; color: #4b5563; display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 14px;
}
.session-body { flex: 1; min-width: 0; }
.session-row1 { display: flex; align-items: baseline; gap: 8px; }
.session-name {
  font-weight: 500; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.session-time { font-size: 11.5px; color: var(--muted-2); flex: none; }
.session-preview {
  color: var(--muted); font-size: 12.8px; margin-top: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.unread-badge {
  background: var(--accent); color: #fff; font-size: 11px;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 6px;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.empty-hint { padding: 40px 24px; text-align: center; color: var(--muted); }
.empty-title { font-weight: 500; color: var(--text); margin: 0 0 6px; }
.empty-sub { font-size: 12.8px; margin: 0; line-height: 1.6; }

/* ---------- chat ---------- */
.chat-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.chat-empty {
  flex: 1; display: flex; align-items: center; justify-content: center; padding: 30px;
}
.chat-empty-inner { text-align: center; max-width: 420px; color: var(--muted); }
.chat-empty-inner h2 { color: var(--text); font-size: 17px; font-weight: 600; margin: 16px 0 8px; }
.chat-empty-inner p { line-height: 1.7; font-size: 13.5px; margin: 0; }
.chat-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.chat-head {
  height: 62px; flex: none; background: var(--panel); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; padding: 0 18px;
}
.chat-title { font-weight: 600; }
.chat-sub { font-size: 12px; color: var(--muted-2); margin-top: 3px; }

.message-list {
  flex: 1; overflow-y: auto; padding: 20px 22px 10px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.msg-row { display: flex; }
.msg-row.in { justify-content: flex-start; }
.msg-row.out { justify-content: flex-end; }
.msg-bubble {
  max-width: min(620px, 78%); padding: 10px 13px; border-radius: 12px;
  line-height: 1.55; white-space: pre-wrap; word-break: break-word;
  border: 1px solid var(--line); background: #fff;
}
.msg-row.out .msg-bubble {
  background: var(--accent); color: #fff; border-color: var(--accent);
  border-bottom-right-radius: 4px;
}
.msg-row.in .msg-bubble { border-bottom-left-radius: 4px; }
.msg-row.ai .msg-bubble { background: #f2f4f7; border-color: #eceef2; color: #2b3038; }
.msg-meta { font-size: 11px; color: var(--muted-2); margin-top: 5px; }
.msg-row.out .msg-meta { text-align: right; color: rgba(255,255,255,.75); }
.msg-attach { margin-top: 8px; }
.msg-attach img { max-width: 100%; border-radius: 8px; display: block; }
.msg-day {
  align-self: center; font-size: 11.5px; color: var(--muted-2);
  background: #eceef2; padding: 3px 10px; border-radius: 999px; margin: 6px 0;
}

.composer {
  flex: none; display: flex; gap: 10px; align-items: flex-end;
  padding: 12px 18px 14px; border-top: 1px solid var(--line); background: var(--panel);
}
.composer textarea {
  flex: 1; border: 1px solid var(--line-strong); border-radius: 10px;
  padding: 11px 13px; outline: none; max-height: 140px; min-height: 42px; line-height: 1.5;
}
.composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(17, 24, 39, .38); }
.modal-panel {
  position: relative; background: #fff; border-radius: 14px; width: 100%; max-width: 620px;
  max-height: 86vh; overflow-y: auto; box-shadow: 0 24px 60px rgba(16,24,40,.22);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: #fff; z-index: 2;
}
.modal-head h3 { margin: 0; font-size: 15.5px; font-weight: 600; }

.manage-list { padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.manage-item {
  border: 1px solid var(--line); border-radius: 10px; padding: 11px 13px;
  display: flex; align-items: center; gap: 10px; background: var(--panel-2);
}
.manage-info { flex: 1; min-width: 0; }
.manage-name { font-weight: 500; display: flex; align-items: center; gap: 8px; }
.manage-url { font-size: 12px; color: var(--muted-2); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.manage-actions { display: flex; gap: 6px; flex: none; }

.instance-form { border-top: 1px solid var(--line); padding: 16px 20px 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .field:nth-child(2), .form-grid .field:nth-child(3) { grid-column: span 2; }
.form-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; margin-top: 14px; }
.form-actions .error-text { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #1f2329; color: #fff; padding: 10px 16px; border-radius: 9px;
  font-size: 13px; z-index: 90; box-shadow: 0 8px 24px rgba(0,0,0,.2);
  max-width: 90vw;
}

.only-mobile { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .only-mobile { display: inline-flex; }
  .topbar-title { display: none; }
  .instance-btn span:nth-child(2) { max-width: 120px; }
  .layout { position: relative; }
  .sidebar {
    position: absolute; inset: 0; width: 100%; z-index: 15; transform: translateX(0);
    transition: transform .2s ease;
  }
  .sidebar.off { transform: translateX(-100%); }
  .chat-panel { position: absolute; inset: 0; }
  .msg-bubble { max-width: 84%; }
  .topbar-right .btn-ghost { display: none; }
  .modal-panel { max-height: 92vh; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .field:nth-child(2), .form-grid .field:nth-child(3) { grid-column: auto; }
}

.msg-row.system .msg-bubble {
  background: #fdfaf1; border-color: #f0e7cf; color: #7c6220;
}
