/* ================= ROOT & GLOBALS ================= */
:root {
  /* expose iOS safe area to JS (used in fitStage) */
  --sa-top: env(safe-area-inset-top);
  --sa-right: env(safe-area-inset-right);
  --sa-bottom: env(safe-area-inset-bottom);
  --sa-left: env(safe-area-inset-left);
}

body.capsule-overlay-active {
  overflow: hidden;
}

/* ================= CHAT LAYOUT & CONTAINERS ================= */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--primary-bg);
  overflow: hidden;
  position: relative;
}

.chat-messages {
  position: relative;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  touch-action: pan-y;
  padding: 20px 12px 140px 12px; /* Extra bottom padding to prevent overlap with prompt area */
  display: flex;
  flex-direction: column-reverse;
  gap: 12px;
  transition: background-color 0.15s ease, filter 0.15s ease;
}

.chat-messages.loading {
  filter: blur(20px);
  pointer-events: none;
}

#chatTopSentinel {
  height: 1px;
}

/* Drag & Drop State */
.chat-messages.drag-over {
  background: radial-gradient(circle at center, rgba(255, 255, 255, 0.05) 0%, transparent 70%);
}

.chat-messages.drag-over::before {
  content: "Drop module here to add to conversation";
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 16px 32px;
  background: var(--secondary-bg);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 400;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  z-index: 1200;
  pointer-events: none;
}


/* ================= LOADERS, OVERLAYS & SENTINELS ================= */

/* Project Loading Overlay */
.project-loading {
  position: fixed;
  top: 50px;
  left: 0;
  right: 0;
  bottom: 0;
  width: auto;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  color: #fff;
  z-index: 100;
  background: #000;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.8s ease, visibility 0s linear 0.8s;
}

.project-loading::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle at 20% 30%, rgba(40, 40, 40, 0.8), transparent 50%), radial-gradient(circle at 80% 70%, rgba(0, 60, 120, 0.5), transparent 60%), radial-gradient(circle at 50% 50%, rgba(10, 10, 15, 0.9), rgba(0, 0, 0, 0.9));
  filter: blur(50px);
  animation: plasmaGlow 4s ease-in-out infinite;
}

    .project-loading.active {
      opacity: 1;
      visibility: visible;
      pointer-events: none; /* Allow clicks to pass through loading overlay */
      transition: opacity 0.8s ease;
    }

.project-loading .spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #fff;
  border-top-color: transparent;
  border-radius: 50%;
  animation: chat-spin-rotate 1s linear infinite;
  margin-bottom: 10px;
}

.project-loading .project-name-pill {
  margin-top: 4px;
  padding: 4px 12px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  color: #fff;
  font-family: 'Roboto', sans-serif;
  font-size: 12px;
  text-align: center;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

/* Top Loader (Infinite Scroll) */
.chat-top-loader {
  position: sticky;
  top: 0;
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(
    to bottom, 
    rgba(10,10,10,0.95), 
    rgba(10,10,10,0.8),
    rgba(10,10,10,0)
  );
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 5;
  transition: all 0.3s ease;
}

/* Extend gradient upward to fully cover top area */
.chat-top-loader::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  /* Extend 80px above, plus safe-area if present */
  top: calc(-80px - var(--sa-top, 0px));
  height: calc(80px + var(--sa-top, 0px));
  background: linear-gradient(
    to bottom,
    rgba(10,10,10,0.95),
    rgba(10,10,10,0.8),
    rgba(10,10,10,0)
  );
  pointer-events: none;
}

.chat-top-loader.hidden {
  opacity: 0;
  transform: translateY(-20px);
  pointer-events: none;
}

.chat-top-loader .spinner {
  width: 28px;
  height: 28px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: rgba(74, 158, 255, 0.8);
  border-radius: 50%;
  animation: modernSpin 1.2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}

@keyframes modernSpin {
  to {
    transform: rotate(360deg);
  }
}

