/* ========================================
   ICD-10-CM 2025 Interactive Viewer
   Modern 2025 Design System
   ======================================== */

/* CSS Custom Properties - Design Tokens */
:root {
  /* Typography */
  --font-primary: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Consolas", monospace;

  /* Spacing Scale */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;
  --space-3xl: 4rem;

  /* Border Radius */
  --radius-sm: 0.375rem;
  --radius-md: 0.75rem;
  --radius-lg: 1rem;
  --radius-xl: 1.5rem;
  --radius-full: 9999px;

  /* Transitions */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-spring: 500ms cubic-bezier(0.34, 1.56, 0.64, 1);

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1),
    0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1),
    0 8px 10px -6px rgb(0 0 0 / 0.1);
  --shadow-glow: 0 0 40px -10px;

  /* Z-Index Scale */
  --z-base: 1;
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-modal: 300;
  --z-toast: 400;
  --z-loading: 500;

  /* Layout */
  --header-height: 4rem;
  --sidebar-width: 320px;

  /* Chapter Colors - Medical Color Palette */
  --chapter-1: #ef4444; /* Infectious - Red */
  --chapter-2: #dc2626; /* Neoplasms - Dark Red */
  --chapter-3: #f97316; /* Blood - Orange */
  --chapter-4: #f59e0b; /* Endocrine - Amber */
  --chapter-5: #a855f7; /* Mental - Purple */
  --chapter-6: #8b5cf6; /* Nervous - Violet */
  --chapter-7: #06b6d4; /* Eye - Cyan */
  --chapter-8: #14b8a6; /* Ear - Teal */
  --chapter-9: #e11d48; /* Circulatory - Rose */
  --chapter-10: #3b82f6; /* Respiratory - Blue */
  --chapter-11: #84cc16; /* Digestive - Lime */
  --chapter-12: #fbbf24; /* Skin - Yellow */
  --chapter-13: #fb923c; /* Musculoskeletal - Orange */
  --chapter-14: #ec4899; /* Genitourinary - Pink */
  --chapter-15: #f472b6; /* Pregnancy - Light Pink */
  --chapter-16: #c084fc; /* Perinatal - Light Purple */
  --chapter-17: #818cf8; /* Congenital - Indigo */
  --chapter-18: #94a3b8; /* Symptoms - Slate */
  --chapter-19: #fbbf24; /* Injury - Yellow */
  --chapter-20: #a3e635; /* External Causes - Lime */
  --chapter-21: #22d3ee; /* Health Factors - Cyan */
  --chapter-22: #6366f1; /* Special Codes - Indigo */

  /* Instruction Colors */
  --excludes1: #ef4444;
  --excludes2: #f97316;
  --includes: #22c55e;
  --code-first: #3b82f6;
  --use-additional: #10b981;
  --code-also: #8b5cf6;

  /* Encounter Colors */
  --encounter-initial: #3b82f6;
  --encounter-subsequent: #22c55e;
  --encounter-sequela: #6b7280;
}

/* Dark Theme (Default) */
[data-theme="dark"] {
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-tertiary: #1a1a24;
  --bg-elevated: #22222e;
  --bg-glass: rgba(18, 18, 26, 0.8);
  --bg-glass-hover: rgba(26, 26, 36, 0.9);

  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-tertiary: #64748b;
  --text-muted: #475569;

  --border-primary: rgba(255, 255, 255, 0.08);
  --border-secondary: rgba(255, 255, 255, 0.04);
  --border-accent: rgba(255, 255, 255, 0.12);

  --accent-primary: #6366f1;
  --accent-secondary: #818cf8;
  --accent-glow: rgba(99, 102, 241, 0.3);

  --gradient-mesh: radial-gradient(
      at 40% 20%,
      rgba(99, 102, 241, 0.15) 0px,
      transparent 50%
    ),
    radial-gradient(at 80% 0%, rgba(139, 92, 246, 0.1) 0px, transparent 50%),
    radial-gradient(at 0% 50%, rgba(6, 182, 212, 0.1) 0px, transparent 50%),
    radial-gradient(at 80% 50%, rgba(236, 72, 153, 0.1) 0px, transparent 50%),
    radial-gradient(at 0% 100%, rgba(34, 197, 94, 0.1) 0px, transparent 50%);
}

/* Light Theme */
[data-theme="light"] {
  --bg-primary: #f8fafc;
  --bg-secondary: #ffffff;
  --bg-tertiary: #f1f5f9;
  --bg-elevated: #ffffff;
  --bg-glass: rgba(255, 255, 255, 0.85);
  --bg-glass-hover: rgba(255, 255, 255, 0.95);

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-tertiary: #64748b;
  --text-muted: #94a3b8;

  --border-primary: rgba(0, 0, 0, 0.08);
  --border-secondary: rgba(0, 0, 0, 0.04);
  --border-accent: rgba(0, 0, 0, 0.12);

  --accent-primary: #4f46e5;
  --accent-secondary: #6366f1;
  --accent-glow: rgba(79, 70, 229, 0.2);

  --gradient-mesh: radial-gradient(
      at 40% 20%,
      rgba(99, 102, 241, 0.08) 0px,
      transparent 50%
    ),
    radial-gradient(at 80% 0%, rgba(139, 92, 246, 0.06) 0px, transparent 50%),
    radial-gradient(at 0% 50%, rgba(6, 182, 212, 0.06) 0px, transparent 50%),
    radial-gradient(at 80% 50%, rgba(236, 72, 153, 0.06) 0px, transparent 50%),
    radial-gradient(at 0% 100%, rgba(34, 197, 94, 0.06) 0px, transparent 50%);
}

/* Base Reset */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-primary);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden;
}

/* Background Effects */
.bg-gradient {
  position: fixed;
  inset: 0;
  background: var(--gradient-mesh);
  pointer-events: none;
  z-index: -2;
}

.bg-noise {
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  opacity: 0.02;
  pointer-events: none;
  z-index: -1;
}

/* Loading Screen */
.loading-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
  z-index: var(--z-loading);
}

.loader-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loader-ring {
  position: absolute;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  border: 2px solid transparent;
  animation: loader-spin 2s linear infinite;
}

.loader-ring:nth-child(1) {
  border-top-color: var(--accent-primary);
  animation-delay: 0s;
}

.loader-ring:nth-child(2) {
  width: 170px;
  height: 170px;
  border-right-color: var(--chapter-9);
  animation-delay: -0.5s;
  animation-direction: reverse;
}

.loader-ring:nth-child(3) {
  width: 140px;
  height: 140px;
  border-bottom-color: var(--chapter-21);
  animation-delay: -1s;
}

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

.loader-content {
  text-align: center;
  z-index: 1;
}

.loader-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto var(--space-md);
  color: var(--accent-primary);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(0.95);
  }
}

.loader-content h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: var(--space-xs);
}

.loader-content p {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: var(--space-lg);
}

.progress-bar {
  width: 200px;
  height: 4px;
  background: var(--border-primary);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(
    90deg,
    var(--accent-primary),
    var(--accent-secondary)
  );
  border-radius: var(--radius-full);
  transition: width var(--transition-base);
}

/* Utility Classes */
.hidden {
  display: none !important;
}

/* App Container */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Header */
.header {
  position: sticky;
  top: 0;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-lg);
  background: var(--bg-glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 1px solid var(--border-primary);
  z-index: var(--z-sticky);
  gap: var(--space-lg);
}

.header-left,
.header-right {
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.logo-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent-primary), var(--chapter-9));
  border-radius: var(--radius-md);
  color: white;
}

.logo-icon svg {
  width: 20px;
  height: 20px;
}

.logo-text {
  display: flex;
  flex-direction: column;
}

