/* System Design Bible - Production Grade Stylesheet */

/* ============================================
   CSS Variables - Dark Theme (Default)
   ============================================ */
:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #1f2937;
  --text-primary: #e2e8f0;
  --text-secondary: #b8c5d4;
  --accent-blue: #93c5fd;
  --accent-green: #6ee7b7;
  --accent-purple: #c4b5fd;
  --accent-orange: #fdba74;
  --accent-red: #fca5a5;
  --accent-yellow: #fde68a;
  --border-color: #374151;
  --code-bg: #0f172a;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
  --radius: 8px;

  /* Compatibility aliases */
  --primary-color: var(--accent-blue);
  --secondary-color: var(--accent-purple);
  --bg-color: var(--bg-primary);
  --text-color: var(--text-primary);
  --card-bg: var(--bg-card);
}

/* Light Theme - Handles both data-theme attribute and class-based themes */
[data-theme="light"],
body.light-theme,
html.light-theme {
  --bg-primary: #ffffff;
  --bg-secondary: #f8fafc;
  --bg-card: #f1f5f9;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --accent-blue: #2563eb;
  --accent-green: #059669;
  --accent-purple: #7c3aed;
  --accent-orange: #ea580c;
  --accent-red: #dc2626;
  --accent-yellow: #d97706;
  --border-color: #e2e8f0;
  --code-bg: #f1f5f9;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

/* Dark theme explicit handling */
body.dark-theme,
html.dark-theme {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #1f2937;
  --text-primary: #e2e8f0;
  --text-secondary: #b8c5d4;
  --accent-blue: #93c5fd;
  --accent-green: #6ee7b7;
  --accent-purple: #c4b5fd;
  --accent-orange: #fdba74;
  --accent-red: #fca5a5;
  --accent-yellow: #fde68a;
  --border-color: #374151;
  --code-bg: #0f172a;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
}

/* ============================================
   Global Reset & Base Styles
   ============================================ */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.7;
  font-size: 16px;
  transition: background 0.3s ease, color 0.3s ease;
}

/* ============================================
   Layout Structure
   ============================================ */
.page-wrapper {
  display: flex;
  min-height: 100vh;
}

/* Sidebar - Handles multiple selector variations */
.sidebar,
nav.sidebar,
aside.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 280px;
  height: 100vh;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  overflow-y: auto;
  padding: 24px 16px;
  z-index: 100;
  transition: transform 0.3s ease, background 0.3s ease;
}

.sidebar-header {
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 16px;
}

.sidebar-header h2 {
  font-size: 14px;
  color: var(--accent-blue);
  text-transform: uppercase;
  letter-spacing: 1px;
}

.sidebar nav ul,
.sidebar ul {
  list-style: none;
}

.sidebar nav ul li,
.sidebar ul li {
  margin-bottom: 4px;
}

.sidebar nav ul li a,
.sidebar ul li a {
  display: block;
  padding: 8px 12px;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius);
  font-size: 14px;
  transition: all 0.2s;
}

.sidebar nav ul li a:hover,
.sidebar nav ul li a.active,
.sidebar ul li a:hover,
.sidebar ul li a.active {
  background: var(--bg-card);
  color: var(--accent-blue);
}

/* Main Content - Handles all variations */
.main-content,
main.main-content,
main.content,
.content {
  margin-left: 280px;
  flex: 1;
  padding: 48px 64px;
  max-width: 1400px;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem;
}

/* ============================================
   Typography
   ============================================ */
h1 {
  font-size: 2.5rem;
  font-weight: 800;
  margin-bottom: 16px;
  color: var(--text-primary);
  line-height: 1.2;
}

h2 {
  font-size: 1.8rem;
  font-weight: 700;
  margin-top: 48px;
  margin-bottom: 16px;
  color: var(--text-primary);
  padding-bottom: 8px;
  border-bottom: 2px solid var(--border-color);
}

h3 {
  font-size: 1.4rem;
  font-weight: 600;
  margin-top: 32px;
  margin-bottom: 12px;
  color: var(--accent-blue);
}

h4 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-top: 24px;
  margin-bottom: 8px;
  color: var(--accent-purple);
}

h5 {
  font-size: 1rem;
  font-weight: 600;
  margin-top: 16px;
  margin-bottom: 8px;
  color: var(--text-primary);
}

p {
  margin-bottom: 16px;
  color: var(--text-secondary);
}

/* ============================================
   Intro & Content Boxes
   ============================================ */
