/* === VARIABLES === */
:root {
  --bg: #ffffff;
  --bg2: #f7f7f8;
  --bg3: #efefef;
  --border: #e0e0e0;
  --text: #1a1a1a;
  --text2: #666;
  --accent: #2563eb;
  --accent-bg: #eff6ff;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --sidebar-w: 260px;
  --header-h: 52px;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
}
[data-theme="dark"] {
  --bg: #1c1c27;
  --bg2: #14141e;
  --bg3: #2a2a3a;
  --border: #2a2a3a;
  --text: #e0e0e0;
  --text2: #888;
  --accent: #4a90d9;
  --accent-bg: #1a2a3a;
  --danger: #f87171;
  --danger-bg: #2a1a1a;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
}

/* === RESET === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg2); color: var(--text); height: 100vh; overflow: hidden; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }

/* === LOGIN === */
#login-screen {
  position: fixed; inset: 0; background: var(--bg2);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
#login-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: 16px;
  padding: 48px 40px; width: 360px; text-align: center;
  box-shadow: var(--shadow);
}
#login-logo {
  width: 64px; height: 64px; border-radius: 50%; background: var(--accent);
  color: white; font-size: 22px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 20px;
}
#login-box h1 { font-size: 22px; margin-bottom: 6px; }
#login-box p { color: var(--text2); margin-bottom: 24px; font-size: 14px; }
#login-input-wrap { display: flex; gap: 8px; }
#login-pin {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 18px; text-align: center;
  letter-spacing: 4px; background: var(--bg); color: var(--text);
  outline: none;
}
#login-pin:focus { border-color: var(--accent); }
#login-btn {
  padding: 10px 20px; background: var(--accent); color: white;
  border: none; border-radius: var(--radius); font-size: 15px;
  transition: opacity 0.15s;
}
#login-btn:hover { opacity: 0.85; }
#login-error { margin-top: 12px; color: var(--danger); font-size: 13px; min-height: 20px; }

/* === APP === */
#app { display: flex; height: 100vh; overflow: hidden; }

/* === SIDEBAR === */
#sidebar {
  width: var(--sidebar-w); background: var(--bg); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; overflow: hidden;
  transition: transform 0.25s ease;
}
#sidebar-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 12px; border-bottom: 1px solid var(--border);
}
#sidebar-logo {
  width: 32px; height: 32px; background: var(--accent); color: white;
  border-radius: 8px; display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
}
#sidebar-controls { display: flex; gap: 4px; }
#sidebar-controls button {
  width: 32px; height: 32px; border: none; background: transparent;
  color: var(--text2); border-radius: 8px; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
}
#sidebar-controls button:hover { background: var(--bg3); color: var(--text); }

#new-chat-btn {
  margin: 10px; padding: 9px; border: 1px solid var(--border);
  border-radius: var(--radius); background: transparent; color: var(--text);
  font-size: 13px; font-weight: 500; text-align: center;
  transition: background 0.15s;
}
#new-chat-btn:hover { background: var(--bg3); }

#search-wrap { padding: 0 10px 8px; }
#search-input {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px; background: var(--bg2); color: var(--text);
  outline: none;
}
#search-input:focus { border-color: var(--accent); }

