/* ============ نظام الثيم ============ */
:root {
  --brand: #128c7e;
  --brand-deep: #075e54;
  --brand-2: #0b7d6e;
  --accent: #25d366;
  --accent-hover: #1eb858;

  --bg: #eae6df;
  --bg-grad-a: #f0f2f5;
  --bg-grad-b: #e7ece9;
  --panel: #ffffff;
  --panel-2: #f7f9fa;
  --sidebar: #ffffff;
  --sidebar-active: #e8f6ef;
  --header: #f0f2f5;

  --text: #111b21;
  --text-2: #54656f;
  --text-3: #8696a0;
  --line: #e9edef;
  --line-2: #d8dfe3;

  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --chat-bg: #efeae2;

  --danger: #e04a45;
  --shadow: 0 10px 30px rgba(11, 20, 26, .10);
  --shadow-sm: 0 2px 8px rgba(11, 20, 26, .06);
  --shadow-lg: 0 24px 70px rgba(11, 20, 26, .28);
  --radius: 16px;
  --radius-sm: 11px;

  --sidebar-w: 292px;
  --topbar-h: 60px;
}

:root[data-theme="dark"] {
  --brand: #00a884;
  --brand-deep: #005c4b;
  --brand-2: #027e6f;
  --accent: #00a884;
  --accent-hover: #06cf9c;

  --bg: #0b141a;
  --bg-grad-a: #0b141a;
  --bg-grad-b: #0d1a20;
  --panel: #111b21;
  --panel-2: #182229;
  --sidebar: #111b21;
  --sidebar-active: #1f2c33;
  --header: #202c33;

  --text: #e9edef;
  --text-2: #aebac1;
  --text-3: #8696a0;
  --line: #22303a;
  --line-2: #2a3942;

  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --chat-bg: #0b141a;

  --danger: #f15c57;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --shadow-sm: 0 2px 10px rgba(0, 0, 0, .3);
  --shadow-lg: 0 24px 70px rgba(0, 0, 0, .6);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: "IBM Plex Sans Arabic", "Segoe UI", "SF Arabic", Tahoma, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
svg { width: 1em; height: 1em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ============ الهيكل العام ============ */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ============ الشريط العلوي (جوال) ============ */
.topbar {
  display: none;
  align-items: center;
  gap: 10px;
  height: var(--topbar-h);
  padding: 0 12px;
  padding-top: env(safe-area-inset-top);
  background: linear-gradient(135deg, var(--brand-deep), var(--brand));
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-title { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; flex: 1; }
.topbar-logo { font-size: 22px; display: inline-flex; }
.topbar .icon-btn { color: #fff; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 12px;
  background: transparent; border: none; cursor: pointer;
  color: var(--text-2); font-size: 21px; transition: background .15s, color .15s;
}
.icon-btn:hover { background: rgba(128,128,128,.14); }

/* زر الثيم: يُظهر أيقونة حسب الوضع */
.theme-toggle .ico-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .ico-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .ico-sun { display: inline; }

/* ============ الشريط الجانبي ============ */
.sidebar {
  background: var(--sidebar);
  border-inline-end: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 6px;
  overflow-y: auto;
  z-index: 40;
}
.brand {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 8px 16px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 12px;
}
.brand-logo {
  width: 46px; height: 46px; flex: none;
  border-radius: 14px;
  display: grid; place-items: center;
  color: #fff; font-size: 26px;
  background: linear-gradient(140deg, var(--brand), var(--brand-deep));
  box-shadow: 0 6px 16px rgba(18,140,126,.4);
}
.brand-txt { flex: 1; min-width: 0; }
.brand-name { font-weight: 700; font-size: 17px; color: var(--text); }
.prov { font-size: 11.5px; color: var(--text-3); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.desk-theme { color: var(--text-2); }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav button {
  display: flex; align-items: center; gap: 13px;
  width: 100%; padding: 12px 14px;
  border: none; background: transparent;
  border-radius: 13px; cursor: pointer;
  font-family: inherit; font-size: 14.5px; font-weight: 600;
  color: var(--text-2); text-align: start;
  position: relative; transition: background .15s, color .15s;
}
.nav button svg { font-size: 20px; flex: none; opacity: .9; }
.nav button span { display: inline-flex; align-items: center; gap: 6px; }
.nav button:hover { background: var(--panel-2); color: var(--text); }
.nav button.active { background: var(--sidebar-active); color: var(--brand); font-weight: 700; }
.nav button.active::before {
  content: ""; position: absolute; inset-inline-start: 0; top: 22%; bottom: 22%;
  width: 4px; border-radius: 4px; background: var(--brand);
}
.nav button .nav-badge {
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 10px; background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 700; display: none;
  align-items: center; justify-content: center;
}
.nav button .nav-badge.show { display: inline-flex; }

.stats-title { margin: 18px 8px 8px; font-size: 11px; font-weight: 700; letter-spacing: .5px; color: var(--text-3); text-transform: uppercase; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pill {
  background: var(--panel-2); border: 1px solid var(--line);
  padding: 11px 12px; border-radius: 13px;
  font-size: 11.5px; color: var(--text-3); font-weight: 600;
  display: flex; flex-direction: column; gap: 2px;
}
.pill b { font-size: 20px; color: var(--text); font-weight: 700; }

.backdrop {
  display: none; position: fixed; inset: 0; z-index: 35;
  background: rgba(0,0,0,.5); opacity: 0; transition: opacity .25s;
}

/* ============ المحتوى ============ */
.main {
  overflow-y: auto;
  background:
    radial-gradient(1200px 600px at 100% -10%, rgba(18,140,126,.08), transparent 60%),
    linear-gradient(180deg, var(--bg-grad-a), var(--bg-grad-b));
}
.main-inner { max-width: 960px; margin: 0 auto; padding: 30px 26px 60px; }
.page { display: none; animation: fade .3s ease; }
.page.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.page-head { margin-bottom: 18px; }
.page-head h2 { font-size: 24px; font-weight: 700; color: var(--text); }
.page-head p { font-size: 13.5px; color: var(--text-3); margin-top: 4px; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}

/* ============ عناصر النموذج ============ */
label { display: block; text-align: start; font-size: 13px; color: var(--text-2); margin-bottom: 7px; font-weight: 700; }
input, textarea, select {
  width: 100%; font-family: inherit; font-size: 14.5px;
  border: 1.5px solid var(--line-2); border-radius: var(--radius-sm);
  padding: 12px 14px; margin-bottom: 16px;
  text-align: start; background: var(--panel); color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder, textarea::placeholder { color: var(--text-3); }
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent);
}
textarea { resize: vertical; min-height: 76px; line-height: 1.6; }
select { cursor: pointer; }
input[type="file"] { padding: 10px; cursor: pointer; }
.hint { font-size: 11.5px; color: var(--text-3); text-align: start; margin: -10px 0 16px; line-height: 1.5; }
.row { display: flex; gap: 14px; flex-wrap: wrap; }
.row > div { flex: 1; min-width: 150px; }
.row-btn { display: flex; align-items: flex-end; flex: 0 0 auto; }
.hide { display: none !important; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0 14px; }

/* ============ الأزرار ============ */
button.primary {
  width: 100%; background: linear-gradient(135deg, var(--accent), var(--brand));
  color: #fff; border: none; border-radius: var(--radius-sm);
  padding: 13px; font-family: inherit; font-size: 15px; font-weight: 700;
  cursor: pointer; transition: transform .1s, box-shadow .15s, filter .15s;
  box-shadow: 0 8px 20px color-mix(in srgb, var(--accent) 32%, transparent);
}
button.primary:hover { filter: brightness(1.05); box-shadow: 0 10px 26px color-mix(in srgb, var(--accent) 42%, transparent); }
button.primary:active { transform: translateY(1px); }
button.primary:disabled { filter: grayscale(.4) opacity(.6); cursor: not-allowed; box-shadow: none; }
button.primary.alt { background: linear-gradient(135deg, var(--brand), var(--brand-deep)); box-shadow: 0 8px 20px color-mix(in srgb, var(--brand) 32%, transparent); }

button.ghost {
  background: var(--panel-2); border: 1.5px solid var(--line-2);
  border-radius: var(--radius-sm); padding: 11px 16px;
  font-family: inherit; font-size: 13px; font-weight: 700; color: var(--text-2);
  cursor: pointer; transition: background .15s;
}
button.ghost:hover { background: var(--line); }
button.danger {
  background: var(--danger); color: #fff; border: none;
  border-radius: var(--radius-sm); padding: 10px 18px;
  font-family: inherit; font-size: 13px; font-weight: 700; cursor: pointer;
  transition: filter .15s;
}
button.danger:hover { filter: brightness(1.05); }
button.danger:disabled { filter: grayscale(.5) opacity(.5); cursor: not-allowed; }

.c_chk, #c_all { width: 18px; height: 18px; cursor: pointer; accent-color: var(--accent); }

/* مفتاح التبديل (toggle) */
.switch-line { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; font-weight: 700; color: var(--text); cursor: pointer; }
.switch { position: relative; display: inline-block; width: 46px; height: 26px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; margin: 0; }
.slider { position: absolute; inset: 0; background: var(--line-2); border-radius: 26px; transition: .2s; }
.slider::before { content: ""; position: absolute; height: 20px; width: 20px; inset-inline-start: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(-20px); }
[dir="rtl"] .switch input:checked + .slider::before { transform: translateX(-20px); }
[dir="ltr"] .switch input:checked + .slider::before { transform: translateX(20px); }

/* ============ التبويبات ============ */
.tabs { display: flex; gap: 6px; margin-bottom: 20px; background: var(--panel-2); padding: 5px; border-radius: 13px; border: 1px solid var(--line); }
.tabs .t {
  flex: 1; padding: 10px; border-radius: 9px; cursor: pointer;
  font-size: 13.5px; font-weight: 700; color: var(--text-2); text-align: center; transition: .15s;
}
.tabs .t.active { background: var(--panel); color: var(--brand); box-shadow: var(--shadow-sm); }

.status { font-size: 13px; margin-top: 14px; min-height: 18px; text-align: center; white-space: pre-line; font-weight: 600; }
.status.ok { color: var(--accent-hover); } .status.err { color: var(--danger); }
.note { margin-top: 18px; padding: 12px 14px; background: color-mix(in srgb, #f0a020 12%, transparent); border: 1px solid color-mix(in srgb, #f0a020 30%, transparent); border-radius: 12px; font-size: 12px; color: var(--text-2); line-height: 1.7; }

/* ============ الجداول ============ */
.tablewrap { max-height: 460px; overflow: auto; border: 1px solid var(--line); border-radius: 13px; background: var(--panel); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 12px 12px; text-align: start; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--text-3); font-weight: 700; background: var(--panel-2); position: sticky; top: 0; z-index: 1; font-size: 12px; }
tbody tr:last-child td { border-bottom: none; }
.badge { padding: 3px 11px; border-radius: 20px; font-size: 11px; font-weight: 700; white-space: nowrap; display: inline-block; }
.b-sent { background: color-mix(in srgb, var(--text-3) 18%, transparent); color: var(--text-2); }
.b-delivered { background: color-mix(in srgb, #2b8fff 18%, transparent); color: #2b8fff; }
.b-read { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent-hover); }
.b-failed { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.b-received { background: color-mix(in srgb, #f0a020 20%, transparent); color: #c07d12; }
.empty { text-align: center; color: var(--text-3); font-size: 13px; padding: 26px; }

.summary { display: flex; gap: 10px; flex-wrap: wrap; margin: 6px 0 16px; }
.summary .box { flex: 1; min-width: 92px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 13px; padding: 12px; text-align: center; }
.summary .box b { display: block; font-size: 20px; color: var(--brand); font-weight: 700; }
.summary .box span { font-size: 11px; color: var(--text-3); }

.clickable { cursor: pointer; transition: background .12s; }
.clickable:hover { background: var(--panel-2); }

/* صف فيه رد جديد غير مقروء */
tr.unread { background: color-mix(in srgb, #f0a020 10%, transparent); }
tr.unread:hover { background: color-mix(in srgb, #f0a020 16%, transparent); }
.newdot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-inline-end: 7px; vertical-align: middle; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }

/* ============ نافذة المحادثة ============ */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(3px); display: flex; align-items: center; justify-content: center; z-index: 60; padding: 20px; animation: fade .2s; }
.chatbox {
  background: var(--chat-bg); width: 100%; max-width: 480px; height: 82vh; height: 82dvh;
  border-radius: 18px; overflow: hidden; display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg); animation: pop .25s ease;
}
@keyframes pop { from { transform: scale(.94) translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }
.chathead {
  background: linear-gradient(135deg, var(--brand-deep), var(--brand));
  color: #fff; padding: 12px 14px; display: flex; align-items: center; gap: 11px;
}
.chathead .icon-btn { color: #fff; width: 36px; height: 36px; }
.chatback { display: none; }
.avatar {
  width: 42px; height: 42px; flex: none; border-radius: 50%;
  background: rgba(255,255,255,.22); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 17px;
}
.chathead-txt { flex: 1; min-width: 0; }
.chathead-txt b { font-size: 15.5px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chatphone { font-size: 11.5px; opacity: .85; direction: ltr; text-align: start; }

.chatbody {
  flex: 1; overflow-y: auto; padding: 18px 16px;
  display: flex; flex-direction: column; gap: 6px;
  background-color: var(--chat-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='%23000000' fill-opacity='0.03'%3E%3Ccircle cx='10' cy='10' r='2'/%3E%3Ccircle cx='50' cy='30' r='2'/%3E%3Ccircle cx='30' cy='60' r='2'/%3E%3Ccircle cx='70' cy='70' r='2'/%3E%3C/g%3E%3C/svg%3E");
}
:root[data-theme="dark"] .chatbody { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Ccircle cx='10' cy='10' r='2'/%3E%3Ccircle cx='50' cy='30' r='2'/%3E%3Ccircle cx='30' cy='60' r='2'/%3E%3Ccircle cx='70' cy='70' r='2'/%3E%3C/g%3E%3C/svg%3E"); }

.bubble {
  max-width: 80%; padding: 8px 12px 7px; border-radius: 12px;
  font-size: 14px; line-height: 1.55; word-wrap: break-word;
  color: var(--text); box-shadow: 0 1px 1px rgba(0,0,0,.12); position: relative;
}
.bubble .time { display: block; font-size: 10px; color: var(--text-3); margin-top: 3px; text-align: start; }
.bubble.in { align-self: flex-start; background: var(--bubble-in); border-top-inline-start-radius: 3px; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); border-top-inline-end-radius: 3px; }
:root[data-theme="dark"] .bubble.out { color: #e9edef; }
.bubble.out .st { font-size: 10px; color: color-mix(in srgb, var(--accent) 80%, #000); margin-inline-start: 5px; font-weight: 700; }
:root[data-theme="dark"] .bubble.out .st { color: #8fd9c4; }
.chatempty { text-align: center; color: var(--text-3); font-size: 13px; margin: auto; }

.chatfoot { display: flex; gap: 9px; padding: 11px; background: var(--header); border-top: 1px solid var(--line); align-items: center; }
.chatfoot input { flex: 1; margin: 0; border-radius: 24px; border: 1.5px solid transparent; background: var(--panel); padding: 12px 16px; font-size: 14.5px; }
.chatsend {
  flex: none; width: 46px; height: 46px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--brand)); color: #fff;
  border: none; cursor: pointer; display: grid; place-items: center; font-size: 20px;
  transition: transform .1s, filter .15s;
}
.chatsend svg { transform: scaleX(-1); }
.chatsend:hover { filter: brightness(1.08); }
.chatsend:active { transform: scale(.92); }
.chatsend:disabled { filter: grayscale(.5) opacity(.6); cursor: not-allowed; }

/* ============ الاستجابة (جوال/لوحي) ============ */
@media (max-width: 900px) {
  html, body { overflow-x: hidden; }
  body { overflow-y: auto; }
  .app { grid-template-columns: 1fr; height: auto; min-height: 100dvh; overflow-x: clip; }
  .topbar { display: flex; }
  .desk-theme { display: none; }
  .sidebar {
    position: fixed; top: 0; right: 0; /* RTL: الدرج على اليمين */
    width: min(84vw, 320px); height: 100dvh;
    transform: translateX(100%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
    box-shadow: var(--shadow-lg);
  }
  .app.sidebar-open .sidebar { transform: translateX(0) !important; }
  .app.sidebar-open .backdrop { display: block; opacity: 1; }
  .main { overflow: visible; }
  .main-inner { padding: 20px 16px 48px; }
  .page-head h2 { font-size: 21px; }
  .card { padding: 18px; }
}

@media (max-width: 560px) {
  .main-inner { padding: 16px 12px 40px; }
  .card { padding: 15px; border-radius: 14px; }
  .row-btn { width: 100%; }
  .row-btn button { width: 100%; }
  .modal { padding: 0; }
  .chatbox { max-width: 100%; height: 100dvh; border-radius: 0; }
  .chatback { display: inline-flex; }
  .chatclose { display: none; }
  .page-head h2 { font-size: 19px; }
}
