/*!
 * Cool Digital API Tools - Dark Mode System
 * Implements comprehensive dark mode with system preference detection
 * Features: Automatic switching, manual toggle, improved accessibility
 */

/* ================================
   DARK MODE TOKENS OVERRIDE
   ================================ */

[data-theme="dark"] {
  /* Primary color adjustments for dark mode */
  --primary-900: #f0fffe;
  --primary-800: #ccfffe;
  --primary-700: #a3fffd;
  --primary-600: #67fffa;
  --primary-500: #22d6d0;
  --primary-400: #1ba8a3;
  --primary-300: #177a77;
  --primary-200: #134c4a;
  --primary-100: #0f2d2c;
  --primary-50: #0a1615;
  
  --color-primary-1: #f0fffe;
  --color-primary-2: #ccfffe;
  --color-primary-3: #a3fffd;
  --color-primary-4: #67fffa;
  --color-primary-5: #22d6d0;
  --color-primary-6: #1ba8a3;
  --color-primary-7: #177a77;
  --color-primary-8: #134c4a;
  --color-primary-9: #0f2d2c;
  --color-primary-10: #0a1615;

  /* Secondary color adjustments */
  --color-secondary-1: #1a1614;
  --color-secondary-2: #2d251f;
  --color-secondary-3: #3f342a;
  --color-secondary-4: #524335;
  --color-secondary-5: #655240;
  --color-secondary-6: #78614b;
  --color-secondary-7: #8b7056;
  --color-secondary-8: #9e7f61;
  --color-secondary-9: #b18e6c;
  --color-secondary-10: #e6d7c3;

  /* Accent adjustments */
  --color-accent-1: #2d1a16;
  --color-accent-2: #3d2119;
  --color-accent-3: #4d281c;
  --color-accent-4: #5d2f1f;
  --color-accent-5: #6d3622;
  --color-accent-6: #7d3d25;
  --color-accent-7: #8d4428;
  --color-accent-8: #9d4b2b;
  --color-accent-9: #ad522e;
  --color-accent-10: #ff6b47;

  /* Neutral scale for dark mode */
  --color-neutral-1: #0a0a0a;
  --color-neutral-2: #171717;
  --color-neutral-3: #262626;
  --color-neutral-4: #404040;
  --color-neutral-5: #525252;
  --color-neutral-6: #737373;
  --color-neutral-7: #a3a3a3;
  --color-neutral-8: #d4d4d4;
  --color-neutral-9: #e5e5e5;
  --color-neutral-10: #f5f5f5;

  /* Semantic colors for dark mode */
  --color-surface-primary: #0f0f0f;
  --color-surface-secondary: #1a1a1a;
  --color-surface-tertiary: #262626;
  --color-surface-elevated: #2d2d2d;

  --color-text-primary: #f5f5f5;
  --color-text-secondary: #a3a3a3;
  --color-text-tertiary: #737373;
  --color-text-heading: #ffffff;
  --color-text-muted: #525252;

  --color-border: #404040;
  --color-border-light: #262626;
  --color-border-strong: #525252;

  /* Status colors adjusted for dark mode */
  --color-success: #22c55e;
  --color-success-bg: #14532d;
  --color-success-border: #166534;

  --color-warning: #f59e0b;
  --color-warning-bg: #451a03;
  --color-warning-border: #92400e;

  --color-error: #ef4444;
  --color-error-bg: #450a0a;
  --color-error-border: #991b1b;

  --color-info: #3b82f6;
  --color-info-bg: #172554;
  --color-info-border: #1d4ed8;

  /* Shadows adjusted for dark mode */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4), 0 2px 4px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4), 0 4px 6px rgba(0, 0, 0, 0.3);
  --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.4), 0 10px 10px rgba(0, 0, 0, 0.2);
  --shadow-focus: 0 0 0 3px rgba(34, 214, 208, 0.3);

  /* Interactive state adjustments */
  --color-hover-overlay: rgba(255, 255, 255, 0.05);
  --color-active-overlay: rgba(255, 255, 255, 0.1);
  --color-selected-overlay: rgba(34, 214, 208, 0.15);
}