/* Chat List */
#chat-list { flex: 1; overflow-y: auto; padding: 4px 6px; }
#chat-list::-webkit-scrollbar { width: 4px; }
#chat-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.chat-section-label {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--text2); padding: 8px 6px 4px;
}
.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 8px; border-radius: 8px; cursor: pointer;
  user-select: none; transition: background 0.1s; position: relative;
}
.chat-item:hover { background: var(--bg3); }
.chat-item.active { background: var(--accent-bg); color: var(--accent); }
.chat-item-icon { font-size: 14px; flex-shrink: 0; }
.chat-item-text { flex: 1; overflow: hidden; }
.chat-item-title {
  font-size: 13px; font-weight: 500; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.chat-item-title input {
  width: 100%; background: transparent; border: none; border-bottom: 1px solid var(--accent);
  color: var(--text); font-size: 13px; font-weight: 500; outline: none; padding: 0;
}
.chat-item-sub { font-size: 11px; color: var(--text2); margin-top: 1px; }

/* Settings */
#settings-wrap { border-top: 1px solid var(--border); }
#settings-toggle {
  width: 100%; padding: 12px; background: transparent; border: none;
  color: var(--text2); font-size: 13px; text-align: left; display: flex; align-items: center; gap: 6px;
}
#settings-toggle:hover { color: var(--text); }
#settings-panel {
  display: none; padding: 0 12px 12px; overflow-y: auto; max-height: 320px;
  display: flex; flex-direction: column; gap: 8px;
}
#settings-panel.open { display: flex; }
#settings-panel label { font-size: 11px; font-weight: 600; color: var(--text2); text-transform: uppercase; margin-top: 4px; }
#settings-panel select, #settings-panel textarea {
  width: 100%; padding: 6px 8px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg2); color: var(--text);
  font-size: 12px; outline: none;
}
#settings-panel textarea { resize: vertical; min-height: 60px; }
#settings-panel input[type=range] { width: 100%; accent-color: var(--accent); }
.setting-row { display: flex; align-items: center; justify-content: space-between; }
.toggle { position: relative; width: 34px; height: 19px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.slider-t { position: absolute; inset: 0; background: var(--border); border-radius: 99px; cursor: pointer; transition: background 0.2s; }
.slider-t::before { content: ''; position: absolute; width: 13px; height: 13px; left: 3px; top: 3px; background: white; border-radius: 50%; transition: transform 0.2s; }
.toggle input:checked + .slider-t { background: var(--accent); }
.toggle input:checked + .slider-t::before { transform: translateX(15px); }

/* === MAIN === */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

#main-header {
  height: var(--header-h); display: flex; align-items: center; gap: 10px;
  padding: 0 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
#menu-btn {
  display: none; width: 36px; height: 36px; border: none;
  background: transparent; color: var(--text); font-size: 18px;
  border-radius: 8px; flex-shrink: 0;
}
#menu-btn:hover { background: var(--bg3); }
#chat-title-header { flex: 1; font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#header-actions { display: flex; gap: 6px; }
#header-actions button {
  padding: 6px 12px; border: 1px solid var(--border); background: transparent;
  color: var(--text2); border-radius: 8px; font-size: 12px;
}
#header-actions button:hover { background: var(--bg3); color: var(--text); }

/* Messages */
#messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 16px; max-width: 1100px; width: 100%; margin: 0 auto; }
#messages::-webkit-scrollbar { width: 4px; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

#welcome-msg { text-align: center; color: var(--text2); margin: auto; padding: 2rem; }
#welcome-icon { width: 56px; height: 56px; background: var(--accent); color: white; border-radius: 50%; font-size: 18px; font-weight: 700; display: flex; align-items: center; justify-content: center; margin: 0 auto 16px; }
#welcome-msg h2 { color: var(--text); margin-bottom: 8px; }

.msg { display: flex; gap: 10px; align-items: flex-start; }
.msg.user { flex-direction: row-reverse; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0; }
.avatar.ai { background: var(--accent-bg); color: var(--accent); }
.avatar.user { background: var(--bg3); color: var(--text2); }
.bubble { max-width: 75%; padding: 10px 30px; border-radius: 12px; font-size: 14px; line-height: 1.65; }
.msg.assistant .bubble { background: var(--bg2); border: 1px solid var(--border); color: var(--text); }
.msg.user .bubble { background: var(--accent); color: white; }
.think-block { font-size: 12px; color: var(--text2); border-left: 2px solid var(--border); padding: 4px 8px; margin-bottom: 8px; font-style: italic; white-space: pre-wrap; }

/* Code */
.code-wrap { position: relative; margin: 8px 0; }
.code-wrap pre { margin: 0; border-radius: 8px; overflow-x: auto; padding-bottom: 36px !important; }
.copy-btn {
  position: absolute; bottom: 8px; right: 8px;
  padding: 4px 12px; font-size: 11px; font-family: inherit;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text2); cursor: pointer; transition: all 0.15s;
}
.copy-btn:hover { background: var(--bg3); color: var(--text); }
.copy-btn.copied { background: #e8f5e9; color: #2e7d32; border-color: #a5d6a7; }

/* Typing */
.typing { display: inline-flex; gap: 5px; align-items: center; padding: 4px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,80%,100%{opacity:0.2} 40%{opacity:1} }

/* Scroll btn */
#scroll-btn {
  display: none; position: absolute; bottom: 80px; right: 16px;
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 16px; cursor: pointer;
  box-shadow: var(--shadow); z-index: 5; align-items: center; justify-content: center;
}
#main { position: relative; }

/* Stat bar */
#stat-bar {
  padding: 5px 16px; font-size: 11px; color: var(--text2);
  border-top: 1px solid var(--border); background: var(--bg2);
  display: flex; gap: 6px;
}
#stat-bar span { color: var(--accent); font-weight: 600; }

