/* 縦書きリーダー。配色は quartz.config.default.yaml の値に合わせている */

:root {
  --paper: #f8f8f8;
  --ink: #100f0f;
  --ink-weak: #6f6b68;
  --rule: #cecdc3;
  --panel: #f2f0e5;
  --accent: #5e409d;
  --shadow: rgba(16, 15, 15, 0.12);
  --lines-per-page: 24;
  --line-length-max: 38;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #100f0f;
    --ink: #cecdc3;
    --ink-weak: #9f9898;
    --rule: #403e3c;
    --panel: #282726;
    --accent: #a68adf;
    --shadow: rgba(0, 0, 0, 0.5);
  }
}

:root[data-theme="dark"] {
  --paper: #100f0f;
  --ink: #cecdc3;
  --ink-weak: #9f9898;
  --rule: #403e3c;
  --panel: #282726;
  --accent: #a68adf;
  --shadow: rgba(0, 0, 0, 0.5);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: "Noto Serif JP", "Hiragino Mincho ProN", "Yu Mincho", serif;
  -webkit-text-size-adjust: 100%;
}

body[data-mode="horizontal"] {
  overflow: auto;
}

/* 紙面 */

#stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 3.4rem 1rem 2.6rem;
}

#viewport {
  position: relative;
  overflow: hidden;
}

#flow {
  position: absolute;
  inset: 0;
  transition: transform 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
}

/* 節は帯（トラック）の入れ物。本文を流すのは帯のほう */
#flow section {
  position: absolute;
  top: 0;
  height: 100%;
}

#flow .track {
  position: absolute;
  right: 0;
  width: max-content;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  line-break: strict;
  overflow-wrap: normal;
  word-break: normal;
}

/* 帯を並べられない画面では順に読ませるので、どの帯かを名乗らせる */
body.stacked #flow .track[data-role]::before {
  content: attr(data-role);
  display: block;
  line-height: var(--step);
  padding-block-end: var(--step);
  font-family: system-ui, sans-serif;
  font-size: 0.7em;
  letter-spacing: 0.3em;
  color: var(--ink-weak);
}

/* 灰の語りのような、余白に置かれる副の声 */
#flow .track.faint {
  color: color-mix(in srgb, var(--ink) 58%, var(--paper));
}

/* 傾斜する帯の、1ページぶんの切れ端。中身は組み付けのたびに切り直す */
#flow .track.slice {
  overflow: hidden;
}

#flow p {
  margin: 0;
  text-indent: 1em;
}

#flow p.gap {
  text-indent: 0;
}

/* 行頭がカギ括弧の段落。括弧そのものが字下げの役を果たすので、
   これを字下げすると一字ぶん余計に空く。原稿側の行頭全角スペースの
   有無に関わらず、この規則で揃える（判定は reader.js が行う） */
#flow p.speech {
  text-indent: 0;
}

/* レイヤー（閲覧者）。縦組では inline の始まりが天なので、段下げは
   そのまま天からの食い込みになる。横書きなら左からの字下げになる。
   段差は行送りと直交する量なので、ページの行グリッドには影響しない。
   --d は深さ（0 が最上段）、--indent は1段ぶんの量で reader.js が渡す */
#flow p[data-depth] {
  margin-inline-start: calc(var(--d, 0) * var(--indent, 1em));
  text-indent: 0;
}

/* エピグラフと出典注記 */
#flow p.quote {
  font-size: 0.86em;
  margin-inline: 2em 1em;
  text-indent: 0;
}

#flow p.quote-by {
  font-size: 0.86em;
  margin-inline: 2em 1em;
  text-indent: 0;
  text-align: end;
}

/* 画面からの引用。縦組では欧字が倒れるので、書体だけ等幅にする */
#flow p.code {
  font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
  font-size: 0.86em;
  text-indent: 0;
}

/* 明朝の合成ボールドは縦組で汚いので、ゴシックに振る */
#flow strong {
  font-weight: 500;
  font-family: "Noto Sans JP", "Hiragino Sans", "Yu Gothic", sans-serif;
}

/* ページをまたいだ段落の、続きのほう */
#flow p.cont {
  text-indent: 0;
}

/* 見出しの下のアキは、かならず行送りちょうど1本分にする。
   端数が混じるとそれ以降の行がページの行グリッドから外れ、
   ページの境目で行が切れてしまう */
#flow h2,
#flow h3 {
  margin: 0;
  font-size: 1em;
  font-weight: 600;
  letter-spacing: 0.25em;
  padding-block-end: var(--step, 1.9em);
}

#flow ruby rt {
  font-size: 0.5em;
  font-weight: 400;
  letter-spacing: 0;
}

.tcy {
  text-combine-upright: all;
}

#flow em.bouten {
  font-style: normal;
  text-emphasis: filled sesame;
  -webkit-text-emphasis: filled sesame;
}

/* テイスティングノートのような、本文と地続きだが書式の違う行。
   行送りは px で継いでいるので、字だけ小さくしても行のグリッドは崩れない */
#flow p.small {
  font-size: 0.86em;
}

/* オーディオコメンタリの映像描写のような、地の文の塊 */
#flow p.desc,
#flow p.desc-open {
  color: color-mix(in srgb, var(--ink) 76%, var(--paper));
}

#flow p.desc-open {
  text-indent: 0;
}

/* 註釈 */

#flow a.noteref {
  font-size: 0.55em;
  text-combine-upright: all;
  vertical-align: super;
  color: var(--accent);
  text-decoration: none;
  padding-inline: 0.15em;
  cursor: pointer;
}

#flow a.noteref:hover {
  text-decoration: underline;
}

