/* ==========================================================================
   ai-panel.css — Right-side AI chat panel with resize, messages, input
   UI/UX Pro Max — Notion/Linear-level fixed panel with polished interactions
   ========================================================================== */

/* ---------- Panel Inner ---------- */
.ai-panel-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  background: white;
  border-left: 1px solid #E5E7EB;
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.04), -1px 0 4px rgba(0, 0, 0, 0.02);
}

/* ---------- Resize Handle ---------- */
.ai-resize-handle {
  position: absolute;
  left: -3px;
  top: 0;
  bottom: 0;
  width: 4px;
  cursor: col-resize;
  z-index: 10;
  background: #E5E7EB;
  transition: all 150ms ease;
}

.ai-resize-handle:hover,
.ai-resize-handle.active {
  background: #818CF8;
  width: 4px;
}

.ai-resize-handle::after {
  content: "";
  position: absolute;
  left: -2px;
  top: 0;
  bottom: 0;
  width: 8px;
  cursor: col-resize;
}

/* ---------- Header ---------- */
.ai-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid #E5E7EB;
  flex-shrink: 0;
  background: white;
  height: var(--header-height);
}

.ai-header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

/* ---------- Write Toggle ---------- */
.ai-write-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.ai-write-toggle input {
  display: none;
}

.ai-write-slider {
  position: relative;
  width: 32px;
  height: 18px;
  background: var(--gray-200, #E5E7EB);
  border-radius: 9px;
  transition: background 200ms ease;
  flex-shrink: 0;
}

.ai-write-slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
  transition: transform 200ms ease;
}

.ai-write-toggle input:checked ~ .ai-write-slider {
  background: #F59E0B;
}

.ai-write-toggle input:checked ~ .ai-write-slider::after {
  transform: translateX(14px);
}

/* Lock/unlock icon toggle */
.ai-write-icon {
  flex-shrink: 0;
  transition: color 200ms ease;
}

.ai-write-icon--locked {
  color: var(--text-muted, #9CA3AF);
}

.ai-write-icon--unlocked {
  display: none;
  color: #D97706;
}

.ai-write-toggle input:checked ~ .ai-write-icon--locked {
  display: none;
}

.ai-write-toggle input:checked ~ .ai-write-icon--unlocked {
  display: block;
}

.ai-write-label {
  font-size: 12px;
  color: var(--text-muted, #9CA3AF);
  transition: color 200ms ease;
  white-space: nowrap;
}

.ai-write-toggle input:checked ~ .ai-write-label {
  color: #D97706;
  font-weight: 500;
}

.ai-write-toggle input:checked ~ .ai-write-label::after {
  content: " ON";
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

/* ---------- Opus Toggle ---------- */
.ai-opus-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}
.ai-opus-toggle input { display: none; }
.ai-opus-slider {
  position: relative;
  width: 32px;
  height: 18px;
  background: var(--gray-200, #E5E7EB);
  border-radius: 9px;
  transition: background 200ms ease;
  flex-shrink: 0;
}
.ai-opus-slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
  transition: transform 200ms ease;
}
.ai-opus-toggle input:checked ~ .ai-opus-slider {
  background: #7C3AED;  /* 紫色區分 Opus 模式 */
}
.ai-opus-toggle input:checked ~ .ai-opus-slider::after {
  transform: translateX(14px);
}
.ai-opus-label {
  font-size: 12px;
  color: var(--text-muted, #9CA3AF);
  font-weight: 500;
  white-space: nowrap;
  transition: color 200ms ease;
}
.ai-opus-toggle input:checked ~ .ai-opus-label {
  color: #7C3AED;
}

/* Opus hint — Notion callout 風格 */
.ai-opus-hint {
  margin: 8px 14px 0;
  padding: 10px 12px;
  background: #F5F3FF;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.55;
  color: var(--text, #1F2937);
}
.ai-opus-hint strong {
  color: #6D28D9;
  font-weight: 600;
}
.ai-opus-hint code {
  background: rgba(124, 58, 237, 0.1);
  color: #6D28D9;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 11px;
}

/* ---------- Chat Messages (scrollable) ---------- */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
  background: var(--bg);
}

/* Welcome screen */
.ai-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-md);
  padding: var(--space-3xl) var(--space-xl);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
}

.ai-welcome-icon {
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #EEF2FF 0%, var(--primary-100) 100%);
  border-radius: var(--radius-full);
}

.ai-welcome-icon svg {
  width: 28px;
  height: 28px;
  color: #6366F1;
}

.ai-welcome h3 {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text);
  margin-top: var(--space-sm);
}

/* Suggestion chips */
.ai-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
  justify-content: center;
  margin-top: var(--space-sm);
}

.ai-suggestion-chip {
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-xs);
  color: #6366F1;
  background: #EEF2FF;
  border: 1px solid var(--primary-100);
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all 150ms ease;
}

.ai-suggestion-chip:hover {
  background: var(--primary-100);
  border-color: #6366F1;
}

