/* ---- Dyslexia-friendly font -------------------------------------------
   OpenDyslexic (SIL Open Font License) from the Fontsource package.
   Browsers only download it once the toggle is switched on. */
@font-face { font-family: "OpenDyslexic"; font-style: normal; font-weight: 400; font-display: swap;
  src: url(https://cdn.jsdelivr.net/npm/@fontsource/opendyslexic@5.3.0/files/opendyslexic-latin-400-normal.woff2) format("woff2"); }
@font-face { font-family: "OpenDyslexic"; font-style: normal; font-weight: 700; font-display: swap;
  src: url(https://cdn.jsdelivr.net/npm/@fontsource/opendyslexic@5.3.0/files/opendyslexic-latin-700-normal.woff2) format("woff2"); }
@font-face { font-family: "OpenDyslexic"; font-style: italic; font-weight: 400; font-display: swap;
  src: url(https://cdn.jsdelivr.net/npm/@fontsource/opendyslexic@5.3.0/files/opendyslexic-latin-400-italic.woff2) format("woff2"); }
@font-face { font-family: "OpenDyslexic"; font-style: italic; font-weight: 700; font-display: swap;
  src: url(https://cdn.jsdelivr.net/npm/@fontsource/opendyslexic@5.3.0/files/opendyslexic-latin-700-italic.woff2) format("woff2"); }

/* ---- Themes ------------------------------------------------------------
   Each theme is just a set of variables. Add your own by copying a block,
   giving it a new data-theme name, and adding an <option> in index.php. */
:root {
  --fs: 16px;            /* document text size, changed by the A− / A+ buttons */
  --leading: 1.65;
  --tracking: normal;
  --bg: #fcfcfb;
  --surface: #f3f3f1;
  --text: #1f2328;
  --muted: #646b73;
  --border: #dcdcd8;
  --accent: #1d5fd6;
  --on-accent: #ffffff;
  --code-bg: #eeeeeb;
  --quote: #c9c9c4;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #15171c;
  --surface: #1b1e24;
  --text: #e2e4e8;
  --muted: #9097a1;
  --border: #2c3038;
  --accent: #7ea6ff;
  --on-accent: #10131a;
  --code-bg: #232730;
  --quote: #3b404a;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #15171c;
    --surface: #1b1e24;
    --text: #e2e4e8;
    --muted: #9097a1;
    --border: #2c3038;
    --accent: #7ea6ff;
    --on-accent: #10131a;
    --code-bg: #232730;
    --quote: #3b404a;
    color-scheme: dark;
  }
}

:root[data-theme="sepia"] {
  --bg: #f5eedc;
  --surface: #ece2c9;
  --text: #3d2f1f;
  --muted: #7a6650;
  --border: #d8cbab;
  --accent: #9a4f14;
  --on-accent: #ffffff;
  --code-bg: #e9ddc0;
  --quote: #cdb98f;
  --font-body: Charter, "Iowan Old Style", Georgia, "Times New Roman", serif;
  color-scheme: light;
}

/* Comes after the themes so it wins over Sepia's serif. */
:root[data-dyslexic] {
  --font-body: "OpenDyslexic", system-ui, sans-serif;
  --leading: 1.8;
  --tracking: 0.01em;
}

/* ---- Base & layout ----------------------------------------------------- */
* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html.lb-open { overflow: hidden; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  display: grid;
  grid-template-columns: 17.5rem minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
}

button, select, input { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#topbar, #scrim { display: none; }

#sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.25rem 1rem 2rem;
  font-size: 0.92rem;
}

#sidebar > header {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-bottom: 1rem;
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 650;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
}

/* ---- Reading controls -------------------------------------------------- */
.controls { display: flex; flex-direction: column; gap: 0.6rem; }

.control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 2rem;
}
.control .label { color: var(--muted); font-size: 0.85rem; }

.control select {
  width: 8.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
}

.stepper {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg);
}
.stepper button {
  border: 0;
  background: none;
  padding: 0.3rem 0.6rem;
  cursor: pointer;
  min-width: 2.4rem;
}
.stepper button + button { border-left: 1px solid var(--border); }
.stepper button:hover:not(:disabled) { background: var(--code-bg); }
.stepper button:disabled { opacity: 0.35; cursor: default; }
#font-reset { font-size: 0.8rem; color: var(--muted); min-width: 3.3rem; font-variant-numeric: tabular-nums; }
#font-down { font-size: 0.82rem; }
#font-up { font-size: 1.05rem; }

/* Toggle switch built on a real checkbox, so it stays keyboard and screen-reader friendly. */
.switch { cursor: pointer; position: relative; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  flex: none;
  width: 2.5rem;
  height: 1.4rem;
  border-radius: 1rem;
  background: var(--border);
  position: relative;
  transition: background 0.15s;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 0.2rem;
  left: 0.2rem;
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(1.1rem); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- File tree --------------------------------------------------------- */
#tree ul { list-style: none; margin: 0; padding: 0; }
#tree ul ul { padding-left: 0.85rem; border-left: 1px solid var(--border); margin-left: 0.45rem; }

#tree summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
  padding: 0.3rem 0.4rem;
  border-radius: 5px;
}
#tree summary:hover { color: var(--text); }

#tree a {
  display: block;
  padding: 0.3rem 0.5rem;
  border-radius: 5px;
  color: var(--text);
  text-decoration: none;
  overflow-wrap: anywhere;
}
#tree a:hover { background: var(--code-bg); }
#tree a.active { background: var(--accent); color: var(--on-accent); }

.empty { color: var(--muted); }

/* ---- Document ---------------------------------------------------------- */
#content {
  padding: 2.5rem 2rem 5rem;
  font-size: var(--fs);
  line-height: var(--leading);
  letter-spacing: var(--tracking);
}

/* htmx adds this class while a page is loading */
#content.htmx-request,
#content:has(.htmx-request) { opacity: 0.55; transition: opacity 0.15s; }

/* Line length grows a little with the text so bigger text isn't cramped. */
.doc { max-width: 46em; margin: 0 auto; }

.crumbs {
  color: var(--muted);
  font-size: 0.82rem;
  letter-spacing: normal;
  margin-bottom: 1.5rem;
  overflow-wrap: anywhere;
}
.crumbs span { opacity: 0.5; padding: 0 0.15rem; }

.md-body { overflow-wrap: break-word; }
.md-body > :first-child { margin-top: 0; }

.md-body h1, .md-body h2, .md-body h3,
.md-body h4, .md-body h5, .md-body h6 {
  line-height: 1.25;
  margin: 2em 0 0.6em;
  font-weight: 650;
  scroll-margin-top: 4.5rem;
}
.md-body h1 { font-size: 2em; }
.md-body h2 { font-size: 1.45em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.md-body h3 { font-size: 1.2em; }
.md-body h4 { font-size: 1em; }
.md-body h5, .md-body h6 { font-size: 0.9em; color: var(--muted); }

.md-body a { color: var(--accent); text-underline-offset: 0.15em; }

.md-body p, .md-body ul, .md-body ol,
.md-body blockquote, .md-body pre, .md-body table { margin: 0 0 1.1em; }

.md-body ul, .md-body ol { padding-left: 1.6em; }
.md-body li + li { margin-top: 0.25em; }
.md-body li:has(> input[type="checkbox"]) { list-style: none; margin-left: -1.4em; }
.md-body input[type="checkbox"] { margin-right: 0.4em; }

.md-body img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}
.md-body img.zoomable { cursor: zoom-in; }

.md-body code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  letter-spacing: normal;
  background: var(--code-bg);
  padding: 0.15em 0.35em;
  border-radius: 4px;
}

.md-body pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  line-height: 1.5;
}
.md-body pre code { background: none; padding: 0; font-size: 0.86em; }

