/* ═══════════════════════════════════════════════════════════════════════════
   SCREENPLAY AI PANEL
   Slide-in AI assistant sidebar for screenplay editor
   ═══════════════════════════════════════════════════════════════════════════ */

/* ─── Toggle Button (floating right edge) ─── */
.sp-ai-toggle-btn {
  position: fixed;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  z-index: 900;
  
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  
  background: linear-gradient(135deg, var(--color-primary), var(--color-dark-purple));
  color: white;
  border: none;
  border-radius: 12px 0 0 12px;
  padding: 12px 10px;
  
  box-shadow: 
    0 4px 16px rgba(90,24,154,0.25),
    inset 0 1px 0 rgba(255,255,255,0.2);
  
  cursor: pointer;
  transition: all 0.2s ease;
  
  font-size: 1.1rem;
  font-weight: 700;
}

.sp-ai-toggle-btn:hover {
  right: 2px;
  box-shadow: 
    0 6px 24px rgba(90,24,154,0.4),
    inset 0 1px 0 rgba(255,255,255,0.3);
}

.sp-ai-toggle-label {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.9;
}

body.dark .sp-ai-toggle-btn {
  background: linear-gradient(135deg, var(--color-primary), rgba(130,60,200,1));
}

/* ─── Panel Container ─── */
.sp-ai-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 420px;
  max-width: 100vw;
  z-index: 1000;
  
  background: var(--bg-glass-card, #ffffff);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-left: 1px solid var(--border-glass);
  box-shadow: -8px 0 32px rgba(0,0,0,0.12);
  
  display: flex;
  flex-direction: column;
  
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

.sp-ai-panel--open {
  transform: translateX(0);
}

body.dark .sp-ai-panel {
  background: rgba(20,15,35,0.97);
  border-left-color: rgba(90,24,154,0.4);
  box-shadow: -8px 0 32px rgba(0,0,0,0.6);
}

/* When panel is open, shrink toggle button slightly */
body.sp-ai-panel-active .sp-ai-toggle-btn {
  opacity: 0.5;
  pointer-events: none;
}

/* ─── Panel Header ─── */
.sp-ai-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-glass);
  background: rgba(90,24,154,0.03);
}

body.dark .sp-ai-panel-header {
  background: rgba(90,24,154,0.12);
}

.sp-ai-panel-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--color-primary);
}

body.dark .sp-ai-panel-title {
  color: var(--color-secondary);
}

.sp-ai-close-btn {
  background: none;
  border: none;
  color: var(--color-soft-gray);
  font-size: 1.15rem;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: all 0.15s ease;
}

.sp-ai-close-btn:hover {
  background: rgba(90,24,154,0.1);
  color: var(--color-primary);
}

body.dark .sp-ai-close-btn:hover {
  color: var(--color-secondary);
}

/* ─── Quick-action Chips ─── */
.sp-ai-quick-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border-glass);
}

.sp-ai-chip {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  background: var(--bg-glass-panel);
  border: 1px solid var(--border-glass);
  border-radius: 12px;
  color: var(--color-text);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
  white-space: nowrap;
}

.sp-ai-chip:hover {
  background: rgba(90,24,154,0.08);
  border-color: var(--color-primary);
  color: var(--color-primary);
  transform: translateY(-1px);
}

body.dark .sp-ai-chip:hover {
  border-color: var(--color-secondary);
  color: var(--color-secondary);
}

.sp-ai-chip i {
  opacity: 0.75;
  font-size: 0.85rem;
}

/* ─── Context Pill ─── */
.sp-ai-context-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  font-size: 0.76rem;
  color: var(--color-soft-gray);
  background: rgba(90,24,154,0.04);
  border-bottom: 1px solid var(--border-glass);
}

body.dark .sp-ai-context-pill {
  background: rgba(90,24,154,0.08);
}

.sp-ai-context-pill i {
  opacity: 0.6;
}

/* ─── Chat Messages Area ─── */
.sp-ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--bg-body);
}

.sp-ai-chat-messages::-webkit-scrollbar {
  width: 6px;
}

.sp-ai-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(90,24,154,0.2);
  border-radius: 8px;
}

body.dark .sp-ai-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(90,24,154,0.4);
}

/* ─── Chat Bubbles ─── */
.sp-ai-bubble {
  max-width: 90%;
  padding: 11px 15px;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.5;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.sp-ai-bubble p {
  margin: 0;
}

.sp-ai-bubble--user {
  align-self: flex-end;
  background: var(--color-primary);
  color: white;
  border-bottom-right-radius: 4px;
}

body.dark .sp-ai-bubble--user {
  background: rgba(90,24,154,0.85);
}

.sp-ai-bubble--ai {
  align-self: flex-start;
  background: var(--bg-glass-card);
  color: var(--color-text);
  border: 1px solid var(--border-glass);
  border-bottom-left-radius: 4px;
}

body.dark .sp-ai-bubble--ai {
  background: rgba(40,35,55,0.92);
}

.sp-ai-bubble--script {
  font-family: 'Courier Prime', 'Courier New', monospace;
  font-size: 0.84rem;
  white-space: pre-wrap;
  background: rgba(90,24,154,0.04);
  border-color: rgba(90,24,154,0.15);
}

body.dark .sp-ai-bubble--script {
  background: rgba(90,24,154,0.12);
  border-color: rgba(90,24,154,0.3);
}

.sp-ai-bubble--streaming {
  animation: sp-ai-pulse 1.5s ease-in-out infinite;
}

@keyframes sp-ai-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.85; }
}