.ai-suggestion-chip:focus-visible {
  box-shadow: var(--focus-ring);
}

.ai-suggestion-chip:active {
  transform: scale(0.97);
}

.ai-suggestion-chip:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* ---------- Message Bubbles ---------- */
.ai-message {
  display: flex;
  margin-bottom: var(--space-md);
  animation: fadeSlideIn 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.ai-message.user,
.ai-message-user {
  justify-content: flex-end;
}

.ai-message.assistant,
.ai-message-assistant {
  justify-content: flex-start;
  gap: var(--space-sm);
}

/* Shared bubble base */
.ai-message-bubble,
.ai-message-content {
  padding: var(--space-md) var(--space-lg);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  word-break: break-word;
  max-width: 88%;
}

/* User bubble — right-aligned, indigo gradient, white text */
.ai-message.user .ai-message-bubble,
.ai-message-user .ai-message-content {
  background: #EEF2FF !important;
  color: #1E1E1E !important;
  border-radius: 16px 16px 4px 16px;
  border: 1px solid #C7D2FE;
  box-shadow: none;
  max-width: 85%;
}

.ai-message-user .ai-message-content,
.ai-message-user .ai-message-content p,
.ai-message-user .ai-message-content * {
  color: #1E1E1E !important;
}

/* Assistant bubble — left-aligned, light bg with border */
.ai-message.assistant .ai-message-bubble,
.ai-message-assistant .ai-message-content {
  background: #F9FAFB;
  color: var(--text);
  border: 1px solid #E5E7EB;
  border-radius: 4px 16px 16px 16px;
  box-shadow: var(--shadow-xs);
  max-width: 90%;
}

/* Assistant avatar */
.ai-avatar-assistant {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 50%;
  background: #EEF2FF;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
  flex-shrink: 0;
}

/* Error message */
.ai-message-error .ai-message-content {
  background: var(--danger-50);
  border: 1px solid var(--danger-500);
  color: var(--danger-500);
  border-radius: var(--radius);
  padding: var(--space-md) var(--space-lg);
}

/* System message (memory limit prompt, compression notice) */
.ai-message-system {
  display: flex;
  justify-content: center;
  padding: var(--space-sm) 0;
}
.ai-message-system .ai-message-content {
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
  color: var(--text);
  border-radius: var(--radius);
  padding: var(--space-md) var(--space-lg);
  font-size: 13px;
  text-align: center;
}

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

/* Paragraph spacing in bubbles */
.ai-message-bubble p {
  margin: 0 0 var(--space-sm);
}

.ai-message-bubble p:last-child {
  margin-bottom: 0;
}

/* Code in messages */
.ai-message-bubble code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: rgba(0, 0, 0, 0.06);
  padding: 1px 5px;
  border-radius: 4px;
}

.ai-message.user .ai-message-bubble code {
  background: rgba(255, 255, 255, 0.18);
}

.ai-message-bubble pre {
  margin: var(--space-sm) 0;
  padding: var(--space-md);
  background: #1E293B;
  color: #CDD6F4;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: var(--text-xs);
  line-height: 1.6;
}

.ai-message.user .ai-message-bubble pre {
  background: rgba(0, 0, 0, 0.15);
  color: rgba(255, 255, 255, 0.9);
}

/* Message timestamp */
.ai-message-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
  padding: 0 var(--space-xs);
}

.ai-message.user .ai-message-time {
  text-align: right;
}

/* ---------- AI Markdown Rendering ---------- */
.ai-markdown {
  line-height: 1.7;
  font-size: 13.5px;
  color: var(--text);
}

.ai-markdown p {
  margin: 0 0 0.6em 0;
}

.ai-markdown p:last-child {
  margin-bottom: 0;
}

.ai-markdown strong {
  font-weight: 600;
  color: var(--text);
}

.ai-markdown ul,
.ai-markdown ol {
  margin: 0.4em 0 0.6em 0;
  padding-left: 1.4em;
  list-style: revert;
}

.ai-markdown li {
  margin-bottom: 0.3em;
  line-height: 1.6;
}

.ai-markdown li::marker {
  color: #6366F1;
}

.ai-markdown code {
  background: rgba(99, 102, 241, 0.08);
  color: var(--primary-600);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.88em;
  font-family: var(--font-mono);
}

.ai-markdown pre {
  background: #1E293B;
  color: #CDD6F4;
  border-radius: var(--radius);
  padding: var(--space-md) var(--space-lg);
  margin: 0.6em 0;
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.5;
}

.ai-markdown pre code {
  background: none;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

.ai-markdown hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.8em 0;
}

.ai-markdown h1,
.ai-markdown h2,
.ai-markdown h3 {
  font-weight: 600;
  margin: 0.8em 0 0.3em 0;
  color: var(--text);
  letter-spacing: -0.01em;
}

