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

:root {
  --bg:        #1a1a2e;
  --bg-light:  #232342;
  --bg-hover:  #2c2c50;
  --surface:   #16213e;
  --text:      #e0e0e0;
  --text-dim:  #8888aa;
  --accent:    #4fc3f7;
  --accent-dim:#2a7da8;
  --user-bg:   #2a3a5c;
  --assist-bg: #1e1e38;
  --error:     #ef5350;
  --border:    #333355;
  --radius:    6px;
  --sidebar-w: 220px;
  --header-h:  48px;
  --footer-h:  32px;
  --input-h:   56px;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "Fira Code", "Cascadia Code", "JetBrains Mono", monospace;
}

html, body { height: 100%; }

body {
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

/* ── Login ──────────────────────────────────────────────────── */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-y: auto;
  padding: 2rem 0;
}

.login-box {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 360px;
  max-width: 90vw;
  text-align: center;
}

.login-box h1 {
  font-size: 1.1rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.login-box p {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
}

.login-box input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font-size: 0.9rem;
  font-family: var(--font-mono);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.75rem;
  outline: none;
}

.login-box input:focus {
  border-color: var(--accent);
}

.token-mask {
  -webkit-text-security: disc;
}

.login-sub {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 1.5rem;
}

.login-box button#passkey-btn {
  padding: 0.7rem 1.5rem;
  font-size: 0.85rem;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s;
}

.login-box button#passkey-btn:hover { background: var(--accent); color: var(--bg); }
.login-box button#passkey-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.login-status {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 1rem;
  min-height: 1.2rem;
}

.error {
  color: var(--error);
  font-size: 0.8rem;
  margin-top: 0.75rem;
}

/* ── App Layout ─────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.toggle-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.toggle-label {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.toggle-btn {
  font-size: 0.75rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-light);
  color: var(--accent);
  cursor: pointer;
}

.toggle-btn.toggle-off {
  color: var(--text-dim);
}

.toggle-btn.active {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}

.toggle-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.2rem;
  display: none;
}

.icon-btn {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-light);
  color: var(--text);
  cursor: pointer;
}

.icon-btn:hover { background: var(--bg-hover); }

/* ── Body (sidebar + main) ──────────────────────────────────── */
.body {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ── Sidebar ────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem 0.75rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.sidebar-heading {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 0.6rem;
}

.corpus-list {
  list-style: none;
}

.corpus-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
  border: none;
  border-radius: var(--radius);
  background: none;
  color: var(--text);
  cursor: pointer;
}

.corpus-btn:hover { background: var(--bg-hover); }

.corpus-btn.active {
  background: var(--accent-dim);
  color: #fff;
}

.corpus-meta {
  display: block;
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-top: 0.1rem;
}

.sidebar-divider {
  border-top: 1px solid var(--border);
  margin: 0.75rem 0;
}

.corpus-options { margin-top: auto; }

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

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Empty state */
.messages:empty::after {
  content: "Select a corpus and ask a question.";
  color: var(--text-dim);
  text-align: center;
  margin: auto;
  font-size: 0.9rem;
}

