:root {
  --bg: #090b10;
  --surface: #11141c;
  --surface-2: #181c27;
  --surface-3: #1f2433;
  --border: #2a3144;
  --text: #eef1f6;
  --muted: #8b93a7;
  --accent: #5b7cfa;
  --accent-hover: #4a6ae8;
  --accent-soft: rgba(91, 124, 250, 0.12);
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --user-bg: #1c2440;
  --agent-bg: #141824;
  --radius: 12px;
  --sessions-width: 280px;
}

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

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.sm { font-size: 0.8rem; }

/* ── Main layout: sessions + chat only ── */
.app {
  display: grid;
  grid-template-columns: var(--sessions-width) 1fr;
  height: 100vh;
}

/* ── Sessions sidebar ── */
.sessions-panel {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sessions-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem 1rem 1rem;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  color: white;
  flex-shrink: 0;
}

.brand-icon.sm {
  width: 32px;
  height: 32px;
  font-size: 0.75rem;
}

.sessions-brand h1 {
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.2;
}

.new-chat-btn {
  margin: 0 0.75rem 0.75rem;
}

.sessions-label {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding: 0 1rem 0.4rem;
}

.sessions-list {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  padding: 0 0.5rem;
}

.sessions-empty {
  padding: 0.75rem;
  text-align: center;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.15rem;
}

.session-btn {
  flex: 1;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.82rem;
  transition: all 0.15s;
}

.session-btn:hover {
  background: var(--surface-2);
}

.session-btn.active {
  background: var(--accent-soft);
  border-color: rgba(91, 124, 250, 0.3);
}

.session-btn .title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

.session-btn .meta {
  display: block;
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 0.1rem;
}

.session-delete {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0.25rem 0.35rem;
  border-radius: 4px;
  font-size: 1rem;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s;
}

.session-item:hover .session-delete { opacity: 1; }
.session-delete:hover { color: var(--danger); background: rgba(239, 68, 68, 0.1); }

.sessions-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}

.session-tokens {
  margin-bottom: 0.5rem;
  text-align: center;
}

.settings-trigger {
  justify-content: center;
}

/* ── Chat panel ── */
.chat-panel {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--bg);
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-header h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.1rem;
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--accent-soft);
}

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

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 2rem;
  max-width: 520px;
}

.empty-icon {
  margin-bottom: 0.75rem;
  opacity: 0.4;
  display: flex;
  justify-content: center;
}

.empty-state p {
  font-weight: 500;
  color: var(--text);
  font-size: 1.05rem;
  margin-bottom: 1rem;
}

.empty-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.suggestion-chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  font-size: 0.78rem;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.suggestion-chip:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.message {
  max-width: 75%;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 0.9rem;
  white-space: pre-wrap;
  word-break: break-word;
  animation: fadeIn 0.2s ease;
}

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

.message.user {
  align-self: flex-end;
  background: var(--user-bg);
  border: 1px solid rgba(91, 124, 250, 0.2);
  border-bottom-right-radius: 4px;
}

.message.agent {
  align-self: flex-start;
  background: var(--agent-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message .role {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.message .events {
  margin-top: 0.6rem;
  font-size: 0.72rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 0.5rem;
}

.message .tokens {
  margin-top: 0.45rem;
  font-size: 0.65rem;
  color: var(--muted);
}

.message .rag-badge {
  display: inline-block;
  font-size: 0.6rem;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(91, 124, 250, 0.15);
  color: var(--accent);
  margin-left: 0.35rem;
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 0.65rem;
  padding: 0.85rem 1.5rem 0.25rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.chat-composer textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  padding: 0.7rem 1rem;
  line-height: 1.5;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

.chat-composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.send-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 12px;
  flex-shrink: 0;
}

.composer-hint {
  text-align: center;
  padding: 0.25rem 1.5rem 0.65rem;
  background: var(--surface);
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 1.5rem 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.typing-indicator .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s infinite;
}

.typing-indicator .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  font-family: inherit;
  transition: all 0.15s;
}

.btn.primary { background: var(--accent); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover:not(:disabled) { border-color: var(--accent); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn.ghost:hover:not(:disabled) { color: var(--text); }
.btn.sm { padding: 0.35rem 0.65rem; font-size: 0.75rem; }
.btn.full { width: 100%; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
}

/* ── Settings overlay ── */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.settings-shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  width: min(960px, 100%);
  height: min(680px, 92vh);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}

.settings-nav {
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 0.5rem;
}

.settings-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding-bottom: 0.75rem;
  margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.settings-brand strong {
  display: block;
  font-size: 0.85rem;
}

.settings-menu {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1;
}

.settings-nav-btn {
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  transition: all 0.15s;
}

.settings-nav-btn:hover {
  color: var(--text);
  background: var(--surface-3);
}

.settings-nav-btn.active {
  color: var(--text);
  background: var(--accent-soft);
}

.settings-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.settings-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}

.settings-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
}

.settings-panel { display: none; }
.settings-panel.active { display: block; }

.settings-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

.settings-block {
  margin-bottom: 1.75rem;
}

.settings-block h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
}

