:root {
  --bg: #0e1117;
  --bg-2: #151a23;
  --bg-3: #1c2331;
  --line: #252d3b;
  --text: #e7ecf3;
  --muted: #8b97a8;
  --primary: #6c8cff;
  --primary-2: #4f6ef0;
  --green: #2ecc71;
  --red: #ff5c6c;
  --amber: #ffb454;
  --in: #222b3a;
  --out: #2d4276;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Vazirmatn', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 14px;
}

body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.hidden { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  border-bottom: 1px solid var(--line);
  gap: 12px;
  flex: none;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px; border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), #9b6cff);
  display: grid; place-items: center; font-weight: 800; font-size: 20px; color: #fff;
}
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

.top-actions { display: flex; align-items: center; gap: 14px; }
.stat { text-align: center; line-height: 1.1; }
.stat span { font-weight: 800; font-size: 17px; color: var(--primary); }
.stat label { display: block; font-size: 11px; color: var(--muted); }

.conn { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.conn .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--red); box-shadow: 0 0 0 0 rgba(255,92,108,.5); }
.conn.online .dot { background: var(--green); animation: pulse 2s infinite; }
@keyframes pulse { 0%{box-shadow:0 0 0 0 rgba(46,204,113,.5)} 70%{box-shadow:0 0 0 8px rgba(46,204,113,0)} 100%{box-shadow:0 0 0 0 rgba(46,204,113,0)} }

/* ---------- Buttons & switches ---------- */
.btn {
  border: 1px solid var(--line); background: var(--bg-3); color: var(--text);
  padding: 8px 14px; border-radius: 10px; cursor: pointer; font-family: inherit; font-size: 13px;
  transition: .15s; white-space: nowrap;
}
.btn:hover { border-color: var(--primary); }
.btn.sm { padding: 6px 10px; font-size: 12px; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--primary-2); border-color: var(--primary-2); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--primary); }

.switch-wrap { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); cursor: pointer; }
.switch { position: relative; width: 38px; height: 22px; display: inline-block; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: var(--bg-3); border: 1px solid var(--line); border-radius: 999px; transition: .2s; }
.slider::before { content: ''; position: absolute; height: 16px; width: 16px; right: 3px; top: 2px; background: var(--muted); border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: rgba(108,140,255,.3); border-color: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(-16px); background: var(--primary); }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 320px 1fr; flex: 1; min-height: 0; }

/* ---------- Sidebar ---------- */
.sidebar { border-left: 1px solid var(--line); background: var(--bg-2); display: flex; flex-direction: column; min-height: 0; }
.search { padding: 12px; border-bottom: 1px solid var(--line); }
.search input { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-family: inherit; }
.search input:focus { outline: none; border-color: var(--primary); }

.conv-list { overflow-y: auto; flex: 1; }
.conv {
  display: flex; gap: 11px; padding: 12px; cursor: pointer; border-bottom: 1px solid var(--line);
  transition: background .12s; align-items: center;
}
.conv:hover { background: var(--bg-3); }
.conv.active { background: var(--bg-3); box-shadow: inset 3px 0 0 var(--primary); }
.conv .avatar { flex: none; }
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.conv-name { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 11px; color: var(--muted); flex: none; }
.conv-prev { color: var(--muted); font-size: 12.5px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-badges { display: flex; align-items: center; gap: 5px; margin-top: 4px; }
.badge { font-size: 10px; padding: 1px 7px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.badge.unread { background: var(--primary-2); color: #fff; border-color: var(--primary-2); font-weight: 700; }
.badge.human { background: rgba(255,180,84,.15); color: var(--amber); border-color: rgba(255,180,84,.4); }
.badge.botoff { background: rgba(255,92,108,.12); color: var(--red); border-color: rgba(255,92,108,.35); }

.avatar {
  width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center;
  font-weight: 700; color: #fff; font-size: 16px;
  background: linear-gradient(135deg, #5466b3, #7b53b8);
}

/* ---------- Chat ---------- */
.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }
.chat-empty { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); gap: 6px; }
.empty-icon { font-size: 54px; }
.chat-active { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.chat-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--bg-2); }
.peer { display: flex; align-items: center; gap: 11px; }
.peer-name { font-weight: 700; }
.peer-id { font-size: 11px; color: var(--muted); font-family: ui-monospace, monospace; }
.chat-tools { display: flex; align-items: center; gap: 8px; }
.flag { font-size: 12px; color: var(--amber); background: rgba(255,180,84,.12); padding: 5px 10px; border-radius: 8px; border: 1px solid rgba(255,180,84,.35); }

.messages { flex: 1; overflow-y: auto; overflow-x: hidden; min-width: 0; padding: 18px 16px; display: flex; flex-direction: column; gap: 10px; }

.msg { max-width: 72%; display: flex; flex-direction: column; gap: 4px; }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 13px; border-radius: 14px; line-height: 1.7; white-space: pre-wrap; word-break: break-word; }
.msg.in .bubble { background: var(--in); border-bottom-right-radius: 4px; }
.msg.out .bubble { background: var(--out); border-bottom-left-radius: 4px; }
.msg.failed .bubble { border: 1px dashed var(--red); }

