:root {
  --bg: #f5f5f5;
  --surface: rgba(255, 255, 255, 0.9);
  --surface-strong: #ffffff;
  --surface-soft: rgba(200, 30, 30, 0.06);
  --text: #1a1a1a;
  --muted: #666666;
  --accent: #d32f2f;
  --accent-strong: #b71c1c;
  --line: rgba(0, 0, 0, 0.1);
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
  --sidebar-bg: #1a1d23;
  --sidebar-surface: rgba(255, 255, 255, 0.06);
  --sidebar-surface-hover: rgba(255, 255, 255, 0.10);
  --sidebar-surface-active: rgba(255, 255, 255, 0.14);
  --sidebar-text: rgba(255, 255, 255, 0.92);
  --sidebar-text-muted: rgba(255, 255, 255, 0.55);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(211, 47, 47, 0.05), transparent 30%),
    radial-gradient(circle at bottom right, rgba(211, 47, 47, 0.04), transparent 25%),
    linear-gradient(135deg, #fafafa 0%, #f5f5f5 50%, #f0f0f0 100%);
}

button,
input,
textarea {
  font: inherit;
}

.shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: 100vh;
  transition: grid-template-columns 0.3s ease;
}

.shell.sidebar-collapsed {
  grid-template-columns: 0px minmax(0, 1fr);
}

.sidebar-toggle {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 1100;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease, transform 0.15s ease, left 0.3s ease;
  box-shadow: none;
}

.sidebar-toggle:hover {
  background: rgba(255, 255, 255, 0.14);
  transform: scale(1.05);
}

.hamburger-icon {
  position: relative;
  width: 22px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
  transition: background 0.3s ease;
}

.hamburger-icon::before,
.hamburger-icon::after {
  content: "";
  position: absolute;
  left: 0;
  width: 22px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
  transition: transform 0.3s ease;
}

.hamburger-icon::before {
  top: -7px;
}

.hamburger-icon::after {
  top: 7px;
}

/* When sidebar is collapsed, show accent-colored toggle */
.shell.sidebar-collapsed .sidebar-toggle {
  background: var(--accent);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.shell.sidebar-collapsed .sidebar-toggle:hover {
  background: var(--accent-strong);
}

/* Animate to X when sidebar is open */
.shell:not(.sidebar-collapsed) .hamburger-icon {
  background: transparent;
}

.shell:not(.sidebar-collapsed) .hamburger-icon::before {
  transform: translateY(7px) rotate(45deg);
}

.shell:not(.sidebar-collapsed) .hamburger-icon::after {
  transform: translateY(-7px) rotate(-45deg);
}

.sidebar {
  display: flex;
  flex-direction: column;
  padding: 28px 24px;
  padding-top: 72px;
  padding-bottom: 56px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  overflow-y: auto;
  overflow-x: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  gap: 0;
}

.shell.sidebar-collapsed .sidebar {
  opacity: 0;
  visibility: hidden;
}

.eyebrow {
  margin: 0 0 10px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--sidebar-text-muted);
}

.sidebar .eyebrow {
  color: var(--sidebar-text-muted);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
}

.sidebar-brand .brand-logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, #e53935, #c62828);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.1rem;
  color: #fff;
  flex-shrink: 0;
}

.sidebar h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.1;
  font-weight: 700;
  color: var(--sidebar-text);
}

.panel-header h2 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3rem);
  line-height: 0.95;
}

.sidebar-copy {
  max-width: 28ch;
  color: var(--sidebar-text-muted);
  font-size: 0.85rem;
  line-height: 1.5;
  margin-top: 6px;
  margin-bottom: 0;
}

.sidebar-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.08);
  margin: 20px 0;
  border: none;
}

.nav {
  display: grid;
  gap: 6px;
  margin: 28px 0 auto;
}

.nav-link,
.settings-form button,
#send-button {
  border: 0;
  border-radius: 14px;
  padding: 13px 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 150ms ease, background-color 200ms ease, color 150ms ease;
}

.nav-link {
  background: var(--sidebar-surface);
  color: var(--sidebar-text-muted);
  text-align: left;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.92rem;
}

