/* ── Reset & base ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1a202c;
  --text-muted: #718096;
  --primary: #2b6cb0;
  --primary-hover: #2c5282;
  --danger: #e53e3e;
  --danger-hover: #c53030;
  --citation-bg: #ebf8ff;
  --citation-border: #bee3f8;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
}

html, body { height: 100%; font-family: system-ui, -apple-system, sans-serif; font-size: 15px; color: var(--text); background: var(--bg); }

/* ── Utilities ────────────────────────────────────── */
.hidden { display: none !important; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border: none; border-radius: var(--radius); font-size: 14px; font-weight: 500; cursor: pointer; transition: background .15s; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #cbd5e0; }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-sm { padding: 5px 12px; font-size: 13px; }
.btn-full { width: 100%; justify-content: center; }
.error-msg { color: var(--danger); background: #fff5f5; border: 1px solid #fed7d7; border-radius: var(--radius); padding: 10px 14px; font-size: 13px; }

/* ── Spinner ──────────────────────────────────────── */
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Login page ───────────────────────────────────── */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-container { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 40px 36px; width: 100%; max-width: 380px; text-align: center; }
.login-logo { color: var(--primary); margin-bottom: 16px; }
.login-container h1 { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
.login-subtitle { color: var(--text-muted); font-size: 13px; margin-bottom: 28px; }
.form-group { text-align: left; margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.form-group input { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 14px; outline: none; transition: border-color .15s; }
.form-group input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(43,108,176,.15); }
#login-error { margin-bottom: 12px; }

/* ── App layout ───────────────────────────────────── */
.app-page { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.app-header { display: flex; align-items: center; justify-content: space-between; padding: 0 20px; height: 56px; background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0; }
.header-brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; color: var(--primary); }

.app-layout { display: flex; flex: 1; overflow: hidden; }

/* ── Documents panel ──────────────────────────────── */
.panel-docs { width: 300px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; }
.panel-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.panel-header h2 { font-size: 14px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }

.upload-progress { padding: 10px 16px; border-bottom: 1px solid var(--border); }
.progress-bar { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin-bottom: 6px; }
.progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width .3s; animation: indeterminate 1.2s linear infinite; }
@keyframes indeterminate { 0% { transform: translateX(-100%); width: 60%; } 100% { transform: translateX(200%); width: 60%; } }
#upload-status { font-size: 12px; color: var(--text-muted); }
#upload-error { margin: 8px 16px; }

.doc-list { flex: 1; overflow-y: auto; list-style: none; padding: 8px 0; }
.doc-empty { padding: 20px 16px; color: var(--text-muted); font-size: 13px; text-align: center; }

.doc-item { display: flex; align-items: flex-start; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--bg); transition: background .1s; }
.doc-item:hover { background: var(--bg); }
.doc-icon { font-size: 18px; flex-shrink: 0; margin-top: 1px; }
.doc-info { flex: 1; min-width: 0; }
.doc-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.doc-delete { flex-shrink: 0; opacity: 0; transition: opacity .15s; background: none; border: none; cursor: pointer; color: var(--danger); padding: 2px 4px; border-radius: 4px; }
.doc-item:hover .doc-delete { opacity: 1; }
.doc-delete:hover { background: #fff5f5; }

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

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

.chat-welcome { text-align: center; color: var(--text-muted); margin: auto; }
.chat-welcome p { margin-bottom: 4px; font-size: 14px; }

/* Message bubbles */
.msg { max-width: 820px; }
.msg-user { align-self: flex-end; }
.msg-user .msg-bubble { background: var(--primary); color: #fff; border-radius: 16px 16px 4px 16px; padding: 12px 16px; font-size: 14px; line-height: 1.5; }
.msg-assistant { align-self: flex-start; width: 100%; }
.msg-assistant .msg-bubble { background: var(--surface); border: 1px solid var(--border); border-radius: 16px 16px 16px 4px; padding: 14px 18px; font-size: 14px; line-height: 1.6; white-space: pre-wrap; }

/* Citations */
.msg-citations { margin-top: 10px; }
.citations-title { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px; }
.citation-item { background: var(--citation-bg); border: 1px solid var(--citation-border); border-radius: var(--radius); padding: 8px 12px; margin-bottom: 6px; font-size: 12px; }
.citation-source { font-weight: 600; color: var(--primary); margin-bottom: 3px; }
.citation-fragment { color: var(--text-muted); line-height: 1.4; }

/* Thinking indicator */
.msg-thinking .msg-bubble { color: var(--text-muted); font-style: italic; }

/* ── Chat input ───────────────────────────────────── */
.chat-input-area { display: flex; align-items: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
.chat-input { flex: 1; resize: none; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; font-size: 14px; font-family: inherit; line-height: 1.5; outline: none; transition: border-color .15s; }
.chat-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(43,108,176,.15); }
#query-btn { padding: 10px 14px; align-self: flex-end; }

/* ── Groups ───────────────────────────────────────── */
.group-list { list-style: none; padding: 4px 0; border-bottom: 2px solid var(--border); max-height: 180px; overflow-y: auto; }
.group-item { display: flex; align-items: center; gap: 6px; padding: 8px 16px; cursor: pointer; transition: background .1s; font-size: 13px; }
.group-item:hover { background: var(--bg); }
.group-item.active { background: #ebf4ff; color: var(--primary); font-weight: 500; }
.group-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.group-count { flex-shrink: 0; background: var(--border); color: var(--text-muted); border-radius: 10px; padding: 1px 7px; font-size: 11px; }
.group-delete { flex-shrink: 0; opacity: 0; background: none; border: none; cursor: pointer; color: var(--danger); padding: 2px 4px; border-radius: 4px; transition: opacity .15s; font-size: 12px; }
.group-item:hover .group-delete { opacity: 1; }
.group-delete:hover { background: #fff5f5; }

.new-group-form { padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--bg); }
.new-group-form input { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; font-family: inherit; outline: none; margin-bottom: 8px; }
.new-group-form input:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(43,108,176,.15); }
.new-group-actions { display: flex; gap: 6px; }
.new-group-form .error-msg { margin-top: 8px; margin-bottom: 0; }

.panel-section-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.panel-section-header h2 { font-size: 14px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }

.group-indicator { padding: 6px 20px; background: #ebf4ff; border-bottom: 1px solid var(--citation-border); font-size: 12px; color: var(--primary); flex-shrink: 0; }
.group-indicator span { font-weight: 600; }
