/**
 * DeepSeek AI Chat — Unified Design System
 * Inherits from Chat Deep AI Theme (--cd-*) variables
 * Version: 3.0.0
 * License: GPL v2 or later
 */

/* ── 1. Fallback tokens (when theme vars are absent) ──────────────── */
:root {
  --ds-primary:     var(--cd-primary, #4F46E5);
  --ds-secondary:   var(--cd-secondary, #7C3AED);
  --ds-accent:      var(--cd-accent, #06B6D4);
  --ds-bg:          var(--cd-light-bg, #fff);
  --ds-bg-alt:      var(--cd-surface, #F8FAFC);
  --ds-text:        var(--cd-text, #1E293B);
  --ds-text-muted:  var(--cd-text-muted, #64748B);
  --ds-border:      var(--cd-border, #E2E8F0);
  --ds-radius:      var(--cd-radius, 8px);
  --ds-radius-lg:   var(--cd-radius-lg, 12px);
  --ds-font:        var(--cd-font, 'Segoe UI', system-ui, -apple-system, sans-serif);
  --ds-mono:        var(--cd-font-mono, 'SF Mono', 'Cascadia Code', 'Consolas', monospace);
  --ds-transition:  var(--cd-transition, 200ms ease);
  --ds-shadow:      var(--cd-shadow, 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06));
  --ds-shadow-lg:   var(--cd-shadow-lg, 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05));
  --ds-gradient:    linear-gradient(135deg, var(--ds-primary) 0%, var(--ds-secondary) 100%);
}

/* ── 2. Scoped reset ──────────────────────────────────────────────── */
.ds-chat *,
.ds-chat *::before,
.ds-chat *::after { box-sizing: border-box; margin: 0; padding: 0; }

.ds-chat {
  font-family: var(--ds-font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ds-text);
  -webkit-font-smoothing: antialiased;
}

/* ── 3. Container ─────────────────────────────────────────────────── */
.ds-chat {
  position: relative;
  width: 90% !important;
  max-width: 1160px !important;
  margin: 0 auto;
}

.ds-chat__shell {
  display: flex;
  flex-direction: column;
  background: var(--ds-bg);
  border-radius: var(--ds-radius-lg);
  border: 1px solid var(--ds-border);
  box-shadow: var(--ds-shadow-lg);
  overflow: hidden;
  height: 620px;
  transition: height var(--ds-transition), border-radius var(--ds-transition);
  position: relative;
}

/* ── 4. Header ────────────────────────────────────────────────────── */
.ds-chat__header {
  flex: 0 0 auto;
  background: var(--ds-primary);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: relative;
  z-index: 2;
}

.ds-chat__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.ds-chat__logo {
  width: 36px; height: 36px;
  background: rgba(255,255,255,.15);
  border-radius: var(--ds-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  backdrop-filter: blur(8px);
}

.ds-chat__logo svg { width: 20px; height: 20px; color: #fff; }

.ds-chat__title {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Controls ── */
.ds-chat__controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Model selector pills */
.ds-chat__models {
  display: flex;
  background: rgba(255,255,255,.12);
  border-radius: 999px;
  padding: 3px;
  border: 1px solid rgba(255,255,255,.15);
}

.ds-chat__model-btn {
  padding: 5px 12px;
  background: transparent;
  border: none;
  border-radius: 999px;
  color: rgba(255,255,255,.85);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 150ms ease;
  display: flex;
  align-items: center;
  gap: 5px;
  font-family: var(--ds-font);
  white-space: nowrap;
}

.ds-chat__model-btn svg { width: 14px; height: 14px; }

.ds-chat__model-btn:hover { color: #fff; background: rgba(255,255,255,.1); }

.ds-chat__model-btn[aria-pressed="true"] {
  background: #fff;
  color: var(--ds-primary);
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}

/* Icon buttons */
.ds-chat__icon-btn {
  width: 34px; height: 34px;
  border-radius: 999px;
  background: rgba(255,255,255,.1);
  border: 1px solid rgba(255,255,255,.15);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 150ms ease;
  flex-shrink: 0;
}

.ds-chat__icon-btn svg { width: 16px; height: 16px; pointer-events: none; }
.ds-chat__icon-btn:hover { background: rgba(255,255,255,.2); }
.ds-chat__icon-btn[aria-pressed="true"] { background: #fff; color: var(--ds-primary); }

/* ── 5. Conversation area ─────────────────────────────────────────── */
.ds-chat__conversation {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  background: var(--ds-bg-alt);
  -webkit-overflow-scrolling: touch;
}

.ds-chat__conversation::-webkit-scrollbar { width: 5px; }
.ds-chat__conversation::-webkit-scrollbar-track { background: transparent; }
.ds-chat__conversation::-webkit-scrollbar-thumb { background: var(--ds-border); border-radius: 999px; }

/* Messages */
.ds-chat__msg {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  animation: ds-msgIn 250ms ease-out;
}

@keyframes ds-msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.ds-chat__msg--user { flex-direction: row-reverse; }

/* Avatar */
.ds-chat__avatar {
  width: 32px; height: 32px;
  border-radius: var(--ds-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-weight: 600;
  font-size: 12px;
}

.ds-chat__msg--ai .ds-chat__avatar {
  background: var(--ds-gradient);
  color: #fff;
}

.ds-chat__msg--user .ds-chat__avatar {
  background: var(--ds-border);
  color: var(--ds-text-muted);
}

/* Bubble */
.ds-chat__bubble {
  max-width: 75%;
  min-width: 0;
}

.ds-chat__bubble-inner {
  padding: 10px 14px;
  border-radius: var(--ds-radius-lg);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.ds-chat__msg--ai .ds-chat__bubble-inner {
  background: var(--ds-bg);
  border: 1px solid var(--ds-border);
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
  border-bottom-left-radius: 4px;
}

.ds-chat__msg--user .ds-chat__bubble-inner {
  background: var(--ds-gradient);
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* ── Markdown prose ── */
.ds-chat__prose { font-size: 15px; line-height: 1.65; }
.ds-chat__prose p { margin-bottom: 10px; }
.ds-chat__prose p:last-child { margin-bottom: 0; }
.ds-chat__prose strong { font-weight: 700; }
.ds-chat__prose a { color: var(--ds-primary); text-decoration: underline; text-underline-offset: 2px; }
.ds-chat__prose ul, .ds-chat__prose ol { margin: 8px 0; padding-left: 22px; }
.ds-chat__prose li { margin-bottom: 4px; }

.ds-chat__prose code {
  padding: 1px 5px;
  background: var(--ds-bg-alt);
  border-radius: 4px;
  font-family: var(--ds-mono);
  font-size: .88em;
  color: var(--ds-primary);
}

.ds-chat__prose pre {
  margin: 10px 0;
  padding: 14px;
  background: #0F172A;
  border-radius: var(--ds-radius);
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.ds-chat__prose pre code {
  background: none;
  color: #E2E8F0;
  padding: 0;
  font-size: inherit;
}

.ds-chat__prose blockquote {
  border-left: 3px solid var(--ds-primary);
  padding: 8px 14px;
  margin: 10px 0;
  background: rgba(79,70,229,.04);
  border-radius: 0 var(--ds-radius) var(--ds-radius) 0;
  font-style: italic;
  color: var(--ds-text-muted);
}

.ds-chat__prose table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 14px; }
.ds-chat__prose th, .ds-chat__prose td { padding: 7px 10px; text-align: left; border: 1px solid var(--ds-border); }
.ds-chat__prose th { background: var(--ds-bg-alt); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.ds-chat__prose hr { border: none; border-top: 1px solid var(--ds-border); margin: 14px 0; }

/* ── Thinking box ── */
.ds-chat__thinking {
  margin-bottom: 10px;
  border: 1px solid var(--ds-border);
  border-radius: var(--ds-radius);
  overflow: hidden;
  background: var(--ds-bg-alt);
}

.ds-chat__thinking-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 12px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--ds-font);
  font-size: 12px;
  font-weight: 600;
  color: var(--ds-primary);
  transition: background 150ms ease;
}

.ds-chat__thinking-toggle:hover { background: rgba(79,70,229,.04); }
.ds-chat__thinking-toggle svg { width: 12px; height: 12px; transition: transform 200ms ease; }
.ds-chat__thinking[open] .ds-chat__thinking-toggle svg { transform: rotate(180deg); }

.ds-chat__thinking-body {
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ds-text-muted);
  border-top: 1px solid var(--ds-border);
  max-height: 180px;
  overflow-y: auto;
}

/* ── Message actions ── */
.ds-chat__actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--ds-border);
}

.ds-chat__action-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  background: none;
  border: 1px solid var(--ds-border);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  color: var(--ds-text-muted);
  cursor: pointer;
  font-family: var(--ds-font);
  transition: all 150ms ease;
}

.ds-chat__action-btn svg { width: 13px; height: 13px; }
.ds-chat__action-btn:hover { border-color: var(--ds-primary); color: var(--ds-primary); background: rgba(79,70,229,.04); }
.ds-chat__action-btn--ok { border-color: #10B981; color: #10B981; }

/* ── Typing indicator ── */
.ds-chat__typing {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 6px 20px;
}

.ds-chat__typing.active { display: flex; }

.ds-chat__typing .ds-chat__avatar {
  width: 32px; height: 32px;
  background: var(--ds-gradient);
  color: #fff;
  border-radius: var(--ds-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}

.ds-chat__dots {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--ds-bg);
  border: 1px solid var(--ds-border);
  border-radius: var(--ds-radius-lg);
}

.ds-chat__dot {
  width: 7px; height: 7px;
  background: var(--ds-text-muted);
  border-radius: 999px;
  animation: ds-bounce 1.4s ease-in-out infinite;
}

.ds-chat__dot:nth-child(2) { animation-delay: .16s; }
.ds-chat__dot:nth-child(3) { animation-delay: .32s; }

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

/* ── 6. Input area ────────────────────────────────────────────────── */
.ds-chat__input-area {
  flex: 0 0 auto;
  padding: 14px 20px;
  background: var(--ds-bg);
  border-top: 1px solid var(--ds-border);
}

.ds-chat__form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.ds-chat__input-wrap { flex: 1; position: relative; }

.ds-chat__input {
  width: 100%;
  min-height: 48px;
  max-height: 120px;
  padding: 11px 16px;
  background: var(--ds-bg-alt);
  border: 2px solid var(--ds-border);
  border-radius: var(--ds-radius-lg);
  font-family: var(--ds-font);
  font-size: 15px;
  color: var(--ds-text);
  resize: none;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.ds-chat__input::placeholder { color: var(--ds-text-muted); }

.ds-chat__input:focus {
  border-color: var(--ds-primary);
  background: var(--ds-bg);
  box-shadow: 0 0 0 3px rgba(79,70,229,.1);
}

.ds-chat__send {
  width: 48px; height: 48px;
  border-radius: var(--ds-radius-lg);
  background: var(--ds-gradient);
  border: none;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 150ms ease, box-shadow 150ms ease;
  box-shadow: 0 2px 8px rgba(79,70,229,.25);
  flex-shrink: 0;
}

.ds-chat__send svg { width: 18px; height: 18px; }
.ds-chat__send:hover { transform: translateY(-1px); box-shadow: 0 4px 14px rgba(79,70,229,.35); }
.ds-chat__send:active { transform: translateY(0); }
.ds-chat__send:disabled { opacity: .5; cursor: not-allowed; transform: none; }

/* ── 7. Model info (below chat) ───────────────────────────────────── */
.ds-chat__model-info {
  text-align: center;
  margin-top: 12px;
  font-size: 13px;
  color: var(--ds-text-muted);
  line-height: 1.5;
}

.ds-chat__model-info strong { color: var(--ds-text); font-weight: 600; }

/* ── 8. Fullscreen mode ───────────────────────────────────────────── */
.ds-chat--fullscreen {
  position: fixed;
  inset: 0;
  z-index: 99999;
  width: 100% !important;
  max-width: 100% !important;
  height: 100% !important;
  margin: 0;
  background: var(--ds-bg, #fff);
  display: flex;
  flex-direction: column;
}

.ds-chat--fullscreen .ds-chat__shell {
  height: 100% !important;
  flex: 1 1 auto;
  border-radius: 0;
  border: none;
  box-shadow: none;
}

/* ── 9. Dark mode (theme-driven) ──────────────────────────────────── */
[data-theme="dark"] .ds-chat__shell {
  background: var(--ds-bg);
  border-color: var(--ds-border);
}

[data-theme="dark"] .ds-chat__header {
  background: #312E81;
}

[data-theme="dark"] .ds-chat__model-btn[aria-pressed="true"] {
  background: rgba(255,255,255,.15);
  color: #fff;
}

[data-theme="dark"] .ds-chat__icon-btn:hover {
  background: rgba(255,255,255,.2);
}

[data-theme="dark"] .ds-chat__conversation { background: var(--ds-bg); }

[data-theme="dark"] .ds-chat__msg--ai .ds-chat__bubble-inner {
  background: var(--ds-bg-alt);
  border-color: var(--ds-border);
  color: var(--ds-text);
}

[data-theme="dark"] .ds-chat__msg--user .ds-chat__bubble-inner {
  background: linear-gradient(135deg, #6366F1 0%, #7C3AED 100%);
}

[data-theme="dark"] .ds-chat__prose { color: var(--ds-text); }
[data-theme="dark"] .ds-chat__prose h1,
[data-theme="dark"] .ds-chat__prose h2,
[data-theme="dark"] .ds-chat__prose h3,
[data-theme="dark"] .ds-chat__prose h4 { color: #F8FAFC; }
[data-theme="dark"] .ds-chat__prose p { color: #E2E8F0; }
[data-theme="dark"] .ds-chat__prose strong { color: #F8FAFC; }
[data-theme="dark"] .ds-chat__prose a { color: #A5B4FC; }
[data-theme="dark"] .ds-chat__prose code { background: rgba(99,102,241,.12); color: #C7D2FE; }
[data-theme="dark"] .ds-chat__prose pre { background: #020617; border: 1px solid #334155; }
[data-theme="dark"] .ds-chat__prose pre code { color: #E2E8F0; }
[data-theme="dark"] .ds-chat__prose blockquote { border-left-color: #6366F1; background: rgba(99,102,241,.08); color: #94A3B8; }
[data-theme="dark"] .ds-chat__prose th { background: #1E293B; color: #F1F5F9; }
[data-theme="dark"] .ds-chat__prose td { color: #CBD5E1; border-color: #334155; }
[data-theme="dark"] .ds-chat__prose hr { border-color: #334155; }
[data-theme="dark"] .ds-chat__prose li { color: #E2E8F0; }

[data-theme="dark"] .ds-chat__thinking { background: #1E293B; border-color: #334155; }
[data-theme="dark"] .ds-chat__thinking-toggle { color: #A5B4FC; }
[data-theme="dark"] .ds-chat__thinking-body { border-color: #334155; color: #94A3B8; }

[data-theme="dark"] .ds-chat__actions { border-color: #334155; }
[data-theme="dark"] .ds-chat__action-btn { border-color: #334155; color: #94A3B8; }
[data-theme="dark"] .ds-chat__action-btn:hover { border-color: #818CF8; color: #A5B4FC; background: rgba(99,102,241,.08); }

[data-theme="dark"] .ds-chat__input-area { background: var(--ds-bg-alt); border-color: var(--ds-border); }
[data-theme="dark"] .ds-chat__input { background: var(--ds-bg); border-color: var(--ds-border); color: var(--ds-text); }
[data-theme="dark"] .ds-chat__input::placeholder { color: #64748B; }
[data-theme="dark"] .ds-chat__input:focus { border-color: #818CF8; box-shadow: 0 0 0 3px rgba(99,102,241,.2); }

[data-theme="dark"] .ds-chat__send { background: linear-gradient(135deg, #6366F1, #7C3AED); box-shadow: 0 2px 8px rgba(99,102,241,.3); }
[data-theme="dark"] .ds-chat__send:hover { box-shadow: 0 4px 14px rgba(99,102,241,.4); }

[data-theme="dark"] .ds-chat__dots { background: #1E293B; border-color: #334155; }
[data-theme="dark"] .ds-chat__dot { background: #64748B; }

[data-theme="dark"] .ds-chat__typing .ds-chat__avatar {
  background: linear-gradient(135deg, #6366F1, #7C3AED);
}

[data-theme="dark"] .ds-chat__model-info { color: #94A3B8; }
[data-theme="dark"] .ds-chat__model-info strong { color: #E2E8F0; }

[data-theme="dark"] .ds-chat__conversation::-webkit-scrollbar-thumb { background: #475569; }

[data-theme="dark"] .ds-chat__msg--user .ds-chat__avatar { background: #334155; color: #94A3B8; }
[data-theme="dark"] .ds-chat__msg--ai .ds-chat__avatar { background: linear-gradient(135deg, #6366F1, #7C3AED); }

/* ── 10. Responsive ───────────────────────────────────────────────── */
@media (max-width: 768px) {
  .ds-chat { width: 96% !important; max-width: none !important; }
  .ds-chat__shell { height: 560px; }
  .ds-chat__header { padding: 10px 14px; gap: 8px; }
  .ds-chat__title { font-size: 14px; }
  .ds-chat__conversation { padding: 14px; }
  .ds-chat__bubble { max-width: 85%; }
  .ds-chat__input-area { padding: 10px 14px; }
  .ds-chat__models { display: none; }
  .ds-chat__model-info { font-size: 12px; }
}

/* ── 11. Reduced motion ───────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .ds-chat__msg { animation: none; }
  .ds-chat__dot { animation: none; opacity: .5; }
  .ds-chat__send:hover { transform: none; }
}

/* ── 12. Focus & a11y ─────────────────────────────────────────────── */
.ds-chat *:focus-visible {
  outline: 2px solid var(--ds-primary);
  outline-offset: 2px;
}

.ds-chat .ds-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;
}

/* ── 13. Print ────────────────────────────────────────────────────── */
@media print {
  .ds-chat__header, .ds-chat__input-area, .ds-chat__actions { display: none !important; }
  .ds-chat__shell { height: auto !important; box-shadow: none; border: none; }
  .ds-chat__conversation { overflow: visible; height: auto; }
}
