/* ============================================
   Design tokens
   لوحة ألوان مكتب محاماة: كحلي غامق + ذهبي هادئ
   ============================================ */
:root {
  --bg: #F4F5F8;
  --surface: #FFFFFF;
  --surface-2: #FAFAFC;
  --ink: #1D2620;
  --ink-muted: #6E7B71;
  --ink-faint: #9BA69D;
  --primary: #1B2A41;
  --primary-dark: #0F1A2B;
  --primary-tint: #E1E6EF;
  --accent: #B8924A;
  --accent-tint: #F1E7D2;
  --warn: #A6532E;
  --border: #E5E1D3;
  --bubble-in: #FFFFFF;
  --bubble-out: #DCE9DE;
  --radius-s: 8px;
  --radius-m: 14px;
  --radius-l: 20px;
  --font: 'Tajawal', system-ui, sans-serif;
}

@supports (color: color-mix(in srgb, red 10%, white)) {
  :root { --bubble-out: color-mix(in srgb, var(--primary) 14%, #fff); }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

button, input, select {
  font-family: inherit;
}

/* التطبيق مثبّت على المساحة المرئية فعلاً من الشاشة (بدون شريط سفاري ولا الكيبورد):
   --app-h و --app-top بيتحدّثوا من app.js عبر visualViewport، و100dvh احتياطي. */
.app {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--app-top, 0px);
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
  display: flex;
  overflow: hidden;
}

/* ============================================
   Sidebar (قائمة المحادثات)
   ============================================ */
.sidebar {
  width: 360px;
  min-width: 360px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-top {
  padding: 20px 18px 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-m);
  background: var(--primary-tint);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.brand-text h1 {
  font-size: 17px;
  font-weight: 700;
  margin: 0 0 2px;
  color: var(--primary-dark);
}

.status-line {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--ink-muted);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ink-faint);
  display: inline-block;
  transition: background .2s ease;
}
.status-dot.connected { background: var(--primary); }
.status-dot.qr_pending { background: var(--accent); }
.status-dot.disconnected { background: var(--warn); }

/* ---------- قائمة المستخدم ---------- */
.user-menu { position: relative; flex-shrink: 0; }
.user-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--primary-dark);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}
.user-dropdown {
  display: none;
  position: absolute;
  left: 0;
  top: 44px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  box-shadow: 0 6px 20px rgba(0,0,0,.08);
  min-width: 200px;
  z-index: 20;
  overflow: hidden;
}
.user-dropdown.open { display: block; }
.user-dropdown-name {
  padding: 12px 14px;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink);
  border-bottom: 1px solid var(--border);
}
.user-dropdown-item {
  display: block;
  width: 100%;
  text-align: right;
  padding: 11px 14px;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
}
.user-dropdown-item:hover { background: var(--surface-2); }

.search-row {
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-row input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  background: var(--surface-2);
  font-size: 14px;
  color: var(--ink);
  outline: none;
}
.search-row input:focus {
  border-color: var(--primary);
}

.filter-tabs {
  display: flex;
  gap: 6px;
  padding: 4px 18px 14px;
  overflow-x: auto;
}
.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-muted);
  padding: 6px 8px 6px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.tab:hover:not(.active) {
  border-color: var(--primary);
  color: var(--primary-dark);
}
.tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.tab-count {
  min-width: 22px;
  height: 20px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--primary-tint);
  color: var(--primary-dark);
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  transition: background .15s ease, color .15s ease;
}
.tab.active .tab-count {
  background: rgba(255, 255, 255, .22);
  color: #fff;
}

.convo-list-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.convo-list {
  height: 100%;
  overflow-y: auto;
  border-top: 1px solid var(--border);
}

/* سحب لتحديث القائمة (خصوصاً مهم للتطبيق المضاف على الشاشة الرئيسية،
   لأن سحب المتصفح الافتراضي بيتعطّل في وضع standalone) */
.pull-refresh {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.pull-refresh-spinner {
  width: 22px;
  height: 22px;
  color: var(--primary);
  opacity: 0;
  transition: opacity 0.15s ease;
}
.pull-refresh.visible .pull-refresh-spinner { opacity: 1; }
.pull-refresh-spinner.spinning { animation: pull-refresh-spin 0.7s linear infinite; }
@keyframes pull-refresh-spin {
  to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
  .pull-refresh-spinner.spinning { animation: none; }
}

.empty-hint {
  padding: 40px 20px;
  text-align: center;
  color: var(--ink-faint);
  font-size: 14px;
}

.convo-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 18px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .12s ease;
  position: relative;
}
.convo-item:hover { background: var(--surface-2); }
.convo-item.active { background: var(--primary-tint); }
.convo-item.urgency-urgent { border-right: 3px solid #D64545; }
.convo-item.urgency-angry { border-right: 3px solid #D64545; background: #FCEEEE; }
.convo-item.urgency-angry:hover { background: #FAE2E2; }

.avatar {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  background: var(--primary-dark);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
}

.convo-meta {
  flex: 1;
  min-width: 0;
}
.convo-top-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.convo-name {
  font-weight: 600;
  font-size: 14.5px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo-time {
  font-size: 11.5px;
  color: var(--ink-faint);
  white-space: nowrap;
}
.convo-bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
}
.convo-preview {
  font-size: 13px;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badges { display: flex; align-items: center; gap: 6px; }
.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 19px;
  height: 19px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}
.paused-badge {
  font-size: 13px;
}
.status-pill {
  font-size: 10.5px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--ink-muted);
  border: 1px solid var(--border);
}
.status-pill.assigned { background: var(--accent-tint); color: #7A5719; border-color: transparent; }
.status-pill.mine { background: var(--primary-tint); color: var(--primary-dark); border-color: transparent; }

/* ============================================
   Chat panel (شاشة المحادثة)
   ============================================ */
.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
  min-height: 0;
}

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--ink-faint);
  gap: 10px;
}
.empty-illustration { font-size: 46px; opacity: .5; }

