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

/* ── Theme variables ── */
:root {
  --sidebar-w: 260px;
  --tr: 0.32s cubic-bezier(0.4, 0, 0.2, 1);
  --r-sm: 8px; --r-md: 12px; --r-lg: 16px; --r-xl: 20px;
  --accent: #7c6af7; --accent-2: #9d8df8;
  --accent-glow: rgba(124,106,247,0.15);
  --font: 'Geist', -apple-system, sans-serif;
  --mono: 'Geist Mono', 'Fira Code', monospace;
}

/* Dark theme (default) */
[data-theme="dark"] {
  --bg:       #0f0f10;
  --bg-2:     #18181b;
  --bg-3:     #1e1e22;
  --bg-4:     #27272b;
  --bg-h:     #2e2e33;
  --bd:       rgba(255,255,255,0.07);
  --bd-2:     rgba(255,255,255,0.12);
  --t1: #f4f4f5; --t2: #a1a1aa; --t3: #71717a;
  --user-bg:  #252530;
  --code-bg:  #13131a;
  --code-c:   #b8a9f8;
}

/* Light theme */
[data-theme="light"] {
  --bg:       #ffffff;
  --bg-2:     #f5f5f7;
  --bg-3:     #ebebef;
  --bg-4:     #e0e0e6;
  --bg-h:     #d8d8e0;
  --bd:       rgba(0,0,0,0.08);
  --bd-2:     rgba(0,0,0,0.15);
  --t1: #111118; --t2: #52525e; --t3: #8b8b9a;
  --user-bg:  #eeeef4;
  --code-bg:  #f0f0f6;
  --code-c:   #6350d0;
}

html, body { height: 100%; overflow: hidden; background: var(--bg); color: var(--t1); font-family: var(--font); font-size: 15px; line-height: 1.65; }
body { display: flex; height: 100vh; transition: background var(--tr), color var(--tr); }

/* ── Sidebar ── */
.sidebar {
  flex-shrink: 0; width: var(--sidebar-w); height: 100vh;
  background: var(--bg-2); border-right: 1px solid var(--bd);
  display: flex; flex-direction: column; overflow: hidden;
  transition: width var(--tr); position: relative; z-index: 20;
}
.sidebar.collapsed { width: 0; border-right-color: transparent; }
.sidebar-inner { width: var(--sidebar-w); height: 100%; display: flex; flex-direction: column; overflow: hidden; padding: 12px 10px; }

.sidebar-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; padding: 2px 4px; flex-shrink: 0; }
.sidebar-logo { display: flex; align-items: center; gap: 8px; }
.logo-mark { width: 28px; height: 28px; background: var(--accent); border-radius: var(--r-sm); display: flex; align-items: center; justify-content: center; color: #fff; font-size: 14px; flex-shrink: 0; }
.logo-text { font-size: 15px; font-weight: 600; color: var(--t1); letter-spacing: -0.01em; }

.icon-btn { width: 32px; height: 32px; border-radius: var(--r-sm); border: none; background: transparent; color: var(--t2); cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 17px; transition: background 0.15s, color 0.15s; flex-shrink: 0; }
.icon-btn:hover { background: var(--bg-h); color: var(--t1); }
.icon-btn:active { transform: scale(0.95); }

/* Expand btn */
.expand-btn {
  position: fixed; left: 12px; top: 12px; width: 32px; height: 32px; z-index: 30;
  border-radius: var(--r-sm); border: 1px solid var(--bd-2); background: var(--bg-2);
  color: var(--t2); cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 17px;
  opacity: 0; pointer-events: none; transform: translateX(-6px);
  transition: opacity var(--tr), transform var(--tr), background 0.15s, color 0.15s;
}
.expand-btn.visible { opacity: 1; pointer-events: all; transform: translateX(0); }
.expand-btn:hover { background: var(--bg-h); color: var(--t1); }

/* New chat */
.new-chat-btn { display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 38px; border-radius: var(--r-md); border: 1px solid var(--bd-2); background: transparent; color: var(--t1); font-family: var(--font); font-size: 14px; font-weight: 500; cursor: pointer; width: 100%; transition: background 0.15s; flex-shrink: 0; white-space: nowrap; overflow: hidden; margin-bottom: 16px; }
.new-chat-btn i { font-size: 16px; flex-shrink: 0; }
.new-chat-btn:hover { background: var(--bg-h); }

/* Section label */
.sidebar-section-label { font-size: 11px; font-weight: 500; letter-spacing: 0.07em; text-transform: uppercase; color: var(--t3); padding: 0 8px; margin-bottom: 4px; flex-shrink: 0; }

/* Chat list */
.chat-list { flex: 1; overflow-y: auto; overflow-x: hidden; margin: 0 -2px; padding: 0 2px; scrollbar-width: thin; scrollbar-color: var(--bg-4) transparent; }
.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 2px; }

