/* ============================================================
   SAFe Knowledge Base — Main Stylesheet
   ============================================================ */

/* --- Variables --- */
:root {
  --sidebar-width: 300px;
  --header-height: 56px;

  /* Light theme */
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-sidebar: #1e2a3a;
  --bg-sidebar-hover: rgba(255,255,255,0.08);
  --bg-sidebar-active: rgba(59,130,246,0.25);
  --bg-card: #ffffff;
  --bg-code: #f3f4f6;

  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-sidebar: #e2e8f0;
  --text-sidebar-muted: #94a3b8;
  --text-link: #2563eb;
  --text-heading: #1e3a5f;

  --border-color: #e5e7eb;
  --border-sidebar: rgba(255,255,255,0.1);

  --accent: #2563eb;
  --accent-light: #dbeafe;
  --accent-hover: #1d4ed8;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}

body.dark {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-sidebar: #0d1b2a;
  --bg-sidebar-hover: rgba(255,255,255,0.06);
  --bg-sidebar-active: rgba(59,130,246,0.2);
  --bg-card: #1e293b;
  --bg-code: #1e293b;

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-link: #60a5fa;
  --text-heading: #93c5fd;

  --border-color: #334155;
  --accent: #3b82f6;
  --accent-light: rgba(59,130,246,0.15);
  --accent-hover: #2563eb;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
}

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

html { scroll-behavior: smooth; }

body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  transition: background 0.2s, color 0.2s;
  font-size: 16px;
}

/* --- Layout --- */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* --- Mobile Header --- */
.mobile-header {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: var(--header-height);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  box-shadow: var(--shadow-md);
}

.mobile-title {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.3px;
}

.mobile-header button {
  background: none;
  border: none;
  color: var(--text-sidebar);
  font-size: 20px;
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.mobile-header button:hover { background: var(--bg-sidebar-hover); }

/* --- Sidebar --- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 100;
  transition: transform 0.3s ease;
  flex-shrink: 0;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.2) transparent;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 2px; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 18px 16px;
  border-bottom: 1px solid var(--border-sidebar);
  flex-shrink: 0;
}

.logo { display: flex; align-items: center; gap: 10px; }

.logo-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 18px;
  color: white;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(59,130,246,0.4);
}

.logo-text { display: flex; flex-direction: column; line-height: 1.2; }
.logo-title { font-weight: 700; font-size: 17px; color: #fff; letter-spacing: 0.5px; }
.logo-sub { font-size: 11px; color: var(--text-sidebar-muted); letter-spacing: 0.3px; }

.theme-btn {
  background: var(--bg-sidebar-hover);
  border: 1px solid var(--border-sidebar);
  color: var(--text-sidebar);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.theme-btn:hover { background: rgba(255,255,255,0.12); }

/* --- Search --- */
.search-container {
  padding: 14px 14px 10px;
  position: relative;
}

.search-input {
  width: 100%;
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border-sidebar);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  color: var(--text-sidebar);
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
  font-family: var(--font-sans);
}
.search-input::placeholder { color: var(--text-sidebar-muted); }
.search-input:focus {
  border-color: var(--accent);
  background: rgba(255,255,255,0.1);
}

.search-dropdown {
  position: absolute;
  top: calc(100% - 4px);
  left: 14px;
  right: 14px;
  background: #1e2d40;
  border: 1px solid var(--border-sidebar);
  border-radius: var(--radius-sm);
  max-height: 280px;
  overflow-y: auto;
  z-index: 300;
  box-shadow: var(--shadow-lg);
}

.search-result-item {
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  transition: background 0.1s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--bg-sidebar-hover); }
.search-result-title { font-size: 13px; font-weight: 500; color: #e2e8f0; }
.search-result-category { font-size: 11px; color: var(--text-sidebar-muted); margin-top: 2px; }
.search-result-snippet { font-size: 12px; color: #8da3ba; margin-top: 3px; line-height: 1.4; }
.search-no-results { padding: 16px 12px; text-align: center; color: var(--text-sidebar-muted); font-size: 13px; }

/* --- Navigation Tree --- */
.nav-tree {
  flex: 1;
  padding: 8px 0 16px;
  overflow-y: visible;
}

.nav-section { margin-bottom: 4px; }

.nav-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 18px;
  cursor: pointer;
  user-select: none;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-sidebar-muted);
  border-radius: 0;
  transition: color 0.15s;
}
.nav-section-header:hover { color: #cbd5e1; }

.nav-section-toggle {
  font-size: 10px;
  transition: transform 0.2s;
  color: var(--text-sidebar-muted);
}
.nav-section.open .nav-section-toggle { transform: rotate(90deg); }

.nav-items {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease;
}
.nav-section.open .nav-items { max-height: 2000px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 18px 7px 30px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-sidebar-muted);
  border-left: 2px solid transparent;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
  line-height: 1.35;
}
.nav-item:hover {
  color: #e2e8f0;
  background: var(--bg-sidebar-hover);
}
.nav-item.active {
  color: #93c5fd;
  background: var(--bg-sidebar-active);
  border-left-color: var(--accent);
  font-weight: 500;
}

