/* Enhanced Code Block Styles */

/* Code block wrapper */
.code-block-wrapper {
  position: relative;
  margin: 1.5rem 0;
  border-radius: 8px;
  overflow: hidden;
  background-color: #f6f8fa;
  border: 1px solid #e1e4e8;
}

/* Language label */
.code-language-label {
  display: inline-block;
  position: absolute;
  top: 0;
  right: 50px;
  padding: 0.25rem 0.75rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: #586069;
  background-color: #e1e4e8;
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  z-index: 1;
}

/* Pre and code elements */
.code-block-wrapper pre {
  margin: 0;
  padding: 1rem;
  background-color: #f6f8fa;
  border: none;
  border-radius: 0;
  overflow-x: auto;
  font-size: 0.875rem;
  line-height: 1.6;
}

.code-block-wrapper pre code {
  background-color: transparent;
  padding: 0;
  border: none;
  color: inherit;
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', 'Menlo', 'Monaco', monospace;
}

/* Inline code (not in pre blocks) */
code {
  background-color: #f6f8fa;
  padding: 0.2em 0.4em;
  margin: 0;
  font-size: 85%;
  border-radius: 3px;
  font-family: 'SFMono-Regular', 'Consolas', 'Liberation Mono', 'Menlo', 'Monaco', monospace;
  color: #e83e8c;
}

pre code {
  background-color: transparent;
  padding: 0;
  font-size: inherit;
  color: inherit;
}

/* Copy button */
.code-copy-button {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.5rem 0.75rem;
  background-color: #ffffff;
  border: 1px solid #d1d5da;
  border-radius: 6px;
  color: #586069;
  font-size: 0.875rem;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 2;
  opacity: 0;
}

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

.code-copy-button:hover {
  background-color: #f3f4f6;
  border-color: #959da5;
  color: #24292e;
}

.code-copy-button:active {
  background-color: #edeff2;
  transform: scale(0.95);
}

.code-copy-button i {
  font-size: 1rem;
}

/* Copy button states */
.code-copy-button.copied {
  background-color: #28a745;
  border-color: #28a745;
  color: #ffffff;
}

.code-copy-button.copy-error {
  background-color: #dc3545;
  border-color: #dc3545;
  color: #ffffff;
}

/* Line numbers styling (if using Rouge with line numbers) */
.highlight .lineno {
  color: #959da5;
  user-select: none;
  padding-right: 1em;
  border-right: 1px solid #e1e4e8;
  margin-right: 1em;
}

/* Scrollbar styling for code blocks */
.code-block-wrapper pre::-webkit-scrollbar {
  height: 8px;
}

.code-block-wrapper pre::-webkit-scrollbar-track {
  background: #e1e4e8;
  border-radius: 4px;
}

.code-block-wrapper pre::-webkit-scrollbar-thumb {
  background: #959da5;
  border-radius: 4px;
}

.code-block-wrapper pre::-webkit-scrollbar-thumb:hover {
  background: #6a737d;
}

/* Dark mode support */
:root[data-theme="dark"] .code-block-wrapper,
:root:not([data-theme]) {
  @media (prefers-color-scheme: dark) {
    .code-block-wrapper {
      background-color: #1e1e1e;
      border-color: #3e3e3e;
    }

    .code-language-label {
      color: #b0b0b0;
      background-color: #2d2d2d;
    }

    .code-block-wrapper pre {
      background-color: #1e1e1e;
    }

    code {
      background-color: #2d2d2d;
      color: #f092b0;
    }

    .code-copy-button {
      background-color: #2d2d2d;
      border-color: #4a4a4a;
      color: #b0b0b0;
    }

    .code-copy-button:hover {
      background-color: #3a3a3a;
      border-color: #5a5a5a;
      color: #e0e0e0;
    }

    .code-copy-button:active {
      background-color: #2a2a2a;
    }

    .highlight .lineno {
      color: #6a737d;
      border-right-color: #3e3e3e;
    }

    .code-block-wrapper pre::-webkit-scrollbar-track {
      background: #2d2d2d;
    }

    .code-block-wrapper pre::-webkit-scrollbar-thumb {
      background: #4a4a4a;
    }

    .code-block-wrapper pre::-webkit-scrollbar-thumb:hover {
      background: #5a5a5a;
    }
  }
}

:root[data-theme="dark"] .code-block-wrapper {
  background-color: #1e1e1e;
  border-color: #3e3e3e;
}

:root[data-theme="dark"] .code-language-label {
  color: #b0b0b0;
  background-color: #2d2d2d;
}

:root[data-theme="dark"] .code-block-wrapper pre {
  background-color: #1e1e1e;
}

:root[data-theme="dark"] code {
  background-color: #2d2d2d;
  color: #f092b0;
}

:root[data-theme="dark"] .code-copy-button {
  background-color: #2d2d2d;
  border-color: #4a4a4a;
  color: #b0b0b0;
}

:root[data-theme="dark"] .code-copy-button:hover {
  background-color: #3a3a3a;
  border-color: #5a5a5a;
  color: #e0e0e0;
}

:root[data-theme="dark"] .code-copy-button:active {
  background-color: #2a2a2a;
}

:root[data-theme="dark"] .highlight .lineno {
  color: #6a737d;
  border-right-color: #3e3e3e;
}

:root[data-theme="dark"] .code-block-wrapper pre::-webkit-scrollbar-track {
  background: #2d2d2d;
}

:root[data-theme="dark"] .code-block-wrapper pre::-webkit-scrollbar-thumb {
  background: #4a4a4a;
}

:root[data-theme="dark"] .code-block-wrapper pre::-webkit-scrollbar-thumb:hover {
  background: #5a5a5a;
}

/* Mobile responsiveness */
@media (max-width: 768px) {
  .code-block-wrapper pre {
    font-size: 0.8rem;
    padding: 0.75rem;
  }

  .code-copy-button {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
    opacity: 1; /* Always show on mobile */
  }

  .code-language-label {
    font-size: 0.7rem;
    padding: 0.2rem 0.5rem;
    right: 45px;
  }
}

/* Accessibility - ensure good keyboard focus */
.code-copy-button:focus {
  outline: 2px solid #0366d6;
  outline-offset: 2px;
  opacity: 1;
}

:root[data-theme="dark"] .code-copy-button:focus {
  outline-color: #4da3ff;
}

/* Improve text selection in code blocks */
.code-block-wrapper pre code {
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}
