/**
 * AI Sidebar Plugin Styles
 * Standalone sliding panel with toggle button
 */

/* ============================================
   CSS Variables
   ============================================ */
:root {
  --ai-sidebar-width: 380px;
  --ai-sidebar-bg: #1a1a2e;
  --ai-sidebar-header-bg: #16213e;
  --ai-sidebar-border: #0f3460;
  --ai-sidebar-text: #e8e8e8;
  --ai-sidebar-text-muted: #8892a0;
  --ai-sidebar-accent: #4a9eff;
  --ai-sidebar-error: #ff6b6b;
  --ai-sidebar-transition: 300ms ease;
  --ai-sidebar-z-index: 9999;
}

/* ============================================
   Toggle Button (Tab on right edge)
   Compact, draggable, shrinks when not hovered
   ============================================ */
#ai-sidebar-toggle {
  position: fixed;
  right: 0;
  bottom: 60px;
  z-index: calc(var(--ai-sidebar-z-index) + 1);

  /* Compact size - shrinks when not hovered */
  width: 28px;
  height: 36px;
  padding: 0;
  margin: 0;

  background: var(--ai-sidebar-header-bg);
  border: 1px solid var(--ai-sidebar-border);
  border-right: none;
  border-radius: 6px 0 0 6px;

  color: var(--ai-sidebar-text);
  cursor: grab;
  opacity: 0.6;

  display: flex;
  align-items: center;
  justify-content: center;

  /* Use transform-origin for center expansion */
  transform-origin: right center;
  transition: all 200ms ease;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.3);
}

#ai-sidebar-toggle:hover {
  background: var(--ai-sidebar-border);
  transform: scale(1.3);
  opacity: 1;
  cursor: pointer;
}

#ai-sidebar-toggle.dragging {
  cursor: grabbing;
  opacity: 0.9;
  transition: none; /* Disable transitions while dragging */
}

#ai-sidebar-toggle .icon-ai {
  display: block;
  width: 16px;
  height: 16px;
}

#ai-sidebar-toggle .icon-close {
  display: none;
  width: 16px;
  height: 16px;
}

#ai-sidebar-toggle.open {
  right: var(--ai-sidebar-width);
}

#ai-sidebar-toggle.open .icon-ai {
  display: none;
}

#ai-sidebar-toggle.open .icon-close {
  display: block;
}

/* ============================================
   Sidebar Panel
   ============================================ */
#ai-sidebar-panel {
  position: fixed;
  top: 0;
  right: calc(-1 * var(--ai-sidebar-width));
  width: var(--ai-sidebar-width);
  height: 100vh;
  z-index: var(--ai-sidebar-z-index);

  background: var(--ai-sidebar-bg);
  border-left: 1px solid var(--ai-sidebar-border);

  display: flex;
  flex-direction: column;

  transition: right var(--ai-sidebar-transition);
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.4);
}

#ai-sidebar-panel.open {
  right: 0;
}

/* Disable transitions during resize for smooth dragging */
#ai-sidebar-panel.resizing {
  transition: none;
}

/* ============================================
   Resize Handle
   ============================================ */
#ai-sidebar-resize-handle {
  position: absolute;
  top: 0;
  left: -6px;
  width: 12px;
  height: 100%;
  cursor: ew-resize;
  z-index: 10;

  /* Subtle visible background */
  background: linear-gradient(to right, transparent, rgba(74, 158, 255, 0.1), transparent);
  transition: background 150ms ease;
}

#ai-sidebar-resize-handle:hover,
#ai-sidebar-resize-handle.active {
  background: linear-gradient(to right, transparent, rgba(74, 158, 255, 0.4), transparent);
}

#ai-sidebar-resize-handle.active {
  background: linear-gradient(to right, transparent, rgba(74, 158, 255, 0.6), transparent);
}

