/* Bookshelf — everything the eye touches.
 *
 * Two rules govern this file:
 *   1. NOTHING is ever drawn between one part of the book and the next. No
 *      borders, no separators, no shadows, no page edges. That thin white line
 *      is the whole reason this app exists.
 *   2. Every typographic value the reader controls is a CSS variable set on
 *      #reader, so changing the font size is one property write and one reflow.
 */

:root {
  --ui-bg: #f2efe9;
  --ui-fg: #23211e;
  --ui-dim: #6f6a62;
  --ui-line: rgba(0, 0, 0, .10);
  --ui-card: #fffdfa;
  --ui-accent: #8a6a3d;
  --ui-shadow: 0 1px 3px rgba(0, 0, 0, .10), 0 6px 18px rgba(0, 0, 0, .06);
  --bar: 56px;

  /* reading surface — overridden per theme below */
  --page-bg: #fbf9f5;
  --page-fg: #1d1b18;
  --page-dim: #7b756c;
  --page-link: #6b5330;
}

:root[data-theme="sepia"] { --page-bg: #f5ecd8; --page-fg: #43382a; --page-dim: #8b7c66; --page-link: #7a5c2e; }
:root[data-theme="grey"]  { --page-bg: #d9d7d1; --page-fg: #26251f; --page-dim: #6d6a62; --page-link: #5b4a2c; }
:root[data-theme="dark"]  { --page-bg: #1b1c1e; --page-fg: #c8c5bf; --page-dim: #85817a; --page-link: #b99a67;
                            --ui-bg: #141517; --ui-fg: #ddd9d3; --ui-dim: #8d8880; --ui-line: rgba(255,255,255,.12);
                            --ui-card: #212327; --ui-accent: #c4a26a; }
:root[data-theme="black"] { --page-bg: #000; --page-fg: #b6b3ae; --page-dim: #77746e; --page-link: #a68a5c;
                            --ui-bg: #000; --ui-fg: #d6d2cc; --ui-dim: #86827b; --ui-line: rgba(255,255,255,.14);
                            --ui-card: #131315; --ui-accent: #c4a26a; }

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  background: var(--ui-bg);
  color: var(--ui-fg);
  font-family: -apple-system, "Segoe UI", Roboto, "Noto Sans", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button, input, select { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }

.hidden { display: none !important; }

/* ====================================================== screens ======== */

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
#shelf { background: var(--ui-bg); }
#reader-screen { background: var(--page-bg); }

/* ====================================================== the shelf ====== */

#shelf-bar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--ui-line); background: var(--ui-bg);
}
#shelf-bar h1 { font-size: 20px; font-weight: 650; margin: 0 auto 0 0; letter-spacing: -.01em; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 14px; border-radius: 10px; font-size: 15px; font-weight: 550;
  background: var(--ui-card); border: 1px solid var(--ui-line); color: var(--ui-fg);
  min-height: 42px;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--ui-accent); border-color: transparent; color: #fff; }
.btn.ghost { background: none; border-color: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; min-height: 34px; }

#search {
  flex: 0 0 auto; width: min(280px, 40vw); padding: 9px 12px; min-height: 42px;
  border-radius: 10px; border: 1px solid var(--ui-line); background: var(--ui-card);
}

#shelf-scroll { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 14px 90px; }

#grid {
  display: grid; gap: 22px 18px;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
}
@media (min-width: 900px) { #grid { grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); } }

.book { display: flex; flex-direction: column; gap: 8px; text-align: left; padding: 0; }
.book:active .cover { transform: scale(.97); }
.cover {
  position: relative; width: 100%; aspect-ratio: 2 / 3; border-radius: 6px; overflow: hidden;
  background: var(--ui-card); box-shadow: var(--ui-shadow);
  transition: transform .12s ease; display: flex;
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .fallback {
  width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center;
  padding: 12px 11px; gap: 6px; background: linear-gradient(160deg, #6f6152, #453c33);
  color: #f4efe6;
}
.cover .fallback .t { font-size: 14px; font-weight: 650; line-height: 1.25; overflow: hidden;
                      display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; }
.cover .fallback .a { font-size: 11px; opacity: .8; }
.cover .badge {
  position: absolute; right: 5px; top: 5px; font-size: 9px; font-weight: 700; letter-spacing: .06em;
  padding: 3px 5px; border-radius: 4px; background: rgba(0, 0, 0, .55); color: #fff;
}
.cover .bar { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0,0,0,.28); }
.cover .bar i { display: block; height: 100%; background: var(--ui-accent); }
.book .t2 { font-size: 13px; font-weight: 600; line-height: 1.3;
            display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book .a2 { font-size: 11.5px; color: var(--ui-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#empty { padding: 40px 20px; max-width: 560px; margin: 0 auto; text-align: center; color: var(--ui-dim); }
#empty h2 { color: var(--ui-fg); font-size: 20px; margin: 0 0 10px; }
#empty p { line-height: 1.6; margin: 0 0 18px; }
#empty .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

#status {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%);
  background: var(--ui-card); border: 1px solid var(--ui-line); box-shadow: var(--ui-shadow);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw;
  display: flex; align-items: center; gap: 10px; z-index: 60;
}
#status .spin { width: 14px; height: 14px; border: 2px solid var(--ui-line); border-top-color: var(--ui-accent);
                border-radius: 50%; animation: spin .8s linear infinite; flex: 0 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ====================================================== the reader ===== */

#reader {
  flex: 1 1 auto; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scroll-behavior: auto;
  background: var(--page-bg);
}

#flow {
  /* the reading column. --page-margin is a percentage of the screen, so it
     stays right whether she is in portrait or landscape on a 14" tablet */
  max-width: var(--measure, 40em);
  margin: 0 auto;
  padding: calc(var(--bar) + 14px) var(--page-margin, 6%) 45vh;
  color: var(--page-fg);
  font-family: var(--book-font, Georgia, "Noto Serif", "Times New Roman", serif);
  font-size: var(--font-size, 20px);
  line-height: var(--line-height, 1.62);
  text-align: var(--align, start);
  hyphens: var(--hyphens, manual);
  -webkit-hyphens: var(--hyphens, manual);
  word-break: normal;
  overflow-wrap: break-word;
}

/* A chapter. Note what is NOT here: no border, no margin collapse tricks, no
   background, no page break marker. One section simply follows the last. */
.bs-ch { display: block; }
.bs-ch-body > *:first-child { margin-top: 0; }

#flow p { margin: 0 0 .85em; text-indent: var(--indent, 0); }
#flow p:first-of-type, #flow h1 + p, #flow h2 + p, #flow h3 + p,
#flow blockquote p:first-child, #flow p.bs-verse { text-indent: 0; }

#flow h1, #flow h2, #flow h3, #flow h4, #flow h5, #flow h6 {
  font-weight: 620; line-height: 1.25; margin: 1.9em 0 .7em;
  text-align: initial; text-indent: 0; page-break-after: avoid;
}
#flow h1 { font-size: 1.5em; } #flow h2 { font-size: 1.32em; }
#flow h3 { font-size: 1.15em; } #flow h4, #flow h5, #flow h6 { font-size: 1.03em; }

#flow img, #flow svg { max-width: 100%; height: auto; display: block; margin: 1.1em auto; }
#flow .bs-page-img { margin: 0 auto; width: 100%; }
#flow blockquote { margin: 1.1em 0 1.1em 1.4em; color: var(--page-dim); font-style: italic; }
#flow hr { border: 0; height: 1px; background: var(--page-dim); opacity: .3; margin: 2em auto; width: 34%; }
#flow a { color: var(--page-link); text-decoration: none; border-bottom: 1px solid currentColor; }
#flow table { border-collapse: collapse; margin: 1.2em auto; max-width: 100%; display: block; overflow-x: auto; }
#flow td, #flow th { border: 1px solid var(--page-dim); padding: .35em .6em; }
#flow ul, #flow ol { padding-left: 1.5em; margin: 0 0 .9em; }
#flow li { margin: 0 0 .3em; }
#flow code, #flow pre { font-family: ui-monospace, "Roboto Mono", monospace; font-size: .92em; }
#flow pre { overflow-x: auto; padding: .7em .9em; background: rgba(128,128,128,.10); border-radius: 6px; }
#flow sup, #flow sub { line-height: 0; font-size: .72em; }
#flow .bs-poem { margin: 1.1em 0 1.1em 1em; }
#flow .bs-verse { margin: 0 0 .18em; }
#flow .bs-missing { color: var(--page-dim); font-style: italic; text-align: center; }
#flow .bs-pdf-page { width: 100%; background: #fff; margin: 0 auto; }
:root[data-theme="dark"] #flow .bs-pdf-page,
:root[data-theme="black"] #flow .bs-pdf-page { filter: invert(1) hue-rotate(180deg); }

/* -------- chrome that appears on a tap, and gets out of the way -------- */

#top, #bottom {
  position: absolute; left: 0; right: 0; z-index: 20;
  background: color-mix(in srgb, var(--ui-bg) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  transition: transform .2s ease, opacity .2s ease;
}
#top { top: 0; display: flex; align-items: center; gap: 4px; padding: 6px 8px;
       padding-top: max(6px, env(safe-area-inset-top)); border-bottom: 1px solid var(--ui-line); }