/* ================================
   SYSTEM PREFERENCE DETECTION
   ================================ */

/* Default to light mode */
:root {
  color-scheme: light;
}

/* Auto dark mode based on system preference */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    
    /* Apply dark mode variables automatically */
    --color-primary-1: #f0fffe;
    --color-primary-2: #ccfffe;
    --color-primary-3: #a3fffd;
    --color-primary-4: #67fffa;
    --color-primary-5: #22d6d0;
    --color-primary-6: #1ba8a3;
    --color-primary-7: #177a77;
    --color-primary-8: #134c4a;
    --color-primary-9: #0f2d2c;
    --color-primary-10: #0a1615;

    --color-surface-primary: #0f0f0f;
    --color-surface-secondary: #1a1a1a;
    --color-surface-tertiary: #262626;
    --color-surface-elevated: #2d2d2d;

    --color-text-primary: #f5f5f5;
    --color-text-secondary: #a3a3a3;
    --color-text-tertiary: #737373;
    --color-text-heading: #ffffff;
    --color-text-muted: #525252;

    --color-border: #404040;
    --color-border-light: #262626;
    --color-border-strong: #525252;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4), 0 2px 4px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.4), 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.4), 0 10px 10px rgba(0, 0, 0, 0.2);
    --shadow-focus: 0 0 0 3px rgba(34, 214, 208, 0.3);
  }
}

/* ================================
   DARK MODE SPECIFIC STYLES
   ================================ */

[data-theme="dark"] {
  /* Enhanced contrast for better readability */
  --color-contrast-high: #ffffff;
  --color-contrast-medium: #d4d4d4;
  --color-contrast-low: #737373;

  /* Glow effects for dark mode */
  --glow-primary: 0 0 20px rgba(34, 214, 208, 0.2);
  --glow-accent: 0 0 20px rgba(255, 107, 71, 0.2);
  --glow-success: 0 0 20px rgba(34, 197, 94, 0.2);
}

/* Dark mode image adjustments */
[data-theme="dark"] img:not([data-no-invert]) {
  filter: brightness(0.9) contrast(1.1);
}

/* Dark mode logo adjustments */
[data-theme="dark"] .logo-light {
  display: none;
}

[data-theme="dark"] .logo-dark {
  display: block;
}

/* Light mode logo adjustments */
[data-theme="light"] .logo-light,
:root:not([data-theme]) .logo-light {
  display: block;
}

[data-theme="light"] .logo-dark,
:root:not([data-theme]) .logo-dark {
  display: none;
}

/* ================================
   THEME TOGGLE BUTTON
   ================================ */

.theme-toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: var(--color-surface-secondary);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all var(--transition-fast) var(--transition-ease);
  color: var(--color-text-secondary);
  font-size: 18px;
  overflow: hidden;
}

.theme-toggle:hover {
  background: var(--color-surface-tertiary);
  border-color: var(--color-primary-6);
  color: var(--color-primary-6);
  box-shadow: var(--shadow-sm);
  transform: translateY(-1px);
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--color-primary-6);
  outline-offset: 2px;
}

.theme-toggle:active {
  transform: translateY(0);
  box-shadow: none;
}