/* Visual indicator line - always visible */
#ai-sidebar-resize-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 3px;
  height: 50px;
  background: rgba(74, 158, 255, 0.3);
  border-radius: 2px;
  transition: all 150ms ease;
}

#ai-sidebar-resize-handle:hover::after,
#ai-sidebar-resize-handle.active::after {
  background: var(--ai-sidebar-accent);
  height: 80px;
  box-shadow: 0 0 8px rgba(74, 158, 255, 0.5);
}

/* ============================================
   Header
   ============================================ */
.ai-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;

  background: var(--ai-sidebar-header-bg);
  border-bottom: 1px solid var(--ai-sidebar-border);

  flex-shrink: 0;
}

.ai-sidebar-title {
  display: flex;
  align-items: center;
  gap: 10px;

  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  font-weight: 600;
  color: var(--ai-sidebar-text);
}

.ai-sidebar-title svg {
  color: var(--ai-sidebar-accent);
}

.ai-sidebar-ticket-id {
  font-size: 13px;
  font-weight: 500;
  color: var(--ai-sidebar-accent);
  background: rgba(99, 102, 241, 0.1);
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 4px;
}

.ai-sidebar-ticket-id:empty {
  display: none;
}

.ai-sidebar-close {
  width: 32px;
  height: 32px;
  padding: 0;
  margin: 0;

  background: transparent;
  border: none;
  border-radius: 6px;

  color: var(--ai-sidebar-text-muted);
  cursor: pointer;

  display: flex;
  align-items: center;
  justify-content: center;

  transition: all 150ms ease;
}

.ai-sidebar-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ai-sidebar-text);
}

/* ============================================
   Content Area
   ============================================ */
.ai-sidebar-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ============================================
   Iframe
   ============================================ */
#ai-sidebar-iframe {
  flex: 1;
  width: 100%;
  height: 100%;
  border: none;
  background: var(--ai-sidebar-bg);
  color-scheme: dark;
}

/* Disable iframe pointer events during resize to prevent drag interruption */
#ai-sidebar-panel.resizing #ai-sidebar-iframe,
#ai-sidebar-panel.resizing .ai-sidebar-content {
  pointer-events: none;
}

/* ============================================
   Placeholder State
   ============================================ */
.ai-sidebar-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;
  text-align: center;

  color: var(--ai-sidebar-text-muted);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

/* ============================================
   Loading State
   ============================================ */
.ai-sidebar-loading {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;

  color: var(--ai-sidebar-text-muted);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

.ai-sidebar-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--ai-sidebar-border);
  border-top-color: var(--ai-sidebar-accent);
  border-radius: 50%;
  animation: ai-sidebar-spin 0.8s linear infinite;
}

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

/* ============================================
   Error State
   ============================================ */
.ai-sidebar-error {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 32px;
  text-align: center;

  color: var(--ai-sidebar-error);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

.ai-sidebar-error-hint {
  color: var(--ai-sidebar-text-muted);
  font-size: 12px;
  margin-top: 8px;
}

.ai-sidebar-error code {
  display: block;
  margin-top: 4px;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 4px;
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  font-size: 11px;
  color: var(--ai-sidebar-text);
}

.ai-sidebar-retry {
  margin-top: 12px;
  padding: 8px 20px;

  background: var(--ai-sidebar-accent);
  border: none;
  border-radius: 6px;

  color: white;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;

  transition: all 150ms ease;
}

.ai-sidebar-retry:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

/* ============================================
   Responsive Adjustments
   ============================================ */
@media (max-width: 1200px) {
  :root {
    --ai-sidebar-width: 340px;
  }
}

@media (max-width: 768px) {
  :root {
    --ai-sidebar-width: 100%;
  }

  #ai-sidebar-toggle.open {
    right: calc(100% - 40px);
  }

  /* Hide resize handle on mobile - full width mode */
  #ai-sidebar-resize-handle {
    display: none;
  }
}

/* ============================================
   Left Edge Toggle Button Support
   ============================================ */
