/* ==========================================================================
   لوحة الشات الجانبية — مثل المرجع
   ========================================================================== */

.chat-widget {
  position: fixed;
  top: 72px;
  right: 16px;
  left: auto;
  bottom: 16px;
  z-index: 1002;
  width: min(440px, 42vw);
  min-width: 320px;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  opacity: 0;
  visibility: hidden;
  transform: translateX(24px);
  transition:
    opacity 0.35s ease,
    transform 0.35s cubic-bezier(0.34, 1.2, 0.64, 1),
    visibility 0.35s;
  pointer-events: none;
}

.chat-widget:not(.hidden) {
  display: flex;
}

.chat-widget.open {
  opacity: 1;
  visibility: visible;
  transform: translateX(0);
  pointer-events: auto;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #fff;
  border-radius: 16px;
  box-shadow:
    0 8px 40px rgba(0, 0, 0, 0.18),
    0 0 0 1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
}

/* ─── Header تيل ────────────────────────────────────────────────────── */

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--school-primary), var(--school-accent));
  flex-shrink: 0;
}

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

.panel-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, 0.9);
  background: #fff;
  padding: 4px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.panel-avatar img,
.panel-avatar .bot-icon-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.panel-header-text {
  min-width: 0;
}

.panel-header-text h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.9);
  margin-top: 2px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #4ade80;
  box-shadow: 0 0 6px #4ade80;
  animation: statusPulse 2s ease infinite;
}

@keyframes statusPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.panel-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}

.panel-btn:hover {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(1.05);
}

/* ─── Body ──────────────────────────────────────────────────────────── */

.panel-body {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
  background: #f8fafb;
}

.panel-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.panel-sidebar {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: #fff;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  border-right: 1px solid #e2e8f0;
}

html[dir="rtl"] .panel-sidebar {
  transform: translateX(-100%);
  border-right: none;
  border-left: 1px solid #e2e8f0;
}

.panel-sidebar.open {
  transform: translateX(0);
}

.panel-sidebar-header {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid #e2e8f0;
}

.panel-sidebar .btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #fff;
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--school-primary);
  cursor: pointer;
}

.panel-sidebar .chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.panel-sidebar .history-item {
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.panel-sidebar .history-item:hover {
  background: #f1f5f9;
}

.panel-sidebar .history-btn {
  flex: 1;
  text-align: right;
  padding: 10px 12px;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.8125rem;
  color: #475569;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-sidebar .history-item.active .history-btn {
  color: var(--school-primary);
  font-weight: 600;
}

.panel-sidebar .history-delete {
  padding: 8px;
  border: none;
  background: none;
  color: #94a3b8;
  cursor: pointer;
  opacity: 0;
}

.panel-sidebar .history-item:hover .history-delete {
  opacity: 1;
}

.panel-sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid #e2e8f0;
}

.panel-sidebar-footer .btn-clear-all {
  width: 100%;
  padding: 10px;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.75rem;
  color: #ef4444;
  cursor: pointer;
  border-radius: 8px;
}

.panel-sidebar-footer .btn-clear-all:hover {
  background: #fef2f2;
}

/* ─── Messages area ───────────────────────────────────────────────────── */

.chat-panel .chat-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 14px;
  background: #fff;
  scroll-behavior: smooth;
}

.chat-panel .welcome-screen {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-panel .welcome-screen.hidden {
  display: none !important;
}

/* صف الرسالة */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 14px;
  animation: msgIn 0.3s ease;
}

.msg-row.user {
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #fff;
  padding: 2px;
  border: 2px solid #fee2e2;
  box-shadow: 0 2px 6px rgba(185, 28, 28, 0.12);
  overflow: hidden;
}

.msg-avatar img,
.msg-avatar .bot-icon-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.msg-bubble {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 16px;
  border-bottom-right-radius: 4px;
  background: #f1f5f9;
  color: #1e293b;
  font-size: 0.9rem;
  line-height: 1.65;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

html[dir="rtl"] .msg-row.bot .msg-bubble {
  border-bottom-right-radius: 16px;
  border-bottom-left-radius: 4px;
}

.msg-row.user .msg-bubble {
  background: linear-gradient(135deg, var(--school-primary), var(--school-accent));
  color: #fff;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 16px;
  box-shadow: 0 2px 8px rgba(185, 28, 28, 0.25);
}

.msg-bubble p {
  margin: 0 0 0.5em;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble pre {
  background: rgba(0, 0, 0, 0.06);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 0.8rem;
  margin: 8px 0;
}

.msg-row.user .msg-bubble pre {
  background: rgba(0, 0, 0, 0.15);
}

@keyframes msgIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* typing */
.chat-panel .typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 0 12px;
  max-width: none;
  margin: 0;
}

.chat-panel .typing-indicator.hidden {
  display: none;
}

.chat-panel .typing-dots {
  padding: 10px 14px;
  background: #f1f5f9;
  border-radius: 16px;
}

.chat-panel .loading-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: #e2e8f0;
  z-index: 5;
}

.chat-panel .loading-bar-inner {
  background: linear-gradient(90deg, var(--school-accent), var(--school-primary));
}

/* ─── Footer + input + chips ────────────────────────────────────────── */

.panel-footer {
  flex-shrink: 0;
  padding: 12px 14px 14px;
  background: #fff;
  border-top: 1px solid #e8eef2;
}

.panel-footer .input-form {
  margin: 0;
}

.panel-footer .input-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1.5px solid #d1dbe5;
  border-radius: 999px;
  padding: 6px 6px 6px 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.panel-footer .input-wrapper:focus-within {
  border-color: var(--school-accent);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15);
}

