/* ============================================================================
   Mobile-First Book Reader
   - Single column, natural scrolling
   - Apple-like design with glassmorphism
   - Typography-focused using IBM Plex Serif + Sans
   ============================================================================ */

/* ============================================================================
   Fonts
   ============================================================================ */

/* ATKyriosDisplay for book headings */
@font-face {
  font-family: 'ATKyriosDisplay';
  src: url('../fonts/ATKyriosDisplay-Ultra.woff2') format('woff2'),
       url('../fonts/ATKyriosDisplay-Ultra.otf') format('opentype');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

/* ============================================================================
   CSS Variables
   ============================================================================ */

:root {
  /* Typography */
  --font-serif: 'IBM Plex Serif', Georgia, serif;
  --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-display: 'ATKyriosDisplay', serif;
  
  /* Spacing & Sizes */
  --header-height: 60px;
  --max-width-mobile: 650px;
  --max-width-desktop: 700px;
  --body-size-mobile: 18px;
  --body-size-desktop: 19px;
  --line-height: 1.7;
  
  /* Animation */
  --ease-smooth: cubic-bezier(0.4, 0, 0.2, 1);
  --transition-speed: 0.3s;
}

/* Light Mode Colors */
[data-theme="light"] {
  --bg: #fafafa;
  --text: #222222;
  --text-muted: #666666;
  --link: #cc0000;
  --link-hover: #990000;
  --border: #e0e0e0;
  --header-bg: rgba(250, 250, 250, 0.85);
}

/* Dark Mode Colors */
[data-theme="dark"] {
  --bg: #121212;
  --text: #eeeeee;
  --text-muted: #aaaaaa;
  --link: #cc0000;
  --link-hover: #990000;
  --border: #333333;
  --header-bg: rgba(18, 18, 18, 0.85);
}

/* ============================================================================
   Base Styles
   ============================================================================ */

* {
  box-sizing: border-box;
}

html {
  margin: 0;
  padding: 0;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  padding: 0;
  font-family: var(--font-serif);
  background: var(--bg);
  color: var(--text);
  font-size: var(--body-size-mobile);
  line-height: var(--line-height);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background var(--transition-speed), color var(--transition-speed);
}

@media (min-width: 768px) {
  body {
    font-size: var(--body-size-desktop);
  }
}

/* ============================================================================
   Sticky Reader Header
   ============================================================================ */

.reader-header {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
}

/* Progress bar at top of header - shows section count */
.progress-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: rgba(204, 0, 0, 0.2);
  --progress-width: 0%;
}

.progress-bar::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: var(--link);
  width: var(--progress-width);
  transition: width 0.3s ease;
}

/* Header content layout */
.header-content {
  max-width: var(--max-width-desktop);
  margin: 0 auto;
  padding: 0 1.5rem;
  height: var(--header-height);
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 1rem;
  font-family: var(--font-sans);
}

@media (max-width: 767px) {
  .header-content {
    padding: 0 1rem;
    gap: 0.5rem;
  }
}

/* Home logo link */
.home-logo {
  display: flex;
  align-items: center;
  transition: opacity 0.2s ease;
  text-decoration: none;
}

.home-logo:hover {
  opacity: 0.8;
}

.logo-icon {
  height: 42px;
  width: auto;
  display: block;
}

@media (max-width: 767px) {
  .logo-icon {
    height: 36px;
  }
}

/* Book title - left aligned, clickable */
.book-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
  cursor: pointer;
  transition: color 0.2s ease;
}

.book-title:hover {
  color: var(--link);
}

@media (max-width: 767px) {
  .book-title {
    font-size: 0.85rem;
  }
}

