/**
 * ⚠️ **الإخفاء يسبق كل شيء — ولا يُحذف هذا السطر.**
 *
 * `[hidden]` في متصفّحك يأتي من ورقة أنماط المتصفّح، وأي قاعدة يكتبها
 * المؤلّف تغلبها. و`.conn { display: flex }` كانت تفعل ذلك بالضبط: الزرّ
 * يضبط `hidden = true` فعلاً، ولا يتغيّر شيء على الشاشة. فبدت لوحة الربط
 * «لا تُغلق» بينما الزرّ ومعالجه سليمان تماماً.
 *
 * ودرسٌ أعمّ: اختبار الإغلاق بـ`element.click()` ثم فحص `el.hidden` **يمرّ
 * على هذه العلّة** — لأنه يفحص الخاصية لا الظهور. ما يكشفها ضغطة حقيقية
 * ونظرة إلى الشاشة.
 */
[hidden] { display: none !important; }

:root {
  /* ── الأسطح: ثلاث درجات متمايزة، لا رماديات متقاربة ── */
  --bg: #0b0d12;
  --panel: #151922;
  --panel-2: #1c212c;
  --line: #2b323f;
  --line-soft: #222834;

  /* ── النصّ ── */
  --text: #e8ecf4;
  --muted: #8892a6;

  /* ── الدلالات: كل لون يعني شيئاً واحداً ── */
  --accent: #4c8bf5;
  --ok: #45c463;
  --warn: #e0a63a;
  --danger: #f2564d;

  /* ── فقاعات المحادثة: لكل طرف حافة ملوّنة تفرّقه من طرف العين ── */
  --client: #1d2b40;
  --client-edge: #4c8bf5;
  --agent: #16301f;
  --agent-edge: #45c463;

  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.7 "Segoe UI", Tahoma, system-ui, sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── الشريط العلوي ── */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; flex-direction: column; }
.brand strong { font-size: 16px; letter-spacing: .2px; }
.env { font-size: 11.5px; color: var(--warn); }

.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

button, select, input, textarea {
  font-family: inherit;
  font-size: 13px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 12px;
  transition: border-color .12s, background .12s;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); background: #232937; }
button.danger:hover { border-color: var(--danger); color: var(--danger); }
button:disabled { opacity: .45; cursor: not-allowed; }

.key-slot { display: flex; align-items: center; gap: 6px; }
.key-slot input { width: 230px; }
.key-slot .hint { font-size: 11px; color: var(--muted); }
.key-ok {
  color: var(--ok); font-size: 12.5px; font-weight: 600;
  background: #12301c; border: 1px solid #2a5c39;
  padding: 5px 11px; border-radius: 20px;
}

/* ── التخطيط ── */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 400px;
  min-height: 0;
}

/* ── المحادثة ── */
.chat { display: flex; flex-direction: column; min-height: 0; border-inline-start: 1px solid var(--line); }

