:root {
  --bg: #f4f4f8;
  --panel: #ffffff;
  --ink: #1d2520;
  --muted: #6b756f;
  --line: #e3e6e1;
  --brand: #4f46e5;
  --brand-dark: #3730a3;
  --brand-soft: #eef2ff;
  --danger: #c43d3d;
  --warn: #b7791f;
  --bubble-in: #ffffff;
  --bubble-out: #e0e7ff;
  --bubble-bot: #fef6e4;
  --chat-bg: #eeedf4;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'IBM Plex Sans Arabic', 'Segoe UI', Tahoma, sans-serif;
  -webkit-tap-highlight-color: transparent;
  background: var(--bg); color: var(--ink); font-size: 14px;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- أزرار وحقول ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel); padding: 8px 14px; border-radius: 9px;
  cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; transition: background .15s;
}
.btn:hover { background: #f6f7f5; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=password], input[type=time], input[type=number], input[type=tel], input[type=date], input[type=email], select, textarea {
  border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; background: #fff; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea { resize: vertical; min-height: 80px; line-height: 1.6; }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-weight: 600; margin-bottom: 6px; font-size: 13px; }
label.field small { color: var(--muted); font-weight: 400; display: block; margin-top: 4px; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch i { width: 36px; height: 20px; background: #cfd5d0; border-radius: 20px; position: relative; transition: .2s; flex: none; }
.switch i::after { content: ''; position: absolute; top: 2px; inset-inline-start: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + i { background: var(--brand); }
.switch input:checked + i::after { inset-inline-start: 18px; }

/* ---------- تسجيل الدخول ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 20% 10%, #e0e7ff, transparent 50%), var(--bg); }
.login-card { background: var(--panel); padding: 36px; border-radius: 18px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 24px; color: var(--muted); }
.logo { width: 48px; height: 48px; border-radius: 14px; background: var(--brand); display: grid; place-items: center; color: #fff; font-size: 24px; margin-bottom: 16px; }
.err { color: var(--danger); min-height: 20px; font-size: 13px; margin-bottom: 8px; }

/* ---------- الهيكل ---------- */
.shell { display: grid; grid-template-columns: 72px 1fr; height: 100vh; }
.rail { background: #1e1b4b; display: flex; flex-direction: column; align-items: center; padding: 14px 0; gap: 6px; }
.rail .logo { width: 42px; height: 42px; font-size: 20px; margin-bottom: 14px; }
.rail button {
  width: 54px; padding: 8px 0 6px; border: 0; background: transparent; color: #9fb5a8; border-radius: 10px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 10.5px; position: relative;
}
.rail button svg { width: 22px; height: 22px; }
.rail button:hover { background: rgba(255,255,255,.06); color: #fff; }
.rail button.active { background: rgba(255,255,255,.12); color: #fff; }
.rail .spacer { flex: 1; }
.rail .badge { position: absolute; top: 2px; inset-inline-end: 6px; }
.badge { background: var(--brand); color: #fff; border-radius: 20px; font-size: 11px; min-width: 19px; height: 19px; padding: 0 5px; display: inline-grid; place-items: center; font-weight: 600; }
.main { overflow: hidden; display: flex; flex-direction: column; }

/* ---------- صندوق الوارد ---------- */
.inbox { display: grid; grid-template-columns: 360px 1fr; height: 100vh; }
.list-pane { background: var(--panel); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 14px 14px 10px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.list-head h2 { margin: 0; font-size: 18px; display: flex; justify-content: space-between; align-items: center; }
.tabs { display: flex; gap: 4px; background: #f1f3ef; padding: 3px; border-radius: 10px; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 6px 4px; border-radius: 8px; cursor: pointer; font-size: 12.5px; color: var(--muted); }
.tabs button.active { background: #fff; color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.row { display: flex; gap: 8px; align-items: center; }
.conv-list { overflow-y: auto; flex: 1; }
.conv {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; padding: 11px 14px; cursor: pointer;
  border-bottom: 1px solid #f0f2ee; align-items: center;
}
.conv:hover { background: #f8f9f7; }
.conv.active { background: var(--brand-soft); }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 16px; flex: none; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv .meta { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; font-size: 11.5px; color: var(--muted); }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; padding: 2px 8px; border-radius: 20px; background: #eef0ec; color: #4a554e; white-space: nowrap; }
.chip.acc { background: #e8f0fb; color: #2b5a9b; }
.chip.green { background: var(--brand-soft); color: var(--brand-dark); }
.chip.red { background: #fbeaea; color: var(--danger); }
.chip.amber { background: #fdf3e1; color: var(--warn); }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.chat-pane { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.chat-empty { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; }
.chat-empty .big { font-size: 46px; margin-bottom: 8px; }
.chat-head { background: var(--panel); padding: 10px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.chat-head .who { flex: 1; min-width: 160px; }
.chat-head .who b { font-size: 15px; }
.chat-head .who div { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.chat-head select { width: auto; padding: 6px 9px; font-size: 13px; }
.bot-bar { background: #fff8e6; color: #7a5a14; font-size: 12.5px; padding: 6px 16px; display: flex; gap: 10px; align-items: center; border-bottom: 1px solid #f2e4bf; }
.bot-bar.off { background: #f4f4f4; color: #666; border-color: #e5e5e5; }
.messages { flex: 1; overflow-y: auto; padding: 18px 6%; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: min(560px, 75%); padding: 7px 11px 5px; border-radius: 10px; box-shadow: 0 1px .5px rgba(0,0,0,.1); word-wrap: break-word; line-height: 1.55; }
.msg .body { white-space: pre-wrap; }
.msg.in { background: var(--bubble-in); align-self: flex-start; border-start-start-radius: 3px; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-start-end-radius: 3px; }
.msg.bot { background: var(--bubble-bot); }
.msg .by { font-size: 11.5px; font-weight: 600; color: var(--brand-dark); margin-bottom: 2px; }
.msg.bot .by { color: #b45309; }
.msg .time { font-size: 10.5px; color: var(--muted); text-align: end; margin-top: 2px; }
.msg .media { color: #555; font-style: italic; }
.day-sep { align-self: center; background: #fff; color: var(--muted); font-size: 11.5px; padding: 3px 12px; border-radius: 8px; margin: 10px 0; box-shadow: 0 1px .5px rgba(0,0,0,.08); }
.composer { background: var(--panel); padding: 10px 14px; border-top: 1px solid var(--line); }
.composer .qr { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.composer .qr button { border: 1px solid var(--line); background: #f7f8f6; border-radius: 20px; padding: 3px 11px; cursor: pointer; font-size: 12.5px; }
.composer .qr button:hover { border-color: var(--brand); color: var(--brand-dark); }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 44px; max-height: 160px; resize: none; }
.composer .send { height: 44px; padding: 0 18px; }

/* ---------- الصفحات ---------- */
.page { overflow-y: auto; height: 100vh; padding: 26px 32px 60px; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 22px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.acc-card { display: flex; flex-direction: column; gap: 12px; }
.acc-card .top { display: flex; gap: 12px; align-items: center; }
.acc-card .top b { font-size: 15px; }
.acc-card .top .phone { color: var(--muted); font-size: 13px; }
.acc-card .qr-box { display: grid; place-items: center; padding: 10px; background: #fafbf9; border-radius: 10px; border: 1px dashed var(--line); text-align: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.acc-card .qr-box img { width: 220px; height: 220px; }
.acc-card .toggles { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; }
.acc-card .actions { display: flex; gap: 6px; flex-wrap: wrap; border-top: 1px solid var(--line); padding-top: 12px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { padding: 11px 12px; text-align: start; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th { font-size: 12.5px; color: var(--muted); font-weight: 600; background: #fafbf9; }
.tbl td.resp { max-width: 380px; white-space: pre-wrap; color: #3b4540; }
.section { margin-bottom: 18px; }
.section h3 { margin: 0 0 4px; font-size: 16px; }
.section > p { margin: 0 0 16px; color: var(--muted); font-size: 13px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.days { display: flex; gap: 6px; flex-wrap: wrap; }
.days label { border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; cursor: pointer; font-size: 13px; }
.days input { display: none; }
.days label:has(input:checked) { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-dark); font-weight: 600; }
.checks { display: flex; gap: 6px; flex-wrap: wrap; }
.checks label { border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; cursor: pointer; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.note { background: #fff8e6; border: 1px solid #f2e4bf; color: #6f5312; padding: 10px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 18px; line-height: 1.7; }

/* ---------- نافذة منبثقة وتنبيه ---------- */
.modal { position: fixed; inset: 0; background: rgba(30,27,75,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-box { background: var(--panel); border-radius: 16px; padding: 22px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; }
.modal-box h3 { margin: 0 0 16px; }
.modal-foot { display: flex; justify-content: flex-start; gap: 8px; margin-top: 8px; }
#toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60; display: grid; gap: 8px; }
#toast div { background: #1e1b4b; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13.5px; animation: up .2s; }
#toast div.bad { background: var(--danger); }
@keyframes up { from { transform: translateY(8px); opacity: 0; } }

/* ---------- موبايل ---------- */
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr 60px; }
  .rail { flex-direction: row; order: 2; padding: 4px; justify-content: space-around; }
  .rail .logo, .rail .logo-img, .rail .spacer { display: none; }
  .inbox, .page { height: calc(100vh - 60px); }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-chat .list-pane { display: none; }
  .inbox:not(.has-chat) .chat-pane { display: none; }
  .page { padding: 18px 14px 40px; }
  .two { grid-template-columns: 1fr; }
  .messages { padding: 14px 10px; }
}
.back-btn { display: none; }
@media (max-width: 820px) { .back-btn { display: inline-flex; } }

/* ---------- الوسائط ---------- */
.msg .media-img { display: block; max-width: 100%; width: 320px; max-height: 360px; object-fit: cover; border-radius: 8px; margin: 2px 0 4px; background: #0001; cursor: zoom-in; }
.msg video.media-img { cursor: default; object-fit: contain; background: #000; }
.msg .media-img.sticker { width: 140px; height: 140px; object-fit: contain; background: none; }
.msg audio { display: block; width: 280px; max-width: 100%; margin: 2px 0 4px; }
.msg .doc { display: flex; align-items: center; gap: 8px; background: #0000000d; border-radius: 8px; padding: 10px 12px; margin: 2px 0 4px; color: inherit; text-decoration: none; min-width: 200px; }
.msg .doc span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px; }
.msg .doc small { color: var(--brand-dark); font-weight: 600; }
.msg .media small { font-style: normal; color: var(--muted); }
.msg .body a { color: #1a66c9; word-break: break-all; }
.composer .attach { height: 44px; width: 44px; justify-content: center; font-size: 18px; padding: 0; flex: none; }

/* ---------- إضافات ---------- */
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }
.logo-img { width: 48px; height: 48px; border-radius: 14px; margin-bottom: 16px; display: block; }
.rail .logo-img { width: 42px; height: 42px; border-radius: 12px; margin-bottom: 14px; }
.lang-select { display: flex; gap: 4px; justify-content: center; margin-top: 18px; }
.lang-select button { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 12.5px; }
.lang-select button.active { border-color: var(--brand); color: var(--brand-dark); font-weight: 600; background: var(--brand-soft); }
.load-more { align-self: center; margin: 4px 0 10px; }
.banner { background: var(--brand-soft); border-bottom: 1px solid #c7d2fe; color: var(--brand-dark); padding: 9px 14px; font-size: 13px; display: flex; gap: 8px; align-items: center; }
.banner .btn { margin-inline-start: auto; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; margin-bottom: 14px; }
.kv b { color: var(--muted); font-weight: 500; }
.app-box { border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 14px; }
.app-box h4 { margin: 0 0 6px; }
.app-box p { margin: 0 0 10px; color: var(--muted); font-size: 13px; line-height: 1.7; }
.rail .more-btn { display: none !important; }
.code { font-family: ui-monospace, Consolas, monospace; background: #f1f3ef; padding: 2px 6px; border-radius: 6px; direction: ltr; unicode-bidi: isolate; }
@media (max-width: 820px) {
  .rail { padding-bottom: env(safe-area-inset-bottom); }
  .shell { grid-template-rows: 1fr calc(60px + env(safe-area-inset-bottom)); }
  .inbox, .page { height: calc(100vh - 60px - env(safe-area-inset-bottom)); height: calc(100dvh - 60px - env(safe-area-inset-bottom)); }
  .rail button span:not(.badge) { font-size: 10px; }
  #btn-account, #btn-logout { display: none; }
  .rail .more-btn { display: flex !important; }
}
/* ---------- إصلاحات عرض الموبايل ---------- */
.shell > *, .inbox > *, .main, .chat-pane, .list-pane { min-width: 0; }
.messages { overflow-x: hidden; }
.composer .qr { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.composer .qr button { flex: none; }
@media (max-width: 820px) {
  html, body { overflow: hidden; }
  .rail { gap: 0; overflow-x: auto; scrollbar-width: none; }
  .rail button { flex: 1 1 0; min-width: 48px; width: auto; padding: 6px 2px 4px; }
  .rail button span:not(.badge) { font-size: 9.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .chat-head { padding: 8px 10px; gap: 8px; }
  .chat-head .who { min-width: 0; }
  .chat-head .who b { display: inline-block; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }
  .chat-head .chip.acc { order: 10; width: 100%; justify-content: center; }
  .chat-head .avatar { width: 36px; height: 36px; font-size: 14px; }
  .bot-bar { flex-wrap: wrap; padding: 6px 10px; }
  .msg { max-width: 85%; }
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px)); }
  .composer .send { padding: 0 12px; }
  .page-head .btn { width: 100%; justify-content: center; }
  .grid { grid-template-columns: 1fr; }
}
a.btn { text-decoration: none; color: inherit; }
a.btn.primary { color: #fff; }

/* ---------- الاتصالات ---------- */
.call-bubble { display: flex; align-items: center; gap: 10px; min-width: 190px; padding: 2px 2px 4px; }
.call-bubble .call-ic { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; background: #eef0ec; font-size: 18px; flex: none; }
.call-bubble div div { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.call-bubble.call-missed .call-ic, .call-bubble.call-declined .call-ic { background: #fbeaea; }
.call-bubble.call-missed div div { color: var(--danger); font-weight: 600; }
.call-bubble.call-ringing .call-ic { background: var(--brand-soft); animation: ring 1s infinite; }
.call-bubble.call-answered .call-ic { background: var(--brand-soft); }
@keyframes ring { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-12deg); } 75% { transform: rotate(12deg); } }

/* ---------- لوحة المالك ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.card.stat { padding: 14px 16px; }
.stat-n { font-size: 24px; font-weight: 700; }
.stat-l { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.sub-banner { background: #fff8e6; border-bottom-color: #f2e4bf; color: #6f5312; }
.tbl input[type=text], .tbl input[type=number] { padding: 6px 8px; }

.avatar.group { font-size: 19px; }

.rail .me-name { max-width: 62px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }

.ctx-menu { position: fixed; z-index: 70; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 4px; min-width: 190px; }
.ctx-menu button { display: block; width: 100%; text-align: start; border: 0; background: none; padding: 9px 12px; border-radius: 7px; cursor: pointer; font-size: 13.5px; }
.ctx-menu button:hover { background: var(--brand-soft); }

.msg.first { margin-top: 6px; }
.msg.cont.in { border-start-start-radius: 10px; }
.msg.cont.out { border-start-end-radius: 10px; }

/* ---------- بطاقة الملف ---------- */
.doc-card { width: 300px; max-width: 100%; background: #0000000a; border-radius: 9px; overflow: hidden; margin: 2px 0 4px; }
.doc-thumb-wrap { display: block; background: #fff; border-bottom: 1px solid #0000000d; }
.doc-thumb { display: block; width: 100%; max-height: 190px; object-fit: cover; object-position: top; }
canvas.doc-thumb { height: 150px; background: #f5f5f7; }
canvas.doc-thumb.ready { height: auto; }
.doc-row { display: flex; align-items: center; gap: 10px; padding: 9px 10px; }
.doc-ic { flex: none; width: 34px; height: 40px; border-radius: 5px; background: #6b7280; color: #fff; font-size: 10px; font-weight: 700; display: grid; place-items: center; }
.doc-ic.pdf { background: #dc2626; }
.doc-info { flex: 1; min-width: 0; color: inherit; text-decoration: none; display: flex; flex-direction: column; }
.doc-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.doc-info small { color: var(--muted); font-size: 11.5px; margin-top: 2px; }
.doc-dl { flex: none; width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--line); display: grid; place-items: center; color: var(--muted); text-decoration: none; background: #fff; }
.doc-dl:hover { color: var(--brand); border-color: var(--brand); }
