/* ========================================================================
   OutWhatsApp Pro — Design System (احترافي، داكن، RTL)
   ======================================================================== */
:root {
  /* الأسطح (تدرّج عمق) */
  --bg: #0a0f14;
  --bg-grad: radial-gradient(1200px 600px at 100% -10%, rgba(0,168,132,.10), transparent 60%),
             radial-gradient(900px 500px at -10% 110%, rgba(37,211,102,.06), transparent 55%);
  --surface: #0f1820;
  --surface-2: #16222b;
  --surface-3: #1d2c36;
  --elevated: #18242d;
  --border: #25333d;
  --border-soft: #1c2731;

  /* النص */
  --text: #e9edef;
  --text-2: #aebac4;
  --muted: #6b7d89;

  /* العلامة التجارية */
  --accent: #00a884;
  --accent-2: #25d366;
  --accent-ink: #04241c;
  --accent-glow: rgba(0,168,132,.35);

  /* حالات دلالية */
  --danger: #f15c6d;
  --danger-bg: rgba(241,92,109,.14);
  --warn: #f0b232;
  --warn-bg: rgba(240,178,50,.14);
  --ok: #25d366;
  --ok-bg: rgba(37,211,102,.14);
  --info: #53bdeb;
  --info-bg: rgba(83,189,235,.14);

  /* مقاسات */
  --r-sm: 8px;
  --r: 12px;
  --r-lg: 16px;
  --r-xl: 22px;
  --r-full: 999px;

  /* ظلال (مقياس ارتفاع موحّد) */
  --sh-1: 0 1px 2px rgba(0,0,0,.3);
  --sh-2: 0 4px 14px rgba(0,0,0,.35);
  --sh-3: 0 12px 32px rgba(0,0,0,.45);
  --sh-glow: 0 6px 22px rgba(0,168,132,.28);

  /* حركة */
  --t-fast: .15s cubic-bezier(.4,0,.2,1);
  --t: .22s cubic-bezier(.4,0,.2,1);
  --sidebar-w: 256px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  background-image: var(--bg-grad);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', Tahoma, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { cursor: pointer; text-decoration: none; color: inherit; }
.muted { color: var(--muted); }
::selection { background: var(--accent-glow); }

/* أرقام جدولية للبيانات والإحصاءات */
.num, td, th, .card-num, .stat-num, .time { font-variant-numeric: tabular-nums; }

/* شريط تمرير راقٍ */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: var(--r-full); border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: #2b3d49; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ============================ أزرار ============================ */
button, .btn {
  font-family: inherit; font-size: 14px; font-weight: 600;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #fff; border: 0; padding: 10px 18px; border-radius: var(--r-sm);
  cursor: pointer; display: inline-flex; align-items: center; gap: 8px;
  transition: transform var(--t-fast), box-shadow var(--t-fast), filter var(--t-fast);
  box-shadow: var(--sh-1);
}
button:hover, .btn:hover { transform: translateY(-1px); box-shadow: var(--sh-glow); }
button:active, .btn:active { transform: translateY(0); }
button:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
button.ghost { background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2); box-shadow: none; }
button.ghost:hover { background: var(--surface-3); color: var(--text); box-shadow: var(--sh-1); }
button.danger { background: linear-gradient(135deg, #ff6b7d, var(--danger)); }
button.small { padding: 6px 12px; font-size: 13px; }
button.icon { padding: 8px; border-radius: var(--r-sm); }

/* ============================ حقول الإدخال ============================ */
input, textarea, select {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 11px 14px; border-radius: var(--r-sm); font-family: inherit; font-size: 14px; width: 100%;
  transition: border-color var(--t-fast), box-shadow var(--t-fast), background var(--t-fast);
}
input::placeholder, textarea::placeholder { color: var(--muted); }
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent); background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
textarea { min-height: 96px; resize: vertical; line-height: 1.6; }
select { cursor: pointer; }

/* ============================ تسجيل الدخول ============================ */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card {
  background: linear-gradient(180deg, var(--elevated), var(--surface));
  padding: 40px 34px; border-radius: var(--r-xl); width: 360px; max-width: 92vw;
  display: flex; flex-direction: column; gap: 16px;
  border: 1px solid var(--border); box-shadow: var(--sh-3);
  position: relative; overflow: hidden;
}
.login-card::before {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 3px;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
}
.login-card .logo { font-size: 24px; font-weight: 800; text-align: center; letter-spacing: -.5px; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; text-align: center; }
.logo span { color: var(--accent-2); }

/* ============================ التخطيط ============================ */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w); background: linear-gradient(180deg, var(--surface), #0c141b);
  border-left: 1px solid var(--border); padding: 22px 16px;
  display: flex; flex-direction: column; gap: 6px;
  position: sticky; top: 0; height: 100vh;
}
.sidebar .logo { font-size: 19px; font-weight: 800; padding: 6px 10px 20px; letter-spacing: -.5px; display: flex; align-items: center; gap: 8px; }
.sidebar .logo span { color: var(--accent-2); }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
nav a {
  padding: 11px 14px; border-radius: var(--r-sm); color: var(--text-2);
  display: flex; align-items: center; gap: 12px; font-weight: 500;
  transition: background var(--t-fast), color var(--t-fast); position: relative;
}
nav a svg { width: 19px; height: 19px; flex: 0 0 auto; opacity: .85; }
nav a:hover { background: var(--surface-2); color: var(--text); }
nav a.active { background: linear-gradient(135deg, rgba(0,168,132,.18), rgba(37,211,102,.08)); color: #fff; }
nav a.active::before {
  content: ''; position: absolute; right: 0; top: 50%; transform: translateY(-50%);
  width: 3px; height: 20px; border-radius: var(--r-full); background: var(--accent-2);
}
nav a.active svg { opacity: 1; color: var(--accent-2); }
.logout { margin-top: 8px; background: transparent; border: 1px solid var(--border); color: var(--muted); justify-content: center; }
.logout:hover { background: var(--danger-bg); border-color: var(--danger); color: var(--danger); box-shadow: none; }

.content { flex: 1; padding: 26px 30px; max-width: 100%; overflow-x: hidden; }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 26px; gap: 14px; flex-wrap: wrap; }
.topbar h1 { font-size: 24px; margin: 0; font-weight: 700; letter-spacing: -.5px; }
.session-pick { display: flex; align-items: center; gap: 10px; background: var(--surface-2); padding: 6px 8px 6px 14px; border-radius: var(--r-full); border: 1px solid var(--border); }
.session-pick label { font-size: 13px; color: var(--muted); }
.session-pick select { width: auto; min-width: 150px; border: 0; background: transparent; padding: 4px 8px; border-radius: var(--r-sm); }
.session-pick select:focus { box-shadow: none; }