.logo-title {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.logo-subtitle {
  font-size: 0.7rem;
  color: var(--text-tertiary);
  font-weight: 500;
}

.header-center {
  flex: 1;
  max-width: 600px;
}

/* Search */
.search-container {
  position: relative;
  width: 100%;
}

.search-input {
  width: 100%;
  height: 44px;
  padding: 0 var(--space-lg) 0 44px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  font-family: inherit;
  font-size: 0.9375rem;
  color: var(--text-primary);
  outline: none;
  transition: all var(--transition-base);
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-input:focus {
  background: var(--bg-elevated);
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-shortcuts {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 4px;
}

.search-shortcuts kbd {
  padding: 2px 6px;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
}

/* Search Results Dropdown */
.search-results {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 400px;
  overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  z-index: var(--z-dropdown);
}

.search-result-item {
  display: flex;
  align-items: flex-start;
  gap: var(--space-md);
  padding: var(--space-md);
  cursor: pointer;
  transition: background var(--transition-fast);
  border-bottom: 1px solid var(--border-secondary);
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background: var(--bg-glass-hover);
}

.search-result-code {
  font-family: var(--font-mono);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 4px 8px;
  background: var(--accent-glow);
  border-radius: var(--radius-sm);
  color: var(--accent-secondary);
  white-space: nowrap;
}

.search-result-info {
  flex: 1;
  min-width: 0;
}

.search-result-title {
  font-size: 0.9375rem;
  font-weight: 500;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-result-path {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.search-source-pill {
  display: inline-block;
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 8px;
  border-radius: var(--radius-full);
}

/* Header Buttons */
.header-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition-base);
}

.header-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-color: var(--border-accent);
}

.header-btn svg {
  width: 18px;
  height: 18px;
}

/* Theme Toggle Icons */
[data-theme="dark"] .sun-icon {
  display: block;
}
[data-theme="dark"] .moon-icon {
  display: none;
}
[data-theme="light"] .sun-icon {
  display: none;
}
[data-theme="light"] .moon-icon {
  display: block;
}

/* Main Content Layout */
.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  height: calc(100vh - var(--header-height));
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-primary);
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg);
  border-bottom: 1px solid var(--border-primary);
}

.sidebar-header h3 {
  font-size: 0.875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.chapter-count {
  font-size: 0.75rem;
  padding: 4px 8px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-full);
  color: var(--text-tertiary);
}

.chapter-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-sm);
}

.chapter-list::-webkit-scrollbar {
  width: 6px;
}

.chapter-list::-webkit-scrollbar-track {
  background: transparent;
}

.chapter-list::-webkit-scrollbar-thumb {
  background: var(--border-primary);
  border-radius: var(--radius-full);
}

/* Chapter Items */
.chapter-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md);
  margin-bottom: 2px;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-base);
  width: 100%;
  text-align: left;
}

.chapter-item:hover {
  background: var(--bg-tertiary);
}

.chapter-item.active {
  background: var(--bg-tertiary);
}

.chapter-color {
  width: 4px;
  height: 32px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
}

.chapter-number {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}

.chapter-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.chapter-range {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.chapter-name {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Content Area */
.content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Breadcrumb */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-md) var(--space-xl);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-primary);
  overflow-x: auto;
  white-space: nowrap;
}

.breadcrumb-item {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: 0.8125rem;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.breadcrumb-item:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.breadcrumb-item svg {
  width: 14px;
  height: 14px;
}

.breadcrumb-separator {
  color: var(--text-muted);
}

.breadcrumb-separator::after {
  content: "/";
}

/* Welcome Screen */
.welcome-screen {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-2xl);
}

.welcome-hero {
  text-align: center;
  margin-bottom: var(--space-3xl);
}

.welcome-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto var(--space-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent-primary), var(--chapter-9));
  border-radius: var(--radius-xl);
  color: white;
  box-shadow: var(--shadow-glow) var(--accent-glow);
}

.welcome-icon svg {
  width: 40px;
  height: 40px;
}

.welcome-hero h1 {
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-bottom: var(--space-sm);
  background: linear-gradient(
    135deg,
    var(--text-primary),
    var(--text-secondary)
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-hero p {
  font-size: 1.125rem;
  color: var(--text-secondary);
  max-width: 500px;
  margin: 0 auto;
}

/* Quick Stats */
.quick-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-lg);
  margin-bottom: var(--space-3xl);
}

.stat-card {
  background: var(--bg-glass);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  text-align: center;
  transition: all var(--transition-base);
}

.stat-card:hover {
  transform: translateY(-4px);
  border-color: var(--border-accent);
  box-shadow: var(--shadow-lg);
}

.stat-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto var(--space-md);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
}

.stat-icon svg {
  width: 24px;
  height: 24px;
}

.chapters-icon {
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent-primary);
}
.sections-icon {
  background: rgba(236, 72, 153, 0.15);
  color: var(--chapter-14);
}
.codes-icon {
  background: rgba(34, 197, 94, 0.15);
  color: var(--includes);
}
.index-icon {
  background: rgba(6, 182, 212, 0.15);
  color: var(--chapter-21);
}

.stat-value {
  font-size: 2.25rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-bottom: var(--space-xs);
}

.stat-label {
  font-size: 0.875rem;
  color: var(--text-secondary);
  font-weight: 500;
}

/* Quick Actions */
.quick-actions {
  margin-bottom: var(--space-3xl);
}

.quick-actions h3,
.color-legend h3 {
  font-size: 1.125rem;
  font-weight: 600;
  margin-bottom: var(--space-lg);
  color: var(--text-primary);
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--space-md);
}

.action-card {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-lg);
  background: var(--bg-glass);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-base);
  text-decoration: none;
}

.action-card:hover {
  background: var(--bg-glass-hover);
  border-color: var(--border-accent);
  transform: translateX(4px);
}

.action-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.action-icon svg {
  width: 24px;
  height: 24px;
}

.action-info h4 {
  font-size: 0.9375rem;
  font-weight: 600;
  margin-bottom: 2px;
  color: var(--text-primary);
}

.action-info p {
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

/* Color Legend */
.legend-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: var(--space-sm);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-glass);
  border: 1px solid var(--border-secondary);
  border-radius: var(--radius-md);
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}

.legend-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Code Detail View */
.code-detail {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-xl);
}

.detail-header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-lg);
  margin-bottom: var(--space-xl);
  padding-bottom: var(--space-xl);
  border-bottom: 1px solid var(--border-primary);
}

.code-badge {
  font-family: var(--font-mono);
  font-size: 1.5rem;
  font-weight: 700;
  padding: var(--space-md) var(--space-lg);
  border-radius: var(--radius-lg);
  flex-shrink: 0;
}

.detail-title-area h1 {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: var(--space-sm);
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
}

.meta-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 4px 10px;
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: var(--radius-full);
}

.meta-badge.chapter {
  background: var(--accent-glow);
  color: var(--accent-secondary);
}

.meta-badge.section {
  background: rgba(236, 72, 153, 0.15);
  color: var(--chapter-14);
}

.meta-badge.billable {
  background: rgba(34, 197, 94, 0.15);
  color: var(--includes);
}

.meta-badge.category {
  background: rgba(245, 158, 11, 0.15);
  color: var(--chapter-4);
}

/* Detail Content */
.detail-content {
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
}

.detail-section {
  background: var(--bg-glass);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.detail-section-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  background: var(--bg-tertiary);
  font-size: 0.875rem;
  font-weight: 600;
}

.detail-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.detail-icon svg {
  width: 100%;
  height: 100%;
}

.detail-section-header.excludes1 {
  border-left: 4px solid var(--excludes1);
}

.detail-section-header.excludes1 .detail-icon {
  color: var(--excludes1);
}

.detail-section-header.excludes2 {
  border-left: 4px solid var(--excludes2);
}

.detail-section-header.excludes2 .detail-icon {
  color: var(--excludes2);
}

.detail-section-header.includes {
  border-left: 4px solid var(--includes);
}

.detail-section-header.includes .detail-icon {
  color: var(--includes);
}

