/* ===========================================
   JAVY 5 - AI Chat Window
   Chappy AI V2 Style - Floating, draggable
   + Conversation sidebar
   =========================================== */

/* ---- Chat Container ---- */
.chat-container {
  position: fixed;
  bottom: 60px;
  right: 20px;
  width: var(--chat-width);
  height: 520px;
  min-width: var(--chat-min-width);
  min-height: var(--chat-min-height);
  max-width: 90vw;
  max-height: calc(100vh - 80px);
  background: rgba(12, 12, 25, 0.92);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xl);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: var(--shadow-xl), 0 0 40px rgba(139, 92, 246, 0.1);
  display: flex;
  flex-direction: column;
  z-index: var(--z-chat);
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.chat-container.visible,
.chat-container:not(.hidden) {
  opacity: 1;
  transform: none;
  pointer-events: all;
}

/* While dragging, disable all transitions */
.chat-container.dragging {
  transition: none !important;
  transform: none !important;
}

.chat-container.hidden {
  display: none;
}

.chat-container.minimized {
  height: auto !important;
  min-height: auto;
  width: 280px;
}

.chat-container.minimized .chat-main,
.chat-container.minimized .chat-resize-handle {
  display: none;
}

/* ---- Chat Header (Drag Handle) ---- */
.chat-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 12px 14px;
  border-bottom: 1px solid var(--glass-border);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  background: linear-gradient(135deg, rgba(139, 92, 246, 0.08) 0%, transparent 60%);
  cursor: move;
  user-select: none;
  flex-shrink: 0;
}

.chat-header:active {
  cursor: grabbing;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex: 1;
  min-width: 0;
}

/* AI icon with gradient */
.chat-header-icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  border-radius: var(--radius-md);
  flex-shrink: 0;
  font-size: 14px;
}

.chat-header-title {
  font-size: var(--font-md);
  font-weight: 600;
  color: var(--text-primary);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* ---- Mode Toggle (Chat / Bearbeiten) ---- */
.chat-mode-toggle {
  display: flex;
  background: rgba(255, 255, 255, 0.06);
  border-radius: var(--radius-md);
  padding: 2px;
  gap: 2px;
  flex-shrink: 0;
}

.chat-mode-btn {
  font-size: 11px;
  font-weight: 500;
  padding: 3px 10px;
  border: none;
  border-radius: calc(var(--radius-md) - 2px);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.chat-mode-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
}

.chat-mode-btn.active {
  color: white;
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  box-shadow: 0 1px 4px rgba(139, 92, 246, 0.3);
}

.chat-header-status {
  font-size: var(--font-xs);
  color: var(--text-muted);
  padding: 2px 8px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: var(--radius-full);
  flex-shrink: 0;
}

.chat-header-status.online {
  color: var(--color-success);
  background: rgba(16, 185, 129, 0.1);
}

.chat-header-status.streaming {
  color: var(--accent-cyan);
  background: rgba(6, 182, 212, 0.1);
  animation: pulseStatus 1.5s ease-in-out infinite;
}

@keyframes pulseStatus {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

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

.chat-header-btn {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: var(--font-sm);
  transition: all var(--transition-fast);
  flex-shrink: 0;
  cursor: pointer;
}

.chat-header-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
}

/* ---- Chat Main (Sidebar + Content wrapper) ---- */
.chat-main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ---- Chat Content (Body + Input) ---- */
.chat-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

/* ---- Chat Sidebar ---- */
.chat-sidebar {
  width: 220px;
  min-width: 220px;
  background: rgba(0, 0, 0, 0.25);
  border-right: 1px solid var(--glass-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.2s ease, min-width 0.2s ease, opacity 0.2s ease;
}

.chat-sidebar.hidden {
  width: 0;
  min-width: 0;
  opacity: 0;
  border-right: none;
}

.chat-sidebar-header {
  padding: 10px 14px;
  font-size: var(--font-sm);
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--glass-border);
  flex-shrink: 0;
}

.chat-sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
  scrollbar-width: thin;
  scrollbar-color: rgba(139, 92, 246, 0.2) transparent;
}

.chat-sidebar-list::-webkit-scrollbar {
  width: 4px;
}