.meta { font-size: 10.5px; color: var(--muted); display: flex; gap: 7px; align-items: center; padding: 0 4px; }
.tag { font-size: 10px; padding: 1px 7px; border-radius: 6px; border: 1px solid var(--line); }
.tag.bot { color: var(--primary); border-color: rgba(108,140,255,.4); background: rgba(108,140,255,.1); }
.tag.op { color: var(--green); border-color: rgba(46,204,113,.4); background: rgba(46,204,113,.1); }
.tag.user { color: var(--muted); }
.tag.fail { color: var(--red); border-color: rgba(255,92,108,.4); background: rgba(255,92,108,.1); }

.bubble img, .bubble video { max-width: 240px; border-radius: 10px; display: block; }
.bubble audio { width: 240px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 7px; }
.chip { font-size: 12px; padding: 4px 11px; border-radius: 999px; background: rgba(255,255,255,.06); border: 1px solid var(--line); }

.cards { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; }
.card { flex: none; width: 200px; background: var(--bg-3); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.card img { width: 100%; height: 120px; object-fit: cover; }
.card-body { padding: 9px 11px; }
.card-title { font-weight: 700; font-size: 13px; }
.card-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.card-btns { display: flex; flex-direction: column; gap: 5px; margin-top: 8px; }
.card-btn { font-size: 11.5px; text-align: center; padding: 6px; border-radius: 8px; background: rgba(108,140,255,.12); border: 1px solid rgba(108,140,255,.3); color: var(--primary); }

.sys-note { align-self: center; font-size: 11px; color: var(--muted); background: var(--bg-2); padding: 3px 12px; border-radius: 999px; border: 1px solid var(--line); }

.composer { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--bg-2); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 120px; padding: 10px 13px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-family: inherit; font-size: 13.5px; line-height: 1.6; }
.composer textarea:focus { outline: none; border-color: var(--primary); }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; z-index: 50; }
.modal-box { background: var(--bg-2); border: 1px solid var(--line); border-radius: 16px; padding: 20px; width: min(520px, 92vw); box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head h3 { margin: 0; }
.flows { margin: 0; padding-right: 18px; display: flex; flex-direction: column; gap: 8px; }
.flows li { padding: 9px 12px; background: var(--bg-3); border: 1px solid var(--line); border-radius: 10px; }
.flows .fid { color: var(--primary); font-family: ui-monospace, monospace; font-size: 11px; }
.modal-note { color: var(--muted); font-size: 12px; margin-top: 14px; line-height: 1.8; }
.modal-note code { background: var(--bg); padding: 1px 6px; border-radius: 5px; }

/* ---------- Toasts ---------- */
.toast-wrap { position: fixed; bottom: 18px; left: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--bg-3); border: 1px solid var(--line); border-right: 3px solid var(--primary); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13px; animation: slidein .25s ease; max-width: 320px; }
.toast.bot { border-right-color: var(--primary); }
.toast.msg { border-right-color: var(--green); }
@keyframes slidein { from { transform: translateX(-20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: #34405200; background: #344052; }