.detail-section-header.code-first {
  border-left: 4px solid var(--code-first);
}

.detail-section-header.code-first .detail-icon {
  color: var(--code-first);
}

.detail-section-header.use-additional {
  border-left: 4px solid var(--use-additional);
}

.detail-section-header.use-additional .detail-icon {
  color: var(--use-additional);
}

.detail-section-header.code-also {
  border-left: 4px solid var(--code-also);
}

.detail-section-header.code-also .detail-icon {
  color: var(--code-also);
}

.detail-section-content {
  padding: var(--space-lg);
}

.detail-section-content ul {
  list-style: none;
}

.detail-section-content li {
  padding: var(--space-xs) 0;
  padding-left: var(--space-lg);
  position: relative;
  font-size: 0.9375rem;
  color: var(--text-secondary);
}

.detail-section-content li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.75rem;
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
}

/* List View */
.list-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg) var(--space-xl);
  border-bottom: 1px solid var(--border-primary);
}

.list-header h2 {
  font-size: 1.25rem;
  font-weight: 700;
}

.list-actions {
  display: flex;
  gap: var(--space-sm);
}

.list-action-btn {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.list-action-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.list-action-btn svg {
  width: 14px;
  height: 14px;
}

/* Code Tree */
.code-tree {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

.tree-item {
  border-radius: var(--radius-md);
  margin-bottom: 2px;
}

.tree-item-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  transition: background var(--transition-fast);
  border-radius: var(--radius-md);
}

.tree-item-header:hover {
  background: var(--bg-tertiary);
}

.tree-toggle {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  flex-shrink: 0;
  transition: transform var(--transition-fast);
}

.tree-toggle.expanded {
  transform: rotate(90deg);
}

.tree-toggle svg {
  width: 12px;
  height: 12px;
}

.tree-toggle.empty {
  opacity: 0;
}

.tree-code {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  cursor: pointer;
  transition: transform var(--transition-fast),
    box-shadow var(--transition-fast);
}

.tree-code:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.tree-desc {
  flex: 1;
  font-size: 0.875rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.tree-desc:hover {
  color: var(--accent-primary);
}

.tree-badges {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.tree-badge {
  font-size: 0.625rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.tree-badge.billable {
  background: rgba(34, 197, 94, 0.15);
  color: var(--includes);
}

.tree-badge.has-notes {
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent-secondary);
}

.tree-children {
  padding-left: var(--space-xl);
  display: none;
}

.tree-children.expanded {
  display: block;
}

/* Code depth indicators */
.tree-code.depth-3 {
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent-secondary);
}
.tree-code.depth-4 {
  background: rgba(99, 102, 241, 0.3);
}
.tree-code.depth-5 {
  background: rgba(99, 102, 241, 0.4);
}
.tree-code.depth-6 {
  background: rgba(99, 102, 241, 0.5);
}
.tree-code.depth-7 {
  background: rgba(99, 102, 241, 0.6);
}

/* Section notes and subsections container */
.section-notes-container {
  margin-bottom: var(--space-md);
  padding: var(--space-md);
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  border-left: 4px solid var(--accent-primary);
}

.section-notes-container .note-group {
  margin-bottom: var(--space-sm);
}

.section-notes-container .note-group:last-child {
  margin-bottom: 0;
}

.section-notes-container .note-title {
  font-weight: 600;
  margin-bottom: var(--space-xs);
  font-size: 0.875rem;
}

.section-notes-container ul {
  margin: 0;
  padding-left: var(--space-lg);
  color: var(--text-secondary);
  font-size: 0.875rem;
}

.section-notes-container li {
  margin-bottom: var(--space-xs);
}

.section-subsections-notice {
  margin-bottom: var(--space-md);
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  border-left: 4px solid var(--accent-secondary);
}

.section-subsections-notice .subsection-info {
  color: var(--text-secondary);
  font-size: 0.875rem;
  font-style: italic;
  margin: 0;
}

.subsection-item {
  border-left: 2px solid var(--accent-secondary);
  margin-left: var(--space-md);
  padding-left: var(--space-sm);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal);
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  background: var(--bg-elevated);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg);
  border-bottom: 1px solid var(--border-primary);
}

.modal-header h2 {
  font-size: 1.125rem;
  font-weight: 600;
}

.modal-close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.modal-close:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.modal-close svg {
  width: 18px;
  height: 18px;
}

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-lg);
}

/* ============================================
   RESPONSIVE STYLES
   ============================================ */

/* Mobile Menu Button */
.mobile-menu-btn {
  display: none;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  color: var(--text-primary);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.mobile-menu-btn:hover {
  background: var(--bg-secondary);
  border-color: var(--accent-primary);
}

.mobile-menu-btn svg {
  width: 20px;
  height: 20px;
}

/* Mobile Overlay */
.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: calc(var(--z-dropdown) - 1);
  opacity: 0;
  transition: opacity var(--transition-base);
}

.mobile-overlay.active {
  opacity: 1;
}

/* Tablet Breakpoint (1024px) */
@media (max-width: 1024px) {
  .sidebar {
    width: 280px;
  }

  .search-shortcuts {
    display: none;
  }

  .header-center {
    flex: 1;
    max-width: none;
    margin: 0 var(--space-md);
  }

  .special-table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .special-table {
    min-width: 800px;
  }
}

