:root {
  --bg-main: #212121;
  --bg-sidebar: #171717;
  --bg-surface: #2f2f2f;
  --bg-surface-hover: #3a3a3a;
  --bg-user-bubble: #2f2f2f;
  --bg-code: #0d0d0d;
  --border-subtle: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-muted: #8e8e8e;
  --accent: #10a37f;
  --accent-soft: rgba(16, 163, 127, 0.15);
  --danger: #ef4444;
  --send-bg: #ececec;
  --send-fg: #171717;
  --shadow-composer: 0 4px 24px rgba(0, 0, 0, 0.35);
}

[data-theme="light"] {
  --bg-main: #ffffff;
  --bg-sidebar: #f9f9f9;
  --bg-surface: #f4f4f4;
  --bg-surface-hover: #eaeaea;
  --bg-user-bubble: #f4f4f4;
  --bg-code: #f3f4f6;
  --border-subtle: rgba(0, 0, 0, 0.08);
  --border-strong: rgba(0, 0, 0, 0.16);
  --text-primary: #0d0d0d;
  --text-secondary: #525252;
  --text-muted: #737373;
  --accent: #10a37f;
  --accent-soft: rgba(16, 163, 127, 0.12);
  --send-bg: #0d0d0d;
  --send-fg: #ffffff;
  --shadow-composer: 0 4px 20px rgba(0, 0, 0, 0.08);
}

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

html, body {
  height: 100%;
  background: var(--bg-main);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 264px;
  min-width: 264px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  padding: 12px;
  transition: margin-left 0.2s ease, transform 0.2s ease;
  z-index: 20;
}

.sidebar.collapsed {
  margin-left: -264px;
}

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

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.new-chat-btn {
  flex: 1;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  padding: 0 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s ease;
}

.new-chat-btn:hover {
  background: var(--bg-surface);
}

.sidebar-section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 6px 10px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 14px;
  transition: background 0.15s ease, color 0.15s ease;
}

.chat-item:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.chat-item.active {
  background: var(--bg-surface);
  color: var(--text-primary);
  font-weight: 500;
}

.chat-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.chat-delete-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 4px;
  opacity: 0;
  font-size: 13px;
  transition: opacity 0.15s ease, color 0.15s ease;
}

.chat-item:hover .chat-delete-btn,
.chat-item.active .chat-delete-btn {
  opacity: 1;
}

.chat-delete-btn:hover {
  color: var(--danger);
}

.sidebar-footer {
  border-top: 1px solid var(--border-subtle);
  padding-top: 12px;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 4px;
}

.setting-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
}

.setting-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.status-card {
  background: var(--bg-surface);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12px;
}

.status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: var(--text-primary);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}

.status-dot.ready {
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}

.status-dot.error {
  background: var(--danger);
}

.status-meta {
  color: var(--text-muted);
  margin-top: 3px;
  font-size: 11px;
}

.theme-btn {
  width: 100%;
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.15s ease, color 0.15s ease;
}

.theme-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* Main Area */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

