/*
 * books/reader.css
 *
 * Host styling for the three-panel reader UI: header, left panel (TOC/search),
 * reader pane, right panel (context).  Nothing inside .book-scope is styled here
 * — that area belongs to the book's custom_css.
 *
 * All colours use site CSS custom properties so dark/light theme works automatically.
 */

/* ---------- Reader shell ---------- */

.book-reader {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

/* ---------- Header ---------- */

.book-reader__header {
  display: flex;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border-default);
  background: var(--color-bg-secondary);
  position: sticky;
  top: 0;
  z-index: 10;
}

/* Panel toggle buttons — left (TOC) and right (context) */
.book-reader__panel-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  cursor: pointer;
  padding: .45rem .55rem;
  color: var(--color-text-secondary);
  border-radius: 4px;
  transition: background .15s, color .15s;
  line-height: 1;
  font-size: 1rem;
}
.book-reader__panel-btn:hover,
.book-reader__panel-btn[aria-expanded="true"] {
  background: var(--color-bg-tertiary);
  color: var(--color-text-primary);
}

/* Hamburger icon for left panel */
.book-reader__panel-btn::before {
  content: '';
  display: inline-block;
  width: 1.1em;
  height: .85em;
  vertical-align: middle;
  background:
    linear-gradient(currentColor, currentColor) 0    0   / 100% .13em no-repeat,
    linear-gradient(currentColor, currentColor) 0   50%  / 100% .13em no-repeat,
    linear-gradient(currentColor, currentColor) 0  100%  / 100% .13em no-repeat;
}

/* Right panel icon: three lines, shorter on the left (right-sidebar indicator) */
.book-reader__panel-btn--right::before {
  background:
    linear-gradient(currentColor, currentColor) 30%   0   / 70% .13em no-repeat,
    linear-gradient(currentColor, currentColor)  0%  50%  / 100% .13em no-repeat,
    linear-gradient(currentColor, currentColor) 30% 100%  / 70% .13em no-repeat;
}

.book-reader__title {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-reader__title a { color: inherit; text-decoration: none; }
.book-reader__subtitle { color: var(--color-text-secondary); margin: 0; font-size: 0.85rem; }
.book-reader__search { flex-shrink: 0; }
.book-reader__search input {
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--color-border-default);
  border-radius: 4px;
  min-width: 14rem;
  background: var(--color-bg-primary);
  color: var(--color-text-primary);
}
@media (max-width: 480px) {
  .book-reader__search input { min-width: 8rem; }
}

/* ---------- Layout ---------- */

.book-reader__pane {
  padding: 2rem 3rem;
}
@media (max-width: 768px) {
  .book-reader__pane { padding: 1.5rem 1rem; }
}

/* ---------- Panels — unified off-canvas (both sides, all screen sizes) ---------- */

.book-reader__sidebar,
.book-reader__ctx-panel {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 20rem;
  max-width: 85vw;
  z-index: 200;
  background: var(--color-bg-secondary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform .25s ease, box-shadow .25s ease;
}

.book-reader__sidebar {
  left: 0;
  border-right: 1px solid var(--color-border-default);
  transform: translateX(-100%);
}
.book-reader__sidebar.is-open {
  transform: translateX(0);
  box-shadow: 4px 0 20px rgba(0, 0, 0, .15);
}

.book-reader__ctx-panel {
  right: 0;
  border-left: 1px solid var(--color-border-default);
  transform: translateX(100%);
}
.book-reader__ctx-panel.is-open {
  transform: translateX(0);
  box-shadow: -4px 0 20px rgba(0, 0, 0, .15);
}

/* ---------- Panel header (inside each panel) ---------- */

.book-reader__panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 1rem;
  border-bottom: 1px solid var(--color-border-default);
  flex-shrink: 0;
  background: var(--color-bg-secondary);
}

.book-reader__panel-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--color-text-secondary);
}

.book-reader__font-ctrl { display: flex; gap: .25rem; }
.book-reader__font-ctrl button {
  border: 1px solid var(--color-border-default);
  background: transparent;
  padding: .2rem .5rem;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  font-size: .8rem;
  color: var(--color-text-secondary);
  line-height: 1;
}
.book-reader__font-ctrl button:hover {
  background: var(--color-bg-tertiary);
  color: var(--color-text-primary);
}
#btn-random-section::before { content: '⇄'; }

/* ---------- TOC (in left panel) ---------- */

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

.book-toc__list, .book-toc__sublist {
  list-style: none;
  margin: 0;
  padding: 0;
}
.book-toc__list { font-size: 0.9rem; }
.book-toc__group > details > summary {
  padding: 0.45rem 1rem;
  cursor: pointer;
  font-weight: 600;
  user-select: none;
  line-height: 1.4;
}
.book-toc__group > details > summary:hover { background: var(--color-bg-tertiary); }
.book-toc__group.is-current-group > details > summary { color: var(--color-accent); }
.book-toc__sublist { padding-left: 0; }
.book-toc__leaf > a {
  display: block;
  padding: 0.3rem 1.5rem;
  color: var(--color-text-primary);
  text-decoration: none;
  border-left: 3px solid transparent;
  line-height: 1.4;
  font-size: 0.85rem;
}
.book-toc__leaf > a:hover { background: var(--color-bg-tertiary); }
.book-toc__leaf.is-current > a {
  background: var(--color-accent-light);
  border-left-color: var(--color-accent);
  color: var(--color-accent-text);
  font-weight: 600;
}

