/* Shared chat window layout and states, styled by the selected skin. */
/* ── Overlay (extends .modal-veil) ─────────────────────────────────── */
.chat-veil {
  z-index: 200;
}
/* ── Chat container (extends .modal) ───────────────────────────────── */
.chat-modal {
  max-width: 440px;
  height: var(--chat-height, min(640px, 82svh));
  padding: 0;
  text-align: left;
  display: flex; flex-direction: column;
  overflow: visible;
}
/* Clip inner content but allow close button to escape */
.chat-modal > .chat-header,
.chat-modal > .chat-messages,
.chat-modal > .chat-typing,
.chat-modal > .chat-input-area {
  position: relative; z-index: 1;
}
/* Close button must sit above all modal content */
.chat-modal > .modal-close {
  position: absolute; z-index: 10;
}

/* ── Chat header ───────────────────────────────────────────────────── */
.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(var(--color-border-rgb), 0.12);
  flex-shrink: 0;
}
.chat-avatar {
  width: 88px; height: 88px;
  border-radius: var(--radius-cat-avatar);
  object-fit: cover;
  border: 1px solid rgba(var(--color-border-rgb), 0.3);
  transition: box-shadow .4s ease, border-color .4s ease;
}
.chat-avatar.avatar-pulse {
  border-color: rgba(var(--color-border-rgb), 0.9);
  box-shadow: 0 0 18px 6px rgba(var(--color-glow-rgb), 0.5), 0 0 40px 10px rgba(var(--color-glow-rgb), 0.25);
  animation: chatAvatarGlow .6s ease-out;
}
@keyframes chatAvatarGlow {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.06); }
  100% { transform: scale(1); }
}
.chat-header-info {
  display: flex; flex-direction: column; gap: 2px;
}
.chat-header-name {
  font-family: var(--font-ui);
  font-size: 14px; font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--color-text);
}
.chat-header-status {
  font-family: var(--font-ui);
  font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--color-text-caption);
}
.chat-header-status::before {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: var(--radius-round);
  background: var(--color-online);
  margin-right: 5px;
  vertical-align: middle;
  animation: chatStatusPulse 2s ease-in-out infinite;
}
@keyframes chatStatusPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ── Messages area ─────────────────────────────────────────────────── */
.chat-messages {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: 20px 16px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
/* Push bubbles to the bottom without breaking overflow scroll */
.chat-messages::before {
  content: '';
  flex: 1 1 auto;
}
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(var(--color-accent-rgb), 0.2); border-radius: var(--radius-focus);
}

