/* assets/chat.css */

/* زر الدردشة العائم */
#chatFab {
  position: fixed;
  inset-inline-end: 24px;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  z-index: 1050;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 0;
  background: var(--primary);
  color: var(--on-primary);
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 10px 28px color-mix(in srgb, var(--primary) 40%, transparent);
  transition: transform 0.25s var(--ease), filter 0.25s;
}

#chatFab::before {
  content: "";
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 2px solid var(--primary);
  opacity: 0.4;
  animation: chatPulse 2.4s ease-out infinite;
  pointer-events: none;
}

@keyframes chatPulse {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(1.6); opacity: 0; }
}

#chatFab:hover {
  transform: translateY(-4px) scale(1.05);
  filter: brightness(1.1);
}

#chatFab.hidden {
  opacity: 0;
  visibility: hidden;
  transform: scale(0.5);
}

.fab-badge {
  position: absolute;
  top: -4px;
  inset-inline-end: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--bad, #c33a52);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: none;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--bg);
  pointer-events: none;
}

.tk-toast {
  position: absolute;
  left: 50%;
  bottom: 88px;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 650;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s var(--ease);
  z-index: 5;
  white-space: nowrap;
  max-width: calc(100% - 24px);
}
.tk-toast.show { opacity: 1; transform: translate(-50%, 0); }

.fab-badge.show { display: flex; }

/* نافذة الدردشة */
#chatPanel {
  position: fixed;
  inset-inline-end: 24px;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  z-index: 1060;
  width: min(380px, calc(100vw - 32px));
  height: min(600px, calc(100vh - 100px));
  display: none;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-card, 22px);
  box-shadow: var(--shadow-pop);
  overflow: hidden;
  font-family: 'Tajawal', 'Manrope', var(--font);
}

@supports (height: 100dvh) {
  #chatPanel { height: min(600px, calc(100vh - 100px)); height: min(600px, calc(100dvh - 100px)); }
}

#chatPanel.open {
  display: flex;
  animation: chatUp 0.35s var(--ease);
}

