:root {
  --background: #191919;
  --text: #f5f5f5;
  --muted: #f5f5f580;
  --faint: #f5f5f55c;
  --font: system-ui, sans-serif;
  --serif: Iowan Old Style, Palatino, Palatino Linotype, Georgia, serif;
  --flip-duration: 860ms;
  --flip-ease: cubic-bezier(0.42, 0.02, 0.18, 1);
  --margin: 16px;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  overflow: hidden;
  background: var(--background);
  color: var(--text);
  font-family: var(--font);
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
}

.reader {
  height: 100%;
  padding: var(--margin);
}

.surface {
  position: relative;
  height: 100%;
  background: var(--background);
  perspective: 2000px;
}

.pages {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 100%;
  background: var(--background);
  transform-style: preserve-3d;
}

.sheet,
.leaf-face {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: var(--background);
}

.arrow {
  position: absolute;
  z-index: 6;
  top: 50%;
  width: 36px;
  height: 56px;
  display: grid;
  place-items: center;
  translate: 0 -50%;
  color: var(--muted);
  cursor: pointer;
}

#prev {
  left: 8px;
}

#next {
  right: 8px;
}

.arrow svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.arrow:hover:not(:disabled) {
  color: var(--text);
}

.arrow:focus-visible {
  outline: 1px solid #f5f5f533;
  outline-offset: 3px;
}

.arrow:disabled {
  opacity: 0.28;
  cursor: default;
}

.status {
  position: absolute;
  z-index: 6;
  left: 28px;
  right: 28px;
  bottom: 18px;
  text-align: center;
  font-size: 12px;
  letter-spacing: 0.04em;
  color: var(--faint);
}

.leaf {
  position: absolute;
  z-index: 5;
  top: 0;
  bottom: 0;
  width: 50%;
  visibility: hidden;
  pointer-events: none;
  transform-style: preserve-3d;
}

.leaf.is-on {
  visibility: visible;
}

.leaf.from-right {
  left: 50%;
  transform-origin: left center;
}

.leaf.from-left {
  left: 0;
  transform-origin: right center;
}

.leaf.flip-next,
.leaf.flip-prev {
  will-change: transform;
}

.leaf.flip-next {
  animation: flip-next var(--flip-duration) var(--flip-ease) forwards;
}

.leaf.flip-prev {
  animation: flip-prev var(--flip-duration) var(--flip-ease) forwards;
}

.leaf-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.leaf-back {
  transform: rotateY(180deg);
}

.page {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  padding:
    clamp(28px, 5vh, 56px)
    clamp(28px, 3vw, 48px)
    clamp(44px, 7vh, 64px);
  overflow: hidden;
  color: var(--text);
  font-family: var(--serif);
}

.side-left {
  padding-left: clamp(52px, 5vw, 72px);
}

.side-right {
  padding-right: clamp(52px, 5vw, 72px);
}

.page-kicker {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: clamp(10px, 1.6vh, 12px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.page-title {
  margin: clamp(10px, 2vh, 16px) 0 clamp(12px, 2.2vh, 18px);
  font-size: clamp(22px, 3.6vh, 30px);
  font-weight: 500;
  letter-spacing: -0.03em;
  line-height: 1.12;
}

.page-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  max-width: 38em;
  font-size: clamp(15px, 2.1vh, 17px);
  line-height: 1.5;
  color: var(--text);
}

.page-body p + p {
  margin-top: 0.95em;
}

.text {
  min-height: 1.5em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  outline: none;
}

.text-block {
  position: relative;
}

.text-block + .text-block,
.text-block + .code-block,
.code-block + .text-block,
.code-block + .code-block {
  margin-top: 0.95em;
}

.plus {
  position: absolute;
  top: 0.05em;
  left: -30px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  color: var(--faint);
  opacity: 0;
  cursor: pointer;
}

.plus svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

.text-block:hover .plus,
.text-block:focus-within .plus {
  opacity: 1;
}

.plus:hover,
.plus:focus-visible {
  color: var(--text);
}

.code-block {
  position: relative;
  border-radius: 6px;
  background: #141414;
  padding: 30px 12px 10px;
}

.code-tools {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 2px;
}

.code-tool {
  position: relative;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  color: var(--muted);
  cursor: pointer;
}

.code-tool svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.code-tool:hover,
.code-tool:focus-visible {
  color: var(--text);
  background: #f5f5f514;
}

.code-tool[data-feedback]::after {
  content: attr(data-feedback);
  position: absolute;
  top: calc(100% + 2px);
  right: 0;
  font-family: var(--font);
  font-size: 11px;
  letter-spacing: 0;
  color: var(--text);
  white-space: nowrap;
}

.code {
  display: block;
  width: 100%;
  min-height: 1.45em;
  border: 0;
  padding: 0;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--text);
  font: 13.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  outline: none;
}

.page-title[contenteditable] {
  outline: none;
}

.page-num {
  position: absolute;
  bottom: 18px;
  font-family: var(--font);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
}

.side-left .page-num {
  left: 28px;
}

.side-right .page-num,
.side-single .page-num {
  right: 28px;
}

.page.title-page {
  justify-content: center;
}

.page.title-page .page-kicker {
  margin-bottom: 14px;
}

.page.title-page .page-title {
  margin: 0;
  font-size: clamp(34px, 6vh, 52px);
  font-weight: 500;
  letter-spacing: -0.04em;
}

.page.title-page .page-body {
  flex: 0;
  margin-top: 14px;
  overflow: visible;
  color: var(--muted);
  font-size: clamp(15px, 2.4vh, 18px);
}

@keyframes flip-next {
  0% {
    transform: rotateY(0deg) translateZ(0);
  }
  45% {
    transform: rotateY(-78deg) translateZ(28px);
  }
  100% {
    transform: rotateY(-180deg) translateZ(0);
  }
}

@keyframes flip-prev {
  0% {
    transform: rotateY(0deg) translateZ(0);
  }
  45% {
    transform: rotateY(78deg) translateZ(28px);
  }
  100% {
    transform: rotateY(180deg) translateZ(0);
  }
}

@media (max-width: 720px) {
  .pages {
    grid-template-columns: 1fr;
  }

  .sheet-right {
    display: none;
  }

  .leaf,
  .leaf.from-right,
  .leaf.from-left {
    left: 0;
    width: 100%;
  }

  .page,
  .side-left,
  .side-right,
  .side-single {
    padding-right: clamp(52px, 8vw, 72px);
    padding-left: clamp(52px, 8vw, 72px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .leaf.flip-next,
  .leaf.flip-prev {
    animation: none;
  }
}
