/* Вёрстка в духе русской технической книги: титул, оглавление, главы. */

@counter-style list-paren {
  system: extends decimal;
  suffix: ") ";
}

:root {
  --ink: #000;
  --paper: #fff;
  --serif: "Old Standard TT", "Times New Roman", serif;
  --mono: "PT Mono", ui-monospace, Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 20px;
  line-height: 1.5;
  font-kerning: normal;
  text-rendering: optimizeLegibility;
}

a {
  color: inherit;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

a:hover {
  text-decoration-thickness: 2px;
}

a:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}

.page {
  max-width: 56em;
  margin: 0 auto;
  padding: 2em 20px 5em;
}

.running-head {
  margin: 0 0 4em;
  font-size: 16px;
  font-style: italic;
  text-align: center;
}

.running-head a {
  text-decoration: none;
}

.running-head a:hover {
  text-decoration: underline;
}

/* Титул */

.title-page {
  margin: 16vh 0 4em;
  text-align: center;
}

.title-page .author {
  margin: 0 0 0.6em;
  font-style: italic;
}

.title-page h1 {
  margin: 0;
  font-size: 64px;
  font-weight: 400;
  line-height: 1;
}

.title-page p {
  margin: 0;
}

.title-page p + p {
  margin-top: 1em;
}

/*
 * Оглавление: название, отточие, дата. Отточие это float нулевой ширины
 * в конце строки: точки тянутся по последней строке названия и прячутся
 * под фоном названия и даты, поэтому длинные названия переносятся честно.
 */

