:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #faf8f4;
  --text: #1d1b18;
  --muted: #6f6a61;
  --border: #e6e1d7;
  --accent: #c2410c;
  --accent-soft: #fdeee4;
  --accent-text: #9a3412;
  --ok: #15803d;
  --ok-soft: #e7f6ec;
  --warn: #b45309;
  --warn-soft: #fef3dc;
  --danger: #b91c1c;
  --bubble-in: #ffffff;
  --bubble-out: #fdeee4;
  --bubble-human: #e8f0fe;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161412; --surface: #1f1c19; --surface-2: #252220; --text: #f1ede6; --muted: #a39d92;
    --border: #36322d; --accent: #fb923c; --accent-soft: #3a2416; --accent-text: #fdba74;
    --ok: #4ade80; --ok-soft: #173322; --warn: #fbbf24; --warn-soft: #3a2c10; --danger: #f87171;
    --bubble-in: #252220; --bubble-out: #3a2416; --bubble-human: #1d2a44;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161412; --surface: #1f1c19; --surface-2: #252220; --text: #f1ede6; --muted: #a39d92;
  --border: #36322d; --accent: #fb923c; --accent-soft: #3a2416; --accent-text: #fdba74;
  --ok: #4ade80; --ok-soft: #173322; --warn: #fbbf24; --warn-soft: #3a2c10; --danger: #f87171;
  --bubble-in: #252220; --bubble-out: #3a2416; --bubble-human: #1d2a44;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: 'IBM Plex Sans Arabic', system-ui, sans-serif; font-size: 15px; line-height: 1.6;
}
[hidden] { display: none !important; }
h2 { font-size: 17px; font-weight: 600; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 0; font-size: 14px; }

