:root {
  --bg:        #111b21;
  --sidebar:   #111b21;
  --sidebar2:  #202c33;
  --card:      #202c33;
  --card2:     #2a3942;
  --bubble-in: #202c33;
  --bubble-out:#005c4b;
  --accent:    #00a884;
  --accent2:   #53bdeb;
  --text:      #e9edef;
  --muted:     #8696a0;
  --border:    #2a3942;
  --danger:    #ef4444;
  --radius:    8px;
  --font:      'Segoe UI', system-ui, sans-serif;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
  direction: rtl;
}

.d-none { display: none !important; }

/* ── Layout ── */
.app {
  display: grid;
  grid-template-columns: 360px 1fr;
  height: 100dvh;
}

/* ── Sidebar ── */
.sidebar {
  background: var(--sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .9rem 1rem;
  background: var(--sidebar2);
  flex-shrink: 0;
}

.sidebar-title {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: 1rem;
  font-weight: 700;
}
.wa-icon { font-size: 1.3rem; }

.sidebar-actions { display: flex; gap: .4rem; }

.icon-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: .35rem .45rem;
  border-radius: 50%;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--border); color: var(--text); }

.sidebar-stats {
  padding: .5rem 1rem;
  font-size: .75rem;
  color: var(--muted);
  background: var(--sidebar2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.search-wrap {
  padding: .6rem .75rem;
  background: var(--sidebar2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-input {
  width: 100%;
  background: var(--bg);
  border: none;
  border-radius: var(--radius);
  padding: .5rem .75rem;
  color: var(--text);
  font-size: .88rem;
  outline: none;
}
.search-input::placeholder { color: var(--muted); }

.conv-list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

/* ── Conversation item ── */
.conv-item {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .75rem 1rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.conv-item:hover   { background: var(--card2); }
.conv-item.active  { background: var(--card2); }
.conv-item.unread .conv-name { font-weight: 700; color: var(--text); }

.conv-avatar {
  width: 46px; height: 46px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem; font-weight: 700; color: #fff;
  flex-shrink: 0;
}

.conv-body { flex: 1; min-width: 0; }
.conv-top  { display: flex; justify-content: space-between; align-items: baseline; }
.conv-name { font-size: .9rem; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: .72rem; color: var(--muted); flex-shrink: 0; margin-right: .4rem; }
.conv-preview { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-bottom { display: flex; justify-content: space-between; align-items: center; }
.unread-badge {
  background: var(--accent);
  color: #fff;
  border-radius: 50px;
  font-size: .7rem;
  font-weight: 700;
  padding: .1rem .45rem;
  min-width: 20px;
  text-align: center;
}

.empty-state-small {
  text-align: center;
  color: var(--muted);
  font-size: .85rem;
  padding: 2rem 1rem;
}

/* ── Chat area ── */
.chat-area {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  color: var(--muted);
}
.chat-empty-icon  { font-size: 3rem; }
.chat-empty-title { font-size: 1.1rem; font-weight: 600; color: var(--text); }
.chat-empty-sub   { font-size: .85rem; }

.chat-main { display: flex; flex-direction: column; height: 100%; }

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1.2rem;
  background: var(--sidebar2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-header-info { display: flex; align-items: center; gap: .75rem; }
.chat-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem; font-weight: 700; color: #fff;
}
.chat-name  { font-size: .95rem; font-weight: 600; }
.chat-phone { font-size: .78rem; color: var(--muted); direction: ltr; }

.btn-close-conv {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: var(--radius);
  padding: .3rem .75rem;
  font-size: .8rem;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.btn-close-conv:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ── Messages ── */
.messages-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .35rem;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.msg-row {
  display: flex;
  margin-bottom: .2rem;
}
.msg-row.out { justify-content: flex-start; }
.msg-row.in  { justify-content: flex-end; }

.msg-bubble {
  max-width: 65%;
  padding: .55rem .85rem;
  border-radius: 7.5px;
  font-size: .88rem;
  line-height: 1.45;
  position: relative;
  word-break: break-word;
}
.msg-row.in  .msg-bubble { background: var(--bubble-in);  border-radius: 7.5px 0 7.5px 7.5px; }
.msg-row.out .msg-bubble { background: var(--bubble-out); border-radius: 0 7.5px 7.5px 7.5px; }

.msg-meta {
  font-size: .68rem;
  color: rgba(255,255,255,.45);
  margin-top: .3rem;
  text-align: left;
  direction: ltr;
}
.msg-row.in .msg-meta { text-align: right; direction: rtl; }

.msg-date-divider {
  text-align: center;
  font-size: .72rem;
  color: var(--muted);
  margin: .75rem 0;
}

/* ── Suggestion panel ── */
.suggest-panel {
  padding: .75rem 1rem;
  background: var(--sidebar2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.suggest-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  margin-bottom: .6rem;
}
.suggest-label { font-size: .8rem; font-weight: 600; color: var(--accent); }
.suggest-meta  { font-size: .75rem; color: var(--muted); flex: 1; }
.suggest-refresh {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 1rem;
  transition: color .15s, transform .3s;
}
.suggest-refresh:hover { color: var(--accent); }
.suggest-refresh.spinning { animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.suggest-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .6rem;
}

.suggest-card {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: .65rem .85rem;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  font-size: .83rem;
  line-height: 1.45;
}
.suggest-card:hover   { border-color: var(--accent); background: var(--card2); }
.suggest-card.formal  { border-top: 3px solid var(--accent2); }
.suggest-card.friendly{ border-top: 3px solid var(--accent); }

.suggest-card-label {
  font-size: .7rem;
  font-weight: 700;
  margin-bottom: .3rem;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.suggest-card.formal   .suggest-card-label { color: var(--accent2); }
.suggest-card.friendly .suggest-card-label { color: var(--accent); }

.suggest-loading {
  grid-column: 1/-1;
  text-align: center;
  color: var(--muted);
  font-size: .82rem;
  padding: .75rem 0;
}

.multi-q-badge {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  background: rgba(0,168,132,.15);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 50px;
  font-size: .72rem;
  font-weight: 600;
  padding: .15rem .55rem;
}

/* ── Reply box ── */
.reply-box {
  display: flex;
  align-items: flex-end;
  gap: .6rem;
  padding: .75rem 1rem;
  background: var(--sidebar2);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.reply-input {
  flex: 1;
  background: var(--bg);
  border: none;
  border-radius: var(--radius);
  padding: .65rem .9rem;
  color: var(--text);
  font-size: .88rem;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  min-height: 42px;
  scrollbar-width: thin;
}
.reply-input::placeholder { color: var(--muted); }
.reply-actions { display: flex; gap: .4rem; }

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: .6rem 1rem;
  font-size: .88rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}
.btn-send:hover    { background: #00c99f; }
.btn-send:disabled { opacity: .5; cursor: default; }

/* ── Modal ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal {
  background: var(--card);
  border-radius: 12px;
  width: 100%;
  max-width: 600px;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
}
.modal-sm { max-width: 400px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close {
  background: none; border: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer;
}
.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.2rem;
  scrollbar-width: thin;
}

/* ── Knowledge ── */
.knowledge-add-form {
  display: flex; flex-direction: column; gap: .5rem;
  margin-bottom: 1.2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}
.k-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .55rem .75rem;
  color: var(--text);
  font-size: .88rem;
  font-family: inherit;
  outline: none;
  resize: vertical;
  width: 100%;
}
.k-input:focus { border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: .55rem 1.1rem;
  font-size: .88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary:hover { background: #00c99f; }

.knowledge-list { display: flex; flex-direction: column; gap: .6rem; }

.k-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem;
}
.k-item-q {
  font-size: .85rem;
  font-weight: 600;
  margin-bottom: .3rem;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: .5rem;
}
.k-item-a  { font-size: .82rem; color: var(--muted); line-height: 1.4; }
.k-source  { font-size: .68rem; color: var(--accent); flex-shrink: 0; margin-top: .1rem; }
.k-del {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: .85rem; padding: 0;
  flex-shrink: 0;
}
.k-del:hover { color: var(--danger); }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--card2);
  color: var(--text);
  border-radius: var(--radius);
  padding: .65rem 1.2rem;
  font-size: .88rem;
  z-index: 999;
  transition: transform .25s ease;
  pointer-events: none;
  white-space: nowrap;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ── Scrollbar ── */
::-webkit-scrollbar       { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Responsive ── */
@media (max-width: 680px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .sidebar.mobile-open { display: flex; position: fixed; inset: 0; z-index: 50; }
}