/* Message bubbles */
.msg {
  max-width: 80%;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg-user {
  align-self: flex-end;
  background: var(--user-bg);
  border: 1px solid #3a4a6c;
}

.msg-assistant {
  align-self: flex-start;
  background: var(--assist-bg);
  border: 1px solid var(--border);
}

.msg-error {
  align-self: center;
  background: none;
  color: var(--error);
  font-size: 0.85rem;
  border: 1px solid var(--error);
}

/* Loading indicator */
.msg-loading {
  align-self: flex-start;
  background: var(--assist-bg);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-style: italic;
}

.loading-dots::after {
  content: '';
  animation: dots 1.2s steps(4, end) infinite;
}

@keyframes dots {
  0%   { content: ''; }
  25%  { content: '.'; }
  50%  { content: '..'; }
  75%  { content: '...'; }
  100% { content: ''; }
}

/* ── Markdown-ish content ───────────────────────────────────── */
.msg h1, .msg h2, .msg h3 {
  margin: 0.6rem 0 0.3rem;
  color: var(--accent);
}

.msg h1 { font-size: 1.15rem; }
.msg h2 { font-size: 1.05rem; }
.msg h3 { font-size: 0.95rem; }

.msg p { margin: 0.35rem 0; }

.msg ul, .msg ol {
  margin: 0.35rem 0 0.35rem 1.5rem;
}

.msg strong { color: #fff; }

.msg code {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  background: var(--bg);
  padding: 0.15rem 0.35rem;
  border-radius: 3px;
}

.msg pre {
  background: var(--bg);
  padding: 0.75rem;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 0.5rem 0;
}

.msg pre code {
  background: none;
  padding: 0;
}

/* ── Sources ────────────────────────────────────────────────── */
.sources {
  margin-top: 0.6rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.sources-heading {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}

.source-item {
  font-size: 0.78rem;
  color: var(--text-dim);
  padding: 0.15rem 0;
}

.source-item strong {
  color: var(--text);
}

/* ── Raw Chunks toggle ──────────────────────────────────────── */
.chunks-toggle {
  display: inline-block;
  font-size: 0.72rem;
  color: var(--accent-dim);
  cursor: pointer;
  margin-top: 0.4rem;
  background: none;
  border: none;
  text-decoration: underline;
}

.chunks-container {
  display: none;
  margin-top: 0.4rem;
}

.chunks-container.open { display: block; }

.chunk {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem;
  margin-bottom: 0.4rem;
  font-size: 0.78rem;
  color: var(--text-dim);
  font-family: var(--font-mono);
  white-space: pre-wrap;
}

.chunk-meta {
  font-size: 0.68rem;
  color: var(--accent-dim);
  margin-bottom: 0.3rem;
  font-family: var(--font-sans);
}

/* ── Input Bar ──────────────────────────────────────────────── */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.input-bar textarea {
  flex: 1;
  resize: none;
  padding: 0.55rem 0.75rem;
  font-size: 0.9rem;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  max-height: 150px;
  line-height: 1.4;
}

.input-bar textarea:focus { border-color: var(--accent); }

.input-bar button {
  padding: 0.55rem 1.2rem;
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  flex-shrink: 0;
}

.input-bar button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.input-bar button:hover:not(:disabled) { opacity: 0.9; }

/* ── Footer ─────────────────────────────────────────────────── */
.footer {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  height: var(--footer-h);
  padding: 0 1rem;
  font-size: 0.72rem;
  color: var(--text-dim);
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.footer span::before {
  content: '\2022 ';
}

.footer span:first-child::before {
  content: '';
}

/* ── Mobile ─────────────────────────────────────────────────── */
@media (max-width: 700px) {
  .sidebar-toggle { display: block; }

  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .msg { max-width: 95%; }

  .messages { padding: 1rem; }

  .input-bar { padding: 0.5rem 1rem; }
}

/* ── Modal ─────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal.open {
  display: flex;
}

.modal-content {
  background: #1e1e36;
  border-radius: 8px;
  width: 90%;
  max-width: 520px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  border: 1px solid #333;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid #333;
}

.modal-header h3 { margin: 0; font-size: 1.1rem; }

.modal-close {
  background: none;
  border: none;
  color: #aaa;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0 0.3rem;
}

.modal-body {
  padding: 1rem 1.2rem;
  overflow-y: auto;
  flex: 1;
}

/* ── Drive browser ─────────────────────────────────────────── */
.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  margin-bottom: 0.8rem;
  font-size: 0.85rem;
  color: #aaa;
}

.breadcrumb-item {
  background: none;
  border: none;
  color: #4fc3f7;
  cursor: pointer;
  padding: 0;
  font-size: 0.85rem;
}

.breadcrumb-item:hover { text-decoration: underline; }

.drive-list { min-height: 100px; }

.drive-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid #2a2a4a;
}

.drive-folder-name {
  background: none;
  border: none;
  color: #e0e0e0;
  cursor: pointer;
  text-align: left;
  flex: 1;
  padding: 0.25rem 0;
  font-size: 0.9rem;
}

.drive-folder-name:hover { color: #4fc3f7; }

.drive-folder-name::before {
  content: "📁 ";
}

.drive-select-btn {
  background: #333;
  border: 1px solid #555;
  color: #4fc3f7;
  padding: 0.25rem 0.7rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  flex-shrink: 0;
}

.drive-select-btn:hover { background: #444; }

.drive-empty { color: #888; padding: 1rem 0; text-align: center; }

.drive-select-current {
  width: 100%;
  padding: 0.6rem;
  margin-bottom: 0.8rem;
  background: #2a2a4a;
  border: 1px solid #4fc3f7;
  color: #4fc3f7;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
}

.drive-select-current:hover { background: #333366; }

.drive-add-form {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #333;
}

.drive-add-form label {
  display: block;
  font-size: 0.8rem;
  color: #aaa;
  margin-bottom: 0.3rem;
  margin-top: 0.8rem;
}

.drive-add-form label:first-child { margin-top: 0; }

.drive-add-form input {
  width: 100%;
  padding: 0.5rem;
  background: #16162b;
  border: 1px solid #444;
  border-radius: 4px;
  color: #e0e0e0;
  font-size: 0.9rem;
  box-sizing: border-box;
}

.drive-add-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.primary-btn {
  background: #4fc3f7;
  color: #111;
  border: none;
  padding: 0.5rem 1.2rem;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
}

.primary-btn:hover { background: #39b0e0; }

.secondary-btn {
  background: #333;
  color: #e0e0e0;
  border: 1px solid #555;
  padding: 0.5rem 1.2rem;
  border-radius: 4px;
  cursor: pointer;
}

.drive-progress {
  padding: 1.5rem 0;
  text-align: center;
}

/* ── Sidebar actions ───────────────────────────────────────── */
.sidebar-actions { padding: 0.5rem 1rem; }

.sidebar-action-btn {
  background: none;
  border: 1px dashed #555;
  color: #4fc3f7;
  width: 100%;
  padding: 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

.sidebar-action-btn:hover { border-color: #4fc3f7; }

/* ── Context menu ──────────────────────────────────────────── */
.context-menu {
  position: fixed;
  background: #1e1e36;
  border: 1px solid #444;
  border-radius: 6px;
  padding: 0.3rem 0;
  z-index: 2000;
  min-width: 120px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.context-menu button {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: #e0e0e0;
  padding: 0.5rem 1rem;
  text-align: left;
  cursor: pointer;
  font-size: 0.85rem;
}

.context-menu button:hover { background: #2a2a4a; }
.context-menu button.danger { color: #ef5350; }
.context-menu button.danger:hover { background: #3a1a1a; }
