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

:root {
  --bg: #0d0d0d;
  --bg2: #161616;
  --bg3: #1e1e1e;
  --border: #2a2a2a;
  --text: #e8e8e8;
  --text2: #888;
  --accent: #7c6af7;
  --accent2: #5f4fd6;
  --user-bg: #1e1e2e;
  --radius: 12px;
}

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

.layout {
  display: flex;
  height: 100dvh;
  position: relative;
}

/* ── Sidebar ── */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-close-sidebar {
  display: none;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  transition: color 0.15s;
}
.btn-close-sidebar:hover { color: var(--text); }

.btn-new {
  width: 100%;
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-new:hover { background: var(--accent2); }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text2);
  transition: background 0.15s, color 0.15s;
  gap: 8px;
}
.conv-item:hover { background: var(--bg3); color: var(--text); }
.conv-item.active { background: var(--bg3); color: var(--text); }

.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-del {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
  border-radius: 4px;
  transition: opacity 0.15s, color 0.15s;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-del:hover { color: #ff5555; }

/* ── Sidebar overlay (mobile) ── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 98;
  background: rgba(0, 0, 0, 0.5);
}
@media (max-width: 640px) {
  .sidebar-overlay.visible { display: block; }
}

/* ── Sidebar toggle (mobile) ── */
.sidebar-toggle {
  display: none;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
}

/* ── Main ── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

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

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex: 1;
  color: var(--text2);
}

.welcome-icon {
  font-size: 40px;
  color: var(--accent);
}

.welcome h1 {
  font-size: 22px;
  font-weight: 500;
  color: var(--text);
}

/* ── Messages ── */
.msg {
  display: flex;
  gap: 12px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.msg.user { justify-content: flex-end; }

.msg-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.65;
  max-width: 80%;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: var(--user-bg);
  border: 1px solid #2a2a3e;
}

.msg.assistant .msg-bubble {
  background: transparent;
  max-width: 100%;
  width: 100%;
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 2px;
}

/* Markdown */
.msg-bubble p { margin-bottom: 10px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin-bottom: 10px; }
.msg-bubble li { margin-bottom: 4px; }
.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 { margin: 14px 0 8px; font-weight: 600; }
.msg-bubble code:not(pre code) {
  background: var(--bg3);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: 'JetBrains Mono', monospace;
}
.msg-bubble pre {
  background: #0a0a0a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
}
.msg-bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text2);
  margin: 8px 0;
}
.msg-bubble table { border-collapse: collapse; width: 100%; margin: 10px 0; }
.msg-bubble th, .msg-bubble td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.msg-bubble th { background: var(--bg3); }

.cursor { display: inline-block; width: 2px; height: 16px; background: var(--accent); animation: blink 0.8s infinite; vertical-align: text-bottom; margin-left: 2px; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }

.file-msg {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg3);
  border-radius: 8px;
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 6px;
}

/* ── Input area ── */
.input-area {
  padding: 12px 16px 20px;
  background: var(--bg);
}

.input-box {
  max-width: 820px;
  margin: 0 auto;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  transition: border-color 0.2s;
}
.input-box:focus-within { border-color: var(--accent); }

.file-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text2);
}
.file-preview button {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
}

.input-row {
  display: flex;
  align-items: flex-end;
  padding: 10px 12px;
  gap: 8px;
}

.btn-attach {
  color: var(--text2);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  transition: color 0.15s;
  flex-shrink: 0;
  line-height: 1;
  margin-bottom: 4px;
}
.btn-attach:hover { color: var(--text); }

textarea#msgInput {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 15px;
  resize: none;
  outline: none;
  font-family: inherit;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
  padding: 4px 0;
}
textarea#msgInput::placeholder { color: var(--text2); }

.btn-send {
  width: 34px;
  height: 34px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, opacity 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-send:hover { background: var(--accent2); }
.btn-send:disabled { opacity: 0.4; cursor: default; }

.input-hint {
  text-align: center;
  font-size: 12px;
  color: #444;
  margin-top: 8px;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#modelSelect {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  padding: 6px 32px 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  outline: none;
  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 12 12'%3E%3Cpath fill='%23888' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  max-width: 280px;
  width: 100%;
  transition: border-color 0.2s;
}
#modelSelect:focus { border-color: var(--accent); }
#modelSelect option { background: var(--bg3); color: var(--text); }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Mobile */
@media (max-width: 640px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 99;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: flex; }
  .btn-close-sidebar { display: flex; }
  .top-bar { justify-content: flex-start; }
  #modelSelect { max-width: none; flex: 1; }
  .messages { padding: 16px 12px; }
  .input-area { padding: 8px 12px 16px; }
}