.md-body blockquote {
  margin-left: 0;
  padding: 0.1em 1em;
  border-left: 4px solid var(--quote);
  color: var(--muted);
}

.md-body hr { border: 0; border-top: 1px solid var(--border); margin: 2.5em 0; }

.md-body table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
.md-body th, .md-body td { border: 1px solid var(--border); padding: 0.45em 0.8em; text-align: left; }
.md-body th { background: var(--surface); font-weight: 600; }

.notice h1 { font-size: 1.6em; }

/* ---- Lightbox ---------------------------------------------------------- */
#lightbox {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: rgb(8 9 12 / 0.94);
  color: #f2f3f5;
  overscroll-behavior: contain;
}
#lightbox::backdrop { background: transparent; }
#lightbox:not([open]) { display: none; }

#lightbox figure {
  margin: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: max(3.5rem, env(safe-area-inset-top)) max(4.5rem, env(safe-area-inset-right))
           max(2.5rem, env(safe-area-inset-bottom)) max(4.5rem, env(safe-area-inset-left));
}
#lightbox img {
  max-width: 100%;
  max-height: calc(100% - 3rem);
  object-fit: contain;
  border-radius: 4px;
  background: #fff; /* transparent PNGs/SVGs stay readable on the dark backdrop */
  user-select: none;
  -webkit-user-drag: none;
}
#lightbox figcaption {
  max-width: 50rem;
  text-align: center;
  font-size: 0.95rem;
  line-height: 1.4;
  color: #c8ccd3;
}