.toc {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc li + li {
  margin-top: 0.6em;
}

.toc a {
  display: block;
  overflow: hidden;
  text-decoration: none;
}

.toc .title {
  padding-right: 0.3em;
  background: var(--paper);
}

.toc time {
  position: relative;
  z-index: 1;
  float: right;
  padding-left: 0.3em;
  background: var(--paper);
}

.toc .leader {
  float: left;
  width: 0;
  white-space: nowrap;
}

.toc .leader::before {
  content: ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .";
}

.toc a:hover .title {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

/* Глава */

.article {
  overflow-wrap: break-word;
  counter-reset: listing;
}

.article-head {
  margin: 0 0 3em;
}

.article h1 {
  margin: 0 0 0.4em;
  font-size: 40px;
  font-weight: 400;
  line-height: 1.15;
  text-align: center;
  text-wrap: balance;
}

.article .subtitle {
  margin: 0 0 1em;
  font-size: 22px;
  line-height: 1.4;
  text-align: center;
  text-indent: 0;
  text-wrap: balance;
}

.article .byline {
  font-size: 16px;
  font-style: italic;
  text-align: center;
  text-indent: 0;
}

.article h2 {
  margin: 2.4em 0 1em;
  font-size: 24px;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  text-wrap: balance;
  scroll-margin-top: 1em;
}

.article h3 {
  margin: 1.2em 0 0.3em;
  font-size: inherit;
  font-weight: 400;
  font-style: italic;
}

.article p {
  margin: 0;
  text-indent: 1.5em;
  hyphens: auto;
}

.article-head + p,
.article h2 + p,
.article h3 + p,
.article pre + p,
.article .flow + p,
.article li p,
.article .callout p {
  text-indent: 0;
}

.article ol {
  list-style: list-paren;
  margin: 0.3em 0 0.5em;
  padding-left: 1.6em;
}

.article ul {
  margin: 0.3em 0 0.5em;
  padding-left: 1.6em;
}

.article li {
  margin: 0.2em 0;
  hyphens: auto;
}

.article .steps > li + li {
  margin-top: 0.7em;
}

.article em {
  hyphens: manual;
}

.article .colophon {
  margin: 3em 0 0;
  font-size: 16px;
  font-style: italic;
  text-align: center;
  text-indent: 0;
}

/* Врезка */

.callout {
  margin: 1.2em 0;
  padding: 0.8em 1.1em;
  border: 1px solid var(--ink);
}

/* Содержание главы */

.contents ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.contents li {
  margin: 0.15em 0;
}

.contents a {
  text-decoration: none;
}

.contents a:hover {
  text-decoration: underline;
}

/* Код, листинги, формулы */

code,
pre {
  font-family: var(--mono);
}

.article :not(pre) > code {
  font-size: 0.85em;
  hyphens: manual;
}

/*
 * Листинги нумеруются сквозь всю статью, как в книге: «Листинг 3. ...».
 * Номер и подсказку справа генерирует CSS, подпись берётся из <summary>.
 */

.article details {
  margin: 0.4em 0;
  counter-increment: listing;
}

.article summary {
  display: flow-root;
  cursor: pointer;
  list-style: none;
}

.article summary::-webkit-details-marker {
  display: none;
}

.article summary::before {
  content: "Листинг " counter(listing) ".\00a0";
  font-weight: 700;
}

/* Подсказка прижата вправо на последней строке подписи; кегль меньше, интерлиньяж тот же. */
.article summary::after {
  content: "показать код";
  float: right;
  padding-left: 1.5em;
  font-size: 0.8em;
  font-style: italic;
  line-height: 1.875;
}

.article details[open] > summary::after {
  content: "свернуть";
}

.article summary:hover::after {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

/*
 * Кнопка копирования лежит на верхней линии рамки, как подпись в разрыве:
 * отрицательный нижний отступ подтягивает рамку к середине кнопки,
 * белый фон кнопки перекрывает линию. Код она не загораживает.
 */
.copy-button {
  display: block;
  position: relative;
  z-index: 1;
  width: fit-content;
  margin: 0.5em 0.8em -0.75em auto;
  padding: 0 0.4em;
  border: 0;
  background: var(--paper);
  color: var(--ink);
  font: italic 16px/1.5 var(--serif);
  cursor: pointer;
}

.copy-button:hover {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

.copy-button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.article .copy-button + pre {
  margin-top: 0;
}

.article pre {
  margin: 0.5em 0 1.2em;
  padding: 1em 1.2em;
  border: 1px solid var(--ink);
  overflow-x: auto;
  font-size: 15px;
  line-height: 1.5;
  tab-size: 4;
  overflow-wrap: normal;
  hyphens: none;
}

/* Таблица в книжном стиле: толстые линейки сверху и снизу, тонкая под шапкой */

.table-wrap {
  margin: 1em 0;
  overflow-x: auto;
}

.article table {
  width: 100%;
  min-width: 33em;
  border-collapse: collapse;
  font-size: 17px;
  line-height: 1.35;
}

.article th,
.article td {
  padding: 0.45em 0.8em 0.45em 0;
  text-align: left;
  vertical-align: top;
  hyphens: manual;
}

.article th:last-child,
.article td:last-child {
  padding-right: 0;
}

.article thead th {
  border-top: 2px solid var(--ink);
  border-bottom: 1px solid var(--ink);
  vertical-align: bottom;
}

.article tbody tr:last-child td {
  border-bottom: 2px solid var(--ink);
}

/* Глоссарий */

.article dl {
  margin: 0;
}

.article dt {
  margin-top: 0.6em;
  font-weight: 700;
}

.article dd {
  margin: 0 0 0 1.5em;
  hyphens: auto;
}

/*
 * Схема потока: сетка из трёх колонок (ось, стрелка вправо, ответвление).
 * Элементы оси (.spine) стоят по центру первой колонки, поэтому вертикальные
 * стрелки совпадают с центрами рамок. Наконечники стрелок: треугольники из border.
 */

.flow {
  display: grid;
  grid-template-columns: max-content auto auto;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  margin: 1.5em auto 2em;
  font-size: 18px;
  line-height: 1.35;
}

/* Фигуры оси растянуты на ширину колонки: стрелки вправо начинаются от их края. */
.flow .spine {
  grid-column: 1;
  justify-self: stretch;
  text-align: center;
}

.flow .spine.down {
  justify-self: center;
}

/* Ответвления по своей ширине, без растяжения на всю колонку. */
.flow > :not(.spine) {
  justify-self: start;
}

.flow .term,
.flow .terminator,
.flow .process,
.flow .data {
  font-style: italic;
}

/* Подпись без рамки той же высоты, что и фигура: стрелки попадают в её середину. */
.flow .term {
  padding: 0.3em 0;
}

/* Фигуры по ГОСТ 19.701 */

.flow .terminator,
.flow .process,
.flow .data,
.flow .node {
  position: relative;
  padding: 0.3em 1em;
  border: 1px solid var(--ink);
}

.flow .terminator {
  border-radius: 999px;
}

.flow .data {
  border: 0;
}

.flow .data::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid var(--ink);
  transform: skewX(-15deg);
}

.flow .node {
  padding: 0.3em 1.3em;
  text-decoration: none;
}

.flow .node::before,
.flow .node::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  border-left: 1px solid var(--ink);
}

.flow .node::before {
  left: 0.45em;
}

.flow .node::after {
  right: 0.45em;
}

.flow .node:hover {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.15em;
}

.flow .branch {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4em 0.6em;
}

/* Стрелка начинается от края фигуры и почти касается следующей. */
.flow .right,
.flow .assoc {
  position: relative;
  width: 4em;
  height: 0;
  margin: 0 0.15em 0 0;
  border-top: 1px solid var(--ink);
}

.flow .branch .right {
  width: 3em;
  margin: 0 -0.45em 0 -0.6em;
}

/* Цепочка со стрелкой внутри не переносится, иначе стрелка оторвётся от цели. */
.flow .chain {
  flex-wrap: nowrap;
}

.flow .chain .process {
  min-width: 0;
}

.flow .assoc {
  border-top-style: dashed;
}

.flow .right::after {
  content: "";
  position: absolute;
  top: -4.5px;
  right: -1px;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-left: 7px solid var(--ink);
}

.flow .down {
  position: relative;
  width: 0;
  height: 2em;
  margin: 0.3em 0;
  border-left: 1px solid var(--ink);
}

.flow .down::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: -4.5px;
  border-top: 7px solid var(--ink);
  border-right: 4px solid transparent;
  border-left: 4px solid transparent;
}

.flow .label {
  position: absolute;
  font-size: 15px;
  font-style: italic;
  line-height: 1;
  white-space: nowrap;
}

.flow .right .label {
  bottom: 0.25em;
  left: 50%;
  transform: translateX(-50%);
}

.flow .down .label {
  top: 50%;
  left: 0.6em;
  transform: translateY(-50%);
}

.back {
  margin: 4em 0 0;
  font-style: italic;
  text-align: center;
}

@media (max-width: 600px) {
  body {
    font-size: 18px;
  }

  .page {
    padding-top: 1.5em;
  }

  .running-head {
    margin-bottom: 3em;
  }

  .title-page {
    margin-top: 10vh;
  }

  .title-page h1 {
    font-size: 48px;
  }

  .article h1 {
    font-size: 32px;
  }

  .article .subtitle {
    font-size: 19px;
  }

  .article h2 {
    font-size: 21px;
  }

  .article pre {
    padding: 0.8em 0.9em;
    font-size: 14px;
  }

  .article table {
    font-size: 16px;
  }

  /*
   * На узком экране схема становится деревом: ось уходит к левому краю,
   * фигуры оси во всю ширину, ответвления отходят от оси отводом во вторую
   * колонку. Стрелка вправо (.right, .assoc) превращается в отвод: вертикаль
   * оси через всю строку и горизонтальный штрих к ответвлению.
   */
  /*
   * Отвод указывает на первую строку ответвления: 1.05em это середина фигуры
   * (интерлиньяж 1.35 плюс поля 0.3em и рамка). Ответвления прижаты к верху
   * строки, чтобы их первая фигура была на этой высоте, даже если они переносятся.
   */
  .flow {
    --stub-y: 1.05em;
    grid-template-columns: 2.8em minmax(0, 1fr);
    width: 100%;
    font-size: 16px;
  }

  .flow .spine {
    grid-column: 1 / -1;
  }

  .flow .spine.down {
    justify-self: start;
    margin-left: 0.9em;
  }

  .flow > :not(.spine) {
    align-self: start;
  }

  .flow > .right,
  .flow > .assoc {
    grid-column: 1;
    align-self: stretch;
    width: 0;
    height: auto;
    margin: 0 0 0 0.9em;
    border-top: 0;
    border-left: 1px solid var(--ink);
  }

  /* Последнее ответвление: отвод «└», ось ниже него не продолжается. */
  .flow > .right:nth-last-child(2) {
    align-self: start;
    height: var(--stub-y);
  }

  .flow > .right::before,
  .flow > .assoc::before {
    content: "";
    position: absolute;
    top: var(--stub-y);
    left: 0;
    width: 1.6em;
    border-top: 1px solid var(--ink);
  }

  .flow > .right:nth-last-child(2)::before {
    top: 100%;
  }

  .flow > .assoc::before {
    border-top-style: dashed;
  }

  .flow > .right::after {
    top: calc(var(--stub-y) - 4.5px);
    right: auto;
    left: calc(1.6em - 7px);
  }

  .flow > .right:nth-last-child(2)::after {
    top: calc(100% - 4.5px);
  }

  .flow > .right .label {
    top: 0.05em;
    bottom: auto;
    left: 0.35em;
    transform: none;
  }

  .flow .branch {
    align-items: flex-start;
  }

  .flow .branch .right {
    width: 2em;
    margin-top: var(--stub-y);
  }

  .flow .chain .node {
    white-space: nowrap;
  }
}

@media print {
  body {
    font-size: 11pt;
  }

  .page {
    max-width: none;
    padding: 0;
  }

  .running-head,
  .contents,
  .back {
    display: none;
  }

  .article h2,
  .article h3 {
    break-after: avoid;
  }

  .article summary::after,
  .copy-button {
    display: none;
  }

  .article pre {
    white-space: pre-wrap;
    break-inside: avoid;
    font-size: 9pt;
  }

  .table-wrap {
    overflow: visible;
  }

  .article table {
    min-width: 0;
    font-size: 10pt;
  }
}

/*
 * Раскрыть свёрнутые <details> при печати. Отдельным правилом: браузер,
 * не знающий ::details-content, отбросит только его, а не весь блок выше.
 */
@media print {
  details::details-content {
    content-visibility: visible;
  }
}
