: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;
}

* { 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: 13.5px;
}
.hidden { display: none !important; }

.btn {
  border: 1px solid transparent; border-radius: 8px; padding: 7px 13px;
  font-family: inherit; font-size: 12.5px; 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-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: 7px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }

/* Overlays */
.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: 30px; width: 360px; text-align: center;
}
.login-logo { font-size: 40px; margin-bottom: 8px; }
.login-card h1 { margin: 0 0 6px; font-size: 18px; }
.login-card p { margin: 0 0 18px; color: var(--muted); font-size: 12px; }
.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-wide { width: 920px; }
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 15px; }
.settings-form { padding: 16px 18px; overflow-y: auto; }
.settings-form h3 { margin: 10px 0 8px; font-size: 13.5px; color: #93c5fd; }
.settings-form hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.modal-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--border); }
.status-msg { font-size: 12px; color: #86efac; }

/* Topbar & Tabs */
.app-shell { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  height: 50px; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 14px; flex-shrink: 0;
}
.topbar-right, .topbar-left { display: flex; align-items: center; gap: 10px; }
.brand { font-weight: 700; font-size: 14px; }
.status-dot { width: 9px; height: 9px; 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); }

.nav-tabs { display: flex; gap: 4px; margin-right: 12px; }
.nav-tab {
  background: transparent; color: var(--muted); border: 1px solid transparent;
  border-radius: 8px; padding: 5px 11px; font-family: inherit; font-size: 12.5px; cursor: pointer;
}
.nav-tab:hover { color: var(--text); background: var(--surface-2); }
.nav-tab.active { background: #1e3a8a; color: #dbeafe; border-color: #3b82f6; font-weight: 600; }
.pill { background: #16a34a; color: #fff; border-radius: 999px; padding: 0 6px; font-size: 11px; margin-right: 4px; }

.badge { display: inline-block; padding: 2px 7px; 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 Chat Workspace */
.workspace { flex: 1; display: grid; grid-template-columns: 290px 1fr 335px; min-height: 0; overflow: hidden; }
.col-chats { background: var(--surface); border-left: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.col-header { padding: 8px; border-bottom: 1px solid var(--border); }
.search-input { width: 100%; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.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: 3px; }
.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); }
.chat-item-preview { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 5px; }
.chat-item-badges { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.unread-pill { background: var(--primary); color: #fff; font-size: 10.5px; font-weight: 700; padding: 1px 6px; border-radius: 999px; }
.empty-state { padding: 24px; text-align: center; color: var(--muted); font-size: 12px; line-height: 1.8; }

/* Thread */
.col-thread { display: flex; flex-direction: column; min-height: 0; background: #0b1120; }
.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: 46px; margin-bottom: 8px; }
.thread-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-header {
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.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; }

.messages-container {
  flex: 1; overflow-y: auto; padding: 12px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.msg {
  width: fit-content; min-width: 72px; max-width: 65%;
  padding: 5px 10px; border-radius: 10px; font-size: 13px; line-height: 1.45;
}
.msg-body { white-space: pre-wrap; word-break: break-word; }
.msg.operator { align-self: flex-start; background: #064e3b; border: 1px solid #059669; color: #ecfdf5; border-top-right-radius: 3px; }
.msg.user { align-self: flex-end; background: #1e293b; border: 1px solid #334155; color: #f1f5f9; border-top-left-radius: 3px; }
.msg-sender { font-size: 10px; font-weight: 700; margin-bottom: 1px; line-height: 1.2; }
.msg.operator .msg-sender { color: #6ee7b7; }
.msg.user .msg-sender { color: #93c5fd; }
.msg-meta { font-size: 10px; opacity: 0.72; margin-top: 2px; line-height: 1.1; display: flex; justify-content: flex-end; align-items: center; gap: 6px; }
.msg-token-pill {
  background: rgba(15, 23, 42, 0.65); border: 1px solid rgba(147, 197, 253, 0.35);
  color: #bae6fd; border-radius: 999px; padding: 0 6px; font-size: 10px; cursor: pointer; font-family: inherit;
}
.msg-token-pill:hover { background: rgba(59, 130, 246, 0.35); color: #fff; }
.msg img.msg-thumb { max-width: 220px; max-height: 200px; border-radius: 8px; display: block; margin-top: 4px; cursor: pointer; }
.msg video { max-width: 240px; border-radius: 8px; display: block; margin-top: 4px; }
.msg audio { width: 220px; height: 34px; margin-top: 4px; }

.typing-bar {
  padding: 5px 12px; font-size: 12px; background: #1e293b; color: #fde68a;
  border-top: 1px dashed #f59e0b;
}
.ai-banner {
  padding: 6px 12px; 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; }
.dns-banner { background: #7f1d1d; color: #fee2e2; border-bottom: 2px solid #ef4444; padding: 8px 14px; font-size: 12px; font-weight: 700; }

.quick-replies { display: flex; gap: 5px; padding: 5px 10px; 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: 3px 9px; font-size: 11px; font-family: inherit; cursor: pointer; }
.chip:hover { border-color: var(--primary); color: #93c5fd; }

.composer { padding: 8px 10px; background: var(--surface); border-top: 1px solid var(--border); }
.composer-row { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; line-height: 1.5; font-size: 13px; padding: 7px 10px; min-height: 44px; max-height: 120px; }
.send-btn { height: 44px; padding: 0 15px; flex-shrink: 0; }

/* Context Sidebar */
.col-context { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 12px; }
.context-section { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px; }
.context-title { font-weight: 600; font-size: 12px; color: #93c5fd; display: flex; justify-content: space-between; align-items: center; margin-bottom: 7px; }
.context-body { font-size: 12px; line-height: 1.6; }
.muted { color: var(--muted); }
.inline-search { display: flex; gap: 6px; margin-bottom: 7px; }
.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: 7px; }
.info-card:last-child { margin-bottom: 0; }
.info-card-title { font-weight: 600; font-size: 12px; margin-bottom: 3px; }
.info-card-title a { color: #93c5fd; text-decoration: none; }
.info-row { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 2px; gap: 6px; }
.page-link { display: block; color: #93c5fd; font-size: 11px; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; }
.summary-card { background: #172554; border: 1px solid #1d4ed8; border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; font-size: 11px; line-height: 1.7; }
.summary-card.danger { background: #7f1d1d; border-color: #ef4444; color: #fee2e2; }
.var-list { margin-top: 5px; padding-top: 5px; border-top: 1px dashed var(--border); }
.var-row { display: flex; justify-content: space-between; font-size: 11px; padding: 2px 0; }

/* Prompt Inspector */
.prompt-box { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; font-size: 12px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; max-height: 240px; overflow-y: auto; color: #e2e8f0; margin-top: 5px; }
.token-summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.token-stat-card { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 9px; text-align: center; }
.token-stat-val { font-size: 16px; font-weight: 700; color: #93c5fd; }
.token-stat-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* Online & Analytics Panels */
.page-panel { flex: 1; overflow-y: auto; padding: 18px 22px; }
.panel-header-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.panel-header-row h2 { margin: 0; font-size: 16px; }
.card-panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; }
.kpi-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 14px; }
.analytics-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th, .data-table td { text-align: right; padding: 7px 8px; border-bottom: 1px solid var(--border); }
.data-table th { color: var(--muted); font-weight: 600; font-size: 11px; }
.data-table a { color: #93c5fd; text-decoration: none; }
.timeline-step { display: flex; justify-content: space-between; align-items: center; padding: 6px 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 5px; font-size: 12px; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 230px 1fr; }
  .col-context { display: none; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .analytics-grid { grid-template-columns: 1fr; }
}