.ai-markdown h1 { font-size: 1.15em; }
.ai-markdown h2 { font-size: 1.05em; }
.ai-markdown h3 { font-size: 1em; }

.ai-markdown table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 12.5px;
}

.ai-markdown th,
.ai-markdown td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.ai-markdown th {
  background: #F9FAFB;
  font-weight: 600;
}

.ai-markdown blockquote {
  border-left: 3px solid #6366F1;
  margin: 0.5em 0;
  padding: 0.4em 0 0.4em var(--space-md);
  color: var(--text-secondary);
  background: rgba(99, 102, 241, 0.03);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* ---------- Typing Indicator (bouncing dots) ---------- */
.ai-typing {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: var(--space-md) var(--space-lg);
  background: #F9FAFB;
  border: 1px solid #E5E7EB;
  border-radius: var(--radius-lg);
  border-bottom-left-radius: var(--radius-sm);
  align-self: flex-start;
  box-shadow: var(--shadow-xs);
}

.ai-typing-dot {
  width: 6px;
  height: 6px;
  background: #818CF8;
  border-radius: var(--radius-full);
  animation: typing-bounce 1.4s infinite ease-in-out;
}

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

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.35;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* ---------- Image Attachment Icon (輸入框左下) ---------- */
.ai-image-icon-btn {
  cursor: pointer;
}

/* ---------- Image Preview (顯示於輸入框上方) ---------- */
.ai-image-preview {
  padding: 0 0 var(--space-sm);
  position: relative;
  display: inline-block;
}

.ai-image-preview img {
  max-width: 100%;
  max-height: 120px;
  border-radius: 8px;
  border: 1px solid var(--border);
  object-fit: contain;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.ai-remove-image {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--danger-500);
  color: #FFFFFF;
  border: 2px solid white;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all 150ms ease;
  font-size: 12px;
}

.ai-remove-image:hover {
  transform: scale(1.15);
  box-shadow: 0 2px 6px rgba(239, 68, 68, 0.4);
}

.ai-remove-image:focus-visible {
  box-shadow: var(--focus-ring);
}

.ai-remove-image:active {
  transform: scale(1);
}

/* Message image attachment */
.ai-message-image {
  max-width: 100%;
  max-height: 200px;
  border-radius: 8px;
  margin-bottom: var(--space-sm);
  border: 1px solid var(--border-light);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

/* User-pasted image in chat */
.ai-user-image {
  max-width: 200px;
  max-height: 160px;
  border-radius: 8px;
  margin-bottom: var(--space-sm);
  border: 1px solid rgba(255, 255, 255, 0.2);
  display: block;
}

/* ---------- Input Area (bottom-fixed) ---------- */
.ai-input-area {
  padding: var(--space-md) var(--space-lg) var(--space-lg);
  border-top: 1px solid #E5E7EB;
  flex-shrink: 0;
  background: white;
}

#ai-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.ai-input-wrap {
  position: relative;
}

#ai-input {
  width: 100%;
  min-height: 44px;
  max-height: 140px;
  padding: var(--space-md);
  font-size: var(--text-sm);
  resize: none;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: white;
  transition: border-color 150ms ease, box-shadow 150ms ease;
  line-height: var(--leading-relaxed);
}

#ai-input:hover {
  border-color: var(--gray-300);
}

#ai-input:focus {
  outline: none;
  border-color: #6366F1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

#ai-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.ai-input-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
}

.ai-input-actions-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* ---------- Fullscreen Mode ---------- */
#ai-panel.ai-fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw !important;
  max-width: 100vw !important;
  min-width: 100vw !important;
  height: 100vh;
  z-index: 1000;
  background: var(--surface, #fff);
}

#ai-panel.ai-fullscreen .ai-resize-handle {
  display: none;
}

#ai-panel.ai-fullscreen .ai-panel-inner {
  border-left: none;
  box-shadow: none;
}

#ai-panel.ai-fullscreen .ai-chat-messages {
  padding: var(--space-xl) 10vw;
}

#ai-panel.ai-fullscreen .ai-message-content {
  max-width: 100%;
}

#ai-panel.ai-fullscreen .ai-markdown table {
  font-size: 14px;
}

#ai-panel.ai-fullscreen .ai-input-area {
  padding: var(--space-md) 10vw var(--space-lg);
}

/* Fullscreen icon toggle — collapse hidden by default, swap on fullscreen */
#ai-fullscreen-btn .ai-fs-collapse { display: none; }
#ai-panel.ai-fullscreen #ai-fullscreen-btn .ai-fs-expand { display: none; }
#ai-panel.ai-fullscreen #ai-fullscreen-btn .ai-fs-collapse { display: inline-block; }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .ai-chat-messages {
    padding: var(--space-md);
  }

  .ai-message {
    max-width: 95%;
  }

  .ai-message-bubble,
  .ai-message-content {
    max-width: 95%;
  }

  .ai-input-area {
    padding: var(--space-sm) var(--space-md) var(--space-md);
  }
}
