/* =========================================
   1. VARIABLES & THEME CONFIGURATION
   ========================================= */
:root {
  /* Brand Colors */
  --leo-primary: #4f46e5;
  --leo-primary-rgb: 79, 70, 229;
  --leo-secondary: #0f172a; /* Sidebar Dark */
  --leo-spotlight: #FFF01A;
  
  /* UI Colors */
  --leo-bg: #f7f8fb;
  --leo-surface: #ffffff;
  --leo-border: #e5e7eb;
  --leo-text-main: #111827;
  --leo-text-muted: #6b7280;
  --leo-text-light: #cbd5f5;

  /* Dimensions */
  --leo-navbar-height: 56px;
  --leo-sidebar-width: 280px;
}

/* =========================================
   2. GLOBAL BASE STYLES
   ========================================= */
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background-color: var(--leo-bg);
  color: var(--leo-text-main);
  overflow-x: hidden;
  padding-top: var(--leo-navbar-height);
}

a {
  text-decoration: none;
  transition: all 0.2s ease;
}

/* Utility: Development Flags */
.in_development {
  display: none !important;
}

/* =========================================
   3. LAYOUT STRUCTURE
   ========================================= */

.leo-app-layout {
  /* Min-height accounts for the viewport minus the header space */
  min-height: calc(100vh - var(--leo-navbar-height));
  display: flex;
  align-items: stretch;
}

/* Navbar Customizations */
.navbar {
  height: var(--leo-navbar-height);
  z-index: 1030; /* Stays above content */
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
  
  /* FIX: Fixed positioning for Desktop/Laptop */
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
}

.navbar-brand {
  font-weight: 700;
  letter-spacing: 0.5px;
}

.navbar input::placeholder {
  color: var(--leo-text-light);
  opacity: 0.8;
}

.navbar .form-control:focus {
  box-shadow: none;
  background-color: #374151;
}

/* =========================================
   4. SIDEBAR & NAVIGATION (Refactored UX)
   ========================================= */

#sidebar {
  background-color: var(--leo-secondary);
  color: white;
  
  /* UX: Sticky positioning relative to viewport */
  position: sticky;
  /* Top value = height of the fixed navbar */
  top: var(--leo-navbar-height);
  /* Height = Viewport - Navbar */
  height: calc(100vh - var(--leo-navbar-height));
  
  overflow-y: auto;
  border-right: 1px solid rgba(255, 255, 255, 0.05);
  transition: all 0.3s ease;
  
  /* Custom Scrollbar */
  scrollbar-width: thin;
  scrollbar-color: #334155 transparent;
}

/* Webkit Scrollbar Styling */
#sidebar::-webkit-scrollbar {
  width: 6px;
}
#sidebar::-webkit-scrollbar-track {
  background: transparent;
}
#sidebar::-webkit-scrollbar-thumb {
  background-color: #334155;
  border-radius: 20px;
}

/* Book Cover Styling */
.book-card img {
  max-width: 140px;
  border-radius: 8px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
  margin-bottom: 1rem;
}

#book-title {
  color: #fff;
  font-weight: 600;
  margin-bottom: 1.5rem;
  letter-spacing: 0.02em;
}

/* --- Chapter List Typography & Hierarchy --- */

.chapter-list > div > .fw-semibold {
  color: #94a3b8;
  text-transform: uppercase;
  font-size: 0.7rem !important;
  letter-spacing: 0.08em;
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
  padding-left: 0.5rem;
}

/* --- Section Links Interaction --- */

.chapter-list a {
  display: block;
  padding: 8px 12px 8px 16px;
  margin-bottom: 2px;
  border-radius: 0 6px 6px 0;
  color: #cbd5e1;
  font-size: 0.9rem;
  border-left: 3px solid transparent;
  line-height: 1.4;
}

.chapter-list a:hover {
  background: rgba(255, 255, 255, 0.04);
  color: #fff;
  border-left-color: rgba(255, 255, 255, 0.2);
  text-decoration: none;
}

.chapter-list a.active {
  background: linear-gradient(90deg, rgba(79, 70, 229, 0.15) 0%, transparent 100%);
  color: var(--leo-spotlight);
  border-left-color: var(--leo-spotlight);
  font-weight: 550;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* Mobile Sidebar Behavior */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    top: var(--leo-navbar-height);
    left: 0;
    bottom: 0;
    width: var(--leo-sidebar-width);
    z-index: 2000;
    border-right: 1px solid #334155;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
    height: calc(100vh - var(--leo-navbar-height)); /* Ensure it fills height on mobile too */
  }
}

/* =========================================
   5. READER AREA
   ========================================= */

#reader {
  background-color: var(--leo-bg);
  /* Ensure reader takes full available height if content is short */
  min-height: 100%; 
}