/* ─── Typing Indicator ─── */
.sp-ai-typing {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  font-size: 0.8rem;
  color: var(--color-soft-gray);
  background: var(--bg-body);
  border-top: 1px solid var(--border-glass);
}

.sp-ai-typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-primary);
  animation: sp-ai-typing-bounce 1.4s infinite ease-in-out both;
}

.sp-ai-typing-dot:nth-child(1) { animation-delay: -0.32s; }
.sp-ai-typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes sp-ai-typing-bounce {
  0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* ─── Composer (input area) ─── */
.sp-ai-composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border-glass);
  background: var(--bg-glass-card);
}

body.dark .sp-ai-composer {
  background: rgba(20,15,35,0.97);
}

.sp-ai-chat-input {
  flex: 1;
  min-height: 38px;
  max-height: 140px;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  background: var(--bg-input);
  color: var(--color-text);
  font-size: 0.88rem;
  font-family: inherit;
  resize: none;
  outline: none;
  transition: border-color 0.15s ease;
}

.sp-ai-chat-input:focus {
  border-color: var(--color-primary);
}

body.dark .sp-ai-chat-input:focus {
  border-color: var(--color-secondary);
}

.sp-ai-send-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-primary);
  color: white;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: 0 2px 8px rgba(90,24,154,0.25);
}

.sp-ai-send-btn:hover:not(:disabled) {
  background: var(--color-dark-purple);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(90,24,154,0.35);
}

.sp-ai-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

body.dark .sp-ai-send-btn {
  background: rgba(90,24,154,0.9);
}

body.dark .sp-ai-send-btn:hover:not(:disabled) {
  background: var(--color-primary);
}

/* ─── Insert Bar (shows after AI generates script content) ─── */
.sp-ai-insert-bar {
  padding: 14px 20px;
  background: linear-gradient(135deg, rgba(90,24,154,0.08), rgba(130,60,200,0.04));
  border-top: 1px solid var(--border-glass);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

body.dark .sp-ai-insert-bar {
  background: linear-gradient(135deg, rgba(90,24,154,0.15), rgba(130,60,200,0.08));
}

.sp-ai-insert-bar-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text);
}

.sp-ai-insert-bar-actions {
  display: flex;
  gap: 8px;
}

.sp-ai-insert-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  background: var(--bg-glass-panel);
  border: 1px solid var(--border-glass);
  border-radius: 10px;
  color: var(--color-text);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.sp-ai-insert-btn:hover {
  background: rgba(90,24,154,0.08);
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.sp-ai-insert-btn--primary {
  background: var(--color-primary);
  color: white;
  border-color: transparent;
}

.sp-ai-insert-btn--primary:hover {
  background: var(--color-dark-purple);
  color: white;
}

.sp-ai-insert-btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--color-soft-gray);
}

.sp-ai-insert-btn--ghost:hover {
  background: rgba(0,0,0,0.03);
  color: var(--color-text);
}

body.dark .sp-ai-insert-btn--ghost:hover {
  background: rgba(255,255,255,0.05);
}

/* ─── AI Scene Icon — absolutely positioned, zero layout impact ─── */
/*
 * Floats just to the LEFT of the scene heading text start.
 * Uses position:absolute so it takes up NO space and never shifts
 * the screenplay text. .sp-scene-heading already has position:relative
 * via .sp-line in screenplay.css.
 *
 * The .sp-text for scene-heading has no left margin (padding-left:0),
 * so left:0 on the .sp-line is where "INT." begins.
 * We push the icon to left:-22px so it hovers just before the "I".
 */
.sp-ai-scene-icon {
  position: absolute;
  left: -22px;
  top: 50%;
  transform: translateY(-50%);

  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;

  background: none;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  user-select: none;
  pointer-events: none; /* controlled by hover state below */

  color: rgba(90,24,154,0.4);
  font-size: 0.65rem;

  border-radius: 3px;
  opacity: 0;
  transition: color 0.12s ease, opacity 0.12s ease, transform 0.12s ease;
  z-index: 10;
}

/* Fade in only when the scene heading row is hovered or focused */
.sp-scene-heading:hover .sp-ai-scene-icon,
.sp-scene-heading:focus-within .sp-ai-scene-icon {
  opacity: 1;
  pointer-events: auto;
}

.sp-ai-scene-icon:hover {
  color: var(--color-primary);
  transform: translateY(-50%) scale(1.25);
}

body.dark .sp-ai-scene-icon        { color: rgba(255,195,0,0.45); }
body.dark .sp-ai-scene-icon:hover  { color: var(--color-secondary); }

/* On tablet where paper padding is tighter, nudge in slightly */
@media (max-width: 900px) {
  .sp-ai-scene-icon { left: -18px; }
}

/* On very small screens hide it — scene numbers are hidden too */
@media (max-width: 480px) {
  .sp-ai-scene-icon { display: none; }
}

/* ─── Responsive Mobile ─── */
@media (max-width: 768px) {
  .sp-ai-panel {
    width: 100vw;
  }

  .sp-ai-toggle-btn {
    right: 12px;
    top: auto;
    bottom: 80px;
    transform: none;
    border-radius: 50%;
    width: 56px;
    height: 56px;
    flex-direction: row;
    box-shadow: 0 6px 20px rgba(90,24,154,0.35);
  }

  .sp-ai-toggle-label {
    display: none;
  }

  .sp-ai-quick-actions {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .sp-ai-chip {
    font-size: 0.7rem;
    padding: 8px 10px;
  }

  .sp-ai-inline-btn {
    max-width: calc(100% - 32px);
    font-size: 0.76rem;
  }
}
