/* ==========================================================================
   Newton Online Library (D-056)
   --------------------------------------------------------------------------
   Self-contained on purpose: the library pages load this one stylesheet and
   nothing from any other origin. Arabic interface text uses the Cairo font
   the project already vendors; English book text uses the system serif,
   which every device has and which reads well at length.
   ========================================================================== */

@font-face {
  font-family: "Cairo";
  font-style: normal;
  font-weight: 300 900;
  font-display: swap;
  src: url("../fonts/cairo-var.ef9919827ad8.woff2") format("woff2-variations"),
       url("../fonts/cairo-var.ef9919827ad8.woff2") format("woff2");
}

:root {
  --navy-950: #050d1a;
  --navy-900: #0a192f;
  --navy-700: #1e3a68;
  --gold-600: #996515;
  --gold-400: #c59b27;
  --gold-300: #d4af37;
  --gold-100: #f7edc4;
  --cream-50: #fffdf9;
  --cream-100: #fbf9f5;
  --cream-200: #f5efe6;
  --cream-300: #e8dfd0;
  --ink: #1c2230;
  --ink-soft: #4b5563;
  --ink-faint: #8a93a3;

  --ui-font: "Cairo", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  --book-font: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", "Times New Roman", serif;
  --lib-prose-size: 19px;

  --radius: 12px;
  --shadow: 0 6px 20px rgba(10, 25, 47, 0.10);
  --shadow-lg: 0 14px 36px rgba(10, 25, 47, 0.18);
}

/* Page turns in the reader cross-fade instead of flashing white (Chrome 126+,
   ignored elsewhere). */
@view-transition { navigation: auto; }

*, *::before, *::after { box-sizing: border-box; }

body.lib {
  margin: 0;
  background: var(--cream-100);
  color: var(--ink);
  font-family: var(--ui-font);
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

a { color: inherit; }
img { max-width: 100%; height: auto; }
button { font: inherit; }

/* --- Top bar ---------------------------------------------------------------- */

.lib-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem clamp(1rem, 4vw, 2rem);
  background: var(--navy-900);
  color: #fff;
}

.lib-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.lib-bar__link {
  text-decoration: none;
  color: var(--gold-300);
  font-weight: 600;
}

.lib-langs {
  display: flex;
  gap: 0.25rem;
  margin-inline-start: auto;
}

.lib-langs a {
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.85rem;
  text-decoration: none;
  color: #cbd5e1;
}

.lib-langs a.is-active { background: var(--gold-300); color: var(--navy-950); font-weight: 700; }

/* --- Shared ----------------------------------------------------------------- */

.lib-wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.5rem clamp(1rem, 4vw, 2rem) 4rem;
}

.lib-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 46px;
  padding: 0.6rem 1.4rem;
  border-radius: 999px;
  background: var(--navy-900);
  color: var(--gold-300);
  font-weight: 700;
  text-decoration: none;
  border: 1px solid var(--navy-900);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.lib-btn:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

.lib-btn--ghost { background: transparent; color: var(--navy-900); }

.lib-badges { display: flex; flex-wrap: wrap; gap: 0.35rem; }

.lib-badge {
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.12rem 0.55rem;
  border-radius: 999px;
  background: var(--cream-200);
  color: var(--ink-soft);
}

.lib-badge--early     { background: #dcfce7; color: #166534; }
.lib-badge--growing   { background: var(--gold-100); color: var(--gold-600); }
.lib-badge--confident { background: #e0e7ff; color: #3730a3; }

/* Book covers: real artwork where the source has it, otherwise the title set
   on a cloth spine — no request, no placeholder image. */
.lib-cover {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 6px;
  background: var(--cream-200);
  box-shadow: var(--shadow);
}

.lib-cover--drawn {
  position: relative;
  display: grid;
  place-items: center;
  padding: 1rem 0.9rem 1rem 1.4rem;
  overflow: hidden;
}

.lib-cover--drawn::before {
  content: "";
  position: absolute;
  inset-block: 0;
  left: 0;
  width: 12px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.04));
}