/* Navigation buttons container - right aligned */
.nav-buttons {
  justify-self: end;
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

/* Previous and Next buttons - matches site purchase buttons */
.prev-section,
.next-section {
  background: #333;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: var(--font-sans);
  border-radius: 0;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: all 0.3s ease,
              opacity var(--transition-speed) var(--ease-smooth);
  white-space: nowrap;
}

.prev-section:hover,
.next-section:hover {
  background: #333;
  color: #cc0000;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.prev-section:active,
.next-section:active {
  transform: scale(0.98);
}

/* Hide buttons when not applicable */
.prev-section.hidden,
.next-section.hidden {
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 767px) {
  .nav-buttons {
    gap: 0.35rem;
  }
  
  .prev-section,
  .next-section {
    font-size: 0.75rem;
    padding: 0.4rem 0.7rem;
  }
}

/* Dark Mode Toggle */
.dark-toggle {
  background: none;
  border: none;
  color: var(--text);
  padding: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  transition: color 0.2s ease;
  min-width: 44px;
  min-height: 44px;
  margin-left: 0.5rem;
}

.dark-toggle:hover {
  color: var(--link);
}

.dark-toggle svg {
  width: 1.25rem;
  height: 1.25rem;
  transition: transform 0.12s ease-out, opacity 0.12s ease-out;
}

.dark-toggle:active svg {
  transform: rotate(15deg);
}

/* Dark toggle icons with crossfade effect */
.dark-toggle .icon-moon {
  display: inline-block;
  opacity: 1;
}

.dark-toggle .icon-sun {
  display: none;
  opacity: 0;
}

[data-theme="dark"] .dark-toggle .icon-moon {
  display: none;
  opacity: 0;
}

[data-theme="dark"] .dark-toggle .icon-sun {
  display: inline-block;
  opacity: 1;
}

/* ============================================================================
   Reader Footer
   ============================================================================ */

.reader-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 900;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  transform: translateY(100%);
  opacity: 0;
  transition: transform var(--transition-speed) var(--ease-smooth),
              opacity var(--transition-speed) var(--ease-smooth);
}

.reader-footer.visible {
  transform: translateY(0);
  opacity: 1;
}

.footer-content {
  max-width: var(--max-width-desktop);
  margin: 0 auto;
  text-align: center;
  font-family: var(--font-sans);
}

/* Current section in footer */
.footer-content .current-section {
  font-size: 0.9rem;
  color: var(--text-muted);
  font-weight: 400;
}

@media (max-width: 767px) {
  .reader-footer {
    padding: 0.5rem 1rem;
  }
  
  .footer-content .current-section {
    font-size: 0.8rem;
  }
}

/* ============================================================================
   Chapter Navigation Overlay
   ============================================================================ */

.chapter-nav-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 2000;
  display: none;
  opacity: 0;
  transition: opacity 0.3s ease;
  overflow-y: auto;
  padding: 2rem;
}

.chapter-nav-overlay.active {
  display: flex;
  opacity: 1;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

/* Chapter nav container */
.chapter-nav-container {
  max-width: 1200px;
  width: 100%;
  margin-top: calc(var(--header-height) + 2rem);
}

/* Chapter nav header */
.chapter-nav-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding: 0 0.5rem;
}

.chapter-nav-title {
  font-family: var(--font-sans);
  font-size: 1.5rem;
  font-weight: 700;
  color: white;
}

.chapter-nav-close {
  background: transparent;
  border: none;
  color: white;
  font-size: 2rem;
  font-weight: 300;
  line-height: 1;
  cursor: pointer;
  transition: color 0.2s ease;
  padding: 0.5rem;
  transform: rotate(45deg); /* Display as X */
}

.chapter-nav-close:hover {
  color: var(--link);
}

/* Chapter cards grid */
.chapter-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-auto-rows: minmax(200px, auto);
  gap: 1.5rem;
  padding: 0 0.5rem;
  grid-auto-flow: dense;
}

@media (max-width: 767px) {
  .chapter-cards {
    grid-template-columns: 1fr;
    grid-auto-rows: auto;
    gap: 1rem;
  }
}

/* Individual chapter card - square corners, matches tags page style */
.chapter-card {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 1.5rem;
  border-radius: 0;
  cursor: pointer;
  transition: all 0.3s ease, border-color 0.2s ease;
  position: relative;
  overflow: hidden;
  min-height: 200px;
}

.chapter-card:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--link);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(204, 0, 0, 0.2);
}

.chapter-card.active {
  background: rgba(204, 0, 0, 0.1);
  border-color: var(--link);
}

/* Chapter card content */
.chapter-card-number {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.5);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.5rem;
}

.chapter-card-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  color: white;
  margin-bottom: 0.5rem;
  line-height: 1.2;
}

.chapter-card-meta {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.6);
}

/* Cover card special styling - adapts to image proportions */
.chapter-card-cover {
  padding: 0;
  overflow: hidden;
  grid-row: auto;
  min-height: auto;
}

.chapter-card-cover-image {
  width: 100%;
  height: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
}