.top-bar {
  height: 54px;
  padding: 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-pill {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-title {
  font-size: 16px;
  font-weight: 600;
}

.model-badge {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg-surface);
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
}

.model-badge-btn {
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.model-badge-btn:hover {
  background: var(--bg-surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

/* Config Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-size: 17px;
  font-weight: 600;
}

.modal-subtitle {
  font-size: 12.5px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 13px;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-field label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
}

.form-field select,
.form-field input[type="text"] {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 13.5px;
  font-family: inherit;
  outline: none;
}

.form-field select:focus,
.form-field input[type="text"]:focus {
  border-color: var(--accent);
}

.field-hint {
  font-size: 11.5px;
  color: var(--text-muted);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.primary-btn {
  border: none;
  background: var(--accent);
  color: #ffffff;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.primary-btn:hover {
  opacity: 0.9;
}

.ghost-btn {
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.ghost-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* Chat Stream */
.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 18px 16px;
  display: flex;
  flex-direction: column;
}

.empty-state {
  margin: auto;
  max-width: 720px;
  width: 100%;
  text-align: center;
  padding: 24px 12px;
}

.empty-state.hidden {
  display: none;
}

.empty-logo {
  width: 54px;
  height: 54px;
  border-radius: 16px;
  background: var(--accent-soft);
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

.empty-state h1 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
  letter-spacing: -0.02em;
}

.empty-subtitle {
  color: var(--text-secondary);
  margin-bottom: 28px;
}

.starter-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  text-align: left;
}

.starter-card {
  border: 1px solid var(--border-subtle);
  background: transparent;
  border-radius: 14px;
  padding: 14px 16px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--text-primary);
  text-align: left;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.starter-card:hover {
  background: var(--bg-surface);
  border-color: var(--border-strong);
}

.starter-title {
  font-size: 14px;
  font-weight: 600;
}

.starter-desc {
  font-size: 12.5px;
  color: var(--text-secondary);
}

/* Messages */
.messages-list {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.message-row {
  display: flex;
  gap: 14px;
  width: 100%;
}

.message-row.user {
  justify-content: flex-end;
}

.user-bubble {
  max-width: 78%;
  background: var(--bg-user-bubble);
  color: var(--text-primary);
  padding: 11px 16px;
  border-radius: 20px;
  white-space: pre-wrap;
  word-break: break-word;
}

.assistant-avatar {
  width: 30px;
  height: 30px;
  min-width: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-top: 2px;
}

.assistant-body {
  flex: 1;
  min-width: 0;
}

.assistant-content {
  color: var(--text-primary);
  word-break: break-word;
}

.assistant-content p {
  margin-bottom: 10px;
}

.assistant-content p:last-child {
  margin-bottom: 0;
}

.assistant-content h1,
.assistant-content h2,
.assistant-content h3 {
  margin: 14px 0 8px;
  font-weight: 600;
  line-height: 1.35;
}

.assistant-content h1 { font-size: 1.25em; }
.assistant-content h2 { font-size: 1.12em; }
.assistant-content h3 { font-size: 1.03em; }

.assistant-content ul,
.assistant-content ol {
  margin: 8px 0 12px 22px;
}

.assistant-content li {
  margin-bottom: 4px;
}

.assistant-content code {
  background: var(--bg-surface);
  padding: 2px 5px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9em;
}

.assistant-content pre {
  background: var(--bg-code);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 10px 0;
}

.assistant-content pre code {
  background: transparent;
  padding: 0;
}

.assistant-content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 10px 0;
}

.assistant-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
  font-size: 14px;
}

.assistant-content th,
.assistant-content td {
  border: 1px solid var(--border-strong);
  padding: 7px 10px;
  text-align: left;
}

.assistant-content th {
  background: var(--bg-surface);
  font-weight: 600;
}

.streaming-cursor::after {
  content: "▍";
  display: inline-block;
  margin-left: 2px;
  color: var(--accent);
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.message-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.action-chip {
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: background 0.15s ease, color 0.15s ease;
}

.action-chip:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* Sources Panel */
.sources-panel {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sources-panel.hidden {
  display: none;
}

.source-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12.5px;
}

.source-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}

.source-badge {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  font-size: 11.5px;
  padding: 2px 8px;
  border-radius: 999px;
}

.source-hierarchy {
  color: var(--text-secondary);
  font-size: 11.5px;
}

.source-snippet {
  color: var(--text-secondary);
  line-height: 1.45;
  white-space: pre-wrap;
}

.error-banner {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: #fca5a5;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13.5px;
}

[data-theme="light"] .error-banner {
  color: #b91c1c;
}

/* Composer */
.composer-wrap {
  padding: 8px 18px 16px;
  flex-shrink: 0;
}

.composer-box {
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: 26px;
  padding: 10px 12px 10px 18px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  box-shadow: var(--shadow-composer);
}

.composer-box:focus-within {
  border-color: rgba(255, 255, 255, 0.32);
}

[data-theme="light"] .composer-box:focus-within {
  border-color: rgba(0, 0, 0, 0.3);
}

.composer-box textarea {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  max-height: 180px;
  padding: 4px 0;
}

.composer-box textarea::placeholder {
  color: var(--text-muted);
}

.send-btn {
  width: 34px;
  height: 34px;
  min-width: 34px;
  border-radius: 50%;
  border: none;
  background: var(--send-bg);
  color: var(--send-fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.send-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.send-btn:not(:disabled):hover {
  opacity: 0.88;
}

.stop-btn {
  background: var(--text-primary);
  color: var(--bg-main);
}

.composer-footnote {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 8px;
}

.hidden {
  display: none !important;
}

.mobile-only {
  display: none;
}

@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    margin-left: 0;
  }

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

  .mobile-only {
    display: inline-flex;
  }

  .starter-grid {
    grid-template-columns: 1fr;
  }
}
