:root {
  --bg: #fbf1c7;
  --bg-soft: #f2e5bc;
  --bg-hard: #ebdbb2;
  --bg-overlay: rgba(251, 241, 199, 0.95);
  --fg: #3c3836;
  --fg-soft: #504945;
  --fg-muted: #928374;
  --orange: #af3a03;
  --orange-soft: #d65d0e;
  --red: #9d0006;
  --green: #79740e;
  --yellow: #b57602;
  --blue: #076678;
  --purple: #8f3f71;
  --aqua: #427b58;
  --border: #d5c4a1;
  --shadow: rgba(60, 56, 54, 0.08);
  --shadow-lg: rgba(60, 56, 54, 0.15);
  --code-bg: #f2e5bc;
  --link: #af3a03;
  --link-hover: #d65d0e;
  --search-highlight: #fabd2f;
  --radius: 8px;
  --radius-sm: 6px;
}

[data-theme="dark"] {
  --bg: #282828;
  --bg-soft: #32302f;
  --bg-hard: #3c3836;
  --bg-overlay: rgba(40, 40, 40, 0.95);
  --fg: #ebdbb2;
  --fg-soft: #d5c4a1;
  --fg-muted: #928374;
  --orange: #fe8019;
  --orange-soft: #ff8c00;
  --red: #fb4934;
  --green: #b8bb26;
  --yellow: #fabd2f;
  --blue: #83a598;
  --purple: #d3869b;
  --aqua: #8ec07c;
  --border: #504945;
  --shadow: rgba(0, 0, 0, 0.2);
  --shadow-lg: rgba(0, 0, 0, 0.4);
  --code-bg: #32302f;
  --link: #fe8019;
  --link-hover: #ff8c00;
  --search-highlight: #fabd2f;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.65;
  min-height: 100vh;
  transition: background 0.25s ease, color 0.25s ease;
}

/* Header */
.header {
  position: sticky;
  top: 0;
  background: var(--bg-overlay);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 100;
}

.header-logo {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--orange);
  text-decoration: none;
  cursor: pointer;
  letter-spacing: -0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.search-bar {
  position: relative;
}

.search-input {
  width: 280px;
  padding: 0.5rem 1rem 0.5rem 2.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-soft);
  color: var(--fg);
  font-size: 0.9rem;
  transition: border-color 0.2s, box-shadow 0.2s, width 0.2s;
}

.search-input:focus {
  outline: none;
  border-color: var(--orange);
  box-shadow: 0 0 0 3px rgba(175, 58, 3, 0.1);
  width: 320px;
}

.search-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--fg-muted);
  pointer-events: none;
}

.search-results {
  position: absolute;
  top: calc(100% + 0.5rem);
  left: 0;
  right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px var(--shadow-lg);
  max-height: 420px;
  overflow-y: auto;
  display: none;
}

.search-results.active {
  display: block;
}

.search-result-item {
  padding: 0.85rem 1rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}

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

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

.search-result-title {
  font-weight: 600;
  color: var(--orange);
  font-size: 0.95rem;
}

.search-result-path {
  font-size: 0.75rem;
  color: var(--fg-muted);
  margin-top: 0.15rem;
}

.search-result-snippet {
  font-size: 0.85rem;
  color: var(--fg-soft);
  margin-top: 0.35rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-result-snippet mark {
  background: var(--search-highlight);
  color: var(--fg);
  padding: 0 2px;
  border-radius: 2px;
}

/* Theme Toggle */
.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem;
  cursor: pointer;
  color: var(--fg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, border-color 0.2s;
}

.theme-toggle:hover {
  background: var(--bg-soft);
  border-color: var(--orange);
}

/* Home Page */
.home {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 60px);
  padding: 2rem;
  text-align: center;
}

.home-title {
  font-size: 2.75rem;
  font-weight: 700;
  color: var(--orange);
  margin-bottom: 0.5rem;
  letter-spacing: -0.03em;
}

.home-subtitle {
  font-size: 1.1rem;
  color: var(--fg-muted);
  margin-bottom: 2rem;
}

.home-search {
  width: 100%;
  max-width: 480px;
  position: relative;
  margin-bottom: 3rem;
}

.home-search .search-input {
  width: 100%;
  padding: 1rem 1rem 1rem 3rem;
  font-size: 1.1rem;
  border-radius: var(--radius);
}

.home-search .search-input:focus {
  width: 100%;
}

.home-search .search-icon {
  left: 1rem;
}

.home-categories {
  width: 100%;
  max-width: 720px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
}

.category-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: left;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s;
}

.category-card:hover {
  border-color: var(--orange);
  box-shadow: 0 4px 12px var(--shadow);
  transform: translateY(-2px);
}

.category-card h3 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--orange);
  margin-bottom: 0.75rem;
}

.category-card ul {
  list-style: none;
}

.category-card li {
  margin-bottom: 0.35rem;
}

.category-card a {
  font-size: 0.9rem;
  color: var(--fg-soft);
  text-decoration: none;
  cursor: pointer;
  transition: color 0.15s;
}

.category-card a:hover {
  color: var(--orange);
}

/* Page Layout */
.page-container {
  display: none;
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem;
}

.page-container.active {
  display: block;
}

.page-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 2rem;
}