.nav-item-icon { font-size: 14px; flex-shrink: 0; }

/* --- Sidebar Overlay (mobile) --- */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}

/* --- Main Content --- */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  background: var(--bg-primary);
  transition: background 0.2s;
}

.article {
  max-width: 860px;
  margin: 0 auto;
  padding: 40px 48px 80px;
}

/* --- Breadcrumb --- */
.breadcrumb {
  font-size: 12.5px;
  color: var(--text-secondary);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.breadcrumb-sep { color: var(--border-color); }

/* --- Article Header --- */
.article-header { margin-bottom: 32px; }

.article-header h1 {
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-heading);
  line-height: 1.25;
  letter-spacing: -0.5px;
}

/* --- Article Body (Markdown rendered) --- */
.article-body h1 {
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--text-heading);
  margin: 0 0 24px;
  padding-bottom: 12px;
  border-bottom: 2px solid var(--border-color);
  line-height: 1.25;
}

.article-body h2 {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text-heading);
  margin: 40px 0 14px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border-color);
}

.article-body h3 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-heading);
  margin: 28px 0 10px;
}

.article-body h4 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 20px 0 8px;
}

.article-body p {
  margin-bottom: 16px;
  color: var(--text-primary);
  line-height: 1.75;
}

.article-body a {
  color: var(--text-link);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s;
}
.article-body a:hover { border-bottom-color: var(--text-link); }

.article-body ul, .article-body ol {
  margin: 0 0 16px 24px;
  line-height: 1.75;
}
.article-body li { margin-bottom: 5px; color: var(--text-primary); }

.article-body strong { font-weight: 600; color: var(--text-primary); }
.article-body em { color: var(--text-secondary); }

/* Tables */
.article-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0 24px;
  font-size: 14px;
  box-shadow: var(--shadow-sm);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.article-body thead {
  background: var(--accent);
  color: white;
}

.article-body th {
  padding: 11px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.3px;
}

.article-body td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-color);
  color: var(--text-primary);
  vertical-align: top;
}

.article-body tr:last-child td { border-bottom: none; }
.article-body tbody tr:nth-child(even) { background: var(--bg-secondary); }
.article-body tbody tr:hover { background: var(--accent-light); }

/* Code blocks */
.article-body code {
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 2px 6px;
  font-family: var(--font-mono);
  font-size: 13.5px;
  color: var(--accent);
}

.article-body pre {
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 18px 20px;
  overflow-x: auto;
  margin: 16px 0 24px;
  line-height: 1.6;
}

.article-body pre code {
  background: none;
  border: none;
  padding: 0;
  color: var(--text-primary);
  font-size: 13.5px;
}

/* Blockquotes */
.article-body blockquote {
  border-left: 4px solid var(--accent);
  background: var(--accent-light);
  padding: 14px 18px;
  margin: 20px 0;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-style: italic;
  color: var(--text-secondary);
}

.article-body blockquote p { margin-bottom: 0; color: var(--text-secondary); }

/* HR */
.article-body hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 32px 0;
}

/* --- Welcome Page --- */
.welcome-hero {
  text-align: center;
  padding: 40px 20px 48px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 40px;
}

.welcome-icon {
  font-size: 52px;
  margin-bottom: 16px;
  display: block;
}

.welcome-hero h2 {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-heading);
  margin-bottom: 12px;
}

.welcome-subtitle {
  font-size: 1rem;
  color: var(--text-secondary);
  max-width: 480px;
  margin: 0 auto;
  line-height: 1.6;
}

.welcome-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

.welcome-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 24px;
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.15s, border-color 0.2s;
}
.welcome-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
  border-color: var(--accent);
}

.card-icon { font-size: 28px; margin-bottom: 12px; }
.welcome-card h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-heading);
}
.welcome-card p { font-size: 13.5px; color: var(--text-secondary); line-height: 1.5; }

/* --- Loading State --- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 40px;
  color: var(--text-secondary);
  gap: 12px;
  font-size: 15px;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* --- Error State --- */
