/* Admin top navbar — desktop & mobile */
:root {
  --admin-topbar-h: 3.25rem;
  --admin-topbar-bg: rgba(255, 255, 255, 0.96);
  --admin-topbar-border: var(--admin-border, #e2e8f0);
}

.admin-topbar {
  position: sticky;
  top: 0;
  z-index: 45;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: nowrap;
  gap: 0.5rem;
  min-height: var(--admin-topbar-h);
  padding: 0.5rem 0.75rem;
  padding-top: max(0.5rem, env(safe-area-inset-top, 0px));
  padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
  padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
  background: var(--admin-topbar-bg);
  border-bottom: 1px solid var(--admin-topbar-border);
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  flex-shrink: 0;
  overflow: hidden;
}

@media (min-width: 640px) {
  .admin-topbar {
    padding-left: max(1.5rem, env(safe-area-inset-left, 0px));
    padding-right: max(1.5rem, env(safe-area-inset-right, 0px));
  }
}

.admin-topbar__start {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

.admin-topbar__actions {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 0.25rem;
  flex: 0 0 auto;
  min-width: 0;
  padding-inline-start: 0.5rem;
  border-inline-start: 1px solid rgba(148, 163, 184, 0.35);
}

@media (min-width: 640px) {
  .admin-topbar__actions {
    gap: 0.375rem;
    padding-inline-start: 0.75rem;
  }
}

.admin-topbar__pwa {
  display: inline-flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 0.25rem;
  flex-shrink: 0;
}

.admin-topbar__pwa.hidden {
  display: none !important;
}

.admin-topbar__action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  min-width: 2.25rem;
  min-height: 2.25rem;
  padding: 0.375rem 0.5rem;
  border-radius: 0.625rem;
  border: 1px solid var(--admin-border, #e2e8f0);
  background: #fff;
  color: var(--admin-text, #0f172a);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.admin-topbar__action-btn:hover {
  background: var(--admin-bg, #f8fafc);
  border-color: #cbd5e1;
}

.admin-topbar__action-btn--primary {
  background: var(--admin-primary, #128C7E);
  border-color: var(--admin-primary, #128C7E);
  color: #fff;
}

.admin-topbar__action-btn--primary:hover {
  opacity: 0.92;
  background: var(--admin-primary, #128C7E);
  border-color: var(--admin-primary, #128C7E);
  color: #fff;
}

.admin-topbar__action-label {
  display: none;
}

@media (min-width: 900px) {
  .admin-topbar__action-btn {
    min-height: 2.375rem;
    padding: 0.4375rem 0.75rem;
    font-size: 0.8125rem;
  }

  .admin-topbar__action-label {
    display: inline;
  }
}

.admin-topbar__back-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  flex-shrink: 0;
  border-radius: 0.75rem;
  border: 1px solid var(--admin-border, #e2e8f0);
  background: var(--admin-bg, #f8fafc);
  color: var(--admin-text, #0f172a);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.admin-topbar__back-btn:hover {
  background: #fff;
  border-color: #cbd5e1;
}

@media (min-width: 1024px) {
  .admin-topbar__back-btn {
    display: none;
  }
}

.admin-topbar__menu-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  flex-shrink: 0;
  border-radius: 0.75rem;
  border: 1px solid var(--admin-border, #e2e8f0);
  background: var(--admin-bg, #f8fafc);
  color: var(--admin-text, #0f172a);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.admin-topbar__menu-btn:hover {
  background: #fff;
  border-color: #cbd5e1;
}

.admin-topbar__menu-btn:active {
  transform: scale(0.96);
}

@media (min-width: 1024px) {
  .admin-topbar__menu-btn {
    display: none;
  }
}

.admin-topbar__inbox-badge {
  display: none;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.625rem;
  background: rgba(18, 140, 126, 0.12);
  color: #128C7E;
  font-size: 1.125rem;
  flex-shrink: 0;
}

.admin-topbar__title {
  margin: 0;
  min-width: 0;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--admin-text, #0f172a);
  font-family: "Cairo", "Segoe UI", Tahoma, sans-serif;
}

@media (min-width: 640px) {
  .admin-topbar__title {
    font-size: 1.125rem;
  }
}

.admin-topbar__user {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  min-width: 0;
  max-width: min(28vw, 7rem);
  flex-shrink: 1;
}

@media (min-width: 640px) {
  .admin-topbar__user {
    max-width: 12rem;
  }
}

@media (min-width: 900px) {
  .admin-topbar__user {
    max-width: 14rem;
  }
}

.admin-topbar__user-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  flex-shrink: 0;
  border-radius: 999px;
  border: 1px solid var(--admin-border, #e2e8f0);
  background: #f1f5f9;
  color: #64748b;
  font-size: 1rem;
}

.admin-topbar__user-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--admin-text, #0f172a);
  font-family: "Cairo", "Segoe UI", Tahoma, sans-serif;
}

@media (max-width: 479px) {
  .admin-topbar__user-name {
    display: none;
  }

  .admin-topbar__title {
    font-size: 0.875rem;
  }
}

.admin-topbar__logout {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  min-width: 2.25rem;
  min-height: 2.25rem;
  padding: 0.375rem 0.5rem;
  border-radius: 0.625rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--admin-text-muted, #64748b);
  text-decoration: none;
  flex-shrink: 0;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.admin-topbar__logout:hover {
  background: #fef2f2;
  color: #dc2626;
}

@media (min-width: 900px) {
  .admin-topbar__logout {
    padding: 0.4375rem 0.75rem;
    font-size: 0.8125rem;
  }
}

/* Mobile: fullscreen chat — hide filters when chat is open */
@media (max-width: 768px) {
  body.chat-mobile-open .msg-inbox-filter-nav,
  body.chat-mobile-open .msg-filter-scroll-wrap {
    display: none !important;
  }

  body.chat-mobile-open main > .flex-1 {
    padding: 0 !important;
  }

  body.chat-mobile-open .msg-v2 {
    padding: 0 !important;
    min-height: 100dvh !important;
    background: var(--wa-panel-bg, #e8e4df);
  }

  body.chat-mobile-open .msg-v2 .msg-chat-layout.chat-wrapper {
    min-height: 100dvh !important;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    border: none !important;
    box-shadow: none !important;
  }

  body.chat-mobile-open .msg-v2 .msg-chat-sidebar.chat-sidebar,
  body.chat-mobile-open .msg-v2 .msg-sidebar-head {
    border-radius: 0 !important;
  }

  body.chat-mobile-open .chat-back-btn {
    display: inline-flex !important;
  }
}

/* Sidebar footer — user, PWA, logout */
.sidebar-footer {
  background: rgba(0, 0, 0, 0.12);
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.75rem;
  border-radius: 0.75rem;
  background: rgba(255, 255, 255, 0.06);
}

.sidebar-user__avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: #e2e8f0;
  flex-shrink: 0;
}

.sidebar-user__meta {
  min-width: 0;
  flex: 1;
}

.sidebar-user__name {
  font-size: 0.875rem;
  font-weight: 600;
  color: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-user__email {
  font-size: 0.6875rem;
  color: rgba(255, 255, 255, 0.55);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer-actions {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.sidebar-footer-actions.hidden {
  display: none !important;
}

.sidebar-footer-btn {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  width: 100%;
  padding: 0.625rem 0.75rem;
  border-radius: 0.625rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.06);
  color: #e2e8f0;
  font-size: 0.8125rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.sidebar-footer-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.sidebar-footer-btn--primary {
  background: rgba(18, 140, 126, 0.28);
  border-color: rgba(18, 140, 126, 0.45);
  color: #d1fae5;
}

.sidebar-footer-btn--primary:hover {
  background: rgba(18, 140, 126, 0.4);
  color: #fff;
}

.sidebar-footer-btn--logout:hover {
  background: rgba(239, 68, 68, 0.18);
  border-color: rgba(239, 68, 68, 0.35);
  color: #fecaca;
}

.sidebar-footer-btn.hidden {
  display: none !important;
}

/* Inbox page: admin sidebar above chat panels */
body.msg-page-active #sidebar.sidebar {
  z-index: 60;
}
body.msg-page-active #sidebarBackdrop:not(.hidden) {
  z-index: 55;
}

/* Sidebar mobile overlay */
@media (max-width: 1023px) {
  #sidebar.sidebar {
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.18);
  }

  #sidebarBackdrop:not(.hidden) {
    backdrop-filter: blur(2px);
  }
}

/* —— Language switcher —— */
.admin-lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  padding: 0.15rem;
  border-radius: 0.65rem;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  flex-shrink: 0;
}
.admin-lang-switch__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.25rem;
  padding: 0.35rem 0.65rem;
  border-radius: 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: #64748b;
  text-decoration: none;
  transition: background .15s, color .15s;
}
.admin-lang-switch__btn:hover {
  color: #0f172a;
  background: #fff;
}
.admin-lang-switch__btn.is-active {
  background: #2563eb;
  color: #fff;
}
.admin-lang-switch--sidebar {
  width: 100%;
  justify-content: center;
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.12);
}
.admin-lang-switch--sidebar .admin-lang-switch__btn {
  flex: 1;
  color: rgba(255, 255, 255, 0.7);
}
.admin-lang-switch--sidebar .admin-lang-switch__btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}
.admin-lang-switch--sidebar .admin-lang-switch__btn.is-active {
  background: rgba(37, 99, 235, 0.9);
  color: #fff;
}

.sidebar-close-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  flex-shrink: 0;
  border-radius: 0.65rem;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-close-btn:hover {
  background: rgba(255, 255, 255, 0.16);
}

/* —— Admin sidebar drawer: RIGHT → LEFT open/close ——
   IMPORTANT: do NOT use inset-inline here — in RTL it flips
   left/right and leaves a stuck sliver of the menu on screen.
*/
#sidebar.sidebar {
  top: 0;
  bottom: 0;
  width: 280px;
  max-width: min(280px, 88vw);
  will-change: transform;
  -webkit-overflow-scrolling: touch;
  transition: transform 0.28s ease-out, visibility 0.28s;
}

@media (max-width: 1023px) {
  #sidebar.sidebar {
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    /* Always dock to the physical RIGHT edge */
    right: 0 !important;
    left: auto !important;
    margin: 0 !important;
    z-index: 50;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.18);
  }

  /* Closed: fully off-screen to the RIGHT — must beat open selectors */
  #sidebar.sidebar.sidebar-closed,
  html.admin-sidebar-open #sidebar.sidebar.sidebar-closed {
    transform: translateX(110%) !important;
    visibility: hidden !important;
    pointer-events: none !important;
    box-shadow: none !important;
  }

  /* Open: slide left into view */
  #sidebar.sidebar.sidebar-open:not(.sidebar-closed) {
    transform: translateX(0) !important;
    visibility: visible;
    pointer-events: auto;
  }

  html.admin-sidebar-open,
  html.admin-sidebar-open body {
    overflow: hidden !important;
  }

  #sidebarBackdrop {
    z-index: 48;
  }
  html.admin-sidebar-open #sidebarBackdrop {
    display: block !important;
  }
  html.admin-sidebar-open body.msg-page-active main > .msg-inbox-filter-nav {
    z-index: 30;
  }

  body.pwa-standalone #sidebar.sidebar {
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  body.pwa-standalone .admin-topbar {
    padding-top: max(0.5rem, env(safe-area-inset-top, 0px));
  }
}