.chat-sidebar-list::-webkit-scrollbar-thumb {
  background: rgba(139, 92, 246, 0.2);
  border-radius: 10px;
}

.chat-sidebar-empty {
  text-align: center;
  padding: var(--space-lg);
  color: var(--text-muted);
  font-size: var(--font-xs);
}

.chat-sidebar-item {
  padding: 8px 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative;
  margin-bottom: 2px;
}

.chat-sidebar-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.chat-sidebar-item.active {
  background: rgba(139, 92, 246, 0.12);
  box-shadow: inset 0 0 0 1px rgba(139, 92, 246, 0.2);
}

.chat-sidebar-item-title {
  font-size: var(--font-xs);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 20px;
  line-height: 1.4;
}

.chat-sidebar-item-meta {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

.chat-sidebar-item-delete {
  position: absolute;
  top: 8px;
  right: 6px;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  opacity: 0;
  transition: all var(--transition-fast);
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
}

.chat-sidebar-item:hover .chat-sidebar-item-delete {
  opacity: 0.6;
}

.chat-sidebar-item-delete:hover {
  opacity: 1 !important;
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

/* Inline rename input */
.chat-sidebar-rename-input {
  width: 100%;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(139, 92, 246, 0.4);
  border-radius: 3px;
  color: var(--text-primary);
  font-size: var(--font-xs);
  padding: 1px 4px;
  outline: none;
  font-family: var(--font-family);
}

/* Loading state */
.chat-loading {
  text-align: center;
  padding: var(--space-lg);
  color: var(--text-muted);
  font-size: var(--font-sm);
}

/* ---- Chat Message Area ---- */
.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: rgba(139, 92, 246, 0.3) transparent;
}

.chat-body::-webkit-scrollbar {
  width: 5px;
}

.chat-body::-webkit-scrollbar-thumb {
  background: rgba(139, 92, 246, 0.3);
  border-radius: 10px;
}

/* Welcome message */
.chat-welcome {
  text-align: center;
  padding: var(--space-2xl) var(--space-lg);
  color: var(--text-muted);
}

.chat-welcome-icon {
  font-size: 40px;
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.chat-welcome-title {
  font-size: var(--font-md);
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: var(--space-xs);
}

.chat-welcome-text {
  font-size: var(--font-sm);
  line-height: 1.6;
}

/* Message Bubbles */
.chat-message {
  display: flex;
  gap: var(--space-sm);
  max-width: 95%;
  animation: messageIn 200ms ease;
}

.chat-message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-message.assistant {
  align-self: flex-start;
}

.chat-message-avatar {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 600;
  flex-shrink: 0;
  margin-top: 2px;
}

.chat-message.user .chat-message-avatar {
  background: rgba(139, 92, 246, 0.15);
  color: var(--accent-purple);
}

.chat-message.assistant .chat-message-avatar {
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  color: white;
}

.chat-message-bubble {
  padding: 8px 12px;
  border-radius: var(--radius-lg);
  font-size: var(--font-sm);
  line-height: 1.6;
  word-break: break-word;
}

.chat-message.user .chat-message-bubble {
  background: rgba(139, 92, 246, 0.15);
  border: 1px solid rgba(139, 92, 246, 0.2);
  color: var(--text-primary);
  border-bottom-right-radius: var(--radius-sm);
}

.chat-message.assistant .chat-message-bubble {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--glass-border);
  color: var(--text-secondary);
  border-bottom-left-radius: var(--radius-sm);
}

/* Typing indicator */
.chat-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: var(--space-sm) var(--space-md);
}

.chat-typing-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typingBounce 1.2s ease-in-out infinite;
}

.chat-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

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

/* ---- Chat Input Area ---- */
.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
  padding: 10px 12px;
  border-top: 1px solid var(--glass-border);
  background: rgba(0, 0, 0, 0.2);
  border-radius: 0 0 var(--radius-xl) var(--radius-xl);
  flex-shrink: 0;
}

.chat-input-wrapper {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
}

.chat-input {
  flex: 1;
  min-height: 36px;
  max-height: 100px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: var(--font-sm);
  line-height: 1.4;
  resize: none;
  outline: none;
  transition: border-color var(--transition-fast);
  overflow-y: auto;
}