.nav-link .nav-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0.7;
}

.nav-link.active .nav-icon,
.nav-link:hover .nav-icon {
  opacity: 1;
}

.nav-link.active {
  background: var(--sidebar-surface-active);
  color: var(--sidebar-text);
}

.nav-link:hover {
  background: var(--sidebar-surface-hover);
  color: var(--sidebar-text);
}

.settings-form button,
#send-button {
  background: linear-gradient(135deg, #ef5350, #c62828);
  color: white;
}

.nav-link:hover,
.settings-form button:hover,
#send-button:hover {
  transform: translateY(-1px);
}

.status-card {
  padding: 18px;
  border-radius: 16px;
  background: var(--sidebar-surface);
  border: 1px solid rgba(255, 255, 255, 0.06);
  margin-top: 24px;
}

.status-label,
.message-role,
.composer-hint,
.status-hint,
.settings-feedback {
  margin: 0;
  font-size: 0.84rem;
}

.status-label {
  color: var(--sidebar-text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.status-value {
  margin: 6px 0 4px;
  font-weight: 700;
  font-size: 0.95rem;
}

.status-hint {
  color: var(--sidebar-text-muted);
  font-size: 0.8rem;
  line-height: 1.45;
}

.content {
  padding: 28px;
  transition: padding-left 0.3s ease;
}

.shell.sidebar-collapsed .content {
  padding-left: 72px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.panel-header {
  margin-bottom: 22px;
}

.chat-thread,
.settings-form {
  border: 1px solid var(--line);
  border-radius: 28px;
  background: var(--surface);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}

.chat-thread {
  display: grid;
  gap: 14px;
  padding: 24px;
  max-height: 60vh;
  overflow: auto;
}

.message {
  max-width: min(78ch, 100%);
  padding: 16px 18px;
  border-radius: 20px;
  background: var(--surface-strong);
  border: 1px solid var(--line);
}

.message.user {
  margin-left: auto;
  background: #c62828;
  color: #ffffff;
}

.message-role {
  margin-bottom: 8px;
  font-family: "IBM Plex Mono", monospace;
  text-transform: uppercase;
  color: var(--muted);
}

.message.user .message-role {
  color: rgba(255, 255, 255, 0.7);
}

/* Rendered markdown inside assistant messages */
.message-body {
  line-height: 1.6;
}

.message-body p {
  margin: 0 0 0.6em;
}

.message-body p:last-child {
  margin-bottom: 0;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4 {
  margin: 0.8em 0 0.4em;
  line-height: 1.3;
}

.message-body h1 { font-size: 1.3em; }
.message-body h2 { font-size: 1.15em; }
.message-body h3 { font-size: 1.05em; }

.message-body ul,
.message-body ol {
  margin: 0.4em 0;
  padding-left: 1.5em;
}

.message-body li {
  margin-bottom: 0.25em;
}

.message-body code {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.88em;
  background: rgba(0, 0, 0, 0.06);
  padding: 2px 6px;
  border-radius: 4px;
}

.message-body pre {
  margin: 0.6em 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #1a1d23;
  color: #e0e0e0;
  overflow-x: auto;
}

.message-body pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.message-body blockquote {
  margin: 0.6em 0;
  padding: 4px 12px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.message-body table {
  border-collapse: collapse;
  margin: 0.6em 0;
  width: 100%;
}

.message-body th,
.message-body td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
}

.message-body th {
  background: rgba(0, 0, 0, 0.04);
  font-weight: 600;
}

.message.user .message-body code {
  background: rgba(255, 255, 255, 0.15);
}

.message.user .message-body pre {
  background: rgba(0, 0, 0, 0.25);
  color: #ffffff;
}

.composer {
  margin-top: 18px;
  padding: 18px;
  border-radius: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

textarea,
.settings-form input {
  width: 100%;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 18px;
  background: #ffffff;
  padding: 16px 18px;
  color: var(--text);
}

textarea {
  resize: vertical;
  min-height: 120px;
}

.composer-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-top: 14px;
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#clear-chat-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: #1a1a1a;
  color: #ffffff;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.15s ease;
}

#clear-chat-button:hover {
  background: #333333;
  transform: scale(1.05);
}

#clear-chat-button:active {
  transform: scale(0.95);
}

.composer-hint,
.status-hint,
.settings-feedback {
  color: var(--muted);
}

.settings-form {
  display: grid;
  gap: 12px;
  padding: 24px;
  max-width: 680px;
}

.settings-form label {
  font-weight: 700;
}

.settings-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.settings-actions button[type="submit"] {
  flex: 1;
}

.export-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 2px solid var(--line);
  border-radius: 14px;
  padding: 13px 20px;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface-strong);
  color: var(--text);
  transition: transform 150ms ease, background-color 200ms ease, border-color 200ms ease;
  white-space: nowrap;
}

