/* ==========================================================================
   Tworks — Reader Engine (Phase 1)
   ========================================================================== */

/* Tworks addition: a plain wrapper div rather than a body class — the app
   is a single-page shell, so scoping the reader theme to a div inside
   #view-root means leaving the chapter view (a normal re-render) clears it
   automatically, no cross-view cleanup needed. */
.reader-body {
  background: var(--reader-bg);
  color: var(--reader-text);
  transition: background .2s ease, color .2s ease;
  margin: 0 calc(-1 * clamp(16px, 4vw, 40px));
  padding: 0 clamp(16px, 4vw, 40px);
}

/* ---------- Progress bar ---------- */
.reader-progress {
  position: fixed; top: 0; left: 0; right: 0; height: 3px;
  background: transparent; z-index: 80;
}
.reader-progress-fill { height: 100%; width: 0%; background: var(--reader-accent); transition: width .1s linear; }

/* ---------- Pre-content: story title / chapter title / author / nav / edit icons ---------- */
.reader-precontent {
  text-align: center;
  padding: var(--space-6) clamp(16px, 4vw, 40px) 0;
  max-width: 760px;
  margin: 0 auto;
}
.reader-story-title {
  display: inline-block;
  background: none;
  border: none;
  appearance: none;
  padding: 0;
  font-size: 13px; font-weight: 700;
  color: var(--reader-accent);
  margin-bottom: 6px;
}
.reader-story-title:hover { text-decoration: underline; }
.reader-precontent .chapter-heading { margin-bottom: 6px; }
.reader-author {
  font-size: 13px; color: var(--reader-text-soft);
  margin-bottom: var(--space-4);
}
.reader-edit-icons {
  display: flex; align-items: center; justify-content: center;
  gap: 8px; flex-wrap: wrap;
  margin: var(--space-4) 0 var(--space-2);
}
/* Icon-only edit-icons row (icon glyphs, no adjacent label text) — sized up
   slightly from the row's own default so a lone glyph doesn't read as too
   small/lost. "Nm" (Edit name) and "Aa" (Reading settings — relocated here
   from a floating fab) are both text-glyphs, sharing this same bold/
   smaller-font treatment so a two-letter glyph reads as an icon, not
   body text. */
.reader-edit-icons button { font-size: 15px; }
.reader-edit-icons button svg { width: 16px; height: 16px; display: block; }
.reader-edit-name-btn { font-weight: 800; font-size: 13px; }
/* Edit raw icon's active state while in-place raw-edit mode is open
   (see wireEditRawIcon) — same accent-color cue as .nav-toc. */
.reader-edit-icons button.is-active { color: var(--reader-accent); }

/* ---------- Prev / TOC / Next nav bars ---------- */
/* Standalone buttons now, not one connected toolbar strip — no shared
   background/border-top/bottom, each button gets its own border + radius,
   with a real gap between them instead of the old 1px hairline. */
.reader-navbar {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: none;
}
.reader-navbar.reader-navbar-bottom {
  /* Capped/centered the same as .reader-channel/.reader-precontent
     elsewhere on this page. This bar sits between the two .reader-channel
     blocks as a plain sibling (see FIX 1.28 — it's not nested in either),
     so without its own cap it stretched to .reader-body's full width,
     spreading 3 buttons across up to ~1240px on a wide desktop window
     instead of the tight column mobile shows. The top .reader-navbar
     doesn't need this rule — it's already nested inside
     .reader-precontent, which carries its own 760px cap. */
  max-width: 760px;
  margin: 0 auto;
  margin-top: 16px;
}
/* Prev/Next: compact, icon-sized buttons (not stretched via flex:1 across
   the row anymore) — exact dimensions per spec. TOC: same height, ~1.88x
   Prev/Next's width, the visually dominant button of the three.
   .reader-navbar's own justify-content:center keeps this now-compact
   cluster from stranding itself at the row's left edge, since the
   buttons no longer stretch to fill the full 760px-capped width between
   them. */
.reader-navbar button {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  background: none;
  border: 1px solid var(--reader-border);
  border-radius: var(--radius-md);
  padding: 0;
  width: 47.83px;
  height: 32.93px;
  line-height: 1;
  font-size: 20px; font-weight: 600;
  color: var(--reader-text-soft);
  box-sizing: border-box;
}
.reader-navbar button:hover { color: var(--reader-text); background: rgba(127,127,127,0.06); }
.reader-navbar button[disabled] { opacity: 0.35; pointer-events: none; }
.reader-navbar button.nav-toc { width: 89.78px; color: var(--reader-accent); }

