:root {
  --bg: #f7f9f8;
  --surface: #ffffff;
  --surface-2: #f0f5f3;
  --border: #dce6e2;
  --text: #1d2b27;
  --text-soft: #4c635c;
  --muted: #7d938c;
  --accent: #1f7a5a;
  --accent-soft: #e3f1ec;
  --accent-strong: #155c43;
  --warn: #b8651b;
  --warn-soft: #fbeedd;
  --code-bg: #eef3f1;
  --shadow: 0 1px 3px rgba(20, 40, 34, .08), 0 8px 24px rgba(20, 40, 34, .06);
  --radius: 14px;
  --soil: #c2a07a;
  --rock: #9aa6a0;
  --leaf: #3a9d6e;
  --sidebar-w: 290px;
  --maxw: 860px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0e1513;
  --surface: #151f1c;
  --surface-2: #1b2724;
  --border: #293833;
  --text: #e6efeb;
  --text-soft: #a9c0b8;
  --muted: #7e958d;
  --accent: #4fd1a0;
  --accent-soft: #16302a;
  --accent-strong: #6fe0b4;
  --warn: #e0995a;
  --warn-soft: #2e2113;
  --code-bg: #1b2724;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
  --soil: #5b4a36;
  --rock: #41504a;
  --leaf: #57c98c;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.brand {
  padding: 22px 22px 14px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 5;
}
.brand .logo {
  display: flex; align-items: center; gap: 10px;
  font-weight: 800; font-size: 18px; letter-spacing: -.3px;
}
.brand .leaf { font-size: 22px; }
.brand .tag {
  margin: 6px 0 0; font-size: 12.5px; color: var(--muted); font-weight: 500;
}

.search-wrap { padding: 14px 16px; position: sticky; top: 78px; background: var(--surface); z-index: 4; }
.search {
  width: 100%;
  padding: 9px 12px 9px 34px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%237d938c' stroke-width='2'><circle cx='11' cy='11' r='7'/><path d='M21 21l-4.3-4.3'/></svg>") no-repeat 10px center;
  color: var(--text);
  font-size: 14px;
  font-family: var(--font);
  outline: none;
}
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.nav { padding: 6px 12px 30px; }
.nav-group { margin-top: 10px; }
.nav-group > .group-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); font-weight: 700; padding: 10px 10px 4px;
}
.nav a {
  display: block;
  padding: 7px 10px;
  border-radius: 8px;
  color: var(--text-soft);
  font-size: 14px;
  font-weight: 500;
  border-left: 2px solid transparent;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-left-color: var(--accent);
  font-weight: 600;
}
.nav .num { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 8px; font-size: 12px; }

/* ---------- Content ---------- */
.main { flex: 1; min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: none;
  align-items: center; gap: 12px;
  padding: 12px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .menu-btn {
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 8px 12px; font-size: 18px; cursor: pointer;
}
.content { max-width: var(--maxw); margin: 0 auto; padding: 40px 28px 100px; }

.hero {
  background: linear-gradient(135deg, var(--accent-soft), var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 32px;
  margin-bottom: 40px;
}
.hero h1 { margin: 0 0 8px; font-size: 32px; letter-spacing: -.5px; }
.hero p { margin: 0; color: var(--text-soft); font-size: 16px; max-width: 60ch; }
.hero .meta { margin-top: 18px; display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-soft); font-size: 12.5px; font-weight: 600;
  padding: 5px 11px; border-radius: 999px;
}

section.chapter { scroll-margin-top: 20px; margin-bottom: 56px; }
section.chapter > h2 {
  font-size: 25px; letter-spacing: -.3px; margin: 0 0 6px;
  padding-bottom: 10px; border-bottom: 2px solid var(--border);
}
section.chapter .lead { color: var(--text-soft); font-size: 16px; margin: 14px 0 22px; }
h3 { font-size: 19px; margin: 30px 0 8px; letter-spacing: -.2px; }
h4 { font-size: 15.5px; margin: 22px 0 6px; color: var(--accent-strong); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 12px 0; }
ul, ol { padding-left: 22px; margin: 12px 0; }
li { margin: 6px 0; }
strong { color: var(--text); }

table {
  width: 100%; border-collapse: collapse; margin: 18px 0;
  font-size: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden;
}
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); font-weight: 700; color: var(--text); }
tr:last-child td { border-bottom: none; }