/* Image Viewer ("Capsule") Overlay */
#capsuleImageOverlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  background: rgba(10, 10, 12, 0.86);
  backdrop-filter: blur(6px) saturate(110%);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease;
  z-index: 9999;
}

#capsuleImageOverlay.active {
  opacity: 1;
  pointer-events: auto;
}

#capsuleOverlayMedia {
  position: relative;
  width: min(92vw, 1200px); /* fallback initial size before fitStage() */
  height: min(70dvh, 92vw); /* fallback initial size before fitStage() */
  border-radius: 0px;
  border: none;
  background: transparent;
  overflow: hidden;
  touch-action: none;
}

#capsuleOverlayMedia img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

#capsuleOverlaySlider {
  position: absolute;
  top: 0; bottom: 0;
  left: 50%;
  width: 2px;
  background: rgba(255,255,255,.9);
  box-shadow: 0 0 0 1px rgba(0,0,0,.15), 2px 0 8px rgba(0,0,0,.2);
  display: none;
}

#capsuleOverlaySlider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 19px; height: 19px;
  transform: translate(-50%, -50%);
  background: url('/static/formas/icons/gpt/ab.png') no-repeat center center;
  background-size: 70%;
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0,0,0,.3), 0 6px 16px rgba(0,0,0,.15);
}

#capsuleImageOverlay .overlay-actions {
  position: absolute;
  top: calc(16px + var(--sa-top, 0px));
  right: calc(16px + var(--sa-right, 0px));
  display: flex;
  gap: 8px;
}

#capsuleOverlayClose, #capsuleOverlayDownload {
  appearance: none;
  border: 0;
  background: rgba(255,255,255,.08);
  color: #fff;
  border-radius: 10px;
  padding: 8px 12px;
  font: 500 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}

.viewer-error-bubble {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255,77,77,.92);
  color: #fff;
  padding: 10px 14px;
  border-radius: 10px;
  font: 600 13px/1 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}


/* ================= BASE MESSAGE STYLES ================= */

.message img, .message video {
  max-width: 100%;
  margin-top: 4px;
  border-radius: 6px;
  display: block;
}

.message img {
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.message img:hover {
  opacity: 0.9;
}

.dropped-prompt {
  font-style: italic;
  font-size: 0.85em;
}

/* Desktop screen adjustments for all messages */
@media (min-width: 1024px) {
  .message {
    margin-left: 0;
    margin-right: 0;
    max-width: 900px;
  }
}


/* ================= ASSISTANT MESSAGE STYLES ================= */

.message.assistant {
  display: flex;
  justify-content: flex-start;
  align-items: flex-end;
  gap: 8px;
}

.message.assistant .message-content {
  background: #373737;
  color: var(--text-primary);
  padding: 12px 16px;
  border-radius: 16px 16px 16px 4px;
  /* max-width: 70%; REMOVED to allow 3D models to display at full size */
  border: 1px solid #3c3c3c;
  font-weight: 400;
  font-size: 14px;
  line-height: 1.5;
  position: relative;
  --message-bg: #373737;
  overflow: hidden;
}

/* Assistant media wrappers (large, responsive width) */
.message.assistant .chat-img-wrapper,
.message.assistant .chat-model-wrapper {
  position: relative;
  display: block;
  max-width: 600px; /* MAXIMUM WIDTH - same as images */
  width: auto; /* Allow natural width up to max-width */
  height: auto;
  margin-bottom: 0;
  border-radius: 8px;
  overflow: hidden;
  transition: width 0.3s ease, height 0.3s ease;
}

.message.assistant .chat-model-wrapper {
  min-height: 307px;
  background: var(--secondary-bg);
  aspect-ratio: 1 / 1;
}

/* Assistant media content preserves aspect ratio */
.message.assistant .chat-img-wrapper img,
.message.assistant .chat-img-wrapper video {
  width: 100%;
  height: auto;
  object-fit: contain;
  position: relative;
  z-index: 1;
}

/* Desktop screen adjustments for assistant messages */
@media (min-width: 1024px) {
  .message.assistant {
    margin-right: auto;
    margin-left: 20px;
  }
  .message.assistant .chat-img-wrapper,
  .message.assistant .chat-model-wrapper {
    max-width: 800px; /* LARGER MAXIMUM WIDTH on desktop */
  }
  .message.assistant .chat-model-wrapper {
    min-height: 400px;
  }
}

@media (min-width: 1600px) {
  .message.assistant {
    margin-left: calc((100vw - 1600px) / 2 + 20px);
  }
}

/* Mobile screen adjustments for assistant messages */
@media (max-width: 768px) {
  .message.assistant .chat-img-wrapper,
  .message.assistant .chat-model-wrapper {
    max-width: 400px; /* SMALLER MAXIMUM WIDTH on mobile */
  }
  .message.assistant .chat-model-wrapper {
    min-height: 200px;
  }
}


/* ================= USER MESSAGE STYLES ================= */

.message.user {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  width: 100%;
  gap: 8px;
}

.message.user .message-content {
  background: #2a2a2a;
  color: var(--button-secondary-text);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
  max-width: 70%;
  font-weight: 400;
  font-size: 14px;
  line-height: 1.4;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  position: relative;
  margin-left: auto;
  order: 1;
}

.message.user .message-content span:not(.chat-img-wrapper) {
  align-self: flex-end;
  text-align: right;
}

.message.user video {
  width: 100%;
  height: auto;
  border-radius: 20px;
  object-fit: cover;
}

/* ================= WHATSAPP-STYLE AVATARS ================= */

/* Base avatar styles */
.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  transition: opacity 0.2s ease, visibility 0s linear 0.2s;
  transform-origin: center center;
}