.chat-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: var(--r-md); cursor: pointer; transition: background 0.15s; white-space: nowrap; min-width: 0; }
.chat-item:hover { background: var(--bg-3); }
.chat-item.active { background: var(--bg-4); }
.chat-item > i { font-size: 15px; color: var(--t3); flex-shrink: 0; }
.chat-item-title { font-size: 13.5px; color: var(--t2); overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.chat-item.active .chat-item-title { color: var(--t1); }
.chat-item-del { flex-shrink: 0; width: 22px; height: 22px; border-radius: 6px; border: none; background: transparent; color: var(--t3); cursor: pointer; display: none; align-items: center; justify-content: center; font-size: 14px; transition: background 0.15s, color 0.15s; }
.chat-item:hover .chat-item-del { display: flex; }
.chat-item-del:hover { background: var(--bg-h); color: #e24b4a; }

/* Sidebar bottom */
.sidebar-bottom { flex-shrink: 0; border-top: 1px solid var(--bd); padding-top: 12px; margin-top: 8px; display: flex; flex-direction: column; gap: 10px; }

.settings-entry { display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 34px; border-radius: var(--r-sm); border: none; background: transparent; color: var(--t2); font-family: var(--font); font-size: 13px; cursor: pointer; width: 100%; transition: background 0.15s, color 0.15s; white-space: nowrap; overflow: hidden; }
.settings-entry i { font-size: 15px; flex-shrink: 0; }
.settings-entry:hover { background: var(--bg-h); color: var(--t1); }

.model-selector-wrap { padding: 0 2px; }
.model-label { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 500; letter-spacing: 0.07em; text-transform: uppercase; color: var(--t3); margin-bottom: 6px; }
.model-label i { font-size: 13px; }
.model-select-row { display: flex; align-items: center; gap: 6px; }
.model-select { flex: 1; min-width: 0; height: 34px; background: var(--bg-3); border: 1px solid var(--bd-2); border-radius: var(--r-sm); color: var(--t1); font-family: var(--font); font-size: 13px; padding: 0 28px 0 8px; cursor: pointer; appearance: none; -webkit-appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 8px center; transition: border-color 0.15s; overflow: hidden; text-overflow: ellipsis; }
.model-select:focus { outline: none; border-color: var(--accent); }
.model-select option { background: var(--bg-3); color: var(--t1); }

.sidebar.collapsed .sidebar-label { display: none; }
#refreshModels.spinning i { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Main ── */
.main { flex: 1; display: flex; flex-direction: column; height: 100vh; overflow: hidden; min-width: 0; }
.chat-area { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 24px 0; scrollbar-width: thin; scrollbar-color: var(--bg-4) transparent; display: flex; flex-direction: column; }
.chat-area::-webkit-scrollbar { width: 5px; }
.chat-area::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 3px; }

/* ── Welcome ── */
.welcome { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 40px 24px; text-align: center; gap: 12px; }
.welcome-icon { width: 56px; height: 56px; background: var(--accent-glow); border: 1px solid rgba(124,106,247,0.3); border-radius: var(--r-lg); display: flex; align-items: center; justify-content: center; font-size: 24px; color: var(--accent-2); margin-bottom: 8px; }
.welcome h1 { font-size: 24px; font-weight: 600; color: var(--t1); letter-spacing: -0.02em; }
.welcome p { color: var(--t3); font-size: 14px; }
.welcome-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 20px; max-width: 520px; width: 100%; }
.welcome-card { background: var(--bg-2); border: 1px solid var(--bd); border-radius: var(--r-md); padding: 14px 16px; display: flex; align-items: center; gap: 10px; cursor: pointer; text-align: left; font-size: 13px; color: var(--t2); transition: background 0.15s, border-color 0.15s, color 0.15s; }
.welcome-card i { font-size: 18px; color: var(--accent); flex-shrink: 0; }
.welcome-card:hover { background: var(--bg-3); border-color: var(--bd-2); color: var(--t1); }