.callout {
  border-radius: 12px; padding: 14px 18px; margin: 20px 0;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 14.5px;
}
.callout.tip { border-left: 4px solid var(--accent); background: var(--accent-soft); }
.callout.warn { border-left: 4px solid var(--warn); background: var(--warn-soft); }
.callout .label { font-weight: 700; display: block; margin-bottom: 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
.callout.tip .label { color: var(--accent-strong); }
.callout.warn .label { color: var(--warn); }

code { font-family: var(--mono); background: var(--code-bg); padding: 2px 6px; border-radius: 5px; font-size: 13.5px; }

.formula {
  background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px; margin: 16px 0;
  font-family: var(--mono); font-size: 14px; overflow-x: auto;
}

.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin: 20px 0; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow);
}
.card h4 { margin-top: 0; text-transform: none; letter-spacing: 0; color: var(--text); font-size: 15px; }
.card p { font-size: 13.5px; color: var(--text-soft); margin: 6px 0 0; }

.toc-inline { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 20px; margin: 22px 0; }
.toc-inline ul { columns: 2; column-gap: 30px; margin: 0; }
.toc-inline li { break-inside: avoid; }

.diff-fig {
  display: flex; gap: 10px; align-items: flex-end; height: 120px;
  padding: 14px; background: var(--surface-2); border-radius: 12px; border: 1px solid var(--border);
  margin: 18px 0;
}
.diff-fig .bar { flex: 1; background: var(--accent); border-radius: 6px 6px 0 0; position: relative; }
.diff-fig .bar span { position: absolute; bottom: -22px; left: 0; right: 0; text-align: center; font-size: 12px; color: var(--text-soft); }
.diff-fig .bar small { position: absolute; top: -20px; left: 0; right: 0; text-align: center; font-size: 12px; font-weight: 700; }

footer.foot {
  border-top: 1px solid var(--border); margin-top: 60px; padding-top: 24px;
  color: var(--muted); font-size: 13px;
}

.theme-toggle {
  margin-left: auto; background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 14px;
}

/* scroll progress */
.progress { position: fixed; top: 0; left: 0; height: 3px; width: 0; background: var(--accent); z-index: 50; transition: width .1s; }

/* search results */
mark { background: #ffe9a8; color: inherit; border-radius: 3px; padding: 0 1px; }
[data-theme="dark"] mark { background: #6b5a1f; }

.no-results { color: var(--muted); padding: 20px; text-align: center; }

/* responsive */
@media (max-width: 980px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 40; transform: translateX(-100%); transition: transform .25s; box-shadow: var(--shadow); }
  .sidebar.open { transform: translateX(0); }
  .topbar { display: flex; }
  .content { padding: 24px 18px 80px; }
  .toc-inline ul { columns: 1; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 35; display: none; }
  .scrim.show { display: block; }
}
@media (min-width: 981px) {
  .scrim { display: none !important; }
}

/* ---------- Breadcrumb ---------- */
.breadcrumb { font-size: 13px; color: var(--muted); margin-bottom: 18px; }
.breadcrumb a { color: var(--muted); }
.breadcrumb a:hover { color: var(--accent); }
.breadcrumb .sep { margin: 0 7px; opacity: .6; }

/* ---------- Brand link reset ---------- */
.brand .logo { text-decoration: none; color: var(--text); display: flex; align-items: center; gap: 10px; }
.brand .logo svg { color: var(--accent); flex: 0 0 auto; }

/* ---------- Cross-page search results ---------- */
.search-results { margin-top: 8px; max-height: 52vh; overflow-y: auto; display: none; }
.search-results.show { display: block; }
.search-results a {
  display: block; padding: 9px 11px; border-radius: 9px;
  border: 1px solid var(--border); margin-bottom: 7px; background: var(--surface);
}
.search-results a:hover { border-color: var(--accent); text-decoration: none; background: var(--accent-soft); }
.search-results .r-title { font-weight: 700; font-size: 12.5px; color: var(--accent-strong); }
.search-results .r-head { font-size: 13.5px; color: var(--text); font-weight: 600; }
.search-results .r-snip { font-size: 12px; color: var(--muted); display: block; margin-top: 2px; line-height: 1.5; }
.search-results .empty { font-size: 13px; color: var(--muted); padding: 8px 4px; }

/* ---------- Home cards ---------- */
.group-block { margin: 30px 0; }
.group-h { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); border-bottom: 1px solid var(--border); padding-bottom: 8px; margin: 0 0 16px; }
a.card { color: inherit; text-decoration: none; display: block; transition: transform .12s ease, box-shadow .12s ease; }
a.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.card-num { font-size: 11px; font-weight: 700; color: var(--accent); letter-spacing: .05em; }
.card h4 { margin: 4px 0 6px; transition: none; }

