/**
 * Edit Mode Styles
 * Sidebar, chat interface, and edit mode UI components
 * Dark theme inspired by modern chat interfaces
 */

/* CSS Variables for edit mode - Dark Theme */
:root {
  --edit-sidebar-width: clamp(320px, 25vw, 400px);
  
  /* Dark theme colors */
  --edit-bg-primary: #1a1a2e;
  --edit-bg-secondary: #16213e;
  --edit-bg-tertiary: #0f0f1a;
  --edit-bg-hover: #252545;
  --edit-bg-active: #2d2d5a;
  
  /* Text colors */
  --edit-text-primary: #e4e4e7;
  --edit-text-secondary: #a1a1aa;
  --edit-text-muted: #71717a;
  
  /* Accent colors */
  --edit-accent: #6366f1;
  --edit-accent-hover: #818cf8;
  --edit-accent-muted: rgba(99, 102, 241, 0.2);
  
  /* Border colors */
  --edit-border: #2d2d5a;
  --edit-border-light: #3d3d6a;
  
  /* Message colors */
  --edit-msg-user-bg: #3730a3;
  --edit-msg-assistant-bg: #252545;
  --edit-msg-code-bg: #0f0f1a;
}

/* Edit mode active state - shift content to make room for sidebar */
body.edit-mode-active {
  padding-right: var(--edit-sidebar-width);
  transition: padding-right 0.3s ease;
  overflow-x: hidden;
}

/* Sidebar fixed width and dark theme */
#edit-mode-sidebar {
  width: var(--edit-sidebar-width) !important;
  min-width: var(--edit-sidebar-width) !important;
  max-width: var(--edit-sidebar-width) !important;
  background-color: var(--edit-bg-primary) !important;
  color: var(--edit-text-primary) !important;
  border-left: 1px solid var(--edit-border) !important;
}

/* Mobile drawer behavior - treat as overlay drawer on touch devices */
@media (max-width: 768px), (hover: none) and (pointer: coarse) {
  :root {
    --edit-sidebar-width: 100vw;
  }
  
  /* Prevent double-tap zoom on edit mode elements */
  body.edit-mode-active {
    padding-right: 0;
    touch-action: manipulation;
  }
  
  /* Sidebar as drawer - can be hidden/shown */
  #edit-mode-sidebar {
    width: 100vw !important;
    min-width: 100vw !important;
    max-width: 100vw !important;
    transform: translateX(0);
    transition: transform 0.3s ease;
    /* iOS safe area support */
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  
  /* Hidden state for sidebar on mobile */
  #edit-mode-sidebar.sidebar-hidden {
    transform: translateX(100%) !important;
  }
  
  /* Show FAB toggle button on mobile even when sidebar is open */
  body.edit-mode-active #edit-mode-toggle {
    display: flex !important;
    z-index: 70 !important;
    bottom: calc(24px + env(safe-area-inset-bottom, 0)) !important;
  }
  
  /* Overlay behind sidebar for tap-to-close */
  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 55;
    opacity: 1;
    transition: opacity 0.3s ease;
  }
  
  .sidebar-overlay.hidden {
    opacity: 0;
    pointer-events: none;
  }
  
  /* Close button for mobile sidebar */
  .mobile-close-btn {
    display: flex !important;
    position: absolute;
    top: 12px;
    left: 12px;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    background-color: var(--edit-bg-hover);
    border: 1px solid var(--edit-border);
    border-radius: 8px;
    color: var(--edit-text-primary);
    cursor: pointer;
    z-index: 10;
  }
  
  .mobile-close-btn:hover {
    background-color: var(--edit-bg-active);
  }
  
  /* Adjust header padding for close button */
  #edit-mode-sidebar .edit-sidebar-header {
    padding-left: 56px;
  }
  
  /* Prevent zoom on input focus */
  #edit-mode-sidebar input,
  #edit-mode-sidebar textarea,
  #edit-mode-sidebar select {
    font-size: 16px !important;
  }
}

/* Hide mobile-only elements on desktop */
@media (min-width: 769px) and (hover: hover) {
  .mobile-close-btn {
    display: none !important;
  }
  
  .sidebar-overlay {
    display: none !important;
  }
}

