:root {
  --brand-primary: #6b2fb3;
  --brand-primary-dark: #4c1d95;
  --brand-primary-soft: #f3ecff;
  --brand-bg: #ffffff;
  --brand-text: #1f1235;
  --brand-muted: #6b6480;
  --brand-border: #e6dff3;
  --brand-error: #c92a2a;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-md: 0 10px 30px rgba(76, 29, 149, 0.15);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PT Sans", Roboto, Helvetica, Arial, sans-serif;
  color: var(--brand-text);
  background: var(--brand-bg);
}

/* ---------- Login page ---------- */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 20% 20%, #8a4ad9 0%, #5b21b6 40%, #321766 100%);
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: #fff;
  padding: 32px 28px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.brand {
  text-align: center;
  margin-bottom: 24px;
}

.brand-logo img {
  width: 56px;
  height: 56px;
}

.brand-name {
  font-size: 28px;
  font-weight: 700;
  margin-top: 12px;
  color: var(--brand-primary-dark);
}

.brand-name span {
  color: var(--brand-primary);
}

.brand-sub {
  font-size: 14px;
  color: var(--brand-muted);
  margin-top: 4px;
}

.alert {
  background: #fff0f0;
  color: var(--brand-error);
  padding: 10px 12px;
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  font-size: 14px;
  text-align: center;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-form label {
  display: grid;
  gap: 6px;
}

.login-form span {
  font-size: 13px;
  color: var(--brand-muted);
}

.login-form input {
  border: 1px solid var(--brand-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.login-form input:focus {
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px rgba(107, 47, 179, 0.18);
}

.btn-primary {
  margin-top: 6px;
  background: var(--brand-primary);
  color: #fff;
  border: 0;
  padding: 12px 18px;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  transition: background-color 0.15s;
}

.btn-primary:hover { background: var(--brand-primary-dark); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

/* ---------- Chat page ---------- */
.chat-shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  position: relative;
}

.chat-sidebar {
  background: linear-gradient(180deg, #4c1d95 0%, #321766 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  min-height: 0;
}

.sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 30;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--brand-border);
  background: #fff;
  cursor: pointer;
  padding: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  box-shadow: 0 2px 8px rgba(76, 29, 149, 0.12);
}

.sidebar-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--brand-primary-dark);
  border-radius: 1px;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(31, 18, 53, 0.45);
  z-index: 19;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 4px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-brand img { width: 32px; height: 32px; }
.sidebar-brand strong { font-size: 18px; }

.sidebar-new {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px dashed rgba(255, 255, 255, 0.45);
  color: #fff;
  border-radius: var(--radius-md);
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
}

.sidebar-new:hover { background: rgba(255, 255, 255, 0.22); }

.sidebar-list {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-right: 4px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.25) transparent;
}

.sidebar-list::-webkit-scrollbar { width: 6px; }
.sidebar-list::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.22);
  border-radius: 3px;
}

.sidebar-list .chat-item {
  display: block;
  width: 100%;
  background: transparent;
  color: #efe7ff;
  border: 0;
  text-align: left;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  font-size: 14px;
  line-height: 1.3;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background-color 0.12s;
}