@keyframes chatUp {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* رأس الدردشة */
.tk-head {
  background: var(--surface);
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.tk-head > div:first-child { flex: 1; }
.tk-head b { font-size: 15px; font-weight: 700; }
.tk-head small { color: var(--muted); font-size: 12px; }

.tk-ib {
  border: 1px solid var(--line-2);
  background: transparent;
  color: var(--muted);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.tk-ib:hover { background: var(--well); color: var(--text); }
.tk-ib svg { width: 18px; height: 18px; }

/* التبويبات */
.tk-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 6px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
  flex: none;
}
.tk-tab {
  border: 0;
  background: none;
  border-radius: 8px;
  padding: 10px 8px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
  min-height: 40px;
}
.tk-tab.on {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.tk-tab[aria-disabled="true"] { opacity: 0.5; pointer-events: none; }

/* سجل الرسائل */
.tk-v { display: none; flex: 1; flex-direction: column; min-height: 0; }
.tk-v.on { display: flex; }

.tk-log {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--surface);
}

/* فقاعات الرسائل */
.bub {
  max-width: 85%;
  padding: 10px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.bub.bot {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-end-start-radius: 4px;
}
.bub.me {
  align-self: flex-end;
  background: var(--primary);
  color: var(--on-primary);
  border-end-end-radius: 4px;
}
.bub-time { display: block; font-size: 11px; opacity: 0.6; margin-top: 6px; text-align: end; }

/* نموذج الإرسال */
.tk-form {
  padding: 14px 20px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  flex: none;
}
.tk-row { display: flex; gap: 10px; align-items: flex-end; }
.tk-row textarea {
  flex: 1;
  min-width: 0;
  max-height: 120px;
  resize: none;
  padding: 12px 16px;
  border: 1px solid var(--line-2);
  border-radius: 20px;
  font: inherit;
  font-size: 16px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.tk-row textarea:focus { border-color: var(--primary); }
.tk-row textarea:focus-visible, .tk-in:focus-visible, #chatFab:focus-visible, .tk-ib:focus-visible, .tk-tab:focus-visible, .tk-send:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; }
.tk-in:focus-visible { outline-offset: 2px; }

.tk-send {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 0;
  background: var(--primary);
  color: var(--on-primary);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
[dir="rtl"] .tk-send svg { transform: scaleX(-1); }
.tk-send[disabled] { opacity: 0.5; cursor: not-allowed; }

/* =========================================
   MOBILE OVERRIDES
   ========================================= */

/* على الجوال: نخلي الشريط السفلي فوق زر الدردشة */
@media (max-width: 768px) {
  #chatFab {
    bottom: calc(78px + env(safe-area-inset-bottom, 0px));
  }
  #chatPanel {
    bottom: calc(78px + env(safe-area-inset-bottom, 0px));
    height: min(600px, calc(100dvh - 180px));
  }
}

/* على الشاشات الصغيرة جداً: الدردشة تأخذ الشاشة كاملة */
@media (max-width: 520px) {
  #chatPanel {
    inset: 0;
    width: 100%;
    height: 100dvh;
    border-radius: 0;
    border: 0;
    bottom: 0;
  }
  #chatFab {
    inset-inline-end: 16px;
    bottom: calc(78px + env(safe-area-inset-bottom, 0px));
  }
}

/* احترام تفضيل تقليل الحركة */
@media (prefers-reduced-motion: reduce) {
  #chatFab::before { animation: none; }
  #chatPanel.open { animation: none; }
  #chatFab { transition: none; }
}

/* عناصر واجهة التذكرة */
.tk-empty{display:flex;flex:1;flex-direction:column;align-items:center;justify-content:center;gap:14px;padding:24px;text-align:center;min-height:0}
.tk-empty p{color:var(--muted);font-size:14px;margin:0}
.tk-in{width:100%;max-width:280px;height:44px;padding:0 16px;border:1px solid var(--line-2);border-radius:14px;font:inherit;font-size:16px;background:var(--surface-2);color:var(--text);outline:none}
.tk-in:focus{border-color:var(--primary)}
.tk-info{display:flex;justify-content:space-between;align-items:center;gap:12px;padding:10px 20px;border-bottom:1px solid var(--line);font-size:13px;font-weight:700;flex:none}
.tk-link{border:0;background:none;color:var(--bad,#c33a52);font:inherit;font-size:13px;font-weight:650;cursor:pointer;padding:6px}
.tk-file{display:flex;gap:8px;align-items:center;padding:8px 20px;background:var(--surface-2);border-top:1px solid var(--line);font-size:12.5px;color:var(--muted)}
.tab-count{display:inline-grid;place-items:center;min-width:18px;height:18px;padding:0 5px;margin-inline-start:6px;border-radius:9px;background:var(--primary);color:var(--on-primary);font-size:10.5px;font-weight:800;transform:scale(0);transition:transform .25s var(--ease)}
.tab-count.show{transform:scale(1)}
.typing{align-self:flex-start;display:inline-flex;gap:4px;padding:12px 16px;background:var(--surface-2);border:1px solid var(--line);border-radius:16px;border-end-start-radius:4px}
.typing i{width:6px;height:6px;border-radius:50%;background:var(--muted);animation:typingBounce 1.2s infinite ease-in-out}
.typing i:nth-child(2){animation-delay:.15s}.typing i:nth-child(3){animation-delay:.3s}
@keyframes typingBounce{0%,60%,100%{transform:none;opacity:.5}30%{transform:translateY(-4px);opacity:1}}
.bub-img{display:block;max-width:100%;max-height:220px;border-radius:10px;margin-top:4px}
.bub-file{display:inline-block;margin-top:6px;padding:6px 10px;border-radius:8px;background:var(--well);color:var(--text);font-size:12.5px;font-weight:650;text-decoration:underline;text-underline-offset:2px;overflow-wrap:anywhere}
.ci{padding:12px 14px;border-radius:12px;background:var(--surface-2);border:1px solid var(--line);display:grid;gap:2px;font-size:13px;min-width:0}
.ci b{color:var(--text);font-weight:800}.ci small{color:var(--muted);font-size:11.5px}
.ci span{color:var(--muted);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.ci button{justify-self:start;margin-top:6px;padding:0;border:0;background:transparent;font:inherit;font-size:12px;font-weight:700;color:var(--primary);cursor:pointer}