/* Smooth pop animation when avatar becomes visible */
.message-avatar[style*="opacity: 1"] {
  transition: opacity 0.2s ease, visibility 0s linear 0s;
  animation: avatarPop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* Ghost placeholder - transparent but maintains layout */
.message-avatar[style*="opacity: 0"] {
  pointer-events: none;
  transform: scale(0.8);
}

/* Avatar pop animation */
@keyframes avatarPop {
  0% {
    transform: scale(0.5);
    opacity: 0;
  }
  60% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* User avatar on the right */
.message.user .message-avatar {
  order: 2;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.message.user .avatar-initials {
  color: white;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  text-transform: uppercase;
}

/* Assistant avatar on the left */
.message.assistant .message-avatar {
  order: 1;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 0; /* Align with message content bottom */
}

.message.assistant .message-content {
  order: 2;
  flex: 0 1 auto; /* Don't grow, but can shrink if needed */
}

.message.assistant .formas-icon {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

/* Ensure actions are positioned correctly in assistant messages */
.message.assistant .message-actions {
  order: 3;
  margin-left: 0;
}

/* Invisible avatar for thinking messages - maintains alignment */
.message.thinking .message-avatar,
.message.assistant.thinking .message-avatar {
  opacity: 0 !important;
  pointer-events: none !important;
  visibility: hidden !important;
  width: 32px !important; /* Ensure it maintains full width */
  flex-shrink: 0 !important;
}

/* Single user image (small, fixed square size) */
.message.user:not(.multi-image) .chat-img-wrapper {
  border-radius: 20px;
  margin-left: auto;
  margin-bottom: 8px; /* Add space between image and text */
  display: block;
  width: 160px;
  height: 160px;
  aspect-ratio: 1 / 1;
  align-self: flex-end;
  overflow: hidden;
  position: relative;
}

/* Multi-image user message container */
.message.user.multi-image .message-content {
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  max-width: 50%;
}

.message.user.multi-image .image-grid {
  display: grid;
  gap: 8px;
  align-self: flex-end;
  width: auto;
  max-width: 100%;
  order: 1; /* Images come first */
}

/* Multi-image grid item sizing */
.message.user.multi-image .chat-img-wrapper {
  margin: 0;
  width: 160px;
  height: 160px;
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
}

/* User image content is always cropped to fill the square */
.message.user .chat-img-wrapper img,
.message.user .chat-img-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: relative;
  z-index: 1;
  display: block;
}

/* Layout for 2 images */
.message.user.multi-image.image-count-2 .image-grid {
  grid-template-columns: repeat(2, 1fr);
  width: 328px; /* 2 * 160px + 8px gap */
  height: 160px;
}

/* Layout for 3 images (L-shape from bottom-right) */
.message.user.multi-image.image-count-3 .image-grid {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  width: 328px;
  height: 328px;
}
.message.user.multi-image.image-count-3 .chat-img-wrapper:nth-child(1) { grid-column: 2; grid-row: 2; }
.message.user.multi-image.image-count-3 .chat-img-wrapper:nth-child(2) { grid-column: 1; grid-row: 2; }
.message.user.multi-image.image-count-3 .chat-img-wrapper:nth-child(3) { grid-column: 2; grid-row: 1; }

/* Layout for 4 images (2x2 grid) */
.message.user.multi-image.image-count-4 .image-grid {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  width: 328px;
  height: 328px;
}

/* Text in a multi-image message */
.message.user.multi-image .message-text {
  order: 2; /* Text comes after images */
  align-self: flex-end;
  text-align: right;
  width: 100%;
  margin-top: 12px; /* Add space between images and text */
}

/* Desktop screen adjustments for user messages */
@media (min-width: 1024px) {
  .message.user {
    margin-left: auto;
    margin-right: 20px;
  }
}
@media (min-width: 1600px) {
  .message.user {
    margin-right: calc((100vw - 1600px) / 2 + 20px);
  }
}

/* Mobile responsive adjustments for user multi-image */
@media (max-width: 768px) {
  .message.user.multi-image .message-content { max-width: 85%; }
  .message.user:not(.multi-image) .chat-img-wrapper { width: 120px; height: 120px; }
  .message.user.multi-image .chat-img-wrapper { width: 120px; height: 120px; border-radius: 10px; }
  .message.user.multi-image.image-count-2 .image-grid { width: 248px; height: 120px; }
  .message.user.multi-image.image-count-3 .image-grid,
  .message.user.multi-image.image-count-4 .image-grid { width: 248px; height: 248px; }
}

/* Very small screen adjustments */
@media (max-width: 480px) {
  .message.user:not(.multi-image) .chat-img-wrapper { width: 100px; height: 100px; }
  .message.user.multi-image .chat-img-wrapper { width: 100px; height: 100px; }
  .message.user.multi-image.image-count-2 .image-grid { width: 208px; height: 100px; }
  .message.user.multi-image.image-count-3 .image-grid,
  .message.user.multi-image.image-count-4 .image-grid { width: 208px; height: 208px; }
}


/* ================= MODULE MESSAGE STYLES ================= */

.message.module {
  display: flex;
  justify-content: center;
  width: 100%;
  max-width: none;
  margin-bottom: 16px;
}

.message.module .message-content {
  background: var(--secondary-bg);
  border: 1px solid var(--border-subtle);
  border-radius: 26px;
  padding: 16px;
  width: 100%;
  max-width: 900px;
  position: relative;
  overflow: hidden;
}

.module-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-subtle);
}

.module-icon {
  width: 36px;
  height: 36px;
  background: var(--tertiary-bg);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.module-info {
  flex: 1;
  min-width: 0;
}

.module-title {
  font-size: 15px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.module-description {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 400;
}

.module-badge {
  background: var(--button-secondary);
  color: var(--text-primary);
  padding: 3px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 400;
  border: 1px solid var(--border-color);
  flex-shrink: 0;
}

.module-controls {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.module-control-btn,
.module-minimize {
  background: var(--tertiary-bg);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.module-control-btn:hover,
.module-minimize:hover {
  background: var(--hover-subtle);
  color: var(--text-primary);
  transform: scale(1.05);
}

.module-fullscreen:hover {
  background: var(--accent-color);
  color: white;
}

.module-fullscreen.active {
  background: var(--accent-color);
  color: white;
}

.module-interface {
  background: var(--primary-bg);
  border-radius: 8px;
  padding: 8px;
  border: 1px solid var(--border-subtle);
  min-height: 200px;
  max-height: 70vh;
  overflow-y: auto;
}

.module-interface.drag-over {
  background: var(--hover-subtle);
  border: 1px dashed var(--border-color);
  transition: background 0.15s, border 0.15s;
}

.module-fullscreen-active {
  opacity: 0.5;
  pointer-events: none;
}

.module-fullscreen-active .message-content {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px rgba(74, 158, 255, 0.2);
}

.canvas-interface,
.interior-interface,
.editor-interface {
  text-align: center;
  padding: 16px;
}

.canvas-interface h3,
.editor-interface h3 {
  color: var(--text-primary);
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 500;
}

.canvas-controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 12px;
}

.canvas-btn {
  background: var(--button-secondary);
  color: var(--button-secondary-text);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 400;
  transition: all 0.15s ease;
}

.canvas-btn:hover {
  background: var(--hover-bg);
  transform: translateY(-1px);
}

.interior-interface {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 12px;
}

.interior-section h4 {
  color: var(--text-primary);
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 500;
}

.interior-option {
  background: var(--tertiary-bg);
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  border: 1px solid var(--border-subtle);
  font-size: 12px;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.interior-option:hover {
  background: var(--hover-subtle);
  border-color: var(--border-color);
  transform: translateY(-1px);
}

.editor-workspace {
  background: var(--tertiary-bg);
  border: 1px dashed var(--border-color);
  border-radius: 8px;
  padding: 24px;
  margin: 12px 0;
  cursor: pointer;
  transition: all 0.15s ease;
  color: var(--text-secondary);
  min-height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.editor-workspace:hover {
  border-color: var(--text-muted);
  background: var(--hover-subtle);
  color: var(--text-primary);
}


/* ================= MISC CHAT COMPONENTS ================= */

/* Date Bubble */
.chat-date-bubble {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
  z-index: 10;
}

.chat-date-bubble.show {
  opacity: 1;
}

/* Image Actions */
.chat-img-actions {
  display: flex;
  gap: 6px;
  margin-top: 2px;
  margin-bottom: 2px;
  justify-content: flex-end;
  width: 100%;
}

.chat-img-actions[aria-hidden="true"] {
  display: flex;
}

.chat-img-action {
  background: #666;
  color: #fff;
  border: none;
  padding: 2px 8px;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  cursor: pointer;
}

.chat-img-action:focus {
  outline: none;
}

/* Collapsible Text */
.message-text.cursor::after {
  content: '_';
  display: inline-block;
}

.message-text.typing::after {
  animation: blinkCursor 0.6s steps(2, start) infinite;
}

.message-text.collapsible {
  display: block;
  overflow: hidden;
  padding-right: 20px;
  will-change: max-height;
  transition: max-height 0.28s ease, padding-top 0.28s ease, padding-bottom 0.28s ease, margin-top 0.28s ease;
  position: relative;
}

.message-text.collapsible:not(.expanded) {
  padding-top: 0;
  padding-bottom: 0;
  margin-top: 0;
  max-height: 7.5em; /* ~5 lines at 1.5 line-height */
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
}

.message-text.collapsible:not(.expanded)::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2em;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0), var(--message-bg, #373737));
  z-index: 1;
}

.message-text.collapsible.expanded {
  overflow: hidden;
}

.message-collapse-toggle {
  position: absolute;
  bottom: 6px;
  right: 8px;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 50%;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
  z-index: 2;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  backdrop-filter: blur(6px);
}

.message-collapse-toggle:hover {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.25);
}

.message-collapse-toggle svg {
  width: 14px;
  height: 14px;
  color: rgba(255,255,255,0.9);
  transition: transform 0.25s ease;
}

.message-collapse-toggle.expanded svg {
  transform: rotate(180deg);
}

/* AI Thinking Indicator */
.ai-thinking {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 400;
}

.thinking-dots {
  display: flex;
  gap: 4px;
}

.thinking-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: thinking 1.4s infinite ease-in-out;
}
.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }

.thinking-dots.inline {
  display: inline-flex;
  margin-left: 4px;
}

/* Progressive Thinking Animation */
.ai-thinking.progressive {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px;
  align-items: flex-start; /* Left-align the thinking sequence */
}

.thinking-line {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 0;
  transition: all 0.3s ease;
  width: 100%;
  justify-content: flex-start; /* Left-align the content */
  position: relative;
}

.thinking-line.active {
  color: var(--text-primary);
}

.thinking-line.active .thinking-text {
  background: linear-gradient(
    90deg,
    var(--text-secondary) 0%,
    var(--text-primary) 25%,
    var(--text-primary) 50%,
    var(--text-secondary) 75%,
    var(--text-secondary) 100%
  );
  background-size: 200% 100%;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textShine 1.8s ease-in-out infinite;
  font-weight: 500;
}

.thinking-line .thinking-dots {
  flex-shrink: 0;
}

.thinking-line .thinking-text {
  flex: 0 0 auto;
  font-size: 13px;
  line-height: 1.4;
  background: var(--message-bg, #373737);
  padding: 0 8px;
  border-radius: 2px; /* Reduced from 4px for sharper edges */
}

.thinking-line::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--text-muted), transparent);
  opacity: 0.3;
  z-index: -1;
}