.chat-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

/* ---------- أيقونات SVG ---------- */
.ico {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ico-fill { fill: currentColor; stroke: none; }

.icon-btn {
  width: 40px;
  height: 40px;
  min-width: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn .ico { width: 22px; height: 22px; }
.icon-btn:hover { background: var(--primary-tint); color: var(--primary-dark); }
.icon-btn:focus-visible,
.chip:focus-visible,
.send-btn:focus-visible,
.sheet-option:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ---------- هيدر المحادثة ---------- */
.chat-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 8px;
  padding: 8px 10px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.back-btn { display: none; color: var(--ink); }

.customer-info {
  order: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 0;
  min-width: 0;
  padding-bottom: 8px;
}
.customer-info .avatar {
  width: 40px;
  height: 40px;
  min-width: 40px;
  font-size: 15px;
}
.customer-text { min-width: 0; }
.name-row { display: flex; align-items: center; gap: 2px; min-width: 0; }
.customer-text h2 {
  margin: 0;
  font-size: 15.5px;
  font-weight: 700;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.edit-name-btn {
  width: 26px;
  height: 26px;
  min-width: 26px;
  color: var(--ink-faint);
  flex-shrink: 0;
}
.edit-name-btn .ico { width: 15px; height: 15px; }
.edit-name-btn:hover { color: var(--primary-dark); }
.modal-field-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-muted);
  margin: 0 0 4px;
}
#renameInput,
#renamePhoneInput {
  width: 100%;
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-2);
  font-family: inherit;
  font-size: 14.5px;
  color: var(--ink);
}
#renamePhoneInput { text-align: left; }
#renameInput:focus,
#renamePhoneInput:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-tint);
}
.phone {
  font-size: 12px;
  color: var(--ink-muted);
  direction: ltr;
  display: inline-block;
}