.lb-count {
  position: absolute;
  top: max(1.1rem, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.85rem;
  color: #aab0b9;
  font-variant-numeric: tabular-nums;
}

.lb-btn {
  position: absolute;
  width: 2.9rem;
  height: 2.9rem;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: rgb(255 255 255 / 0.12);
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.lb-btn:hover { background: rgb(255 255 255 / 0.22); }
.lb-btn svg { width: 1.4rem; height: 1.4rem; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.lb-close { top: max(0.75rem, env(safe-area-inset-top)); right: max(0.75rem, env(safe-area-inset-right)); }
.lb-prev, .lb-next { top: 50%; transform: translateY(-50%); }
.lb-prev { left: max(0.75rem, env(safe-area-inset-left)); }
.lb-next { right: max(0.75rem, env(safe-area-inset-right)); }
#lightbox.single .lb-prev, #lightbox.single .lb-next { display: none; }

/* ---- Small screens ----------------------------------------------------- */
@media (max-width: 820px) {
  body { display: block; }

  #topbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: sticky;
    top: 0;
    z-index: 5;
    height: calc(3.25rem + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) max(0.5rem, env(safe-area-inset-right)) 0 max(0.5rem, env(safe-area-inset-left));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  #nav-toggle {
    flex: none;
    width: 2.75rem;
    height: 2.75rem;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 8px;
    background: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  #nav-toggle:active { background: var(--code-bg); }
  #topbar-title {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #scrim:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 9;
    background: rgb(0 0 0 / 0.35);
  }

  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(19rem, 86vw);
    z-index: 10;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0s 0.22s;
    box-shadow: 0 0 24px rgb(0 0 0 / 0.2);
    padding-top: max(1.25rem, env(safe-area-inset-top));
    padding-bottom: max(2rem, env(safe-area-inset-bottom));
    padding-left: max(1rem, env(safe-area-inset-left));
  }
  body.nav-open #sidebar {
    transform: none;
    visibility: visible;
    transition: transform 0.22s ease;
  }
  body.nav-open { overflow: hidden; }

  #content {
    padding: 1.5rem max(1rem, env(safe-area-inset-right)) max(4rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  }

  .md-body h1 { font-size: 1.7em; }

  /* Lightbox buttons move to the bottom where thumbs reach them. */
  #lightbox figure { padding-left: max(0.5rem, env(safe-area-inset-left)); padding-right: max(0.5rem, env(safe-area-inset-right)); padding-bottom: calc(5rem + env(safe-area-inset-bottom)); }
  .lb-prev, .lb-next { top: auto; transform: none; bottom: max(1rem, env(safe-area-inset-bottom)); }
  .lb-prev { left: calc(50% - 4rem); }
  .lb-next { right: calc(50% - 4rem); }
}

/* Bigger tap targets wherever the main pointer is a finger. */
@media (pointer: coarse) {
  #tree a, #tree summary { padding-top: 0.6rem; padding-bottom: 0.6rem; }
  .control { min-height: 2.75rem; }
  .stepper button { min-height: 2.5rem; min-width: 2.9rem; }
  .control select { font-size: 16px; padding: 0.45rem; } /* 16px stops iOS zooming on focus */
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

@media print {
  #sidebar, #topbar, #scrim, #lightbox, .crumbs { display: none; }
  body { display: block; }
}
