/**
 * MWANGI Code Block Enhancements
 *
 * Beautiful, functional, accessible code blocks with:
 * - Line wrapping (no horizontal scroll on mobile)
 * - Stunning copy buttons with animations
 * - WCAG AA compliant
 * - Mobile-first responsive design
 * - Brand-consistent styling
 *
 * @version 1.0.0
 * @date December 2024
 */

/* ========================================================================
   CODE BLOCK LINE WRAPPING
   ======================================================================== */

/**
 * Override default overflow behavior to enable line wrapping
 * while maintaining code readability and indentation
 */
pre {
  overflow-x: visible !important;
  white-space: pre-wrap !important;
  word-wrap: break-word;
  word-break: break-word;

  /* Preserve indentation with proper tab sizing */
  tab-size: 4;
  -moz-tab-size: 4;
  -o-tab-size: 4;
}

pre code {
  white-space: pre-wrap !important;
  word-wrap: break-word;
  display: block;
}

/**
 * Inline code - wrap but don't break awkwardly
 */
code:not(pre code) {
  white-space: normal;
  word-break: break-word;
}

/**
 * Code block wrapper for positioning copy button
 */
.code-block-wrapper {
  position: relative;
  margin-bottom: var(--space-lg, 1.5rem);
}

/* ========================================================================
   ENHANCED COPY BUTTON DESIGN
   ======================================================================== */

/**
 * Copy button - Beautiful glassmorphic design with MWANGI brand colors
 */
.copy-code-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;

  /* Layout */
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;

  /* Typography */
  font-size: 0.75rem;
  font-family: var(--font-sans, 'Lexend', sans-serif);
  font-weight: 500;
  letter-spacing: 0.02em;

  /* Glassmorphic background with brand color */
  background: rgba(41, 181, 237, 0.1);
  border: 1px solid rgba(41, 181, 237, 0.3);
  border-radius: 8px;

  /* Visual effects */
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);

  /* Color */
  color: #29B5ED;

  /* Interaction */
  cursor: pointer;
  z-index: 10;

  /* Smooth transitions */
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/**
 * Hover state - Electric glow effect
 */
.copy-code-btn:hover {
  background: rgba(41, 181, 237, 0.2);
  border-color: #29B5ED;
  transform: translateY(-1px);
  box-shadow:
    0 4px 12px rgba(41, 181, 237, 0.2),
    0 0 20px rgba(41, 181, 237, 0.1);
}

/**
 * Active state - Pressed down
 */
.copy-code-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(41, 181, 237, 0.15);
}

/**
 * Focus state - Accessible keyboard navigation
 */
.copy-code-btn:focus {
  outline: 2px solid #29B5ED;
  outline-offset: 2px;
}

.copy-code-btn:focus:not(:focus-visible) {
  outline: none;
}

.copy-code-btn:focus-visible {
  outline: 2px solid #29B5ED;
  outline-offset: 2px;
}

/**
 * Copied success state - Green confirmation
 */