.chat-menu { order: 2; position: relative; padding-bottom: 8px; }
.chat-menu-dropdown {
  display: none;
  position: absolute;
  left: 0;
  top: 46px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  box-shadow: 0 10px 30px rgba(0,0,0,.12);
  min-width: 200px;
  z-index: 20;
  overflow: hidden;
}
.chat-menu-dropdown.open { display: block; }
.user-dropdown-item.danger { color: var(--warn); display: flex; align-items: center; gap: 8px; }
.user-dropdown-item.danger:hover { background: #F3E2D9; }
#closeConvoBtn { display: flex; align-items: center; gap: 8px; }
#closeConvoBtn .ico { width: 17px; height: 17px; color: var(--ink-muted); }
/* شارة \"مغلقة\" صغيرة جنب اسم العميل في قائمة المحادثات لما المحادثة تبقى مقفولة */
.closed-pill {
  display: inline-flex;
  align-items: center;
  font-size: 10.5px;
  font-weight: 600;
  color: var(--ink-faint);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  margin-inline-start: 6px;
}

/* ---------- شريط التحكم (إسناد / وضع الرد / الردود الآلية) ---------- */
.chat-toolbar {
  order: 3;
  flex: 1 1 100%;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 0 2px 10px;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.chat-toolbar::-webkit-scrollbar { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  flex: 0 1 auto;
  min-width: 0;
  transition: background .15s ease, border-color .15s ease, color .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.chip:hover { background: var(--surface-2); border-color: var(--ink-faint); }
.chip .ico { width: 17px; height: 17px; color: var(--ink-muted); }
.chip .chip-caret { width: 14px; height: 14px; color: var(--ink-faint); margin-inline-start: -2px; }
.chip-label { min-width: 0; max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
.chip-switch { flex-shrink: 0; }

.chip-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.chip.is-assigned { background: var(--primary-tint); border-color: transparent; color: var(--primary-dark); }
.chip.is-unassigned { border-style: dashed; }

.chip.mode-ai .ico:first-child { color: var(--primary); }
.chip.mode-bot .ico:first-child { color: #8A6417; }

.chip-switch .track {
  width: 30px;
  height: 18px;
  border-radius: 999px;
  background: var(--ink-faint);
  position: relative;
  transition: background .18s ease;
  flex-shrink: 0;
}
.chip-switch .knob {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.25);
  transition: transform .18s ease;
}
.chip-switch[aria-checked="true"] .track { background: var(--primary); }
.chip-switch[aria-checked="true"] .knob { transform: translateX(-12px); }
.chip-switch[aria-checked="false"] {
  background: #F3E2D9;
  border-color: transparent;
  color: var(--warn);
}

.lbl-short { display: none; }
@media (max-width: 420px) {
  .chip { padding: 0 9px; gap: 6px; font-size: 12.5px; }
  .chat-toolbar { gap: 6px; }
  .chip-label { max-width: 120px; }
  .lbl-full { display: none; }
  .lbl-short { display: inline; }
  .chip .chip-caret { display: none; } /* المساحة على الجوال أهم؛ الشكل نفسه واضح إنه قابل للضغط */
  .chip-label { max-width: none; }
}

@media (min-width: 1180px) {
  .chat-header { padding: 8px 14px 0; }
  .chat-toolbar { order: 2; flex: 0 0 auto; overflow: visible; padding: 0 0 8px; }
  .chat-menu { order: 3; }
}

/* ---------- قائمة الاختيار (sheet): نافذة سفلية على الجوال، قائمة منبثقة على الديسكتوب ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20,28,22,.4);
  z-index: 60;
  opacity: 0;
  transition: opacity .2s ease;
}
.sheet-backdrop.show { opacity: 1; }

.sheet {
  position: fixed;
  z-index: 61;
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(0,0,0,.2);
  overflow: hidden;
}
.sheet[hidden], .sheet-backdrop[hidden] { display: none; }
.sheet-grip {
  width: 38px;
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin: 8px auto 0;
}
.sheet-title {
  padding: 12px 18px 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-muted);
}
.sheet-list { display: flex; flex-direction: column; padding: 4px 8px 8px; max-height: 60vh; overflow-y: auto; }
.sheet-option {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 10px;
  border: none;
  border-radius: 12px;
  background: transparent;
  text-align: right;
  cursor: pointer;
  color: var(--ink);
}
.sheet-option:hover { background: var(--surface-2); }
.sheet-option .opt-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary-tint);
  color: var(--primary-dark);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}
.sheet-option .opt-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.sheet-option .opt-label { font-size: 14.5px; font-weight: 700; }
.sheet-option .opt-desc { font-size: 12.5px; color: var(--ink-muted); margin-top: 1px; }
.sheet-option .opt-check { color: var(--primary); opacity: 0; }
.sheet-option[aria-selected="true"] { background: var(--primary-tint); }
.sheet-option[aria-selected="true"] .opt-check { opacity: 1; }

@media (min-width: 881px) {
  .sheet { width: 300px; border: 1px solid var(--border); border-radius: 14px; }
  .sheet-grip { display: none; }
  .sheet-backdrop { background: transparent; }
}
@media (max-width: 880px) {
  .sheet {
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 20px 20px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateY(100%);
    transition: transform .22s ease;
  }
  .sheet.show { transform: none; }
}

/* ---------- نافذة الردود الجاهزة/الملاحظات المنبثقة فوق صندوق الكتابة ---------- */
.quick-actions-popup {
  border-top: 1px solid var(--border);
  background: var(--surface);
  max-height: min(240px, 40%);
  overflow-y: auto;
  padding: 10px 14px;
  flex-shrink: 0;
}
.quick-actions-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink-muted);
  margin-bottom: 8px;
}
.quick-actions-header button {
  border: none;
  background: var(--primary-tint);
  color: var(--primary-dark);
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
}
.quick-replies-list-inline { display: flex; flex-direction: column; gap: 6px; }
.quick-reply-item-inline {
  text-align: right;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 13.5px;
  cursor: pointer;
}
.quick-reply-item-inline:hover { background: var(--primary-tint); }
.quick-reply-item-inline .qr-title { font-weight: 700; display: block; margin-bottom: 2px; }
.quick-reply-item-inline .qr-text { color: var(--ink-muted); font-size: 12.5px; }

/* ---------- شريط وضع التعديل + معاينة المرفق ---------- */
.edit-banner,
.attach-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  font-size: 13px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.edit-banner { background: var(--accent-tint); color: #6B4F16; }
.attach-preview { background: var(--primary-tint); color: var(--primary-dark); }
.edit-banner button,
.attach-preview button {
  border: none;
  background: none;
  color: var(--warn);
  font-size: 13px;
  cursor: pointer;
  font-weight: 700;
  padding: 2px 6px;
}
.attach-preview span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- منطقة الرسائل ---------- */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 10px clamp(10px, 4vw, 64px) 14px;
  display: flex;
  flex-direction: column;
  background-image: radial-gradient(circle at 1px 1px, rgba(120,130,122,.16) 1px, transparent 0);
  background-size: 22px 22px;
}

.load-more-btn {
  align-self: center;
  margin: 6px auto 4px;
  padding: 7px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-muted);
  font-size: 12.5px;
  cursor: pointer;
}
.load-more-btn:hover { background: var(--surface-2); }

.date-sep {
  align-self: center;
  margin: 14px 0 6px;
  padding: 4px 13px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-muted);
  background: var(--surface);
  border-radius: 999px;
  box-shadow: 0 1px 2px rgba(20,30,24,.12);
}

.bubble-row {
  display: flex;
  margin-top: 8px;
  position: relative;
}
.bubble-row.cont { margin-top: 2px; }
/* صادر (نحن) على اليمين، وارد (العميل) على اليسار — نفس ترتيب واتساب */
.bubble-row.out { justify-content: flex-start; }
.bubble-row.in { justify-content: flex-end; }