/* ── Bubbles ───────────────────────────────────────────────────────── */
.chat-bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--radius-bubble);
  font-family: var(--font-display);
  font-size: 17px; line-height: 1.5;
  overflow-wrap: anywhere;
  min-width: 0;
  flex-shrink: 0;
  animation: chatBubbleIn .5s cubic-bezier(0.16, 1, 0.3, 1) both;
}
@keyframes chatBubbleIn {
  from { opacity: 0; transform: translateY(24px) scale(0.92); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.chat-bubble--bot {
  align-self: flex-start;
  background: var(--surface-bubble-bot);
  border: 1px solid rgba(var(--color-border-rgb), 0.15);
  color: var(--color-text);
  border-bottom-left-radius: var(--radius-small);
  line-height: var(--chat-bot-leading, 1.5);
}
.chat-bubble--user {
  align-self: flex-end;
  background: var(--surface-bubble-user);
  border: 1px solid rgba(var(--color-border-rgb), 0.3);
  color: var(--color-text);
  border-bottom-right-radius: var(--radius-small);
}

/* ── Typing indicator ──────────────────────────────────────────────── */
.chat-typing {
  display: none;
  align-self: flex-start;
  align-items: center; gap: 4px;
  padding: 12px 16px;
  background: var(--surface-bubble-bot);
  border: 1px solid rgba(var(--color-border-rgb), 0.15);
  border-radius: var(--radius-bubble); border-bottom-left-radius: var(--radius-small);
}
.chat-typing.is-visible { display: flex; }
.chat-typing-dot {
  width: 7px; height: 7px;
  border-radius: var(--radius-round);
  background: rgba(var(--color-accent-rgb), 0.6);
  animation: chatTypingBounce 1.4s ease-in-out infinite;
}
.chat-typing-dot:nth-child(2) { animation-delay: .2s; }
.chat-typing-dot:nth-child(3) { animation-delay: .4s; }
@keyframes chatTypingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ── Input area ────────────────────────────────────────────────────── */
.chat-input-area {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid rgba(var(--color-border-rgb), 0.12);
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: var(--radius-chat-input);
  border: 1px solid rgba(var(--color-border-rgb), 0.25);
  background: var(--surface-chat-input);
  color: var(--color-text);
  font-family: var(--font-display);
  font-size: 17px; /* ≥16px prevents iOS auto-zoom on focus */
  outline: none;
  transition: border-color .25s ease;
}
.chat-input::placeholder {
  color: var(--color-text-placeholder);
}
.chat-input:focus {
  border-color: var(--color-accent);
}
.chat-send {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-round);
  background: var(--color-accent);
  color: var(--color-on-accent);
  flex-shrink: 0;
  border: none;
  cursor: pointer;
  transition: background .25s ease, transform .15s ease;
}
.chat-send:hover { background: var(--color-accent-hover); }
.chat-send:active { transform: scale(0.92); }
.chat-send:disabled { opacity: 0.35; pointer-events: none; }
.chat-send svg { width: 16px; height: 16px; }

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .chat-veil {
    padding: 0;
    overflow: hidden;
    align-items: stretch;
  }
  .chat-modal {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: none;
    overflow: hidden;
    border-radius: 0;
  }
  /* Force pseudo-element border to not overflow on mobile */
  .chat-modal::before,
  .chat-modal > .glow-bloom { display: none; }
  /* Close button: fixed to top-right of screen */
  .chat-veil .modal-close {
    position: fixed;
    top: max(12px, env(safe-area-inset-top, 0px));
    right: max(12px, env(safe-area-inset-right, 0px));
    z-index: 210;
    width: 36px; height: 36px;
  }
  .chat-avatar {
    width: 80px; height: 80px;
    border-radius: var(--radius-cat-avatar);
  }
  .chat-header {
    padding-top: max(16px, env(safe-area-inset-top, 0px));
    padding-right: 52px; /* room for close btn */
  }
  .chat-input-area {
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  }
}

/* ── Mobile keyboard isolation ─────────────────────────────────────── */
/* Touch viewport sizing is owned by overlays.js. */
body.chat-mobile-open {
  overflow: hidden !important;
  height: 100dvh;
  overscroll-behavior: none;
}
body.chat-mobile-open .viewport,
body.chat-mobile-open .ambient,
body.chat-mobile-open .route-nav,
body.chat-mobile-open .chrome-top,
body.chat-mobile-open .chrome-bottom,
body.chat-mobile-open main {
  display: none !important;
}
body.chat-mobile-open .chat-veil {
  position: fixed;
  inset: 0;
  height: 100dvh;
}
body.chat-mobile-open .chat-modal {
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
/* Header and input must not shrink — messages area absorbs keyboard resize */
body.chat-mobile-open .chat-header {
  flex-shrink: 0;
}
body.chat-mobile-open .chat-input-area {
  flex-shrink: 0;
}

/* Safe formatted replies share styling; plain responses never opt into markup. */
.chat-bubble .chat-para {
  margin: 0;
}
.chat-bubble .chat-para + .chat-para {
  margin-top: 0.65em;
}
.chat-bubble--bot strong {
  color: rgba(var(--color-accent-rgb), 0.95);
  font-weight: 600;
}
.chat-bubble--bot em {
  font-style: var(--font-style-display);
  opacity: 0.85;
}
.chat-bubble--bot a {
  color: rgba(var(--color-accent-rgb), 0.95);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}
.chat-bubble--bot a:hover {
  color: var(--color-highlight);
}