#ai-sidebar-toggle.edge-left {
  left: 0;
  right: auto;
  border-radius: 0 6px 6px 0;
  border-left: none;
  border-right: 1px solid var(--ai-sidebar-border);
  transform-origin: left center;
}

#ai-sidebar-toggle.edge-left .icon-close {
  transform: scaleX(-1);
}

#ai-sidebar-toggle.edge-left.open {
  left: var(--ai-sidebar-width);
  right: auto;
}

/* Snap preview during drag */
#ai-sidebar-toggle.snap-preview-left {
  border-radius: 0 6px 6px 0;
  border-left: none;
  border-right: 1px solid var(--ai-sidebar-border);
}

#ai-sidebar-toggle.snap-preview-right {
  border-radius: 6px 0 0 6px;
  border-right: none;
  border-left: 1px solid var(--ai-sidebar-border);
}

/* ============================================
   Left Edge Sidebar Support
   ============================================ */
#ai-sidebar-panel.edge-left {
  left: calc(-1 * var(--ai-sidebar-width));
  right: auto;
  border-left: none;
  border-right: 1px solid var(--ai-sidebar-border);
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.4);
  transition: left var(--ai-sidebar-transition);
}

#ai-sidebar-panel.edge-left.open {
  left: 0;
  right: auto;
}

#ai-sidebar-panel.edge-left #ai-sidebar-resize-handle {
  left: auto;
  right: -6px;
}

/* ============================================
   Header Controls (including popout button)
   ============================================ */
.ai-sidebar-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.ai-sidebar-popout {
  width: 32px;
  height: 32px;
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--ai-sidebar-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms ease;
}

.ai-sidebar-popout:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ai-sidebar-accent);
}

/* ============================================
   Floating Window
   ============================================ */
.ai-float-window {
  position: fixed;
  z-index: calc(var(--ai-sidebar-z-index) + 10);
  background: var(--ai-sidebar-bg);
  border: 1px solid var(--ai-sidebar-border);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: ai-float-pop-in 250ms ease-out;
}

@keyframes ai-float-pop-in {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.ai-float-window.dragging {
  user-select: none;
  transition: none;
}

.ai-float-window.resizing {
  user-select: none;
  transition: none;
}

/* Fullscreen mode */
.ai-float-window.fullscreen {
  top: 0 !important;
  left: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  border-radius: 0;
  animation: ai-float-fullscreen-in 250ms ease-out;
}

@keyframes ai-float-fullscreen-in {
  from {
    opacity: 0.8;
  }
  to {
    opacity: 1;
  }
}

/* ============================================
   Floating Window Title Bar
   ============================================ */
.ai-float-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--ai-sidebar-header-bg);
  border-bottom: 1px solid var(--ai-sidebar-border);
  cursor: grab;
  flex-shrink: 0;
  user-select: none;
}

.ai-float-titlebar:active {
  cursor: grabbing;
}

.ai-float-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 13px;
  font-weight: 600;
  color: var(--ai-sidebar-text);
}

.ai-float-title svg {
  color: var(--ai-sidebar-accent);
}

.ai-float-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.ai-float-control {
  width: 28px;
  height: 28px;
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  border-radius: 5px;
  color: var(--ai-sidebar-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 150ms ease;
}

.ai-float-control:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ai-sidebar-text);
}

.ai-float-dock:hover {
  color: var(--ai-sidebar-error);
}

/* ============================================
   Floating Window Content
   ============================================ */
.ai-float-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.ai-float-content #ai-sidebar-iframe {
  flex: 1;
  width: 100%;
  height: 100%;
  border: none;
}

/* Disable iframe interactions during drag/resize */
.ai-float-window.dragging .ai-float-content,
.ai-float-window.resizing .ai-float-content {
  pointer-events: none;
}

/* ============================================
   Floating Window Resize Handles
   ============================================ */
.ai-float-resize {
  position: absolute;
  z-index: 10;
}

