/* Visual system
   Dark by default; light overrides via prefers-color-scheme: light.
   No frameworks, no build step. */

:root {
  color-scheme: dark;

  --bg: #0b0d12;
  --bg-elevated: #11141b;
  --bg-elevated-2: #171b24;
  --border: #232838;
  --border-soft: #1a1e29;
  --text: #e8eaf0;
  --text-muted: #9aa1b2;
  --text-faint: #656d80;

  --accent: #6d8bff;
  --accent-strong: #8ea1ff;
  --accent-ink: #0b0d12;
  --accent-wash: rgba(109, 139, 255, 0.14);

  --amber: #e8a23a;
  --amber-wash: rgba(232, 162, 58, 0.14);
  --amber-ink: #3a2705;

  --danger: #f2665a;
  --danger-wash: rgba(242, 102, 90, 0.14);

  --success: #4fd18b;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.35);

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Consolas, monospace;

  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;

    --bg: #f6f7fb;
    --bg-elevated: #ffffff;
    --bg-elevated-2: #f0f1f6;
    --border: #e2e5ee;
    --border-soft: #ebedf3;
    --text: #1a1d29;
    --text-muted: #5b6072;
    --text-faint: #8a90a2;

    --accent: #3f5fe0;
    --accent-strong: #2e4bcc;
    --accent-ink: #ffffff;
    --accent-wash: rgba(63, 95, 224, 0.08);

    --amber: #b6790f;
    --amber-wash: rgba(182, 121, 15, 0.1);
    --amber-ink: #3a2705;

    --danger: #cc4034;
    --danger-wash: rgba(204, 64, 52, 0.08);

    --success: #1f9d5c;

    --shadow-1: 0 1px 2px rgba(30, 35, 60, 0.06);
    --shadow-2: 0 8px 24px rgba(30, 35, 60, 0.1);
  }
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -40px;
  left: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  z-index: 100;
  transition: top 0.15s var(--ease);
}
.skip-link:focus {
  top: 8px;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button {
  font-family: inherit;
}

/* ── Header ─────────────────────────────────────────────────────── */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-name {
  font-weight: 650;
  font-size: 17px;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.brand-tagline {
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

.badge-button {
  cursor: pointer;
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
}
.badge-button:hover {
  border-color: var(--accent);
  background: var(--accent-wash);
  color: var(--text);
}

.badge-ghost {
  color: var(--accent-strong);
  border-color: var(--border);
}

.is-hidden {
  display: none !important;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
  flex: 0 0 auto;
  transition: background 0.2s var(--ease);
}
.dot.ok { background: var(--success); box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 20%, transparent); }
.dot.down { background: var(--danger); box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 20%, transparent); }

/* ── Layout ─────────────────────────────────────────────────────── */

.app-main {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  min-height: 0;
}

.chat-column {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
}

.chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ── Empty state / example chips ───────────────────────────────── */

.empty-state {
  margin: auto;
  max-width: 520px;
  text-align: center;
  padding: 32px 16px;
}

.empty-state h1 {
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin: 0 0 10px;
}

.empty-state p {
  color: var(--text-muted);
  margin: 0 0 22px;
}

.example-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.chip {
  background: var(--bg-elevated-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s var(--ease), transform 0.1s var(--ease), background 0.15s var(--ease);
}
.chip:hover {
  border-color: var(--accent);
  background: var(--accent-wash);
}
.chip:active {
  transform: scale(0.98);
}

/* ── Chat messages ──────────────────────────────────────────────── */

.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 92%;
  animation: rise 0.22s var(--ease);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-user {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-assistant {
  align-self: flex-start;
  align-items: flex-start;
}

.bubble {
  border-radius: var(--radius-lg);
  padding: 12px 16px;
  font-size: 14.5px;
  line-height: 1.6;
  word-wrap: break-word;
}

.msg-user .bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 4px;
}

.msg-assistant .bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg-assistant.is-refused .bubble {
  background: var(--amber-wash);
  border-color: color-mix(in srgb, var(--amber) 45%, var(--border));
}

.msg-assistant.is-error .bubble {
  background: var(--danger-wash);
  border-color: color-mix(in srgb, var(--danger) 45%, var(--border));
}

.stream-error-note {
  margin-top: 8px;
  color: var(--danger);
  font-size: 12.5px;
  font-weight: 600;
}

.bubble p {
  margin: 0 0 10px;
}
.bubble p:last-child {
  margin-bottom: 0;
}
.bubble ul, .bubble ol {
  margin: 0 0 10px;
  padding-left: 20px;
}
.bubble strong {
  font-weight: 650;
}

.refusal-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--amber);
  margin-bottom: 6px;
}
.refusal-tag::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--amber);
}