.sidebar-list .chat-item:hover { background: rgba(255, 255, 255, 0.10); }
.sidebar-list .chat-item.active { background: rgba(255, 255, 255, 0.22); color: #fff; }

.sidebar-empty {
  color: #c8b9eb;
  font-size: 13px;
  padding: 10px 12px;
  text-align: center;
  opacity: 0.8;
}

.sidebar-loader {
  color: #c8b9eb;
  font-size: 12px;
  text-align: center;
  padding: 8px;
}

.sidebar-sentinel {
  height: 1px;
  width: 100%;
  flex-shrink: 0;
  pointer-events: none;
}

.sidebar-footer {
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 13px;
  color: #d6c8f4;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-footer a {
  color: #fff;
  text-decoration: none;
  background: rgba(255, 255, 255, 0.12);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
}

.sidebar-footer a:hover { background: rgba(255, 255, 255, 0.22); }

.chat-main {
  display: grid;
  grid-template-rows: 1fr auto;
  background: #faf7ff;
}

.chat-messages {
  overflow-y: auto;
  padding: 24px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

.chat-empty {
  margin-top: 12vh;
  text-align: center;
  color: var(--brand-muted);
}

.chat-empty h2 {
  color: var(--brand-primary-dark);
  margin: 0 0 6px;
}

.message {
  width: 100%;
  max-width: 760px;
  display: flex;
  gap: 12px;
}

.message .avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
}

.message.user .avatar { background: #5b21b6; }
.message.assistant .avatar { background: #8a4ad9; }

.message .bubble {
  background: #fff;
  border: 1px solid var(--brand-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  flex: 1;
}

/* Markdown elements rendered inside an assistant bubble. */
.message .bubble p { margin: 0 0 8px; }
.message .bubble p:last-child { margin-bottom: 0; }
.message .bubble strong { font-weight: 700; }
.message .bubble em { font-style: italic; }
.message .bubble code {
  background: #f3efff;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.92em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.message .bubble ul,
.message .bubble ol {
  margin: 6px 0 8px;
  padding-left: 22px;
}
.message .bubble ul li,
.message .bubble ol li { margin: 2px 0; }
.message .bubble h1,
.message .bubble h2,
.message .bubble h3 {
  margin: 10px 0 6px;
  line-height: 1.25;
}
.message .bubble h1 { font-size: 1.15em; }
.message .bubble h2 { font-size: 1.08em; }
.message .bubble h3 { font-size: 1em; }
.message .bubble a {
  color: var(--brand-primary-dark);
  text-decoration: underline;
}
.message .bubble a:hover { opacity: 0.85; }
.message .bubble img {
  display: block;
  /* Фиксированная «коробка» под превью товара, чтобы верстка не прыгала
     при появлении <img> во время стриминга. Картинка вписывается в этот
     прямоугольник с сохранением пропорций (object-fit: contain), а
     фон не даёт визуального «провала» пока изображение грузится. */
  width: 260px;
  max-width: 100%;
  height: 200px;
  margin: 6px 0 8px;
  border-radius: var(--radius-sm, 8px);
  border: 1px solid var(--brand-border);
  background: #f8f5ff;
  object-fit: contain;
}
.message .bubble table {
  border-collapse: collapse;
  margin: 8px 0;
}
.message .bubble th,
.message .bubble td {
  border: 1px solid var(--brand-border);
  padding: 4px 8px;
  text-align: left;
  font-size: 0.95em;
}
/* Preserve whitespace only when the bubble still contains raw text (user msgs). */
.message.user .bubble { white-space: pre-wrap; }

.message.user .bubble {
  background: var(--brand-primary-soft);
  border-color: #d8caf5;
}

.message .meta {
  font-size: 11px;
  color: var(--brand-muted);
  margin-top: 6px;
}

.intent-pill {
  display: inline-block;
  background: var(--brand-primary-soft);
  color: var(--brand-primary-dark);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  margin-bottom: 6px;
}

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

/* Thinking dots shown before the first streamed token arrives. */
.bubble.thinking::after { content: none; }
.bubble.thinking {
  display: flex;
  align-items: center;
  gap: 5px;
  min-height: 22px;
  padding: 14px 16px;
  width: max-content;
  max-width: 100%;
  clear: both;
}
.bubble.thinking .typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--brand-primary);
  opacity: 0.3;
  animation: typingDot 1.2s ease-in-out infinite;
}
.bubble.thinking .typing-dot:nth-child(2) { animation-delay: 0.15s; }
.bubble.thinking .typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
  to { opacity: 0; }
}

@keyframes typingDot {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

.chat-input {
  border-top: 1px solid var(--brand-border);
  background: #fff;
  padding: 16px;
  display: flex;
  justify-content: center;
}

.chat-input form {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 760px;
  align-items: flex-end;
}

.chat-input textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--brand-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: 15px;
  font-family: inherit;
  min-height: 48px;
  max-height: 200px;
  outline: none;
}

.chat-input textarea:focus {
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px rgba(107, 47, 179, 0.18);
}

.chat-input button {
  background: var(--brand-primary);
  color: #fff;
  border: 0;
  padding: 0 18px;
  border-radius: var(--radius-md);
  font-weight: 600;
  height: 48px;
  cursor: pointer;
  transition: background-color 0.15s;
}

.chat-input button:hover { background: var(--brand-primary-dark); }
.chat-input button:disabled { opacity: 0.6; cursor: not-allowed; }

.connection-status {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f43f5e;
  margin-right: 6px;
  vertical-align: middle;
}

.connection-status.online { background: #34d399; }

@media (max-width: 768px) {
  .chat-shell {
    grid-template-columns: 1fr;
  }
  .sidebar-toggle { display: flex; }
  .chat-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 280px;
    max-width: 85vw;
    z-index: 20;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: 4px 0 24px rgba(31, 18, 53, 0.35);
  }
  .chat-shell.sidebar-open .chat-sidebar { transform: translateX(0); }
  .chat-shell.sidebar-open .sidebar-overlay { display: block; }
  .chat-main { padding-top: 56px; }
  .chat-input { padding: 12px; }
}