/* Horizontal dots for generating state */
.thinking-dots.horizontal {
  gap: 6px;
}

.thinking-dots.horizontal .thinking-dot {
  animation: thinkingPulseHorizontal 1.2s ease-in-out infinite both;
}

.thinking-dots.horizontal .thinking-dot:nth-child(1) { animation-delay: -0.24s; }
.thinking-dots.horizontal .thinking-dot:nth-child(2) { animation-delay: -0.12s; }
.thinking-dots.horizontal .thinking-dot:nth-child(3) { animation-delay: 0s; }

@keyframes thinkingPulseHorizontal {
  0%, 80%, 100% {
    transform: scale(0.7);
    opacity: 0.4;
  }
  40% {
    transform: scale(1.1);
    opacity: 1;
  }
}

/* Ensure thinking messages maintain flex layout with avatar space */
.message.assistant.thinking {
  display: flex !important;
  justify-content: flex-start !important;
  align-items: flex-end !important;
  gap: 8px !important;
}

/* Compact, left-aligned thinking bubble (no background) */
.message.assistant.thinking .message-content {
  background: transparent !important;
  border: 0 !important;
  padding: 0 !important;
  max-width: 100% !important;
  box-shadow: none !important;
}

.message.assistant.thinking .ai-thinking.progressive {
  padding: 0 !important;
  gap: 2px !important;
  align-items: flex-start;
}

