*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --text: #1e293b;
  --muted: #64748b;
  --danger: #ef4444;
  --radius: 8px;
}
body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
header {
  background: #0f172a;
  color: white;
  padding: 0 20px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
header h1 { font-size: 17px; font-weight: 600; flex: 1; }
.badge {
  background: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
}
.lang-btn {
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.2);
  color: white;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}
.lang-btn:hover { background: rgba(255,255,255,0.22); }
.menu-btn {
  display: none;
  background: none; border: none; color: white;
  font-size: 20px; cursor: pointer; padding: 4px 6px; line-height: 1;
}
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.45); z-index: 40;
}
.sidebar-overlay.show { display: block; }
.privacy-note { font-size: 11px; line-height: 1.45; color: var(--muted); margin-top: 8px; }
.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-section { padding: 16px; border-bottom: 1px solid var(--border); }
.sidebar-section h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 12px;
}
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 20px 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
}
.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--primary);
  background: #eef2ff;
}
.upload-zone input[type=file] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.upload-zone .icon { font-size: 26px; margin-bottom: 6px; }
.upload-zone p { font-size: 12px; color: var(--muted); line-height: 1.5; }
.upload-zone strong { color: var(--primary); }
.uploading { display: none; font-size: 12px; color: var(--primary); text-align: center; margin-top: 8px; }
.uploading.show { display: block; }
.docs-wrapper {
  flex: 1; overflow: hidden; display: flex;
  flex-direction: column; padding: 16px;
}
.docs-wrapper h2 {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--muted); margin-bottom: 10px; flex-shrink: 0;
}
.docs-list { flex: 1; overflow-y: auto; }
.doc-item {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border-radius: 6px;
  font-size: 13px; transition: background 0.1s;
}
.doc-item:hover { background: var(--bg); }
.doc-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; font-size: 12px;
}
.doc-delete {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 13px; padding: 2px 5px;
  border-radius: 4px; opacity: 0; transition: all 0.1s; flex-shrink: 0;
}
.doc-item:hover .doc-delete { opacity: 1; }
.doc-delete:hover { background: #fee2e2; color: var(--danger); }
.empty-docs {
  padding: 20px 8px; text-align: center;
  color: var(--muted); font-size: 12px; line-height: 1.6;
}

/* ── Chat ── */
.chat-area { display: flex; flex-direction: column; overflow: hidden; }
.messages {
  flex: 1; overflow-y: auto; padding: 28px 32px;
  display: flex; flex-direction: column; gap: 22px;
}
.welcome {
  text-align: center; color: var(--muted);
  padding: 80px 20px; max-width: 420px; margin: 0 auto;
}
.welcome .icon { font-size: 44px; margin-bottom: 14px; }
.welcome h2 { font-size: 20px; color: var(--text); margin-bottom: 8px; font-weight: 600; }
.welcome p { font-size: 14px; line-height: 1.7; }
.message { display: flex; gap: 10px; }
.message.user { align-self: flex-end; flex-direction: row-reverse; max-width: 75%; }
.message.assistant { max-width: 85%; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; flex-shrink: 0; margin-top: 2px;
}
.message.assistant .avatar { background: var(--primary); color: white; }
.message.user .avatar { background: #dde1e9; }
.bubble {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 11px 15px;
  font-size: 14px; line-height: 1.65;
}
.copy-btn {
  display: block; margin-top: 6px; margin-left: auto;
  background: none; border: 1px solid var(--border);
  border-radius: 5px; padding: 3px 10px; font-size: 11px;
  cursor: pointer; color: var(--muted); font-family: inherit;
  opacity: 0; transition: opacity 0.15s, background 0.15s;
}
.message.assistant:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--bg); color: var(--text); }
.copy-btn.ok { color: #16a34a; border-color: #16a34a; }
.message.user .bubble {
  background: var(--primary); border-color: var(--primary);
  color: white; border-radius: 12px 4px 12px 12px;
}
.message.assistant .bubble { border-radius: 4px 12px 12px 12px; }
.sources {
  margin-top: 9px; display: flex;
  gap: 6px; flex-wrap: wrap; align-items: center;
}
.sources-label { font-size: 11px; color: var(--muted); }
.source-chip {
  background: #eef2ff; color: var(--primary-dark);
  font-size: 11px; padding: 2px 8px;
  border-radius: 10px; font-weight: 500;
}
.chunks-toggle {
  margin-top: 7px; background: none; border: none;
  color: var(--muted); font-size: 12px;
  cursor: pointer; padding: 0; text-decoration: underline; display: block;
}
.chunks-toggle:hover { color: var(--text); }
.chunks-list { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.chunk-item {
  background: var(--bg); border-left: 3px solid #c7d2fe;
  padding: 8px 10px; font-size: 12px; color: var(--muted);
  border-radius: 0 4px 4px 0; line-height: 1.55;
}
.chunk-source { font-weight: 600; color: var(--primary); margin-bottom: 3px; font-size: 11px; }
.msg-body { flex: 1; min-width: 0; }
.bubble.md p { margin: 0 0 8px; }
.bubble.md p:last-child { margin-bottom: 0; }
.bubble.md ul, .bubble.md ol { padding-left: 20px; margin: 6px 0; }
.bubble.md li { margin: 3px 0; }
.bubble.md h1, .bubble.md h2, .bubble.md h3 { font-size: 15px; font-weight: 700; margin: 10px 0 4px; }
.bubble.md code { background: #f1f5f9; border-radius: 3px; padding: 1px 5px; font-size: 12px; font-family: monospace; }
.bubble.md pre { background: #f1f5f9; border-radius: 6px; padding: 10px 12px; overflow-x: auto; margin: 8px 0; }
.bubble.md pre code { background: none; padding: 0; }
.bubble.md strong { font-weight: 600; }
.bubble.md blockquote { border-left: 3px solid var(--border); margin: 6px 0; padding-left: 10px; color: var(--muted); }
.typing { display: flex; gap: 4px; padding: 3px 0; align-items: center; }
.typing span {
  width: 6px; height: 6px; background: #cbd5e1;
  border-radius: 50%; animation: bounce 1.1s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}
.input-area {
  padding: 14px 24px; border-top: 1px solid var(--border);
  background: var(--surface); display: flex;
  gap: 10px; align-items: flex-end;
}
.input-area textarea {
  flex: 1; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; font-size: 14px; font-family: inherit;
  resize: none; outline: none; min-height: 44px; max-height: 120px;
  line-height: 1.5; transition: border-color 0.15s;
}
.input-area textarea:focus { border-color: var(--primary); }
.input-area textarea::placeholder { color: #94a3b8; }
.send-btn {
  background: var(--primary); color: white; border: none;
  border-radius: var(--radius); width: 44px; height: 44px;
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; font-size: 16px;
  transition: background 0.15s; flex-shrink: 0;
}
.send-btn:hover:not(:disabled) { background: var(--primary-dark); }
.send-btn:disabled { background: var(--border); cursor: not-allowed; }
.new-chat-btn {
  background: none; border: 1px solid var(--border); color: var(--muted);
  border-radius: var(--radius); padding: 0 14px; height: 44px;
  cursor: pointer; font-size: 13px; font-family: inherit;
  white-space: nowrap; transition: all 0.15s; flex-shrink: 0;
}
.new-chat-btn:hover { border-color: var(--primary); color: var(--primary); }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1e293b; color: white; padding: 9px 16px;
  border-radius: 8px; font-size: 13px;
  opacity: 0; transition: opacity 0.2s;
  z-index: 100; pointer-events: none; white-space: nowrap;
}
.toast.show { opacity: 1; }

@media (max-width: 640px) {
  html { width: 100%; overflow-x: hidden; }
  body { width: 100%; height: 100dvh; overflow-x: hidden; }

  header { padding: 0 10px; gap: 6px; width: 100%; box-sizing: border-box; }
  header h1 { font-size: 14px; }
  .badge { display: none; }
  .menu-btn { display: block; }

  .layout { grid-template-columns: 1fr; width: 100%; }
  .chat-area { grid-column: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; }

  .sidebar {
    position: fixed; top: 56px; left: 0; bottom: 0;
    width: 280px; z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  .sidebar.open { transform: translateX(0); }

  .messages { width: 100%; box-sizing: border-box; padding: 14px 12px; gap: 16px; }
  .message.user { max-width: 88%; }
  .message.assistant { display: block; width: 100%; max-width: none; }
  .message.assistant .avatar { display: none; }
  .message.assistant .msg-body,
  .message.assistant > div { width: 100%; }
  .message.assistant .bubble { width: 100%; box-sizing: border-box; border-radius: 12px; }

  .input-area { width: 100%; box-sizing: border-box; padding: 10px 12px; gap: 8px; }
  .new-chat-btn { padding: 0 10px; font-size: 12px; }
}
