/* === AI 助手 PWA · 极简聊天版（v0.5） === */
:root {
  --bg: #0b1020;
  --bg-2: #0f1530;
  --surface: #141a2e;
  --card: #1a2240;
  --card-2: #1f2850;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #e8ecf4;
  --text-2: #aab2c8;
  --text-3: #6f7a96;
  --accent: #6ea8ff;
  --accent-workbench: #6ea8ff;
  --accent-knowledge: #d4a85e;
  --accent-lighting: #f5b572;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 96px;
  --input-h: 64px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { background: var(--bg); height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
               "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB",
               "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  height: 100%;
}

/* === App = column: header + body === */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* === Header（紧凑） === */
.app-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 14px;
  height: 52px;
  padding: 0 calc(14px + env(safe-area-inset-right, 0px)) 0 calc(14px + env(safe-area-inset-left, 0px));
  padding-top: var(--safe-top);
  height: calc(52px + var(--safe-top));
  background: rgba(11, 16, 32, 0.85);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  flex-shrink: 0;
}
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), #9277ff);
  color: #0b1020;
  font-weight: 800;
  font-size: 13px;
  box-shadow: 0 4px 14px rgba(110, 168, 255, 0.3);
}
.brand-text { font-size: 14px; }
.search-wrap { flex: 1; max-width: 360px; }
#search {
  width: 100%;
  height: 34px;
  padding: 0 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  outline: none;
}
#search::placeholder { color: var(--text-3); }
#search:focus { border-color: var(--accent); }

/* === Body = row: [sidebar | chat] (mobile: project-bar + chat) === */
.body {
  flex: 1;
  min-height: 0;
  display: flex;
}

/* === Sidebar (hidden mobile) === */
.sidebar {
  display: none;
  flex-direction: column;
  width: 200px;
  flex-shrink: 0;
  padding: 12px 10px;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.sidebar-header {
  padding: 4px 8px 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--text-3);
}
.sidebar-tree { display: flex; flex-direction: column; gap: 4px; }

.tree-project-head {
  appearance: none;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  background: transparent;
  color: var(--text-2);
  border: 0;
  border-radius: 9px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s, color 0.1s;
}
.tree-project-head:hover { background: var(--surface); color: var(--text); }
.sidebar-project.is-current .tree-project-head {
  background: var(--card-2);
  color: var(--text);
}
.sidebar-project[data-project="workbench"].is-current .tree-project-head { box-shadow: inset 3px 0 0 var(--accent-workbench); }
.sidebar-project[data-project="knowledge"].is-current .tree-project-head { box-shadow: inset 3px 0 0 var(--accent-knowledge); }
.sidebar-project[data-project="lighting"].is-current  .tree-project-head { box-shadow: inset 3px 0 0 var(--accent-lighting); }
.tree-project-num {
  font-size: 10px;
  color: var(--text-3);
  background: rgba(255, 255, 255, 0.05);
  padding: 1px 6px;
  border-radius: 4px;
  font-weight: 700;
}
.tree-project-icon { font-size: 16px; }
.tree-project-name { flex: 1; }
.tree-project-count {
  font-size: 11px;
  color: var(--text-3);
  background: rgba(255, 255, 255, 0.05);
  padding: 1px 6px;
  border-radius: 4px;
}