/* Mobile Breakpoint (768px) */
@media (max-width: 768px) {
  :root {
    --header-height: 56px;
  }

  /* Header Mobile */
  .header {
    padding: 0 var(--space-sm);
    gap: var(--space-xs);
  }

  .header-left {
    gap: var(--space-xs);
  }

  .mobile-menu-btn {
    display: flex;
  }

  .logo-text {
    display: none;
  }

  .logo-icon {
    width: 32px;
    height: 32px;
  }

  .header-center {
    flex: 1;
    margin: 0 var(--space-xs);
  }

  .search-container {
    max-width: none;
  }

  .search-input {
    padding: var(--space-sm) var(--space-sm) var(--space-sm) 36px;
    font-size: 0.875rem;
  }

  .search-icon {
    left: var(--space-sm);
    width: 16px;
    height: 16px;
  }

  .header-right {
    gap: var(--space-xs);
  }

  .header-btn {
    width: 36px;
    height: 36px;
  }

  .header-btn svg {
    width: 18px;
    height: 18px;
  }

  /* Sidebar Mobile */
  .sidebar {
    position: fixed;
    left: -100%;
    top: var(--header-height);
    width: 85%;
    max-width: 320px;
    height: calc(100vh - var(--header-height));
    z-index: var(--z-dropdown);
    transition: left var(--transition-base);
    border-right: 1px solid var(--border-primary);
  }

  .sidebar.open {
    left: 0;
  }

  .mobile-overlay {
    display: block;
    top: var(--header-height);
    pointer-events: none;
  }

  .mobile-overlay.active {
    pointer-events: auto;
  }

  /* Sidebar Tabs Mobile */
  .sidebar-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: var(--space-xs);
    gap: var(--space-xs);
  }

  .sidebar-tabs::-webkit-scrollbar {
    display: none;
  }

  .sidebar-tab {
    flex: 0 0 auto;
    padding: var(--space-xs) var(--space-sm);
    font-size: 0.75rem;
    white-space: nowrap;
  }

  .sidebar-tab svg {
    width: 14px;
    height: 14px;
  }

  /* Main Content Mobile */
  .main-content {
    grid-template-columns: 1fr;
  }

  .content-area {
    padding: var(--space-sm);
  }

  /* Breadcrumb Mobile */
  .breadcrumb {
    padding: var(--space-xs) var(--space-sm);
    font-size: 0.75rem;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  .breadcrumb::-webkit-scrollbar {
    display: none;
  }

  .breadcrumb-item {
    padding: 4px 8px;
  }

  .breadcrumb-item svg {
    width: 12px;
    height: 12px;
  }

  /* Welcome Screen Mobile */
  .welcome-screen {
    padding: var(--space-md);
  }

  .welcome-hero {
    padding: var(--space-lg) var(--space-md);
    margin-bottom: var(--space-md);
  }

  .welcome-icon {
    width: 60px;
    height: 60px;
  }

  .welcome-icon svg {
    width: 28px;
    height: 28px;
  }

  .welcome-hero h1 {
    font-size: 1.5rem;
  }

  .welcome-hero p {
    font-size: 0.8125rem;
  }

  /* Stats Cards Mobile */
  .quick-stats {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-sm);
  }

  .stat-card {
    padding: var(--space-md);
  }

  .stat-icon {
    width: 36px;
    height: 36px;
    margin-bottom: var(--space-xs);
  }

  .stat-icon svg {
    width: 16px;
    height: 16px;
  }

  .stat-value {
    font-size: 1.25rem;
  }

  .stat-label {
    font-size: 0.6875rem;
  }

  /* Quick Actions Mobile */
  .quick-actions h3,
  .color-legend h3 {
    font-size: 0.9375rem;
    margin-bottom: var(--space-sm);
  }

  .action-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-sm);
  }

  .action-card {
    padding: var(--space-sm);
  }

  .action-icon {
    width: 36px;
    height: 36px;
    margin-bottom: var(--space-xs);
  }

  .action-info h4 {
    font-size: 0.75rem;
  }

  .action-info p {
    font-size: 0.625rem;
  }

  /* Color Legend Mobile */
  .legend-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-xs);
  }

  .legend-item {
    padding: var(--space-xs);
  }

  .legend-color {
    width: 10px;
    height: 10px;
  }

  .legend-label {
    font-size: 0.625rem;
  }

  /* Code Detail Mobile */
  .code-detail {
    padding: var(--space-md);
  }

  .detail-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-sm);
  }

  .code-badge {
    font-size: 1.25rem;
    padding: var(--space-xs) var(--space-sm);
  }

  .detail-title-area h1 {
    font-size: 1.125rem;
  }

  .detail-meta {
    flex-wrap: wrap;
    gap: var(--space-xs);
  }

  .meta-badge {
    font-size: 0.625rem;
    padding: 2px 6px;
  }

  .detail-section {
    padding: var(--space-sm);
    margin-bottom: var(--space-sm);
  }

  .section-title {
    font-size: 0.75rem;
    margin-bottom: var(--space-xs);
  }

  .section-content li {
    font-size: 0.8125rem;
    padding: var(--space-xs) 0;
  }

  /* List View Mobile */
  .list-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
  }

  .list-header h2 {
    font-size: 1rem;
  }

  .list-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .list-action-btn {
    font-size: 0.75rem;
    padding: var(--space-xs) var(--space-sm);
  }

  /* Tree View Mobile */
  .tree-item-header {
    padding: var(--space-xs) var(--space-sm);
    gap: var(--space-xs);
  }

  .tree-toggle {
    width: 20px;
    height: 20px;
  }

  .tree-code {
    font-size: 0.6875rem;
    padding: 2px 6px;
  }

  .tree-desc {
    font-size: 0.75rem;
  }

  .tree-badges {
    display: none;
  }

  .tree-children {
    padding-left: var(--space-md);
  }

  /* Table Views Mobile */
  .table-view {
    padding: var(--space-sm);
  }

  .table-view-header {
    margin-bottom: var(--space-sm);
  }

  .table-view-header h2 {
    font-size: 1.125rem;
  }

  .table-view-desc {
    font-size: 0.75rem;
  }

  /* Special Tables Mobile */
  .special-table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(-1 * var(--space-sm));
    padding: 0 var(--space-sm);
  }

  .special-table {
    min-width: 600px;
    font-size: 0.75rem;
  }

  .special-table th {
    padding: var(--space-xs) var(--space-sm);
    font-size: 0.625rem;
  }

  .special-table td {
    padding: var(--space-xs) var(--space-sm);
  }

  .code-cell {
    font-size: 0.625rem;
    padding: 2px 4px;
  }

  .site-text {
    font-size: 0.75rem;
  }

  /* Alpha Navigation Mobile */
  .alpha-nav {
    gap: 2px;
    padding: var(--space-xs);
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
  }

  .alpha-nav::-webkit-scrollbar {
    display: none;
  }

  .alpha-btn {
    min-width: 28px;
    height: 28px;
    font-size: 0.6875rem;
    flex-shrink: 0;
  }

  /* External Index Mobile */
  .external-letter-section {
    margin-bottom: var(--space-md);
  }

  .external-letter-header {
    padding: var(--space-sm);
    gap: var(--space-sm);
  }

  .external-letter {
    width: 32px;
    height: 32px;
    font-size: 1rem;
  }

  .external-letter-title {
    font-size: 0.75rem;
  }

  /* External causes mobile overflow fix */
  .external-index-container {
    padding: var(--space-xs);
    overflow-x: hidden;
    width: 100%;
    box-sizing: border-box;
  }

  .external-letter-section {
    width: 100%;
    overflow: hidden;
  }

  .external-term-item.main-term {
    padding: var(--space-sm);
    overflow: hidden;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
  }

  .external-term-item.sub-term {
    margin-left: 0 !important;
    padding-left: var(--space-sm);
    border-left-width: 2px;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
  }

  /* Use only border-left for hierarchy indication on mobile - no margin */
  .external-term-item.sub-term.level-2,
  .external-term-item.sub-term.level-3,
  .external-term-item.sub-term.level-4 {
    margin-left: 0 !important;
    border-left-color: var(--chapter-20);
  }

  .external-term-item.sub-term.level-3,
  .external-term-item.sub-term.level-4 {
    border-left-style: dashed;
  }

  /* Ensure nested containers don't overflow */
  .external-children {
    width: 100%;
    overflow: hidden;
    padding-left: var(--space-xs);
  }

  .external-term-content {
    gap: var(--space-xs);
    width: 100%;
    max-width: 100%;
    overflow: hidden;
  }

  .external-term-icon {
    width: 16px;
    height: 16px;
  }

  .ext-main-icon {
    width: 14px;
    height: 14px;
  }

  .ext-sub-icon {
    width: 12px;
    height: 12px;
  }

  .external-term-title {
    font-size: 0.8125rem;
    min-width: 0;
    flex: 1 1 auto;
    word-break: break-word;
    overflow-wrap: break-word;
    max-width: calc(100% - 30px);
  }

  .external-term-title.main-term-title {
    font-size: 0.875rem;
  }

  .external-term-content {
    flex-wrap: wrap;
    min-width: 0;
    display: flex;
    align-items: flex-start;
  }

  .external-term-icon {
    flex-shrink: 0;
  }

  .external-see,
  .external-see-also {
    font-size: 0.6875rem;
    padding: 1px 6px;
  }

  .external-see .ref-icon,
  .external-see-also .ref-icon {
    width: 10px;
    height: 10px;
  }

  .external-term-code {
    font-size: 0.625rem;
    padding: 2px 6px;
  }

  .external-children {
    margin-top: var(--space-xs);
  }

  /* Modal Mobile */
  .modal-content {
    width: 95%;
    max-width: none;
    max-height: 90vh;
    margin: var(--space-md);
  }

  .modal-header {
    padding: var(--space-sm) var(--space-md);
  }

  .modal-header h2 {
    font-size: 1rem;
  }

  .modal-body {
    padding: var(--space-md);
  }

  /* Search Results Mobile */
  .search-results {
    max-height: 60vh;
    width: calc(100vw - var(--space-md) * 2);
    left: 50%;
    transform: translateX(-50%);
  }

  .search-result-item {
    padding: var(--space-sm);
  }

  .result-code {
    font-size: 0.75rem;
  }

  .result-desc {
    font-size: 0.8125rem;
  }

  .result-chapter {
    font-size: 0.625rem;
  }
}

