/* ==========================================================================
   Chat screen
   --------------------------------------------------------------------------
   IMPORTANT: these class names are contracts with send_function.html, which
   builds message markup in JS. Do not rename without updating that file:
     .chat  .left-msg  .right-msg  .chat-image  .chat-bubble
     .chat-info  .chat-info-name  .chat-info-time  .chat-text
     .main-chat  .chat-inputarea  .chat-input  #textInput  #message-form
   ========================================================================== */

body.chat-page {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.chatbot {
  display: flex;
  height: calc(100vh - var(--nav-h));
  height: calc(100dvh - var(--nav-h));
  max-width: 82rem;
  margin: 0 auto;
  padding: clamp(0rem, 1.5vw, 1.1rem) clamp(0rem, 2vw, 1.5rem) clamp(0rem, 1.5vw, 1.1rem);
  gap: 1.1rem;
}

/* ----- Sidebar ------------------------------------------------------------ */

.chatbot-profile {
  flex: 0 0 19rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  padding: 1.35rem 1.15rem;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.bot-card {
  text-align: center;
  padding-bottom: 0.35rem;
}

.bot-card__avatar {
  position: relative;
  display: inline-block;
  margin-bottom: 0.85rem;
}

.bot-card__avatar img {
  width: 6.5rem;
  height: 6.5rem;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--surface);
  box-shadow: 0 0 0 1px var(--border), var(--shadow-md);
  background: #000;
}

.bot-card__status {
  position: absolute;
  right: 0.3rem;
  bottom: 0.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 50%;
  background: var(--success);
  border: 2.5px solid var(--surface);
}

.bot-card__name {
  margin: 0 0 0.15rem;
  font-size: 1.02rem;
  font-weight: 650;
}

.bot-card__meta {
  margin: 0;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.sidebar-label {
  margin: 0.35rem 0 0.1rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.sidebar-actions {
  display: grid;
  gap: 0.5rem;
}

.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 550;
  font-family: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.sidebar-btn:hover {
  background: var(--brand-softer);
  border-color: var(--brand);
  color: var(--brand-deep);
}

html[data-bs-theme="dark"] .sidebar-btn:hover {
  color: var(--brand);
}

.sidebar-btn svg {
  flex: none;
  width: 1.05rem;
  height: 1.05rem;
  color: var(--text-muted);
}

.sidebar-btn:hover svg {
  color: var(--brand);
}

.sidebar-disclaimer {
  margin-top: auto;
  padding: 0.85rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  font-size: 0.78rem;
  line-height: 1.55;
  color: var(--text-muted);
}

.sidebar-disclaimer strong {
  color: var(--heading);
}

.sidebar-credit {
  margin: 0;
  font-size: 0.72rem;
  color: var(--text-faint);
  text-align: center;
}

/* ----- Chat column ------------------------------------------------------- */

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.9rem 1.15rem;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface-2) 60%, transparent);
}

.chat-header__avatar {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  background: #000;
}

.chat-header__title {
  margin: 0;
  font-size: 0.97rem;
  font-weight: 650;
  line-height: 1.2;
}

.chat-header__status {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.79rem;
  color: var(--text-muted);
}

.chat-header__dot {
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--success);
}

.chat-sidebar-toggle {
  display: none;
  margin-left: auto;
}

.main-chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1.4rem clamp(0.85rem, 2.5vw, 1.75rem);
  scroll-behavior: smooth;
  background-image: radial-gradient(
    circle at 1px 1px,
    color-mix(in srgb, var(--border) 70%, transparent) 1px,
    transparent 0
  );
  background-size: 22px 22px;
}

html[data-bs-theme="dark"] .main-chat {
  background-image: radial-gradient(
    circle at 1px 1px,
    color-mix(in srgb, var(--border) 55%, transparent) 1px,
    transparent 0
  );
}

/* Date / section divider */
.main-chat > p.text-muted {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  margin: 0.35rem 0 1.35rem;
  font-size: 0.76rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint) !important;
}

.main-chat > p.text-muted::before,
.main-chat > p.text-muted::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.main-chat > hr {
  margin: 1.5rem 0;
}

/* ----- Message rows ------------------------------------------------------ */

.chat {
  display: flex;
  align-items: flex-end;
  gap: 0.65rem;
  margin-bottom: 1.15rem;
  animation: msg-in 0.32s cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(0.5rem);
  }
}

.chat-image {
  flex: none;
  width: 2.3rem;
  height: 2.3rem;
  margin: 0;
  border-radius: 50%;
  object-fit: cover;
  background: #000;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
}

.chat-bubble {
  max-width: min(34rem, 76%);
  padding: 0.8rem 1rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  border-bottom-left-radius: var(--r-xs);
  background: var(--surface-2);
  box-shadow: var(--shadow-xs);
}

.chat-info {
  display: flex;
  align-items: baseline;
  justify-content: flex-start;
  gap: 0.6rem;
  margin-bottom: 0.3rem;
}

.chat-info-name {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--brand-deep);
  letter-spacing: -0.005em;
}

html[data-bs-theme="dark"] .chat-info-name {
  color: var(--brand);
}

.chat-info-time {
  margin-left: auto;
  font-size: 0.72rem;
  color: var(--text-faint);
  white-space: nowrap;
}