/* ---------- On-page TOC ---------- */
.toc { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 14px 18px; margin: 18px 0 26px; }
.toc-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; margin-bottom: 8px; }
.toc-time { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--muted); }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li { margin: 2px 0; }
.toc a { display: block; padding: 4px 0; color: var(--text-soft); font-size: 14px; }
.toc a:hover { color: var(--accent); text-decoration: none; }
.toc-h3 a { padding-left: 14px; font-size: 13.5px; }
.toc-h2 a { font-weight: 600; color: var(--text); }

/* ---------- Prev / Next pager ---------- */
.pager { display: flex; justify-content: space-between; gap: 14px; margin: 40px 0 10px; }
.pager a { flex: 1; border: 1px solid var(--border); border-radius: 12px; padding: 14px 18px; text-decoration: none; background: var(--surface); transition: border-color .12s, box-shadow .12s; }
.pager a:hover { border-color: var(--accent); box-shadow: var(--shadow); text-decoration: none; }
.pager span { display: block; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 4px; }
.pager strong { color: var(--text); font-size: 15px; }
.pager-next { text-align: right; }

/* ---------- Back to top ---------- */
.back-to-top { position: fixed; right: 20px; bottom: 20px; z-index: 40; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 10px 16px; font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: var(--shadow); opacity: 0; transform: translateY(8px); pointer-events: none; transition: opacity .2s, transform .2s; }
.back-to-top.show { opacity: 1; transform: translateY(0); pointer-events: auto; }

/* ---------- Heading anchor-copy links ---------- */
.anchor { margin-right: 8px; color: var(--muted); text-decoration: none; font-weight: 400; opacity: 0; transition: opacity .12s, color .12s; }
.has-anchor:hover .anchor, .anchor:focus { opacity: 1; }
.anchor.copied { color: var(--accent); opacity: 1; }
h2, h3, h4 { scroll-margin-top: 84px; }

/* ---------- Search group filters ---------- */
.search-filters { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip-btn {
  font: inherit; font-size: 11.5px; font-weight: 600; cursor: pointer;
  padding: 4px 9px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-soft);
}
.chip-btn:hover { border-color: var(--accent); color: var(--accent); }
.chip-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Topbar buttons (print) ---------- */
.topbtn {
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.topbtn:hover { border-color: var(--accent); }

/* ---------- Formula copy button ---------- */
.formula { position: relative; }
.copy-btn {
  position: absolute; top: 8px; right: 8px;
  font: inherit; font-size: 11px; font-weight: 600; cursor: pointer;
  padding: 3px 8px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-soft);
}
.copy-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- Glossary tooltips ---------- */
.gloss { border-bottom: 1px dotted var(--accent); cursor: help; color: inherit; }
.gloss-tip {
  position: fixed; z-index: 60; max-width: 280px; pointer-events: none;
  background: var(--text); color: var(--bg); font-size: 12.5px; line-height: 1.5;
  padding: 8px 11px; border-radius: 8px; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(4px); transition: opacity .12s, transform .12s;
}
.gloss-tip.show { opacity: 1; transform: translateY(0); }

/* ---------- Related articles ---------- */
.related { margin: 36px 0 10px; }
.related h4 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; }
.related-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.related-grid a {
  display: block; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); font-weight: 600; font-size: 14px; text-decoration: none;
}
.related-grid a:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- A-Z index page ---------- */
.az-group { margin: 22px 0; }
.az-group > h3 { font-size: 20px; color: var(--accent-strong); margin: 0 0 8px; }
.az-list { list-style: none; margin: 0; padding: 0; }
.az-list li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.az-list a { font-weight: 600; }
.az-list span { color: var(--text-soft); font-size: 13.5px; }

/* ---------- Figures / media ---------- */
figure.media { margin: 24px 0; }
figure.media img {
  width: 100%; height: auto; display: block; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); cursor: zoom-in;
}
figure.media figcaption { font-size: 13px; color: var(--muted); margin-top: 8px; text-align: center; line-height: 1.5; }
.hero-media { margin: 0 0 26px; }
.hero-media img { width: 100%; height: auto; display: block; border-radius: 14px; border: 1px solid var(--border); }