/* 註釈の番号。段落の頭に組み込むので、行送りには影響しない */
#flow span.note-no {
  font-size: 0.8em;
  font-weight: 600;
  color: var(--ink-weak);
  text-combine-upright: all;
  padding-inline-end: 0.6em;
  scroll-margin: 2rem;
}

/* 図版のページ */

#flow section.plate {
  display: grid;
  place-items: center;
  padding: 1.2rem;
}

#flow figure {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.9rem;
  max-width: 100%;
  max-height: 100%;
}

#flow figure img {
  max-width: 100%;
  max-height: calc(100% - 2.6rem);
  object-fit: contain;
  background: #fff;
  border: 1px solid var(--rule);
  border-radius: 2px;
  cursor: zoom-in;
}

#flow figcaption {
  font-family: system-ui, sans-serif;
  font-size: 0.78rem;
  color: var(--ink-weak);
  text-align: center;
}

/* 図版の拡大 */

#plate {
  position: fixed;
  inset: 0;
  z-index: 6;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  backdrop-filter: blur(4px);
  cursor: zoom-out;
}

#plate[hidden] {
  display: none;
}

#plate img {
  max-width: 100%;
  max-height: 100%;
  background: #fff;
  border: 1px solid var(--rule);
}

/* 横書きモード */

body[data-mode="horizontal"] #stage {
  position: static;
  display: block;
  padding: 4.2rem 1.2rem 4rem;
}

body[data-mode="horizontal"] #viewport,
body[data-mode="horizontal"] #flow {
  position: static;
  overflow: visible;
  width: auto;
  height: auto;
  transform: none !important;
  margin: 0 auto;
  max-width: 34em;
}

body[data-mode="horizontal"] #flow section {
  position: static;
  width: auto;
  height: auto;
  margin-block-end: 3em;
}

/* 横書きでは帯を並置できないので、上から順に読ませる */
body[data-mode="horizontal"] #flow .track {
  position: static;
  width: auto;
  height: auto !important;
  font-size: inherit !important;
  line-height: 1.9 !important;
  writing-mode: horizontal-tb;
  margin-block-end: 2em;
}

body[data-mode="horizontal"] #flow .track[data-role]::before {
  content: attr(data-role);
  display: block;
  margin-block-end: 0.8em;
  font-family: system-ui, sans-serif;
  font-size: 0.75em;
  letter-spacing: 0.2em;
  color: var(--ink-weak);
}

body[data-mode="horizontal"] #flow h2 {
  letter-spacing: 0.1em;
  padding-block: 0 1em;
}

body[data-mode="horizontal"] #flow a.noteref,
body[data-mode="horizontal"] #flow span.note-no {
  text-combine-upright: none;
}

body[data-mode="horizontal"] #flow section.plate {
  display: block;
  padding: 0;
}

body[data-mode="horizontal"] #flow figure img {
  max-height: none;
}

/* 操作系 */

#bar,
#status {
  position: fixed;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.9rem;
  font-family: system-ui, sans-serif;
  font-size: 0.8rem;
  color: var(--ink-weak);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(6px);
  transition: opacity 0.2s;
}

#bar {
  inset: 0 0 auto 0;
  border-block-end: 1px solid var(--rule);
}

#status {
  inset: auto 0 0 0;
  justify-content: center;
  border-block-start: 1px solid var(--rule);
  font-variant-numeric: tabular-nums;
}

body.ui-hidden #bar,
body.ui-hidden #status {
  opacity: 0;
  pointer-events: none;
}

#work-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
  font-family: inherit;
}

#bar button,
#toc button,
#status button {
  font: inherit;
  color: var(--ink-weak);
  background: none;
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
}

#bar button:hover,
#toc button:hover,
#status button:hover {
  color: var(--ink);
  border-color: var(--accent);
}

#back-note {
  color: var(--accent);
  border-color: var(--accent);
}

#back-note[hidden] {
  display: none;
}

#bar a {
  color: var(--ink-weak);
  text-decoration: none;
  border-bottom: 1px solid var(--rule);
}

#bar a:hover {
  color: var(--accent);
}

/* ページ送り */

.turn {
  position: fixed;
  z-index: 2;
  top: 0;
  bottom: 0;
  width: 18%;
  border: 0;
  background: none;
  cursor: pointer;
  color: var(--ink-weak);
  opacity: 0;
  transition: opacity 0.2s;
  font-size: 1.6rem;
}

.turn:hover,
.turn:focus-visible {
  opacity: 0.7;
}

#next {
  left: 0;
}

#prev {
  right: 0;
}

body[data-mode="horizontal"] .turn {
  display: none;
}

/* 目次 */

#toc {
  position: fixed;
  z-index: 4;
  inset: 0 0 0 auto;
  width: min(20rem, 82vw);
  padding: 3.4rem 1.2rem 1.2rem;
  overflow-y: auto;
  background: var(--panel);
  border-inline-start: 1px solid var(--rule);
  box-shadow: -8px 0 24px var(--shadow);
  font-family: system-ui, sans-serif;
  font-size: 0.85rem;
}

#toc[hidden] {
  display: none;
}

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

#toc li {
  border-block-end: 1px solid var(--rule);
}

#toc li button {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
  border: 0;
  border-radius: 0;
  padding: 0.7rem 0.2rem;
  text-align: start;
}

#toc li button span:last-child {
  color: var(--ink-weak);
  font-variant-numeric: tabular-nums;
}

#toc .meta {
  margin: 0 0 1rem;
  color: var(--ink-weak);
  line-height: 1.7;
}

/* 状態表示 */

#message {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 2rem;
  text-align: center;
  line-height: 1.9;
  background: var(--paper);
}

#message[hidden] {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  #flow {
    transition: none;
  }
}
