/*
 * Aeon Reader — reader.css
 * ========================
 * Phase 4: Reader view — typography, layout, and reading-specific UI.
 *
 * This stylesheet supplements main.css with rules specific to the reader view.
 * It is loaded separately so the feed view does not pay the cost of all these
 * detailed typography rules until a reader view is opened.
 *
 * Typography system:
 *   - All font sizes use clamp() for fluid scaling between breakpoints.
 *   - All sizes are further multiplied by var(--font-scale) so the settings
 *     slider uniformly scales the whole type system.
 *   - Line-height is controlled by var(--line-spacing) (1.75 comfortable / 1.5 compact).
 *   - Body text is constrained to ~660px for optimal reading line length.
 *
 * See ENGINEERING_PLAN.md §7 and §9 for the full design spec.
 */

/* =========================================================
   Reader Header
   Minimal back + actions header. Hides on scroll-down,
   reappears when the user scrolls back up.
   JS adds .reader-header--hidden to animate it out.
   ========================================================= */

.reader-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--header-bg);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1),
              opacity  0.25s ease;
}

/* Hidden state — slide header up off screen */
.reader-header--hidden {
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

.reader-header__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.25rem 0.5rem 0.25rem;
  max-width: 720px;
  margin: 0 auto;
}

.reader-header__actions {
  display: flex;
  align-items: center;
  gap: 0.125rem;
}

/* =========================================================
   Reading Progress Bar
   4px bar pinned below the sticky reader header.
   Width is set by JS as a percentage (0–100%).
   ========================================================= */

.reading-progress-bar {
  position: sticky;
  top: 0;
  /* Sits below the reader-header (z-index 100), above content */
  z-index: 99;
  height: 4px;
  background-color: var(--accent);
  width: 0%;
  /* Do not transition width — JS updates it on every scroll event, transition would lag */
  transition: none;
  /* When header is hidden this bar aligns to the top of the viewport */
  margin-top: 0;
}

/* =========================================================
   Reader Article Layout
   ========================================================= */

.reader-article {
  max-width: 680px;
  margin: 0 auto;
  padding: 1.5rem 1rem calc(5rem + env(safe-area-inset-bottom));
}

/* =========================================================
   Article Header Section (inside reader-article)
   Rendered by js/reader.js: hero image, category, title,
   author line, reading time.
   ========================================================= */

/* Hero / cover image — full width, max 56vw tall */
.reader-hero-image {
  width: calc(100% + 2rem);   /* bleed to edges of padding */
  margin-left: -1rem;
  margin-right: -1rem;
  margin-bottom: 1.5rem;
  display: block;
  max-height: 56vw;
  object-fit: cover;
  object-position: center top;
  /* Subtle rounded bottom corners */
  border-radius: 0 0 4px 4px;
}

/* Category label — small-caps, accent colour */
.reader-category {
  font-family: system-ui, sans-serif;
  font-size: calc(0.6875rem * var(--font-scale));
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--accent);
  margin-bottom: 0.5rem;
  display: block;
}

/* Article title */
.reader-title {
  font-family: var(--body-font);
  font-size: calc(clamp(22px, 5vw, 30px) * var(--font-scale));
  font-weight: 700;
  line-height: 1.25;
  color: var(--text);
  margin-bottom: 0.75rem;
  max-width: 680px;
}

/* Byline — "By Author" */
.reader-author {
  font-family: system-ui, sans-serif;
  font-size: calc(0.875rem * var(--font-scale));
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

/* Reading time display */
.reader-time {
  font-family: system-ui, sans-serif;
  font-size: calc(0.8125rem * var(--font-scale));
  color: var(--text-muted);
  margin-bottom: 2rem;
}

/* Separator line between header and body */
.reader-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5rem 0;
}

/* =========================================================
   Article Body Typography
   These rules style the sanitised HTML returned by the
   fetch_articles.py script and stored in bodyHtml.
   ========================================================= */

/* Body container — constrains line length for readability */
.reader-body {
  color: var(--text);
  font-family: var(--body-font);
  font-size: calc(clamp(17px, 2.5vw, 19px) * var(--font-scale));
  line-height: var(--line-spacing);
  max-width: 660px;
}

/* Drop cap — first letter of first paragraph, toggleable in settings */
.reader-body.drop-cap > p:first-of-type::first-letter {
  float: left;
  font-size: calc(3em * var(--font-scale));
  line-height: 0.85;
  padding-right: 0.1em;
  padding-top: 0.05em;
  font-weight: 700;
  color: var(--accent);
}

/* Paragraph spacing */
.reader-body p {
  margin-bottom: 1.5em;
}

/* Paragraph within focus mode — dimmed unless focused */
.reader-body.focus-mode p {
  opacity: 0.35;
  transition: opacity 0.3s ease;
}

.reader-body.focus-mode p.focus-active {
  opacity: 1;
}

/* Subheadings */
.reader-body h2 {
  font-family: var(--body-font);
  font-size: calc(clamp(19px, 3vw, 22px) * var(--font-scale));
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  margin-top: 2em;
  margin-bottom: 0.75em;
  max-width: 660px;
}

.reader-body h3 {
  font-family: var(--body-font);
  font-size: calc(clamp(17px, 2.5vw, 19px) * var(--font-scale));
  font-weight: 600;
  line-height: 1.35;
  color: var(--text);
  margin-top: 1.5em;
  margin-bottom: 0.5em;
}