/* ---------- Reading channel ---------- */
.reader-channel {
  max-width: 760px;
  min-width: 0;
  width: 100%;
  margin: 0 auto;
}
@media (max-width: 640px) {
  .reader-channel {
    /* Cancels .reader-body's own clamp(16px,4vw,40px) side padding, which
       on a narrow viewport (where .reader-channel's max-width:760px isn't
       the limiting factor) is what actually creates the gap from the real
       screen edge. Desktop is untouched — margin:0 auto there already
       recentres the 760px column regardless of side margins, so this is
       scoped here only to avoid fighting that auto-centering. */
    margin-left: calc(-1 * clamp(16px, 4vw, 40px));
    margin-right: calc(-1 * clamp(16px, 4vw, 40px));
  }
}

.chapter-heading {
  font-size: clamp(20px, 3vw, 26px);
  font-weight: 800;
  letter-spacing: -0.01em;
  margin-bottom: var(--space-2);
}

.chapter-text {
  font-size: var(--reader-fs, 18px);
  line-height: var(--reader-lh, 1.75);
  font-family: var(--reader-font, var(--font-reading-serif));
}
.chapter-text p { margin: 0 0 1.35em; }
.chapter-text p:first-of-type::first-letter {
  font-size: 2.6em;
  font-weight: 800;
  float: left;
  line-height: 0.85;
  padding: 0.05em 0.09em 0 0;
  color: var(--reader-accent);
}

/* raw-text collapsible panel (bilingual toggle) */
.raw-panel {
  margin-bottom: var(--space-6);
  border: 1px solid var(--reader-border);
  border-radius: var(--radius-md);
  background: var(--reader-surface);
  overflow: hidden;
}
.raw-panel[hidden] { display: none; }
.raw-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  font-size: 12px; font-weight: 700; color: var(--reader-text-soft);
  border-bottom: 1px solid var(--reader-border);
}
.raw-panel-body {
  padding: 16px;
  font-family: var(--font-reading-serif);
  font-size: 15px;
  line-height: 1.9;
  color: var(--reader-text-soft);
  max-height: 220px;
  overflow-y: auto;
  overflow-wrap: break-word;
}
/* [Bug fix] One real <p> per logical paragraph (paragraphBlocksHtml) now,
   not a single pre-wrapped block. */
.raw-panel-body p { margin: 0 0 10.5px; }
.raw-panel-body p:last-child { margin-bottom: 0; }

/* ---------- Reading settings panel ---------- */
/* Trigger used to be a floating circular fab (bottom-right, always on top
   of the page), then relocated into .reader-edit-icons as an inline "Aa"
   icon. Now reached via the Speed Dial's "Reading settings" item (see
   .speed-dial below, #readerFab) instead — same #toolbarPanel, same
   wireReaderToolbar wiring, just a different trigger location each time.
   bottom:148px (not the original 84px) clears the Speed Dial's own toggle
   FAB, which now occupies the bottom:80px/52px-tall spot right below. */
.reader-toolbar-panel {
  position: fixed; right: 20px; bottom: 148px; z-index: 70;
  width: min(320px, calc(100vw - 40px));
  background: var(--reader-surface);
  border: 1px solid var(--reader-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-4);
  transform: translateY(12px) scale(0.98);
  opacity: 0;
  visibility: hidden;
  transition: transform .16s ease, opacity .16s ease, visibility .16s;
  color: var(--reader-text);
}
.reader-toolbar-panel[data-open='true'] {
  transform: translateY(0) scale(1);
  opacity: 1;
  visibility: visible;
}

/* ---------- Speed Dial (bottom-right floating quick-actions FAB) ----------
   Fixed at bottom:80px/right:20px per spec — a compact toggle that expands
   a 2x2 icon-only grid above it: Top+Nav share the near row, Comments+
   Reading-settings share the far row (DOM order top/nav/comments/settings,
   grid-auto-flow fills left-to-right then wraps, giving that exact
   2-per-row pairing). z-index:999 keeps it above ordinary page content but
   still below system modals (.modal-overlay is 100, .toc-drawer is 85 —
   both comfortably under it since neither is meant to coexist with the
   reader chrome anyway; 999 is simply what the spec calls for). */