/* Small Mobile (480px) */
@media (max-width: 480px) {
  .header {
    padding: 0 var(--space-xs);
  }

  .search-input::placeholder {
    font-size: 0.75rem;
  }

  .quick-stats {
    grid-template-columns: 1fr 1fr;
  }

  .stat-card {
    padding: var(--space-sm);
  }

  .stat-value {
    font-size: 1rem;
  }

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

  .legend-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .special-table {
    min-width: 500px;
  }

  .alpha-btn {
    min-width: 24px;
    height: 24px;
    font-size: 0.625rem;
  }

  .sidebar {
    width: 90%;
  }

  .sidebar-tab {
    padding: 6px 8px;
    font-size: 0.6875rem;
  }

  .sidebar-tab svg {
    display: none;
  }
}

/* Landscape Phone */
@media (max-width: 768px) and (orientation: landscape) {
  :root {
    --header-height: 48px;
  }

  .welcome-hero {
    padding: var(--space-md);
  }

  .welcome-icon {
    width: 48px;
    height: 48px;
  }

  .welcome-hero h1 {
    font-size: 1.25rem;
  }

  .quick-stats {
    grid-template-columns: repeat(4, 1fr);
  }

  .sidebar {
    width: 50%;
    max-width: 280px;
  }
}

/* Touch Device Optimizations */
@media (hover: none) and (pointer: coarse) {
  .tree-item-header {
    min-height: 44px;
  }

  .chapter-item {
    min-height: 56px;
  }

  .sidebar-tab {
    min-height: 44px;
  }

  .alpha-btn {
    min-width: 36px;
    min-height: 36px;
  }

  .search-result-item {
    min-height: 56px;
  }

  .header-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .mobile-menu-btn {
    min-width: 44px;
    min-height: 44px;
  }
}

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

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

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

.animate-fade-in {
  animation: fadeIn var(--transition-base) ease-out;
}

.animate-slide-up {
  animation: slideUp var(--transition-slow) ease-out;
}

.animate-slide-in {
  animation: slideIn var(--transition-slow) ease-out;
}

/* Scrollbar styling for main areas */
.content-area::-webkit-scrollbar,
.code-tree::-webkit-scrollbar,
.welcome-screen::-webkit-scrollbar,
.code-detail::-webkit-scrollbar,
.modal-body::-webkit-scrollbar {
  width: 8px;
}

.content-area::-webkit-scrollbar-track,
.code-tree::-webkit-scrollbar-track,
.welcome-screen::-webkit-scrollbar-track,
.code-detail::-webkit-scrollbar-track,
.modal-body::-webkit-scrollbar-track {
  background: transparent;
}

.content-area::-webkit-scrollbar-thumb,
.code-tree::-webkit-scrollbar-thumb,
.welcome-screen::-webkit-scrollbar-thumb,
.code-detail::-webkit-scrollbar-thumb,
.modal-body::-webkit-scrollbar-thumb {
  background: var(--border-primary);
  border-radius: var(--radius-full);
}

.content-area::-webkit-scrollbar-thumb:hover,
.code-tree::-webkit-scrollbar-thumb:hover,
.welcome-screen::-webkit-scrollbar-thumb:hover,
.code-detail::-webkit-scrollbar-thumb:hover,
.modal-body::-webkit-scrollbar-thumb:hover {
  background: var(--border-accent);
}

/* ========================================
   Sidebar Tabs & Special Tables
   ======================================== */

/* Sidebar Tabs */
.sidebar-tabs {
  display: flex;
  padding: var(--space-xs);
  gap: 2px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border-primary);
}

.sidebar-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--space-sm) var(--space-xs);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  font-size: 0.625rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.sidebar-tab svg {
  width: 16px;
  height: 16px;
}

.sidebar-tab:hover {
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.sidebar-tab.active {
  background: var(--accent-primary);
  color: white;
}

/* Sidebar Panels */
.sidebar-panel {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.sidebar-panel.active {
  display: flex;
}

/* Sidebar Search */
.sidebar-search {
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--border-primary);
}

.sidebar-search-input {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 0.8125rem;
  color: var(--text-primary);
  outline: none;
  transition: all var(--transition-fast);
}

.sidebar-search-input::placeholder {
  color: var(--text-muted);
}

.sidebar-search-input:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Alpha Navigation */
.alpha-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--border-primary);
}

.alpha-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6875rem;
  font-weight: 600;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.alpha-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.alpha-btn.active {
  background: var(--accent-primary);
  color: white;
}

.alpha-btn.disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* Table Views */
.table-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.table-view-header {
  padding: var(--space-xl);
  border-bottom: 1px solid var(--border-primary);
}

.table-view-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: var(--space-xs);
}

.table-view-desc {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

/* Special Table Container */
.special-table-container {
  flex: 1;
  overflow: auto;
  padding: var(--space-md);
}

.special-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.8125rem;
}

.special-table th {
  position: sticky;
  top: 0;
  padding: var(--space-md);
  background: var(--bg-elevated);
  border-bottom: 2px solid var(--border-primary);
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
  z-index: 10;
}

.special-table td {
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--border-secondary);
  vertical-align: top;
}

.special-table tbody tr:hover {
  background: var(--bg-glass-hover);
}

/* Neoplasm Table Specific */
.neoplasm-table .col-site {
  min-width: 250px;
}

.neoplasm-table .col-malignant-primary {
  background: rgba(220, 38, 38, 0.1);
  color: #ef4444;
}
.neoplasm-table .col-malignant-secondary {
  background: rgba(239, 68, 68, 0.1);
  color: #f87171;
}
.neoplasm-table .col-ca-in-situ {
  background: rgba(249, 115, 22, 0.1);
  color: #f97316;
}
.neoplasm-table .col-benign {
  background: rgba(34, 197, 94, 0.1);
  color: #22c55e;
}
.neoplasm-table .col-uncertain {
  background: rgba(234, 179, 8, 0.1);
  color: #eab308;
}
.neoplasm-table .col-unspecified {
  background: rgba(148, 163, 184, 0.1);
  color: #94a3b8;
}

/* Drug Table Specific */
.drugs-table .col-substance {
  min-width: 250px;
}

.drugs-table .col-accidental {
  background: rgba(234, 179, 8, 0.1);
  color: #eab308;
}
.drugs-table .col-self-harm {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
}
.drugs-table .col-assault {
  background: rgba(168, 85, 247, 0.1);
  color: #a855f7;
}
.drugs-table .col-undetermined {
  background: rgba(148, 163, 184, 0.1);
  color: #94a3b8;
}
.drugs-table .col-adverse {
  background: rgba(249, 115, 22, 0.1);
  color: #f97316;
}
.drugs-table .col-underdosing {
  background: rgba(6, 182, 212, 0.1);
  color: #06b6d4;
}

/* ============================================
   Alphabetical Index Styles
   ============================================ */

/* Index View Container */
.index-view-container {
  padding: var(--space-xl);
}

.index-instructions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-lg);
  margin-bottom: var(--space-xl);
}

.instruction-card {
  display: flex;
  align-items: flex-start;
  gap: var(--space-md);
  padding: var(--space-lg);
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-primary);
}

.instruction-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary)
  );
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.instruction-icon svg {
  width: 20px;
  height: 20px;
  color: white;
}

.instruction-text h4 {
  font-weight: 600;
  margin-bottom: var(--space-xs);
  color: var(--text-primary);
}

.instruction-text p {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin: 0;
}

.index-legend {
  padding: var(--space-lg);
  background: var(--bg-tertiary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-primary);
}

.index-legend h4 {
  font-weight: 600;
  margin-bottom: var(--space-md);
  color: var(--text-primary);
}

.legend-items {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-lg);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: 0.875rem;
}

