:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --card: #1e293b;
  --card-border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #22c55e;
  --primary-dark: #16a34a;
  --danger: #ef4444;
  --input-bg: #0f172a;
  --input-border: #475569;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Tajawal", "Segoe UI", "Tahoma", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* تعميم خط تجوال على كل العناصر بما فيها الحقول والأزرار */
body, input, textarea, select, button, .tab, .btn, .pill, code, h1, h2 {
  font-family: "Tajawal", "Segoe UI", "Tahoma", system-ui, sans-serif;
}

/* ── الشريط العلوي ── */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 18px;
  padding: 10px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--card-border);
  position: sticky;
  top: 0;
  z-index: 20;
}
/* شريط التنقّل داخل النافبار (يسار) — بشكل segmented */
.topnav {
  display: flex; gap: 2px; flex-wrap: wrap;
  margin-inline-start: auto;   /* يدفعها لليسار دائماً (حتى عند اللفّ) */
  background: var(--card); border: 1px solid var(--card-border);
  border-radius: 10px; padding: 3px;
}
.topnav .tab { padding: 8px 16px; border-radius: 7px; border-bottom: none; }
.topnav .tab:hover:not(.active) { color: var(--text); }
.topnav .tab.active { background: var(--primary); color: #052e16; border-bottom: none; }
.brand { font-size: 20px; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.toast { color: var(--primary); font-size: 14px; min-width: 60px; }

/* ── التبويبات ── */
.tabs {
  display: flex;
  gap: 4px;
  padding: 0 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--card-border);
  overflow-x: auto;
}
.tab {
  background: none;
  border: none;
  color: var(--muted);
  padding: 14px 18px;
  font-size: 15px;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ── المحتوى ── */
main { max-width: 1600px; margin: 0 auto; padding: 22px; }
/* الإعدادات بعرض مريح للقراءة، والمحادثات/الإدارة تأخذ العرض كامل */
.panel[data-panel="general"] { max-width: 1000px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
}
.card h2 { margin: 0 0 16px; font-size: 17px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.card-head h2 { margin: 0; }

/* ── الحقول ── */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); }
.field.checkbox { flex-direction: row-reverse; justify-content: flex-end; align-items: center; gap: 10px; }
.field.checkbox > span { color: var(--text); }

/* يشمل كل أنواع الحقول تلقائياً (عدا checkbox/radio) — لا حاجة لتعداد الأنواع */
input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
  width: 100%;
  color-scheme: dark;   /* ليظهر منتقي التاريخ/الوقت الأصلي بمظهر داكن */
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
textarea { resize: vertical; }
textarea.mono { font-family: "Tajawal", "SF Mono", Menlo, monospace; font-size: 14px; }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--primary); cursor: pointer; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
@media (max-width: 640px) { .grid2 { grid-template-columns: 1fr; } }

.subgroup { border: 1px solid var(--card-border); border-radius: 10px; padding: 14px 16px; margin-top: 8px; }
.subgroup legend { padding: 0 8px; color: var(--muted); font-size: 13px; }

/* ── الأزرار ── */
.btn {
  background: #334155;
  color: var(--text);
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.15); }
.btn-primary { background: var(--primary); color: #052e16; font-weight: 700; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-small { padding: 6px 12px; font-size: 13px; }
.btn-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 8px; }

