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

:root {
  --bg:         #f0f4f8;
  --surface:    #ffffff;
  --border:     #e2e8f0;
  --primary:    #4f46e5;
  --primary-h:  #4338ca;
  --primary-lt: #ede9fe;
  --text:       #1e293b;
  --muted:      #64748b;
  --danger:     #ef4444;
  --cag-bg:     #d1fae5;
  --cag-txt:    #065f46;
  --rag-bg:     #fef3c7;
  --rag-txt:    #92400e;
  --r:          10px;
  --shadow:     0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --panel-w:    380px;
}

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

/* ── Layout ── */
.app  { display: flex; flex-direction: column; height: 100vh; }
.main { display: flex; flex: 1; overflow: hidden; position: relative; }

/* ── Header ── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 10; flex-shrink: 0;
}
.header-left   { display: flex; align-items: center; gap: 10px; }
.logo          { font-size: 26px; }
header h1      { font-size: 18px; font-weight: 700; letter-spacing: -.3px; }
.header-badges { display: flex; gap: 8px; align-items: center; }
.badge         { padding: 3px 11px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-purple  { background: var(--primary-lt); color: var(--primary); }
.badge-blue    { background: #dbeafe; color: #1d4ed8; }
.badge-green   { background: var(--cag-bg); color: var(--cag-txt); }
.badge-amber   { background: var(--rag-bg); color: var(--rag-txt); }

/* ── Panel toggle strip ── */
.panel-toggle {
  width: 16px; flex-shrink: 0;
  background: var(--border); border: none; cursor: pointer;
  color: var(--muted); font-size: 11px; font-weight: 700;
  align-self: stretch; transition: background .15s, color .15s;
  display: flex; align-items: center; justify-content: center;
  z-index: 5;
}
.panel-toggle:hover { background: var(--primary-lt); color: var(--primary); }

/* ── Document Panel ── */
.doc-panel {
  width: var(--panel-w); min-width: var(--panel-w);
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width .25s ease, min-width .25s ease;
}
.doc-panel.collapsed { width: 0; min-width: 0; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: #fafbfc;
  flex-shrink: 0; gap: 8px;
}
.panel-head h2 { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; flex: 1; }
.panel-actions { display: flex; gap: 6px; flex-shrink: 0; }

.btn-secondary {
  background: #f1f5f9; color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 10px; font-size: 12px; font-weight: 500;
  cursor: pointer; transition: .15s;
}
.btn-secondary:hover { background: var(--border); }

.drop-zone {
  margin: 10px 12px 6px;
  border: 2px dashed var(--border);
  border-radius: var(--r);
  padding: 10px 14px;
  text-align: center; font-size: 12px; color: var(--muted);
  cursor: pointer; transition: .2s; flex-shrink: 0;
}
.drop-zone:hover, .drop-zone.drag-over { border-color: var(--primary); background: var(--primary-lt); color: var(--primary); }

.doc-list { flex: 1; overflow-y: auto; padding: 6px 8px; }
.doc-list::-webkit-scrollbar { width: 4px; }
.doc-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.empty { text-align: center; color: var(--muted); font-size: 13px; padding: 50px 20px; }

/* ── Folder sections ── */
.folder-section { margin-bottom: 6px; }

