@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
  --primary: #3B82F6;
  --primary-dark: #2563EB;
  --primary-light: #DBEAFE;
  --bg: #ffffff;
  --bg-soft: #f8fafc;
  --bg-muted: #f1f5f9;
  --text: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --green: #10b981;
  --green-bg: #d1fae5;
  --amber: #f59e0b;
  --amber-bg: #fef3c7;
  --red: #ef4444;
  --red-bg: #fee2e2;
  --purple: #8b5cf6;
  --purple-bg: #ede9fe;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 20px rgba(0,0,0,0.08);
  --topbar-h: 56px;
  --sidebar-w: 360px;
}

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

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

body:not(.auth-ok) .dash { display: none; }
body.auth-ok .login-shell { display: none; }

.login-shell {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eef2ff, #f8fafc);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.login-card h2 { font-size: 20px; }
.login-card p { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; }
.login-card label { font-size: 12px; color: var(--text-secondary); font-weight: 600; }
.login-card input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}
.login-card input:focus { border-color: var(--primary); }
.login-card .panel-btn { margin-top: 8px; }
.login-error { color: var(--red); font-size: 12px; }

.dash { display: flex; flex-direction: column; height: 100vh; }

/* ── Top Bar ── */
.dash-topbar {
  height: var(--topbar-h);
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
  z-index: 100;
}

.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-logo { width: 24px; height: 24px; fill: var(--primary); }
.topbar-title { font-size: 15px; font-weight: 700; color: var(--text); }

.topbar-right { display: flex; align-items: center; gap: 12px; }

.topbar-widget-select {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-secondary);
}
.topbar-widget-select select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  min-width: 170px;
  outline: none;
  transition: border-color 0.15s;
  background: #fff;
  color: var(--text);
}
.topbar-widget-select select:focus { border-color: var(--primary); }

.topbar-btn {
  width: 34px; height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  text-decoration: none;
  transition: all 0.15s;
}
.topbar-btn:hover { background: var(--bg-muted); color: var(--text); }
.topbar-btn svg { width: 18px; height: 18px; fill: currentColor; }
.topbar-btn-embed {
  width: auto;
  padding: 0 10px;
  gap: 6px;
}
.topbar-btn-embed span { font-size: 12px; font-weight: 500; }

/* ── Body Layout ── */
.dash-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ── */
.dash-sidebar {
  width: var(--sidebar-w);
  background: #fff;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: #fff;
}
.sidebar-mode-tab {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  padding: 8px 6px;
  cursor: pointer;
}
.sidebar-mode-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

#messages-sidebar-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* Analytics Row */
.sidebar-analytics {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
}
.analytics-stat { text-align: center; }
.analytics-val {
  display: block;
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}
.analytics-active { color: var(--primary); }
.analytics-resolved { color: var(--green); }
.analytics-escalated { color: var(--red); }
.analytics-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

/* Filters */
.sidebar-filters {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-soft);
  transition: border-color 0.15s;
}
.search-box:focus-within { border-color: var(--primary); background: #fff; }
.search-box svg { width: 16px; height: 16px; fill: var(--text-muted); flex-shrink: 0; }
.search-box input {
  border: none;
  background: none;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  flex: 1;
  color: var(--text);
}
.search-box input::placeholder { color: var(--text-muted); }

.filter-tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.filter-tab {
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}
.filter-tab:hover { background: var(--bg-muted); }
.filter-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* Conversation List */
.conversation-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.conv-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--text-muted);
  text-align: center;
}
.conv-empty svg { width: 48px; height: 48px; fill: var(--border); margin-bottom: 12px; }
.conv-empty p { font-size: 14px; font-weight: 600; color: var(--text-secondary); }
.conv-empty span { font-size: 12px; margin-top: 4px; }

.conv-item {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-light);
  cursor: pointer;
  transition: background 0.12s;
  position: relative;
}
.conv-item:hover { background: var(--bg-soft); }
.conv-item.active { background: var(--primary-light); }
.conv-item.has-unread {
  border-left: 3px solid var(--primary);
  padding-left: 13px;
}

.conv-item-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--bg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.conv-item-avatar svg { width: 18px; height: 18px; fill: var(--text-muted); }

.conv-item-body { flex: 1; min-width: 0; }
.conv-item-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2px; }
.conv-item-name {
  font-size: 13px;
  font-weight: 400;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-transform: lowercase;
}
.conv-item.conv-unread-bold .conv-item-name {
  font-weight: 700;
}
.conv-item-time {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
  margin-left: 8px;
  text-transform: lowercase;
}
.conv-unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  padding: 0 5px;
  margin-left: 6px;
}
.conv-item.conv-unread-bold .conv-unread-badge {
  font-weight: 700;
}
.conv-item-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-transform: lowercase;
}
.conv-item-bottom { display: flex; align-items: center; gap: 6px; margin-top: 4px; flex-wrap: wrap; text-transform: lowercase; }

.conv-domain {
  font-size: 10px;
  color: var(--primary);
  font-weight: 400;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-transform: lowercase;
}

.conv-status-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.conv-status-badge.status-active { background: var(--primary-light); color: var(--primary); }
.conv-status-badge.status-pending { background: var(--amber-bg); color: var(--amber); }
.conv-status-badge.status-escalated { background: var(--red-bg); color: var(--red); }
.conv-status-badge.status-resolved { background: var(--green-bg); color: var(--green); }