/* ── الحالة ── */
.pill { padding: 5px 14px; border-radius: 999px; font-size: 13px; font-weight: 600; }
.pill-off { background: #7f1d1d; color: #fecaca; }
.pill-on { background: #14532d; color: #bbf7d0; }
.pill-wait { background: #78350f; color: #fde68a; }

.hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.hint code, .hint b { color: var(--text); }

.qr-wrap { text-align: center; margin: 10px 0; }
.qr-wrap img { background: #fff; padding: 10px; border-radius: 10px; width: 280px; max-width: 100%; }
.info-box { display: inline-block; width: auto; max-width: 100%; background: #14532d; color: #bbf7d0; padding: 8px 14px; border-radius: 8px; font-size: 13px; }
.error-box { background: #7f1d1d; color: #fecaca; padding: 12px; border-radius: 8px; }
.test-result { font-size: 13px; color: var(--muted); }

.kb-head { display: flex; justify-content: space-between; align-items: center; margin: 8px 0; font-size: 13px; color: var(--muted); }

/* ── السجل ── */
.log-list { display: flex; flex-direction: column; gap: 10px; max-height: 65vh; overflow-y: auto; }
.log-item { border: 1px solid var(--card-border); border-radius: 10px; padding: 12px; font-size: 14px; }
.log-item .meta { color: var(--muted); font-size: 12px; margin-bottom: 6px; display: flex; gap: 12px; flex-wrap: wrap; }
.log-item .in { color: #93c5fd; }
.log-item .out { color: #86efac; margin-top: 4px; }
.log-item .badge { background: #334155; border-radius: 6px; padding: 1px 8px; font-size: 11px; }

/* ── شارة الحالة في النافبار ── */
.icon { vertical-align: -3px; flex-shrink: 0; }
button .icon, .tab .icon, h2 .icon, .card-head .icon { margin-inline-end: 5px; }
.brand { display: flex; align-items: center; gap: 12px; }
.user-menu-wrap { position: relative; }
.user-dropdown {
  position: absolute; top: calc(100% + 8px); left: 0; min-width: 190px;
  background: var(--card); border: 1px solid var(--card-border); border-radius: 10px;
  padding: 6px; z-index: 30; box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
.user-dropdown-name { padding: 8px 10px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--card-border); margin-bottom: 4px; }
.user-dropdown-item {
  display: block; width: 100%; text-align: right; background: none; border: none;
  color: var(--text); font-family: inherit; font-size: 14px; padding: 9px 10px; border-radius: 7px; cursor: pointer;
}
.user-dropdown-item:hover { background: var(--input-bg); }
.user-dropdown-item.danger { color: var(--danger); }
.user-dropdown hr { border: none; border-top: 1px solid var(--card-border); margin: 6px 2px; }
.nav-status { font-size: 12px; font-weight: 600; }

/* ── الدردشة (شبيه واتساب ويب) ── */
/* تبديل القناة أعلى المحادثات */
.chan-switch {
  display: inline-flex; gap: 2px; margin-bottom: 12px;
  background: var(--card); border: 1px solid var(--card-border); border-radius: 10px; padding: 3px;
}
.chan-tab {
  background: none; border: none; color: var(--muted); cursor: pointer; font-family: inherit;
  padding: 8px 22px; border-radius: 7px; font-size: 14px;
}
.chan-tab:hover:not(.active) { color: var(--text); }
.chan-tab.active { background: var(--primary); color: #052e16; font-weight: 700; }

/* المحادثات بعرض كامل (قائمة + نافذة محادثة) */
.chat-wrap {
  display: flex;
  width: 100%;
  height: calc(100vh - var(--topbar-h, 62px) - 105px);
  min-height: 440px;
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--card);
}
/* في RTL: أول عنصر (القائمة) يظهر على اليمين */
.chat-list {
  width: 300px;
  flex-shrink: 0;
  border-inline-start: 1px solid var(--card-border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
}
.chat-search { padding: 12px; border-bottom: 1px solid var(--card-border); }
.chat-search input[type="search"] {
  width: 100%;
  background: var(--bg) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='11' cy='11' r='7'/><line x1='21' y1='21' x2='16.65' y2='16.65'/></svg>") no-repeat;
  background-position: right 12px center;
  border: 1px solid var(--input-border);
  border-radius: 10px;
  padding: 10px 40px 10px 14px;
  font-size: 13px;
}
.chat-search input::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }
#chatItems { flex: 1; overflow-y: auto; }

/* عمود حالة واتساب — كرت مستقل على أقصى اليسار */
.wa-side {
  width: 250px;
  flex-shrink: 0;
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  background: var(--card);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}
.wa-side h3 { margin: 0; font-size: 15px; }
.wa-side .btn { width: 100%; }
.wa-side .qr-wrap { margin: 0; }
.wa-side .qr-wrap img { width: 100%; max-width: 200px; }
.wa-side .info-box, .wa-side .error-box { font-size: 13px; }

.ci-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.ci-note { display: inline-block; background: #1e3a5f; color: #bfdbfe; font-size: 10px; border-radius: 6px; padding: 1px 6px; }

/* على الشاشات الأضيق: قلّص اللوحات الجانبية ليتّسع مكان المحادثة */
@media (max-width: 1000px) {
  .wa-side { width: 200px; padding: 12px; }
  .chat-list { width: 250px; }
}
/* على الجوال: رصّ عمود الحالة فوق الدردشة */
@media (max-width: 760px) {
  .wa-layout { flex-direction: column; height: auto; }
  .wa-side { width: auto; }
  .chat-wrap { height: 70vh; }
}
.chat-main { min-width: 220px; }
.chat-item { padding: 12px 14px; border-bottom: 1px solid var(--card-border); cursor: pointer; }
.chat-item:hover { background: var(--card); }
.chat-item.active { background: var(--card); border-inline-end: 3px solid var(--primary); }
.ci-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.ci-name { font-weight: 600; font-size: 14px; }
.ci-unread { background: var(--primary); color: #052e16; border-radius: 999px; font-size: 11px; font-weight: 700; padding: 0 7px; }
.ci-preview { color: var(--muted); font-size: 12px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-paused { display: inline-block; margin-top: 4px; background: #78350f; color: #fde68a; font-size: 10px; border-radius: 6px; padding: 1px 6px; }

.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--card-border); background: var(--panel);
}
.ch-info { display: flex; flex-direction: column; }
.ch-info b { font-size: 15px; }
.ch-info span { color: var(--muted); font-size: 12px; }
.ch-actions { display: flex; gap: 8px; }
.btn-note-on { background: #1e3a5f; color: #bfdbfe; }

.chat-note {
  padding: 8px 16px; background: #1e3a5f; color: #dbeafe;
  font-size: 13px; border-bottom: 1px solid var(--card-border);
}

/* نافذة الملاحظات */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal {
  background: var(--card); border: 1px solid var(--card-border);
  border-radius: var(--radius); padding: 20px; width: 90%; max-width: 460px;
}
.modal h3 { margin: 0 0 6px; }
.modal textarea { width: 100%; margin: 10px 0; }

.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 2px;
  background: var(--bg);
}
.msg {
  max-width: 72%; padding: 7px 12px 5px; border-radius: 12px;
  margin: 3px 0; font-size: 14px; word-wrap: break-word; white-space: pre-wrap;
  position: relative;
}
/* رد آلي من البوت: أخضر */
.msg.bot { align-self: flex-end; background: var(--primary-dark); color: #eafff0; border-bottom-left-radius: 3px; }
/* ردك اليدوي: أزرق مميّز */
.msg.me { align-self: flex-end; background: #1d4ed8; color: #eff6ff; border-bottom-left-radius: 3px; }
/* وارد من العميل: رمادي */
.msg.them { align-self: flex-start; background: #334155; color: var(--text); border-bottom-right-radius: 3px; }
.msg-tag { display: block; font-size: 10px; font-weight: 700; opacity: 0.85; margin-bottom: 2px; }
.msg-time { display: block; font-size: 10px; opacity: 0.65; margin-top: 3px; text-align: left; }

.chat-input { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--card-border); background: var(--panel); }
.chat-input input { flex: 1; }

/* مصادر الروابط */
.src-row { border: 1px solid var(--card-border); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
.src-row .grid-src { display: grid; grid-template-columns: 1fr 2fr; gap: 8px 12px; }
@media (max-width: 640px) { .src-row .grid-src { grid-template-columns: 1fr; } }
.src-row .src-mini { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.src-row .src-mini label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.src-row .src-mini input[type="number"] { width: 90px; }
.src-row .src-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; gap: 10px; }
.src-state { font-size: 12px; }
.src-state.ok { color: var(--primary); }
.src-state.bad { color: var(--danger); }
.src-del { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 13px; }

/* ── تبويب الإدارة ── */
.subnav {
  display: flex; gap: 8px; flex-wrap: wrap;
  position: sticky; top: var(--topbar-h, 62px); z-index: 15;
  background: var(--bg); padding: 10px 0; margin-bottom: 8px;
}
.subtab, .set-tab {
  background: var(--card); border: 1px solid var(--card-border); color: var(--muted);
  padding: 9px 16px; border-radius: 999px; cursor: pointer; font-family: inherit; font-size: 14px;
}
.subtab:hover, .set-tab:hover { color: var(--text); }
.subtab.active, .set-tab.active { background: var(--primary); color: #052e16; font-weight: 700; border-color: var(--primary); }

/* أقسام الإعدادات + زر الحفظ العائم */
.set-panel { display: none; }
.set-panel.active { display: block; }
.fab-save {
  position: fixed; bottom: 24px; left: 24px; z-index: 40;
  padding: 13px 22px; font-size: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.subbadge { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 7px; font-size: 11px; font-weight: 700; }
.subpanel { display: none; }
.subpanel.active { display: block; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--card); border: 1px solid var(--card-border); border-radius: var(--radius); padding: 16px; }
.tile .num { font-size: 26px; font-weight: 700; color: var(--primary); }
.tile .lbl { color: var(--muted); font-size: 13px; margin-top: 4px; }

.barchart { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 90px 1fr 44px; align-items: center; gap: 10px; font-size: 13px; }
.bar-row .bar-track { background: var(--input-bg); border-radius: 6px; overflow: hidden; height: 18px; }
.bar-row .bar-fill { background: var(--primary); height: 100%; border-radius: 6px; }
.bar-row .bar-val { color: var(--muted); text-align: left; }

.attn-item, .oos-item { border: 1px solid var(--card-border); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
.attn-item .meta, .oos-item .meta { color: var(--muted); font-size: 12px; display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.attn-item .who { font-weight: 600; color: var(--text); }
.attn-item .reason { background: #78350f; color: #fde68a; border-radius: 6px; padding: 1px 8px; font-size: 11px; }
.attn-item .msg { color: #93c5fd; }
.attn-item .foot, .oos-item .foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; gap: 10px; }

.ci-cat { display: inline-block; background: #3730a3; color: #c7d2fe; font-size: 10px; border-radius: 6px; padding: 1px 6px; }

/* ── تذاكر الدعم ── */
.ticket-toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.ticket-toolbar input[type="search"] { flex: 1; min-width: 200px; width: auto; }
.ticket-toolbar select { width: auto; min-width: 140px; flex: 0 0 auto; }
.ticket-toolbar button { flex: 0 0 auto; }
@media (max-width: 720px) { .ticket-toolbar { flex-direction: column; align-items: stretch; } .ticket-toolbar select { width: 100%; } }
.ticket-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.ticket-table th { text-align: right; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--card-border); }
.ticket-table td { padding: 10px; border-bottom: 1px solid var(--card-border); }
.ticket-table tbody tr { cursor: pointer; }
.ticket-table tbody tr:hover { background: var(--input-bg); }
.tk-status { display: inline-block; border-radius: 999px; padding: 2px 10px; font-size: 11px; font-weight: 700; }
.tk-status.open { background: #78350f; color: #fde68a; }
.tk-status.in_progress { background: #1e3a5f; color: #bfdbfe; }
.tk-status.resolved { background: #14532d; color: #bbf7d0; }
.tk-status.closed { background: #334155; color: #cbd5e1; }
.ticket-meta { color: var(--muted); font-size: 13px; margin-bottom: 12px; display: flex; gap: 14px; flex-wrap: wrap; }
.ticket-thread { max-height: 420px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; margin: 12px 0; padding: 8px; background: var(--bg); border-radius: 10px; }
#ticketDetailView .card-head { gap: 14px; }
#ticketDetailView .card-head h2 { margin: 0; }

.hidden { display: none !important; }
