/* دردشة الغرفة — إضافة فقط.
   نفس الألوان والخطوط والحدود والأقطار المستخدمة في بقية الشاشات.
   الشريط ثابت أسفل الشاشة، والرسائل في ورقة تُفتح فوقه بسلاسة. */
:root { --chatbar-h: 64px; }

.chat-card {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 30;
  max-width: var(--app-max);
  margin-inline: auto;
  /* على iOS تغطّي لوحة المفاتيح الشريط — نرفعه بقيمة visualViewport */
  transform: translateY(calc(-1 * var(--kb, 0px)));
  will-change: transform;
  pointer-events: none;
}

/* ---------- الشريط السفلي ---------- */
.chat-head {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 54px;
  padding: 10px var(--pad) calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(21, 24, 31, .96) 0%, #12161E 62%);
  border-top: 1px solid var(--border-soft);
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 28px rgba(0, 0, 0, .34);
  color: inherit;
  font-family: inherit;
  text-align: start;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}
.chat-head:active { background: #171A21; }
.chat-title {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  flex: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.chat-title .ico { color: var(--gold-text); }
.chat-peek {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-side { display: inline-flex; align-items: center; gap: 8px; flex: none; }
.chat-badge {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-pill);
  background: #27241C;
  border: 1px solid var(--gold-border-2);
  color: var(--gold-text);
  font-size: 11px;
  font-weight: 700;
}
.chat-badge[hidden] { display: none; }
.chat-caret {
  color: var(--muted);
  transition: transform var(--speed) var(--ease);
}
.chat-card.is-open .chat-caret { transform: rotate(180deg); }

/* ---------- ورقة الرسائل ---------- */
.chat-body {
  pointer-events: none;
  position: absolute;
  bottom: calc(100% + 8px);
  inset-inline: 10px;
  display: grid;
  gap: 8px;
  padding: 10px;
  background: rgba(21, 24, 31, .98);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md);
  box-shadow: 0 -16px 38px rgba(0, 0, 0, .48);
  opacity: 0;
  visibility: hidden;
  transform: translateY(12px) scale(.985);
  transform-origin: bottom center;
  /* visibility تُؤجَّل إلى نهاية الإغلاق فقط، وتُطبَّق فورًا عند الفتح
     حتى يقبل حقل الإدخال التركيز مباشرة (iOS) */
  transition: opacity var(--speed) var(--ease),
              transform var(--speed) var(--ease),
              visibility 0s linear var(--speed);
}
.chat-card.is-open .chat-body {
  pointer-events: auto;
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity var(--speed) var(--ease),
              transform var(--speed) var(--ease),
              visibility 0s;
}

.chat-list {
  display: grid;
  align-content: start;
  gap: 7px;
  padding: 2px 2px 4px;
  min-height: 0;
  max-height: min(42vh, 260px);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--r-pill); }
.chat-empty {
  padding: 16px 0 10px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* ---------- الفقاعات ---------- */
.chat-msg { display: flex; justify-content: flex-start; }
.chat-msg.is-mine { justify-content: flex-end; }
.chat-bubble {
  display: grid;
  gap: 2px;
  max-width: 84%;
  padding: 7px 11px 5px;
  border-radius: 16px 16px 16px 5px;
  background: var(--surface-3);
  border: 1px solid var(--border-line);
}
.chat-msg.is-mine .chat-bubble {
  background: #292215;
  border-color: var(--gold-border-3);
  border-radius: 16px 16px 5px 16px;
}
.chat-name {
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-msg.is-mine .chat-name { color: var(--gold-text); }
.chat-text {
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  user-select: text;
  -webkit-user-select: text;
}
.chat-time {
  justify-self: end;
  font-size: 10px;
  line-height: 1.4;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* ---------- الكتابة ---------- */
.chat-compose {
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-input {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  /* 16px يمنع تكبير iPhone التلقائي عند التركيز (سبب فقدان المؤشر) */
  font-size: 16px;
  line-height: 1.4;
  border-radius: 13px;
}
.chat-input:disabled { opacity: .55; }
.chat-send {
  width: 44px;
  height: 44px;
  padding: 0;
  flex: none;
  border-radius: 13px;
}
.chat-card.is-off .chat-send { box-shadow: none; }
.chat-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 2px 2px;
  font-size: 11px;
  color: var(--muted);
}
.chat-hint { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-count { flex: none; font-variant-numeric: tabular-nums; }

/* الغرفة تحجز مكان الشريط حتى لا يغطّي زر الجاهز أو أي عنصر */
.screen.lobby { padding-bottom: calc(86px + env(safe-area-inset-bottom)); }

/* منع سحب/تحديد العناصر وتحريك الشاشة بالخطأ داخل الغرفة */
html.chat-open,
html.chat-open body { overscroll-behavior: none; }
html.chat-open .bg-layer { touch-action: none; }

@media (max-width: 380px) {
  .chat-list { max-height: min(36vh, 210px); }
  .chat-meta { font-size: 10px; }
  .chat-peek { font-size: 11px; }
}