.bubble {
  position: relative;
  display: flow-root;
  max-width: min(72%, 580px);
  padding: 7px 12px 6px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.55;
  box-shadow: 0 1px 1.5px rgba(20,30,24,.14);
  overflow-wrap: anywhere;
}
.bubble.in { background: var(--bubble-in); }
.bubble.out { background: var(--bubble-out); }

/* ذيل الفقاعة: بس على أول رسالة في المجموعة */
.bubble-row:not(.cont):not(.note-row) .bubble::before {
  content: '';
  position: absolute;
  top: 0;
  width: 9px;
  height: 13px;
  background: inherit;
}
.bubble-row:not(.cont):not(.note-row) .bubble.out { border-top-right-radius: 3px; }
.bubble-row:not(.cont):not(.note-row) .bubble.out::before { right: -8px; clip-path: polygon(0 0, 100% 0, 0 100%); }
.bubble-row:not(.cont):not(.note-row) .bubble.in { border-top-left-radius: 3px; }
.bubble-row:not(.cont):not(.note-row) .bubble.in::before { left: -8px; clip-path: polygon(0 0, 100% 0, 100% 100%); }

.bubble-text { white-space: pre-wrap; }

.bubble .meta {
  float: left;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 8px 12px -2px 0;
  font-size: 11px;
  line-height: 1;
  color: var(--ink-muted);
  user-select: none;
}
.bubble .edited-tag { font-size: 10.5px; color: var(--ink-faint); }

.bubble .sender-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 2px;
  color: var(--primary-dark);
}
.bubble .sender-tag .ico { width: 13px; height: 13px; }
.bubble.out.from-agent .sender-tag { color: var(--warn); }
.bubble.out.from-bot .sender-tag { color: #7A5719; }
.bubble-row.cont .sender-tag { display: none; }

.bubble.deleted { opacity: .65; font-style: italic; }

/* ---------- فقاعات الوسائط ---------- */
.bubble img.media-img {
  display: block;
  width: 100%;
  max-width: 300px;
  max-height: 340px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 4px;
  cursor: pointer;
}
.bubble audio {
  display: block;
  width: 250px;
  max-width: 100%;
  height: 40px;
  margin-bottom: 4px;
}
.bubble video {
  display: block;
  max-width: 300px;
  width: 100%;
  border-radius: 8px;
  margin-bottom: 4px;
}
.bubble .doc-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 11px;
  background: rgba(0,0,0,.05);
  border-radius: 8px;
  color: var(--ink);
  text-decoration: none;
  margin-bottom: 4px;
  font-size: 13.5px;
}
.bubble .transcript {
  font-size: 13px;
  font-style: italic;
  color: var(--ink-muted);
  margin-top: 2px;
  padding-top: 4px;
  border-top: 1px dashed var(--border);
}
.bubble.media-only { padding: 3px; }
.bubble.media-only img.media-img,
.bubble.media-only video { margin-bottom: 0; }
.bubble.media-only .meta {
  position: absolute;
  float: none;
  bottom: 8px;
  left: 8px;
  margin: 0;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(0,0,0,.5);
  color: #fff;
}

/* ---------- ملاحظة داخلية ---------- */
.bubble-row.note-row { justify-content: center; margin-top: 10px; }
.bubble.note {
  max-width: min(84%, 520px);
  background: var(--accent-tint);
  border: 1px dashed #C9A25A;
  color: #5C4413;
  font-size: 13.5px;
  box-shadow: none;
}
.bubble.note .sender-tag { color: #7A5719; }

/* ---------- حدث اتصال واتساب ---------- */
.bubble-row.call-row { margin-top: 10px; }
.call-event {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: min(88%, 420px);
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--ink);
  font-size: 13px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
}
.call-event .call-ico { font-size: 15px; line-height: 1; }
.call-event .call-text { font-weight: 600; }
.call-event .call-time { color: var(--ink-faint); font-size: 11.5px; }

/* ---------- أزرار تعديل/حذف الرسالة ---------- */
.bubble-actions {
  display: none;
  gap: 2px;
  position: absolute;
  top: -16px;
  right: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
  z-index: 3;
}
.bubble-row.out:hover .bubble-actions,
.bubble-row.out.actions-visible .bubble-actions { display: flex; }
.bubble-actions button {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--ink-muted);
  border-radius: 50%;
  cursor: pointer;
}
.bubble-actions button .ico { width: 16px; height: 16px; }
.bubble-actions button:hover { background: var(--surface-2); color: var(--ink); }
.bubble-actions button.delete-msg-btn:hover { color: var(--warn); }

/* ---------- صندوق الكتابة ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.composer-field {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: flex-end;
  padding: 3px 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 24px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.composer-field:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-tint);
}
.composer-field .icon-btn { width: 38px; height: 38px; min-width: 38px; }
.composer-field textarea {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  padding: 9px 6px;
  min-height: 42px;
  max-height: 140px;
}
.composer-field textarea::placeholder { color: var(--ink-faint); }

.send-btn {
  width: 46px;
  height: 46px;
  min-width: 46px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s ease, opacity .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.send-btn .ico { width: 21px; height: 21px; transform: scaleX(-1); } /* السهم يشير يساراً في RTL */