.speed-dial {
  position: fixed; right: 20px; bottom: 80px; z-index: 999;
  display: flex; flex-direction: column; align-items: flex-end;
  transition: opacity .25s ease, transform .2s ease;
}
.speed-dial.is-dimmed { opacity: 0.3; }
.speed-dial:hover, .speed-dial:focus-within { opacity: 1; }

/* [Reader FAB enhancement] Modal/dialog/popup exclusion — every modal in
   this app is a .modal-overlay appended to <body>; the TOC drawer/reading-
   settings panel toggle the same body class themselves (see
   updateModalOpenBodyClass, app.js). !important per spec, since this must
   win over any inline left/top a custom drag position has set. */
body.modal-open .speed-dial { display: none !important; }

/* Auto-shift above the comment box (IntersectionObserver, app.js) — a
   transform, not a position change, so it composes safely on top of either
   the default corner position or a custom dragged one. */
.speed-dial.shifted-up { transform: translateY(-72px); }

/* Hide while composing a comment (focus/blur on #comment-body, app.js) —
   leaves maximum space for the virtual keyboard; no transition needed, the
   keyboard's own appearance is already the dominant visual change. */
.speed-dial.is-hidden-for-input { display: none; }

/* Drag-to-reposition (pointerdown/move/up on .speed-dial-toggle, app.js):
   no transition while actively dragging, so the FAB tracks the pointer
   1:1 instead of visibly lagging behind it. */
.speed-dial.is-dragging { transition: none; }

/* Same 44px size as .speed-dial-item — the closed FAB reads as one more
   icon in the same family rather than a visually heavier "master" button. */
/* Same white-bg/red-icon treatment as .speed-dial-item (per the reference
   screenshot) — the closed FAB reads as one more icon in the same family,
   not a visually distinct "master" button. Flex centering keeps equal
   space around the icon on all four sides regardless of button/icon size. */
.speed-dial-toggle {
  width: 44px; height: 44px; border-radius: 50%;
  background: #ffffff; color: var(--brand);
  border: none; box-shadow: var(--shadow-md);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  cursor: grab;
  touch-action: none; /* let pointermove drive dragging instead of the browser's own scroll/pan gestures */
}
.speed-dial.is-dragging .speed-dial-toggle { cursor: grabbing; }
/* Resting icon is a nav-bar (hamburger/list) glyph — the closed FAB
   visually doubles as a jump-to-navigation affordance even before the
   dial is opened. Same 20px size as .speed-dial-item svg for visual
   consistency across the whole family. */
.speed-dial-toggle svg { width: 20px; height: 20px; display: block; }

.speed-dial-menu {
  display: grid;
  grid-template-columns: repeat(2, 44px);
  gap: 10px;
  margin-bottom: 12px;
  pointer-events: none;
}
.speed-dial-menu[data-open='true'] { pointer-events: auto; }

/* Icon-only, per the reference screenshot: white circle, red glyph —
   fixed regardless of reader theme (a floating quick-actions control,
   not reading content, so it keeps the app's own brand red rather than
   shifting with the sepia/green/dark reader palette). title/aria-label
   cover the tooltip and accessible name in place of a visible label. */
.speed-dial-item {
  width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: #ffffff; color: var(--brand);
  border: none;
  box-shadow: var(--shadow-md);
  font-size: 15px; font-weight: 800;
  opacity: 0;
  transform: translateY(8px) scale(0.92);
  transition: opacity .16s ease, transform .16s ease;
}
.speed-dial-item svg { width: 20px; height: 20px; }
.speed-dial-menu[data-open='true'] .speed-dial-item {
  opacity: 1;
  transform: translateY(0) scale(1);
}
/* Staggered reveal, in DOM/reading order. */
.speed-dial-menu[data-open='true'] .speed-dial-item:nth-child(1) { transition-delay: 0ms; }
.speed-dial-menu[data-open='true'] .speed-dial-item:nth-child(2) { transition-delay: 30ms; }
.speed-dial-menu[data-open='true'] .speed-dial-item:nth-child(3) { transition-delay: 60ms; }
.speed-dial-menu[data-open='true'] .speed-dial-item:nth-child(4) { transition-delay: 90ms; }

