/* The blog and its posts, read on a phone held upright.

   Loaded on every page with nav: blog (/blog/ and every post) after all.css and
   site-header.css; partials/head.njk adds it, and layouts/page.njk adds its script,
   assets/js/blog-reading.js. all.css was written for a desktop column and left a
   phone with too little: at 390px a post's text had 290px, because .site-content's
   30px gutter and the article's own 20px both sat on each side, and running text was
   16px. Medium gives the same phone 342px of text at 18px. This file does that, makes
   the tables readable without turning the phone, hides the pill while the reader
   scrolls down, and styles the full-size picture viewer.

   Every selector starts with html. A post's own <style> block comes later in the
   page with the same selectors (body.page-post ...), and the extra element is what
   lets these win without !important, except where all.css already uses it. */

/* -- The pill, while reading --
   blog-reading.js puts is-hidden on it while the reader scrolls down, and takes it
   off as soon as they scroll up. It slides up past the top of the screen, shadow
   and all. */
html body.page-blog .nav-wrap.is-hidden {
  transform: translateY(calc(-100% - 40px));
}

@media (prefers-reduced-motion: no-preference) {
  html body.page-blog .nav-wrap {
    transition: top 160ms ease, transform 240ms ease;
  }
}

/* -- The desktop site on a phone --
   blog-reading.js adds this note when a phone's browser draws the desktop site. The
   page is then laid out about 980px wide and shrunk to the screen, so every size here
   is multiplied by --k, how many times wider the page is than the screen: on the
   phone the note reads at 15px. */
html body .desktop-note {
  --k: 1;
  display: flex;
  align-items: flex-start;
  gap: calc(0.75rem * var(--k));
  padding: calc(0.9rem * var(--k)) calc(1rem * var(--k));
  background: #eef2ff;
  border-bottom: 1px solid #c7d2fe;
  color: #1e1b4b;
  font-size: calc(15px * var(--k));
  line-height: 1.45;
}

html body .desktop-note p { margin: 0; flex: 1; font-size: inherit; }