.citation-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 5px;
  background: var(--accent-wash);
  color: var(--accent-strong);
  font-size: 10.5px;
  font-weight: 700;
  vertical-align: super;
  line-height: 16px;
  cursor: pointer;
  border: none;
  font-family: var(--font-sans);
  transition: background 0.15s var(--ease), transform 0.1s var(--ease);
}
.citation-chip:hover {
  background: var(--accent);
  color: var(--accent-ink);
}
.citation-chip:active {
  transform: scale(0.92);
}

.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 13px;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: -2px;
  animation: blink 0.9s step-end infinite;
}
@keyframes blink {
  50% { opacity: 0; }
}

.guardrail-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.guardrail-tag {
  font-size: 10.5px;
  color: var(--text-faint);
  background: var(--bg-elevated-2);
  border: 1px solid var(--border-soft);
  padding: 2px 8px;
  border-radius: 999px;
}

.msg-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: 11.5px;
  color: var(--text-faint);
  font-family: var(--font-mono);
  padding: 0 4px;
}
.msg-meta span {
  white-space: nowrap;
}

/* ── Chat form ──────────────────────────────────────────────────── */

.chat-form {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
}

.chat-form textarea {
  flex: 1 1 auto;
  resize: none;
  max-height: 160px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 11px 14px;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.4;
  transition: border-color 0.15s var(--ease);
}
.chat-form textarea:focus {
  border-color: var(--accent);
}
.chat-form textarea:disabled {
  opacity: 0.6;
}

.send-button {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--accent);
  color: var(--accent-ink);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s var(--ease), transform 0.1s var(--ease), opacity 0.15s var(--ease);
}
.send-button:hover {
  background: var(--accent-strong);
}
.send-button:active {
  transform: scale(0.95);
}
.send-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ── Sources panel ──────────────────────────────────────────────── */

.sources-toggle {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-elevated);
  border: none;
  border-top: 1px solid var(--border);
  color: var(--text);
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
}

.sources-toggle-count {
  background: var(--accent-wash);
  color: var(--accent-strong);
  border-radius: 999px;
  padding: 1px 9px;
  font-size: 12px;
  font-weight: 700;
}

.sources-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-elevated);
}

.sources-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  flex: 0 0 auto;
}

.sources-panel-header h2 {
  font-size: 14px;
  font-weight: 650;
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.link-button {
  background: none;
  border: none;
  color: var(--accent-strong);
  font-size: 12.5px;
  cursor: pointer;
  padding: 4px 2px;
  font-weight: 600;
}
.link-button:hover {
  text-decoration: underline;
}

.sources-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sources-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 30px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.sources-empty svg {
  color: var(--text-faint);
}
.sources-empty p {
  font-size: 12.5px;
  margin: 0;
  line-height: 1.5;
}

.source-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  scroll-margin-top: 12px;
  transition: border-color 0.4s var(--ease), background 0.4s var(--ease);
}

.source-card.is-highlighted {
  border-color: var(--accent);
  background: var(--accent-wash);
}

.source-card-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
}