.legend-code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent-primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.legend-see {
  font-style: italic;
  color: var(--code-also);
  padding: 2px 8px;
  background: rgba(139, 92, 246, 0.1);
  border-radius: var(--radius-sm);
}

.legend-see-also {
  font-style: italic;
  color: var(--use-additional);
  padding: 2px 8px;
  background: rgba(16, 185, 129, 0.1);
  border-radius: var(--radius-sm);
}

/* Index Main Area Navigation */
.index-main-nav {
  margin-top: var(--space-xl);
  padding: var(--space-lg);
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-primary);
}

.index-nav-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-md);
  margin-bottom: var(--space-md);
}

.index-nav-header h4 {
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.index-search-main {
  flex: 1;
  max-width: 400px;
  min-width: 200px;
}

.index-search-main input {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  color: var(--text-primary);
  transition: all var(--transition-fast);
}

.index-search-main input:focus {
  outline: none;
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.index-search-main input::placeholder {
  color: var(--text-muted);
}

.alpha-nav-main {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.alpha-btn-main {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.alpha-btn-main:hover:not(.disabled) {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: white;
  transform: scale(1.05);
}

.alpha-btn-main.active {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: white;
}

.alpha-btn-main.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Index Entries Container */
.index-entries-container {
  margin-top: var(--space-xl);
}

.index-letter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-lg);
  padding-bottom: var(--space-md);
  border-bottom: 2px solid var(--border-primary);
}

.index-letter-header h3 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.index-letter-count {
  font-size: 0.875rem;
  color: var(--text-secondary);
  background: var(--bg-tertiary);
  padding: var(--space-xs) var(--space-md);
  border-radius: var(--radius-full);
}

.index-entries-grid {
  display: grid;
  gap: var(--space-sm);
}

/* Index Sidebar Info */
.index-sidebar-info {
  padding: var(--space-md);
  font-size: 0.875rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

/* Index Sidebar List Items */
.index-item {
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: all var(--transition-fast);
}

.index-item:hover {
  border-color: var(--border-secondary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.index-item-header,
.index-subterm-header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  cursor: pointer;
  transition: background var(--transition-fast);
}

.index-item-header:hover,
.index-subterm-header:hover {
  background: var(--bg-tertiary);
}

.index-toggle {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: transform var(--transition-fast);
  padding: 0;
  margin-top: 2px;
}

.index-toggle svg {
  width: 14px;
  height: 14px;
}

.index-toggle-placeholder {
  width: 20px;
  flex-shrink: 0;
}

.index-item.expanded > .index-item-header .index-toggle,
.index-subterm.expanded > .index-subterm-header .index-toggle {
  transform: rotate(90deg);
}

.index-title {
  flex: 1;
  font-weight: 600;
  font-size: 0.9375rem;
  color: var(--text-primary);
  word-break: break-word;
}

.index-subtitle {
  flex: 1;
  font-size: 0.875rem;
  color: var(--text-primary);
  word-break: break-word;
}

.index-code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent-primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--transition-fast);
}

.index-code:hover {
  background: var(--accent-primary);
  color: white;
  transform: scale(1.05);
}

/* Index icons */
.index-term-icon,
.index-subterm-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.index-term-icon .term-icon {
  width: 16px;
  height: 16px;
  color: var(--accent-primary);
}

.index-subterm-icon .subterm-icon {
  width: 14px;
  height: 14px;
  color: var(--text-muted);
}

.index-see,
.index-see-also {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.8125rem;
  font-style: italic;
  flex-shrink: 0;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
}

.index-see {
  color: var(--code-also);
  background: rgba(139, 92, 246, 0.1);
}

.index-see-also {
  color: var(--use-additional);
  background: rgba(16, 185, 129, 0.1);
}

.ref-icon {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
}

.index-children {
  display: none;
  padding: var(--space-md);
  padding-left: var(--space-xl);
  border-top: 1px solid var(--border-secondary);
  background: var(--bg-tertiary);
}

.index-item.expanded > .index-children,
.index-subterm.expanded > .index-children {
  display: block;
}

.index-subterm {
  border-bottom: none;
}

.index-subterm.level-1 {
  /* First level indent */
}

.index-subterm.level-2 .index-subtitle {
  color: var(--text-secondary);
}

.index-subterm.level-3 .index-subtitle {
  color: var(--text-muted);
  font-size: 0.8125rem;
}

/* Loading and empty states */
.loading-entries {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-xl);
  color: var(--text-secondary);
}

.no-entries {
  text-align: center;
  padding: var(--space-xl);
  color: var(--text-secondary);
}

.search-results-count {
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: var(--space-md);
}

/* Code Cell */
.code-cell {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 2px 6px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
  display: inline-block;
}

.code-cell:hover {
  background: var(--accent-primary);
  color: white;
}

.code-cell.empty {
  color: var(--text-muted);
  background: transparent;
  cursor: default;
}

/* Site Name with Indentation */
.site-name {
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
}

.site-indent {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.site-text {
  color: var(--text-primary);
}

.site-text.level-0 {
  font-weight: 600;
}
.site-text.level-1 {
  padding-left: var(--space-md);
}
.site-text.level-2 {
  padding-left: var(--space-xl);
}
.site-text.level-3 {
  padding-left: var(--space-2xl);
}
.site-text.level-4 {
  padding-left: var(--space-3xl);
}

/* External Index Container */
.external-index-container {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-md);
}

.external-letter-section {
  margin-bottom: var(--space-xl);
}

.external-letter-header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md);
  background: var(--bg-elevated);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-md);
  z-index: 5;
}

.external-letter {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--chapter-20), var(--chapter-19));
  color: white;
  border-radius: var(--radius-md);
}

.external-letter-title {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.external-term-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.external-term-item {
  border-radius: var(--radius-md);
  transition: background var(--transition-fast);
}

.external-term-item.main-term {
  background: var(--bg-secondary);
  border: 1px solid var(--border-primary);
  padding: var(--space-md);
  margin-bottom: var(--space-xs);
}

.external-term-item.main-term:hover {
  border-color: var(--border-secondary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.external-term-item.sub-term {
  padding: var(--space-xs) 0;
  border-left: 2px solid var(--border-secondary);
  margin-left: var(--space-md);
  padding-left: var(--space-md);
}

.external-term-item.sub-term.level-2 {
  margin-left: var(--space-lg);
}

.external-term-item.sub-term.level-3 {
  margin-left: var(--space-xl);
}

.external-term-item.sub-term.level-4 {
  margin-left: calc(var(--space-xl) + var(--space-md));
}

.external-term-content {
  display: flex;
  align-items: flex-start;
  gap: var(--space-md);
  flex-wrap: wrap;
}

.external-term-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ext-main-icon {
  width: 18px;
  height: 18px;
  color: var(--chapter-20);
}

.ext-sub-icon {
  width: 14px;
  height: 14px;
  color: var(--text-muted);
}

.external-term-title {
  flex: 1;
  font-size: 0.875rem;
  min-width: 150px;
}

.external-term-title.main-term-title {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text-primary);
}

.external-term-title.sub-term-title {
  font-weight: 400;
  color: var(--text-secondary);
}

.external-see,
.external-see-also {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-style: italic;
  font-weight: 400;
  font-size: 0.8125rem;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
}

.external-see {
  color: var(--code-also);
  background: rgba(139, 92, 246, 0.1);
}

.external-see-also {
  color: var(--use-additional);
  background: rgba(16, 185, 129, 0.1);
}

.external-see .ref-icon,
.external-see-also .ref-icon {
  width: 12px;
  height: 12px;
}

.external-term-code {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 10px;
  background: rgba(163, 230, 53, 0.15);
  color: var(--chapter-20);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.external-term-code:hover {
  background: var(--chapter-20);
  color: white;
  transform: scale(1.05);
}

.external-children {
  margin-top: var(--space-sm);
}

/* Sidebar list items for special tables */
.sidebar-list-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast);
}

.sidebar-list-item:hover {
  background: var(--bg-tertiary);
}

.sidebar-list-item.active {
  background: var(--accent-glow);
}

.sidebar-list-icon {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-list-text {
  flex: 1;
  font-size: 0.8125rem;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Loading indicator for tables */
.table-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-3xl);
  color: var(--text-secondary);
}

.table-loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-primary);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: var(--space-md);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
/* ============================================
   MOBILE BOTTOM NAVIGATION
   ============================================ */