.btn {
  font: inherit; font-size: 14px; border-radius: var(--radius); padding: 8px 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.ok { color: var(--ok); border-color: var(--ok); }
.btn.warn { color: var(--warn); border-color: var(--warn); }
.btn:disabled { opacity: .5; cursor: default; }

input[type=search], input[type=password], textarea {
  font: inherit; width: 100%; padding: 9px 12px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 17px; }
.brand.big { font-size: 24px; justify-content: center; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: #fff;
  display: inline-grid; place-items: center; font-size: 17px;
}

/* ---------- الدخول ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: grid; gap: 12px; text-align: center;
}
.login-card p { margin: 0; }

/* ---------- الهيكل ---------- */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.top-actions { margin-inline-start: auto; display: flex; gap: 4px; }
.top-actions .btn.current { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }
.badges { display: flex; gap: 6px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--warn-soft); color: var(--warn); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; padding: 12px 16px 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.stat b { display: block; font-size: 22px; font-weight: 600; }
.stat span { font-size: 13px; color: var(--muted); }

.chats { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px 1fr; gap: 12px; padding: 12px 16px 16px; }
.list-pane, .thread-pane {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  min-height: 0; display: flex; flex-direction: column; overflow: hidden;
}
.list-tools { padding: 12px; border-bottom: 1px solid var(--border); display: grid; gap: 8px; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 13px; padding: 4px 10px; border-radius: 99px; cursor: pointer;
  border: 1px solid var(--border); background: transparent; color: var(--muted);
}
.chip.active { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv {
  display: grid; grid-template-columns: 36px 1fr auto; gap: 10px; align-items: start;
  padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--accent-soft); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  font-size: 12px; font-weight: 600; color: #fff;
}
.p-messenger { background: #0866ff; }
.p-instagram { background: #d62976; }
.p-whatsapp { background: #25a35a; }
.conv-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-main { min-width: 0; }
.conv-side { text-align: left; font-size: 12px; color: var(--muted); display: grid; gap: 4px; justify-items: end; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 99px; white-space: nowrap; }
.tag.paused { background: var(--accent-soft); color: var(--accent-text); }
.tag.attn { background: var(--warn-soft); color: var(--warn); }
.tag.bot { background: var(--ok-soft); color: var(--ok); }
.tag.stage-inquiry { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.tag.stage-interested { background: var(--warn-soft); color: var(--warn); }
.tag.stage-ready { background: var(--ok-soft); color: var(--ok); font-weight: 600; }
.list-empty { padding: 24px; text-align: center; color: var(--muted); }

.empty { flex: 1; display: grid; place-items: center; color: var(--muted); }
.thread { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.thread-head {
  display: flex; gap: 10px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.back { display: none; }
.who { flex: 1; min-width: 160px; }
.who-name { font-weight: 600; }
.who-meta { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.thread-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; background: var(--surface-2); }
.msg { max-width: min(75%, 560px); padding: 8px 12px; border-radius: 12px; border: 1px solid var(--border); white-space: pre-wrap; word-wrap: break-word; }
.msg.in { align-self: flex-start; background: var(--bubble-in); border-start-start-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--bubble-out); border-color: transparent; border-start-end-radius: 4px; }
.msg.human { align-self: flex-end; background: var(--bubble-human); border-color: transparent; border-start-end-radius: 4px; }
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg-media { font-style: italic; color: var(--muted); }

.msg-img { display: block; max-width: 240px; max-height: 240px; border-radius: 8px; margin-bottom: 4px; cursor: zoom-in; }
.msg audio { display: block; max-width: 260px; margin-bottom: 4px; }
.transcript { font-size: 14px; }
.lead-card {
  margin: 10px 14px 0; padding: 10px 12px; border-radius: 10px; background: var(--warn-soft);
  display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; align-items: baseline;
}
.lead-card b { color: var(--warn); }
.lead-card .ready { color: var(--ok); font-weight: 600; }
.portfolio-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.portfolio-list li { padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

.composer { border-top: 1px solid var(--border); padding: 10px 12px; display: grid; gap: 6px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: vertical; min-height: 44px; max-height: 200px; }
.window-note { margin: 0; font-size: 13px; color: var(--warn); background: var(--warn-soft); padding: 6px 10px; border-radius: 8px; }

/* ---------- الإعدادات ---------- */
.settings { padding: 12px 16px 24px; display: grid; gap: 12px; max-width: 900px; width: 100%; margin: 0 auto; overflow-y: auto; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.switch-row {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  padding: 10px 0; border-top: 1px solid var(--border); cursor: pointer;
}
.switch-row:first-of-type { border-top: 0; }
.switch { appearance: none; width: 44px; height: 24px; border-radius: 99px; background: var(--border); position: relative; cursor: pointer; flex-shrink: 0; transition: background .15s; }
.switch::after { content: ''; position: absolute; top: 3px; right: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: right .15s; }
.switch:checked { background: var(--ok); }
.switch:checked::after { right: 23px; }
.prompt-editor { min-height: 420px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.7; direction: rtl; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 10;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px);
}
.toast.err { background: var(--danger); color: #fff; }

/* ---------- الهاتف ---------- */
.thread-head, .lead-card, .composer { flex-shrink: 0; }

@media (max-width: 760px) {
  body { font-size: 14px; }
  .app { height: 100dvh; }
  .btn { padding: 6px 10px; font-size: 13px; }

  /* الشريط العلوي: سطر واحد */
  .topbar { padding: 8px 10px; gap: 8px; flex-wrap: nowrap; }
  .brand { font-size: 15px; }
  .brand-mark { width: 26px; height: 26px; font-size: 15px; }
  .brand .small, .badges { display: none; }
  .top-actions .btn { padding: 5px 9px; }

  /* الأرقام: صف واحد صغير، وتختفي داخل المحادثة */
  .stats { grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 8px 10px 0; }
  .stat { padding: 6px 4px; text-align: center; }
  .stat b { font-size: 17px; line-height: 1.3; }
  .stat span { font-size: 11px; display: block; line-height: 1.3; }
  .app:has(.chats.show-thread) .stats { display: none; }

  .chats { grid-template-columns: 1fr; padding: 8px; gap: 8px; }
  .chats.show-thread { padding: 0; }
  .chats.show-thread .thread-pane { border-radius: 0; border-inline: 0; border-bottom: 0; }
  .chats.show-thread .list-pane { display: none; }
  .chats:not(.show-thread) .thread-pane { display: none; }

  /* رأس المحادثة: الاسم والرجوع بسطر، والأزرار بسطر يتمرر */
  .thread-head { padding: 8px 10px; gap: 6px 8px; }
  .back { display: inline-flex; padding: 4px 10px; font-size: 16px; }
  .who { min-width: 0; }
  .who-name { font-size: 15px; }
  .who-meta { font-size: 12px; }
  .thread-actions { flex-basis: 100%; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .thread-actions::-webkit-scrollbar { display: none; }
  .thread-actions .btn { flex-shrink: 0; }

  /* ملخص الطلب: سطر واحد، والضغط يفتحه */
  .lead-card { margin: 6px 8px 0; padding: 6px 10px; font-size: 12px; flex-wrap: nowrap; overflow: hidden; white-space: nowrap; cursor: pointer; position: relative; padding-inline-end: 26px; }
  .lead-card > * { flex-shrink: 0; }
  .lead-card::after { content: '▾'; position: absolute; inset-inline-end: 9px; top: 6px; color: var(--warn); }
  .lead-card.open { flex-wrap: wrap; white-space: normal; max-height: 40vh; overflow-y: auto; }
  .lead-card.open::after { content: '▴'; }

  .messages { padding: 10px; gap: 6px; }
  .msg { max-width: 86%; padding: 6px 10px; }
  .msg-img { max-width: 200px; max-height: 200px; }

  /* مربع الكتابة */
  .composer { padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .composer textarea { min-height: 40px; max-height: 120px; resize: none; padding: 8px 10px; }
  .composer-row .btn { padding: 9px 14px; }

  .settings { padding: 10px; }
  .card { padding: 12px; }
  .prompt-editor { min-height: 300px; }
}

/* إعدادات رقمية + معاينة التقرير */
.num-input{width:80px;padding:6px 8px;border:1px solid var(--border);border-radius:8px;font:inherit;background:transparent;color:inherit;text-align:center}
.report-preview{margin-top:12px;padding:12px 14px;border:1px solid var(--border);border-radius:10px;line-height:1.8;font-size:14px}