.folder-header {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px; border-radius: 8px;
  cursor: pointer; user-select: none;
  transition: background .15s;
}
.folder-header:hover { background: #f8fafc; }

.folder-toggle { font-size: 11px; color: var(--muted); width: 12px; flex-shrink: 0; transition: transform .2s; }
.folder-section.collapsed .folder-toggle { transform: rotate(-90deg); }

.folder-icon { font-size: 16px; flex-shrink: 0; }
.folder-name { font-size: 13px; font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.folder-count { font-size: 11px; color: var(--muted); flex-shrink: 0; }

.folder-chk { accent-color: var(--primary); width: 14px; height: 14px; cursor: pointer; flex-shrink: 0; }
.folder-del-btn {
  background: none; border: none; color: #cbd5e1; font-size: 13px;
  cursor: pointer; padding: 2px 4px; border-radius: 4px;
  opacity: 0; transition: .15s; flex-shrink: 0;
}
.folder-header:hover .folder-del-btn { opacity: 1; }
.folder-del-btn:hover { background: #fee2e2; color: var(--danger); }

.folder-docs { padding-left: 10px; }
.folder-section.collapsed .folder-docs { display: none; }

/* ── Doc Cards ── */
.doc-card {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 8px 10px; border-radius: var(--r);
  border: 1.5px solid transparent; cursor: pointer;
  transition: .15s; margin-bottom: 4px; position: relative;
}
.doc-card:hover               { background: #f8fafc; border-color: var(--border); }
.doc-card.selected            { background: var(--primary-lt); border-color: var(--primary); }
.doc-card .chk                { margin-top: 2px; accent-color: var(--primary); width: 14px; height: 14px; flex-shrink: 0; cursor: pointer; }
.doc-icon                     { font-size: 20px; flex-shrink: 0; }
.doc-body                     { flex: 1; min-width: 0; }
.doc-name                     { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 4px; }
.doc-tags                     { display: flex; flex-wrap: wrap; gap: 3px; }
.tag { padding: 1px 6px; border-radius: 20px; font-size: 10px; font-weight: 500; }
.tag-type  { background: #dbeafe; color: #1d4ed8; }
.tag-grey  { background: #f1f5f9; color: var(--muted); }
.tag-cag   { background: var(--cag-bg); color: var(--cag-txt); }
.tag-rag   { background: var(--rag-bg); color: var(--rag-txt); }
.doc-date  { font-size: 10px; color: var(--muted); margin-top: 3px; }

.doc-actions { display: flex; gap: 2px; flex-shrink: 0; }
.btn-move, .btn-del {
  background: none; border: none; font-size: 14px;
  cursor: pointer; padding: 2px 5px; border-radius: 4px;
  opacity: 0; transition: .15s;
}
.doc-card:hover .btn-move,
.doc-card:hover .btn-del { opacity: 1; }
.btn-move { color: #94a3b8; }
.btn-move:hover { background: #f1f5f9; color: var(--primary); }
.btn-del  { color: #cbd5e1; }
.btn-del:hover  { background: #fee2e2; color: var(--danger); }

/* ── Folder picker dropdown ── */
.folder-picker {
  position: fixed; z-index: 100;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  min-width: 160px; overflow: hidden;
}
.folder-picker-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; font-size: 13px; cursor: pointer;
  transition: background .1s;
}
.folder-picker-item:hover { background: var(--primary-lt); }
.folder-picker-item.unassign { color: var(--muted); font-style: italic; }
.folder-picker-sep { height: 1px; background: var(--border); margin: 2px 0; }

/* ── Selection Summary ── */
.sel-summary {
  border-top: 1px solid var(--border);
  padding: 12px 14px; background: #fafbfc; flex-shrink: 0;
}
.sel-row  { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 13px; font-weight: 500; }
.sel-chars { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.bar       { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 3px; }
.bar-fill  { height: 100%; background: var(--primary); border-radius: 2px; transition: width .3s; }
.bar-fill.over { background: var(--danger); }
.btn-clear { background: none; border: none; color: var(--muted); font-size: 11px; cursor: pointer; padding: 3px 6px; border-radius: 4px; }
.btn-clear:hover { background: var(--border); }

/* ── Buttons ── */
.btn-primary {
  background: var(--primary); color: #fff;
  border: none; border-radius: 8px;
  padding: 6px 13px; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: .15s;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-h); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

/* ── New folder modal ── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.modal {
  background: var(--surface); border-radius: 14px;
  padding: 24px; width: 320px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.modal-title { font-size: 16px; font-weight: 700; margin-bottom: 14px; }
.modal-input {
  width: 100%; border: 1.5px solid var(--border); border-radius: 8px;
  padding: 9px 12px; font-size: 14px; outline: none; font-family: inherit;
  transition: border-color .15s; margin-bottom: 14px;
}
.modal-input:focus { border-color: var(--primary); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ── Chat Panel ── */
.chat-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.chat-header {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  min-height: 46px;
}
.ctx-label  { font-size: 12px; font-weight: 600; color: var(--muted); }
.ctx-doc    { font-size: 11px; padding: 2px 8px; background: var(--primary-lt); color: var(--primary); border-radius: 12px; max-width: 160px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ctx-empty  { font-size: 13px; color: var(--muted); font-style: italic; }

.model-bar {
  display: flex; align-items: center;
  padding: 7px 16px;
  background: #fafbfc; border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}

/* ── Messages ── */
.messages {
  flex: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 14px;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.welcome { text-align: center; margin: auto; color: var(--muted); }
.welcome-icon { font-size: 52px; margin-bottom: 12px; }
.welcome h3   { font-size: 16px; margin-bottom: 6px; color: var(--text); }
.welcome p    { font-size: 13px; }

.msg      { display: flex; flex-direction: column; max-width: 82%; }
.msg-user { align-self: flex-end; }
.msg-ai   { align-self: flex-start; }

.msg-meta   { margin-bottom: 4px; display: flex; gap: 6px; align-items: center; }
.msg-method { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 12px; }
.msg-method.cag { background: var(--cag-bg); color: var(--cag-txt); }
.msg-method.rag { background: var(--rag-bg); color: var(--rag-txt); }

.bubble {
  padding: 12px 16px; border-radius: 16px;
  font-size: 14px; line-height: 1.7;
}
.msg-user .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg-ai   .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

/* ── Markdown inside AI bubbles ── */
.bubble h1, .bubble h2, .bubble h3 {
  margin: 12px 0 6px;
  line-height: 1.3;
  color: var(--text);
}
.bubble h1 { font-size: 17px; font-weight: 700; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.bubble h2 { font-size: 15px; font-weight: 700; }
.bubble h3 { font-size: 14px; font-weight: 600; }
.bubble h1:first-child, .bubble h2:first-child, .bubble h3:first-child { margin-top: 0; }
.bubble p  { margin: 4px 0; }
.bubble p:first-child { margin-top: 0; }
.bubble strong { font-weight: 700; }
.bubble em     { font-style: italic; }
.bubble code   { background: #f1f5f9; border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-family: 'Fira Code', 'Consolas', monospace; font-size: 12.5px; }
.bubble pre    { background: #f1f5f9; border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; margin: 8px 0; overflow-x: auto; }
.bubble pre code { background: none; border: none; padding: 0; }
.bubble ul, .bubble ol { margin: 6px 0 6px 18px; }
.bubble li { margin: 3px 0; }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
.bubble br { display: block; content: ''; margin: 2px 0; }

/* typing */
.typing .bubble { display: flex; gap: 5px; align-items: center; }
.typing .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite; }
.typing .dot:nth-child(2) { animation-delay: .2s; }
.typing .dot:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-5px)} }

/* Rating */
.rating-row   { display: flex; align-items: center; gap: 4px; margin-top: 6px; }
.rating-label { font-size: 11px; color: var(--muted); }
.star         { background: none; border: none; font-size: 17px; cursor: pointer; color: #d1d5db; padding: 0 1px; transition: color .1s; }
.star.lit, .star:hover { color: #f59e0b; }

/* ── Input ── */
.input-area { padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.input-row  { display: flex; gap: 8px; align-items: flex-end; }

textarea.prompt {
  flex: 1; border: 1.5px solid var(--border); border-radius: var(--r);
  padding: 10px 13px; font-size: 14px; font-family: inherit;
  resize: none; outline: none; transition: .15s; background: #f8fafc; line-height: 1.5;
}
textarea.prompt:focus  { border-color: var(--primary); background: #fff; }
textarea.prompt:disabled { opacity: .5; cursor: not-allowed; }

.btn-send {
  background: var(--primary); color: #fff;
  border: none; border-radius: var(--r);
  padding: 10px 18px; font-size: 18px; cursor: pointer; transition: .15s; flex-shrink: 0;
}
.btn-send:hover:not(:disabled) { background: var(--primary-h); }
.btn-send:disabled { opacity: .5; cursor: not-allowed; }

.input-hint { display: flex; justify-content: space-between; margin-top: 6px; font-size: 11px; color: var(--muted); }
.session-id { font-family: monospace; opacity: .6; }

/* ── Toast ── */
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: #1e293b; color: #fff;
  padding: 10px 18px; border-radius: 8px;
  font-size: 13px; font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  z-index: 999; opacity: 0; transform: translateY(10px);
  transition: .25s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }

.uploading-badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--primary-lt); color: var(--primary);
  padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600;
  animation: pulse 1.5s infinite;
}
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.6} }