/* Edit button */
#edit-mode-toggle {
  position: fixed !important;
  bottom: 24px !important;
  right: 24px !important;
  z-index: 50 !important;
  background-color: var(--edit-accent);
  color: white;
  border-radius: 9999px;
  padding: 16px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  transition: all 0.3s ease;
  cursor: pointer;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

#edit-mode-toggle:hover {
  transform: scale(1.1);
  background-color: var(--edit-accent-hover);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

/* Sidebar shadow */
#edit-mode-sidebar {
  box-shadow: -4px 0 20px rgba(0, 0, 0, 0.4);
}

/* Sidebar header - dark gradient */
.edit-sidebar-header {
  background: linear-gradient(135deg, var(--edit-bg-secondary) 0%, var(--edit-bg-tertiary) 100%) !important;
  border-bottom: 1px solid var(--edit-border);
}

.edit-sidebar-header h2,
.edit-sidebar-header span {
  color: var(--edit-text-primary) !important;
}

.edit-sidebar-header button {
  color: var(--edit-text-secondary) !important;
}

.edit-sidebar-header button:hover {
  color: var(--edit-text-primary) !important;
  background-color: var(--edit-bg-hover) !important;
}

/* Header hamburger menu */
.header-menu-container {
  position: relative;
}

.header-menu-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  min-width: 180px;
  background-color: var(--edit-bg-secondary);
  border: 1px solid var(--edit-border);
  border-radius: 0.5rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 100;
  overflow: hidden;
}

.header-menu-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.625rem 1rem;
  background: transparent;
  border: none;
  color: var(--edit-text-secondary);
  font-size: 0.8125rem;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
}

.header-menu-item:hover:not(:disabled) {
  background-color: var(--edit-bg-hover);
  color: var(--edit-text-primary);
}

.header-menu-item:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.header-menu-item i {
  color: var(--edit-accent);
  font-size: 0.75rem;
}

.header-menu-item span:last-child {
  margin-left: auto;
  color: var(--edit-text-muted);
}

.header-menu-divider {
  height: 1px;
  background-color: var(--edit-border);
  margin: 0.25rem 0;
}

/* Section dropdown - dark theme, in-flow (not overlay) */
.section-dropdown-container {
  position: relative;
}

.section-dropdown-btn {
  background-color: var(--edit-bg-secondary) !important;
  color: var(--edit-text-primary) !important;
  border: 1px solid var(--edit-border) !important;
  cursor: pointer;
  font-size: 0.875rem;
}

.section-dropdown-btn:hover {
  background-color: var(--edit-bg-hover) !important;
  border-color: var(--edit-border-light) !important;
}

.section-dropdown-btn i {
  color: var(--edit-accent-hover);
}

#dropdown-chevron {
  transition: transform 0.2s ease;
}

#dropdown-chevron.rotate-180 {
  transform: rotate(180deg);
}

/* In-flow dropdown menu (not absolute) to avoid z-index issues */
.section-dropdown-menu {
  background-color: var(--edit-bg-tertiary) !important;
  border: 1px solid var(--edit-border) !important;
  border-radius: 0.5rem;
  margin-top: 0.5rem;
  max-height: 240px;
  overflow-y: auto;
}

.section-option {
  color: var(--edit-text-secondary) !important;
  background-color: transparent !important;
  cursor: pointer;
  font-size: 0.8125rem;
  border: none;
}

.section-option:hover {
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-primary) !important;
}

.section-option:first-child {
  border-radius: 0.5rem 0.5rem 0 0;
}

.section-option:last-child {
  border-radius: 0 0 0.5rem 0.5rem;
}

.section-option.active {
  background-color: var(--edit-accent-muted) !important;
  color: var(--edit-accent-hover) !important;
}

.section-option i {
  width: 16px;
  text-align: center;
  color: var(--edit-accent);
}

.section-option:hover i,
.section-option.active i {
  color: var(--edit-accent-hover);
}

/* Compact section dropdown container */
.edit-sidebar-sections {
  background-color: var(--edit-bg-secondary);
  border-bottom: 1px solid var(--edit-border);
}

