/* Alrehab AI assistant — brand orange, Tajawal, RTL/LTR aware. */

.rehab-chat {
  --rc-primary: var(--primary-color, #f7821f);
  --rc-primary-rgb: var(--primary-color-rgb, 247, 130, 31);
  --rc-primary-dark: #d9660a;
  --rc-ink: #1c1c1c;
  --rc-muted: #6b6b6b;
  --rc-border: #ececec;
  --rc-surface: #ffffff;
  --rc-soft: #faf7f4;
  position: fixed;
  inset-inline-end: 24px;
  bottom: 24px;
  z-index: 9999;
  font-family: "Tajawal", sans-serif;
  line-height: 1.6;
}

.rehab-chat *,
.rehab-chat *::before,
.rehab-chat *::after {
  box-sizing: border-box;
}

/* Launcher */
.rehab-chat__launcher {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  height: 48px;
  padding: 0 1.1rem 0 0.9rem;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--rc-primary) 0%, var(--rc-primary-dark) 100%);
  color: #fff;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  box-shadow: 0 10px 28px rgba(var(--rc-primary-rgb), 0.45);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease;
  animation: rehab-chat-pulse 2.8s ease-in-out 1.5s 3;
}

[dir="rtl"] .rehab-chat__launcher {
  padding: 0 0.9rem 0 1.1rem;
}

.rehab-chat__launcher:hover {
  transform: translateY(-2px);
  box-shadow: 0 14px 34px rgba(var(--rc-primary-rgb), 0.55);
}

.rehab-chat__launcher:focus-visible,
.rehab-chat__close:focus-visible,
.rehab-chat__send:focus-visible,
.rehab-chat__chip:focus-visible {
  outline: 3px solid rgba(var(--rc-primary-rgb), 0.4);
  outline-offset: 2px;
}

.rehab-chat__launcher[aria-expanded="true"] {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.9);
}

.rehab-chat__launcher-icon {
  display: inline-flex;
}

@keyframes rehab-chat-pulse {
  0%, 100% { box-shadow: 0 10px 28px rgba(var(--rc-primary-rgb), 0.45), 0 0 0 0 rgba(var(--rc-primary-rgb), 0.45); }
  50% { box-shadow: 0 10px 28px rgba(var(--rc-primary-rgb), 0.45), 0 0 0 12px rgba(var(--rc-primary-rgb), 0); }
}

/* Panel */
.rehab-chat__panel {
  position: fixed;
  inset-inline-end: 24px;
  bottom: 24px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  width: min(390px, calc(100vw - 2rem));
  height: min(600px, calc(100dvh - 2rem));
  border: 1px solid var(--rc-border);
  border-radius: 20px;
  background: var(--rc-surface);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  animation: rehab-chat-in 0.22s ease;
}

.rehab-chat__panel[hidden] {
  display: none !important;
}