.send-btn:hover:not(:disabled) { background: var(--primary-dark); }
.send-btn:disabled { opacity: .4; cursor: default; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-backdrop, .chip-switch .track, .chip-switch .knob { transition: none; }
}

.status-select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-2);
  font-size: 12.5px;
  color: var(--ink);
}

/* ---------- نافذة إدارة الموظفين ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(29,38,32,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius-l);
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 15.5px; }
.modal-header button {
  border: none;
  background: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--ink-muted);
}
.modal-body { padding: 16px 18px; }

.employees-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.employee-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 13px;
}
.employee-row .emp-name { font-weight: 600; }
.employee-row .emp-role {
  font-size: 11px;
  color: var(--ink-muted);
  margin-right: 6px;
}
.employee-row button {
  border: none;
  background: none;
  color: var(--warn);
  cursor: pointer;
  font-size: 13px;
}

.add-employee-form {
  display: flex;
  flex-direction: column;
  gap: 9px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
.add-employee-form h4 { margin: 0 0 2px; font-size: 13.5px; }
.add-employee-form input,
.add-employee-form select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 13px;
  background: var(--surface-2);
}
.add-employee-form button {
  padding: 10px;
  border: none;
  border-radius: var(--radius-s);
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

/* ============================================================
   نافذة الإعدادات — تصميم قائمة/تفاصيل بأسلوب واتساب (بلمسة أرقى)
   ============================================================ */
.modal-settings { max-width: 440px; }

.settings-detail-header { gap: 10px; }
.settings-detail-header h3 { flex: 1; text-align: center; margin: 0; font-size: 15px; }
.settings-header-spacer { width: 30px; }
.settings-back-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--primary-tint);
  color: var(--primary-dark);
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transform: rotate(180deg); /* السهم يشاور يمين في RTL (رجوع) */
}
.settings-back-btn:hover { background: var(--accent-tint); }

.settings-modal-body { padding: 0; overflow: hidden; }
#settingsForm { position: relative; }

.settings-page {
  display: none;
  padding: 14px 18px 20px;
  animation: settingsFadeIn .18s ease;
}
.settings-page.active { display: block; }
@keyframes settingsFadeIn {
  from { opacity: 0; transform: translateX(8px); }
  to { opacity: 1; transform: translateX(0); }
}

/* ---------- شريط البحث ---------- */
.settings-search {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 14px;
  margin-bottom: 16px;
}
.settings-search-icon { font-size: 13px; opacity: .6; }
.settings-search input {
  flex: 1;
  border: none;
  background: none;
  outline: none;
  font-size: 13px;
  font-family: inherit;
  color: var(--ink);
}
.settings-search input::placeholder { color: var(--ink-faint); }

/* ---------- مجموعات وصفوف القائمة ---------- */
.settings-group { margin-bottom: 18px; }
.settings-group-title {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--ink-faint);
  letter-spacing: .3px;
  margin: 0 4px 6px;
  text-transform: uppercase;
}
.settings-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 10px;
  border: none;
  background: var(--surface);
  border-radius: var(--radius-m);
  cursor: pointer;
  text-align: right;
  font-family: inherit;
  transition: background .12s ease;
}
.settings-row:hover { background: var(--surface-2); }
.settings-row + .settings-row { margin-top: 3px; }
.settings-row-icon {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
}
.settings-row-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.settings-row-title { font-size: 13.5px; font-weight: 700; color: var(--ink); }
.settings-row-sub {
  font-size: 11.5px;
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.settings-row-chevron {
  font-size: 18px;
  color: var(--ink-faint);
  transform: rotate(180deg);
}
.settings-empty-search {
  text-align: center;
  padding: 30px 10px;
  color: var(--ink-faint);
  font-size: 13px;
}

/* ---------- محتوى صفحات التفاصيل ---------- */
.settings-panel { display: flex; flex-direction: column; gap: 8px; }
.settings-panel label {
  font-size: 12px;
  color: var(--ink-muted);
  margin-top: 4px;
}
.settings-sub-label { margin-top: 14px !important; padding-top: 10px; border-top: 1px dashed var(--border); }
.settings-panel input[type="text"],
.settings-panel input[type="number"],
.settings-panel input[type="time"] {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 13px;
  background: var(--surface-2);
  direction: ltr;
  text-align: right;
  font-family: inherit;
}
.settings-panel textarea {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 13px;
  background: var(--surface-2);
  direction: rtl;
  text-align: right;
  font-family: inherit;
  resize: vertical;
  min-height: 90px;
}
/* صف حقلين جنب بعض داخل صفحة تفاصيل (مثال: من/إلى الساعة، أقل/أكبر تأخير) —
   اسم مختلف عمداً عن .settings-row (زرار التنقل في القائمة) لتفادي أي تضارب بينهم */
.settings-field-row { display: flex; gap: 10px; }
.settings-field-row > div { flex: 1; display: flex; flex-direction: column; }
.settings-save-btn {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  border: none;
  border-radius: var(--radius-s);
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 13.5px;
  cursor: pointer;
  transition: background .15s ease;
}
.settings-save-btn:hover { background: var(--primary-dark); }
.settings-save-feedback {
  padding: 0 18px 14px;
  min-height: 14px;
  font-weight: 600;
}

/* ---------- بطاقة حالة اتصال واتساب ---------- */
.wa-status-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  margin-bottom: 14px;
}
.wa-status-dot { width: 11px; height: 11px; min-width: 11px; border-radius: 50%; background: var(--ink-faint); }
.wa-status-dot.connected { background: var(--primary); box-shadow: 0 0 0 4px var(--primary-tint); }
.wa-status-dot.connecting { background: var(--accent); box-shadow: 0 0 0 4px var(--accent-tint); }
.wa-status-dot.disconnected { background: var(--warn); box-shadow: 0 0 0 4px #F3E2D9; }
.wa-status-texts { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.wa-status-title { font-size: 13.5px; font-weight: 700; }
.wa-status-sub { font-size: 12px; color: var(--ink-muted); }
.settings-link-btn {
  display: block;
  text-align: center;
  padding: 12px;
  border-radius: var(--radius-s);
  background: var(--primary);
  color: #fff !important;
  font-weight: 700;
  font-size: 13.5px;
  text-decoration: none;
  margin-bottom: 12px;
}
.settings-link-btn:hover { background: var(--primary-dark); }
.settings-hint {
  font-size: 12px;
  color: var(--ink-faint);
  margin-top: 4px;
}
.settings-hint-block { line-height: 1.7; }

/* ---------- عناصر مشتركة قديمة لسه مستخدمة داخل الصفحات ---------- */
.logo-upload-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 4px;
}
.logo-preview {
  width: 56px;
  height: 56px;
  min-width: 56px;
  border-radius: var(--radius-m);
  background: var(--primary-tint);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.logo-preview img { width: 100%; height: 100%; object-fit: cover; }
.logo-upload-controls { display: flex; flex-direction: column; gap: 5px; }
.logo-upload-controls button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-2);
  font-size: 12.5px;
  cursor: pointer;
  width: fit-content;
}
.logo-upload-controls button:hover { background: var(--primary-tint); }

