/* ============================================================
   APPY — the reading room
   A closed book that opens, a spread you can turn, and a
   plain list for anyone who would rather not.
   ============================================================ */

body[data-immersive="true"] {
  overflow: hidden;
}

.view > .reader {
  max-width: none;
}

.reader {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  flex-direction: column;
  background: var(--paper-2);
  color: var(--ink);
  animation: fade-in var(--mid) var(--ease) both;
}

.reader[data-view="list"] {
  background: var(--paper);
}

/* ---------- bar ---------- */

.reader-bar {
  position: relative;
  z-index: 30;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--s-3);
  padding: 0.7rem clamp(0.9rem, 2.4vw, 1.8rem);
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--paper-2) 88%, transparent);
  backdrop-filter: blur(8px);
}

.reader-bar-start {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.reader-bar-end {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.2rem;
}

.reader-title {
  margin: 0;
  font-size: 0.92rem;
  letter-spacing: 0.02em;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--ink-2);
}

.reader-title span {
  display: block;
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.58rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.reader-tool {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 2.25rem;
  padding: 0.3rem 0.6rem;
  border-radius: var(--r-sm);
  color: var(--muted);
  font-family: var(--sans);
  font-size: var(--t-xs);
  letter-spacing: 0.06em;
  white-space: nowrap;
}

.reader-tool:hover {
  background: color-mix(in srgb, var(--ink) 7%, transparent);
  color: var(--ink);
}

.reader-tool[aria-pressed="true"],
.reader-tool[aria-expanded="true"] {
  background: color-mix(in srgb, var(--ink) 9%, transparent);
  color: var(--ink);
}

.reader-tool svg {
  width: 1rem;
  height: 1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- stage & book ---------- */

.reader-stage {
  position: relative;
  display: grid;
  place-items: center;
  flex: 1;
  min-height: 0;
  padding: clamp(1rem, 3vh, 2.2rem) clamp(2.6rem, 6vw, 5rem);
  perspective: 2200px;
}

.book {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  width: min(64rem, 100%);
  height: min(78vh, 44rem);
  border-radius: 3px;
  background: var(--surface);
  box-shadow: var(--shadow-book);
  transform-style: preserve-3d;
  transition: opacity var(--slow) var(--ease), transform var(--slow) var(--ease-out);
}

.reader[data-state="opening"] .book {
  opacity: 0;
  transform: scale(0.97);
}

.reader[data-state="open"] .book {
  opacity: 1;
  transform: none;
}

.book::before {
  /* the gutter */
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 3.5rem;
  transform: translateX(-50%);
  background: linear-gradient(
    90deg,
    transparent,
    color-mix(in srgb, var(--ink) 11%, transparent) 42%,
    color-mix(in srgb, var(--ink) 14%, transparent) 50%,
    color-mix(in srgb, var(--ink) 11%, transparent) 58%,
    transparent
  );
  pointer-events: none;
  z-index: 3;
}

.book::after {
  /* the closed fore-edge on the right */
  content: "";
  position: absolute;
  top: 5px;
  bottom: 5px;
  right: 0;
  width: 2px;
  background: linear-gradient(
    to right,
    color-mix(in srgb, var(--ink) 10%, transparent),
    transparent
  );
  pointer-events: none;
}

.page {
  position: relative;
  min-width: 0;
  overflow: hidden;
}

.page--left {
  border-radius: 3px 0 0 3px;
  box-shadow: inset -1px 0 0 color-mix(in srgb, var(--ink) 7%, transparent);
}

.page--right {
  border-radius: 0 3px 3px 0;
}

.page-inner {
  height: 100%;
  padding: clamp(1.4rem, 3vw, 2.6rem) clamp(1.3rem, 3vw, 2.9rem)
    clamp(2.4rem, 3vw, 3rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--rule) transparent;
}

.page-inner::-webkit-scrollbar {
  width: 6px;
}

.page-inner::-webkit-scrollbar-thumb {
  background: var(--rule);
  border-radius: 3px;
}

.page--left .page-inner {
  padding-left: clamp(1.6rem, 3.4vw, 3.2rem);
}

.folio {
  position: absolute;
  bottom: 0.95rem;
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  pointer-events: none;
}

.page--left .folio {
  left: 50%;
  transform: translateX(-50%);
}

.page--right .folio {
  left: 50%;
  transform: translateX(-50%);
}

/* ---------- the page itself ---------- */

.reader-entry {
  max-width: 30rem;
  margin: 0 auto;
  animation: page-in var(--mid) var(--ease-out) both;
}

@keyframes page-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

.re-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.55rem;
}

.re-date {
  margin: 0;
  color: var(--clay);
  font-family: var(--sans);
  font-size: 0.63rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.re-mood {
  margin: 0;
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.63rem;
  font-style: italic;
  letter-spacing: 0.04em;
}

.re-title {
  margin: 0 0 1.1rem;
  padding-bottom: 0.9rem;
  border-bottom: 1px solid var(--rule-2);
  font-family: var(--serif);
  font-size: clamp(1.3rem, 1.9vw, 1.62rem);
  font-weight: 400;
  line-height: 1.24;
  letter-spacing: -0.014em;
  text-wrap: balance;
}

.re-title.is-untitled {
  color: var(--muted);
  font-style: italic;
}

.re-body {
  color: var(--ink-2);
  font-size: calc(1rem * var(--reader-scale));
  line-height: 1.82;
  hyphens: auto;
}

.re-body > *:first-child {
  margin-top: 0;
}

.re-body p {
  margin: 0 0 0.95em;
}

.re-body p:first-of-type::first-letter {
  font-size: 2.4em;
  line-height: 0.92;
  float: left;
  padding: 0.06em 0.1em 0 0;
  color: var(--clay);
  font-family: var(--serif);
}

.re-body h3 {
  margin: 1.5em 0 0.5em;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1.22em;
  font-weight: 400;
  line-height: 1.3;
}

.re-body h4 {
  margin: 1.4em 0 0.4em;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.66em;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.re-body blockquote {
  margin: 1.2em 0;
  padding-left: 1rem;
  border-left: 2px solid var(--clay);
  color: var(--ink);
  font-style: italic;
}

.re-body ul,
.re-body ol {
  margin: 0 0 0.95em 1.3em;
  padding: 0;
}

.re-body li {
  margin-bottom: 0.3em;
}

.re-body a {
  color: var(--clay);
}

.re-body strong {
  color: var(--ink);
}

.re-end {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 1.6rem;
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.6rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.re-end::before,
.re-end::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--rule-2);
}

/* blank page (end of book) */
.page-blank {
  display: grid;
  place-items: center;
  height: 100%;
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.62rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

/* ---------- turn controls ---------- */

.turn {
  position: absolute;
  top: 50%;
  z-index: 10;
  display: grid;
  place-items: center;
  width: 2.9rem;
  height: 2.9rem;
  border: 1px solid var(--rule);
  border-radius: 50%;
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  color: var(--ink-2);
  transform: translateY(-50%);
  transition: background var(--fast) var(--ease), color var(--fast) var(--ease),
    border-color var(--fast) var(--ease);
}

.turn:hover:not([disabled]) {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.turn[disabled] {
  opacity: 0.3;
  cursor: default;
}

.turn svg {
  width: 1.1rem;
  height: 1.1rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.turn--prev {
  left: clamp(0.4rem, 2vw, 1.2rem);
}

.turn--next {
  right: clamp(0.4rem, 2vw, 1.2rem);
}

/* ---------- footer ---------- */

.reader-foot {
  display: flex;
  align-items: center;
  gap: var(--s-4);
  padding: 0 clamp(0.9rem, 2.4vw, 1.8rem) 0.9rem;
}

.reader-count {
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.64rem;
  letter-spacing: 0.12em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.progress {
  position: relative;
  flex: 1;
  height: 2px;
  background: color-mix(in srgb, var(--ink) 10%, transparent);
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  display: block;
  height: 100%;
  width: 0;
  background: var(--clay);
  border-radius: inherit;
  transition: width var(--mid) var(--ease);
}

/* ---------- opening cover ---------- */

.reader-cover {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: var(--paper-2);
}

.reader-cover .cover {
  width: min(17rem, 62vw);
  aspect-ratio: 3 / 4.3;
  cursor: default;
  transform-origin: left center;
  animation: cover-rest 700ms var(--ease-out) both;
}

.reader-cover .cover:hover {
  transform: none;
}

@keyframes cover-rest {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
}

.reader-cover[data-opening="true"] .cover {
  animation: cover-open 640ms var(--ease-out) both;
}

@keyframes cover-open {
  0% {
    transform: perspective(1800px) rotateY(0) scale(1);
    opacity: 1;
  }
  45% {
    transform: perspective(1800px) rotateY(-14deg) scale(1.02);
    opacity: 1;
  }
  100% {
    transform: perspective(1800px) rotateY(-122deg) scale(0.96);
    opacity: 0;
  }
}

/* ---------- contents panel ---------- */

.toc {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  width: min(23rem, 92vw);
  border-left: 1px solid var(--rule);
  background: var(--surface);
  box-shadow: var(--shadow-3);
  animation: toc-in var(--mid) var(--ease-out);
}

@keyframes toc-in {
  from {
    transform: translateX(100%);
  }
}

.toc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: 1rem 1.1rem;
  border-bottom: 1px solid var(--rule);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0 2rem;
}

.toc-month {
  padding: 1.1rem 1.1rem 0.4rem;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.toc-item {
  display: grid;
  grid-template-columns: 3.1rem minmax(0, 1fr);
  gap: 0.6rem;
  width: 100%;
  padding: 0.5rem 1.1rem;
  border-left: 2px solid transparent;
  color: var(--ink-2);
  text-align: left;
}

.toc-item:hover {
  background: var(--surface-2);
}

.toc-item[aria-current="true"] {
  border-left-color: var(--clay);
  background: var(--surface-2);
  color: var(--ink);
}

.toc-item-day {
  color: var(--faint);
  font-family: var(--sans);
  font-size: 0.64rem;
  letter-spacing: 0.06em;
  padding-top: 0.15rem;
}

.toc-item-title {
  font-size: 0.95rem;
  line-height: 1.35;
}

.toc-item[aria-current="true"] .toc-item-day {
  color: var(--clay);
}

/* ---------- list view ---------- */

.reader-list {
  flex: 1;
  overflow-y: auto;
  padding: clamp(1.5rem, 5vw, 4rem) clamp(1.2rem, 5vw, 3rem) 4rem;
}

.reader-list-inner {
  max-width: 34rem;
  margin: 0 auto;
}

.reader-list .reader-entry {
  animation: none;
  padding-bottom: 2.4rem;
  margin-bottom: 2.4rem;
  border-bottom: 1px solid var(--rule);
}

.reader-list .reader-entry:last-child {
  border-bottom: 0;
}

.reader-list .re-body {
  font-size: calc(1.02rem * var(--reader-scale));
}

/* ---------- empty book ---------- */

.reader-empty {
  display: grid;
  place-items: center;
  text-align: center;
  height: 100%;
  padding: 2rem;
}

.reader-empty .cover {
  width: 10rem;
  margin-bottom: 1.6rem;
  cursor: default;
}

.reader-empty h2 {
  margin-bottom: 0.4rem;
}

.reader-empty p {
  color: var(--muted);
  margin-bottom: 1.2rem;
}

/* ---------- responsive ---------- */

@media (max-width: 60rem) {
  .reader-stage {
    padding: 0.75rem 0.75rem 0;
  }

  .book {
    grid-template-columns: minmax(0, 1fr);
    width: min(34rem, 100%);
    height: 100%;
  }

  .book::before {
    display: none;
  }

  .page--left {
    display: none;
  }

  .page--right {
    border-radius: 3px;
  }

  .folio {
    left: 50%;
    transform: translateX(-50%);
  }

  .turn {
    top: auto;
    bottom: -0.2rem;
    width: 2.6rem;
    height: 2.6rem;
    transform: none;
    background: var(--surface);
  }

  .turn--prev {
    left: 0.5rem;
  }

  .turn--next {
    right: 0.5rem;
  }

  .reader-bar {
    grid-template-columns: auto 1fr auto;
  }

  .reader-title {
    text-align: left;
  }

  .reader-tool .reader-tool-label {
    display: none;
  }

  .reader-tool {
    padding: 0.3rem;
  }
}

@media (max-width: 34rem) {
  .reader-bar .reader-tool--list {
    display: none;
  }
}

/* Paper, binding and a flexible, two-sided leaf. No remote assets. */
.reader-stage {
  background: radial-gradient(ellipse at 50% 38%, #ffffff0d, transparent 70%);
  padding-bottom: 1.8rem;
}
.book {
  perspective: 2400px;
  height: min(74vh, 43rem);
  max-height: 100%;
  background: #e9dfca;
  border-radius: 8px 12px 14px 8px;
  box-shadow: 0 2px 0 #c6b99e, 0 4px 0 #eee3ce, 0 6px 0 #bdaf93,
    0 8px 0 #e2d6bd, 0 11px 0 #544436, 0 14px 0 #372c25,
    0 25px 30px -12px #0006, 0 45px 75px -30px #0007;
}
.page {
  background-color: var(--surface);
  background-image: repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
}
.page--left {
  border-radius: 6px 0 0 9px;
  background-image: linear-gradient(90deg, #88704408, transparent 12%, transparent 80%, #6b4b1826 96%, #301c1542),
    repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
}
.page--right {
  border-radius: 0 8px 10px 0;
  background-image: linear-gradient(90deg, #301c1538, #6b4b1817 5%, transparent 19%, transparent 95%, #88704412),
    repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
}
[data-theme="dark"] .reader { --surface: #302b24; --ink: #f1e8d6; --ink-2: #d6cbb7; --faint: #a99c86; --rule-2: #514635; }
.book::before { width: 2.8rem; }
.book::after {
  top: auto; bottom: -9px; left: 5px; right: 5px; width: auto; height: 8px;
  background: repeating-linear-gradient(0deg, #8d7a5f 0 1px, #e7dcc6 1px 2px);
  border-radius: 0 0 45% 10%;
}
.page-inner { touch-action: pan-y; }
.page--right { cursor: grab; }
.page--left { cursor: grab; }
.book[data-turning] { cursor: grabbing; user-select: none; }
.book[data-turning] .page { cursor: grabbing; }
.reader-entry { animation: none; }
.reader-hint { color: var(--muted); font: 0.65rem var(--sans); white-space: nowrap; }
@media (max-width: 60rem) {
  .book { height: 100%; max-height: 100%; }
  .reader-stage { padding: 1rem 1rem 3.4rem; }
  .reader-hint { display: none; }
  .turn { bottom: 0.2rem; }
  .page--right { background-image: linear-gradient(90deg, #6b4b1818, transparent 10%, transparent 95%, #88704412); }
}
@media (prefers-reduced-motion: reduce) {
  .reader, .book, .reader-entry, .toc { animation: none; transition: none; }
}

/* The fold renderer owns page transforms, clipping and moving shadows. */
.book-frame { position: relative; max-width: 100%; min-height: 0; }
.book--physics {
  display: block; width: 100%; height: 100%; max-height: none;
  perspective: none; transform-style: flat; transition: opacity var(--mid);
}
.book--physics::before, .book--physics::after { display: none; }
.book--physics .stf__wrapper { position: relative; width: 100%; height: 100%; }
.book--physics .stf__block { width: 100%; height: 100%; }
.book--physics .physics-page {
  display: none; margin: 0; border-radius: 2px 7px 8px 2px;
  background-color: var(--surface); color: var(--ink);
  background-image: repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
  box-shadow: inset 0 0 18px #55331009;
}
.book--physics .physics-page.--left {
  border-radius: 7px 2px 2px 8px;
  background-image: linear-gradient(90deg, transparent 75%, #6b4b1812 94%, #301c153b),
    repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
}
.book--physics .physics-page.--right {
  background-image: linear-gradient(90deg, #301c152e, #6b4b1810 6%, transparent 22%),
    repeating-linear-gradient(0deg, #94774904 0 1px, transparent 1px 3px);
}
.book--physics .page-inner { height: 100%; cursor: grab; }
.book--physics .folio { left: 50%; transform: translateX(-50%); }
.reader[data-turn-state="user_fold"] .page-inner { cursor: grabbing; }
.book--physics .physics-page a { cursor: pointer; }
@media (max-width: 60rem) {
  .book--physics { width: 100%; height: 100%; }
}

/* Let the immersive reader sit above the sticky mobile shell. */
body[data-immersive="true"] #view { animation: none; }
body[data-immersive="true"] #rail,
body[data-immersive="true"] #mobileBar { visibility: hidden; }
@media (max-width: 34rem) {
  .reader-bar .reader-tool--list { display: inline-flex; }
}