.message.assistant.thinking .thinking-line {
  gap: 8px !important;
  padding: 0 !important;
  min-height: 0 !important;
}

.message.assistant.thinking .thinking-dots {
  width: 18px; /* tighter alignment */
}

.message.assistant.thinking .thinking-dot {
  width: 3px;
  height: 3px;
}

.message.assistant.thinking .thinking-line .thinking-text {
  padding: 0 !important;
  margin: 0 !important;
  /* Keep gradient shine for active; just remove capsule background color */
  background-color: transparent !important;
}

.message.assistant.thinking .thinking-line.completed .thinking-text {
  background: none !important;
  padding: 0 !important;
}

@keyframes textShine {
  0% {
    background-position: -200% center;
    opacity: 0.3;
  }
  25% {
    opacity: 0.6;
  }
  50% {
    background-position: 0% center;
    opacity: 1;
  }
  75% {
    opacity: 0.6;
  }
  100% {
    background-position: 200% center;
    opacity: 0.3;
  }
}

/* Analysing text with shine effect */
.analysing-text {
  position: relative;
  display: inline-block;
  background: linear-gradient(
    90deg,
    var(--text-secondary) 0%,
    var(--text-primary) 25%,
    var(--text-primary) 50%,
    var(--text-secondary) 75%,
    var(--text-secondary) 100%
  );
  background-size: 200% 100%;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textShine 1.8s ease-in-out infinite;
  font-weight: 500;
  letter-spacing: 0.02em;
}