.status-select.full-width { width: 100%; padding: 9px 12px; }
.bulk-apply-row { display: flex; gap: 8px; margin-top: 8px; }
.bulk-apply-row select { flex: 1; }
.bulk-apply-row button {
  padding: 9px 12px;
  border: 1px solid var(--warn);
  background: #F3E2D9;
  color: var(--warn);
  border-radius: var(--radius-s);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.bulk-apply-row button:hover { background: var(--warn); color: #fff; }

.days-row { display: flex; flex-wrap: wrap; gap: 8px 12px; margin-top: 4px; }
.days-row label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--ink);
  margin-top: 0;
  cursor: pointer;
}
.days-row input[type="checkbox"] { cursor: pointer; }

/* ---------- تنبيه إيقاف الردود الآلية عالمياً (منقول لأعلى الشريط الجانبي) ---------- */
.global-pause-banner {
  margin: 0 18px 10px;
  padding: 9px 12px;
  background: #F3E2D9;
  color: var(--warn);
  border-radius: var(--radius-s);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

/* ---------- نافذة خريطة البوت ---------- */
.flow-hint {
  font-size: 12.5px;
  color: var(--ink-muted);
  margin: 0 0 10px;
  line-height: 1.6;
}
#flowEditor {
  width: 100%;
  min-height: 320px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-2);
  font-family: 'Courier New', monospace;
  font-size: 12.5px;
  direction: ltr;
  text-align: left;
  resize: vertical;
}
#saveFlowBtn {
  margin-top: 10px;
  padding: 11px;
  width: 100%;
  border: none;
  border-radius: var(--radius-s);
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
/* ============================================
   Responsive: mobile
   ============================================ */
@media (max-width: 880px) {
  .sidebar {
    width: 100%;
    min-width: 0;
    border-left: none;
  }
  .chat-panel { display: none; }
  .back-btn { display: inline-flex; }

  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat-panel { display: flex; }

  .bubble { max-width: 86%; }
  .composer-field textarea { font-size: 16px; } /* يمنع تكبير الصفحة تلقائياً على iOS عند التركيز */
}

/* ===== زرار + نافذة جهة اتصال جديدة ===== */
.new-contact-btn {
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-m);
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
}
.new-contact-btn:hover { background: var(--primary-dark); }
.new-contact-btn:active { transform: scale(0.95); }
.new-contact-btn:focus-visible { outline: 2px solid var(--primary-dark); outline-offset: 2px; }
.new-contact-btn .ico { width: 21px; height: 21px; }
.nc-field {
  width: 100%;
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-2);
  font-family: inherit;
  font-size: 14.5px;
  color: var(--ink);
}
.nc-field:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-tint);
}
textarea.nc-field { resize: vertical; min-height: 70px; }
.nc-phone-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
.nc-phone-row select.nc-field { flex: 0 0 46%; }
.nc-phone-row input.nc-field { flex: 1; min-width: 0; text-align: left; }
.nc-preview {
  min-height: 18px;
  margin: -6px 0 14px;
  font-size: 12.5px;
  color: var(--ink-muted);
  text-align: left;
}
.nc-preview.ok { color: var(--primary-dark); font-weight: 600; }
.nc-error {
  margin: 0 0 12px;
  padding: 9px 12px;
  border-radius: var(--radius-s);
  background: #F3E2D9;
  color: var(--warn);
  font-size: 13px;
  line-height: 1.5;
}
.nc-error[hidden] { display: none; }
#ncSaveBtn:disabled { opacity: 0.6; cursor: wait; }

