:root {
  color-scheme: dark;
  background: #211c18;
  color: #f5e7d0;
  font-family: system-ui, "Microsoft YaHei", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --left-gutter: max(clamp(1rem, 5vw, 4.5rem), calc(var(--safe-left) + .75rem));
  --right-gutter: max(clamp(1rem, 5vw, 4.5rem), calc(var(--safe-right) + .75rem));
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(ellipse at 50% 25%, #463728 0, #29211a 52%, #201b17 100%);
}

.masthead, .brand, .toolbar, .languages, #jump {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.masthead {
  min-height: 4.25rem;
  padding: calc(.875rem + var(--safe-top)) var(--right-gutter) .875rem var(--left-gutter);
  justify-content: space-between;
  gap: .75rem;
  border-bottom: 1px solid #6d5743;
  background: #261f19;
}

.brand {
  align-items: baseline;
  min-width: 0;
  gap: 0 1rem;
}

h1 {
  margin: 0;
  font: 700 2rem / 1.08 "Songti SC", SimSun, serif;
  letter-spacing: .1em;
  overflow-wrap: anywhere;
}

.brand span, .bookshelf-link { color: #d9bda0; font-size: .8125rem; }
.bookshelf-link {
  display: inline-flex;
  align-items: center;
  min-height: 2.75rem;
  padding-inline: .25rem;
  text-underline-offset: .2em;
}

.toolbar { gap: .5rem 1rem; }
.languages { gap: .375rem; }
#jump { gap: .5rem; }
label { color: #ddc1a4; font-size: .8125rem; }

button, input { font: inherit; color: inherit; }
button, #home, .navigation a, .languages a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  padding: .4375rem .75rem;
  gap: .25em;
  border: 1px solid #8b7056;
  border-radius: .3125rem;
  background: #35291f;
  color: inherit;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

@media (hover: hover) {
  button:hover:not(:disabled), #home[href]:hover, .navigation a[href]:hover, .languages a[href]:hover {
    background: #503c2a;
    border-color: #cca982;
  }
}

button:disabled, #home[aria-disabled="true"], .navigation a[aria-disabled="true"] {
  opacity: .42;
  cursor: default;
}