.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: var(--bg-glass);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-top: 1px solid var(--border-primary);
  z-index: var(--z-sticky);
  padding: var(--space-xs) var(--space-sm);
  padding-bottom: env(safe-area-inset-bottom, var(--space-xs));
}

.mobile-nav-items {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100%;
}

.mobile-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: var(--space-xs);
  border-radius: var(--radius-md);
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  cursor: pointer;
  transition: all var(--transition-fast);
  min-width: 60px;
}

.mobile-nav-item svg {
  width: 22px;
  height: 22px;
}

.mobile-nav-item span {
  font-size: 0.625rem;
  font-weight: 500;
}

.mobile-nav-item.active {
  color: var(--accent-primary);
}

.mobile-nav-item:hover {
  color: var(--text-primary);
  background: var(--bg-tertiary);
}

/* Scroll Hint Indicator */
.scroll-hint {
  position: relative;
}

.scroll-hint::after {
  content: "→ Scroll";
  position: absolute;
  right: var(--space-sm);
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.625rem;
  color: var(--text-tertiary);
  background: var(--bg-secondary);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--transition-fast);
  animation: fadeInOut 3s ease-in-out;
}

@keyframes fadeInOut {
  0%,
  100% {
    opacity: 0;
  }
  20%,
  80% {
    opacity: 1;
  }
}

/* Safe area support for notched phones */
@supports (padding: env(safe-area-inset-bottom)) {
  .mobile-bottom-nav {
    padding-bottom: calc(var(--space-xs) + env(safe-area-inset-bottom));
  }

  @media (max-width: 768px) {
    .sidebar {
      padding-bottom: calc(64px + env(safe-area-inset-bottom));
    }

    .content-area {
      padding-bottom: calc(
        64px + env(safe-area-inset-bottom) + var(--space-md)
      );
    }
  }
}

@media (max-width: 768px) {
  .mobile-bottom-nav {
    display: block;
  }

  .content-area {
    padding-bottom: calc(64px + var(--space-md));
  }

  .sidebar {
    padding-bottom: 72px;
  }
}

/* Pull to refresh indicator (visual only) */
.pull-indicator {
  display: none;
  position: fixed;
  top: var(--header-height);
  left: 50%;
  transform: translateX(-50%);
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-elevated);
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-tooltip);
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Improve tap targets for mobile */
@media (max-width: 768px) {
  .chapter-item {
    min-height: 60px;
  }

  .sidebar-list-item {
    min-height: 44px;
  }

  .search-result-item {
    min-height: 60px;
  }
}

/* ========================================
   AI Assistant Styles - Enhanced UI/UX
   ======================================== */

/* Custom Scrollbar for AI Chat */
.ai-messages::-webkit-scrollbar {
  width: 8px;
}

.ai-messages::-webkit-scrollbar-track {
  background: transparent;
}

.ai-messages::-webkit-scrollbar-thumb {
  background: var(--accent-primary-alpha);
  border-radius: var(--radius-full);
  border: 2px solid transparent;
  background-clip: padding-box;
}

.ai-messages::-webkit-scrollbar-thumb:hover {
  background: var(--accent-primary);
  border: 2px solid transparent;
  background-clip: padding-box;
}

.ai-view {
  height: 100%;
  padding: var(--space-lg);
  display: flex;
  flex-direction: column;
  max-width: 900px;
  margin: 0 auto;
  position: relative;
}

.ai-chat-container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--header-height) - var(--space-2xl));
  background: linear-gradient(145deg, var(--bg-elevated), var(--bg-secondary));
  border-radius: var(--radius-xl);
  border: 1px solid var(--border-color);
  overflow: hidden;
  position: relative;
  box-shadow: var(--shadow-xl), 0 0 60px -20px var(--accent-primary-alpha);
}

.ai-chat-header {
  padding: var(--space-lg) var(--space-xl);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, var(--bg-secondary), var(--bg-tertiary));
  backdrop-filter: blur(10px);
}

.ai-chat-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: linear-gradient(
    135deg,
    var(--text-primary) 0%,
    var(--accent-primary) 100%
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.ai-chat-header h2::before {
  content: "";
  display: inline-block;
  width: 28px;
  height: 28px;
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary, #a855f7)
  );
  border-radius: var(--radius-md);
  margin-right: var(--space-sm);
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2'%3E%3Cpath d='m12 13 1 2 2 1-2 1-1 2-1-2-2-1 2-1 1-2Z'/%3E%3Cpath d='m19 2 1 2 2 1-2 1-1 2-1-2-2-1 2-1 1-2Z'/%3E%3Cpath d='m5 6 2 4 4 2-4 2-2 4-2-4-4-2 4-2 2-4Z'/%3E%3C/svg%3E");
  mask-size: 60%;
  mask-position: center;
  mask-repeat: no-repeat;
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2'%3E%3Cpath d='m12 13 1 2 2 1-2 1-1 2-1-2-2-1 2-1 1-2Z'/%3E%3Cpath d='m19 2 1 2 2 1-2 1-1 2-1-2-2-1 2-1 1-2Z'/%3E%3Cpath d='m5 6 2 4 4 2-4 2-2 4-2-4-4-2 4-2 2-4Z'/%3E%3C/svg%3E");
  -webkit-mask-size: 60%;
  -webkit-mask-position: center;
  -webkit-mask-repeat: no-repeat;
}

.ai-model-badge {
  font-size: 0.7rem;
  font-weight: 500;
  padding: 4px 10px;
  background: linear-gradient(135deg, var(--accent-primary-alpha), transparent);
  color: var(--accent-primary);
  border-radius: var(--radius-full);
  border: 1px solid var(--accent-primary-alpha);
  letter-spacing: 0.02em;
}

.ai-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-xl);
  scroll-behavior: smooth;
  background: radial-gradient(
    ellipse at top,
    var(--bg-secondary) 0%,
    transparent 70%
  );
}

.message {
  display: flex;
  gap: var(--space-md);
  max-width: 88%;
  animation: messageSlideIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes messageSlideIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.ai-message {
  align-self: flex-start;
}

.user-message {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--shadow-md);
  transition: transform var(--transition-fast);
}

.message:hover .message-avatar {
  transform: scale(1.05);
}

.ai-message .message-avatar {
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary, #a855f7)
  );
  color: white;
}

.user-message .message-avatar {
  background: linear-gradient(135deg, var(--bg-tertiary), var(--bg-elevated));
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

.message-avatar svg {
  width: 20px;
  height: 20px;
}

.message-content {
  padding: var(--space-md) var(--space-lg);
  border-radius: var(--radius-lg);
  font-size: 0.9375rem;
  line-height: 1.7;
  box-shadow: var(--shadow-sm);
}

.ai-message .message-content {
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-top-left-radius: var(--radius-sm);
  color: var(--text-primary);
}

.user-message .message-content {
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary, #a855f7)
  );
  color: white;
  border-top-right-radius: var(--radius-sm);
  border: none;
}

.user-message .message-content p {
  margin: 0;
}

.ai-input-area {
  padding: var(--space-lg) var(--space-xl);
  background: linear-gradient(to top, var(--bg-secondary), var(--bg-tertiary));
  border-top: 1px solid var(--border-color);
}