#bottom { bottom: 0; padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
          border-top: 1px solid var(--ui-line); }
.chrome-off #top { transform: translateY(-105%); opacity: 0; pointer-events: none; }
.chrome-off #bottom { transform: translateY(105%); opacity: 0; pointer-events: none; }

#top .title { flex: 1 1 auto; min-width: 0; font-size: 14px; font-weight: 600;
              overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: center; }
.iconbtn { width: 44px; height: 44px; border-radius: 10px; display: inline-flex;
           align-items: center; justify-content: center; font-size: 17px; flex: 0 0 auto; }
.iconbtn:active { background: var(--ui-line); }

#progress-row { display: flex; align-items: center; gap: 12px; font-size: 12px; color: var(--ui-dim); }
#track { flex: 1 1 auto; height: 4px; border-radius: 2px; background: var(--ui-line); position: relative; }
#track i { position: absolute; inset: 0 auto 0 0; background: var(--ui-accent); border-radius: 2px; }
#chapter-name { max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* a real slider you can grab on a touch screen */
#track-input { position: absolute; inset: -14px 0; width: 100%; opacity: 0; margin: 0; }

/* -------- the panels: contents, and Aa -------------------------------- */

.sheet {
  position: absolute; z-index: 40; background: var(--ui-card); color: var(--ui-fg);
  box-shadow: var(--ui-shadow); border: 1px solid var(--ui-line);
  transition: transform .22s cubic-bezier(.2,.8,.3,1), opacity .18s ease;
}
#toc {
  top: 0; bottom: 0; left: 0; width: min(400px, 84vw); border-radius: 0 14px 14px 0;
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
#toc.closed { transform: translateX(-102%); opacity: 0; pointer-events: none; }
#toc h3 { margin: 0; padding: 16px 18px 10px; font-size: 16px; }
#toc-list { flex: 1 1 auto; overflow-y: auto; padding: 0 8px 20px; }
#toc-list button {
  display: block; width: 100%; text-align: left; padding: 11px 12px; border-radius: 9px;
  font-size: 14.5px; line-height: 1.35; color: var(--ui-fg);
}
#toc-list button:active { background: var(--ui-line); }
#toc-list button.d1 { padding-left: 28px; font-size: 13.5px; color: var(--ui-dim); }
#toc-list button.d2 { padding-left: 44px; font-size: 13px; color: var(--ui-dim); }
#toc-list button.here { background: var(--ui-line); font-weight: 650; }

