/* =============================================
   TREE DOCUMENT VIEWER — DARK MODE
   Geist font, mobile-optimized, rich aesthetics
   ============================================= */

/* --- Reset & Base ---- */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-base: #0a0a0c;
  --bg-surface: #111114;
  --bg-elevated: #18181c;
  --bg-hover: #1f1f24;
  --bg-active: #26262c;
  --border: #2a2a30;
  --border-subtle: #1e1e24;
  --text-primary: #ededf0;
  --text-secondary: #9d9da6;
  --text-tertiary: #6b6b76;
  --accent: #249582;
  --accent-dim: rgba(36, 149, 130, 0.12);
  --accent-hover: #2eb89f;
  --green: #34d399;
  --orange: #fb923c;
  --sidebar-width: 280px;
  --toc-width: 240px;
  --header-height: 56px;
  --content-max-width: 740px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --transition: 0.2s ease;
}

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Geist', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-base);
  color: var(--text-primary);
  line-height: 1.7;
  font-size: 16px;
  font-weight: 400;
  overflow-x: hidden;
  min-height: 100vh;
}

/* --- Layout --- */
.mobile-header {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: rgba(10, 10, 12, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 100;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}

.mobile-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

/* Hamburger */
.menu-btn {
  background: none;
  border: none;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  padding: 4px;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}

.menu-btn:hover {
  background: var(--bg-hover);
}

.menu-btn span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text-secondary);
  border-radius: 2px;
  transition: all 0.3s ease;
}

.menu-btn.active span:nth-child(1) {
  transform: rotate(45deg) translate(5px, 5px);
}

.menu-btn.active span:nth-child(2) {
  opacity: 0;
}

.menu-btn.active span:nth-child(3) {
  transform: rotate(-45deg) translate(5px, -5px);
}

/* --- Sidebar --- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  z-index: 200;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--border-subtle);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text-primary);
}

.logo svg {
  color: var(--accent);
}

.sidebar-subtitle {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-tertiary);
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.doc-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 10px;
}

.doc-category {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  padding: 14px 12px 6px;
  margin-top: 4px;
}

.doc-category:first-child {
  margin-top: 0;
  padding-top: 4px;
}

.doc-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition);
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--text-secondary);
  border: 1px solid transparent;
  margin-bottom: 2px;
}

.doc-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.doc-item.active {
  background: var(--accent-dim);
  color: var(--accent-hover);
  border-color: rgba(36, 149, 130, 0.15);
  font-weight: 500;
}

.doc-item .doc-number {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--text-tertiary);
  margin-top: 1px;
}

.doc-item.active .doc-number {
  background: rgba(36, 149, 130, 0.18);
  color: var(--accent);
}

.doc-item .doc-title {
  flex: 1;
  min-width: 0;
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 150;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.sidebar-overlay.visible {
  display: block;
  opacity: 1;
}

/* --- Main --- */
.main {
  margin-left: var(--sidebar-width);
  min-height: 100vh;
}

.content-wrapper {
  display: flex;
  max-width: calc(var(--content-max-width) + var(--toc-width) + 80px);
  margin: 0 auto;
  padding: 0 24px;
  position: relative;
}

/* --- Article --- */
.article {
  flex: 1;
  max-width: var(--content-max-width);
  padding: 48px 0 120px;
  min-width: 0;
}

.article-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 120px 0;
  color: var(--text-tertiary);
  font-size: 14px;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- TOC --- */
.toc {
  position: sticky;
  top: 48px;
  width: var(--toc-width);
  max-height: calc(100vh - 96px);
  overflow-y: auto;
  margin-left: 40px;
  flex-shrink: 0;
  padding-top: 48px;
}

.toc-header {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-tertiary);
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: 12px;
}

.toc-list {
  list-style: none;
}

.toc-list li {
  margin-bottom: 2px;
}

.toc-list a {
  display: block;
  padding: 5px 10px;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-tertiary);
  text-decoration: none;
  border-radius: var(--radius-sm);
  border-left: 2px solid transparent;
  transition: all var(--transition);
}

.toc-list a:hover {
  color: var(--text-secondary);
  background: var(--bg-hover);
}

.toc-list a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-dim);
}

.toc-list .toc-h2 {
  padding-left: 10px;
}

.toc-list .toc-h3 {
  padding-left: 22px;
  font-size: 12px;
}

