/* Immersive book reading modal */

.reading-modal {
  --book-page-bg: #fdfaf5;
  --book-shadow: rgba(15, 23, 42, 0.25);
  --book-outline: rgba(15, 23, 42, 0.08);
  border: 0;
  padding: 0;
  width: 100vw;
  max-width: none;
  height: 100vh;
  max-height: none;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-ink);
  opacity: 0;
  transform: translateY(20px) scale(0.98);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.reading-modal[open] {
  opacity: 1;
  transform: none;
}

.reading-modal--closing {
  opacity: 0;
  transform: translateY(30px) scale(0.96);
  pointer-events: none;
}

.reading-modal::backdrop {
  background: rgba(7, 11, 27, 0.75);
  backdrop-filter: blur(12px);
}

.reading-modal__shell {
  width: min(1400px, 96vw);
  max-height: min(920px, 96vh);
  height: auto;
  background: linear-gradient(135deg, #f9f7f2 0%, #ffffff 45%, #f5f1e9 100%);
  border-radius: 1.75rem;
  box-shadow: 0 40px 90px rgba(8, 15, 35, 0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  isolation: isolate;
}

.reading-modal__toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.25rem 2rem;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(6px);
}

.reading-modal__toolbar-info {
  flex: 1;
  min-width: 0;
}

.reading-modal__toolbar-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reading-modal__toolbar-subtitle {
  margin: 0.1rem 0 0;
  font-size: 0.85rem;
  color: var(--color-ink);
  opacity: 0.65;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reading-modal__toolbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--color-ink);
  background: rgba(15, 23, 42, 0.04);
  border-radius: 999px;
  border: 1px solid transparent;
  transition: background 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
  cursor: pointer;
}

.reading-modal__toolbar-btn:hover {
  background: rgba(15, 23, 42, 0.08);
  border-color: rgba(15, 23, 42, 0.08);
  transform: translateY(-1px);
}

.reading-modal__close-btn {
  background: rgba(15, 23, 42, 0.08);
  border-color: transparent;
}

.reading-modal__close-btn:hover {
  background: rgba(15, 23, 42, 0.15);
}

.reading-modal__toc-toggle {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.75rem;
}

.reading-modal__body {
  flex: 1 1 auto;
  position: relative;
  padding: clamp(1.5rem, 3vw, 3rem);
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top, rgba(0, 0, 0, 0.04), transparent 60%);
  overflow-y: auto;
  min-height: 0;
}