/* Breadcrumbs */
.breadcrumbs {
  padding: 0.5rem 0;
  margin-bottom: 1.25rem;
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.breadcrumbs a {
  color: var(--fg-muted);
  text-decoration: none;
  transition: color 0.2s;
}

.breadcrumbs a:hover {
  color: var(--orange);
}

.breadcrumb-sep {
  margin: 0 0.4rem;
  opacity: 0.5;
}

.breadcrumbs .current {
  color: var(--fg);
  font-weight: 500;
}

/* Sidebar / TOC */
.sidebar {
  position: sticky;
  top: 80px;
  height: fit-content;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}

.toc {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.toc-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  margin-bottom: 0.75rem;
}

.toc-list {
  list-style: none;
}

.toc-item {
  margin-bottom: 0.2rem;
}

.toc-link {
  display: block;
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
  color: var(--fg-soft);
  text-decoration: none;
  border-radius: 4px;
  border-left: 2px solid transparent;
  transition: all 0.15s;
}

.toc-link:hover {
  color: var(--orange);
  background: var(--bg-hard);
}

.toc-link.active {
  color: var(--orange);
  border-left-color: var(--orange);
  background: var(--bg-hard);
}

.toc-link.h3 {
  padding-left: 1.25rem;
  font-size: 0.8rem;
}

/* Page Content */
.page-content {
  min-width: 0;
}

.page-content h1 {
  font-size: 2rem;
  font-weight: 700;
  color: var(--orange);
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--border);
  letter-spacing: -0.02em;
}

.page-content h2 {
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--fg);
  margin-top: 2rem;
  margin-bottom: 0.75rem;
  padding-bottom: 0.25rem;
  border-bottom: 1px solid var(--border);
}

.page-content h3 {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--fg-soft);
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
}

.page-content p {
  margin-bottom: 1rem;
}

.page-content ul, .page-content ol {
  margin-bottom: 1rem;
  padding-left: 1.5rem;
}

.page-content li {
  margin-bottom: 0.3rem;
}

.page-content a {
  color: var(--link);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s;
}

.page-content a:hover {
  color: var(--link-hover);
  border-bottom-color: var(--link-hover);
}

.page-content strong {
  font-weight: 600;
  color: var(--fg);
}

.page-content em {
  font-style: italic;
  color: var(--fg-soft);
}

.page-content code {
  background: var(--code-bg);
  padding: 0.2rem 0.45rem;
  border-radius: 4px;
  font-size: 0.88em;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', Menlo, Consolas, monospace;
}

.page-content pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem;
  overflow-x: auto;
  margin-bottom: 1rem;
}

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

.page-content blockquote {
  border-left: 3px solid var(--orange);
  padding: 0.85rem 1rem;
  margin: 1rem 0;
  background: var(--bg-soft);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

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

.page-content table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}

.page-content th, .page-content td {
  padding: 0.75rem;
  border: 1px solid var(--border);
  text-align: left;
}

.page-content th {
  background: var(--bg-soft);
  font-weight: 600;
  font-size: 0.9rem;
}

.page-content td {
  font-size: 0.9rem;
}

.page-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

/* Wiki Links */
.wiki-link {
  color: var(--orange);
  font-weight: 500;
  cursor: pointer;
  border-bottom: 1px dashed var(--orange);
  transition: all 0.2s;
}

.wiki-link:hover {
  color: var(--orange-soft);
  border-bottom-style: solid;
}

.wiki-link.missing {
  color: var(--red);
  opacity: 0.6;
  border-bottom-color: var(--red);
}

/* Backlinks Section */
.backlinks {
  margin-top: 2.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.backlinks h2 {
  font-size: 0.95rem;
  color: var(--fg-muted);
  margin-bottom: 0.75rem;
  border-bottom: none;
  padding-bottom: 0;
  margin-top: 0;
}

.backlinks-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  list-style: none;
  padding: 0;
}

.backlinks-list li a {
  display: inline-block;
  padding: 0.4rem 0.85rem;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.85rem;
  color: var(--fg-soft);
  text-decoration: none;
  transition: all 0.2s;
}

.backlinks-list li a:hover {
  border-color: var(--orange);
  color: var(--orange);
}

/* Sources Section */
.sources {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.sources h2 {
  font-size: 0.95rem;
  color: var(--fg-muted);
  margin-bottom: 0.5rem;
}

.sources ul {
  list-style: none;
  padding: 0;
}

.sources li {
  padding: 0.25rem 0;
}

/* Loading State */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--fg-muted);
}

.loading::after {
  content: '';
  width: 24px;
  height: 24px;
  border: 2px solid var(--border);
  border-top-color: var(--orange);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-left: 0.75rem;
}

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

/* Error State */
.error {
  text-align: center;
  padding: 4rem 2rem;
  color: var(--fg-soft);
}

.error h2 {
  color: var(--red);
  margin-bottom: 0.5rem;
  border: none;
}

/* Responsive */
@media (max-width: 768px) {
  .page-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    max-height: none;
    margin-bottom: 1.5rem;
  }

  .search-input {
    width: 180px;
  }

  .search-input:focus {
    width: 220px;
  }

  .home-title {
    font-size: 2.25rem;
  }

  .home-categories {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .header {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .search-input {
    width: 100%;
  }

  .search-input:focus {
    width: 100%;
  }

  .search-bar {
    order: 3;
    width: 100%;
  }
}