/* Section header in chat area */
#chat-section-header {
  color: var(--edit-text-primary) !important;
  background-color: var(--edit-bg-secondary) !important;
  border-bottom: 1px solid var(--edit-border) !important;
}

#chat-section-header h3 {
  color: var(--edit-text-primary) !important;
}

#chat-section-header p {
  color: var(--edit-text-muted) !important;
}

/* Chat messages - dark theme */
#chat-messages {
  background: var(--edit-bg-primary) !important;
}

.chat-message {
  animation: slideIn 0.3s ease;
}

/* User messages */
.chat-message .bg-blue-100 {
  background-color: var(--edit-msg-user-bg) !important;
  color: var(--edit-text-primary) !important;
  border-radius: 12px 12px 4px 12px;
}

/* Assistant messages */
.chat-message .bg-gray-100 {
  background-color: var(--edit-msg-assistant-bg) !important;
  color: var(--edit-text-primary) !important;
  border-radius: 12px 12px 12px 4px;
  border: 1px solid var(--edit-border);
}

/* Message timestamps and metadata */
.chat-message .text-gray-500,
.chat-message .text-xs {
  color: var(--edit-text-muted) !important;
}

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

/* Starter prompts - dark theme */
.starter-prompt {
  transition: all 0.2s ease;
  cursor: pointer;
  background-color: var(--edit-bg-secondary) !important;
  color: var(--edit-text-secondary) !important;
  border: 1px solid var(--edit-border) !important;
}

.starter-prompt:hover {
  transform: translateY(-1px);
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-primary) !important;
  border-color: var(--edit-accent) !important;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.2);
}

/* Starter prompts container */
#chat-starter-prompts {
  background-color: var(--edit-bg-primary) !important;
}

/* Chat input area - dark theme */
#chat-input-container,
.chat-input-area {
  background-color: var(--edit-bg-secondary) !important;
  border-top: 1px solid var(--edit-border) !important;
}

#chat-input {
  transition: all 0.2s ease;
  background-color: var(--edit-bg-tertiary) !important;
  color: var(--edit-text-primary) !important;
  border: 1px solid var(--edit-border) !important;
}

#chat-input::placeholder {
  color: var(--edit-text-muted) !important;
}

#chat-input:focus {
  border-color: var(--edit-accent) !important;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2) !important;
  outline: none;
}

#chat-send {
  transition: all 0.2s ease;
  background-color: var(--edit-accent) !important;
  color: white !important;
}

#chat-send:hover:not(:disabled) {
  transform: scale(1.05);
  background-color: var(--edit-accent-hover) !important;
}

#chat-send:disabled {
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-muted) !important;
}

/* Loading spinner */
.animate-spin {
  animation: spin 1s linear infinite;
}

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

/* Modals */
#api-key-modal,
#preview-modal,
#commit-modal {
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

#api-key-modal > div,
#preview-modal > div,
#commit-modal > div {
  animation: scaleIn 0.2s ease;
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* Notifications */
.notification {
  animation: slideInRight 0.3s ease;
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* Section highlight when edited */
section.editing-highlight {
  position: relative;
}

section.editing-highlight::after {
  content: '';
  position: absolute;
  inset: -4px;
  border: 2px solid #2563eb;
  border-radius: 8px;
  pointer-events: none;
  animation: pulse 2s ease-in-out infinite;
}

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

/* Custom scrollbar for sidebar - dark theme */
#edit-mode-sidebar::-webkit-scrollbar,
#edit-mode-sidebar *::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

#edit-mode-sidebar::-webkit-scrollbar-track,
#edit-mode-sidebar *::-webkit-scrollbar-track {
  background: var(--edit-bg-tertiary);
  border-radius: 3px;
}

#edit-mode-sidebar::-webkit-scrollbar-thumb,
#edit-mode-sidebar *::-webkit-scrollbar-thumb {
  background: var(--edit-border-light);
  border-radius: 3px;
}

#edit-mode-sidebar::-webkit-scrollbar-thumb:hover,
#edit-mode-sidebar *::-webkit-scrollbar-thumb:hover {
  background: var(--edit-accent);
}

