:root {
  --paper-width: 210mm;
  --paper-height: 297mm;
  --paper-margin-top: 40px;
  --paper-margin-right: 80px;
  --paper-margin-bottom: 40px;
  --paper-margin-left: 80px;
  --page-header-height: 40px;
  --page-header-bottom: 40px;
  --page-footer-height: 40px;
  --page-footer-top: 40px;
  /* (copies, so that a landscape page can swap the sizes without a cycle) */
  --paper-w: var(--paper-width);
  --paper-h: var(--paper-height);
}
@page {
  size: var(--paper-width) var(--paper-height);
  margin: 0;
}
/* the page of a float that is too wide for a portrait page (pages.js): the paper turned */
@page wide {
  size: var(--paper-height) var(--paper-width);
  margin: 0;
}
@page multiple {
  margin-top: var(--paper-margin-top);
  margin-bottom: var(--paper-margin-bottom);
}
.page-multiple {
  page: multiple;
}
.page-wide {
  page: wide;
  --paper-width: var(--paper-h);
  --paper-height: var(--paper-w);
}
.pagesjs {
  padding: 0;
  max-width: 100%;
  margin: 5mm;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 5mm;
  .pagesjs-front, .chapter {
    min-height: unset;
    box-shadow: none;
  }
  & > *, &:not(:has(.pagesjs-front)):has(.chapter-toc) #TOC:not(.chapter-toc) {
    display: none;
  }
  .pagesjs-page {
    width: var(--paper-width);
    height: var(--paper-height);
    flex: none;  /* (keep the paper size in a narrow window: the content is laid out for it) */
    padding: var(--paper-margin-top) var(--paper-margin-right) var(--paper-margin-bottom) var(--paper-margin-left);
    break-before: page;  /* (not break-after: a named page after a break-after gets a blank page in Chrome) */
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow: hidden;
    img {
      max-width: 100%;
    }
    .chapter-before, .chapter-after, .buttons:has(+ .chapter-before) {
      display: none;
    }
    #TOC {
      border: none;
      padding: 0;
      margin-left: auto;
      &:not(.chapter-toc):is(.fragment-first, :not(.pagesjs-fragmented))::before {
        content: "Contents";
        display: block;
        font-weight: bold;
        font-size: 1.5em;
        margin-bottom: 1em;
      }
      a {
        display: flex;
        &::after {
          content: attr(data-page-number);
          align-self: end;
          margin-left: 1ch;
        }
        .section-number {
          margin-right: .3em;
        }
        .toc-text {
          position: relative;
          flex: 1 0;
          overflow: hidden;
          &::after {
            content: "";
            width: 100%;
            position: absolute;
            bottom: 1.5ex;
            margin-left: 1ch;
            border-bottom: dashed 1px;
          }
        }
      }
    }
    /* wrappers cloned from the content root and its ancestors: keep their
       context for CSS selectors, but not their boxes */
    .pagesjs-wrap {
      display: block !important;
      margin: 0 !important;
      padding: 0 !important;
      border: none !important;
      box-shadow: none !important;
      background: none !important;
      width: auto !important;
      max-width: none !important;
      min-height: 0 !important;
      height: auto !important;
      float: none !important;
      position: static !important;
      transform: none !important;
    }
    .pagesjs-run {
      display: block;
    }
    /* pieces of a fragmented element: slice it (instead of decorating each piece) */
    .pagesjs-fragmented {
      &:not(.fragment-first) {
        border-top: none;
        margin-top: 0;
        & > thead { opacity: .5; }
      }
      &:not(.fragment-last) {
        border-bottom: none;
        margin-bottom: 0;
      }
    }
    /* a list item continued from the previous page has no marker */
    li.pagesjs-fragmented:not(.fragment-first)::marker {
      content: "";
    }
    .fullwidth {
      float: none;
      max-width: none;
      width: var(--paper-width);
      margin-left: calc(0px - var(--paper-margin-left));
      transform: none;
      /* (article.css caps the content at the viewport, which differs between screen and print) */
      & * { max-width: calc(var(--paper-width) - 4em); }
    }
    /* center the content of a page vertically: the page of a float that takes the whole
       page, or of a part title (the classes are set by pages.js) */
    &:is(.page-float, .page-center) .pagesjs-body {
      display: flex;
      flex-direction: column;
      justify-content: safe center;  /* (content taller than the page starts at the top) */
    }
    .footnotes {
      margin-top: 1.5em;
    }
    .footnotes::before {
      content: "";
      display: block;
      max-width: 20em;
      border-top: solid 1px;
    }
    &:has(.side-left, .embed-left) .pagesjs-body {
      padding-left: calc(0px - var(--side-margin));
      .fullwidth {
        transform: translate(var(--side-margin));
      }
    }
    &:has(.side-right, .embed-right) .pagesjs-body {
      padding-right: calc(0px - var(--side-margin));
    }
    .side {
      font-size: .9em;
      background: none;
    }
  }
}
.pagesjs > .pagesjs-progress {
  display: block;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
  padding: .3em .8em;
  border-radius: 1em;
  background: rgba(0, 0, 0, .75);
  color: white;
  font: 14px sans-serif;
}
.pagesjs-hit {
  outline: 3px solid #e33;
  outline-offset: 2px;
}
.pagesjs-tools {
  position: absolute;  /* (scrolls with the unpaginated page; fixed when paginated) */
  top: 1em;
  left: 1em;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: .5em;
  font: 14px sans-serif;
  & > a, .pagesjs-panel {
    background: Canvas;
    color: CanvasText;
    border: 1px solid;
    border-radius: .3em;
  }
  & > a {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.2em;
    height: 2.2em;
    text-decoration: none;
  }
}
.pagesjs-panel {
  position: absolute;
  top: 0;
  left: calc(100% + .5em);  /* (beside the buttons) */
  width: min(30em, calc(100vw - 5em));
  max-height: calc(100vh - 2em);  /* (the panel starts at the top of the toolbar, 1em from the top) */
  overflow: auto;
  padding: .5em 1em;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
  ul { margin: 0; padding-left: 1.2em; list-style: none; }
  & > nav > ul, .pagesjs-problems { padding-left: 0; }
  a { display: block; padding: .15em 0; color: inherit; text-decoration: none; }
  a::before { content: attr(data-page-number); float: right; opacity: .6; }
  a:hover { text-decoration: underline; }
  li { cursor: pointer; padding: .15em 0; }
}
.pagesjs-settle .pagesjs-body {
  /* the probe page that holds all content has sides, but normal pages don't */
  padding: 0 !important;
}
.pagesjs-header, .pagesjs-footer {
  font-size: .9em;
  display: flex;
  justify-content: space-between;
}
.pagesjs-header {
  flex: 0 0 var(--page-header-height);
  margin-bottom: var(--page-header-bottom);
  align-items: end;
  &::before {
    content: attr(data-main-title);
  }
  &::after {
    content: attr(data-page-title);
  }
}
.pagesjs-footer {
  flex: 0 0 var(--page-footer-height);
  margin-top: var(--page-footer-top);
  align-items: start;
  &::before {
    content: attr(data-page-url);
  }
  &::after {
    content: attr(data-page-number);
  }
}
.pagesjs-body {
  flex-grow: 1;
  min-height: 0;  /* don't let the body push the footer down when content overhangs by a few pixels */
  position: relative;
}
@media only screen {
  .pagesjs {
    background: #f7f7f7;
    .pagesjs-page {
      background: white;
    }
    .page-multiple {
      overflow-y: auto;
    }
  }
  .pagesjs-filling {
    display: block;
    .pagesjs-page {
      opacity: 0;  /* don't show the page that is being filled (only the progress) */
      height: auto;
    }
  }
}
.pagesjs .pagesjs-tools { position: fixed; }
@media print {
  .pagesjs-tools { display: none; }
  .pagesjs-hit { outline: none; }
  .pagesjs {
    display: block;  /* (named pages, i.e., landscape ones, don't work in a flex container) */
    margin: 0;
    gap: 0;
    .page-multiple {
      padding-top: 0;
      padding-bottom: 0;
      height: auto;
    }
  }
}
@media (prefers-color-scheme: dark) {
  .pagesjs-page {
    box-shadow: 0 0 0 1px #bbb;
  }
}