.reading-modal__surface {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reading-modal__pages {
  position: relative;
  width: min(1200px, 92vw);
  height: min(780px, 75vh);
  background: var(--book-page-bg);
  border-radius: 1.5rem;
  box-shadow:
    0 25px 70px var(--book-shadow),
    0 0 0 1px var(--book-outline);
  padding: clamp(2.5rem, 4vw, 3.5rem) clamp(3rem, 5vw, 5rem);
  overflow-y: auto;
  overflow-x: hidden;
  isolation: isolate;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: rgba(15, 23, 42, 0.2) transparent;
  transition: box-shadow 0.3s ease;
}

.reading-modal__pages:focus-within {
  box-shadow:
    0 25px 70px var(--book-shadow),
    0 0 0 2px rgba(59, 130, 246, 0.2),
    0 0 0 1px var(--book-outline);
}

.reading-modal__pages::-webkit-scrollbar {
  width: 8px;
}

.reading-modal__pages::-webkit-scrollbar-track {
  background: transparent;
}

.reading-modal__pages::-webkit-scrollbar-thumb {
  background: rgba(15, 23, 42, 0.2);
  border-radius: 4px;
}

.reading-modal__pages::-webkit-scrollbar-thumb:hover {
  background: rgba(15, 23, 42, 0.3);
}

/* Preview reading pages */
.preview-reading__page {
  position: relative;
  width: 100%;
  min-height: 100%;
  animation: fadeIn 0.4s ease-out;
  opacity: 1;
}

.preview-reading__page[style*="display: none"] {
  animation: fadeOut 0.3s ease-in;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

.reading-modal__pages::before {
  display: none;
}

.reading-modal__pages::after {
  display: none;
}

.reading-modal__pages--section::after,
.reading-modal__pages--picture::after {
  display: none;
}

.reading-modal__preview-banner {
  position: relative;
  z-index: 1;
  margin-bottom: 1.5rem;
  border-radius: 0.75rem;
  padding: 1rem 1.25rem;
  background: rgba(255, 216, 168, 0.4);
  border: 1px solid rgba(255, 159, 67, 0.35);
  font-size: 0.95rem;
  color: var(--color-ink);
  text-align: center;
}

.reading-modal__preview-banner strong {
  display: inline-block;
  margin-right: 0.35rem;
  font-weight: 600;
}

.reading-modal__preview-link {
  color: var(--color-link);
  font-weight: 600;
  text-decoration: underline;
}

.reading-modal__preview-link:hover {
  opacity: 0.85;
}

.reading-modal__reading-content {
  position: relative;
  z-index: 1;
  column-count: 1;
  font-family: var(--font-serif);
  font-size: clamp(1.05rem, 1.6vw, 1.2rem);
  line-height: 1.85;
  letter-spacing: 0.01em;
  color: rgba(15, 23, 42, 0.92);
  text-align: left;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  hyphens: auto;
  -webkit-hyphens: auto;
  -moz-hyphens: auto;
  word-spacing: 0.05em;
  max-width: 65ch;
  margin: 0 auto;
  /* Optimal reading: ~65-75 characters per line */
}

.reading-modal__reading-content h1,
.reading-modal__reading-content h2,
.reading-modal__reading-content h3,
.reading-modal__reading-content h4,
.reading-modal__reading-content h5,
.reading-modal__reading-content h6 {
  font-family: var(--font-sans);
  font-weight: 600;
  line-height: 1.4;
  margin: 2rem 0 1.25rem;
  break-inside: avoid;
  break-after: avoid;
  color: rgba(15, 23, 42, 0.95);
  letter-spacing: -0.01em;
}

.reading-modal__reading-content h1:first-child,
.reading-modal__reading-content h2:first-child,
.reading-modal__reading-content h3:first-child {
  margin-top: 0;
}

.reading-modal__reading-content h1 {
  font-size: clamp(2rem, 3vw, 2.5rem);
  margin-top: 0;
}

.reading-modal__reading-content h2 {
  font-size: clamp(1.6rem, 2.5vw, 2rem);
}

.reading-modal__reading-content h3 {
  font-size: clamp(1.3rem, 2vw, 1.6rem);
}

.reading-modal__reading-content p {
  margin: 0 0 1.5rem;
  text-indent: 1.5rem;
  break-inside: avoid;
  orphans: 3;
  widows: 3;
}

.reading-modal__reading-content p:first-of-type {
  text-indent: 0;
}

.reading-modal__reading-content p:last-child {
  margin-bottom: 0;
}

.reading-modal__reading-content ul,
.reading-modal__reading-content ol {
  margin: 0 0 1.5rem 1.5rem;
  padding-left: 1rem;
}

.reading-modal__reading-content li {
  margin-bottom: 0.5rem;
  break-inside: avoid;
}

.reading-modal__reading-content blockquote {
  margin: 2rem 0;
  padding: 1.25rem 0 1.25rem 2rem;
  border-left: 3px solid rgba(15, 23, 42, 0.25);
  font-style: italic;
  color: rgba(15, 23, 42, 0.85);
  break-inside: avoid;
  column-span: all;
}

.reading-modal__reading-content figure {
  margin: 2rem 0;
  text-align: center;
  break-inside: avoid;
}

.reading-modal__reading-content img {
  max-width: 100%;
  height: auto;
  border-radius: 0.5rem;
  box-shadow: 0 12px 35px rgba(15, 23, 42, 0.18);
}

.reading-modal__reading-content figcaption {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  margin-top: 0.75rem;
  color: rgba(15, 23, 42, 0.75);
}

.reading-modal__reading-content a {
  color: var(--color-link);
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 2px;
  transition: opacity 0.2s ease;
}

.reading-modal__reading-content a:hover {
  opacity: 0.8;
  text-decoration-thickness: 2px;
}

.reading-modal__reading-content code {
  font-family: var(--font-mono);
  background: rgba(15, 23, 42, 0.08);
  padding: 0.15em 0.35em;
  border-radius: 0.3rem;
}

.reading-modal__reading-content pre {
  margin: 1.5rem 0;
  padding: 1rem;
  border-radius: 0.75rem;
  background: rgba(15, 23, 42, 0.08);
  overflow-x: auto;
  column-span: all;
}

.reading-modal__reading-content pre code {
  background: none;
  padding: 0;
}

.reading-modal__reading-content.page--section {
  column-count: 1;
  text-align: center;
  font-size: clamp(1.8rem, 5vw, 3rem);
  font-weight: 600;
  text-indent: 0;
}

.reading-modal__reading-content.page--section h1 {
  margin-bottom: 0;
}

.reading-modal__pages--picture .reading-modal__reading-content,
.reading-modal__reading-content.page--picture {
  column-count: 1;
  text-align: center;
  text-indent: 0;
}

.reading-modal__pages--picture img {
  margin: 0 auto;
  max-height: 70vh;
}

.reading-modal__footer {
  padding: 1.25rem 2rem;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(6px);
}

.reading-modal__footer-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.reading-modal__footer-nav-group {
  flex: 1;
  display: flex;
  align-items: center;
  min-width: 0;
}

.reading-modal__footer-nav-group:first-child {
  justify-content: flex-start;
}

.reading-modal__footer-nav-group:last-child {
  justify-content: flex-end;
}

.reading-modal__footer-nav .btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.2rem;
  border-radius: 999px;
  border: 1px solid rgba(15, 23, 42, 0.15);
  background: rgba(15, 23, 42, 0.05);
  color: var(--color-ink);
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.2s ease, transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
  text-decoration: none;
  max-width: 100%;
  min-width: 0;
  justify-content: space-between;
  cursor: pointer;
}

.reading-modal__footer-nav .btn:hover:not(:disabled) {
  background: rgba(15, 23, 42, 0.12);
  border-color: rgba(15, 23, 42, 0.25);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.1);
}