.export-button:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
  color: var(--accent);
  background: var(--surface-soft);
}

.export-button:active {
  transform: translateY(0);
}

.import-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 2px solid var(--line);
  border-radius: 14px;
  padding: 13px 20px;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface-strong);
  color: var(--text);
  transition: transform 150ms ease, background-color 200ms ease, border-color 200ms ease;
  white-space: nowrap;
}

.import-button:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
  color: var(--accent);
  background: var(--surface-soft);
}

.import-button:active {
  transform: translateY(0);
}

.settings-feedback.success {
  color: #21693e;
}

.settings-feedback.error {
  color: #9b2b2b;
}

/* Toggle switch */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.toggle-label {
  font-weight: 700;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 26px;
  transition: background 0.25s ease;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.25s ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.toggle-switch input:checked + .toggle-slider {
  background: linear-gradient(135deg, #ef5350, #c62828);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(22px);
}

/* Calypso fields container */
.calypso-fields {
  display: grid;
  gap: 12px;
}

.calypso-fields.hidden {
  display: none;
}

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

.demo-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 10px 20px;
  background: var(--accent-strong);
  color: #ffffff;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  z-index: 1000;
}

/* ── RAG toggle in composer ─────────────────────────────────────────── */

.composer-hint-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: 1;
}

.rag-toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.rag-toggle-label {
  font-weight: 700;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.rag-doc-count {
  font-size: 0.78rem;
  color: var(--muted);
  font-family: "IBM Plex Mono", monospace;
}

.toggle-switch.small {
  width: 36px;
  height: 20px;
}

.toggle-switch.small .toggle-slider::before {
  width: 14px;
  height: 14px;
}

.toggle-switch.small input:checked + .toggle-slider::before {
  transform: translateX(16px);
}

/* ── Knowledge Base view ───────────────────────────────────────────── */

.kb-container {
  max-width: 680px;
}

.kb-description {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.6;
  margin-bottom: 24px;
}

.kb-upload-form {
  margin-bottom: 28px;
}

.kb-upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 36px 24px;
  border: 2px dashed var(--line);
  border-radius: 20px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.2s ease, background 0.2s ease;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.kb-upload-area:hover,
.kb-upload-area.dragover {
  border-color: var(--accent);
  background: var(--surface-soft);
  color: var(--text);
}

.kb-section-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 12px;
}

.kb-documents {
  display: grid;
  gap: 8px;
}

.kb-empty {
  color: var(--muted);
  font-size: 0.88rem;
  font-style: italic;
}

.kb-doc-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.kb-doc-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.kb-doc-name {
  font-weight: 600;
  font-size: 0.92rem;
}

.kb-doc-meta {
  font-size: 0.78rem;
  color: var(--muted);
  font-family: "IBM Plex Mono", monospace;
}

.kb-doc-remove {
  background: none;
  border: none;
  font-size: 1.4rem;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: color 0.2s ease, background 0.2s ease;
  line-height: 1;
}

.kb-doc-remove:hover {
  color: var(--accent);
  background: var(--surface-soft);
}

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

@media (max-width: 920px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .sidebar,
  .content {
    padding: 20px;
  }

  .composer-footer {
    flex-direction: column;
    align-items: stretch;
  }

  .composer-hint-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  #send-button,
  .settings-form button {
    width: 100%;
  }
}