.source-n {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background: var(--accent-wash);
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.source-title {
  font-size: 13px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.source-meta {
  font-size: 11px;
  color: var(--text-faint);
  margin-bottom: 8px;
  font-family: var(--font-mono);
}

.score-bar-track {
  height: 4px;
  border-radius: 2px;
  background: var(--border-soft);
  margin-bottom: 8px;
  overflow: hidden;
}

.score-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.4s var(--ease);
}

.source-snippet {
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ── Dialog (upload / manage corpus) ──────────────────────────────*/

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 13, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}

.dialog {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  width: 100%;
  max-width: 460px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 20px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.dialog-header h2 {
  font-size: 16px;
  margin: 0;
}

.icon-button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
}
.icon-button:hover {
  background: var(--bg-elevated-2);
  color: var(--text);
}

.dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 26px 16px;
  text-align: center;
  cursor: pointer;
  color: var(--text-muted);
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.dropzone svg {
  color: var(--text-faint);
}
.dropzone p {
  margin: 0;
  font-size: 13px;
}
.dropzone strong {
  color: var(--text);
}
.dropzone:hover, .dropzone.is-dragover {
  border-color: var(--accent);
  background: var(--accent-wash);
}

.upload-progress {
  margin-top: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.upload-progress-bar {
  flex: 1 1 auto;
  height: 6px;
  border-radius: 3px;
  background: var(--border-soft);
  overflow: hidden;
}
.upload-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s linear;
}
#upload-progress-label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.dialog-subheading {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 20px 0 10px;
}

.documents-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.documents-empty {
  color: var(--text-muted);
  font-size: 13px;
  margin: 0;
}

.document-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
}

.document-row-info {
  flex: 1 1 auto;
  min-width: 0;
}
.document-row-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.document-row-meta {
  color: var(--text-faint);
  font-size: 11px;
  font-family: var(--font-mono);
}

.document-delete {
  flex: 0 0 auto;
  background: none;
  border: 1px solid var(--border);
  color: var(--danger);
  border-radius: var(--radius-sm);
  padding: 5px 9px;
  font-size: 11.5px;
  cursor: pointer;
}
.document-delete:hover {
  background: var(--danger-wash);
}
.document-delete:disabled {
  opacity: 0.5;
  cursor: wait;
}

/* ── Toasts ────────────────────────────────────────────────────── */

.toast-region {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  max-width: 340px;
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-md);
  padding: 11px 14px;
  font-size: 13px;
  box-shadow: var(--shadow-2);
  animation: rise 0.2s var(--ease);
}
.toast.toast-success { border-left-color: var(--success); }
.toast.toast-error { border-left-color: var(--danger); }
.toast.toast-warning { border-left-color: var(--amber); }

/* ── Responsive ───────────────────────────────────────────────── */

@media (max-width: 860px) {
  .app-main {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }

  .chat-column {
    border-right: none;
  }

  .sources-toggle {
    display: flex;
  }

  .sources-panel {
    position: fixed;
    inset: auto 0 0 0;
    top: 30%;
    border-top: 1px solid var(--border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-2);
    transform: translateY(100%);
    transition: transform 0.25s var(--ease);
    z-index: 40;
  }

  .sources-panel.is-open {
    transform: translateY(0);
  }
}

@media (max-width: 520px) {
  .brand-tagline {
    display: none;
  }
  .app-header {
    padding: 12px 14px;
  }
  .chat-log {
    padding: 16px;
  }
  .chat-form {
    padding: 10px 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* ── Sign-in overlay ──────────────────────────────────────────────*/

.login-backdrop {
  z-index: 60; /* above the upload dialog, so an expired session always wins */
  background: rgba(6, 8, 13, 0.75);
  backdrop-filter: blur(4px);
}

.login-dialog {
  max-width: 360px;
  padding: 28px 26px;
  display: flex;
  flex-direction: column;
}

.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  margin-bottom: 18px;
}
.login-brand .brand-mark {
  width: 40px;
  height: 40px;
}
.login-brand h2 {
  margin: 0;
  font-size: 18px;
}

.login-hint {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.login-label {
  font-size: 12.5px;
  color: var(--text-muted);
  margin: 10px 0 5px;
}

.login-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  transition: border-color 0.15s var(--ease);
}
.login-input:focus {
  border-color: var(--accent);
  outline: none;
}

.login-error {
  margin: 12px 0 0;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--danger-wash);
  color: var(--danger);
  font-size: 13px;
}

.login-submit {
  margin-top: 16px;
  padding: 11px 14px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--accent-ink);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s var(--ease), opacity 0.15s var(--ease);
}
.login-submit:hover {
  background: var(--accent-strong);
}
.login-submit:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