.banner {
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}
.banner.red { background: #3d1418; color: #ffa198; }
.banner.amber { background: #3a2c0c; color: #e3b341; }

.messages { flex: 1; overflow-y: auto; padding: 22px 20px; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; color: var(--muted); text-align: center; }

.msg {
  max-width: 72%; padding: 11px 15px; border-radius: 14px;
  white-space: pre-wrap; word-break: break-word;
  font-size: 14.5px; line-height: 1.75;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.msg.client {
  align-self: flex-start; background: var(--client);
  border-inline-start: 3px solid var(--client-edge);
  border-start-start-radius: 4px;
}
.msg.agent {
  align-self: flex-end; background: var(--agent);
  border-inline-end: 3px solid var(--agent-edge);
  border-start-end-radius: 4px;
}
.msg.silent {
  align-self: center; background: transparent;
  border: 1px dashed var(--line); color: var(--muted);
  font-size: 12.5px; max-width: 88%; text-align: center; box-shadow: none;
}
.msg .tag {
  display: block; font-size: 10.5px; font-weight: 700;
  letter-spacing: .3px; opacity: .8; margin-bottom: 5px;
}
.msg.client .tag { color: var(--client-edge); }
.msg.agent .tag { color: var(--agent-edge); }

.composer { display: flex; gap: 10px; padding: 14px; border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea { flex: 1; resize: vertical; min-height: 46px; }
.composer button {
  padding-inline: 26px; background: var(--accent);
  border-color: var(--accent); color: #fff; font-weight: 600;
}
.composer button:hover { filter: brightness(1.1); background: var(--accent); }

.queue { padding: 8px 14px; font-size: 12px; color: var(--warn); border-top: 1px solid var(--line); }

/* ── التشخيص ── */
.debug { overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; background: #0e1116; }
.panel {
  background: var(--panel); border: 1px solid var(--line-soft);
  border-radius: var(--radius); overflow: hidden;
}
/* لوحة بلا محتوى لا تزاحم لوحة ممتلئة */
.panel.empty { opacity: .4; }
.panel.empty .body { padding: 6px 13px; }
.panel h3 {
  margin: 0; padding: 9px 13px; font-size: 12px; font-weight: 700;
  letter-spacing: .3px; color: var(--muted);
  background: var(--panel-2); border-bottom: 1px solid var(--line-soft);
}
.panel .body { padding: 11px 13px; font-size: 12.5px; }
.muted { color: var(--muted); }

.row { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; align-items: baseline; }
.row + .row { border-top: 1px solid var(--line-soft); }
.row .k { color: var(--muted); flex: 0 0 auto; }
.row .v { text-align: start; font-variant-numeric: tabular-nums; }

.pill { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.pill.ok { background: #12301c; color: var(--ok); }
.pill.red { background: #3d1418; color: #ffa198; }
.pill.amber { background: #3a2c0c; color: var(--warn); }
.pill.blue { background: #14243d; color: #79b8ff; }

.item { padding: 5px 0; }
.item + .item { border-top: 1px solid #21252e; }
.item .why { color: var(--muted); font-size: 11.5px; display: block; margin-top: 2px; }

/* ── ملف العميل: حقول لا فقرة ── */
.fields { display: flex; flex-direction: column; }
.field { display: flex; gap: 10px; padding: 5px 0; align-items: baseline; }
.field + .field { border-top: 1px solid var(--line-soft); }
.field .fk { color: var(--muted); flex: 0 0 90px; font-size: 11.5px; }
.field .fv { color: var(--text); }
.field.unset .fv { color: #5c6473; }

.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 5px; }
.chip {
  font-size: 11px; padding: 2px 9px; border-radius: 20px;
  background: var(--panel-2); border: 1px solid var(--line-soft); color: #6f7887;
}
.chip.on { background: #16301f; border-color: #2a5c39; color: var(--ok); }

.subhead {
  margin-top: 11px; padding-top: 9px; border-top: 1px solid var(--line-soft);
  font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .3px;
}
.notes { margin: 5px 0 0; padding-inline-start: 16px; }
.notes li { margin: 3px 0; color: #c3cbd9; line-height: 1.6; }

pre.profile {
  margin: 0; white-space: pre-wrap; font-family: inherit;
  font-size: 12px; line-height: 1.75; color: #c9d1d9;
}

.warnbox { margin-top: 8px; padding: 8px 10px; border-radius: 6px; background: #3a2c0c; color: #e3b341; font-size: 11.5px; }

/**
 * ⚠️ العرض الضيّق كان ينهار: العمودان يتراصّان فتنكمش المحادثة إلى شريط
 * والتشخيص يزحمها. والمحادثة هي الأداة — فتأخذ الأولوية، والتشخيص ينزل تحتها
 * بارتفاع محدود قابل للتمرير.
 */
@media (max-width: 1200px) {
  main { grid-template-columns: 1fr 340px; }
}

@media (max-width: 980px) {
  main { grid-template-rows: minmax(0, 1fr) auto; grid-template-columns: 1fr; }
  .chat { border-inline-start: 0; min-height: 0; }
  .debug {
    max-height: 40vh; border-top: 1px solid var(--line);
    display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    align-items: start;
  }
  .msg { max-width: 88%; }
  .bar { padding: 10px 12px; gap: 10px; }
}

/* ── لوحة الربط ── */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }

.conn {
  position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 21;
  width: 420px; max-width: 92vw;
  background: var(--panel); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.conn > header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel-2);
}
.conn > header button { border: none; background: none; font-size: 15px; padding: 2px 8px; }

.conn-body { overflow-y: auto; padding: 4px 16px 20px; }
.conn section { padding: 14px 0; }
.conn section + section { border-top: 1px solid var(--line); }
.conn h4 { margin: 0 0 8px; font-size: 13px; color: var(--text); }
.conn label { display: block; font-size: 11.5px; color: var(--muted); margin: 8px 0 4px; }
.conn input, .conn select, .conn textarea { width: 100%; }
.conn button { margin-top: 8px; }
.conn code { font-size: 11px; color: var(--accent); }
.note { margin: 8px 0 0; font-size: 11.5px; color: var(--muted); line-height: 1.6; }

.probe { margin-top: 10px; padding: 9px 11px; border-radius: 6px; font-size: 12.5px; line-height: 1.7; background: var(--panel-2); }
.probe.ok { background: #12301c; color: var(--ok); }
.probe.bad { background: #3d1418; color: #ffa198; }

.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.presets button { margin-top: 0; font-size: 12px; padding: 6px 9px; }

button.primary {
  background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
}
button.primary:hover { filter: brightness(1.12); }

.note a { color: var(--accent); }

/* لوح «متقدّم» — ينطوي كي لا يزاحم المسار الشائع */
.conn-body .adv {
  border: 1px solid var(--line, #2a2f3a);
  border-radius: 8px;
  padding: 0 12px;
  margin-top: 4px;
}
.conn-body .adv > summary {
  cursor: pointer;
  padding: 10px 0;
  font-weight: 600;
  font-size: 13px;
  opacity: .75;
  list-style: none;
}
.conn-body .adv > summary::before { content: "▸ "; }
.conn-body .adv[open] > summary::before { content: "▾ "; }
.conn-body .adv > summary:hover { opacity: 1; }
.conn-body .adv > section { border: 0; padding-top: 0; }

/* زر الربط بضغطة واحدة — الإجراء الأساسي في اللوحة، فيبدو كذلك */
.cta {
  width: 100%; padding: 11px 14px; font-size: 14px; font-weight: 700;
  color: #fff; background: var(--accent); border: 0; border-radius: 8px;
  cursor: pointer;
}
.cta:hover { filter: brightness(1.08); }
.cta:disabled { opacity: .6; cursor: default; }
#quickConnect { padding-top: 16px; }
#chatgptOut { margin-top: 10px; }

/* ── شاشة الدخول ── */
.login-wrap {
  position: fixed; inset: 0; z-index: 50;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  width: 320px; max-width: 90vw;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 26px 24px;
  display: flex; flex-direction: column; gap: 10px;
}
.login-card strong { font-size: 17px; }
.login-card input { width: 100%; }
.login-card .note { margin: 0 0 6px; }

/* ── لوحة التحكّم: الاختيارات والمؤقّتات ─────────────────────── */

/* الاختيار سطرٌ كامل قابل للنقر — لا دائرةٌ صغيرة تُخطئها الإصبع */
.row.pick { cursor: pointer; padding: 7px 4px; border-radius: 6px; align-items: center; }
.row.pick:hover { background: var(--panel2, rgba(255,255,255,.04)); }
.row.pick .k { flex: 1; }
.row.pick input[type="radio"] { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

/* الصندوق التابع للاختيار — يبهت حين لا يعني شيئاً */
#allowBox, #hoursBox { transition: opacity .15s; }
#allowBox[data-off], #hoursBox[data-off] { opacity: .35; pointer-events: none; }

.quickset {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  margin: 10px 0; padding: 8px; border-radius: 6px;
  background: var(--panel2, rgba(255,255,255,.03));
}
.quickset .k { font-size: 11.5px; color: var(--muted); }

.ghost {
  background: transparent; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 6px;
  padding: 4px 9px; font: inherit; font-size: 11.5px; cursor: pointer;
}
.ghost:hover { background: color-mix(in srgb, var(--accent) 14%, transparent); }

/* المؤقّت يُعرض بالأرقام اللاتينية دائماً — الساعة ليست نصّاً عربياً */
input[type="time"] {
  font: inherit; font-size: 12.5px; direction: ltr; text-align: center;
  padding: 4px 6px; border-radius: 6px;
  background: var(--input, rgba(255,255,255,.06));
  color: var(--fg); border: 1px solid var(--line, rgba(255,255,255,.12));
}
input[type="time"]:disabled { opacity: .4; }

.hd-row { display: flex; align-items: center; gap: 6px; padding: 3px 0; }
.hd-row .day { flex: 1; font-size: 12px; }
.hd-row .closed { font-size: 11px; color: var(--muted); display: flex; gap: 4px; align-items: center; cursor: pointer; }

/* جدول حقول الويبهوك — ما يُختار عندهم حرفاً بحرف */
.hookfields { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 12px; }
.hookfields td { padding: 5px 7px; border-bottom: 1px solid var(--line, rgba(255,255,255,.08)); }
.hookfields td:first-child {
  width: 78px; color: var(--muted); font-family: ui-monospace, monospace;
  font-size: 11.5px; direction: ltr; text-align: left;
}

/* ── إضافة رقم: الدولة من قائمة، والباقي أرقام ─────────────── */

.phone-add { display: flex; gap: 6px; align-items: center; margin: 6px 0; }
.phone-add select { flex: 0 0 auto; max-width: 45%; }
.phone-add input { flex: 1; direction: ltr; text-align: left; }

#allowChips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 4px; }
/* ⚠️ محصورة بالحاوية — `.chip` مستعمَل أصلاً لرقائق أسئلة الملف */
#allowChips .chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 8px; border-radius: 999px; font-size: 12px;
  direction: ltr; font-family: ui-monospace, monospace;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}
#allowChips .chip button {
  background: none; border: 0; color: var(--muted);
  cursor: pointer; font-size: 14px; line-height: 1; padding: 0;
}
#allowChips .chip button:hover { color: var(--warn, #e66); }
