:root {
  --bg: #0f0f1a;
  --bg2: #1a1a2e;
  --bg3: #16213e;
  --accent: #7c6af7;
  --accent2: #a78bfa;
  --text: #e2e8f0;
  --text2: #94a3b8;
  --border: #2d2d44;
  --user-bubble: #2d1f6e;
  --ai-bubble: #1e1e35;
  --danger: #f87171;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8fafc;
    --bg2: #ffffff;
    --bg3: #f1f5f9;
    --text: #1e293b;
    --text2: #64748b;
    --border: #e2e8f0;
    --user-bubble: #ede9fe;
    --ai-bubble: #ffffff;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
}

#app { height: 100dvh; display: flex; flex-direction: column; }

.page { height: 100%; }
.hidden { display: none !important; }

/* 登录页 */
#login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 40px 32px;
  background: var(--bg2);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  width: 90%;
  max-width: 360px;
}
.logo { font-size: 2.5rem; font-weight: 700; color: var(--accent2); letter-spacing: -1px; }
.tagline { color: var(--text2); font-size: 0.875rem; }
.login-box input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
}
.login-box input:focus { border-color: var(--accent); }
.login-box button {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
}
.error-msg { color: var(--danger); font-size: 0.875rem; min-height: 1.2em; }

/* 主界面布局 */
#main-page { display: flex; position: relative; overflow: hidden; }

/* 侧边栏 */
.sidebar {
  position: fixed;
  left: 0; top: 0; bottom: 0;
  width: 280px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  z-index: 100;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
}
.sidebar.open { transform: translateX(0); }
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}
.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  padding-top: calc(16px + var(--safe-top));
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  flex: 1;
  padding: 10px 4px;
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 0.875rem;
  border-bottom: 2px solid transparent;
}
.tab-btn.active { color: var(--accent2); border-bottom-color: var(--accent2); }
.tab-content { flex: 1; overflow-y: auto; padding: 8px; }
.tab-content.hidden { display: none; }

.full-btn {
  width: 100%;
  padding: 10px;
  margin-bottom: 8px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
}

.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.session-item:hover, .session-item.active { background: var(--bg3); }
.session-title-text { font-size: 0.875rem; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.session-title-edit {
  flex: 1;
  font-size: 0.875rem;
  background: var(--bg2);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--text);
  padding: 2px 6px;
  outline: none;
  min-width: 0;
}
.session-del { color: var(--text2); background: none; border: none; cursor: pointer; padding: 0 4px; font-size: 1.1rem; opacity: 0; }
.session-item:hover .session-del { opacity: 1; }

.memory-card {
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bg3);
  margin-bottom: 8px;
  font-size: 0.8rem;
}
.memory-card .mem-type { color: var(--accent2); font-weight: 600; margin-bottom: 4px; }
.memory-card .mem-subject { font-weight: 600; margin-bottom: 4px; }
.memory-card .mem-body { color: var(--text2); }

.search-row { display: flex; gap: 6px; margin-bottom: 8px; }
.search-row input { flex: 1; padding: 8px; background: var(--bg3); border: 1px solid var(--border); border-radius: 8px; color: var(--text); outline: none; font-size: 0.875rem; }
.search-row button { padding: 8px 12px; background: var(--accent); color: white; border: none; border-radius: 8px; cursor: pointer; }

.ops-btn {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  margin-bottom: 6px;
  font-size: 0.875rem;
}
.ops-btn:hover { border-color: var(--accent); }
.terminal {
  background: #0d1117;
  color: #e6edf3;
  font-family: 'SF Mono', Consolas, monospace;
  font-size: 0.8rem;
  padding: 12px;
  border-radius: 8px;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 300px;
  overflow-y: auto;
  margin-top: 8px;
}

/* 聊天区 */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
}

#top-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  padding-top: calc(12px + var(--safe-top));
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#session-title {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 4px 8px;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--bg3); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 0.9375rem;
  word-break: break-word;
}
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.bubble p { margin-bottom: 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: #0d1117; border-radius: 6px; padding: 12px; overflow-x: auto; margin: 8px 0; }
.bubble code:not(pre code) { background: var(--bg3); padding: 2px 6px; border-radius: 4px; font-size: 0.875em; }
.bubble ul, .bubble ol { padding-left: 20px; margin-bottom: 8px; }
.bubble blockquote { border-left: 3px solid var(--accent); padding-left: 12px; color: var(--text2); margin: 8px 0; }

.tool-indicator {
  font-size: 0.75rem;
  color: var(--accent2);
  padding: 4px 10px;
  background: var(--bg3);
  border-radius: 99px;
  margin-bottom: 6px;
  border: 1px solid var(--border);
}

.typing-cursor::after {
  content: '▋';
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

#input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.input-icon-btn {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-bottom: 2px;
}
.input-icon-btn:hover { color: var(--accent2); background: var(--bg3); }
#msg-input {
  flex: 1;
  padding: 9px 14px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text);
  font-size: 0.9375rem;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.5;
}
#msg-input:focus { border-color: var(--accent); }
#send-btn {
  width: 38px;
  height: 38px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-bottom: 2px;
}
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: var(--text);
  padding: 10px 20px;
  border-radius: 99px;
  font-size: 0.875rem;
  z-index: 200;
  border: 1px solid var(--border);
  animation: fadeInOut 3s ease forwards;
}
@keyframes fadeInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(10px); }
  15% { opacity: 1; transform: translateX(-50%) translateY(0); }
  75% { opacity: 1; }
  100% { opacity: 0; }
}