.chat-input::placeholder {
  color: var(--text-muted);
}

.chat-input:focus {
  border-color: rgba(139, 92, 246, 0.4);
  box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.1);
}

.chat-input-btn {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: var(--font-sm);
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-input-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
}

.chat-send-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-purple-hover));
  color: white;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-send-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 12px var(--glow-purple);
}

.chat-send-btn:active {
  transform: scale(0.95);
}

.chat-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.chat-send-btn svg {
  width: 16px;
  height: 16px;
}

/* ---- Resize Handle ---- */
.chat-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  width: 14px;
  height: 14px;
  cursor: nw-resize;
  z-index: 1;
}

.chat-resize-handle::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 7px;
  height: 7px;
  border-left: 2px solid var(--text-muted);
  border-top: 2px solid var(--text-muted);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.chat-container:hover .chat-resize-handle::before {
  opacity: 0.4;
}

/* ---- Markdown in Chat Bubbles ---- */
.chat-message-bubble code {
  background: rgba(139, 92, 246, 0.15);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.85em;
}

.chat-message-bubble pre {
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  margin: 6px 0;
  overflow-x: auto;
  scrollbar-width: thin;
}

.chat-message-bubble pre code {
  background: none;
  padding: 0;
  font-size: 0.82em;
  line-height: 1.5;
  color: var(--accent-cyan);
}

.chat-message-bubble strong {
  color: var(--text-primary);
  font-weight: 600;
}

.chat-message-bubble em {
  color: var(--accent-purple);
  font-style: italic;
}

.chat-message-bubble ul,
.chat-message-bubble ol {
  margin: 4px 0;
  padding-left: 18px;
}

.chat-message-bubble li {
  margin: 2px 0;
  line-height: 1.5;
}

.chat-message-bubble li::marker {
  color: var(--accent-purple);
}

/* ---- Streaming Cursor ---- */
.chat-message-bubble.streaming::after {
  content: '\2588';
  color: var(--accent-cyan);
  animation: blink 1s step-end infinite;
  margin-left: 1px;
}

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

/* ---- Tool-Use Indicator ---- */
.chat-tool-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  margin-top: 4px;
  background: rgba(6, 182, 212, 0.08);
  border: 1px solid rgba(6, 182, 212, 0.15);
  border-radius: var(--radius-md);
  font-size: var(--font-xs);
  color: var(--accent-cyan);
}

.chat-tool-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid rgba(6, 182, 212, 0.3);
  border-top-color: var(--accent-cyan);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ---- Maximize State ---- */
.chat-container.maximized {
  position: fixed !important;
  inset: 10px !important;
  width: auto !important;
  height: auto !important;
  max-width: none !important;
  max-height: none !important;
  right: 10px !important;
  bottom: 10px !important;
  border-radius: var(--radius-xl);
}

/* ---- Stop Button ---- */
.chat-stop-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #ef4444;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-stop-btn:hover {
  background: rgba(239, 68, 68, 0.25);
  transform: scale(1.05);
}

.chat-stop-btn svg {
  width: 16px;
  height: 16px;
}

/* ---- Upload Button ---- */
.chat-upload-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.chat-upload-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
}

/* ---- Hidden utility ---- */
.chat-send-btn.hidden,
.chat-stop-btn.hidden {
  display: none;
}

/* ---- Mobile ---- */
@media (max-width: 639px) {
  .chat-container {
    position: fixed;
    inset: 0;
    width: 100% !important;
    height: 100% !important;
    max-width: none;
    max-height: none;
    border-radius: 0;
    bottom: 0;
    right: 0;
  }

  .chat-container.minimized {
    inset: auto 0 0 0;
    width: 100% !important;
    height: auto !important;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  }

  .chat-header {
    cursor: default;
    border-radius: 0;
  }

  .chat-resize-handle {
    display: none;
  }

  /* Mobile sidebar: overlay */
  .chat-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 2;
    width: 260px;
    min-width: 260px;
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    background: rgba(12, 12, 25, 0.95);
  }

  .chat-sidebar.hidden {
    width: 0;
    min-width: 0;
  }
}