html body .desktop-note button {
  flex: none;
  display: grid;
  place-items: center;
  width: calc(36px * var(--k));
  height: calc(36px * var(--k));
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

html body .desktop-note button svg {
  width: calc(20px * var(--k));
  height: calc(20px * var(--k));
}

/* -- A picture at full size --
   The script makes each post picture a button and opens it in this dialog: fitted
   to the screen first, then at its own pixels after a tap, panned by scrolling. */
html body.page-post .post-body img.zoomable { cursor: zoom-in; }

html body.page-post .post-body img.zoomable:focus-visible {
  outline: 3px solid #4f46e5;
  outline-offset: 3px;
}

html.zoom-open { overflow: hidden; }

html body dialog.zoom {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: rgba(12, 14, 20, 0.97);
  color: #ffffff;
}

html body dialog.zoom::backdrop { background: transparent; }

/* A flex box with auto margins centres the picture while it fits and lets it
   scroll from its own left and top edges once it does not; grid centring would
   push the overflow off the top and left, out of reach. */
html body dialog.zoom .zoom-scroll {
  display: flex;
  width: 100%;
  height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

html body dialog.zoom .zoom-scroll img {
  display: block;
  margin: auto;
  max-width: calc(100vw - 2rem);
  max-height: calc(100dvh - 2rem);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  background: #ffffff;
  cursor: zoom-in;
}

html body dialog.zoom.is-full .zoom-scroll img {
  max-width: none;
  max-height: none;
  border-radius: 0;
  cursor: zoom-out;
}

html body dialog.zoom .zoom-close {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin: 0;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 999px;
  background: rgba(12, 14, 20, 0.7);
  color: #ffffff;
  cursor: pointer;
}

html body dialog.zoom .zoom-close:hover,
html body dialog.zoom .zoom-close:focus-visible {
  background: rgba(255, 255, 255, 0.16);
}

/* -- The blog page, /blog/ --
   The same 24px gutter as a post. .wrap holds the hero, the featured post, the card
   grid and the footer, on posts as well, so a post's footer lines up with its text. */
@media (max-width: 768px) {
  html body.page-blog .wrap {
    margin-left: -30px;
    margin-right: -30px;
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }

  html body.page-blog .featured {
    padding: 1.25rem;
    border-radius: 22px;
  }
}

/* The card grid's 320px minimum is wider than a 360px phone's column, which pushed
   every card off the right edge. One column at most the column's width. */
html body.page-blog .post-grid {
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
}

@media (max-width: 768px) {
  /* The column. .site-content keeps its 30px, because the footer sits on it, so
     the article cancels it and puts back one 1.5rem gutter. 1.5rem each side is
     the same as the breakouts' calc(100vw - 3rem), so figures, tables and code
     blocks line up with the text edges exactly. */
  html body.page-post .post-article {
    margin-left: -30px;
    margin-right: -30px;
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }

  html body.page-post .post-head { padding-top: 1rem; }

  html body.page-post .deck {
    font-size: 1.2rem;
    line-height: 1.5;
  }

  /* Running text at 18px with a little less leading than the desktop's 1.75,
     which at this size spreads a paragraph too thin. */
  html body.page-post .post-body {
    font-size: 1.125rem;
    line-height: 1.65;
  }

  html body.page-post .post-body li { margin-bottom: 0.55rem; }

  /* Headings stay clearly bigger than the 18px text. */
  html body.page-post .post-body h3 { font-size: 1.3rem !important; }

  html body.page-post .post-body blockquote { font-size: 1.15rem; }
  html body.page-post .callout { padding: 1.2rem 1.25rem; }
  html body.page-post .callout p { font-size: 1rem; }
  html body.page-post .post-body figcaption { font-size: 0.9rem; }

  /* Contents: a bigger target for a thumb. */
  html body.page-post .post-toc { padding: 1.25rem 1.25rem; }
  html body.page-post .post-toc ol { font-size: 1rem; }
  html body.page-post .post-toc li { margin-bottom: 0.6rem; }

  /* Inline code may break at its spaces, so a long name never pushes the line
     past the screen. Code blocks keep their lines and scroll. */
  html body.page-post .post-body :not(pre) > code {
    white-space: normal;
    overflow-wrap: break-word;
  }

  html body.page-post pre.code-block code {
    font-size: 0.8rem;
    line-height: 1.6;
  }

  /* A table's text wraps on a phone, headings included, so a three-column
     table fits the screen instead of running off it. all.css keeps the
     headings and every column after the first on one line, which suits a
     desktop and pushes most tables past a phone's edge. */
  html body.page-post .post-body table.post-table :is(th, td) {
    padding: 0.65rem 0.75rem;
    white-space: normal;
  }

  /* all.css holds every table at 340px or more, which overflows a 360px
     phone's 312px column by a little. A post's own wider minimum (the canvas
     size post's 700px) is more specific and still applies. */
  html body.page-post table.post-table { min-width: 0; }

  /* A table or code block wider than the screen shows a soft shadow at the edge
     that has more to scroll to, so the reader knows to swipe it. The white
     covers ride with the content (local) and the shadows stay put (scroll). */
  html body.page-post .table-wrap {
    background:
      linear-gradient(to right, #ffffff 30%, rgba(255, 255, 255, 0)) left center / 40px 100% no-repeat local,
      linear-gradient(to left, #ffffff 30%, rgba(255, 255, 255, 0)) right center / 40px 100% no-repeat local,
      radial-gradient(farthest-side at 0 50%, rgba(12, 14, 20, 0.16), rgba(12, 14, 20, 0)) left center / 14px 100% no-repeat scroll,
      radial-gradient(farthest-side at 100% 50%, rgba(12, 14, 20, 0.16), rgba(12, 14, 20, 0)) right center / 14px 100% no-repeat scroll,
      #ffffff;
  }

  html body.page-post pre.code-block {
    background:
      linear-gradient(to right, #0c0e14 30%, rgba(12, 14, 20, 0)) left center / 40px 100% no-repeat local,
      linear-gradient(to left, #0c0e14 30%, rgba(12, 14, 20, 0)) right center / 40px 100% no-repeat local,
      radial-gradient(farthest-side at 0 50%, rgba(124, 108, 246, 0.35), rgba(124, 108, 246, 0)) left center / 14px 100% no-repeat scroll,
      radial-gradient(farthest-side at 100% 50%, rgba(124, 108, 246, 0.35), rgba(124, 108, 246, 0)) right center / 14px 100% no-repeat scroll,
      #0c0e14;
  }
}

/* Two-column prose tables (a name, then what it means) stack on a phone: each row
   becomes the name in bold over its sentence, so nothing scrolls sideways. A wider
   table keeps its grid, since most cells mean little without their column heads;
   one whose cells read in order without them (the canvas size post's "where you
   can set it") takes class="stack" to stack too. :has() picks the column count; a
   browser without it keeps the scrolling table. */
@media (max-width: 600px) {
  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) {
    display: block;
    width: auto;
    min-width: 0;
    table-layout: auto;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) :is(tbody, tr, th, td) {
    display: block;
    width: auto;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) tbody tr {
    padding: 0.9rem 1.1rem;
    border-bottom: 1px solid #f0f2f6;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) tbody tr:last-child {
    border-bottom: none;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) tbody :is(th, td) {
    padding: 0;
    border: none;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) tbody tr > :first-child {
    font-weight: 700;
    color: #0c0e14;
  }

  html body.page-post :is(table.post-table.prose-table:not(:has(tr > :nth-child(3))), table.post-table.stack) tbody tr > * + * {
    margin-top: 0.35rem;
  }
}