.error-state {
  padding: 60px 40px;
  text-align: center;
  color: var(--text-secondary);
}
.error-state h3 { font-size: 18px; margin-bottom: 10px; color: var(--text-primary); }

/* --- Responsive --- */
@media (max-width: 900px) {
  .article { padding: 32px 32px 60px; }
}

@media (max-width: 768px) {
  .mobile-header { display: flex; }

  .app-layout { display: block; }

  .sidebar {
    transform: translateX(-100%);
    top: 0;
    z-index: 150;
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-overlay.visible { display: block; }

  .main-content {
    margin-left: 0;
    padding-top: var(--header-height);
  }

  .article { padding: 24px 20px 60px; }

  .article-header h1 { font-size: 1.5rem; }
  .article-body h1 { font-size: 1.5rem; }
  .article-body h2 { font-size: 1.2rem; }

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

  .article-body table { font-size: 13px; }
  .article-body th, .article-body td { padding: 8px 10px; }
}

@media (max-width: 480px) {
  .article { padding: 20px 16px 50px; }
  .article-body pre { padding: 12px 14px; }
  .article-body table { display: block; overflow-x: auto; }
}

/* --- Scrollbar styling (webkit) --- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-secondary); }
::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* --- Print --- */
@media print {
  .sidebar, .mobile-header { display: none !important; }
  .main-content { margin-left: 0 !important; }
  .article { padding: 20px !important; }
}

/* ============================================================
   PWA / iOS Specific Styles
   ============================================================ */

/* Safe area insets — respect iPhone notch, Dynamic Island, home bar */
:root {
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

/* When running as standalone PWA (added to home screen) */
@media all and (display-mode: standalone) {
  /* Extend mobile header to cover the status bar area */
  .mobile-header {
    padding-top: var(--safe-top);
    height: calc(var(--header-height) + var(--safe-top));
  }

  /* Extend sidebar to the top of the screen */
  .sidebar {
    padding-top: var(--safe-top);
  }

  /* Push main content down to account for taller header */
  .main-content {
    padding-top: calc(var(--header-height) + var(--safe-top));
  }

  /* Add bottom padding for home bar on iPhone X+ */
  .article {
    padding-bottom: calc(80px + var(--safe-bottom));
  }

  /* Left safe area for landscape on iPhone */
  @media (orientation: landscape) {
    .sidebar {
      padding-left: max(18px, var(--safe-left));
    }
    .main-content {
      padding-left: var(--safe-left);
      padding-right: var(--safe-right);
    }
  }
}

/* Prevent tap highlight on iOS interactive elements */
.nav-item, .welcome-card, .search-result-item,
.nav-section-header, .theme-btn, .mobile-header button {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

/* Smooth scroll on iOS */
.sidebar, .main-content {
  -webkit-overflow-scrolling: touch;
}

/* Improve touch target sizes on mobile (Apple HIG: 44px minimum) */
@media (max-width: 768px) {
  .nav-item {
    padding-top: 10px;
    padding-bottom: 10px;
    min-height: 44px;
  }
  .nav-section-header {
    min-height: 44px;
    align-items: center;
  }
  .mobile-header button {
    min-width: 44px;
    min-height: 44px;
  }
  .search-input {
    font-size: 16px; /* Prevent iOS auto-zoom on input focus */
    min-height: 44px;
  }
  .welcome-card {
    padding: 20px;
  }
  /* Larger tap targets in search results */
  .search-result-item {
    padding: 14px 12px;
    min-height: 44px;
  }
}

/* Offline status badge in sidebar footer */
.offline-badge {
  display: inline-block;
  font-size: 12px;
  color: #4ade80;
  background: rgba(74, 222, 128, 0.12);
  border: 1px solid rgba(74, 222, 128, 0.25);
  border-radius: 20px;
  padding: 4px 10px;
  margin-bottom: 8px;
  font-weight: 500;
}

/* Sidebar footer refinement */
.sidebar-footer {
  padding: 12px 18px 16px;
  border-top: 1px solid var(--border-sidebar);
  flex-shrink: 0;
}
.sidebar-footer p {
  font-size: 10px;
  color: var(--text-sidebar-muted);
  line-height: 1.5;
}

/* iPad-optimised layout — two-column article for large screens */
@media (min-width: 1200px) {
  .article { max-width: 960px; }
}

@media (min-width: 1400px) {
  :root { --sidebar-width: 320px; }
  .article { max-width: 1040px; }
}

/* iPad split-view / Stage Manager friendly */
@media (min-width: 768px) and (max-width: 1024px) {
  :root { --sidebar-width: 260px; }
  .article { padding: 32px 36px 80px; }
}