.panel-footer .message-input {
  flex: 1;
  min-width: 0;
  max-height: 100px;
  font-size: 0.9rem;
  color: #1e293b;
  padding: 8px 0;
}

.panel-footer .message-input::placeholder {
  color: #94a3b8;
}

.panel-footer .btn-send {
  width: 42px;
  height: 42px;
  min-width: 42px;
  border-radius: 50%;
  background: var(--school-accent);
  flex-shrink: 0;
}

.panel-footer .btn-send:hover:not(:disabled) {
  background: var(--school-primary);
}

.panel-footer .input-hint {
  display: none;
}

.quick-chips {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  margin-top: 12px;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 4px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.quick-chips::-webkit-scrollbar {
  height: 4px;
}

.quick-chips::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 2px;
}

.chip-btn {
  flex-shrink: 0;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1.5px solid var(--school-accent);
  background: #fff;
  color: var(--school-primary);
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s, color 0.2s, transform 0.15s;
}

.chip-btn:hover {
  background: #fef2f2;
  transform: translateY(-1px);
}

/* ─── زر جانبي — صغير وخفيف ─────────────────────────────────────────── */

.chat-launcher {
  position: fixed;
  right: 0;
  top: 50%;
  z-index: 1003;
  display: flex;
  align-items: center;
  flex-direction: row;
  gap: 0;
  padding: 3px 4px 3px 7px;
  border: none;
  background: rgba(20, 30, 36, 0.55);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-radius: 12px 0 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-right: none;
  box-shadow: -2px 2px 14px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(-50%) translateX(100%);
  transition:
    transform 0.35s cubic-bezier(0.34, 1.1, 0.64, 1),
    opacity 0.35s ease,
    visibility 0.35s,
    background 0.25s ease,
    box-shadow 0.25s ease,
    padding 0.3s ease;
}

.chat-launcher.visible {
  opacity: 0.92;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(-50%) translateX(0);
}

.chat-launcher:hover,
.chat-launcher:focus-visible {
  opacity: 1;
  background: rgba(24, 38, 44, 0.82);
  box-shadow: -4px 4px 20px rgba(0, 0, 0, 0.18);
  padding-left: 10px;
}

.chat-launcher.hidden:not(.visible) {
  display: none;
}

.launcher-text {
  color: rgba(255, 255, 255, 0.92);
  font-family: var(--font-ar, "Cairo", sans-serif);
  font-weight: 600;
  font-size: 0.7rem;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  white-space: nowrap;
  padding: 0;
  transition: max-width 0.35s ease, opacity 0.3s ease, padding 0.3s ease;
}

.chat-launcher:hover .launcher-text,
.chat-launcher:focus-visible .launcher-text {
  max-width: 160px;
  opacity: 1;
  padding: 0 6px 0 8px;
}

.launcher-icon {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 9px;
  overflow: hidden;
  padding: 0;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(220, 38, 38, 0.2);
  transition: transform 0.25s ease, border-color 0.25s ease;
}

.chat-launcher:hover .launcher-icon,
.chat-launcher:focus-visible .launcher-icon {
  transform: scale(1.04);
  border-color: rgba(220, 38, 38, 0.45);
}

.launcher-icon .bot-icon-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

body.chat-minimized .site-page {
  padding-inline-start: 0;
}

/* ─── Landing adjustment when chat open ───────────────────────────────── */

body.chat-open .site-page {
  padding-inline-start: 0;
}

.chat-widget-backdrop {
  display: none !important;
}

/* ─── Mobile ──────────────────────────────────────────────────────────── */

@media (max-width: 900px) {
  body.chat-open .site-page {
    padding-inline-start: 0;
  }

  .chat-widget {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    transform: translateY(100%);
  }

  .chat-widget.open {
    transform: translateY(0);
  }

  .chat-panel {
    border-radius: 0;
  }

  .panel-header {
    padding: 12px;
  }

  .panel-avatar {
    width: 42px;
    height: 42px;
  }

  .panel-header-text h2 {
    font-size: 0.98rem;
  }

  .panel-footer {
    padding: 10px 10px 12px;
  }

  .panel-footer .input-wrapper {
    padding: 6px 6px 6px 12px;
  }

  .chat-launcher {
    top: auto;
    bottom: 18px;
    right: 12px;
    transform: translateY(0) translateX(100%);
    border-radius: 999px;
    padding: 4px;
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  .chat-launcher.visible {
    transform: translateY(0) translateX(0);
  }

  .chat-launcher .launcher-text {
    display: none;
  }

  .launcher-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
  }

  .launcher-icon .bot-icon-img {
    border-radius: 50%;
  }
}

@media (max-width: 600px) {
  .chat-panel .chat-container {
    padding: 12px 10px;
  }

  .msg-row {
    gap: 6px;
    margin-bottom: 10px;
  }

  .msg-avatar {
    width: 32px;
    height: 32px;
  }

  .msg-bubble {
    max-width: 92%;
    padding: 10px 12px;
    font-size: 0.85rem;
    line-height: 1.55;
  }

  .quick-chips {
    margin-top: 10px;
    gap: 6px;
  }

  .chip-btn {
    padding: 7px 12px;
    font-size: 0.72rem;
  }
}

@media (max-width: 400px) {
  .chat-widget {
    right: 0;
    left: 0;
  }

  .panel-header {
    padding: 10px;
    gap: 8px;
  }

  .panel-header-info {
    gap: 8px;
  }

  .panel-avatar {
    width: 36px;
    height: 36px;
    border-width: 2px;
  }

  .panel-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
  }

  .panel-footer .btn-send {
    width: 38px;
    height: 38px;
    min-width: 38px;
  }
}

@media (max-height: 640px) {
  .chat-widget {
    top: 56px;
  }
}