/* روابط داخل الرسائل (قابلة للضغط) */
.bubble-text .msg-link {
  color: #0B57D0;
  text-decoration: underline;
  text-underline-offset: 2px;
  overflow-wrap: anywhere;
  word-break: break-word;
  cursor: pointer;
  unicode-bidi: isolate;
}
.bubble-text .msg-link:hover { color: #0842A0; }

/* ===== النسخ الاحتياطي والبيانات ===== */
.bk-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 6px; }
.bk-stat {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 12px 6px; border: 1px solid var(--border); border-radius: var(--radius-s);
  background: var(--surface-2);
}
.bk-stat b { font-size: 18px; color: var(--ink); direction: ltr; }
.bk-stat span { font-size: 11.5px; color: var(--ink-muted); }
.bk-section-title {
  margin: 14px 0 4px; font-size: 12.5px; font-weight: 700; color: var(--ink-muted);
}
.bk-btn {
  display: block; width: 100%; box-sizing: border-box; text-align: center;
  padding: 11px 12px; border: none; border-radius: var(--radius-s);
  font-family: inherit; font-size: 13.5px; font-weight: 700; cursor: pointer;
  text-decoration: none; transition: background 0.15s ease, opacity 0.15s ease;
}
.bk-btn-primary { background: var(--primary); color: #fff !important; }
.bk-btn-primary:hover { background: var(--primary-dark); }
.bk-btn-danger { background: #F3E2D9; color: var(--warn) !important; }
.bk-btn-danger:hover { background: #EBD0C3; }
.bk-btn:disabled { opacity: 0.55; cursor: wait; }
.bk-check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--ink); cursor: pointer; margin: 2px 0 8px; }
.bk-check input { width: 17px; height: 17px; accent-color: var(--primary); }
.bk-auto { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.bk-keep { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-muted); }
.bk-keep select {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius-s);
  background: var(--surface-2); font-family: inherit; font-size: 13px; color: var(--ink);
}
.bk-auto-info { font-size: 12px; color: var(--ink-faint); margin-top: 2px; }
.bk-status {
  margin-top: 8px; padding: 9px 12px; border-radius: var(--radius-s);
  background: var(--primary-tint); color: var(--primary-dark); font-size: 13px; line-height: 1.6;
}
.bk-status.err { background: #F3E2D9; color: var(--warn); }
.bk-status[hidden] { display: none; }
.bk-list { display: flex; flex-direction: column; gap: 8px; }
.bk-empty { padding: 14px; text-align: center; font-size: 13px; color: var(--ink-faint); }
.bk-item {
  border: 1px solid var(--border); border-radius: var(--radius-s); background: var(--surface-2);
  padding: 10px 12px; display: flex; flex-direction: column; gap: 8px;
}
.bk-item-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.bk-item-title { font-size: 13.5px; font-weight: 700; color: var(--ink); }
.bk-item-sub { font-size: 12px; color: var(--ink-muted); direction: rtl; }
.bk-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--primary-tint);
  color: var(--primary-dark); font-weight: 700;
}
.bk-badge.media { background: #DCE6F0; color: #24507A; }
.bk-item-actions { display: flex; gap: 6px; }
.bk-item-actions button, .bk-item-actions a {
  flex: 1; text-align: center; padding: 7px 6px; border-radius: var(--radius-s); border: 1px solid var(--border);
  background: var(--surface); font-family: inherit; font-size: 12.5px; font-weight: 700; color: var(--ink);
  cursor: pointer; text-decoration: none;
}
.bk-item-actions .bk-act-restore { color: var(--primary-dark); }
.bk-item-actions .bk-act-delete { color: var(--warn); }
.bk-item-actions button:hover, .bk-item-actions a:hover { background: var(--primary-tint); }
.bk-restore-file { display: flex; flex-direction: column; gap: 8px; }
.bk-restore-file input[type="file"] { font-family: inherit; font-size: 12.5px; max-width: 100%; }

/* ===== المستندات الجاهزة ===== */
.library-popup { max-height: min(300px, 48%); }
.library-search {
  width: 100%; box-sizing: border-box; margin-bottom: 8px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-s); background: var(--surface-2);
  font-family: inherit; font-size: 14px; color: var(--ink);
}
.library-search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint); }
.library-search[hidden] { display: none; }
.library-popup-status {
  margin-bottom: 8px; padding: 8px 12px; border-radius: var(--radius-s);
  background: var(--primary-tint); color: var(--primary-dark); font-size: 13px; line-height: 1.5;
}
.library-popup-status.err { background: #F3E2D9; color: var(--warn); }
.library-popup-status[hidden] { display: none; }
.library-popup-list { display: flex; flex-direction: column; gap: 6px; }
.library-popup-list.busy { opacity: .55; pointer-events: none; }
.lib-card {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: right;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius-s);
  background: var(--surface-2); font-family: inherit; cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.lib-card:hover { background: var(--primary-tint); border-color: var(--primary); }
.lib-thumb {
  width: 46px; height: 46px; flex-shrink: 0; border-radius: 10px; object-fit: cover; background: var(--primary-tint);
}
.lib-thumb-icon { display: flex; align-items: center; justify-content: center; font-size: 22px; }
.lib-card-body, .lib-row-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.lib-card-title { font-size: 13.5px; font-weight: 700; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-card-meta { font-size: 12px; color: var(--ink-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-card-send {
  flex-shrink: 0; padding: 6px 12px; border-radius: 999px; background: var(--primary); color: #fff;
  font-size: 12.5px; font-weight: 700;
}
.lib-card:hover .lib-card-send { background: var(--primary-dark); }

.library-modal { max-width: 480px; }
.lib-btn {
  display: block; width: 100%; box-sizing: border-box; text-align: center; padding: 11px 12px;
  border: none; border-radius: var(--radius-s); font-family: inherit; font-size: 13.5px; font-weight: 700; cursor: pointer;
}
.lib-btn-primary { background: var(--primary); color: #fff; }
.lib-btn-primary:hover { background: var(--primary-dark); }
.lib-btn-soft { background: var(--primary-tint); color: var(--primary-dark); }
.lib-btn-ghost { background: transparent; color: var(--ink-muted); border: 1px solid var(--border); }
.lib-btn:disabled { opacity: .6; cursor: wait; }
.lib-manage-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.lib-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-s); background: var(--surface-2);
}
.lib-row-actions { display: flex; gap: 6px; flex-shrink: 0; }
.lib-row-actions button {
  padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--radius-s); background: var(--surface);
  font-family: inherit; font-size: 12.5px; font-weight: 700; color: var(--ink); cursor: pointer;
}
.lib-row-actions button:hover { background: var(--primary-tint); }
.lib-row-actions button.danger { color: var(--warn); }
.lib-form { display: flex; flex-direction: column; gap: 6px; }
.lib-form[hidden], #libraryListView[hidden], .lib-error[hidden] { display: none; }
.lib-form input[type="text"], .lib-form textarea {
  width: 100%; box-sizing: border-box; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-s); background: var(--surface-2); font-family: inherit; font-size: 14.5px; color: var(--ink);
}
.lib-form input[type="text"]:focus, .lib-form textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-tint);
}
.lib-label { font-size: 12.5px; font-weight: 700; color: var(--ink-muted); margin-top: 8px; }
.lib-chips { display: flex; flex-direction: column; gap: 6px; }
.lib-chip {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: var(--radius-s); background: var(--surface-2); font-size: 13px;
}
.lib-chip.new { border-color: var(--primary); background: var(--primary-tint); }
.lib-chip-icon { flex-shrink: 0; }
.lib-chip-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.lib-chip-size { flex-shrink: 0; font-size: 11.5px; color: var(--ink-muted); }
.lib-chip button {
  flex-shrink: 0; width: 26px; height: 26px; border: none; border-radius: 50%; background: transparent;
  color: var(--ink-muted); cursor: pointer; font-size: 13px;
}
.lib-chip button:hover { background: #F3E2D9; color: var(--warn); }
.lib-pick { margin-top: 6px; cursor: pointer; }
.lib-error {
  margin-top: 6px; padding: 9px 12px; border-radius: var(--radius-s); background: #F3E2D9;
  color: var(--warn); font-size: 13px; line-height: 1.5;
}
.lib-actions { display: flex; gap: 8px; margin-top: 10px; }
.lib-actions .lib-btn-ghost { width: 36%; }

/* ===== مؤشر تحميل المحادثة (بيظهر لحظات بدل الشاشة الفاضية) ===== */
.thread-loading { display: flex; justify-content: center; gap: 6px; padding: 28px 0; }
.thread-loading span {
  width: 8px; height: 8px; border-radius: 50%; background: var(--primary); opacity: .25;
  animation: thread-dot 1s ease-in-out infinite;
}
.thread-loading span:nth-child(2) { animation-delay: .15s; }
.thread-loading span:nth-child(3) { animation-delay: .3s; }
@keyframes thread-dot { 0%, 80%, 100% { opacity: .25; transform: scale(1); } 40% { opacity: 1; transform: scale(1.25); } }
@media (prefers-reduced-motion: reduce) { .thread-loading span { animation: none; opacity: .6; } }


/* ===== نوع القضية ===== */
.case-filter-row{display:flex;gap:8px;align-items:center;padding:0 12px 8px}
.case-filter-row select{flex:1;min-width:0;padding:7px 10px;border:1px solid var(--border);border-radius:10px;background:var(--surface);font-family:inherit;font-size:13px;color:var(--ink)}
.case-filter-row .adv-search{font-size:12.5px;color:var(--primary);text-decoration:none;white-space:nowrap;font-weight:700}
.case-pill{display:inline-block;max-width:110px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border-radius:99px;padding:1px 8px;font-size:11px;font-weight:700;color:#fff;vertical-align:middle}
.chip.case-chip.is-empty{opacity:.75;border-style:dashed}
.chip.case-chip .dot{width:9px;height:9px;border-radius:50%;display:inline-block;flex:none}