/* === Mobile project bar (shown mobile, hidden desktop) === */
.project-bar {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.project-bar::-webkit-scrollbar { display: none; }
.project-bar:empty { display: none; }
.project-pill {
  appearance: none;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.project-pill[aria-selected="true"] {
  background: var(--card-2);
  color: var(--text);
  border-color: var(--border-strong);
}
.project-pill[data-project="workbench"][aria-selected="true"] { box-shadow: 0 0 0 1px var(--accent-workbench) inset; }
.project-pill[data-project="knowledge"][aria-selected="true"] { box-shadow: 0 0 0 1px var(--accent-knowledge) inset; }
.project-pill[data-project="lighting"][aria-selected="true"]  { box-shadow: 0 0 0 1px var(--accent-lighting) inset; }
.pill-num { color: var(--text-3); font-weight: 700; font-size: 11px; }
.pill-icon { font-size: 14px; }

/* === Chat = 占满中间所有空间 === */
.chat {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-height: 0; /* 关键：让 messages 内部滚动 */
}

/* === Chat header（极简） === */
.chat-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 16, 32, 0.6);
}
.chat-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  min-width: 0;
}
.chat-eyebrow { font-size: 14px; }
.chat-project {
  font-weight: 700;
  color: var(--text);
}
.chat-status-text {
  color: var(--text-3);
  font-size: 12px;
}
.model-select {
  appearance: none;
  -webkit-appearance: none;
  height: 26px;
  margin-left: 6px;
  padding: 0 22px 0 8px;
  background: var(--surface) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path d='M2 3.5l3 3 3-3' stroke='%23aab2c8' stroke-width='1.2' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>") no-repeat right 7px center / 10px;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.model-select:focus { border-color: var(--accent); outline: none; }
.model-select:hover { color: var(--text); }
.chat-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-3);
}
.chat-dot.is-online { background: #6ee7b3; box-shadow: 0 0 0 3px rgba(110, 231, 179, 0.18); }
.chat-dot.is-busy   { background: #f5b572; box-shadow: 0 0 0 3px rgba(245, 181, 114, 0.22); animation: chatPulse 1.1s infinite; }
.chat-dot.is-error  { background: #ff6b6b; box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.22); }
@keyframes chatPulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

.chat-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* 检索/项目入口：用 details 折叠 */
.chat-meta, .entries-panel {
  font-size: 11.5px;
  color: var(--text-2);
  position: relative;
}
.chat-meta summary, .entries-panel summary {
  cursor: pointer;
  list-style: none;
  user-select: none;
  padding: 6px 10px;
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 12px;
}
.chat-meta summary::-webkit-details-marker,
.entries-panel summary::-webkit-details-marker { display: none; }
.chat-meta[open] > summary, .entries-panel[open] > summary {
  background: var(--surface);
  color: var(--text);
}
.chat-meta > ul, .entries-grid {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 10;
  margin: 0;
  padding: 10px 12px;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 4px;
  min-width: 240px;
  max-width: 360px;
  max-height: 280px;
  overflow-y: auto;
}
.chat-meta ul li code,
.entries-grid .entry-chip {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 11.5px;
  color: var(--text-2);
  word-break: break-all;
}
.entries-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.entry-chip {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.entry-chip:hover { background: var(--card-2); color: var(--text); }
.chat-meta-foot {
  margin: 6px 0 0;
  color: var(--text-3);
  font-size: 11px;
}
.chat-meta-foot code { color: var(--text-2); }

.chat-icon-btn {
  appearance: none;
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  line-height: 1;
}
.chat-icon-btn:hover { background: var(--surface); color: var(--text); }

/* === Messages：中间 flex:1，自己滚动 === */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
.chat-empty {
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
  padding: 36px 8px;
  line-height: 1.6;
}
.chat-empty strong { color: var(--text); }

.msg {
  max-width: 84%;
  padding: 9px 13px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}
.msg-user {
  align-self: flex-end;
  background: var(--accent);
  color: #0b1020;
  border-bottom-right-radius: 4px;
}
.msg-assistant {
  align-self: flex-start;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-error {
  align-self: flex-start;
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid rgba(255, 107, 107, 0.4);
  color: #ffd0d0;
}
.msg-reasoning {
  align-self: flex-start;
  max-width: 84%;
  background: transparent;
  color: var(--text-3);
  border: 1px dashed var(--border);
  font-style: italic;
  font-size: 12.5px;
  padding: 7px 11px;
  border-radius: 10px;
}
.msg-cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: -2px;
  animation: chatBlink 1s infinite;
}
@keyframes chatBlink { 0%, 49% { opacity: 1; } 50%, 100% { opacity: 0; } }

/* === 输入行：sticky bottom === */
.chat-input-row {
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: rgba(11, 16, 32, 0.85);
  backdrop-filter: saturate(140%) blur(10px);
  -webkit-backdrop-filter: saturate(140%) blur(10px);
  border-top: 1px solid var(--border);
}
#chat-input {
  flex: 1;
  resize: none;
  font: inherit;
  font-size: 15px;
  padding: 9px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  max-height: 120px;
  min-height: 38px;
  line-height: 1.45;
}
#chat-input:focus { border-color: var(--accent); background: var(--card); }
.chat-send-btn {
  appearance: none;
  flex-shrink: 0;
  height: 38px;
  padding: 0 16px;
  background: var(--accent);
  color: #0b1020;
  border: 0;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.chat-send-btn:hover { filter: brightness(1.08); }
.chat-send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* === Desktop layout === */
@media (min-width: 880px) {
  .sidebar { display: flex; }
  .project-bar { display: none !important; }
  .body { display: flex; flex-direction: row; }
}