/* Firefox scrollbar support */
#edit-mode-sidebar,
#edit-mode-sidebar * {
  scrollbar-width: thin;
  scrollbar-color: var(--edit-border-light) var(--edit-bg-tertiary);
}

/* Media upload dropzone - dark theme */
.media-dropzone {
  border: 2px dashed var(--edit-border-light);
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  transition: all 0.2s ease;
  cursor: pointer;
  background-color: var(--edit-bg-secondary);
  color: var(--edit-text-secondary);
}

.media-dropzone:hover,
.media-dropzone.dragover {
  border-color: var(--edit-accent);
  background-color: var(--edit-bg-hover);
  color: var(--edit-text-primary);
}

.media-dropzone.dragover {
  transform: scale(1.02);
}

/* Action buttons in chat messages - dark theme */
.insert-code-btn,
.chat-message button {
  background-color: var(--edit-accent) !important;
  color: white !important;
  border: none !important;
  transition: all 0.2s ease;
}

.insert-code-btn:hover,
.chat-message button:hover {
  background-color: var(--edit-accent-hover) !important;
  transform: translateY(-1px);
}

/* Re-run button */
.rerun-btn {
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-secondary) !important;
  border: 1px solid var(--edit-border) !important;
}

.rerun-btn:hover {
  background-color: var(--edit-bg-active) !important;
  color: var(--edit-text-primary) !important;
}

/* Applied indicator */
.applied-indicator {
  color: #10b981 !important;
}

/* Quick actions area */
.quick-actions,
#quick-actions {
  background-color: var(--edit-bg-secondary) !important;
  border-top: 1px solid var(--edit-border) !important;
}

.quick-actions button,
#quick-actions button {
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-secondary) !important;
  border: 1px solid var(--edit-border) !important;
}

.quick-actions button:hover,
#quick-actions button:hover {
  background-color: var(--edit-bg-active) !important;
  color: var(--edit-text-primary) !important;
  border-color: var(--edit-accent) !important;
}

/* Commit and preview buttons */
#commit-changes,
#preview-changes {
  background-color: var(--edit-accent) !important;
  color: white !important;
}

#commit-changes:hover,
#preview-changes:hover {
  background-color: var(--edit-accent-hover) !important;
}

#commit-changes:disabled,
#preview-changes:disabled {
  background-color: var(--edit-bg-hover) !important;
  color: var(--edit-text-muted) !important;
}

/* Preview diff styling */
#preview-modal pre {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
  line-height: 1.5;
}

/* Commit form styling */
#commit-modal input[type="text"],
#commit-modal input[type="password"] {
  transition: all 0.2s ease;
}

#commit-modal input[type="text"]:focus,
#commit-modal input[type="password"]:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
  outline: none;
}

/* Responsive adjustments */
@media (max-width: 640px) {
  #edit-mode-sidebar {
    width: 100vw !important;
  }
  
  .edit-sidebar-sections {
    max-height: 150px;
  }
  
  #chat-starter-prompts {
    padding: 8px;
  }
  
  .starter-prompt {
    font-size: 12px;
    padding: 4px 8px;
  }
}

/* Markdown content styling in chat messages - dark theme */
.markdown-content {
  word-wrap: break-word;
  overflow-wrap: break-word;
  color: var(--edit-text-primary);
}

.markdown-content p {
  margin-bottom: 0.5rem;
}

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

.markdown-content ul,
.markdown-content ol {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
}

.markdown-content li {
  margin-bottom: 0.25rem;
}

.markdown-content code {
  background-color: var(--edit-msg-code-bg);
  padding: 0.125rem 0.375rem;
  border-radius: 0.25rem;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.85em;
  color: var(--edit-accent-hover);
}

.markdown-content pre {
  background-color: var(--edit-msg-code-bg);
  color: var(--edit-text-primary);
  padding: 0.75rem;
  border-radius: 0.375rem;
  overflow-x: auto;
  margin: 0.5rem 0;
  border: 1px solid var(--edit-border);
}

.markdown-content pre code {
  background-color: transparent;
  padding: 0;
  color: inherit;
}

.markdown-content a {
  color: var(--edit-accent-hover);
  text-decoration: underline;
}

.markdown-content a:hover {
  color: var(--edit-accent);
}

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