/* ============================ بطاقات الإحصاء ============================ */
.cards { display: grid; grid-template-columns: repeat(6, 1fr); gap: 16px; margin-bottom: 26px; }
@media (max-width: 1200px) { .cards { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 600px) { .cards { grid-template-columns: repeat(2, 1fr); } }
.card {
  background: linear-gradient(160deg, var(--elevated), var(--surface));
  border: 1px solid var(--border); border-radius: var(--r-lg); padding: 20px;
  position: relative; overflow: hidden; transition: transform var(--t), box-shadow var(--t), border-color var(--t);
}
.card:hover { transform: translateY(-3px); box-shadow: var(--sh-2); border-color: #2f4250; }
.card::after {
  content: ''; position: absolute; left: -30%; top: -60%; width: 120px; height: 120px;
  background: radial-gradient(circle, var(--accent-glow), transparent 70%); opacity: .5; pointer-events: none;
}
.card .ico { width: 38px; height: 38px; border-radius: var(--r-sm); display: grid; place-items: center; margin-bottom: 14px; background: var(--accent-ink); color: var(--accent-2); }
.card .ico svg { width: 20px; height: 20px; }
.card .num { font-size: 30px; font-weight: 800; line-height: 1; letter-spacing: -1px; }
.card .label { color: var(--text-2); margin-top: 6px; font-size: 13px; }

/* ============================ ألواح ============================ */
.panel { background: linear-gradient(180deg, var(--surface), var(--bg)); border: 1px solid var(--border); border-radius: var(--r-lg); padding: 22px; margin-bottom: 18px; box-shadow: var(--sh-1); }
.panel h3 { margin: 0 0 18px; font-size: 16px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.panel h3 svg { color: var(--accent-2); }
.count-pill { background: var(--accent-ink); color: var(--accent-2); font-size: 12px; font-weight: 700; padding: 2px 10px; border-radius: var(--r-full); }
.lead-details { font-size: 13px; line-height: 1.7; max-width: 420px; }
.lead-details div { margin: 1px 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.field { display: flex; flex-direction: column; gap: 7px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--text-2); font-weight: 500; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ============================ جداول ============================ */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: right; padding: 12px 14px; border-bottom: 1px solid var(--border-soft); }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tbody tr { transition: background var(--t-fast); }
tbody tr:hover td { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }

/* ============================ شارات ============================ */
.badge { padding: 4px 11px; border-radius: var(--r-full); font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.connected { background: var(--ok-bg); color: var(--ok); }
.badge.qr, .badge.connecting { background: var(--warn-bg); color: var(--warn); }
.badge.disconnected, .badge.loggedout { background: var(--danger-bg); color: var(--danger); }

/* ============================ QR / اقتران ============================ */
.qr-box { text-align: center; padding: 24px; }
.qr-box img { background: #fff; padding: 14px; border-radius: var(--r-lg); width: 280px; max-width: 80vw; box-shadow: var(--sh-2); }
.pairing-code { font-size: 40px; letter-spacing: 8px; font-weight: 800; margin: 18px 0; color: var(--accent-2); font-variant-numeric: tabular-nums; }

/* ============================ صندوق المحادثات ============================ */
.inbox { display: grid; grid-template-columns: 320px 1fr; gap: 16px; height: calc(100vh - 140px); min-height: 0; }
.conv-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); overflow-y: auto; min-height: 0; }
.conv-item { padding: 14px 16px; border-bottom: 1px solid var(--border-soft); cursor: pointer; transition: background var(--t-fast); display: flex; flex-direction: column; gap: 3px; }
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: linear-gradient(135deg, rgba(0,168,132,.15), transparent); box-shadow: inset 3px 0 0 var(--accent-2); }
.conv-item .name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.conv-item .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.chat-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; flex: 0 0 auto; background: var(--surface-2); }
.chat-body {
  flex: 1 1 auto; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 10px; min-height: 0;
  background-image: radial-gradient(rgba(255,255,255,.012) 1px, transparent 1px); background-size: 22px 22px;
}
.bubble { max-width: 72%; padding: 10px 14px; border-radius: var(--r-lg); line-height: 1.55; white-space: pre-wrap; word-break: break-word; box-shadow: var(--sh-1); animation: bubbleIn .2s ease-out; }
@keyframes bubbleIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.bubble.in { background: var(--surface-3); align-self: flex-start; border-bottom-right-radius: 4px; }
.bubble.out { background: linear-gradient(135deg, var(--accent), #00936f); align-self: flex-end; border-bottom-left-radius: 4px; }
.bubble .time { font-size: 10px; color: rgba(255,255,255,.5); margin-top: 5px; }
.chat-input { flex: 0 0 auto; padding: 14px; border-top: 1px solid var(--border); display: flex; gap: 10px; background: var(--surface-2); }
.chat-input input { flex: 1; border-radius: var(--r-full); }
.chat-input select { width: auto; min-width: 52px; border-radius: var(--r-full); }
.chat-input button { border-radius: var(--r-full); }
/* محادثة التدريب الداخلية — صندوق دردشة بارتفاع ثابت قابل للتمرير */
.train-chat {
  height: 380px; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-lg);
}
.train-chat .bubble.muted { opacity: .6; }

.meta-chips { font-size: 12px; color: var(--muted); margin-right: 8px; display: inline-flex; gap: 6px; align-items: center; }
.tag { background: var(--accent-ink); border: 1px solid rgba(37,211,102,.25); color: var(--accent-2); padding: 2px 9px; border-radius: var(--r-full); font-size: 11px; font-weight: 600; display: inline-block; margin: 1px; }

/* ============================ مخطط بياني (SVG) ============================ */
.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; max-height: 260px; }
.chart .grid-line { stroke: var(--border-soft); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .bar { transition: opacity var(--t-fast); cursor: pointer; }
.chart .bar:hover { opacity: .82; }
.legend { display: flex; gap: 20px; margin-top: 14px; font-size: 13px; color: var(--text-2); justify-content: center; }
.legend span { display: inline-flex; align-items: center; gap: 7px; }
.legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

/* ============================ توست ============================ */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--elevated); color: var(--text); padding: 13px 22px; border-radius: var(--r);
  opacity: 0; transition: opacity var(--t), transform var(--t); pointer-events: none; z-index: 1000;
  box-shadow: var(--sh-3); border: 1px solid var(--border); border-right: 3px solid var(--accent-2); font-weight: 500;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { border-right-color: var(--danger); }

/* ============================ حالات فارغة / أكواد ============================ */
.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.empty svg { width: 44px; height: 44px; opacity: .4; margin-bottom: 12px; }
code.key { background: var(--surface-3); padding: 10px 14px; border-radius: var(--r-sm); word-break: break-all; display: block; margin: 8px 0; font-family: 'Consolas', monospace; color: var(--accent-2); border: 1px dashed var(--border); }

/* ============================ هيكل تحميل (skeleton) ============================ */
.skeleton { background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.3s infinite; border-radius: var(--r-sm); }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ============================ استجابة ============================ */
@media (max-width: 860px) {
  /* الشريط الجانبي يصبح شريطاً علوياً أفقياً قابلاً للتمرير — يوفّر كامل العرض للمحتوى */
  .app { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 50;
    flex-direction: row; align-items: center; gap: 4px;
    padding: 8px 12px; border-left: 0; border-bottom: 1px solid var(--border);
  }
  .sidebar .logo { padding: 0 6px; font-size: 16px; flex: 0 0 auto; }
  .sidebar .logo span { display: inline; }
  nav { flex-direction: row; gap: 2px; flex: 1; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
  nav a { justify-content: center; padding: 10px 12px; flex: 0 0 auto; }
  nav a span { display: none; }
  nav a.active::before { display: none; }
  nav a.active { box-shadow: inset 0 -3px 0 var(--accent-2); }
  .logout { margin-top: 0; width: auto; flex: 0 0 auto; padding: 10px 12px; }
  .logout span { display: none; }

  .content { padding: 16px 14px; }
  .topbar { margin-bottom: 18px; }
  .topbar h1 { font-size: 20px; }
  .session-pick { width: 100%; justify-content: space-between; }
  .grid2 { grid-template-columns: 1fr; }
  .panel { padding: 16px; }

  /* جداول قابلة للتمرير أفقياً بدل كسر التخطيط على الشاشات الضيّقة */
  .panel table { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  .lead-details { max-width: none; white-space: normal; }

  /* المحادثات: القائمة أعلى والدردشة أسفل بارتفاعات مناسبة للمس */
  .inbox { grid-template-columns: 1fr; height: auto; gap: 12px; }
  .conv-list { max-height: 38vh; }
  .chat { height: 64vh; }

  .train-chat { height: 52vh; }
  .pairing-code { font-size: 30px; letter-spacing: 5px; }

  /* 16px يمنع iOS من تكبير الصفحة تلقائياً عند لمس الحقول */
  input, textarea, select { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