.reading-modal__footer-nav .btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.08);
}

.reading-modal__footer-nav .btn:focus-visible {
  outline: 2px solid rgba(59, 130, 246, 0.5);
  outline-offset: 2px;
}

.reading-modal__footer-nav .btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  transform: none;
}

.reading-modal__footer-nav-center {
  flex: 0 0 auto;
  font-size: 0.85rem;
  color: rgba(15, 23, 42, 0.7);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.reading-modal__footer-nav .overflow-ellipsis {
  display: inline-block;
  max-width: 100%;
  min-width: 0;
}

.reading-modal__page-indicator {
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.06);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.05em;
  transition: background 0.2s ease;
}

.reading-modal__page-indicator:hover {
  background: rgba(15, 23, 42, 0.1);
}

/* Table of contents panel */
.reading-modal__toc {
  position: absolute;
  top: clamp(1rem, 2vw, 1.5rem);
  left: clamp(1rem, 2vw, 1.5rem);
  width: min(360px, 80vw);
  height: calc(100% - 2 * clamp(1rem, 2vw, 1.5rem));
  transform: translateX(-120%);
  transition: transform 0.35s ease, opacity 0.35s ease;
  pointer-events: none;
  opacity: 0;
  z-index: 10;
}

.reading-modal__toc--open {
  transform: translateX(0);
  pointer-events: auto;
  opacity: 1;
}

.reading-modal__toc-panel {
  height: 100%;
  border-radius: 1rem;
  background: #ffffff;
  box-shadow: 0 35px 70px rgba(15, 23, 42, 0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.reading-modal__toc-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.reading-modal__toc-eyebrow {
  margin: 0;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: rgba(15, 23, 42, 0.6);
}

.reading-modal__toc-title {
  margin: 0.2rem 0 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--color-ink);
}

.reading-modal__toc-close {
  border: none;
  background: rgba(15, 23, 42, 0.08);
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}

.reading-modal__toc-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 0.85rem 1.75rem;
  scroll-padding-top: 1rem;
}

.reading-modal__toc-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem 0 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.reading-modal__toc-item {
  margin: 0;
}

.reading-modal__toc-link {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.85rem 1.2rem;
  border-radius: 0.9rem;
  text-decoration: none;
  background: transparent;
  border: 1px solid transparent;
  color: var(--color-ink);
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.reading-modal__toc-link:hover {
  background: rgba(15, 23, 42, 0.05);
  border-color: rgba(15, 23, 42, 0.08);
}

.reading-modal__toc-link.active {
  background: rgba(59, 130, 246, 0.08);
  border-color: rgba(59, 130, 246, 0.3);
  box-shadow: 0 8px 18px rgba(59, 130, 246, 0.15);
  color: var(--color-link);
}

.reading-modal__toc-title {
  font-size: 0.98rem;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reading-modal__toc-kicker {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: rgba(15, 23, 42, 0.55);
}

.reading-modal__toc-item--section .reading-modal__toc-title {
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* Responsive */
@media (max-width: 1200px) {
  .reading-modal__pages {
    width: 100%;
    height: auto;
    min-height: 65vh;
  }
  
  .reading-modal__reading-content {
    font-size: clamp(1rem, 1.5vw, 1.15rem);
    max-width: 60ch;
  }
}

@media (max-width: 900px) {
  .reading-modal {
    align-items: stretch;
  }
  
  .reading-modal__shell {
    width: 100vw;
    height: 100vh;
    border-radius: 0;
  }
  
  .reading-modal__toolbar {
    flex-wrap: wrap;
    padding: 1rem 1.25rem;
  }
  
  .reading-modal__body {
    padding: 1rem;
  }
  
  .reading-modal__pages {
    padding: 1.5rem 1.75rem;
    border-radius: 1rem;
    width: 100%;
  }
  
  .reading-modal__reading-content {
    max-width: 100%;
    font-size: clamp(1rem, 4vw, 1.1rem);
    line-height: 1.8;
    padding: 0 0.5rem;
  }
  
  .reading-modal__toc {
    width: calc(100% - 2rem);
    left: 1rem;
    right: 1rem;
    top: 1rem;
    height: calc(100% - 2rem);
  }
}

@media (max-width: 600px) {
  .reading-modal__footer-nav {
    flex-direction: column;
    align-items: stretch;
  }

  .reading-modal__footer-nav-group,
  .reading-modal__footer-nav-center {
    width: 100%;
    justify-content: stretch;
  }

  .reading-modal__footer-nav-center {
    order: -1;
    margin-bottom: 0.5rem;
    text-align: center;
  }

  .reading-modal__footer-nav .btn {
    width: 100%;
  }
}