/* --- Markdown Styles --- */
.article h1 {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.2;
  margin: 0 0 12px;
  color: var(--text-primary);
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border-subtle);
}

.article h2 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.3;
  margin: 48px 0 16px;
  color: var(--text-primary);
}

.article h3 {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.4;
  margin: 32px 0 12px;
  color: var(--text-primary);
}

.article h4 {
  font-size: 15px;
  font-weight: 600;
  margin: 24px 0 8px;
  color: var(--text-secondary);
}

.article p {
  margin-bottom: 16px;
  color: var(--text-secondary);
}

.article strong {
  color: var(--text-primary);
  font-weight: 600;
}

.article em {
  color: var(--text-secondary);
  font-style: italic;
}

.article a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid rgba(36, 149, 130, 0.3);
  transition: all var(--transition);
}

.article a:hover {
  color: var(--accent-hover);
  border-bottom-color: var(--accent-hover);
}

.article ul,
.article ol {
  margin-bottom: 16px;
  padding-left: 24px;
  color: var(--text-secondary);
}

.article li {
  margin-bottom: 6px;
}

.article li::marker {
  color: var(--text-tertiary);
}

.article blockquote {
  margin: 24px 0;
  padding: 16px 20px;
  border-left: 3px solid var(--accent);
  background: var(--bg-elevated);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  color: var(--text-secondary);
  font-style: italic;
}

.article blockquote p:last-child {
  margin-bottom: 0;
}

.article hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 48px 0;
}

.article code {
  font-family: 'Geist Mono', 'SF Mono', 'Consolas', monospace;
  background: var(--bg-elevated);
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 13px;
  color: var(--accent);
  border: 1px solid var(--border-subtle);
}

.article pre {
  margin: 20px 0;
  padding: 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  overflow-x: auto;
}

.article pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.65;
}

/* Tables */
.article table {
  width: 100%;
  margin: 20px 0;
  border-collapse: collapse;
  font-size: 14px;
}

.article thead th {
  text-align: left;
  padding: 10px 14px;
  background: var(--bg-elevated);
  border-bottom: 2px solid var(--border);
  font-weight: 600;
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
}

.article tbody td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  vertical-align: top;
}

.article tbody tr:hover {
  background: var(--bg-hover);
}

/* --- Scroll progress --- */
.scroll-progress {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  height: 2px;
  background: transparent;
  z-index: 50;
}

.scroll-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-hover));
  transition: width 0.1s ease-out;
}

/* --- Responsive --- */
@media (max-width: 1100px) {
  .toc {
    display: none;
  }

  .content-wrapper {
    padding: 0 20px;
  }
}

@media (max-width: 768px) {
  .mobile-header {
    display: flex;
  }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main {
    margin-left: 0;
    padding-top: var(--header-height);
  }

  .scroll-progress {
    left: 0;
    top: var(--header-height);
  }

  .content-wrapper {
    padding: 0 16px;
  }

  .article {
    padding: 24px 0 80px;
  }

  .article h1 {
    font-size: 26px;
  }

  .article h2 {
    font-size: 20px;
    margin-top: 36px;
  }

  .article h3 {
    font-size: 17px;
  }

  body {
    font-size: 16px;
  }

  /* Mobile TOC toggle */
  .toc-mobile-toggle {
    display: flex;
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    align-items: center;
    justify-content: center;
    border: none;
    cursor: pointer;
    z-index: 90;
    box-shadow: 0 4px 20px rgba(36, 149, 130, 0.35);
    font-size: 20px;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
  }

  .toc-mobile-toggle:active {
    transform: scale(0.92);
  }

  .toc-mobile-panel {
    display: none;
    position: fixed;
    bottom: 80px;
    right: 16px;
    left: 16px;
    max-height: 55vh;
    overflow-y: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
    z-index: 89;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
  }

  .toc-mobile-panel.visible {
    display: block;
    animation: slideUp 0.25s ease;
  }

  @keyframes slideUp {
    from {
      opacity: 0;
      transform: translateY(12px);
    }

    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .toc-mobile-panel .toc-header {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-tertiary);
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: 10px;
  }

  .toc-mobile-panel .toc-list a {
    padding: 7px 10px;
    font-size: 13px;
  }
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-tertiary);
}

/* Selection */
::selection {
  background: rgba(36, 149, 130, 0.3);
  color: #fff;
}