:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273549;
  --border: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --user-bubble: #1d4ed8;
  --op-bubble: #334155;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: "Vazirmatn", system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 14px;
}

.hidden { display: none !important; }

/* Buttons & Inputs */
.btn {
  border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; font-family: inherit; font-size: 13px;
  cursor: pointer; font-weight: 500; transition: 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #334155; }
.btn-warn { background: #78350f; color: #fde68a; border-color: #b45309; }
.btn-danger { background: #7f1d1d; color: #fecaca; border-color: #b91c1c; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }
.btn-block { width: 100%; }

input, select, textarea {
  font-family: inherit; font-size: 13px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }

/* Overlays & Modals */
.overlay {
  position: fixed; inset: 0; background: rgba(2, 6, 23, 0.85);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.login-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px; width: 360px; text-align: center;
}
.login-logo { font-size: 42px; margin-bottom: 8px; }
.login-card h1 { margin: 0 0 6px; font-size: 19px; }
.login-card p { margin: 0 0 20px; color: var(--muted); font-size: 13px; }
.login-card input { width: 100%; margin-bottom: 12px; }
.error-text { color: #fca5a5; font-size: 12px; margin-top: 8px; min-height: 18px; }

.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; width: 760px; max-width: 95vw; max-height: 90vh;
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 16px; }
.settings-form { padding: 18px 20px; overflow-y: auto; }
.settings-form h3 { margin: 12px 0 10px; font-size: 14px; color: #93c5fd; }
.settings-form hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.form-grid label, .full-width { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.full-width { width: 100%; }
.modal-footer {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border);
}
.status-msg { font-size: 12px; color: #86efac; }

/* App Shell */
.app-shell { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  height: 52px; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 16px;
  flex-shrink: 0;
}
.topbar-right, .topbar-left { display: flex; align-items: center; gap: 10px; }
.brand { font-weight: 700; font-size: 15px; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.status-dot.online { background: var(--success); box-shadow: 0 0 8px var(--success); }
.status-dot.offline { background: var(--danger); }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.badge-info { background: #1e3a8a; color: #93c5fd; }
.badge-success { background: #14532d; color: #86efac; }
.badge-warn { background: #78350f; color: #fde68a; }
.badge-danger { background: #7f1d1d; color: #fecaca; }

/* 3-Column Workspace */
.workspace {
  flex: 1; display: grid;
  grid-template-columns: 300px 1fr 340px;
  min-height: 0; overflow: hidden;
}

/* Column 1: Chats */
.col-chats {
  background: var(--surface); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.col-header { padding: 10px; border-bottom: 1px solid var(--border); }
.search-input { width: 100%; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  padding: 12px; border-bottom: 1px solid var(--border);
  cursor: pointer; transition: background 0.12s;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: #1e3a5f; border-right: 3px solid var(--primary); }
.chat-item-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; margin-bottom: 4px; }
.chat-item-name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.chat-item-preview {
  font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-bottom: 6px;
}
.chat-item-badges { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.unread-pill {
  background: var(--primary); color: #fff; font-size: 11px; font-weight: 700;
  padding: 1px 7px; border-radius: 999px;
}
.empty-state { padding: 24px; text-align: center; color: var(--muted); font-size: 12px; line-height: 1.8; }

/* Column 2: Thread */
.col-thread { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.thread-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; color: var(--muted); text-align: center; padding: 24px;
}
.thread-empty-icon { font-size: 48px; margin-bottom: 10px; }
.thread-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-header {
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.thread-user-name { font-weight: 700; font-size: 14px; }
.thread-user-meta { font-size: 11px; color: var(--muted); }
.thread-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.select-sm { padding: 4px 8px; font-size: 12px; }

.messages-container {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.msg {
  max-width: 78%; padding: 10px 13px; border-radius: 12px;
  line-height: 1.65; white-space: pre-wrap; word-break: break-word;
}
.msg.user { align-self: flex-start; background: var(--user-bubble); border-bottom-right-radius: 3px; }
.msg.operator { align-self: flex-end; background: var(--op-bubble); border-bottom-left-radius: 3px; }
.msg.form { background: #1e293b; border: 1px dashed var(--border); color: var(--muted); font-size: 12px; }
.msg-meta { font-size: 10px; opacity: 0.75; margin-top: 4px; display: flex; justify-content: space-between; gap: 10px; }
.msg img.msg-thumb { max-width: 240px; max-height: 220px; border-radius: 8px; display: block; margin-top: 6px; cursor: pointer; }

.ai-banner {
  padding: 8px 14px; font-size: 12px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.ai-banner.debouncing { background: #1e3a8a; color: #bfdbfe; }
.ai-banner.ready { background: #14532d; color: #bbf7d0; }
.ai-banner.handoff { background: #78350f; color: #fef3c7; }
.ai-banner.human { background: #3f3f46; color: #e4e4e7; }

.quick-replies {
  display: flex; gap: 6px; padding: 8px 12px; overflow-x: auto;
  background: var(--surface); border-top: 1px solid var(--border);
}
.chip {
  flex-shrink: 0; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 4px 10px; font-size: 11px; font-family: inherit; cursor: pointer;
}
.chip:hover { border-color: var(--primary); color: #93c5fd; }

.composer { padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--border); }
.composer textarea { width: 100%; resize: none; line-height: 1.6; }
.composer-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.hint { font-size: 11px; color: var(--muted); }
kbd { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; font-size: 10px; }

/* Column 3: Context Sidebar */
.col-context {
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 14px;
}
.context-section {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
}
.context-title {
  font-weight: 600; font-size: 12px; color: #93c5fd;
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px;
}
.context-body { font-size: 12px; line-height: 1.6; }
.muted { color: var(--muted); }
.inline-search { display: flex; gap: 6px; margin-bottom: 8px; }
.inline-search input { flex: 1; padding: 5px 8px; font-size: 12px; }

.info-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; margin-bottom: 8px;
}
.info-card:last-child { margin-bottom: 0; }
.info-card-title { font-weight: 600; font-size: 12px; margin-bottom: 4px; }
.info-card-title a { color: #93c5fd; text-decoration: none; }
.info-card-title a:hover { text-decoration: underline; }
.info-row { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 2px; }
.page-link { display: block; color: #93c5fd; font-size: 11px; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; }
.page-link:hover { text-decoration: underline; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 240px 1fr; }
  .col-context { display: none; }
}