/* Input area */
#input-area {
  padding: 12px 16px; border-top: 1px solid var(--border);
  display: flex; gap: 8px; align-items: flex-end;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}
#user-input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg2); color: var(--text);
  font-size: 14px; resize: none; min-height: 42px; max-height: 140px;
  outline: none; line-height: 1.5; transition: border-color 0.15s;
}
#user-input:focus { border-color: var(--accent); }
#send-btn, #stop-btn {
  width: 42px; height: 42px; border-radius: var(--radius); border: none;
  font-size: 16px; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
#send-btn { background: var(--accent); color: white; }
#send-btn:hover { opacity: 0.85; }
#send-btn:disabled { opacity: 0.4; cursor: default; }
#stop-btn { background: var(--danger); color: white; }
#stop-btn:hover { opacity: 0.85; }

/* === CONTEXT MENU === */
#ctx-menu, #msg-ctx-menu {
  position: fixed; background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  z-index: 500; min-width: 160px; padding: 4px 0; display: none;
}
.ctx-item { padding: 9px 14px; font-size: 13px; cursor: pointer; color: var(--text); }
.ctx-item:hover { background: var(--bg3); }
.ctx-item.ctx-danger { color: var(--danger); }
.ctx-item.ctx-danger:hover { background: var(--danger-bg); }
.ctx-divider { height: 1px; background: var(--border); margin: 4px 0; }

/* === REPLY BAR === */
#reply-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; background: var(--accent-bg);
  border-top: 1px solid var(--border); font-size: 13px;
}
#reply-bar-text { flex: 1; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#reply-bar-close {
  background: none; border: none; color: var(--text2); font-size: 16px;
  cursor: pointer; padding: 0 4px; line-height: 1;
}
#reply-bar-close:hover { color: var(--danger); }

/* === PINNED BAR === */
#pinned-bar {
  display: flex; align-items: center; flex-wrap: nowrap; gap: 8px;
  padding: 6px 16px; background: var(--accent-bg);
  border-bottom: 1px solid var(--border); font-size: 13px;
  overflow-x: auto; cursor: default;
}
#pinned-bar-text { display: flex; flex-wrap: nowrap; gap: 8px; align-items: center; cursor: default; }
#pinned-bar-close {
  background: none; border: none; color: var(--text2); font-size: 16px;
  cursor: pointer; padding: 0 4px; line-height: 1;
}
#pinned-bar-close:hover { color: var(--danger); }

/* === PIN DIALOG === */
#pin-dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: none; align-items: center; justify-content: center; z-index: 600;
}
#pin-dialog-overlay.open { display: flex; }
#pin-dialog {
  background: var(--bg); border-radius: 14px; padding: 28px;
  width: 340px; box-shadow: var(--shadow); border: 1px solid var(--border);
}
#pin-dialog h3 { margin-bottom: 6px; }
#pin-dialog p { color: var(--text2); font-size: 13px; margin-bottom: 14px; }
#pin-name-input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; background: var(--bg2); color: var(--text);
  outline: none; margin-bottom: 16px;
}
#pin-name-input:focus { border-color: var(--accent); }
#pin-dialog-btns { display: flex; gap: 8px; justify-content: flex-end; }
#pin-cancel-btn { padding: 8px 16px; border: 1px solid var(--border); border-radius: 8px; background: transparent; color: var(--text); font-size: 13px; }
#pin-confirm-btn { padding: 8px 16px; border: none; border-radius: 8px; background: var(--accent); color: white; font-size: 13px; }

/* === MOBILE === */
#sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.4); z-index: 99;
}

@media (max-width: 768px) {
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 100;
    transform: translateX(-100%); box-shadow: var(--shadow);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-overlay.open { display: block; }
  #menu-btn { display: flex; }
  #main { width: 100%; }
  .bubble { max-width: 50%;
  #login-box { width: 90%; padding: 32px 24px; }
}

#pinned-bar { flex-wrap: wrap; gap: 6px; }
.pinned-tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 14px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 99px; font-size: 12px; cursor: pointer;
  white-space: nowrap; margin-right: 4px;user-select: none;
}
.pinned-tag:hover { background: var(--bg3); }
.pinned-tag-close { background: none; border: none; cursor: pointer; color: var(--text2); font-size: 13px; padding: 0; }
.pinned-tag-close:hover { color: var(--danger); }