* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px; color: #222; background: #f5f5f7;
}

.page { display: none; height: 100vh; }
.page.show { display: flex; flex-direction: column; }

/* ========== 登录页 ========== */
#loginPage { align-items: center; justify-content: center; }
.login-box {
  width: 90%; max-width: 360px; background: #fff; border-radius: 12px;
  padding: 28px 24px; box-shadow: 0 4px 20px rgba(0,0,0,.08);
}
.login-box h1 { font-size: 20px; text-align: center; margin-bottom: 20px; }
.tabs { display: flex; margin-bottom: 16px; border-bottom: 1px solid #eee; }
.tabs button {
  flex: 1; padding: 10px; border: none; background: none; cursor: pointer;
  font-size: 14px; color: #888; border-bottom: 2px solid transparent;
}
.tabs button.active { color: #1976d2; border-bottom-color: #1976d2; font-weight: 600; }
.form input {
  width: 100%; padding: 10px 12px; margin-bottom: 12px; border: 1px solid #ddd;
  border-radius: 8px; font-size: 14px;
}
.form input:focus { outline: none; border-color: #1976d2; }
.form button { width: 100%; }

/* ========== 按钮 ========== */
button {
  padding: 8px 14px; border: 1px solid #ddd; background: #fff; border-radius: 8px;
  cursor: pointer; font-size: 14px; font-family: inherit;
}
button:hover { background: #f5f5f5; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: #1976d2; color: #fff; border-color: #1976d2; }
button.primary:hover { background: #1565c0; }
button.full { width: 100%; }
button.icon-btn { border: none; background: none; font-size: 20px; padding: 6px 10px; line-height: 1; cursor: pointer; }
button.danger { color: #e53935; border-color: #ef9a9a; }
button.danger:hover { background: #ffebee; }

/* ========== 顶部栏 ========== */
#topbar {
  height: 52px; flex-shrink: 0; background: #fff; border-bottom: 1px solid #e5e5e5;
  display: flex; align-items: center; justify-content: space-between; padding: 0 8px; z-index: 30;
}
#titleText { font-weight: 600; }

#mainPage { position: relative; }

/* ========== 侧边栏 ========== */
#sidebar {
  position: absolute; top: 52px; left: 0; bottom: 0; width: 280px;
  background: #fff; border-right: 1px solid #e5e5e5;
  display: flex; flex-direction: column;
  transform: translateX(-100%); transition: transform .25s ease; z-index: 20;
}
#sidebar.open { transform: translateX(0); }

.side-tabs { display: flex; border-bottom: 1px solid #eee; flex-shrink: 0; }
.side-tabs button {
  flex: 1; border: none; background: none; padding: 12px; border-radius: 0;
  font-size: 14px; color: #888; border-bottom: 2px solid transparent;
}
.side-tabs button.active { color: #1976d2; border-bottom-color: #1976d2; font-weight: 600; }

.side-panel { display: none; flex: 1; overflow: auto; padding: 8px; flex-direction: column; }
.side-panel.active { display: flex; }

#btnNewSession { margin-bottom: 8px; flex-shrink: 0; }
#sessionList { flex: 1; overflow: auto; }
.session-item {
  padding: 10px; border-radius: 8px; cursor: pointer; margin-bottom: 4px;
  display: flex; justify-content: space-between; align-items: center; font-size: 13px;
}
.session-item:hover { background: #f5f5f5; }
.session-item.active { background: #e3f2fd; }
.session-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .del { color: #999; padding: 0 6px; font-size: 16px; line-height: 1; }
.session-item .del:hover { color: #e53935; }

.side-path { display: flex; gap: 4px; margin-bottom: 6px; flex-shrink: 0; }
.side-path input {
  flex: 1; padding: 6px 8px; border: 1px solid #ddd; border-radius: 6px; font-size: 13px;
}
.side-path button { padding: 6px 10px; font-size: 13px; }
.side-actions { display: flex; gap: 6px; margin-bottom: 6px; flex-shrink: 0; }
.side-actions button { flex: 1; font-size: 12px; padding: 6px; }

/* ========== 批量选择栏 ========== */
.select-bar {
  display: flex; align-items: center; gap: 6px; flex-shrink: 0;
  padding: 6px 8px; margin-bottom: 6px; background: #f7f8fa;
  border: 1px solid #eceef1; border-radius: 8px; font-size: 12px;
}
.select-bar .sel-all { display: flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }
.select-bar .sel-count { flex: 1; color: #888; white-space: nowrap; }
.select-bar button { padding: 4px 8px; font-size: 12px; border-radius: 6px; }

#fileTree { flex: 1; overflow: auto; font-size: 13px; }

.tree-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 8px; cursor: pointer; border-radius: 6px;
  white-space: nowrap; overflow: hidden;
}
.tree-item:hover { background: #f0f0f0; }
.tree-item.selected { background: #e3f2fd; }
.tree-item .tree-check { flex-shrink: 0; margin-right: 6px; cursor: pointer; }
.tree-item .tree-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.tree-item .tree-more {
  padding: 0 6px; color: #999; font-size: 16px; line-height: 1;
  cursor: pointer; flex-shrink: 0;
}
.tree-item .tree-more:hover { color: #1976d2; }

#overlay {
  position: absolute; inset: 52px 0 0 0; background: rgba(0,0,0,.35);
  display: none; z-index: 15;
}
#overlay.show { display: block; }

#chatArea { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: #f5f5f7; }
#chatLog { flex: 1; overflow: auto; padding: 12px; }
.msg {
  margin-bottom: 10px; padding: 10px 12px; border-radius: 12px;
  line-height: 1.6; word-break: break-word; max-width: 88%; white-space: pre-wrap;
}
.msg.user { background: #1976d2; color: #fff; margin-left: auto; }
.msg.assistant { background: #fff; }
.msg.system { background: #fff9c4; font-size: 12px; max-width: 100%; }
.msg.error { background: #ffebee; color: #c62828; font-size: 12px; max-width: 100%; }

/* ========== Agent 步骤卡（思考 / 工具调用） ========== */
.step-card {
  margin-bottom: 8px; border-radius: 10px; overflow: hidden;
  border: 1px solid #e0e0e0; background: #fafafa; max-width: 100%;
  font-size: 13px;
}
.step-card.reasoning { border-color: #d0c4f0; background: #f7f4ff; }
.step-card.tool { border-color: #cfe3d4; background: #f4fbf6; }
.step-card.running { border-color: #ffd54f; background: #fffdf3; }

.step-head {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; cursor: pointer; user-select: none;
}
.step-head:hover { background: rgba(0,0,0,.03); }
.step-arrow { font-size: 10px; color: #999; width: 12px; flex-shrink: 0; }
.step-icon { flex-shrink: 0; }
.step-title { flex: 1; font-weight: 500; color: #333; }
.step-status { flex-shrink: 0; font-size: 12px; }

.step-body {
  padding: 8px 10px 10px 28px; border-top: 1px dashed #e0e0e0;
  white-space: pre-wrap; word-break: break-word;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px; color: #555; max-height: 320px; overflow: auto;
}
.step-card.collapsed .step-body { display: none; }

/* ========== 空会话欢迎卡 + 快捷指令 ========== */
.welcome-card {
  background: #fff; border-radius: 12px; padding: 16px;
  margin-bottom: 10px; max-width: 100%;
}
.welcome-title { font-size: 15px; font-weight: 600; color: #222; margin-bottom: 6px; }
.welcome-desc { font-size: 13px; color: #666; line-height: 1.6; margin-bottom: 12px; }
.quick-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
}
.quick-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; border: 1px solid #e0e0e0; border-radius: 10px;
  background: #fafafa; cursor: pointer; font-size: 13px; color: #333;
  font-family: inherit; text-align: left; transition: all .15s;
}
.quick-btn:hover { border-color: #1976d2; background: #f0f7ff; color: #1976d2; }
.quick-btn:active { transform: scale(.97); }
.quick-icon { font-size: 15px; flex-shrink: 0; }
@media (max-width: 480px) {
  .quick-grid { grid-template-columns: 1fr; }
}

#chatInputBar {
  flex-shrink: 0; display: flex; gap: 8px; padding: 10px; background: #fff;
  border-top: 1px solid #e5e5e5; padding-bottom: calc(10px + env(safe-area-inset-bottom));
}
#chatText {
  flex: 1; resize: none; height: 44px; padding: 10px 12px; border: 1px solid #ddd;
  border-radius: 10px; font-family: inherit; font-size: 14px;
}
#chatText:focus { outline: none; border-color: #1976d2; }
#btnSend { padding: 0 18px; }

/* ========== 个人中心页 ========== */
#topbarProfile {
  height: 52px; flex-shrink: 0; background: #fff; border-bottom: 1px solid #e5e5e5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 8px; font-weight: 600;
}
.profile-content { flex: 1; overflow: auto; padding: 16px; }
.profile-card {
  background: #fff; border-radius: 12px; padding: 24px;
  text-align: center; margin-bottom: 16px;
}
.profile-card .avatar {
  width: 64px; height: 64px; border-radius: 50%; background: #e3f2fd;
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; margin: 0 auto 12px;
}
.profile-card .uname { font-size: 18px; font-weight: 600; }
.profile-card .uhint { font-size: 12px; color: #999; margin-top: 4px; }

.setting-list { background: #fff; border-radius: 12px; overflow: hidden; margin-bottom: 16px; }
.setting-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; cursor: pointer; border-bottom: 1px solid #f0f0f0; font-size: 15px;
}
.setting-item:last-child { border-bottom: none; }
.setting-item:hover { background: #fafafa; }
.setting-item .arrow { color: #ccc; font-size: 20px; }
.setting-item.danger { color: #e53935; justify-content: center; }

/* ========== 弹层 ========== */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: none; align-items: center; justify-content: center; z-index: 100; padding: 16px;
}
.modal.show { display: flex; }
.modal-box {
  background: #fff; border-radius: 12px; padding: 20px;
  width: 100%; max-width: 400px; max-height: 85vh; display: flex; flex-direction: column;
}
.modal-box.wide { max-width: 900px; height: 85vh; }
.modal-box h3 { margin-bottom: 14px; font-size: 16px; }
.modal-box label { display: block; margin-bottom: 10px; font-size: 13px; }
.modal-box input:not([type="radio"]), .modal-box select {
  width: 100%; padding: 8px 10px; border: 1px solid #ddd; border-radius: 8px;
  margin-top: 4px; font-size: 14px;
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 10px; font-size: 13px; color: #666;
}
.head-actions { display: flex; gap: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.radio { display: flex; align-items: center; gap: 8px; }

/* ========== 技能 / 插件列表 ========== */
.ext-list { flex: 1; overflow: auto; }
.ext-empty { color: #999; font-size: 13px; text-align: center; padding: 40px 0; }
.ext-item {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px; border: 1px solid #eee; border-radius: 8px; margin-bottom: 8px;
}
.ext-main { flex: 1; min-width: 0; }
.ext-name { font-size: 14px; font-weight: 600; margin-bottom: 2px; }
.ext-desc {
  font-size: 12px; color: #888; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}
.ext-ops { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.ext-switch { display: flex; align-items: center; gap: 4px; font-size: 12px; color: #666; }
.ext-switch input { margin: 0; }
.icon-btn.danger { color: #e53935; }
.modal-box textarea {
  width: 100%; padding: 8px 10px; border: 1px solid #ddd; border-radius: 8px;
  margin-top: 4px; font-size: 13px; font-family: inherit; resize: vertical;
  box-sizing: border-box;
}

#previewContent {
  flex: 1; overflow: auto; background: #f5f5f5; padding: 12px; border-radius: 8px;
  font-family: monospace; font-size: 13px; white-space: pre-wrap; word-break: break-all;
}
#editContent {
  flex: 1; overflow: auto; background: #fff; padding: 12px; border-radius: 8px;
  border: 1px solid #ddd; font-family: monospace; font-size: 13px;
  resize: none; width: 100%; outline: none;
}
#editContent:focus { border-color: #1976d2; }

#confirmDesc {
  background: #f5f5f5; padding: 12px; border-radius: 8px; font-family: monospace;
  font-size: 12px; white-space: pre-wrap; word-break: break-all;
  max-height: 260px; overflow: auto;
}
.msg { margin-top: 8px; }
#loginMsg, #sshMsg { font-size: 13px; }
#loginMsg.error, #sshMsg.error { color: #c62828; }
#loginMsg.ok, #sshMsg.ok { color: #2e7d32; }

/* ========== 底部菜单 ========== */
.modal.bottom { align-items: flex-end; padding: 0; }
.bottom-sheet {
  width: 100%; background: #fff; border-radius: 16px 16px 0 0;
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  animation: slideUp .2s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.sheet-title {
  text-align: center; padding: 12px; color: #999; font-size: 13px;
  border-bottom: 1px solid #f0f0f0;
}
.sheet-item {
  text-align: center; padding: 16px; cursor: pointer; font-size: 16px;
  border-bottom: 1px solid #f0f0f0;
}
.sheet-item:hover { background: #fafafa; }
.sheet-item.danger { color: #e53935; }
.sheet-item.cancel { border-bottom: none; color: #666; margin-top: 8px; }

/* ========== 桌面端 ========== */
@media (min-width: 768px) {
  #sidebar { transform: translateX(0); width: 300px; top: 52px; }
  #chatArea { margin-left: 300px; }
  #overlay { display: none !important; }
  #btnMenu { display: none; }
}