/* Preserve line breaks in multi-line bot replies (e.g. the crisis response,
   which lists helpline numbers one per line) without injecting any HTML. */
.chat-text {
  white-space: pre-line;
  font-size: 0.945rem;
  line-height: 1.62;
  color: var(--text);
  overflow-wrap: anywhere;
}

/* Bot messages */
.left-msg .chat-bubble {
  border-bottom-left-radius: var(--r-xs);
  border-bottom-right-radius: var(--r-md);
}

/* User messages */
.right-msg {
  flex-direction: row-reverse;
}

.right-msg .chat-bubble {
  border-bottom-left-radius: var(--r-md);
  border-bottom-right-radius: var(--r-xs);
  background: var(--brand-soft);
  border-color: color-mix(in srgb, var(--brand) 24%, var(--border));
}

.right-msg .chat-info-name {
  color: var(--brand-deep);
}

.right-msg .chat-image {
  margin: 0;
}

/* Crisis replies are long and carry helpline numbers — give them weight */
.left-msg .chat-text {
  max-width: 100%;
}

/* ----- Typing indicator -------------------------------------------------- */

.is-typing .chat-bubble {
  padding: 0.85rem 1rem;
}

.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 0.28rem;
}

.typing-dots span {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: var(--text-faint);
  animation: typing 1.3s ease-in-out infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.18s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.36s;
}

@keyframes typing {
  0%,
  60%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-0.2rem);
  }
}

/* ----- Embedded test forms (built by appendTest) ------------------------- */

.chat-bubble h5 {
  margin-bottom: 0.85rem;
  font-size: 1rem;
  font-weight: 650;
}

.test-questions {
  display: grid;
  gap: 0.35rem;
  font-size: 0.92rem;
}

.test-questions > div:not(.options) {
  font-weight: 600;
  color: var(--heading);
  margin-top: 0.35rem;
}

.test-questions .options {
  display: grid;
  gap: 0.3rem;
  margin: 0.45rem 0 0.25rem;
}

.test-questions .options > div {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.55rem;
  border-radius: var(--r-xs);
  transition: background-color 0.12s ease;
}

.test-questions .options > div:hover {
  background: color-mix(in srgb, var(--brand-soft) 60%, transparent);
}

.test-questions .form-check-input {
  margin: 0;
  flex: none;
}

.test-questions hr {
  margin: 0.6rem 0;
}

.chat-bubble audio {
  width: 100%;
  max-width: 20rem;
  margin-top: 0.5rem;
  border-radius: var(--r-pill);
}

/* ----- Tools row --------------------------------------------------------- */

.tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.7rem clamp(0.85rem, 2.5vw, 1.75rem);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.tools .btn {
  --bs-btn-padding-y: 0.42rem;
  --bs-btn-padding-x: 0.85rem;
  --bs-btn-font-size: 0.86rem;
  --bs-btn-border-radius: var(--r-pill);
}

.tools .dropdown-toggle::after {
  margin-left: 0.45rem;
  opacity: 0.6;
}

/* ----- Composer ---------------------------------------------------------- */

.chat-inputarea {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.85rem clamp(0.85rem, 2.5vw, 1.75rem) 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.chat-input {
  flex: 1;
  min-width: 0;
  padding: 0.78rem 1.15rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.97rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
}

.chat-input::placeholder {
  color: var(--text-faint);
  font-style: normal;
}

.chat-input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--brand);
  box-shadow: var(--ring);
}

.chat-inputarea .btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  --bs-btn-padding-y: 0.72rem;
  --bs-btn-padding-x: 1.3rem;
  --bs-btn-border-radius: var(--r-pill);
  --bs-btn-font-size: 0.94rem;
}

.chat-inputarea .btn svg {
  width: 1rem;
  height: 1rem;
}

/* ----- Responsive -------------------------------------------------------- */

@media (max-width: 991.98px) {
  .chatbot {
    padding: 0;
    gap: 0;
  }

  .chat-area {
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
  }

  /* Sidebar becomes an off-canvas drawer */
  .chatbot-profile {
    position: fixed;
    top: var(--nav-h);
    left: 0;
    bottom: 0;
    z-index: 1040;
    width: min(19rem, 84vw);
    border-radius: 0;
    border-left: 0;
    border-bottom: 0;
    box-shadow: var(--shadow-lg);
    transform: translateX(-102%);
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  }

  .chatbot-profile.is-open {
    transform: translateX(0);
  }

  .chat-sidebar-toggle {
    display: inline-flex;
  }

  .chat-bubble {
    max-width: 86%;
  }
}

@media (max-width: 575.98px) {
  .chat-bubble {
    max-width: 92%;
  }

  .chat-image {
    width: 1.9rem;
    height: 1.9rem;
  }

  .chat-inputarea .btn span {
    display: none;
  }

  .chat-inputarea .btn {
    --bs-btn-padding-x: 0.95rem;
  }
}

/* Scrim behind the mobile drawer */
.chat-scrim {
  position: fixed;
  inset: var(--nav-h) 0 0;
  z-index: 1035;
  background: rgba(10, 22, 26, 0.45);
  backdrop-filter: blur(2px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.chat-scrim.is-open {
  opacity: 1;
  visibility: visible;
}

@media (min-width: 992px) {
  .chat-scrim {
    display: none;
  }
}