.reader-card {
  background: var(--leo-surface);
  border-radius: 16px;
  padding: 25px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.02), 0 10px 15px -3px rgba(0, 0, 0, 0.02);
  border: 1px solid var(--leo-border);
  max-width: 960px;
  margin: 0 auto;
}

.reader-card h1 {
  font-weight: 800;
  letter-spacing: -0.025em;
  color: #111827;
}

.reader-card p {
  font-size: 1.05rem;
  line-height: 1.75;
  color: #374151;
  margin-bottom: 1.5rem;
}

.reader-card img {
  max-width: 100%;
  border-radius: 8px;
  height: auto;
}

/* Knowledge Tags */
.tag-container {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--leo-border);
}

.tag-clickable {
  cursor: pointer;
  user-select: none;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 9999px;
  background: rgba(var(--leo-primary-rgb), 0.1);
  color: var(--leo-primary);
  border: 1px solid transparent;
}

.tag-clickable:hover {
  background-color: var(--leo-primary);
  color: #fff;
  transform: translateY(-1px);
}

/* =========================================
   6. AI & SEARCH PANEL
   ========================================= */

#aiPanel {
  background: var(--leo-surface);
  border-left: 1px solid var(--leo-border);
  
  /* AI Panel also needs to respect the Fixed Header */
  position: sticky;
  top: var(--leo-navbar-height);
  height: calc(100vh - var(--leo-navbar-height));
  
  overflow-y: auto;
}

#aiPanel h6 {
  font-weight: 700;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  font-size: 0.75rem;
  color: var(--leo-text-muted);
}

/* Search Input */
#aiPanel .search-container {
  margin-bottom: 1rem;
}

#aiPanel #search-input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  font-size: 0.875rem;
  border-radius: 8px;
  border: 1px solid var(--leo-border);
  background-color: var(--leo-bg);
  color: var(--leo-text-main);
  border-color: var(--leo-primary);
  transition: border-color 0.2s, box-shadow 0.2s;
}

#aiPanel #search-input:focus {
  outline: none;
  border-color: var(--leo-primary);
  box-shadow: 0 0 0 3px rgba(var(--leo-primary-rgb), 0.1);
}

/* Search Results */
#aiPanel #results-container {
  max-height: 70vh;
  overflow-y: auto;
  padding-right: 2px;
}

#aiPanel .result-item {
  background-color: #fff;
  padding: 12px;
  margin-bottom: 10px;
  border-radius: 8px;
  border: 1px solid var(--leo-border);
  cursor: pointer;
  transition: all 0.15s ease;
}

#aiPanel .result-item:hover {
  border-color: var(--leo-primary);
  background-color: rgba(var(--leo-primary-rgb), 0.02);
  transform: translateX(2px);
}

#aiPanel .result-meta {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--leo-primary);
  margin-bottom: 4px;
}

#aiPanel .result-title {
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.3;
  margin-bottom: 4px;
  color: var(--leo-text-main);
}

#aiPanel .result-snippet {
  font-size: 0.8rem;
  line-height: 1.4;
  color: var(--leo-text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

#aiPanel .highlight {
  background-color: #fde047;
  color: #854d0e;
  padding: 0 2px;
  border-radius: 2px;
  font-weight: 500;
}

#aiPanel .keyword-tag {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 4px;
  background-color: var(--leo-bg);
  color: var(--leo-text-muted);
  border: 1px solid var(--leo-border);
}

#aiPanel .no-results {
  text-align: center;
  padding: 20px;
  font-size: 0.85rem;
  color: var(--leo-text-muted);
  font-style: italic;
}

/* =========================================
   7. KNOWLEDGE GRAPH
   ========================================= */

#knowledgeGraph {
  background: #f8fafc;
  border-radius: 0 0 8px 8px;
}

.graph-toolbar button {
  background: white;
  border: 1px solid var(--leo-border);
  color: var(--leo-text-muted);
}

.graph-toolbar button:hover {
  background: var(--leo-bg);
  color: var(--leo-primary);
}

/* =========================================
   8. UTILITY & ANIMATIONS
   ========================================= */

/* Legacy / Toggle Support */
body.fullscreen-mode #sidebar,
body.fullscreen-mode #aiPanel, 
body.fullscreen-mode .navbar {
  display: none !important;
}

body.fullscreen-mode .leo-app-layout {
  height: 100vh;
}

/* AI Status Pulse */
.ai-status {
  background: rgba(var(--leo-primary-rgb), 0.08);
  border-radius: 12px;
  padding: 12px;
}

.pulse {
  width: 8px;
  height: 8px;
  background: #22c55e;
  border-radius: 50%;
  animation: pulse-green 2s infinite;
}

@keyframes pulse-green {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}