/* Blockquote */
.reader-body blockquote {
  font-size: calc(clamp(18px, 2.5vw, 20px) * var(--font-scale));
  font-style: italic;
  line-height: 1.7;
  color: var(--text-muted);
  border-left: 3px solid var(--accent);
  margin: 1.5em 0;
  padding: 0.5em 0 0.5em 1.25em;
  max-width: 580px;
}

/* Inline emphasis */
.reader-body em   { font-style: italic; }
.reader-body strong { font-weight: 700; color: var(--text); }

/* Links in body */
.reader-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
  transition: opacity 0.15s ease;
}

.reader-body a:hover { opacity: 0.75; }

/* Lists */
.reader-body ul,
.reader-body ol {
  padding-left: 1.75em;
  margin-bottom: 1.5em;
}

.reader-body li {
  margin-bottom: 0.5em;
  line-height: var(--line-spacing);
}

/* Figures and images */
.reader-body figure {
  margin: 1.75em 0;
  /* Bleed to full content width */
  width: 100%;
}

.reader-body figure img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 4px;
  object-fit: contain;
  background-color: var(--bg-card);
}

.reader-body figcaption {
  font-family: system-ui, sans-serif;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
  margin-top: 0.5em;
  max-width: 580px;
}

.reader-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.5em 0;
  border-radius: 4px;
  object-fit: contain;
  background-color: var(--bg-card);
}

/* Explicitly remove generic margins from images inside figures
   so figcaptions remain visually close to their images. */
.reader-body figure img {
  margin: 0;
}
/* Horizontal rule */
.reader-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.5em auto;
  width: 40%;
}

/* =========================================================
   Author Bio (shown at the end of the article)
   ========================================================= */

.reader-author-bio {
  font-family: system-ui, sans-serif;
  font-size: calc(0.875rem * var(--font-scale));
  color: var(--text-muted);
  line-height: 1.6;
  padding: 1.25rem;
  background: var(--bg-card);
  border-radius: 8px;
  border-left: 3px solid var(--accent);
  margin-top: 2.5rem;
}

.reader-author-bio-label {
  font-weight: 600;
  color: var(--text);
  display: block;
  margin-bottom: 0.375rem;
  font-size: calc(0.8125rem * var(--font-scale));
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* =========================================================
   Scroll-to-Top Button
   Appears after scrolling 400px in the reader view.
   ========================================================= */

.scroll-top-btn {
  position: fixed;
  bottom: calc(1.5rem + env(safe-area-inset-bottom));
  right: 1rem;
  z-index: 150;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s ease, transform 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  animation: scroll-btn-enter 0.2s ease;
}

@keyframes scroll-btn-enter {
  from { opacity: 0; transform: scale(0.8); }
  to   { opacity: 1; transform: scale(1); }
}

.scroll-top-btn:hover  { opacity: 0.9; transform: translateY(-2px); }
.scroll-top-btn:active { opacity: 0.8; transform: scale(0.95); }

.scroll-top-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* =========================================================
   View Transitions (Feed ↔ Reader)
   JS adds/removes these classes to animate the transition.
   ========================================================= */

/* Feed slides out to the left */
.view-feed--exit {
  animation: slide-out-left 0.28s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Reader slides in from the right */
.view-reader--enter {
  animation: slide-in-right 0.28s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Feed slides back in from the left */
.view-feed--enter {
  animation: slide-in-left 0.28s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Reader slides out to the right */
.view-reader--exit {
  animation: slide-out-right 0.28s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes slide-out-left {
  from { transform: translateX(0); opacity: 1; }
  to   { transform: translateX(-15%); opacity: 0; }
}

@keyframes slide-in-right {
  from { transform: translateX(30%); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

@keyframes slide-in-left {
  from { transform: translateX(-15%); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

@keyframes slide-out-right {
  from { transform: translateX(0); opacity: 1; }
  to   { transform: translateX(30%); opacity: 0; }
}

/* =========================================================
   Responsive Adjustments
   ========================================================= */

@media (min-width: 640px) {
  .reader-article {
    padding-left: 1.5rem;
    padding-right: 1.5rem;
  }

  .reader-hero-image {
    width: calc(100% + 3rem);
    margin-left: -1.5rem;
    margin-right: -1.5rem;
  }
}

@media (min-width: 768px) {
  .reader-title {
    font-size: calc(30px * var(--font-scale));
  }

  .reader-body {
    font-size: calc(19px * var(--font-scale));
  }
}

/* =========================================================
   Text-to-Speech Active State
   ========================================================= */

/* Highlight the TTS button while reading aloud */
.btn--tts-active {
  /* Use the accent colour with a subtle background fill */
  color: var(--accent-color, #8B4513);
  background-color: var(--accent-color-alpha, rgba(139, 69, 19, 0.12));
  border-radius: 50%;
}

/* =========================================================
   Auto-scroll (Teleprompter Mode) Indicator
   ========================================================= */

/* Thin pulsing bar shown at the bottom of the viewport when auto-scroll is on.
   Added by JS when auto-scroll starts; removed when it stops. */
.auto-scroll-indicator {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--accent-color, #8B4513);
  opacity: 0.6;
  animation: auto-scroll-pulse 2s ease-in-out infinite;
  z-index: 10;
  pointer-events: none;
}

@keyframes auto-scroll-pulse {
  0%, 100% { opacity: 0.6; }
  50%       { opacity: 0.25; }
}
