/* The layout of every keibisoft.com blog page and of blog.html.
   Linked after each page's own <style>, and every rule starts with a body class that
   blog-series/layout/blog_layout.py puts on the page: .blog-post on posts, .blog-index on
   blog.html, and .blog-own-toc on a post with a contents box of its own. A post that needs the
   whole width carries .blog-full by hand: no rail, the list of headings stays above the article.
   The pages keep their own colours, fonts and sizes; this file moves blocks. */

:root {
  --bl-rail: 15rem;     /* the list of headings beside the article */
  --bl-gap: 3rem;       /* between the list and the article */
  --bl-measure: 46rem;  /* running text: about 90 characters at the pages' 19 to 21 px base font */
  --bl-wide: 1360px;    /* main, from 1100 px up */
}

/* --- Posts: the list of headings ------------------------------------------------------- */

body.blog-post .page-toc {
  margin: 0 0 2rem;
  padding: 0.6rem 1rem;
  border: 1px solid var(--pico-muted-border-color);
  border-radius: 0.5rem;
  font-size: 0.9rem;
}
/* Some pages style every details and summary (the architecture map draws a box); not this one. */
body.blog-post .page-toc details {
  margin: 0; padding: 0; border: 0; border-radius: 0; background: none; overflow: visible;
}
body.blog-post .page-toc summary {
  margin: 0; padding: 0; font-size: inherit; color: var(--pico-muted-color); font-weight: 600;
}
body.blog-post .page-toc ol { margin: 0.6rem 0 0.2rem; padding-left: 1.6rem; }
body.blog-post .page-toc li { margin: 0 0 0.35rem; line-height: 1.35; }
body.blog-post .page-toc a { color: var(--pico-muted-color); text-decoration: none; }
body.blog-post .page-toc a:hover,
body.blog-post .page-toc a:focus-visible { color: var(--pico-primary); text-decoration: underline; }

/* --- Posts and index: nothing scrolls sideways on a phone ------------------------------ */

body.blog-post main :is(img, video, iframe),
body.blog-index main :is(img, video, iframe) { max-width: 100%; height: auto; }
body.blog-post main pre { max-width: 100%; overflow-x: auto; }
body.blog-post main :is(p, li, td, th, dd, figcaption) { overflow-wrap: break-word; }
body.blog-post main :is(p, li) code { overflow-wrap: anywhere; }

/* A table wider than its column scrolls inside its own box, at every width. The box stands where
   the table stood, so it takes the table's margin: 1rem above and below, as Pico and most pages
   give a table. Inside a scroll box a margin cannot merge with the margin next to it. */
body.blog-post main .table-scroll { max-width: 100%; overflow-x: auto; margin: 1rem 0; }
body.blog-post main .table-scroll > table { margin-top: 0; margin-bottom: 0; }

/* A page with a contents box of its own shows one list: its box under 1100 px, the rail from 1100 px. */
@media (max-width: 1099.98px) { body.blog-post.blog-own-toc main > .page-toc { display: none; } }
@media (min-width: 1100px) { body.blog-post.blog-own-toc main .toc { display: none; } }

@media (max-width: 767.98px) {
  /* The architecture map fixes its column counts inline; on a phone the boxes wrap. */
  body.blog-post .arch-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 140px), 1fr)) !important; }
}

/* --- Posts, from 1100 px: the list as a rail, the article beside it -------------------- */
/* A page with .blog-full has main at the same width, its own padding and no rail. */

@media (min-width: 1100px) {
  body.blog-post main { max-width: var(--bl-wide); }
  body.blog-post:not(.blog-full) main {
    padding-left: calc(1rem + var(--bl-rail) + var(--bl-gap));
  }
  /* The site menu, the page header and the footer keep the full width. */
  body.blog-post:not(.blog-full) main > :is(nav:first-child, header, footer, .footer) {
    margin-left: calc(-1 * (var(--bl-rail) + var(--bl-gap)));
  }

  /* The block stays in the flow with no height, so the article starts where it did; it sticks
     to the top while main scrolls by, and the list hangs from it into the left margin. */
  body.blog-post:not(.blog-full) main > .page-toc {
    position: sticky;
    top: 0;
    height: 0;
    margin: 0;
    padding: 0;
    border: 0;
    z-index: 1;
  }
  body.blog-post:not(.blog-full) main > .page-toc > details {
    position: absolute;
    top: 1rem;
    left: calc(-1 * (var(--bl-rail) + var(--bl-gap)));
    width: var(--bl-rail);
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    padding: 0 0.5rem 0 0;
  }

  /* Running text keeps its measure; figures, tables, code and diagrams take the article's width. */
  body.blog-post :is(main, main article, main section) > :is(p, ul, ol, dl, blockquote, h2, h3, h4, h5, h6,
      details, address, .highlight, .highlight-box, .limits, .warning-box, .series, .toc, .acme-now) {
    max-width: var(--bl-measure);
  }
  /* With no rail the running text sits in the middle, under the page header; a full-width page
     centres its own figures of that measure (the architecture map centres its fold figure). */
  body.blog-post.blog-full :is(main, main article, main section) > :is(p, ul, ol, dl, blockquote, h2, h3, h4, h5, h6,
      details, address, .highlight, .highlight-box, .limits, .warning-box, .series, .toc, .acme-now) {
    margin-left: auto;
    margin-right: auto;
  }
  /* The series figure breaks out of an 860 px column today; beside the rail it fills the article. */
  body.blog-post figure.wide { width: auto; margin-left: 0; transform: none; }
}

/* Where details can show its content while closed, the rail is always open. */
@supports selector(details::details-content) {
  @media (min-width: 1100px) {
    body.blog-post:not(.blog-full) main > .page-toc details::details-content { display: block; content-visibility: visible; }
    body.blog-post:not(.blog-full) main > .page-toc summary { pointer-events: none; list-style: none; }
    body.blog-post:not(.blog-full) main > .page-toc summary::after { display: none; }
    body.blog-post:not(.blog-full) main > .page-toc summary::-webkit-details-marker { display: none; }
  }
}

/* --- blog.html, from 1100 px: the entries of a section in two or three columns ---------- */

@media (min-width: 1100px) {
  body.blog-index main { max-width: var(--bl-wide); }
  body.blog-index main > section {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(19rem, 1fr));
    column-gap: 2rem;
    align-items: start;
  }
  body.blog-index main > section > :not(details) { grid-column: 1 / -1; }
  /* An open entry takes the whole row, so no empty space sits under the closed entries beside it.
     Its text keeps the measure of a post: across the whole row a line held about 150 characters. */
  body.blog-index main > section > details[open] { grid-column: 1 / -1; }
  body.blog-index main > section > details[open] > p { max-width: var(--bl-measure); }
}