@keyframes textShine {
  0% {
    background-position: -200% center;
    opacity: 0.3;
  }
  25% {
    opacity: 0.6;
  }
  50% {
    background-position: 0% center;
    opacity: 1;
  }
  75% {
    opacity: 0.6;
  }
  100% {
    background-position: 200% center;
    opacity: 0.3;
  }
}

/* Drag Preview */
.drag-preview {
  display: flex;
  align-items: center;
  transform-origin: center;
}
.drag-preview .sidebar-item-icon {
  width: 18px;
  height: 18px;
  margin-right: 10px;
  margin-left: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
.drag-preview .sidebar-item-text {
  flex: 1;
  font-size: 13px;
}
.drag-preview .sidebar-item-badge {
  background: var(--button-secondary);
  color: rgb(170, 170, 170);
  font-size: 9px;
  padding: 1px 4px;
  border-radius: 6px;
  font-weight: 400;
  margin-left: 14px;
  flex-shrink: 0;
}


/* ================= MEDIA STATES (LAZY LOAD, PLACEHOLDERS) ================= */

/* Universal Hover Effect */
.chat-img-wrapper:hover::before,
.chat-model-wrapper:hover::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  border-radius: inherit;
  pointer-events: none;
}

/* Generation placeholders moved to placeholders.css */