/* Theme toggle icons */
.theme-toggle .sun-icon,
.theme-toggle .moon-icon {
  position: absolute;
  transition: all var(--transition-medium) var(--transition-ease);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Light mode - show moon, hide sun */
:root:not([data-theme="dark"]) .theme-toggle .sun-icon,
[data-theme="light"] .theme-toggle .sun-icon {
  opacity: 0;
  transform: rotate(180deg) scale(0.5);
}

:root:not([data-theme="dark"]) .theme-toggle .moon-icon,
[data-theme="light"] .theme-toggle .moon-icon {
  opacity: 1;
  transform: rotate(0deg) scale(1);
}

/* Dark mode - show sun, hide moon */
[data-theme="dark"] .theme-toggle .sun-icon {
  opacity: 1;
  transform: rotate(0deg) scale(1);
}

[data-theme="dark"] .theme-toggle .moon-icon {
  opacity: 0;
  transform: rotate(-180deg) scale(0.5);
}

/* ================================
   DARK MODE ANIMATIONS
   ================================ */

/* Smooth theme transition */
*,
*::before,
*::after {
  transition-property: background-color, border-color, color, box-shadow, filter;
  transition-duration: var(--transition-medium);
  transition-timing-function: var(--transition-ease);
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}

/* Theme transition animation */
@keyframes themeTransition {
  0% {
    opacity: 0.8;
  }
  100% {
    opacity: 1;
  }
}

.theme-transitioning {
  animation: themeTransition var(--transition-slow) var(--transition-ease);
}

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

/* High contrast mode support in dark mode */
@media (prefers-contrast: high) {
  [data-theme="dark"] {
    --color-surface-primary: #000000;
    --color-surface-secondary: #111111;
    --color-text-primary: #ffffff;
    --color-border: #ffffff;
    --color-primary-6: #00ffff;
  }
}

/* Forced colors mode (Windows high contrast) */
@media (forced-colors: active) {
  .theme-toggle {
    border: 1px solid ButtonText;
    background: ButtonFace;
    color: ButtonText;
  }
  
  .theme-toggle:hover {
    background: Highlight;
    color: HighlightText;
  }
}

/* ================================
   DARK MODE SPECIFIC COMPONENTS
   ================================ */

/* Enhanced focus rings in dark mode */
[data-theme="dark"] :focus-visible {
  outline-color: var(--color-primary-4);
  box-shadow: 0 0 0 2px var(--color-surface-primary), 
              0 0 0 4px var(--color-primary-4);
}

/* Code blocks in dark mode */
[data-theme="dark"] pre {
  background-color: #1a1a1a;
  border-left-color: var(--color-primary-5);
}

[data-theme="dark"] code {
  background-color: #262626;
  color: var(--color-primary-4);
}

/* Tables in dark mode */
[data-theme="dark"] table {
  background-color: var(--color-surface-primary);
}

[data-theme="dark"] th {
  background-color: var(--color-surface-secondary);
}

[data-theme="dark"] tr:hover {
  background-color: var(--color-surface-tertiary);
}

/* Form elements in dark mode */
[data-theme="dark"] input,
[data-theme="dark"] textarea,
[data-theme="dark"] select {
  background-color: var(--color-surface-secondary);
  border-color: var(--color-border);
  color: var(--color-text-primary);
}

[data-theme="dark"] input:focus,
[data-theme="dark"] textarea:focus,
[data-theme="dark"] select:focus {
  background-color: var(--color-surface-tertiary);
  border-color: var(--color-primary-5);
  box-shadow: var(--shadow-focus);
}

/* Schema Viewer Dark Mode */
[data-theme="dark"] .explorer-sidebar,
[data-theme="dark"] .explorer-content {
  background-color: var(--color-surface-primary);
  border-color: var(--color-border);
}

[data-theme="dark"] .tree-node:hover {
  background-color: var(--color-surface-tertiary);
}

[data-theme="dark"] .tree-node.active {
  background-color: var(--color-primary-9);
  color: var(--color-primary-3);
}

[data-theme="dark"] .schema-content {
  background-color: var(--color-surface-secondary);
  border-color: var(--color-border);
  color: var(--color-text-primary);
}

[data-theme="dark"] .schema-info {
  background-color: var(--color-info-bg);
  border-color: var(--color-info-border);
  color: var(--color-info);
}

/* ================================
   PRINT STYLES FOR DARK MODE
   ================================ */

@media print {
  [data-theme="dark"] {
    --color-surface-primary: white !important;
    --color-surface-secondary: white !important;
    --color-text-primary: black !important;
    --color-text-secondary: #333333 !important;
    --color-border: #cccccc !important;
  }
}

/* ================================
   SYSTEM INTEGRATION
   ================================ */

/* Meta theme color for mobile browsers */
meta[name="theme-color"] {
  content: var(--color-surface-primary);
}

/* Status bar styling for iOS */
meta[name="apple-mobile-web-app-status-bar-style"] {
  content: default;
}

[data-theme="dark"] meta[name="apple-mobile-web-app-status-bar-style"] {
  content: black-translucent;
}