/* ---------- Search results (in left panel, below TOC) ---------- */

.book-search-results {
  border-top: 1px solid var(--color-border-default);
  overflow-y: auto;
  flex-shrink: 0;
  max-height: 50%;
}
.book-search-results:empty { display: none; }
.book-search-results__header {
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}
.book-search-results__clear {
  margin-left: 0.5rem;
  border: 0;
  background: transparent;
  color: var(--color-accent);
  cursor: pointer;
  font-size: 0.85rem;
}
.book-search-results__list { list-style: none; margin: 0; padding: 0; }
.book-search-results__item a {
  display: block;
  padding: 0.45rem 1rem;
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid var(--color-border-subtle);
  font-size: 0.85rem;
}
.book-search-results__item a:hover { background: var(--color-bg-tertiary); }
.book-search-results__snippet {
  margin: 0.2rem 0 0;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
}
.book-search-results__empty {
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

/* When search results are active, hide TOC and give results the full panel. */
.book-reader__sidebar.has-search-results .book-toc { display: none; }
.book-reader__sidebar.has-search-results .book-search-results {
  max-height: none;
  flex: 1;
}

/* ---------- Right panel: context sections ---------- */

.book-ctx__section {
  padding: 1rem;
  border-bottom: 1px solid var(--color-border-subtle);
}
.book-ctx__heading {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--color-text-secondary);
}
.book-ctx__empty {
  margin: 0;
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

/* ---------- Backdrop — shared between left and right panels ---------- */

.book-drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  z-index: 199;
  touch-action: none;
}
.book-drawer-backdrop.is-active { display: block; }

/* ---------- Font scale overrides (data-font-scale on .book-reader) ---------- */

.book-reader[data-font-scale="0"] .book-scope { font-size: .82rem !important; }
.book-reader[data-font-scale="2"] .book-scope { font-size: 1.1rem  !important; }
.book-reader[data-font-scale="3"] .book-scope { font-size: 1.25rem !important; }
.book-reader[data-font-scale="4"] .book-scope { font-size: 1.45rem !important; }

/*
 * The boundary. Everything from the book's custom_css applies only inside
 * here, prefixed with .book-scope by the CSS scoper.
 *
 * `all: revert` clears inherited host styles so the book starts from a
 * neutral baseline.
 */
.book-scope {
  all: revert;
  display: block;
  max-width: 65ch;
  margin: 0 auto;
  -webkit-hyphens: auto;
  -webkit-hyphenate-limit-before: 3;
  -webkit-hyphenate-limit-after: 3;
  -webkit-hyphenate-limit-chars: 6 3 3;
  -webkit-hyphenate-limit-lines: 2;
  -webkit-hyphenate-limit-last: always;
  -webkit-hyphenate-limit-zone: 8%;
  -moz-hyphens: auto;
  -moz-hyphenate-limit-chars: 6 3 3;
  -moz-hyphenate-limit-lines: 2;
  -moz-hyphenate-limit-last: always;
  -moz-hyphenate-limit-zone: 8%;
  -ms-hyphens: auto;
  -ms-hyphenate-limit-chars: 6 3 3;
  -ms-hyphenate-limit-lines: 2;
  -ms-hyphenate-limit-last: always;
  -ms-hyphenate-limit-zone: 8%;
  hyphens: auto;
  hyphenate-limit-chars: 6 3 3;
  hyphenate-limit-lines: 2;
  hyphenate-limit-last: always;
  hyphenate-limit-zone: 8%;
}

.book-scope hr.footnote-rule {
  width: 33%;
  margin: 1.5em 0 0.75em;
  border: none;
  border-top: 1px solid currentColor;
  opacity: 0.35;
  margin-left: 0;
}

.book-scope .footnotes {
  font-size: smaller;
  margin-left: 1.5em;
}

.book-scope .meta_title {
  margin-bottom: 1.5em;
  font-weight: 500;
}

/* ---------- Prev / next navigation ---------- */

.book-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin: 2rem auto 0;
  max-width: 65ch;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border-default);
  font-size: 0.95rem;
}
.book-nav__prev, .book-nav__next {
  color: var(--color-accent);
  text-decoration: none;
  max-width: 45%;
}
.book-nav__prev:hover, .book-nav__next:hover { text-decoration: underline; }
.book-nav__disabled { color: var(--color-border-strong); }
.book-nav__next { text-align: right; margin-left: auto; }

/* ---------- Book landing ---------- */

.book-landing {
  max-width: 65ch;
  margin: 0 auto;
}
.book-landing__cover {
  max-width: 12rem;
  float: right;
  margin: 0 0 1rem 1rem;
  border: 1px solid var(--color-border-default);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .08);
}
.book-landing__meta dt { font-weight: 600; }
.book-landing__meta dt, .book-landing__meta dd { display: inline; margin: 0; }
.book-landing__meta dt::after { content: ": "; }
.book-landing__meta dd::after { content: ""; display: block; }

/* ---------- Footnote hover tooltip ---------- */

#fn-tip {
  position: fixed;
  z-index: 9999;
  max-width: 22rem;
  padding: .4rem .65rem;
  background: var(--color-text-primary);
  color: var(--color-bg-primary);
  font-size: .8rem;
  line-height: 1.45;
  border-radius: 4px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .35);
  word-break: break-word;
  pointer-events: none;
  opacity: 0;
  transition: opacity .1s;
}
#fn-tip.fn-tip--visible {
  pointer-events: auto;
  opacity: 1;
}