.languages a { font-size: .875rem; }
.languages a[aria-current="true"] { background: #614731; border-color: #d9b08a; }
:focus-visible { outline: 2px solid #f5c586; outline-offset: 2px; }

input {
  width: 3.8125rem;
  min-height: 2.75rem;
  padding: .375rem;
  border: 1px solid #8b7056;
  border-radius: .3125rem;
  background: #241c17;
  font-size: 1rem;
  text-align: center;
  appearance: textfield;
}

input::-webkit-inner-spin-button, input::-webkit-outer-spin-button { appearance: none; }

main {
  display: grid;
  justify-items: center;
  gap: .8125rem;
  padding: 1.125rem max(.875rem, var(--safe-right)) calc(.875rem + var(--safe-bottom)) max(.875rem, var(--safe-left));
}

.reading-area { display: grid; place-items: center; width: 100%; perspective: 1500px; }
.book {
  position: relative;
  width: min(100%, 40rem);
  width: min(100%, 40rem, max(17.5rem, calc((100vh - 10.5rem) * 2 / 3)));
  width: min(100%, 40rem, max(17.5rem, calc((100dvh - 10.5rem) * 2 / 3)));
  aspect-ratio: 2 / 3;
  background: #ccaf8c;
  box-shadow: 0 0 0 1px #c3a48275, 0 7px 0 -4px #c4a684, 0 10px 0 -6px #9d7c5e, 0 17px 38px #100b08a6;
  isolation: isolate;
  touch-action: pan-y pinch-zoom;
}

.book[data-zoomed] { touch-action: auto; }

@supports not (aspect-ratio: 2 / 3) {
  .book::before { display: block; padding-top: 150%; content: ""; }
}

.page, .turning, .artwork {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  user-select: none;
}

.artwork { object-fit: contain; }
.lettering { position: absolute; display: block; user-select: none; pointer-events: none; }
.turning {
  z-index: 2;
  overflow: hidden;
  backface-visibility: hidden;
  transform-origin: left center;
  pointer-events: none;
}

.turning.back { transform-origin: right center; }
.turning::after {
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 38%, #18120c55);
  opacity: 0;
  content: "";
}

.turning.back::after { background: linear-gradient(260deg, transparent 38%, #18120c55); }
.turning.active::after { opacity: 1; transition: opacity .35s ease-out; }

.navigation {
  display: grid;
  width: min(100%, 30.625rem);
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr) minmax(0, 1fr);
  align-items: center;
  gap: .75rem;
}

.navigation a:first-child { justify-self: start; }
.navigation a:last-child { justify-self: end; border-color: #b99471; background: #59402c; }
.navigation a { max-width: 100%; }
.navigation a span, output { overflow-wrap: anywhere; }
output { color: #e6cbb0; font-size: .875rem; font-variant-numeric: tabular-nums; text-align: center; }
#status { margin: 0; color: #ffd5b0; font-size: .875rem; }

@media (max-width: 48rem), (orientation: landscape) and (max-height: 31rem) {
  :root {
    --left-gutter: calc(.75rem + var(--safe-left));
    --right-gutter: calc(.75rem + var(--safe-right));
  }

  body {
    display: grid;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .masthead {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: .375rem;
    padding: calc(.25rem + var(--safe-top)) var(--right-gutter) .4375rem var(--left-gutter);
  }

  .brand { align-items: center; gap: 0 .625rem; }
  .brand span { display: none; }
  h1 { font-size: clamp(1.25rem, 5vw, 1.625rem); letter-spacing: .045em; }
  .bookshelf-link { margin-inline-start: auto; }

  .toolbar { display: grid; grid-template-columns: minmax(0, 1fr); gap: .375rem; }
  .languages {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 3.25rem), 1fr));
    gap: .3125rem;
  }

  .languages a { min-width: 0; padding: .1875rem; font-size: .8125rem; overflow-wrap: anywhere; }
  #jump { justify-content: flex-end; gap: .375rem; }
  #jump label {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  #home { margin-inline-end: auto; }

  main {
    min-height: 0;
    grid-template-rows: minmax(0, 1fr) auto auto;
    justify-items: stretch;
    gap: 0;
    padding: 0 0 var(--safe-bottom);
    background: #261f19;
  }

  .reading-area {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    padding: .625rem var(--right-gutter) .875rem var(--left-gutter);
    background: radial-gradient(ellipse at 50% 25%, #463728 0, #29211a 52%, #201b17 100%);
  }

  .book { flex: 0 0 auto; width: min(100%, 28rem); margin-block: auto; }
  .navigation {
    width: 100%;
    gap: .375rem;
    padding: .4375rem var(--right-gutter) .4375rem var(--left-gutter);
    border-top: 1px solid #6d5743;
  }

  .navigation a { width: 100%; padding: .25rem; font-size: .875rem; }
  output { font-size: .8125rem; }
  #status { padding: 0 var(--right-gutter) .4375rem var(--left-gutter); }
}

@media (orientation: landscape) and (max-height: 31rem) {
  .masthead { grid-template-columns: auto minmax(0, 1fr); }
  .toolbar { display: flex; justify-content: flex-end; }
  .languages { flex: 1 1 17.5rem; min-width: 0; }
  #jump { max-width: 100%; }
  .bookshelf-link { margin-inline-start: 0; }
  .book { width: min(100%, 26rem, max(11rem, calc((100vh - 8rem) * 2 / 3))); }

  @supports (width: 100cqh) {
    .reading-area { container-type: size; }
    .book { width: min(100%, 26rem, max(11rem, calc(100cqh * 2 / 3))); }
  }
}

@media (prefers-reduced-motion: reduce) {
  .turning::after { transition: none; }
}
