/* IEOR book — light theme, mdBook-style layout. */

:root {
  --sidebar-w: 280px;
  --topbar-h: 48px;
  --content-max: 1080px;
  --fg: #1a1a1a;
  --bg: #ffffff;
  --muted: #666;
  --accent: #1d4ed8;
  --bold: #0057a3;
  --rule: #e5e5e5;
  --code-bg: #f5f5f5;
  --code-border: #d0d0d0;
  --shade: #fafafa;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: grid;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  min-height: 100vh;
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--fg);
  background: var(--bg);
}

.skip { position: absolute; left: -9999px; }
.skip:focus { left: 1em; top: 1em; z-index: 100; background: var(--bg); padding: 0.5em; }

/* === Top bar === */
.topbar {
  grid-area: topbar;
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0 1em;
  background: var(--bg);
}
.topbar .nav-buttons { display: flex; gap: 0.25em; }
.topbar .history-btn {
  background: none;
  border: 1px solid var(--rule);
  border-radius: 4px;
  height: 26px;
  padding: 0 0.6em;
  font-size: 1em;
  line-height: 1;
  cursor: pointer;
  color: var(--fg);
}
.topbar .history-btn:hover { background: var(--code-bg); }
.topbar #search { margin-left: auto; width: 240px; max-width: 100%; position: relative; }

/* === Pagefind UI: theming + fit into the 48px topbar ===
   Defaults assume a full-page search (64px input, large drawer below).
   We scale the input to topbar height and float the drawer as a dropdown. */
:root {
  --pagefind-ui-scale: 0.65;
  --pagefind-ui-primary: var(--accent);
  --pagefind-ui-text: var(--fg);
  --pagefind-ui-background: var(--bg);
  --pagefind-ui-border: var(--rule);
  --pagefind-ui-tag: var(--code-bg);
  --pagefind-ui-border-width: 1px;
  --pagefind-ui-border-radius: 6px;
}

/* Topbar must let the dropdown overflow its bottom edge. */
.topbar { overflow: visible; }

/* Compact input that matches the 26px back/forward button height. */
.topbar .pagefind-ui__form { position: relative; height: 26px; }
.topbar .pagefind-ui__search-input {
  height: 26px;
  font-size: 0.85em;
  font-weight: 500;
  padding: 0 2em 0 2em;
}
/* Magnifying-glass icon: vertical-center inside the 26px input.
   Icon size 14px → top = (26 - 14) / 2 = 6px. Override Pagefind defaults
   (which scale top/size with --pagefind-ui-scale and don't fit a small bar). */
.topbar .pagefind-ui__form::before {
  width: 14px;
  height: 14px;
  top: 6px;
  left: 8px;
}
.topbar .pagefind-ui__search-clear {
  height: 20px;
  top: 3px;
  right: 3px;
  font-size: 0.75em;
  padding: 0 0.6em;
}

/* Results dropdown: absolute under the input, solid bg, shadow,
   above other content. */
.topbar .pagefind-ui__drawer {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: min(560px, 92vw);
  max-height: 70vh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 0.25em 1em;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  z-index: 20;
}
.topbar .pagefind-ui__drawer.pagefind-ui__hidden { display: none; }
.topbar .pagefind-ui__results-area { margin-top: 0.5em; }

/* Highlight style for `<mark>` when browsers render the text-fragment
   landing target. (Native :target-text styling is still spotty across
   browsers — `mark` covers most of it.) */