.input-wrapper {
  display: flex;
  gap: var(--space-md);
  background: var(--bg-elevated);
  border: 2px solid var(--border-color);
  border-radius: var(--radius-xl);
  padding: var(--space-md) var(--space-lg);
  align-items: flex-end;
  transition: all var(--transition-base);
  box-shadow: var(--shadow-md), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.input-wrapper:focus-within {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 4px var(--accent-primary-alpha), var(--shadow-lg);
  background: var(--bg-primary);
}

#ai-user-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  resize: none;
  padding: var(--space-sm) 0;
  max-height: 140px;
  min-height: 24px;
  outline: none;
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.5;
}

#ai-user-input::placeholder {
  color: var(--text-tertiary);
}

#btn-send-ai {
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary, #a855f7)
  );
  color: white;
  border: none;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition-fast);
  box-shadow: var(--shadow-md);
  flex-shrink: 0;
}

#btn-send-ai svg {
  width: 20px;
  height: 20px;
  transition: transform var(--transition-fast);
}

#btn-send-ai:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow: var(--shadow-lg), 0 0 20px var(--accent-primary-alpha);
}

#btn-send-ai:hover svg {
  transform: translateX(2px) translateY(-2px);
}

#btn-send-ai:active {
  transform: scale(0.95);
}

#btn-send-ai:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.ai-footer {
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-tertiary);
  margin-top: var(--space-md);
  letter-spacing: 0.02em;
  opacity: 0.8;
}

/* API Key Overlay */
.ai-key-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  animation: overlayFadeIn 0.3s ease-out;
}

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

.ai-key-card {
  background: linear-gradient(145deg, var(--bg-elevated), var(--bg-secondary));
  padding: var(--space-2xl);
  border-radius: var(--radius-xl);
  max-width: 420px;
  width: 90%;
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-xl), 0 0 80px -20px var(--accent-primary-alpha);
  text-align: center;
  animation: cardSlideUp 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes cardSlideUp {
  from {
    opacity: 0;
    transform: translateY(30px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.ai-key-card h3 {
  margin: 0 0 var(--space-sm) 0;
  font-size: 1.5rem;
  background: linear-gradient(
    135deg,
    var(--text-primary) 0%,
    var(--accent-primary) 100%
  );
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.ai-key-card p {
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.6;
  margin-bottom: var(--space-lg);
}

.ai-key-card input {
  width: 100%;
  padding: var(--space-md) var(--space-lg);
  margin-bottom: var(--space-lg);
  background: var(--bg-primary);
  border: 2px solid var(--border-color);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: 0.9375rem;
  font-family: var(--font-mono);
  transition: all var(--transition-base);
  box-sizing: border-box;
}

.ai-key-card input:focus {
  outline: none;
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 4px var(--accent-primary-alpha);
}

.ai-key-card input::placeholder {
  color: var(--text-tertiary);
  font-family: inherit;
}

.key-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-md);
}

.key-actions .link-subtle {
  color: var(--text-secondary);
  font-size: 0.85rem;
  text-decoration: none;
  transition: color var(--transition-fast);
}

.key-actions .link-subtle:hover {
  color: var(--accent-primary);
  text-decoration: underline;
}

.key-actions .btn-primary {
  background: linear-gradient(
    135deg,
    var(--accent-primary),
    var(--accent-secondary, #a855f7)
  );
  color: white;
  border: none;
  padding: var(--space-md) var(--space-xl);
  border-radius: var(--radius-lg);
  font-weight: 600;
  font-size: 0.9375rem;
  cursor: pointer;
  transition: all var(--transition-fast);
  box-shadow: var(--shadow-md);
}

.key-actions .btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg), 0 0 20px var(--accent-primary-alpha);
}

/* Sidebar AI Styles */
.ai-sidebar-content {
  padding: var(--space-lg);
}

.ai-help-text {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: var(--space-md);
  font-weight: 500;
}

.ai-feature-list {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-xl) 0;
}

.ai-feature-list li {
  padding: var(--space-sm) var(--space-md);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: 0.875rem;
  color: var(--text-primary);
  background: var(--bg-primary);
  margin-bottom: var(--space-xs);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  transition: all var(--transition-fast);
}

.ai-feature-list li:hover {
  border-color: var(--accent-primary-alpha);
  background: var(--bg-elevated);
}

.ai-feature-list li::before {
  content: "✦";
  color: var(--accent-primary);
  font-size: 0.7rem;
}

.ai-settings-card {
  background: linear-gradient(145deg, var(--bg-primary), var(--bg-secondary));
  padding: var(--space-lg);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  margin-bottom: var(--space-lg);
}

.ai-settings-card h4 {
  margin: 0 0 var(--space-md) 0;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

.ai-settings-card .btn-secondary {
  width: 100%;
  padding: var(--space-md);
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.ai-settings-card .btn-secondary:hover {
  background: var(--accent-primary-alpha);
  border-color: var(--accent-primary);
  color: var(--accent-primary);
}

.ai-disclaimer {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  background: var(--bg-primary);
  padding: var(--space-md);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.ai-disclaimer::before {
  content: "⚠";
  font-size: 1rem;
}

.message-content p {
  margin: 0 0 var(--space-sm) 0;
}

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

.message-content h3 {
  font-size: 1rem;
  margin: 0 0 var(--space-sm) 0;
  color: var(--accent-primary);
}

.message-content ul,
.message-content ol {
  padding-left: 1.25rem;
  margin: var(--space-sm) 0;
}

.message-content li {
  margin-bottom: var(--space-xs);
}

.message-content code {
  background: rgba(127, 127, 127, 0.15);
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.85em;
  border: 1px solid rgba(127, 127, 127, 0.1);
}

.message-content pre {
  background: var(--bg-primary);
  padding: var(--space-md);
  border-radius: var(--radius-md);
  overflow-x: auto;
  border: 1px solid var(--border-color);
  margin: var(--space-sm) 0;
}

.message-content pre code {
  background: none;
  border: none;
  padding: 0;
}

.message-content strong {
  color: var(--accent-primary);
  font-weight: 600;
}

/* Loading State - Typing Indicator */
.message.loading .message-content {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background: var(--accent-primary);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) {
  animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typingBounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-8px);
    opacity: 1;
  }
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--accent-primary-alpha);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

@media (max-width: 768px) {
  .ai-view {
    padding: var(--space-sm);
  }
  .ai-chat-container {
    border-radius: var(--radius-lg);
    height: calc(100vh - var(--header-height) - 80px);
    box-shadow: none;
  }
  .ai-chat-header {
    padding: var(--space-md);
  }
  .ai-messages {
    padding: var(--space-md);
    gap: var(--space-md);
  }
  .message {
    max-width: 92%;
  }
  .message-avatar {
    width: 36px;
    height: 36px;
  }
  .ai-input-area {
    padding: var(--space-md);
  }
  .input-wrapper {
    padding: var(--space-sm) var(--space-md);
  }
  #btn-send-ai {
    width: 40px;
    height: 40px;
  }

  /* Index icons mobile */
  .index-term-icon,
  .index-subterm-icon {
    width: 14px;
    height: 14px;
  }

  .index-term-icon .term-icon {
    width: 12px;
    height: 12px;
  }

  .index-subterm-icon .subterm-icon {
    width: 10px;
    height: 10px;
  }

  .index-see,
  .index-see-also {
    font-size: 0.6875rem;
    padding: 1px 6px;
  }

  .ref-icon {
    width: 10px;
    height: 10px;
  }

  .index-item-header,
  .index-subterm-header {
    gap: var(--space-xs);
    padding: var(--space-sm);
  }

  .index-title {
    font-size: 0.875rem;
  }

  .index-subtitle {
    font-size: 0.8125rem;
  }

  .index-code {
    font-size: 0.625rem;
    padding: 2px 6px;
  }

  /* Detail section icons mobile */
  .detail-icon {
    width: 16px;
    height: 16px;
  }

  .detail-section-header {
    padding: var(--space-sm) var(--space-md);
    font-size: 0.8125rem;
  }
}

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