.toolbar-row { margin-bottom: var(--space-4); }
.toolbar-row:last-child { margin-bottom: 0; }
.toolbar-row-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--reader-text-soft); margin-bottom: 8px; display: flex; align-items: center; justify-content: space-between;
}

.theme-swatches { display: flex; gap: 10px; }
.theme-swatch {
  width: 32px; height: 32px; border-radius: 50%;
  border: 2px solid var(--reader-border);
  cursor: pointer;
  position: relative;
}
.theme-swatch[data-active='true'] { border-color: var(--reader-accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--reader-accent) 30%, transparent); }
.theme-swatch[data-active='true']::after {
  content: '✓'; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 900;
}
.swatch-light { background: #ffffff; color: #1c1c1c; }
.swatch-sepia { background: #fbf0d9; color: #4a3b25; }
.swatch-green { background: #e8f0e8; color: #223322; }
.swatch-dark { background: #1a1a1a; color: #e8e8e8; }

.stepper { display: flex; align-items: center; gap: 0; border: 1px solid var(--reader-border); border-radius: var(--radius-sm); overflow: hidden; width: fit-content; }
.stepper button {
  width: 30px; height: 30px; background: var(--reader-bg); border: none; color: var(--reader-text);
  font-weight: 700; font-size: 15px;
}
.stepper button:hover { background: color-mix(in srgb, var(--reader-accent) 12%, var(--reader-bg)); }
.stepper button svg { width: 15px; height: 15px; display: block; margin: auto; }
.stepper .val { width: 44px; text-align: center; font-size: 12.5px; font-weight: 600; font-family: var(--font-mono); }

.reader-select {
  width: 100%; height: 34px; border-radius: var(--radius-sm);
  border: 1px solid var(--reader-border); background: var(--reader-bg); color: var(--reader-text);
  padding: 0 10px; font-size: 13px; font-weight: 600;
}

.toolbar-divider { height: 1px; background: var(--reader-border); margin: var(--space-3) 0; }

.pin-row { display: flex; align-items: center; justify-content: space-between; font-size: 13px; font-weight: 600; }
.tw-switch { position: relative; width: 38px; height: 22px; }
.tw-switch input { opacity: 0; width: 0; height: 0; }
.tw-switch .track { position: absolute; inset: 0; background: var(--reader-border); border-radius: var(--radius-pill); transition: background .15s ease; }
.tw-switch .track::after { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s ease; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.tw-switch input:checked + .track { background: var(--reader-accent); }
.tw-switch input:checked + .track::after { transform: translateX(16px); }

/* ---------- TOC Drawer ---------- */
.toc-drawer { position: fixed; inset: 0; z-index: 85; visibility: hidden; }
.toc-drawer[data-open='true'] { visibility: visible; }
.toc-drawer .scrim { position: absolute; inset: 0; background: rgba(0,0,0,0.45); opacity: 0; transition: opacity .2s ease; }
.toc-drawer[data-open='true'] .scrim { opacity: 1; }
.toc-drawer .panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: min(380px, 90vw);
  background: var(--reader-surface); color: var(--reader-text);
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform .25s ease;
}
.toc-drawer[data-open='true'] .panel { transform: translateX(0); }

.toc-head { padding: var(--space-4) var(--space-4) var(--space-3); border-bottom: 1px solid var(--reader-border); }
/* Single row now: search bar -> search-scope icon -> sort icon -> close.
   The "Table of Contents" label is gone (the drawer opening already makes
   its purpose obvious), so this row carries the panel's only controls. */
.toc-head-row { display: flex; align-items: center; gap: 8px; }
.toc-close { width: 32px; height: 32px; border-radius: 50%; border: none; background: var(--reader-bg); font-size: 16px; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; }
.toc-close svg { width: 15px; height: 15px; display: block; }
/* Same shape/dimensions as .header-search in components.css (pill radius,
   40px input height, left-aligned icon at 13px/16px) — just using the
   reader's own theme tokens instead of the main site's, so it doesn't
   clash with the dark/sepia/green reading themes. */
.toc-search { position: relative; flex: 1; min-width: 0; }
.toc-search input {
  width: 100%; height: 40px; border-radius: var(--radius-pill); border: 1px solid var(--reader-border);
  background: var(--reader-bg); color: var(--reader-text); padding: 0 16px 0 38px; font-size: var(--fs-sm);
  transition: border-color .15s ease;
}
.toc-search input:focus { outline: none; border-color: var(--reader-accent); }
.toc-search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--reader-text-soft); pointer-events: none; }
.toc-icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  border: 1px solid var(--reader-border); background: var(--reader-bg); color: var(--reader-text-soft);
  padding: 0;
}
.toc-icon-btn svg { width: 15px; height: 15px; transition: transform .2s ease; }
.toc-icon-btn[data-newest-first='true'] svg { transform: rotate(180deg); }
.toc-icon-btn[aria-expanded='true'] { color: var(--reader-accent); border-color: var(--reader-accent); }

/* Search-scope picker. It is intentionally independent from the Main Nav
   dropdown component so it remains available at every reader width. The
   selected scope stays color-coded while the popup is closed. */
#tocDrawerScopeToggle[data-scope='title'] { color: #6d5bd0; border-color: #6d5bd0; background: color-mix(in srgb, #6d5bd0 12%, transparent); }
#tocDrawerScopeToggle[data-scope='raw'] { color: #b86600; border-color: #b86600; background: color-mix(in srgb, #b86600 12%, transparent); }
#tocDrawerScopeToggle[data-scope='converted'] { color: #16805b; border-color: #16805b; background: color-mix(in srgb, #16805b 12%, transparent); }
.toc-scope-menu { position: relative; }
.toc-scope-options {
  position: absolute; z-index: 6; top: calc(100% + 6px); right: 0;
  min-width: 190px; display: flex; flex-direction: column; gap: 2px; padding: 6px;
  background: var(--reader-surface); border: 1px solid var(--reader-border);
  border-radius: var(--radius-md); box-shadow: var(--shadow-lg);
  opacity: 0; visibility: hidden; pointer-events: none; transform: translateY(-4px);
  transition: opacity .15s ease, transform .15s ease, visibility .15s ease;
}
.toc-scope-menu[data-open='true'] .toc-scope-options {
  opacity: 1; visibility: visible; pointer-events: auto; transform: translateY(0);
}
.toc-scope-options button {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 12px;
  border: none; border-radius: var(--radius-sm); background: none; text-align: left;
  font-family: var(--font-ui); font-size: var(--fs-sm); font-weight: 600;
  color: var(--reader-text);
}
.toc-scope-options button:hover,
.toc-scope-options button:focus-visible { background: var(--reader-bg); outline: none; }
.toc-scope-options button.active {
  color: var(--reader-accent);
  background: color-mix(in srgb, var(--reader-accent) 12%, transparent);
}
.toc-scope-options button svg { width: 16px; height: 16px; flex-shrink: 0; }

.toc-list { flex: 1; overflow-y: auto; padding: 6px var(--space-2); }
.toc-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left;
  padding: 10px 12px; border-radius: var(--radius-sm);
  font-size: 13px; background: none; border: none; color: var(--reader-text);
}
.toc-item:hover { background: var(--reader-bg); }
.toc-item.current { background: color-mix(in srgb, var(--reader-accent) 14%, transparent); font-weight: 700; }
.toc-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toc-item .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--reader-border); flex-shrink: 0; }
.toc-item.read .dot { background: var(--success); }
.toc-empty { padding: var(--space-5); text-align: center; color: var(--reader-text-soft); font-size: 13px; }

@media (max-width: 640px) {
  .toc-drawer .panel { width: 100%; }
}

/* Chapter Reading — comment box: borderless/transparent, auto-expands with
   content instead of scrolling internally (app.js's wireAutoExpandTextarea,
   [UI/UX] standardized text box behavior — general boxes start at ~5
   visible lines and grow uncapped from line 6), manual drag-resize
   disabled since the box already grows on its own. Overrides
   .comment-form textarea's old bordered/paper-bg look (styles.css), which
   legacy form textareas still use. */
.comment-form textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--reader-border);
  border-radius: var(--radius-sm);
  background: none;
  resize: none;
  overflow-y: hidden;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  color: var(--reader-text);
}
.comment-form textarea:focus { outline: none; border-color: var(--reader-accent); }
.comment-form textarea::placeholder { color: var(--reader-text-soft); }