.settings-block h4 {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

.block-desc { margin-bottom: 0.75rem; }

/* Stat cards */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.stat-card.loading { color: var(--muted); font-size: 0.85rem; }

.stat-label {
  display: block;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.stat-value {
  font-size: 0.95rem;
  font-weight: 600;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
}

.status-dot.ok { background: var(--success); }
.status-dot.warn { background: var(--warn); }

.info-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem;
}

.info-card h3 {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.info-card p {
  font-size: 0.85rem;
  line-height: 1.6;
}

.info-card strong { color: var(--text); }

/* Presets */
.preset-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.preset-cards, .embed-cards {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.preset-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: var(--text);
  transition: all 0.15s;
}

.preset-card:hover, .preset-card.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.preset-card .title {
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.preset-card .desc {
  font-size: 0.7rem;
  color: var(--muted);
  line-height: 1.4;
}

.preset-card .tier {
  font-size: 0.6rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 0.3rem;
}

.tier-free { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.tier-paid { background: rgba(91, 124, 250, 0.15); color: var(--accent); }

.embed-item {
  font-size: 0.75rem;
  padding: 0.55rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  margin-bottom: 0.4rem;
}

.embed-item strong {
  color: var(--text);
  display: block;
  font-size: 0.78rem;
  margin-bottom: 0.1rem;
}

/* Form fields */
.settings-form-grid {
  display: grid;
  gap: 0.85rem;
}

.field-group label {
  display: block;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.35rem;
}

.field-group input,
.field-group select,
.field-group textarea {
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  font-family: inherit;
}

.field-group input:focus,
.field-group select:focus,
.field-group textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.field-group .help {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 0.25rem;
  line-height: 1.4;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.toggle-row input { width: auto; }

/* Products grid */
.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.75rem;
}

.product-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.product-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.product-header strong {
  font-size: 0.88rem;
}

.product-stack {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
}

.product-services {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin: 0.5rem 0;
}

.service-tag {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: ui-monospace, monospace;
}

.product-path {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 0.5rem;
  word-break: break-all;
}

/* Messages */
.msg { font-size: 0.78rem; }
.msg.ok { color: var(--success); }
.msg.err { color: var(--danger); }

.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

/* Banner & Auth */
.setup-banner {
  background: rgba(245, 158, 11, 0.1);
  border-bottom: 1px solid rgba(245, 158, 11, 0.3);
  color: var(--warn);
  padding: 0.6rem 1.5rem;
  font-size: 0.85rem;
  text-align: center;
}

.auth-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.auth-box {
  max-width: 400px;
  width: 90%;
  padding: 2rem;
}

.auth-box h2 {
  font-size: 1.1rem;
  margin-bottom: 0.35rem;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.auth-desc {
  font-size: 0.85rem;
  line-height: 1.5;
  margin-bottom: 0.75rem;
}

.auth-box input {
  width: 100%;
  margin: 0 0 1rem;
  padding: 0.55rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sessions-panel { max-height: 38vh; border-right: none; border-bottom: 1px solid var(--border); }
  .message { max-width: 92%; }
  .settings-shell { grid-template-columns: 1fr; height: 95vh; }
  .settings-nav { flex-direction: row; flex-wrap: wrap; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .settings-menu { flex-direction: row; flex-wrap: wrap; }
  .settings-brand { display: none; }
  .stat-cards { grid-template-columns: 1fr; }
  .preset-columns { grid-template-columns: 1fr; }
}