/* ---------- Lightbox ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 80; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.82); opacity: 0; pointer-events: none; transition: opacity .15s; padding: 4vh 4vw;
}
.lightbox.show { opacity: 1; pointer-events: auto; }
.lightbox img { max-width: 94vw; max-height: 90vh; border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.5); }

/* ---------- Video embeds ---------- */
.video-embed { margin: 24px 0; max-width: 640px; }
.video-thumb {
  position: relative; display: block; border-radius: 12px; overflow: hidden;
  border: 1px solid var(--border); background: #000;
}
.video-thumb img { width: 100%; height: auto; display: block; opacity: 0.88; transition: opacity .15s; }
.video-thumb:hover img { opacity: 1; }
.play-btn {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 66px; height: 66px; background: rgba(0,0,0,.6); color: #fff; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 26px;
  transition: background .15s, transform .15s; pointer-events: none;
}
.video-thumb:hover .play-btn { background: rgba(200,0,0,.85); transform: translate(-50%, -50%) scale(1.06); }
.video-cap { font-size: 13px; color: var(--muted); margin-top: 8px; line-height: 1.5; }

/* ---------- Print / PDF ---------- */
@media print {
  .sidebar, .topbar, .progress, .scrim, .back-to-top, .toc, .pager, .related,
  .calc-host, .search-filters, .copy-btn, .gloss-tip, .breadcrumb, .anchor, .lightbox, .video-embed { display: none !important; }
  body { background: #fff; }
  .layout { display: block; }
  .main { display: block; }
  .content { max-width: 100%; padding: 0; }
  section.chapter { break-inside: avoid-page; }
  .diagram svg { max-width: 100%; }
  a { color: #000; text-decoration: none; }
  .foot { border: none; }
}

/* ---------- Diagrams / illustrations ---------- */
.diagram { margin: 24px 0; }
.diagram svg {
  width: 100%; height: auto; display: block; max-width: 660px; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px;
}
.diagram figcaption { margin-top: 8px; font-size: 12.5px; color: var(--muted); text-align: center; line-height: 1.5; }
.dgm text { font-family: var(--font); font-size: 13px; fill: var(--text-soft); }
.dgm .lbl { fill: var(--text-soft); }
.dgm .soil { fill: var(--soil); }
.dgm .rock { fill: var(--rock); }
.dgm .water { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
.dgm .surface { stroke: var(--accent); stroke-width: 2; }
.dgm .axis { stroke: var(--border); stroke-width: 1; }
.dgm .node { fill: var(--surface-2); stroke: var(--accent); stroke-width: 1.5; }
.dgm .flow { stroke: var(--accent); stroke-width: 2; fill: none; }
.dgm .flowup { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 4 3; fill: none; }
.dgm .arrow { fill: var(--accent); }
.dgm .lily { fill: var(--leaf); }
.dgm .fish { fill: var(--accent-strong); }
.dgm .coping { fill: var(--rock); stroke: var(--border); }
.dgm .layer-under { stroke: var(--muted); stroke-width: 3; }
.dgm .layer-liner { stroke: var(--accent); stroke-width: 3; }
.dgm .bog { fill: var(--soil); stroke: var(--accent); stroke-width: 1.5; }
.dgm .fold { stroke: var(--accent); stroke-width: 2; fill: none; }
.dgm .bar { fill: var(--accent); fill-opacity: .55; stroke: var(--accent); }
.dgm .t1 { fill: var(--accent); fill-opacity: .22; stroke: var(--accent); }
.dgm .t2 { fill: var(--accent); fill-opacity: .42; stroke: var(--accent); }
.dgm .t3 { fill: var(--accent); fill-opacity: .62; stroke: var(--accent); }
.dgm .t4 { fill: var(--accent); fill-opacity: .82; stroke: var(--accent); }
.dgm .ice { fill: #cfe8f5; stroke: var(--border); }
.dgm .snow { fill: #eef4f7; stroke: var(--border); }
.dgm .hole { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
.dgm .a-green { fill: #7ec96b; }
.dgm .a-weed { fill: #bfe39a; }
.dgm .a-diatom { fill: #b08968; }
.dgm .a-cyano { fill: #5fae9a; }
.dgm .weedline { stroke: #3f7d2f; fill: none; stroke-width: 2; }
.dgm .cyano-sheen { fill: #9fe0c8; opacity: .6; }

/* ---------- Calculators ---------- */
.calc-host { margin: 22px 0; }
.calc {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}
.calc h5 { margin: 0 0 12px; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--accent-strong); }
.calc-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.calc-f { display: flex; flex-direction: column; font-size: 13px; color: var(--text-soft); gap: 4px; }
.calc-f input, .calc-f select {
  font: inherit; font-size: 14px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); outline: none;
}
.calc-f input:focus, .calc-f select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.calc-out { margin-top: 14px; font-size: 14px; color: var(--text); line-height: 1.7; }