mark { background: #fff3a3; padding: 0 0.1em; border-radius: 2px; }

/* === Sidebar === */
.sidebar {
  grid-area: sidebar;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1em;
  border-right: 1px solid var(--rule);
  background: var(--shade);
}
.sidebar .site-title {
  font-weight: 600;
  font-size: 1.05em;
  margin-bottom: 1em;
  padding-bottom: 0.5em;
  border-bottom: 1px solid var(--rule);
}
.sidebar .site-title a { color: var(--fg); text-decoration: none; }

/* Nav list (collapsible groups via <details>) */
.nav-list { list-style: none; padding-left: 0; margin: 0; }
.nav-list .nav-list { padding-left: 0.75em; border-left: 1px solid var(--rule); margin: 0.25em 0 0.25em 0.5em; }
.nav-item { margin: 0.1em 0; }
.nav-item a {
  display: block;
  padding: 0.2em 0.5em;
  border-radius: 3px;
  text-decoration: none;
  color: var(--fg);
  font-size: 0.95em;
}
.nav-item a:hover { background: rgba(0,0,0,0.05); }
.nav-item.current > a { background: var(--accent); color: white; }

.nav-item.nav-group > details > summary {
  display: flex;
  align-items: center;
  gap: 0.4em;
  cursor: pointer;
  padding: 0.25em 0.5em;
  font-weight: 600;
  font-size: 0.95em;
  color: var(--fg);
  list-style: none;
  user-select: none;
  border-radius: 3px;
}
.nav-item.nav-group > details > summary::-webkit-details-marker,
.nav-item.nav-group > details > summary::marker { display: none; content: ""; }
.nav-item.nav-group > details > summary::before {
  content: "▶";
  display: inline-block;
  font-size: 0.6em;
  color: var(--muted);
  transition: transform 0.15s;
}
.nav-item.nav-group > details[open] > summary::before { transform: rotate(90deg); }
.nav-item.nav-group > details > summary:hover { background: rgba(0,0,0,0.05); }

/* === Main column (mdBook-style: full-width with centered max-width article) === */
main {
  grid-area: main;
  padding: 2em 2em 4em 2em;
  overflow-x: hidden;
}
article {
  max-width: var(--content-max);
  margin: 0 auto;
}

article h1, article h2, article h3, article h4, article h5 { line-height: 1.3; }
article h1 { font-size: 2em; margin-top: 0; }
article h2 { font-size: 1.5em; border-bottom: 1px solid var(--rule); padding-bottom: 0.25em; }
article h3 { font-size: 1.2em; }
article h4 { font-size: 1.05em; color: var(--muted); }

article a { color: var(--accent); }
article strong { color: var(--bold); }

article pre, article :not(pre) > code {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: 3px;
}
article pre { padding: 0.75em; overflow-x: auto; }
article code { padding: 0.1em 0.3em; }
article pre code { border: 0; padding: 0; background: transparent; }

/* Tables centered */
article table {
  border-collapse: collapse;
  margin: 1em auto;
}
article th, article td {
  border: 1px solid var(--rule);
  padding: 0.4em 0.6em;
}

/* Figures (cetz, fletcher, etc.): centered, sized to natural width within container */
article figure {
  margin: 1em 0;
  text-align: center;
}
article figure svg {
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

/* === Math === */
/* Typst emits `<svg style="display: block">` for both inline and block math.
   Inline-style has higher specificity than CSS rules, so we need !important
   to bring inline math back to inline-block flow with text. */

/* Block math (figure role="math"): centered on its own line */
article figure[role="math"] {
  display: block;
  margin: 0.75em 0;
  text-align: center;
}
article figure[role="math"] svg {
  margin: 0 auto;       /* center the block-level SVG */
}

/* Inline math (span role="math"): flow with text, override the SVG's display:block.
   Typst SVGs sit slightly below the line center; nudge up a touch. */
article span[role="math"] {
  display: inline-block;
  vertical-align: middle;
}
article span[role="math"] svg {
  display: inline-block !important;
  vertical-align: middle;
  position: relative;
  top: -0.15em;
}

/* Helper titles (proof, definition, code_output) */
article .proof-title, article .definition-title, article .code-output-title {
  margin-top: 1.5em;
  margin-bottom: 0.25em;
  color: var(--accent);
  font-weight: 600;
}

/* === Collapsible blocks: examples + code === */
article details.example, article details.code {
  border: 1px solid var(--rule);
  border-radius: 4px;
  margin: 1em 0;
  background: var(--shade);
}
article details.example > summary, article details.code > summary {
  cursor: pointer;
  padding: 0.6em 1em;
  font-weight: 600;
  color: var(--accent);
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 0.5em;
}
article details.example > summary::-webkit-details-marker,
article details.code > summary::-webkit-details-marker,
article details.example > summary::marker,
article details.code > summary::marker { display: none; content: ""; }
article details.example > summary::before, article details.code > summary::before {
  content: "▶";
  display: inline-block;
  font-size: 0.75em;
  color: var(--muted);
  transition: transform 0.15s;
}
article details.example[open] > summary::before,
article details.code[open] > summary::before { transform: rotate(90deg); }
article details.example > summary:hover, article details.code > summary:hover { background: rgba(0,0,0,0.03); }
article details.example > .example-body, article details.code > .code-body {
  padding: 0 1em 1em 1em;
  border-top: 1px solid var(--rule);
}

/* Code summary shows a filename — render in monospace. */
article details.code > summary { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* === Page nav (prev/next at bottom of each page) === */
.page-nav {
  display: flex;
  justify-content: space-between;
  border-top: 1px solid var(--rule);
  padding-top: 1em;
  margin-top: 3em;
  font-size: 0.95em;
  max-width: var(--content-max);
  margin-left: auto;
  margin-right: auto;
}
.nav-arrow {
  color: var(--accent);
  text-decoration: none;
  padding: 0.5em 1em;
  border-radius: 4px;
}
.nav-arrow:hover { background: var(--code-bg); }

/* === Sidebar toggle button (always visible) === */
.nav-toggle {
  display: inline-block;
  background: none;
  border: 1px solid var(--rule);
  border-radius: 4px;
  height: 26px;
  padding: 0 0.55em;
  font-size: 1em;
  line-height: 1;
  cursor: pointer;
  color: var(--fg);
}
.nav-toggle:hover { background: var(--code-bg); }

/* === Sidebar resize handle === */
.sidebar-resizer {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  width: 6px;
  height: 100vh;
  cursor: col-resize;
  z-index: 11;
  transform: translateX(-3px);
  background: transparent;
  transition: background 0.15s;
}
.sidebar-resizer:hover,
body.resizing .sidebar-resizer { background: var(--accent); opacity: 0.4; }
body.resizing { user-select: none; cursor: col-resize; }

/* === Desktop: collapsed sidebar === */
body.sidebar-collapsed {
  grid-template-columns: 0 1fr;
}
body.sidebar-collapsed .sidebar { display: none; }
body.sidebar-collapsed .sidebar-resizer { display: none; }

/* === Mobile === */
@media (max-width: 900px) {
  body {
    grid-template-areas:
      "topbar"
      "main";
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: var(--sidebar-w);
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  body.nav-open .sidebar { transform: none; }
  .sidebar-resizer { display: none; }
  .topbar #search { min-width: 0; }
}
