:root {
  --bg: #ffffff;
  --surface: #f8f9fa;
  --border: #e9ecef;
  --text: #212529;
  --text-secondary: #6c757d;
  --accent: #0056b3;
  --link: #0056b3;
  --link-hover: #003d80;
  --code-bg: #f6f8fa;
  --radius: 6px;
  --sidebar-width: 260px;

  /* Domain colors */
  --calculus: #2563eb;
  --linear-algebra: #7c3aed;
  --probability-statistics: #059669;
  --optimization: #d97706;
  --machine-learning: #dc2626;
  --quantitative-finance: #0891b2;
  --computing: #4b5563;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  font-size: 16px;
}

a { color: var(--link); text-decoration: none; }
a:hover { color: var(--link-hover); text-decoration: underline; }

header.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

header.site-header .logo {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  white-space: nowrap;
}

header.site-header .search-box {
  flex: 1;
  max-width: 420px;
  position: relative;
}

header.site-header .search-box input {
  width: 100%;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  background: var(--surface);
}

header.site-header .search-box input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg);
}

.search-results {
  position: absolute;
  top: 110%;
  left: 0;
  right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0,0,0,0.08);
  max-height: 360px;
  overflow-y: auto;
  display: none;
}

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

.search-results a {
  display: block;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.search-results a:last-child { border-bottom: none; }
.search-results a:hover { background: var(--surface); text-decoration: none; }
.search-results .match-title { font-weight: 600; }
.search-results .match-domain {
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.layout {
  display: flex;
  min-height: calc(100vh - 56px);
}

aside.sidebar {
  width: var(--sidebar-width);
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 1rem;
  overflow-y: auto;
  position: sticky;
  top: 56px;
  height: calc(100vh - 56px);
}

aside.sidebar h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin: 1.2rem 0 0.5rem;
}

aside.sidebar ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

aside.sidebar li a {
  display: block;
  padding: 0.35rem 0.5rem;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.92rem;
}

aside.sidebar li a:hover {
  background: var(--bg);
  text-decoration: none;
}

aside.sidebar li a.active {
  background: #e7f1ff;
  color: var(--accent);
  font-weight: 600;
}

main.main {
  flex: 1;
  padding: 1.5rem 2rem;
  max-width: 900px;
}

main.full {
  max-width: none;
}

.page-title {
  margin: 0 0 0.25rem;
  font-size: 2rem;
  line-height: 1.2;
}

.meta {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 1.25rem;
}

.domain-badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #fff;
  margin-right: 0.5rem;
}

.domain-calculus { background: var(--calculus); }
.domain-linear-algebra { background: var(--linear-algebra); }
.domain-probability-statistics { background: var(--probability-statistics); }
.domain-optimization { background: var(--optimization); }
.domain-machine-learning { background: var(--machine-learning); }
.domain-quantitative-finance { background: var(--quantitative-finance); }
.domain-computing { background: var(--computing); }

.section {
  margin-top: 1.5rem;
}

.section h2 {
  font-size: 1.2rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.35rem;
  margin-bottom: 0.75rem;
}

.key-points { margin: 0; padding-left: 1.25rem; }
.key-points li { margin-bottom: 0.4rem; }

.formula-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
}

.formula-card .name {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.35rem;
}

.code-card {
  margin-bottom: 1rem;
}

pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.5;
  position: relative;
}

code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}

p code, li code {
  background: var(--code-bg);
  padding: 0.15rem 0.3rem;
  border-radius: 4px;
  font-size: 0.9em;
}

.reference-list {
  list-style: none;
  padding: 0;
}

.reference-list li {
  margin-bottom: 0.5rem;
  padding: 0.5rem;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.reference-list .ref-title { font-weight: 600; }
.reference-list .ref-source { color: var(--text-secondary); font-size: 0.85rem; }

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

.tag-list li {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.6rem;
  font-size: 0.85rem;
}

.relations {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

.relation-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
}

.relation-box h4 {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  text-transform: uppercase;
  color: var(--text-secondary);
  letter-spacing: 0.03em;
}

.relation-box ul {
  margin: 0;
  padding-left: 1.1rem;
}

.relation-box li { margin-bottom: 0.25rem; }

.graph-container {
  height: 70vh;
  min-height: 520px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  position: relative;
}

.graph-controls {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
}

.graph-controls button {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: var(--radius);
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
}

.graph-controls button:hover { background: var(--surface); }

.graph-controls label {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.home-intro {
  max-width: 760px;
  margin-bottom: 1.5rem;
}

footer.site-footer {
  border-top: 1px solid var(--border);
  padding: 1rem 1.25rem;
  color: var(--text-secondary);
  font-size: 0.85rem;
  text-align: center;
  background: var(--surface);
}

@media (max-width: 880px) {
  .layout { flex-direction: column; }
  aside.sidebar {
    width: 100%;
    height: auto;
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  main.main { padding: 1rem; }
  .graph-container { height: 50vh; }
}

/* Utility */
.hidden { display: none !important; }
.text-secondary { color: var(--text-secondary); }