.chapter-intro,
.intro,
.intro-box,
p.chapter-intro {
  font-size: 1.1rem;
  line-height: 1.8;
  margin-bottom: 32px;
  padding: 20px;
  background: var(--bg-card);
  border-left: 4px solid var(--accent-blue);
  border-radius: var(--radius);
  color: var(--text-secondary);
}

/* ============================================
   Mermaid Diagrams - CRITICAL FIXES
   ============================================ */
.mermaid,
div.mermaid,
pre.mermaid {
  background: transparent !important;
  border: none;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 24px 0;
  padding: 16px;
  min-height: 200px;
}

/* Prevent dark mode inversion on Mermaid SVGs */
[data-theme="dark"] .mermaid svg,
body.dark-theme .mermaid svg,
html.dark-theme .mermaid svg {
  filter: none !important;
}

/* Light mode mermaid adjustments */
[data-theme="light"] .mermaid,
body.light-theme .mermaid,
html.light-theme .mermaid {
  background: transparent !important;
}

.diagram-container,
.architecture-diagram {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 24px;
  margin: 24px 0;
  overflow-x: auto;
  transition: background 0.3s ease;
}

.diagram-container .mermaid,
.architecture-diagram .mermaid,
.architecture-diagram pre.mermaid {
  display: flex;
  justify-content: center;
  background: transparent !important;
}

.diagram-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-blue);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 16px;
}

/* ============================================
   Callout Boxes - ALL VARIATIONS
   ============================================ */
.callout,
.interview-tip,
.interview-callout,
.callout.interview {
  border-radius: var(--radius);
  padding: 20px 24px;
  margin: 24px 0;
  border-left: 4px solid;
  background: rgba(147, 197, 253, 0.15);
  border-color: var(--accent-blue);
  transition: background 0.3s ease;
}

.callout-title {
  font-weight: 700;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
  color: var(--accent-blue);
}

/* Callout variants */
.callout-published {
  background: rgba(110, 231, 183, 0.15);
  border-color: var(--accent-green);
}

.callout-published .callout-title {
  color: var(--accent-green);
}

.callout-interview,
.interview-tip,
.interview-callout {
  background: rgba(253, 230, 138, 0.15);
  border-color: var(--accent-yellow);
  font-weight: 500;
}

.callout-interview .callout-title,
.interview-tip strong,
.interview-callout strong {
  color: var(--accent-yellow);
}

.callout-warning {
  background: rgba(252, 165, 165, 0.15);
  border-color: var(--accent-red);
}

.callout-warning .callout-title {
  color: var(--accent-red);
}

.callout-key {
  background: rgba(196, 181, 253, 0.15);
  border-color: var(--accent-purple);
}

.callout-key .callout-title {
  color: var(--accent-purple);
}

.callout-industry {
  background: rgba(147, 197, 253, 0.15);
  border-color: var(--accent-blue);
}

.callout-industry .callout-title {
  color: var(--accent-blue);
}

/* ============================================
   Example & Explanation Boxes
   ============================================ */
.example-box,
.explanation {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 20px;
  margin: 20px 0;
  transition: background 0.3s ease;
}

.example-box h4,
.explanation h4 {
  margin-top: 0;
  color: var(--accent-purple);
}

/* ============================================
   Code Blocks
   ============================================ */
pre {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 20px;
  overflow-x: auto;
  margin: 16px 0;
  transition: background 0.3s ease;
}

code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 14px;
  line-height: 1.6;
}

p code,
li code {
  background: var(--bg-card);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  color: var(--accent-orange);
}

.code-block {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 20px;
  margin: 20px 0;
  overflow-x: auto;
  transition: background 0.3s ease;
}

.code-header {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent-blue);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

/* ============================================
   Calculation & Formula Boxes
   ============================================ */
.calculation,
.formula-box {
  background: var(--code-bg);
  border: 1px solid var(--accent-purple);
  border-radius: var(--radius);
  padding: 20px;
  margin: 20px 0;
  font-family: 'JetBrains Mono', monospace;
  font-size: 15px;
  color: var(--accent-purple);
  transition: background 0.3s ease;
}

.formula-box .label {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
  font-family: 'Inter', sans-serif;
}

/* ============================================
   Tables
   ============================================ */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 24px 0;
  font-size: 14px;
}

thead {
  background: var(--bg-card);
}

th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: var(--accent-blue);
  border-bottom: 2px solid var(--border-color);
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  color: var(--text-secondary);
}

tr:hover {
  background: var(--bg-card);
}

.table-good {
  color: var(--accent-green);
  font-weight: 600;
}