.conv-category-badge {
  font-size: 10px;
  color: var(--text-muted);
  font-weight: 500;
}
.conv-msg-count {
  font-size: 10px;
  color: var(--text-muted);
  margin-left: auto;
}

/* ── Main Panel ── */
.dash-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
  min-height: 0;
}

#section-messages {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.dash-section {
  height: 100%;
  overflow: auto;
  padding: 20px;
  background: #fff;
}
.section-header h3 {
  font-size: 20px;
  margin-bottom: 4px;
}
.section-header p {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 16px;
}
.section-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.panel {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel h4 {
  font-size: 14px;
}
.panel input, .panel textarea, .panel select {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  font-family: inherit;
  font-size: 13px;
  width: 100%;
}
.panel-btn {
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  padding: 10px 12px;
  cursor: pointer;
}
.panel-btn-secondary {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
}
.panel-btn-secondary:hover {
  background: var(--bg-soft);
}
.api-endpoints-toolbar {
  display: flex;
  gap: 8px;
}
.panel-hint {
  color: var(--text-muted);
  font-size: 11px;
}
.panel-hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
  background: var(--bg-soft);
  border: 1px solid var(--border-light);
}

/* Embed section */
.embed-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}
.embed-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 12px;
}
.embed-code {
  flex: 1;
  min-height: 80px;
  margin: 0;
  padding: 12px 14px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
  background: var(--bg-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text);
}
.btn-copy-embed {
  flex-shrink: 0;
}
.btn-copy-embed:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
#section-embed .section-header code {
  font-size: 12px;
  background: var(--bg-muted);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border-light);
}
.training-list {
  max-height: 420px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.training-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}
.training-item-q {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}
.training-item-a {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: pre-wrap;
}
.training-item-actions {
  margin-top: 8px;
  display: flex;
  justify-content: flex-end;
}
.training-item-delete {
  border: 1px solid var(--red);
  background: #fff;
  color: var(--red);
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 8px;
  cursor: pointer;
}

.main-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  text-align: center;
  padding: 40px;
}
.main-empty svg { width: 56px; height: 56px; fill: var(--border); margin-bottom: 16px; }
.main-empty h3 { font-size: 18px; color: var(--text-secondary); margin-bottom: 6px; }
.main-empty p { font-size: 13px; max-width: 300px; }

/* Detail View */
.conv-detail { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.detail-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
  background: #fff;
}
.detail-header-info { display: flex; align-items: center; gap: 10px; }

.detail-back {
  display: none;
  width: 32px; height: 32px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.detail-back svg { width: 18px; height: 18px; fill: var(--text-secondary); }

.detail-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--bg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.detail-avatar svg { width: 20px; height: 20px; fill: var(--text-muted); }

.detail-name { font-size: 14px; font-weight: 600; }
.detail-meta { font-size: 12px; color: var(--text-muted); }

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

.detail-status {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}
.action-btn svg { width: 16px; height: 16px; fill: currentColor; }
.action-btn:hover { background: var(--bg-muted); }

.action-escalate { color: var(--red); }
.action-escalate:hover { background: var(--red-bg); border-color: var(--red); }
.action-resolve { color: var(--green); }
.action-resolve:hover { background: var(--green-bg); border-color: var(--green); }

/* Messages */
.detail-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-soft);
  min-height: 0;
}

.msg-row { display: flex; flex-direction: column; max-width: 70%; }
.msg-row.msg-user { align-self: flex-end; align-items: flex-end; }
.msg-row.msg-agent { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.msg-user .msg-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-agent .msg-bubble {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 3px;
  padding: 0 4px;
}

.msg-system {
  align-self: center;
  text-align: center;
  max-width: 80%;
}
.msg-system .msg-bubble {
  background: var(--amber-bg);
  color: var(--amber);
  font-size: 12px;
  font-weight: 500;
  border: none;
  border-radius: 8px;
}

/* Reply Box */
.detail-reply {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: #fff;
  flex-shrink: 0;
}
.detail-reply textarea {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 40px;
  line-height: 1.5;
  transition: border-color 0.15s;
}
.detail-reply textarea:focus { border-color: var(--primary); }
.detail-reply textarea::placeholder { color: var(--text-muted); }

.reply-send {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: var(--primary);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}
.reply-send:hover { background: var(--primary-dark); }
.reply-send:disabled { background: var(--border); cursor: not-allowed; }
.reply-send svg { width: 18px; height: 18px; fill: #fff; }

/* Loading Spinner */
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.loading-spinner::after {
  content: '';
  width: 24px; height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: #fff;
  background: var(--text);
  box-shadow: var(--shadow-lg);
  z-index: 9999;
  animation: slideUp 0.25s ease;
}
.toast.toast-success { background: var(--green); }
.toast.toast-error { background: var(--red); }
@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .dash-sidebar { width: 100%; }
  .dash-body { position: relative; }
  .dash-main {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: none;
  }
  .dash-main.show-detail {
    display: flex;
  }
  .detail-back { display: flex; }
  .topbar-widget-select label { display: none; }
}