#aa {
  left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom) + 8px);
  width: min(460px, 94vw); border-radius: 16px; padding: 14px 16px 18px;
}
#aa.closed { transform: translate(-50%, 120%); opacity: 0; pointer-events: none; }
.row { display: flex; align-items: center; gap: 10px; margin: 0 0 14px; }
.row:last-child { margin-bottom: 0; }
.row .lab { flex: 0 0 78px; font-size: 12.5px; color: var(--ui-dim); }
.row input[type=range] { flex: 1 1 auto; accent-color: var(--ui-accent); height: 34px; }
.seg { display: flex; gap: 6px; flex: 1 1 auto; flex-wrap: wrap; }
.seg button {
  flex: 1 1 auto; padding: 9px 6px; border-radius: 9px; font-size: 13.5px; min-height: 40px;
  border: 1px solid var(--ui-line); background: var(--ui-bg); color: var(--ui-fg);
}
.seg button[aria-pressed="true"] { background: var(--ui-accent); color: #fff; border-color: transparent; }
.swatch { width: 40px; height: 40px; border-radius: 9px; border: 2px solid var(--ui-line); flex: 0 0 auto; }
.swatch[aria-pressed="true"] { border-color: var(--ui-accent); box-shadow: 0 0 0 2px var(--ui-accent) inset; }
#size-out { font-variant-numeric: tabular-nums; font-size: 12.5px; color: var(--ui-dim); width: 34px; text-align: right; }

#scrim { position: absolute; inset: 0; z-index: 30; background: rgba(0,0,0,.28); }
#scrim.closed { opacity: 0; pointer-events: none; }
#scrim { transition: opacity .18s ease; }

/* Screen dimmer for reading in the dark below the tablet's own minimum. */
#dim { position: fixed; inset: 0; z-index: 90; background: #000; pointer-events: none; opacity: 0; }

/* the tap zone that shows and hides the chrome */
#tapzone { position: absolute; inset: 0; z-index: 10; }

#opening {
  position: absolute; inset: 0; z-index: 70; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; background: var(--page-bg);
  color: var(--page-dim); font-size: 14px;
}
#opening .spin { width: 22px; height: 22px; border: 2px solid var(--ui-line);
                 border-top-color: var(--ui-accent); border-radius: 50%; animation: spin .8s linear infinite; }

dialog {
  border: 1px solid var(--ui-line); border-radius: 14px; background: var(--ui-card); color: var(--ui-fg);
  padding: 18px 20px; max-width: min(460px, 92vw); box-shadow: var(--ui-shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog h3 { margin: 0 0 10px; font-size: 17px; }
dialog p { margin: 0 0 14px; line-height: 1.55; font-size: 14.5px; color: var(--ui-dim); }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
dialog code { font-size: 12.5px; background: var(--ui-bg); padding: 1px 5px; border-radius: 4px; }

/* Shelf only. Fixed-position, it sat on top of the reader's progress figure. */
#buildstamp { position: absolute; right: 8px; bottom: 6px; font-size: 10px; color: var(--ui-dim);
              opacity: .55; z-index: 5; pointer-events: none; }
