:root {
  --bg: #ffffff;
  --bg-sidebar: #f7f6f4;
  --text: #262524;
  --text-muted: #6f6d69;
  --border: #e5e2dd;
  --accent: #a35b2f;
  --accent-hover: #82461f;
  --code-bg: #f0eee9;
  --sidebar-width: 270px;
  --content-width: 680px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
  --bg: #1c1b1a;
  --bg-sidebar: #232221;
  --text: #e7e4de;
  --text-muted: #9a9791;
  --border: #35332f;
  --accent: #e0a06f;
  --accent-hover: #f0b482;
  --code-bg: #2a2826;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1b1a;
    --bg-sidebar: #232221;
    --text: #e7e4de;
    --text-muted: #9a9791;
    --border: #35332f;
    --accent: #e0a06f;
    --accent-hover: #f0b482;
    --code-bg: #2a2826;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
}

body {
  font-size: 17px;
  line-height: 1.6;
}

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

.layout {
  display: flex;
  min-height: 100vh;
  align-items: flex-start;
}

/* Sidebar */

.sidebar {
  width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  min-height: 100vh;
  padding: 1.5rem 1rem;
  position: sticky;
  top: 0;
  align-self: flex-start;
  max-height: 100vh;
  overflow-y: auto;
}

.site-title {
  display: block;
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text);
  margin-bottom: 1.25rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}
.site-title:hover { color: var(--accent); text-decoration: none; }

.nav-tree, .nav-tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.nav-tree ul { padding-left: 0.9rem; }

.nav-tree li { margin: 0.05rem 0; }

.nav-tree a {
  display: block;
  color: var(--text);
  padding: 0.2rem 0.35rem;
  border-radius: 4px;
  font-size: 0.94rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-tree a:hover { background: var(--border); text-decoration: none; }
.nav-tree li.nav-note.active > a {
  background: var(--accent);
  color: #fff;
}

.nav-tree summary {
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  border-radius: 4px;
  font-size: 0.94rem;
  font-weight: 600;
  color: var(--text-muted);
  list-style: none;
}
.nav-tree summary::-webkit-details-marker { display: none; }
.nav-tree summary::before {
  content: "▸";
  display: inline-block;
  width: 0.9em;
  color: var(--text-muted);
}
.nav-tree details[open] > summary::before { content: "▾"; }
.nav-tree summary:hover { background: var(--border); }
.nav-tree summary a { display: inline; padding: 0; }

.theme-toggle {
  margin-top: 1.5rem;
  width: 100%;
  padding: 0.5rem;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
}
.theme-toggle:hover { color: var(--text); border-color: var(--text-muted); }

.sidebar-toggle {
  display: none;
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 20;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
}

/* Content */

.content {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: flex-start;
  gap: 3rem;
  padding: 3rem 1.5rem;
}

article {
  flex: 0 1 var(--content-width);
  min-width: 0;
  width: 100%;
  max-width: var(--content-width);
}

.toc {
  flex: 0 0 220px;
  position: sticky;
  top: 2rem;
  align-self: flex-start;
  font-size: 0.85rem;
}
.toc-title {
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.75rem;
  margin-bottom: 0.6rem;
}
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li { margin: 0.35rem 0; }
.toc a {
  display: block;
  color: var(--text-muted);
  line-height: 1.35;
}
.toc a:hover { color: var(--accent); text-decoration: none; }
.toc .toc-l1 { padding-left: 0.8rem; }
.toc .toc-l2 { padding-left: 1.6rem; }
.toc .toc-l3 { padding-left: 2.4rem; }
.toc .toc-l4, .toc .toc-l5 { padding-left: 3.2rem; }

article h1 {
  font-size: 2rem;
  margin: 0 0 1.25rem;
  line-height: 1.25;
  scroll-margin-top: 1.5rem;
}
article h1:first-child { margin-top: 0; }
article h2 { font-size: 1.4rem; margin: 2rem 0 0.75rem; scroll-margin-top: 1.5rem; }
article h3 { font-size: 1.15rem; margin: 1.5rem 0 0.5rem; scroll-margin-top: 1.5rem; }
article p { margin: 0 0 1rem; }
article ul, article ol { margin: 0 0 1rem; padding-left: 1.4rem; }
article li { margin: 0.2rem 0; }
article blockquote {
  margin: 1rem 0;
  padding: 0.25rem 1rem;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
article code {
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}
article pre {
  background: var(--code-bg);
  padding: 1rem;
  border-radius: 6px;
  overflow-x: auto;
}
article pre code { background: none; padding: 0; }
article hr { border: none; border-top: 1px solid var(--border); margin: 2rem 0; }
article small { color: var(--text-muted); }

article img { max-width: 100%; height: auto; border-radius: 6px; display: block; }
article a > img { transition: opacity 0.15s ease; }
article a.glightbox { display: inline-block; cursor: zoom-in; }
article a.glightbox:hover img { opacity: 0.88; }
article img.embed-icon { display: inline-block; border-radius: 0; }

figure.flickr-photo {
  margin: 0 0 1.25rem;
}
figure.flickr-photo figcaption {
  text-align: left;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

/* GLightbox caption bar: title (if any) on the left, "View on Flickr"
   pinned to the right via margin-left: auto, so it holds its place whether
   or not a title is present. */
.gdesc-inner {
  display: flex !important;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
}
.gslide-title { margin-bottom: 0 !important; }
.gslide-desc {
  margin-left: auto;
  margin-bottom: 0 !important;
  flex: 0 0 auto;
}
.flickr-link {
  font-size: 0.8em;
  color: var(--text-muted);
  text-decoration: none;
  white-space: nowrap;
}
.flickr-link:hover { color: var(--accent); text-decoration: underline; }

.embedded-note {
  margin: 1.5rem 0;
  padding: 1rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sidebar);
}
.embedded-note-title {
  display: block;
  font-weight: 700;
  margin-bottom: 0.5rem;
  color: var(--text);
}
.embedded-note p:last-child { margin-bottom: 0; }

.broken-link {
  color: #b23b3b;
  border-bottom: 1px dashed #b23b3b;
  cursor: help;
}

table { border-collapse: collapse; margin: 1rem 0; width: 100%; }
th, td { border: 1px solid var(--border); padding: 0.4rem 0.6rem; text-align: left; }

/* Sidebar (270) + content padding (2x24) + article at full width (680) +
   gap (48) + toc (220) = 1266px; hide the TOC before that point is reached
   so the article never has to shrink to make room for it. */
@media (max-width: 1320px) {
  .toc { display: none; }
}

@media (max-width: 860px) {
  .sidebar-toggle { display: block; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 15;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  .sidebar.open { transform: translateX(0); }
  .content { padding: 4.5rem 1.25rem 2.5rem; }
}