/* Lazy Loading */
.chat-img-wrapper.lazy-load { background: var(--secondary-bg); }
.message.assistant .chat-img-wrapper.lazy-load { background: #373737; }

.chat-img-wrapper.lazy-load img,
.chat-img-wrapper.lazy-load video {
  opacity: 0;
  transform: scale(0.98);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.chat-img-wrapper.lazy-load.loaded { background: transparent; }
.message.assistant .chat-img-wrapper.lazy-load.loaded {
  aspect-ratio: auto;
  min-height: 0;
  height: auto;
}

.chat-img-wrapper.lazy-load.loaded img,
.chat-img-wrapper.lazy-load.loaded video {
  opacity: 1;
  transform: scale(1);
}

.chat-img-wrapper.lazy-load .spinner-ring {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 2;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.chat-img-wrapper.lazy-load.loaded .spinner-ring {
  opacity: 0;
}

/* Image placeholders moved to placeholders.css */

.chat-img-wrapper.error {
  width: 96px;
  height: 96px;
}


/* ================= 3D MODEL STYLES ================= */

.chat-model-wrapper {
  position: relative;
  display: inline-block;
  margin-bottom: 0;
  border-radius: 6px;
  overflow: hidden;
  /* Ensure the wrapper respects the same sizing as images */
  max-width: 100%;
  height: auto;
}

/* ================= CLEAN 3D MODEL STYLES (Image-like appearance) ================= */

.chat-model-wrapper-clean {
  position: relative;
  display: inline-block;
  margin-bottom: 0;
  border-radius: 8px;
  overflow: hidden;
  /* Match image wrapper styling exactly */
  max-width: 100%;
  height: auto;
  background: transparent;
  /* Remove any special 3D model styling */
  min-height: unset;
  aspect-ratio: unset;
}

/* Clean 3D model container - matches image behavior */
.chat-model-container-clean {
  position: relative;
  width: 100%;
  height: 100%; /* Match wrapper height to maintain aspect ratio */
  max-width: 100%;
  /* Remove forced min-height for clean appearance */
  min-height: unset;
  background: transparent;
  border-radius: 8px;
  overflow: hidden;
}

/* Clean 3D model thumbnail - matches image styling */
.chat-model-thumb-clean {
  width: 100%;
  height: 100%; /* Fill container height to maintain aspect ratio */
  object-fit: contain;
  display: block;
  cursor: pointer;
  border-radius: 8px;
}

/* Clean 3D model canvas/viewer - matches image behavior */
.chat-model-container-clean canvas,
.chat-model-container-clean model-viewer {
  width: 100% !important;
  height: 100% !important; /* Fill container height to maintain aspect ratio */
  max-width: 100% !important;
  display: block;
  pointer-events: none;
  object-fit: contain;
  border-radius: 8px;
}

/* Assistant clean model wrappers - match image wrapper sizing exactly */
.message.assistant .chat-model-wrapper-clean {
  position: relative;
  display: block;
  max-width: 600px; /* Same as images */
  width: auto;
  height: auto;
  margin-bottom: 0;
  border-radius: 8px;
  overflow: hidden;
  transition: width 0.3s ease, height 0.3s ease;
  /* Maintain 1:1 aspect ratio for 3D models */
  background: transparent;
  min-height: unset;
  aspect-ratio: 1 / 1; /* Keep square aspect for 3D models */
}

/* Desktop adjustments for clean model wrappers */
@media (min-width: 1024px) {
  .message.assistant .chat-model-wrapper-clean {
    max-width: 800px; /* Same as images on desktop */
  }
}

/* Mobile adjustments for clean model wrappers */
@media (max-width: 768px) {
  .message.assistant .chat-model-wrapper-clean {
    max-width: 400px; /* Same as images on mobile */
  }
}

.chat-model-container {
  position: relative;
  width: 100%;
  height: auto;
  /* Ensure the container doesn't exceed the wrapper bounds */
  max-width: 100%;
  /* Ensure proper positioning context for retry button */
  min-height: 200px;
}

.chat-model-thumb {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  cursor: pointer;
}

.chat-model-container canvas,
.chat-model-container model-viewer {
  width: 100% !important;
  height: auto !important;
  max-width: 100% !important;
  display: block;
  pointer-events: none;
  /* Ensure the canvas respects the container bounds */
  object-fit: contain;
}

.chat-model-overlay {
  position: absolute;
  inset: 0;
  cursor: pointer;
}

/* Ensure 3D models have the same responsive behavior as images */
.chat-model-wrapper {
  /* Keep the proper sizing from the specific .message.assistant .chat-model-wrapper rules */
  /* Don't override with 100% - let the responsive rules control sizing */
}

/* Force canvas elements to respect container bounds */
.chat-model-container canvas[style*="width: 800px"],
.chat-model-container canvas[style*="height: 800px"] {
  width: 100% !important;
  height: auto !important;
  max-width: 100% !important;
}

/* 3D Model Retry Button Styling */
.chat-model-container .retry-button,
.chat-model-container [style*="RETRY DOWNLOAD"] {
  position: absolute !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  color: #ffffff !important;
  font-size: 14px !important;
  font-weight: 600 !important;
  text-align: center !important;
  cursor: pointer !important;
  background: rgba(0, 0, 0, 0.7) !important;
  padding: 12px 20px !important;
  border-radius: 8px !important;
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  backdrop-filter: blur(10px) !important;
  transition: all 0.3s ease !important;
  z-index: 10 !important;
  width: auto !important;
  height: auto !important;
  max-width: calc(100% - 40px) !important;
  word-wrap: break-word !important;
  /* Ensure proper centering by setting explicit positioning context */
  margin: 0 !important;
  line-height: 1.2 !important;
}

.chat-model-container .retry-button:hover,
.chat-model-container [style*="RETRY DOWNLOAD"]:hover {
  background: rgba(0, 0, 0, 0.9) !important;
  border-color: rgba(255, 255, 255, 0.4) !important;
  transform: translate(-50%, -50%) scale(1.05) !important;
}


/* ================= KEYFRAMES & ANIMATIONS ================= */

/* Removed unused chat-spin animation */

@keyframes chat-spin-rotate {
  to { transform: rotate(360deg); }
}

@keyframes plasmaGlow {
  0% { transform: scale(1); opacity: 0.6; }
  50% { transform: scale(1.1); opacity: 0.8; }
  100% { transform: scale(1); opacity: 0.6; }
}

@keyframes thinking {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1.0); }
}

@keyframes blinkCursor {
  from, to { opacity: 1; }
  50% { opacity: 0; }
}