/* ── Messages ── */
.messages { display: flex; flex-direction: column; padding: 0 0 16px; }
.message { display: flex; padding: 6px 0; animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.message-inner { max-width: 780px; width: 100%; margin: 0 auto; padding: 0 28px; }

/* User message */
.message.user .message-inner { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.message.user .bubble { background: var(--user-bg); border: 1px solid var(--bd); border-radius: var(--r-xl) var(--r-xl) var(--r-sm) var(--r-xl); padding: 11px 15px; max-width: 78%; color: var(--t1); font-size: 14.5px; line-height: 1.65; white-space: pre-wrap; word-break: break-word; }

/* User edit inline */
.user-edit-area { width: 78%; max-width: 78%; }
.user-edit-area textarea { width: 100%; background: var(--bg-3); border: 1px solid var(--accent); border-radius: var(--r-md); padding: 10px 12px; color: var(--t1); font-family: var(--font); font-size: 14.5px; line-height: 1.65; resize: none; outline: none; min-height: 60px; }
.user-edit-btns { display: flex; gap: 6px; justify-content: flex-end; margin-top: 6px; }
.edit-save-btn, .edit-cancel-btn { padding: 5px 14px; border-radius: var(--r-sm); border: 1px solid var(--bd-2); font-family: var(--font); font-size: 12.5px; cursor: pointer; transition: background 0.15s, color 0.15s; }
.edit-save-btn { background: var(--accent); color: #fff; border-color: var(--accent); }
.edit-save-btn:hover { background: var(--accent-2); }
.edit-cancel-btn { background: transparent; color: var(--t2); }
.edit-cancel-btn:hover { background: var(--bg-h); color: var(--t1); }

/* User message actions */
.user-msg-actions { display: flex; gap: 4px; opacity: 0; transition: opacity 0.15s; }
.message.user:hover .user-msg-actions { opacity: 1; }

/* AI message */
.message.ai .ai-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.ai-avatar { width: 26px; height: 26px; background: var(--accent); border-radius: var(--r-sm); display: flex; align-items: center; justify-content: center; font-size: 13px; color: #fff; flex-shrink: 0; }
.ai-name { font-size: 12.5px; font-weight: 500; color: var(--t3); }
.message.ai .bubble { font-size: 14.5px; line-height: 1.75; color: var(--t1); word-break: break-word; }

/* AI branch navigator */
.ai-branch-nav { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.branch-btn { display: flex; align-items: center; justify-content: center; width: 24px; height: 24px; border-radius: 6px; border: 1px solid var(--bd-2); background: transparent; color: var(--t2); cursor: pointer; font-size: 14px; transition: background 0.15s, color 0.15s; }
.branch-btn:hover:not(:disabled) { background: var(--bg-h); color: var(--t1); }
.branch-btn:disabled { opacity: 0.35; cursor: default; }
.branch-label { font-size: 12px; color: var(--t3); min-width: 32px; text-align: center; user-select: none; }

/* Markdown */
.bubble h1,.bubble h2,.bubble h3,.bubble h4 { color: var(--t1); font-weight: 600; margin: 1.1em 0 0.35em; letter-spacing: -0.01em; }
.bubble h1{font-size:1.25em}.bubble h2{font-size:1.1em}.bubble h3{font-size:1em}
.bubble p{margin:.45em 0}.bubble ul,.bubble ol{padding-left:1.4em;margin:.35em 0}.bubble li{margin:.2em 0}
.bubble code { font-family: var(--mono); font-size: .88em; background: var(--bg-3); border: 1px solid var(--bd); border-radius: 5px; padding: 1px 5px; color: var(--code-c); }
.bubble pre { background: var(--code-bg); border: 1px solid var(--bd); border-radius: var(--r-md); padding: 38px 14px 14px; overflow-x: auto; margin: .75em 0; position: relative; }
.bubble pre code { background: none; border: none; padding: 0; color: var(--t1); font-size: 13px; }
.bubble blockquote { border-left: 3px solid var(--accent); padding: 6px 14px; margin: .5em 0; color: var(--t2); background: var(--accent-glow); border-radius: 0 var(--r-sm) var(--r-sm) 0; }
.bubble a { color: var(--accent-2); text-decoration: none; }
.bubble a:hover { text-decoration: underline; }
.bubble table { border-collapse: collapse; width: 100%; margin: .5em 0; }
.bubble th,.bubble td { border: 1px solid var(--bd-2); padding: 6px 12px; font-size: 13.5px; }
.bubble th { background: var(--bg-3); font-weight: 500; }
.bubble hr { border: none; border-top: 1px solid var(--bd); margin: .9em 0; }

/* Copy code btn */
.copy-code-btn { position: absolute; top: 8px; right: 8px; padding: 3px 10px; font-size: 11px; background: var(--bg-4); border: 1px solid var(--bd-2); border-radius: 6px; color: var(--t3); cursor: pointer; font-family: var(--font); transition: background 0.15s, color 0.15s; }
.copy-code-btn:hover { background: var(--bg-h); color: var(--t1); }

/* Typing cursor */
.typing-cursor { display: inline-block; width: 2px; height: 1em; background: var(--accent-2); border-radius: 1px; vertical-align: text-bottom; animation: blink .85s step-end infinite; margin-left: 1px; }
@keyframes blink { 50% { opacity: 0; } }

/* Message action buttons (shared style) */
.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: opacity 0.15s; }
.message:hover .msg-actions { opacity: 1; }
.msg-action-btn { display: flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 6px; border: 1px solid var(--bd); background: transparent; color: var(--t3); font-family: var(--font); font-size: 12px; cursor: pointer; transition: background 0.15s, color 0.15s, border-color 0.15s; }
.msg-action-btn i { font-size: 13px; }
.msg-action-btn:hover { background: var(--bg-3); color: var(--t1); border-color: var(--bd-2); }

/* ── Input ── */
.input-wrap { flex-shrink: 0; padding: 0 28px 16px; max-width: 836px; width: 100%; margin: 0 auto; }
.input-box { background: var(--bg-2); border: 1px solid var(--bd-2); border-radius: var(--r-xl); padding: 12px 14px 12px 16px; display: flex; align-items: flex-end; gap: 10px; transition: border-color .2s, box-shadow .2s; }
.input-box:focus-within { border-color: rgba(124,106,247,.5); box-shadow: 0 0 0 3px var(--accent-glow); }
.input-box textarea { flex: 1; background: none; border: none; outline: none; resize: none; font-family: var(--font); font-size: 14.5px; color: var(--t1); line-height: 1.6; max-height: 200px; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--bg-4) transparent; }
.input-box textarea::placeholder { color: var(--t3); }
.input-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.char-count { font-size: 11.5px; color: var(--t3); min-width: 24px; text-align: right; }
.send-btn { width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--accent); color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 16px; transition: background .15s, transform .1s; flex-shrink: 0; }
.send-btn:disabled { background: var(--bg-4); color: var(--t3); cursor: not-allowed; }
.send-btn:not(:disabled):hover { background: var(--accent-2); transform: scale(1.06); }
.send-btn:not(:disabled):active { transform: scale(0.95); }
.send-btn.stop { background: #e24b4a; }
.send-btn.stop:hover { background: #f09595; }
.input-hint { font-size: 11.5px; color: var(--t3); text-align: center; margin-top: 8px; }

/* ── Settings Modal ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(4px); z-index: 200; display: flex; align-items: center; justify-content: center; opacity: 0; pointer-events: none; transition: opacity .22s; }
.modal-overlay.open { opacity: 1; pointer-events: all; }
.modal { background: var(--bg-2); border: 1px solid var(--bd-2); border-radius: var(--r-lg); width: 440px; max-width: calc(100vw - 32px); box-shadow: 0 24px 48px rgba(0,0,0,.35); transform: translateY(12px) scale(.98); transition: transform .22s; }
.modal-overlay.open .modal { transform: translateY(0) scale(1); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 14px; border-bottom: 1px solid var(--bd); }
.modal-header h2 { font-size: 15px; font-weight: 600; color: var(--t1); display: flex; align-items: center; gap: 8px; }
.modal-header h2 i { color: var(--accent); }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 18px; }
.setting-group { display: flex; flex-direction: column; gap: 6px; }
.setting-label { font-size: 12.5px; font-weight: 500; color: var(--t2); letter-spacing: .03em; }
.setting-input { height: 38px; background: var(--bg-3); border: 1px solid var(--bd-2); border-radius: var(--r-sm); color: var(--t1); font-family: var(--font); font-size: 13.5px; padding: 0 12px; width: 100%; outline: none; transition: border-color .15s; }
.setting-input:focus { border-color: var(--accent); }
.setting-hint { font-size: 11.5px; color: var(--t3); }
.input-eye-wrap { position: relative; }
.input-eye-wrap .setting-input { padding-right: 38px; }
.eye-btn { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); background: none; border: none; cursor: pointer; color: var(--t3); font-size: 16px; display: flex; align-items: center; transition: color .15s; }
.eye-btn:hover { color: var(--t1); }

/* Theme switcher */
.theme-switcher { display: flex; gap: 8px; }
.theme-btn { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px; border-radius: var(--r-md); border: 1px solid var(--bd-2); background: var(--bg-3); color: var(--t2); cursor: pointer; font-family: var(--font); font-size: 12px; transition: background .15s, color .15s, border-color .15s; }
.theme-btn i { font-size: 18px; }
.theme-btn:hover { background: var(--bg-h); color: var(--t1); }
.theme-btn.active { border-color: var(--accent); background: var(--accent-glow); color: var(--accent-2); }

.modal-footer { padding: 14px 20px 18px; border-top: 1px solid var(--bd); display: flex; justify-content: flex-end; }
.btn-primary { display: flex; align-items: center; gap: 6px; padding: 8px 18px; background: var(--accent); color: #fff; border: none; border-radius: var(--r-sm); font-family: var(--font); font-size: 13.5px; font-weight: 500; cursor: pointer; transition: background .15s; }
.btn-primary:hover { background: var(--accent-2); }

/* ── Toast ── */
.toast { position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(10px); background: var(--bg-3); border: 1px solid var(--bd-2); border-radius: var(--r-md); padding: 10px 18px; font-size: 13.5px; color: var(--t1); opacity: 0; pointer-events: none; transition: opacity .22s, transform .22s; z-index: 300; white-space: nowrap; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Empty state ── */
.empty-history { font-size: 12.5px; color: var(--t3); text-align: center; padding: 20px 8px; }

/* ── Scrollbars ── */
textarea::-webkit-scrollbar { width: 4px; }
textarea::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 2px; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .sidebar { position: fixed; height: 100vh; z-index: 50; box-shadow: 4px 0 24px rgba(0,0,0,.4); }
  .welcome-cards { grid-template-columns: 1fr; }
  .input-wrap { padding: 0 14px 14px; }
}