@keyframes rehab-chat-in {
  from { opacity: 0; transform: translateY(12px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.rehab-chat__header {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.9rem 1rem;
  background: linear-gradient(135deg, var(--rc-primary) 0%, var(--rc-primary-dark) 100%);
  color: #fff;
}

.rehab-chat__avatar {
  display: inline-flex;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.rehab-chat__avatar img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.rehab-chat__header-text {
  flex: 1;
  min-width: 0;
}

.rehab-chat__title {
  display: block;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
}

.rehab-chat__subtitle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  opacity: 0.92;
}

.rehab-chat__status {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #3ddc84;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.6);
}

.rehab-chat__close {
  display: inline-flex;
  width: 34px;
  height: 34px;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  cursor: pointer;
  transition: background 0.15s ease;
}

.rehab-chat__close:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Messages */
.rehab-chat__messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 1rem;
  overflow-y: auto;
  background: var(--rc-soft);
  scroll-behavior: smooth;
}

.rehab-chat__bubble {
  max-width: 88%;
  padding: 0.65rem 0.9rem;
  border-radius: 16px;
  font-size: 0.92rem;
  word-break: break-word;
  white-space: normal;
}

.rehab-chat__bubble--assistant {
  align-self: flex-start;
  background: var(--rc-surface);
  border: 1px solid var(--rc-border);
  color: var(--rc-ink);
  border-start-start-radius: 4px;
}

.rehab-chat__bubble--user {
  align-self: flex-end;
  background: var(--rc-primary);
  color: #fff;
  border-start-end-radius: 4px;
}

.rehab-chat__bubble--error {
  align-self: stretch;
  max-width: 100%;
  background: #fff4f4;
  border-color: #f3c2c2;
  color: #a12626;
}

.rehab-chat__bubble a {
  color: var(--rc-primary-dark);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}

.rehab-chat__bubble ul {
  margin: 0.35rem 0 0;
  padding-inline-start: 1.1rem;
}

.rehab-chat__bubble li + li {
  margin-top: 0.2rem;
}

.rehab-chat__bubble p {
  margin: 0;
}

.rehab-chat__bubble p + p,
.rehab-chat__bubble p + ul,
.rehab-chat__bubble ul + p {
  margin-top: 0.4rem;
}

.rehab-chat__typing {
  align-self: flex-start;
  display: inline-flex;
  gap: 0.3rem;
  padding: 0.8rem 0.95rem;
  border: 1px solid var(--rc-border);
  border-radius: 16px;
  background: var(--rc-surface);
}

.rehab-chat__typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--rc-primary);
  animation: rehab-chat-dot 1.2s infinite ease-in-out;
}

.rehab-chat__typing span:nth-child(2) { animation-delay: 0.15s; }
.rehab-chat__typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes rehab-chat-dot {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* Suggestions */
.rehab-chat__suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.65rem 1rem 0.2rem;
  background: var(--rc-soft);
}

.rehab-chat__suggestions[hidden] {
  display: none;
}

.rehab-chat__chip {
  padding: 0.3rem 0.8rem;
  border: 1px solid rgba(var(--rc-primary-rgb), 0.45);
  border-radius: 999px;
  background: var(--rc-surface);
  color: var(--rc-primary-dark);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.rehab-chat__chip:hover {
  background: var(--rc-primary);
  color: #fff;
}

/* Composer */
.rehab-chat__form {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem 0.35rem;
  border-top: 1px solid var(--rc-border);
  background: var(--rc-surface);
}

.rehab-chat__input {
  flex: 1;
  min-height: 42px;
  max-height: 112px;
  padding: 0.55rem 0.85rem;
  border: 1px solid var(--rc-border);
  border-radius: 14px;
  background: var(--rc-soft);
  color: var(--rc-ink);
  font: inherit;
  font-size: 0.92rem;
  resize: none;
}

.rehab-chat__input:focus {
  outline: none;
  border-color: var(--rc-primary);
  box-shadow: 0 0 0 3px rgba(var(--rc-primary-rgb), 0.15);
  background: #fff;
}

.rehab-chat__send {
  display: inline-flex;
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 14px;
  background: var(--rc-primary);
  color: #fff;
  cursor: pointer;
  transition: background 0.15s ease;
}

[dir="rtl"] .rehab-chat__send svg {
  transform: scaleX(-1);
}

.rehab-chat__send:hover {
  background: var(--rc-primary-dark);
}

.rehab-chat__send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.rehab-chat__disclaimer {
  margin: 0;
  padding: 0.2rem 1rem 0.6rem;
  background: var(--rc-surface);
  color: var(--rc-muted);
  font-size: 0.7rem;
  text-align: center;
}

@media (max-width: 575.98px) {
  .rehab-chat {
    inset-inline-end: 25px;
    bottom: 24px;
  }

  .rehab-chat__launcher {
    width: 52px;
    height: 52px;
    padding: 0 !important;
    justify-content: center;
  }

  .rehab-chat__launcher-label {
    display: none;
  }

  .rehab-chat__panel {
    inset: auto 0 0 0;
    width: 100%;
    height: min(85dvh, 640px);
    border-radius: 20px 20px 0 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rehab-chat__launcher,
  .rehab-chat__panel,
  .rehab-chat__typing span {
    animation: none;
  }
}