@media (min-width: 1024px) {
  html:not(.admin-sidebar-collapsed) #sidebar.sidebar,
  html:not(.admin-sidebar-collapsed) #sidebar.sidebar.sidebar-closed,
  html:not(.admin-sidebar-collapsed) #sidebar.sidebar.sidebar-open {
    transform: none !important;
    position: relative !important;
    left: auto !important;
    right: auto !important;
    visibility: visible !important;
    pointer-events: auto !important;
    box-shadow: none;
    display: flex !important;
    width: 280px;
    max-width: min(280px, 88vw);
    min-width: 280px;
    transition: transform 0.28s ease-out, visibility 0.28s;
  }

  html.admin-sidebar-collapsed #sidebar.sidebar,
  html.admin-sidebar-collapsed #sidebar.sidebar.sidebar-open,
  html.admin-sidebar-collapsed #sidebar.sidebar.sidebar-closed,
  body.admin-sidebar-collapsed #sidebar.sidebar {
    display: none !important;
    width: 0 !important;
    min-width: 0 !important;
    max-width: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    overflow: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
    visibility: hidden !important;
    border: none !important;
  }

  #sidebarBackdrop {
    display: none !important;
  }
}

/* —— Arabic / RTL UI polish —— */
html[dir="rtl"] body.locale-ar,
body.locale-ar {
  font-family: "IBM Plex Sans Arabic", "Cairo", Tahoma, system-ui, sans-serif;
}