.table-bad {
  color: var(--accent-red);
  font-weight: 600;
}

.table-neutral {
  color: var(--accent-yellow);
  font-weight: 600;
}

/* ============================================
   Lists
   ============================================ */
ul,
ol {
  margin: 12px 0 16px 24px;
  color: var(--text-secondary);
}

li {
  margin-bottom: 8px;
}

/* ============================================
   Collapsible Sections
   ============================================ */
details {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  margin: 16px 0;
  transition: background 0.3s ease;
}

summary {
  padding: 16px 20px;
  cursor: pointer;
  font-weight: 600;
  color: var(--accent-blue);
  user-select: none;
  transition: background 0.2s ease;
}

summary:hover {
  background: var(--bg-secondary);
}

details[open] summary {
  border-bottom: 1px solid var(--border-color);
}

details .content,
details > div {
  padding: 20px;
}

/* ============================================
   Theme Toggle - CRITICAL FIXES
   ============================================ */
.theme-toggle,
button.theme-toggle,
button#theme-toggle,
div.theme-toggle {
  position: fixed;
  top: 16px;
  right: 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 20px;
  z-index: 200;
  transition: all 0.2s;
  color: var(--text-primary);
}

.theme-toggle:hover,
button.theme-toggle:hover {
  transform: scale(1.1);
  border-color: var(--accent-blue);
}

.theme-toggle .sun,
.theme-toggle .moon {
  font-size: 20px;
}

/* ============================================
   Comparison Sections
   ============================================ */
.comparison-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin: 24px 0;
}

.comparison-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 20px;
  transition: background 0.3s ease;
}

.comparison-card h4 {
  margin-top: 0;
  margin-bottom: 12px;
}

.comparison-card.pros {
  border-top: 3px solid var(--accent-green);
}

.comparison-card.cons {
  border-top: 3px solid var(--accent-red);
}

/* ============================================
   Chapter Navigation
   ============================================ */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 64px;
  padding-top: 32px;
  border-top: 1px solid var(--border-color);
  gap: 16px;
}

.chapter-nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--accent-blue);
  text-decoration: none;
  font-weight: 500;
  transition: all 0.2s;
}

.chapter-nav a:hover {
  border-color: var(--accent-blue);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* ============================================
   Case Study Header
   ============================================ */
.case-study-header {
  background: linear-gradient(135deg, var(--accent-blue) 0%, var(--accent-purple) 100%);
  padding: 32px;
  border-radius: var(--radius);
  margin-bottom: 32px;
  color: white;
}

.case-study-header h1 {
  color: white;
  margin-bottom: 8px;
}

/* ============================================
   Section Styling
   ============================================ */
section {
  margin: 32px 0;
}

section.summary {
  background: var(--bg-card);
  border: 2px solid var(--accent-blue);
  border-radius: var(--radius);
  padding: 32px;
  margin: 48px 0;
}

section.summary h2 {
  color: var(--accent-blue);
  border-bottom: none;
  margin-top: 0;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 1024px) {
  .sidebar,
  nav.sidebar,
  aside.sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open,
  nav.sidebar.open,
  aside.sidebar.open {
    transform: translateX(0);
  }

  .main-content,
  main.main-content,
  main.content,
  .content {
    margin-left: 0;
    padding: 32px 24px;
  }

  .comparison-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.2rem;
  }

  .main-content,
  main.main-content,
  main.content,
  .content {
    padding: 24px 16px;
  }

  .chapter-nav {
    flex-direction: column;
  }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
  .sidebar,
  nav.sidebar,
  aside.sidebar,
  .theme-toggle,
  button.theme-toggle,
  .chapter-nav {
    display: none !important;
  }

  .main-content,
  main.main-content,
  main.content,
  .content {
    margin-left: 0;
    padding: 0;
  }

  body {
    background: white;
    color: black;
  }

  .callout,
  .diagram-container,
  .architecture-diagram {
    border: 1px solid #ccc;
    page-break-inside: avoid;
  }

  .mermaid {
    page-break-inside: avoid;
  }
}

/* ============================================
   Scrollbar Styling
   ============================================ */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--accent-blue);
}

/* ============================================
   Utility Classes
   ============================================ */
.text-center {
  text-align: center;
}

.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }
.mt-4 { margin-top: 32px; }

.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mb-3 { margin-bottom: 24px; }
.mb-4 { margin-bottom: 32px; }

/* ============================================
   Animations
   ============================================ */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeIn 0.3s ease-in-out;
}