.copy-code-btn.copied {
  background: rgba(16, 185, 129, 0.2);
  border-color: #10B981;
  color: #10B981;
  animation: copySuccess 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.copy-code-btn.copied .copy-icon {
  animation: checkmarkPop 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/**
 * Error state - Red warning
 */
.copy-code-btn.copy-error {
  background: rgba(239, 68, 68, 0.2);
  border-color: #EF4444;
  color: #EF4444;
  animation: copyError 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/**
 * Copy button icon
 */
.copy-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.2s ease;
}

/**
 * Button text - Hidden on mobile by default
 */
.copy-btn-text {
  white-space: nowrap;
  transition: opacity 0.2s ease;
}

/**
 * Success checkmark icon
 */
.check-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ========================================================================
   ANIMATIONS
   ======================================================================== */

/**
 * Copy success animation - Scale pulse
 */
@keyframes copySuccess {
  0% {
    transform: scale(1) translateY(0);
  }
  50% {
    transform: scale(1.05) translateY(-1px);
  }
  100% {
    transform: scale(1) translateY(-1px);
  }
}

/**
 * Checkmark pop animation
 */
@keyframes checkmarkPop {
  0% {
    transform: scale(0.8) rotate(-10deg);
    opacity: 0;
  }
  50% {
    transform: scale(1.2) rotate(5deg);
  }
  100% {
    transform: scale(1) rotate(0deg);
    opacity: 1;
  }
}

/**
 * Copy error animation - Shake effect
 */
@keyframes copyError {
  0%, 100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

/**
 * Subtle fade-in on code block hover (optional enhancement)
 */
.code-block-wrapper .copy-code-btn {
  opacity: 0.85;
}

.code-block-wrapper:hover .copy-code-btn {
  opacity: 1;
}

/* ========================================================================
   CODE LANGUAGE LABEL (Enhanced)
   ======================================================================== */

/**
 * Language badge - Moves when copy button is present
 */
.code-language-label {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;

  /* Layout */
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.625rem;

  /* Typography */
  font-size: 0.625rem;
  font-family: var(--font-sans, 'Lexend', sans-serif);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;

  /* Styling */
  background-color: rgba(41, 181, 237, 0.15);
  color: #29B5ED;
  border: 1px solid rgba(41, 181, 237, 0.3);
  border-radius: 6px;

  /* Positioning */
  z-index: 10;

  /* Animation */
  transition: all 0.2s ease;
}

.code-block-wrapper:hover .code-language-label {
  background-color: rgba(41, 181, 237, 0.2);
  border-color: rgba(41, 181, 237, 0.4);
}

/* ========================================================================
   MOBILE RESPONSIVE DESIGN
   ======================================================================== */

/**
 * Tablet and smaller devices (< 768px)
 */
@media (max-width: 768px) {
  .copy-code-btn {
    padding: 0.4rem 0.65rem;
    font-size: 0.7rem;
    gap: 0.35rem;
  }

  /* Hide button text on mobile, show only icon */
  .copy-btn-text {
    display: none;
  }

  .copy-icon,
  .check-icon {
    width: 14px;
    height: 14px;
  }

  /* Smaller language label */
  .code-language-label {
    font-size: 0.55rem;
    padding: 0.2rem 0.5rem;
  }

  /* Ensure code blocks don't overflow */
  pre {
    padding: 1rem;
    font-size: 0.8rem;
  }
}

/**
 * Large mobile devices (< 576px)
 */
@media (max-width: 576px) {
  .copy-code-btn {
    padding: 0.35rem 0.5rem;
    top: 0.5rem;
    right: 0.5rem;
  }

  .copy-icon,
  .check-icon {
    width: 12px;
    height: 12px;
  }

  .code-language-label {
    top: 0.5rem;
    left: 0.5rem;
    font-size: 0.5rem;
    padding: 0.15rem 0.4rem;
  }

  pre {
    padding: 0.75rem;
    border-radius: 8px;
  }
}

/* ========================================================================
   ACCESSIBILITY ENHANCEMENTS
   ======================================================================== */

/**
 * Screen reader only text for copy button
 */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/**
 * Reduced motion support
 */
@media (prefers-reduced-motion: reduce) {
  .copy-code-btn,
  .copy-icon,
  .check-icon,
  .code-language-label {
    animation: none !important;
    transition: none !important;
  }
}

/**
 * High contrast mode support
 */
@media (prefers-contrast: high) {
  .copy-code-btn {
    border-width: 2px;
  }

  .copy-code-btn:focus-visible {
    outline-width: 3px;
  }
}

/* ========================================================================
   ENHANCED PRE/CODE BLOCK STYLES
   ======================================================================== */

/**
 * Add subtle inner glow to code blocks
 */
pre {
  box-shadow:
    inset 0 0 0 1px rgba(41, 181, 237, 0.05),
    0 4px 16px rgba(0, 0, 0, 0.2);
}

/**
 * Smooth scroll for long code blocks
 */
pre {
  scroll-behavior: smooth;
}

/**
 * Selection styling in code blocks
 */
pre code::selection,
pre code *::selection {
  background-color: rgba(41, 181, 237, 0.3);
  color: inherit;
}

/* ========================================================================
   PRINT STYLES
   ======================================================================== */

/**
 * Hide copy button and language label when printing
 */
@media print {
  .copy-code-btn,
  .code-language-label {
    display: none !important;
  }

  pre {
    border: 1px solid #333;
    page-break-inside: avoid;
  }
}