html[dir="rtl"] .admin-topbar__back-btn i {
  transform: scaleX(-1);
}

html[dir="rtl"] .admin-topbar__title {
  text-align: right;
}

html[dir="rtl"] #sidebar.sidebar .ms-auto {
  margin-inline-start: auto;
  margin-inline-end: 0;
}

@media (max-width: 1023px) {
  .admin-topbar__actions {
    max-width: 45vw;
  }
  .admin-lang-switch__btn {
    min-width: 1.85rem;
    padding: 0.3rem 0.45rem;
    font-size: 0.7rem;
  }
}

/* Toasts: stay on-screen on small phones (LTR + RTL) */
#whatsapp-toast-container {
  inset-inline-end: 1rem;
  inset-inline-start: auto;
  top: max(1rem, env(safe-area-inset-top, 0px));
  width: min(380px, calc(100vw - 2rem));
  max-width: calc(100vw - 2rem);
  pointer-events: none;
}
#whatsapp-toast-container > * {
  pointer-events: auto;
}
@media (max-width: 480px) {
  #whatsapp-toast-container {
    inset-inline-end: 0.75rem;
    inset-inline-start: 0.75rem;
    width: auto;
  }
}

/* Messages inbox RTL */
html[dir="rtl"] .msg-chat-layout.chat-wrapper {
  direction: rtl;
}
html[dir="rtl"] .msg-chat-sidebar.chat-sidebar,
html[dir="rtl"] .msg-v2 .msg-no-chat-selected .msg-chat-sidebar {
  border-right: 0;
  border-left: 1px solid var(--wa-sidebar-border, #e4e6eb);
}
html[dir="rtl"] .msg-chat-sidebar a.msg-sidebar-conv.active {
  border-left: 0 !important;
  border-right: 4px solid var(--wa-green, #128C7E);
  padding-left: 16px;
  padding-right: 12px;
}
html[dir="rtl"] .chat-back-btn i,
html[dir="rtl"] .msg-mobile-back-btn i,
html[dir="rtl"] .msg-inbox-list-back i {
  transform: scaleX(-1);
  display: inline-block;
}
html[dir="rtl"] .msg-theme-toggle,
html[dir="rtl"] .msg-sidebar-head-top .msg-theme-toggle {
  margin-left: 0;
  margin-right: auto;
}
html[dir="rtl"] .msg-filter-scroll-wrap {
  mask-image: linear-gradient(to left, transparent, #000 12px, #000 calc(100% - 12px), transparent);
}
html[dir="rtl"] .admin-content-wrap,
html[dir="rtl"] .admin-topbar__title {
  text-align: right;
}

/* PWA banners: logical sides so Arabic doesn’t clip */
html[dir="rtl"] #pwa-ios-guide,
html[dir="rtl"] #pwa-update-banner {
  left: 1rem;
  right: 1rem;
}
@media (min-width: 640px) {
  html[dir="rtl"] #pwa-ios-guide,
  html[dir="rtl"] #pwa-update-banner {
    left: 1rem;
    right: auto;
  }
}