/* Edge handles */
.ai-float-resize-n {
  top: -4px;
  left: 10px;
  right: 10px;
  height: 8px;
  cursor: ns-resize;
}

.ai-float-resize-s {
  bottom: -4px;
  left: 10px;
  right: 10px;
  height: 8px;
  cursor: ns-resize;
}

.ai-float-resize-e {
  right: -4px;
  top: 10px;
  bottom: 10px;
  width: 8px;
  cursor: ew-resize;
}

.ai-float-resize-w {
  left: -4px;
  top: 10px;
  bottom: 10px;
  width: 8px;
  cursor: ew-resize;
}

/* Corner handles */
.ai-float-resize-ne {
  top: -4px;
  right: -4px;
  width: 16px;
  height: 16px;
  cursor: nesw-resize;
}

.ai-float-resize-nw {
  top: -4px;
  left: -4px;
  width: 16px;
  height: 16px;
  cursor: nwse-resize;
}

.ai-float-resize-se {
  bottom: -4px;
  right: -4px;
  width: 16px;
  height: 16px;
  cursor: nwse-resize;
}

.ai-float-resize-sw {
  bottom: -4px;
  left: -4px;
  width: 16px;
  height: 16px;
  cursor: nesw-resize;
}

/* Visual indicator on hover */
.ai-float-resize:hover {
  background: rgba(74, 158, 255, 0.2);
}

/* ============================================
   Minimized Bar
   ============================================ */
.ai-minimized-bar {
  position: fixed;
  bottom: 20px;
  z-index: calc(var(--ai-sidebar-z-index) + 10);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  background: var(--ai-sidebar-header-bg);
  border: 1px solid var(--ai-sidebar-border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  animation: ai-minimize-in 250ms ease-out;
}

.ai-minimized-bar.left {
  left: 20px;
  right: auto;
}

.ai-minimized-bar.right {
  right: 20px;
  left: auto;
}

@keyframes ai-minimize-in {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ai-minimized-restore {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: transparent;
  border: none;
  border-radius: 5px;
  color: var(--ai-sidebar-text);
  cursor: pointer;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 13px;
  font-weight: 500;
  transition: all 150ms ease;
}

.ai-minimized-restore:hover {
  background: rgba(255, 255, 255, 0.1);
}

.ai-minimized-icon {
  font-size: 16px;
}

.ai-minimized-title {
  white-space: nowrap;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-minimized-expand {
  width: 32px;
  height: 32px;
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  border-radius: 5px;
  color: var(--ai-sidebar-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 150ms ease;
}

.ai-minimized-expand:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ai-sidebar-text);
}

/* ============================================
   Body Classes for Mode States
   ============================================ */
body.ai-float-dragging,
body.ai-float-resizing {
  user-select: none;
  cursor: move;
}

body.ai-float-resizing {
  cursor: default;
}

/* Hide sidebar elements when in floating/minimized modes */
body.ai-mode-floating #ai-sidebar-panel,
body.ai-mode-floating #ai-sidebar-toggle,
body.ai-mode-minimized #ai-sidebar-panel,
body.ai-mode-minimized #ai-sidebar-toggle,
body.ai-mode-fullscreen #ai-sidebar-panel,
body.ai-mode-fullscreen #ai-sidebar-toggle {
  display: none !important;
}

/* Hide floating/minimized elements when docked */
body.ai-mode-docked .ai-float-window,
body.ai-mode-docked .ai-minimized-bar {
  display: none !important;
}

/* ============================================
   Transitions & Animations
   ============================================ */
@keyframes ai-float-dock-out {
  from {
    opacity: 1;
    transform: scale(1);
  }
  to {
    opacity: 0;
    transform: scale(0.9);
  }
}

/* Smooth transitions for sidebar edge changes */
#ai-sidebar-panel,
#ai-sidebar-toggle {
  will-change: transform, left, right;
}