.chapter-card-cover-image img {
  width: 100%;
  height: auto;
  display: block;
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

/* Remove greyscale on hover or when active */
.chapter-card-cover:hover .chapter-card-cover-image img,
.chapter-card-cover.active .chapter-card-cover-image img {
  filter: grayscale(0%);
}

/* ============================================================================
   Main Reader Container
   ============================================================================ */

.reader {
  max-width: var(--max-width-mobile);
  margin: 0 auto;
  padding: 2rem 1.5rem 5rem;
}

@media (min-width: 768px) {
  .reader {
    max-width: var(--max-width-desktop);
    padding: 3rem 2rem 7rem;
  }
}

/* Book article wrapper */
.book {
  position: relative;
}

/* Book cover section */
.book-cover-section {
  display: none;
  opacity: 0;
  transition: opacity 0.4s ease-in-out;
  text-align: center;
  padding: 2rem 0;
}

.book-cover-section.active {
  display: block;
  opacity: 1;
}

.book-cover-image {
  /* Constrain dimensions while maintaining aspect ratio */
  max-width: 400px;
  max-height: 640px;
  width: auto;
  height: auto;
  display: block;
  margin: 0 auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  filter: grayscale(100%);
  transition: filter 0.3s ease;
}

.book-cover-image:hover {
  filter: grayscale(0%);
}

@media (max-width: 767px) {
  .book-cover-section {
    padding: 1rem 0;
  }
  
  .book-cover-image {
    max-width: 300px;
    max-height: 480px;
  }
}

/* Hide all H1 sections by default */
.book h1 {
  display: none;
  opacity: 0;
  transition: opacity 0.4s ease-in-out;
}

/* Show active section */
.book h1.active {
  display: block;
  opacity: 1;
}

/* Show content after active H1 until next H1 */
.book h1.active ~ *:not(h1) {
  display: block;
  opacity: 1;
  transition: opacity 0.4s ease-in-out;
}

/* Hide content by default */
.book > *:not(h1) {
  display: none;
  opacity: 0;
}

/* Show content that comes immediately after active H1, up until the next H1 */
.book h1.active ~ :not(h1) {
  display: block;
  opacity: 1;
}

/* But hide content after the next H1 */
.book h1.active ~ h1 ~ * {
  display: none;
  opacity: 0;
}

/* ============================================================================
   Typography - Headings
   ============================================================================ */

/* H1 uses ATKyriosDisplay for dramatic effect */
.book h1 {
  font-family: var(--font-display);
  font-size: 5rem;
  margin: 3rem 0 1.5rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.02em;
  color: var(--text);
  text-align: center;
}

/* H2 and H3 use IBM Plex Sans */
.book h2,
.book h3 {
  font-weight: 700;
  line-height: 1.2;
  margin: 2.5rem 0 1.5rem;
  letter-spacing: -0.02em;
  color: var(--text);
  font-family: var(--font-sans);
}

.book h2 {
  font-size: 2rem;
}

.book h3 {
  font-size: 1.5rem;
  line-height: 1.3;
  margin: 2rem 0 1rem;
}

@media (max-width: 767px) {
  .book h1 {
    font-size: 3rem;
    margin-top: 2rem;
  }
  
  .book h2 {
    font-size: 1.65rem;
  }
  
  .book h3 {
    font-size: 1.25rem;
  }
}

/* ============================================================================
   Typography - Body Text
   ============================================================================ */

.book p {
  margin-top: 0;
  margin-bottom: 0;
  text-indent: 2em;
  hyphens: auto;
  -webkit-hyphens: auto;
  text-align: left;
}

/* Remove indent on first paragraph after headings */
.book h1 + p,
.book h2 + p,
.book h3 + p,
.book > p:first-child {
  text-indent: 0;
  margin-top: 0;
}

/* Drop cap for first paragraph */
.book .drop-cap:first-letter {
  float: left;
  font-size: 6em;
  line-height: 0.7;
  font-family: var(--font-display);
  margin-right: 0.1em;
  margin-top: 0.05em;
  color: var(--text);
}

@media (max-width: 767px) {
  .book .drop-cap:first-letter {
    font-size: 3.5em;
  }
}

/* ============================================================================
   Links
   ============================================================================ */

.book a {
  color: var(--link);
  text-decoration: none;
  transition: color 0.2s var(--ease-smooth), text-decoration 0.2s ease;
}

.book a:hover {
  color: var(--link-hover);
  text-decoration: underline;
}

/* ============================================================================
   Page Break Divs (kept for compatibility with existing content)
   ============================================================================ */

.pagebreak {
  display: none;
}

/* ============================================================================
   Print Styles
   ============================================================================ */

@media print {
  .reader-header {
    display: none;
  }
  
  .reader {
    max-width: 100%;
    padding: 0;
  }
  
  .book h1,
  .book h2 {
    page-break-after: avoid;
  }
  
  .book p {
    page-break-inside: avoid;
    orphans: 3;
    widows: 3;
  }
}

/* ============================================================================
   Special Elements
   ============================================================================ */

.book strong {
  font-weight: 700;
}

.book em {
  font-style: italic;
}

/* Last page marker (for compatibility) */
.last-page {
  display: none;
}