.lib-cover--drawn span {
  font-family: var(--book-font);
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.3;
  text-align: center;
  color: #fdfaf3;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.lib-cover--1 { background: linear-gradient(150deg, #1e3a68, #0a192f); }
.lib-cover--2 { background: linear-gradient(150deg, #7c2d12, #431407); }
.lib-cover--3 { background: linear-gradient(150deg, #14532d, #052e16); }
.lib-cover--4 { background: linear-gradient(150deg, #4c1d95, #2e1065); }
.lib-cover--5 { background: linear-gradient(150deg, #9a3412, #7c2d12); }
.lib-cover--6 { background: linear-gradient(150deg, #155e75, #083344); }

/* --- Catalogue ---------------------------------------------------------------- */

.lib-hero {
  padding: clamp(2rem, 6vw, 3.5rem) 1.25rem;
  text-align: center;
  background: var(--navy-900);
  color: #fff;
}

.lib-hero h1 {
  margin: 0 0 0.5rem;
  font-size: clamp(1.6rem, 4.5vw, 2.4rem);
  color: var(--gold-300);
}

.lib-hero p { margin: 0 auto; max-width: 620px; color: #cbd5e1; }

.lib-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.75rem;
}

.lib-seg {
  display: flex;
  gap: 0.25rem;
  padding: 0.25rem;
  border-radius: 999px;
  background: var(--cream-200);
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 100%;
}

.lib-seg::-webkit-scrollbar { display: none; }

.lib-seg button {
  flex: 0 0 auto;
  border: 0;
  border-radius: 999px;
  padding: 0.4rem 0.95rem;
  min-height: 38px;
  background: transparent;
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
}

.lib-seg button.is-active { background: var(--navy-900); color: var(--gold-300); }

.lib-search {
  flex: 1 1 220px;
  min-height: 42px;
  padding: 0.5rem 1rem;
  border: 1px solid var(--cream-300);
  border-radius: 999px;
  background: #fff;
  font: inherit;
  font-size: 0.92rem;
}

.lib-search:focus { outline: 2px solid var(--gold-300); outline-offset: 1px; }

.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1.75rem 1.25rem;
}

.lib-card {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  text-decoration: none;
  /* Off-screen cards are skipped until scrolled to; the size keeps the
     scrollbar honest meanwhile. */
  content-visibility: auto;
  contain-intrinsic-size: auto 290px;
}

.lib-card[hidden] { display: none; }

.lib-card .lib-cover { transition: transform 0.2s ease, box-shadow 0.2s ease; margin-bottom: 0.35rem; }
.lib-card:hover .lib-cover,
.lib-card:focus-visible .lib-cover { transform: translateY(-4px); box-shadow: var(--shadow-lg); }

.lib-card__title { font-weight: 700; font-size: 0.92rem; line-height: 1.35; color: var(--ink); }
.lib-card__author { font-size: 0.8rem; color: var(--ink-faint); }

.lib-empty { text-align: center; color: var(--ink-faint); padding: 3rem 0; }

.lib-partners {
  margin-top: 4rem;
  padding-top: 2rem;
  border-top: 1px solid var(--cream-300);
}

.lib-partners h2 { margin: 0 0 0.25rem; font-size: 1.3rem; }
.lib-partners > p { margin: 0 0 1.25rem; color: var(--ink-soft); font-size: 0.92rem; }

.lib-partner-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.9rem;
}

.lib-partner {
  display: flex;
  gap: 0.8rem;
  align-items: flex-start;
  padding: 1rem;
  border: 1px solid var(--cream-300);
  border-radius: var(--radius);
  background: #fff;
  text-decoration: none;
  transition: border-color 0.15s ease;
}

.lib-partner:hover { border-color: var(--gold-400); }

.lib-partner__mono {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: linear-gradient(140deg, var(--gold-400), var(--gold-300));
  color: var(--navy-950);
  font-weight: 800;
}

.lib-partner__body { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.85rem; color: var(--ink-soft); }
.lib-partner__body strong { color: var(--ink); font-size: 0.95rem; }

/* --- Book page ------------------------------------------------------------------ */

.lib-book {
  display: grid;
  grid-template-columns: minmax(180px, 280px) 1fr;
  gap: clamp(1.25rem, 4vw, 2.5rem);
  align-items: start;
}

.lib-book__info h1 {
  margin: 0.6rem 0 0.25rem;
  font-family: var(--book-font);
  font-size: clamp(1.6rem, 4vw, 2.3rem);
  line-height: 1.2;
  text-align: start;
}

.lib-book__author { margin: 0 0 1rem; color: var(--ink-soft); font-size: 1.05rem; }
.lib-book__desc { font-family: var(--book-font); font-size: 1.05rem; line-height: 1.7; }
.lib-book__meta { color: var(--ink-faint); font-size: 0.9rem; margin: 0 0 1.25rem; }

.lib-toc, .lib-credits {
  margin-top: 2.5rem;
  padding: 1.25rem 1.5rem;
  background: #fff;
  border: 1px solid var(--cream-300);
  border-radius: var(--radius);
}

.lib-toc h2, .lib-credits h2 { margin: 0 0 0.75rem; font-size: 1.1rem; }

.lib-toc ol {
  margin: 0;
  padding-left: 1.4rem;
  columns: 2 280px;
  column-gap: 2rem;
  font-family: var(--book-font);
}

.lib-toc li { break-inside: avoid; padding: 0.15rem 0; }
.lib-toc a { text-decoration: none; }
.lib-toc a:hover { text-decoration: underline; color: var(--gold-600); }

.lib-credits p { margin: 0 0 0.75rem; font-size: 0.92rem; }

.lib-credits dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.35rem 1rem;
  margin: 0;
  font-size: 0.88rem;
}

.lib-credits dt { font-weight: 700; color: var(--ink-soft); }
.lib-credits dd { margin: 0; }
.lib-credits a { color: var(--navy-700); }

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

.lib-reader {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 clamp(0.75rem, 4vw, 1.5rem) 3rem;
}

.lib-reader__top {
  position: sticky;
  top: 52px;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.6rem 0 0.5rem;
  background: var(--cream-100);
}

.lib-reader__back {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-reader__back::before { content: "‹ "; color: var(--gold-600); }

.lib-progress {
  flex: 1 1 100%;
  height: 4px;
  border-radius: 99px;
  background: var(--cream-300);
  overflow: hidden;
}

.lib-progress span { display: block; height: 100%; background: var(--gold-400); }

.lib-tools { display: flex; align-items: center; gap: 0.3rem; }
.lib-tools__label { font-size: 0.8rem; color: var(--ink-faint); margin-inline-end: 0.2rem; }

.lib-tools button {
  min-width: 40px;
  min-height: 36px;
  border: 1px solid var(--cream-300);
  border-radius: 8px;
  background: #fff;
  font-weight: 700;
  cursor: pointer;
}

/* Picture books: the picture fills the width, its words sit large beneath it. */
.lib-page { margin: 0.75rem 0 0; }

.lib-page img {
  display: block;
  width: 100%;
  border-radius: 10px;
  background: #fff;
  box-shadow: var(--shadow);
}

.lib-page__text {
  margin-top: 1.1rem;
  padding: 1rem 1.25rem;
  background: #fff;
  border-radius: 10px;
  font-family: var(--book-font);
  font-size: clamp(1.15rem, 3.2vw, 1.45rem);
  line-height: 1.6;
  text-align: left;
}

.lib-page__text p { margin: 0 0 0.5rem; }
.lib-page__text p:last-child { margin-bottom: 0; }

/* Novels: a single comfortable column, sized by the reader. */
.lib-prose {
  margin-top: 1rem;
  font-family: var(--book-font);
  font-size: var(--lib-prose-size);
  line-height: 1.75;
  text-align: left;
  hyphens: auto;
  overflow-wrap: break-word;
}

.lib-prose h1, .lib-prose h2, .lib-prose h3, .lib-prose h4 {
  line-height: 1.3;
  text-align: center;
  margin: 2.2em 0 1em;
  scroll-margin-top: 110px;
}

.lib-prose h1 { font-size: 1.6em; }
.lib-prose h2 { font-size: 1.35em; }
.lib-prose h3 { font-size: 1.15em; }
.lib-prose h4 { font-size: 1em; }
.lib-prose p { margin: 0 0 1em; }
.lib-prose img { display: block; margin: 1.5em auto; border-radius: 6px; }
.lib-prose blockquote { margin: 1em 1.5em; font-style: italic; }
.lib-prose hr { border: 0; border-top: 1px solid var(--cream-300); margin: 2em auto; width: 40%; }
.lib-prose table { margin: 1em auto; border-collapse: collapse; }
.lib-prose td, .lib-prose th { padding: 0.2em 0.6em; vertical-align: top; }
.lib-prose .poem, .lib-prose .stanza { margin: 1em 0 1em 1.5em; }
.lib-prose .center { text-align: center; }
.lib-prose pre { white-space: pre-wrap; font-family: inherit; }

.lib-toc-drop {
  margin-top: 0.5rem;
  border: 1px solid var(--cream-300);
  border-radius: 10px;
  background: #fff;
}

.lib-toc-drop summary { padding: 0.6rem 1rem; cursor: pointer; font-weight: 700; }

.lib-toc-drop ol {
  max-height: 50vh;
  overflow-y: auto;
  margin: 0;
  padding: 0 1rem 0.8rem 2.2rem;
  font-family: var(--book-font);
}

.lib-toc-drop li { padding: 0.15rem 0; }
.lib-toc-drop li.is-current a { font-weight: 700; color: var(--gold-600); }
.lib-toc-drop a { text-decoration: none; }

.lib-pager {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.lib-pager__btn {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 48px;
  padding: 0.55rem 1.2rem;
  border-radius: 999px;
  border: 1px solid var(--cream-300);
  background: #fff;
  font-weight: 700;
  text-decoration: none;
}

.lib-pager__btn--next {
  justify-self: end;
  background: var(--navy-900);
  border-color: var(--navy-900);
  color: var(--gold-300);
}

.lib-pager__count { font-size: 0.85rem; color: var(--ink-faint); white-space: nowrap; }

.lib-end {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 2rem;
  text-align: center;
}

.lib-end__title {
  flex: 1 1 100%;
  margin: 0;
  font-family: var(--book-font);
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--gold-600);
}

/* --- Phones --------------------------------------------------------------------------- */

@media (max-width: 640px) {
  .lib-bar { gap: 0.6rem; }
  .lib-brand span { display: none; }
  .lib-grid { grid-template-columns: repeat(2, 1fr); gap: 1.25rem 0.9rem; }
  .lib-controls { flex-direction: column; align-items: stretch; }
  .lib-search { flex: 0 0 auto; }
  .lib-book { grid-template-columns: 1fr; }
  .lib-book__cover { max-width: 240px; margin: 0 auto; }
  .lib-book__info { text-align: center; }
  .lib-book__info .lib-badges { justify-content: center; }
  .lib-book__info h1 { text-align: center; }
  .lib-btn { width: 100%; }
  .lib-toc ol { columns: 1; }
  .lib-credits dl { grid-template-columns: 1fr; }
  .lib-credits dt { margin-top: 0.4rem; }
  .lib-reader__top { top: 50px; }
  .lib-tools__label { display: none; }
  .lib-pager__btn { padding: 0.55rem 0.9rem; }
  .lib-page__text { padding: 0.85rem 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  @view-transition { navigation: none; }
  .lib-btn, .lib-card .lib-cover { transition: none; }
}