.markdown-content em {
  font-style: italic;
}

.markdown-content blockquote {
  border-left: 3px solid var(--edit-accent);
  padding-left: 0.75rem;
  margin: 0.5rem 0;
  color: var(--edit-text-secondary);
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4 {
  font-weight: 600;
  margin-top: 0.75rem;
  margin-bottom: 0.5rem;
  color: var(--edit-text-primary);
}

.markdown-content h1 { font-size: 1.25rem; }
.markdown-content h2 { font-size: 1.125rem; }
.markdown-content h3 { font-size: 1rem; }
.markdown-content h4 { font-size: 0.875rem; }

/* Section Accordion Styles */
.sections-accordion {
  flex: 1;
  overflow-y: auto;
  background-color: var(--edit-bg-primary);
}

.section-accordion-item {
  border-bottom: 1px solid var(--edit-border);
}

.section-accordion-header {
  background-color: var(--edit-bg-secondary) !important;
  color: var(--edit-text-primary) !important;
  border: none !important;
  cursor: pointer;
  transition: all 0.15s ease;
}

.section-accordion-header:hover {
  background-color: var(--edit-bg-hover) !important;
}

.section-accordion-header.active {
  background-color: var(--edit-accent-muted) !important;
  border-left: 3px solid var(--edit-accent) !important;
}

.section-accordion-header i.text-gray-500 {
  color: var(--edit-accent) !important;
}

.section-accordion-header span.text-gray-700 {
  color: var(--edit-text-primary) !important;
}

.section-accordion-header .section-chevron {
  color: var(--edit-text-muted) !important;
  transition: transform 0.2s ease;
}

.section-accordion-header .section-chevron.rotate-90 {
  transform: rotate(90deg);
}

.section-accordion-content {
  background-color: var(--edit-bg-tertiary);
  border-top: 1px solid var(--edit-border);
  max-height: 400px;
  overflow-y: auto;
}

.section-chat-area {
  background-color: var(--edit-bg-tertiary);
}

.section-messages {
  min-height: 60px;
}

.section-messages .text-gray-400 {
  color: var(--edit-text-muted) !important;
}

/* Section chat input styling */
.section-chat-input {
  background-color: var(--edit-bg-primary) !important;
  color: var(--edit-text-primary) !important;
  border: 1px solid var(--edit-border) !important;
  font-size: 0.8125rem;
}

.section-chat-input::placeholder {
  color: var(--edit-text-muted) !important;
}

.section-chat-input:focus {
  border-color: var(--edit-accent) !important;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2) !important;
  outline: none;
}

.section-chat-send {
  background-color: var(--edit-accent) !important;
  color: white !important;
  transition: all 0.15s ease;
}

.section-chat-send:hover {
  background-color: var(--edit-accent-hover) !important;
  transform: scale(1.05);
}

/* Section quick actions */
.section-quick-actions {
  background-color: var(--edit-bg-tertiary);
  border-top: 1px solid var(--edit-border);
}

/* Message styling in accordion sections */
.section-messages .bg-blue-50 {
  background-color: var(--edit-msg-user-bg) !important;
}

.section-messages .bg-gray-50 {
  background-color: var(--edit-msg-assistant-bg) !important;
}

.section-messages .text-gray-700 {
  color: var(--edit-text-primary) !important;
}

.section-messages .bg-blue-600 {
  background-color: var(--edit-accent) !important;
}

.section-messages .bg-blue-100 {
  background-color: var(--edit-accent-muted) !important;
}

.section-messages .text-blue-600 {
  color: var(--edit-accent-hover) !important;
}

/* Final actions area */
.edit-sidebar-final-actions {
  background-color: var(--edit-bg-secondary) !important;
  border-top: 1px solid var(--edit-border) !important;
}

/* Dark mode support (if needed in future) */
@media (prefers-color-scheme: dark) {
  /* Can add dark mode styles here */
}

/* Print styles - hide edit mode elements */
@media print {
  #edit-mode-toggle,
  #edit-mode-sidebar,
  #api-key-modal,
  #preview-modal,
  #commit-modal {
    display: none !important;
  }
  
  body.edit-mode-active {
    margin-right: 0 !important;
  }
}
