/* Purefon Chat — shell inspired by WhatsApp Web (layout only, RTL) */
:root {
  --bg-app: #0b141a;
  --bg-panel: #111b21;
  --bg-hover: #202c33;
  --border: #2a3942;
  --text: #e9edef;
  --text-muted: #8696a0;
  --accent: #25d366;
  --bubble-in: #202c33;
  --bubble-out: #005c4b;
  --danger: #ea0038;
  --radius: 8px;
  --font: "Segoe UI", "Tahoma", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  background: var(--bg-app);
  color: var(--text);
  direction: rtl;
}

a {
  color: var(--accent);
}

.app-shell {
  display: flex;
  flex: 1;
  min-height: 0;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  border-inline: 1px solid var(--border);
}

.sidebar {
  width: min(380px, 38vw);
  min-width: 300px;
  min-height: 0;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 12px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #25d366, #128c7e);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
}

.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.brand-text p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.status-pill {
  margin-inline-start: auto;
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-hover);
  color: var(--text-muted);
}

.status-pill.ok {
  color: var(--accent);
  border: 1px solid rgba(37, 211, 102, 0.35);
}

.status-pill.err {
  color: #ffb4b4;
  border: 1px solid rgba(234, 0, 56, 0.35);
}

.search-wrap {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.search-wrap input {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: none;
  background: var(--bg-hover);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.search-wrap input::placeholder {
  color: var(--text-muted);
}

.conv-list {
  flex: 1;
  overflow-y: auto;
}

.conv-item {
  display: flex;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(42, 57, 66, 0.6);
  transition: background 0.15s;
}

.conv-item:hover,
.conv-item.active {
  background: var(--bg-hover);
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #3d4f5c;
  display: grid;
  place-items: center;
  font-size: 18px;
  flex-shrink: 0;
}

.conv-body {
  flex: 1;
  min-width: 0;
}

.conv-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.conv-unread {
  flex-shrink: 0;
  min-width: 28px;
  text-align: center;
}

.conv-name {
  flex: 1;
  min-width: 0;
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.conv-snippet {
  margin-top: 4px;
  font-size: 14px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.unread-badge {
  margin-inline-start: 8px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #0b141a;
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260' viewBox='0 0 260 260'%3E%3Cpath fill='%23182229' fill-opacity='0.35' d='M0 0h260v260H0z'/%3E%3C/svg%3E");
}

/* عمود المحادثة: لازم min-height و overflow عشان منطقة الرسائل تاخد سكرول */
.chat-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.main-empty {
  flex: 1;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
}

.main-empty h2 {
  margin: 0 0 8px;
  font-weight: 400;
  color: var(--text);
}

.chat-header {
  padding: 12px 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

/* زر العودة لقائمة الأرقام — يظهر على الموبايل فقط عند فتح محادثة */
.btn-back-list {
  display: none;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin: 0;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-hover);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.btn-back-list:hover {
  background: var(--border);
}

.chat-header-text {
  flex: 1;
  min-width: 0;
}

.chat-header .title {
  font-size: 16px;
  font-weight: 500;
}

.chat-header .sub {
  font-size: 13px;
  color: var(--text-muted);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.day-sep {
  align-self: center;
  font-size: 12px;
  color: var(--text-muted);
  background: rgba(17, 27, 33, 0.85);
  padding: 4px 12px;
  border-radius: 8px;
  margin: 8px 0;
}

.msg-row {
  display: flex;
  width: 100%;
}

.msg-row.in {
  justify-content: flex-start;
}

.msg-row.out {
  justify-content: flex-end;
}

.bubble {
  max-width: min(560px, 85%);
  padding: 8px 12px 10px;
  border-radius: 8px;
  font-size: 14.5px;
  line-height: 1.45;
  position: relative;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}

.msg-row.in .bubble {
  background: var(--bubble-in);
  border-top-right-radius: 2px;
}

.msg-row.out .bubble {
  background: var(--bubble-out);
  border-top-left-radius: 2px;
}

.bubble .meta {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 4px;
  font-size: 11px;
  color: rgba(233, 237, 239, 0.65);
}

.composer {
  flex-shrink: 0;
  padding: 12px 20px 16px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 160px;
  padding: 12px 14px;
  border-radius: 8px;
  border: none;
  background: var(--bg-hover);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  outline: none;
}

.composer .btn-attach {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-hover);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.composer .btn-attach:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.composer .btn-attach:not(:disabled):hover {
  background: var(--border);
}

.composer button.send {
  min-width: 96px;
  height: 44px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #0b141a;
  font-weight: 600;
  cursor: pointer;
}

.composer button.send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.banner-remote {
  margin: 12px 16px 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: rgba(255, 193, 7, 0.12);
  border: 1px solid rgba(255, 193, 7, 0.35);
  color: #ffe082;
  font-size: 13px;
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

.token-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 16px;
}

.token-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  max-width: 420px;
  width: 100%;
}

.token-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.token-card p {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

.token-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-hover);
  color: var(--text);
  font-size: 14px;
  margin-bottom: 12px;
}

.token-card button.primary {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #0b141a;
  font-weight: 600;
  cursor: pointer;
}

.bubble img.media-thumb {
  max-width: min(280px, 72vw);
  border-radius: 6px;
  display: block;
  margin-bottom: 6px;
}

.bubble video.media-video,
.bubble audio.media-audio {
  max-width: min(320px, 88vw);
  border-radius: 8px;
  display: block;
  margin-bottom: 6px;
}

.bubble a.media-doc-link {
  color: var(--accent);
  font-weight: 500;
  word-break: break-word;
  text-decoration: underline;
}

.source-tag {
  font-size: 10px;
  color: var(--text-muted);
  margin-inline-start: 8px;
  vertical-align: middle;
}

.app-toolbar {
  flex-shrink: 0;
  max-width: 1600px;
  margin: 0 auto;
  padding: 8px 16px 0;
  direction: rtl;
}

.btn-toolbar {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
}

.btn-toolbar:hover {
  background: var(--bg-hover);
}

.system-dashboard {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(0, 0, 0, 0.75);
  display: grid;
  place-items: center;
  padding: 16px;
  overflow-y: auto;
}

.system-dashboard.hidden {
  display: none;
}

.system-dashboard-inner {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 720px;
  width: 100%;
  padding: 20px;
  max-height: 90vh;
  overflow: auto;
}

.system-dashboard-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.system-dashboard-head h2 {
  margin: 0;
  font-size: 18px;
}

.btn-ghost {
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.diag-intro {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.55;
  margin: 0 0 12px;
}

.diag-actions {
  margin-bottom: 12px;
}

.diag-pre {
  margin: 0;
  padding: 14px;
  border-radius: 8px;
  background: var(--bg-app);
  border: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  direction: ltr;
  text-align: left;
}

@media (max-width: 720px) {
  .banner-remote {
    margin: 8px 12px 0;
    font-size: 12px;
  }

  .sidebar {
    width: 100%;
    min-width: 0;
    border-left: none;
  }

  .app-shell {
    flex-direction: column;
  }

  .app-shell:not(.mobile-chat-open) .sidebar {
    flex: 1;
    min-height: 0;
  }

  /* قائمة الأرقام فقط */
  .app-shell:not(.mobile-chat-open) .main {
    display: none !important;
  }

  /* شاشة المحادثة فقط */
  .app-shell.mobile-chat-open .sidebar {
    display: none !important;
  }

  .app-shell.mobile-chat-open .main {
    display: flex !important;
    flex: 1;
    min-height: 0;
  }

  .app-shell.mobile-chat-open .btn-back-list {
    display: inline-flex;
  }
}
