/* ------------------------------------------------------------------------
 * Reflow reader: themes and the appearance panel (Shelf 2.0, handoff 11).
 * A theme is a set of the app's colour tokens re-declared on the reader's
 * root, so the toolbar, panel and pages all follow it.  --reader-bg is the
 * page; body-text contrast against it is measured in docs/reader2_contract.md.
 * ---------------------------------------------------------------------- */

.text-reader-view[data-reader-theme="paper"] {
  color-scheme: light;
  --reader-bg: #f4eee2;
  --color-paper: #ece4d4;
  --color-paper-deep: #e6dcc9;
  --color-surface: #f4eee2;
  --color-surface-raised: #faf5ea;
  --color-ink: #2b2521;
  --color-ink-soft: #4a423b;
  --color-muted: #6a5f55;
  --color-faint: #6a5f55;
  --color-line: #d4c8b3;
  --color-line-soft: #e0d6c3;
  --color-accent: #82432f;
  --color-accent-strong: #6a3524;
  --color-accent-soft: #ecd9cc;
  --color-on-accent: #fffaf6;
  --color-caution: #8a5c0e;
  --color-warning: #9a352f;
}

.text-reader-view[data-reader-theme="sepia"] {
  color-scheme: light;
  --reader-bg: #efe0c2;
  --color-paper: #e6d4b0;
  --color-paper-deep: #dfcba3;
  --color-surface: #efe0c2;
  --color-surface-raised: #f5e9d0;
  --color-ink: #3a2d1d;
  --color-ink-soft: #58472f;
  --color-muted: #5e4c32;
  --color-faint: #5e4c32;
  --color-line: #cdb88f;
  --color-line-soft: #dccba6;
  --color-accent: #7a3f22;
  --color-accent-strong: #62311a;
  --color-accent-soft: #e6cfb2;
  --color-on-accent: #fff8ee;
  --color-caution: #85560c;
  --color-warning: #8a2f26;
}

.text-reader-view[data-reader-theme="night"] {
  color-scheme: dark;
  --reader-bg: #1d1c21;
  --color-paper: #17161a;
  --color-paper-deep: #131216;
  --color-surface: #1d1c21;
  --color-surface-raised: #26242b;
  --color-ink: #d9d3c7;
  --color-ink-soft: #c3bdb1;
  --color-muted: #a39c91;
  --color-faint: #a39c91;
  --color-line: #46414a;
  --color-line-soft: #34303a;
  --color-accent: #e0a088;
  --color-accent-strong: #eab6a2;
  --color-accent-soft: #452d28;
  --color-on-accent: #211512;
  --color-caution: #e0b762;
  --color-warning: #ed8b82;
  --color-backdrop: rgb(4 3 6 / 68%);
}

.text-reader-view[data-reader-theme="oled"] {
  color-scheme: dark;
  --reader-bg: #000;
  --color-paper: #000;
  --color-paper-deep: #000;
  --color-surface: #000;
  --color-surface-raised: #0e0e0e;
  --color-ink: #cdc7ba;
  --color-ink-soft: #b9b3a7;
  --color-muted: #9a948a;
  --color-faint: #9a948a;
  --color-line: #2f2c33;
  --color-line-soft: #1f1d22;
  --color-accent: #dc9c84;
  --color-accent-strong: #e8b19c;
  --color-accent-soft: #3a2621;
  --color-on-accent: #1a100d;
  --color-caution: #e0b762;
  --color-warning: #ed8b82;
  --color-backdrop: rgb(0 0 0 / 78%);
}

/* The page: one fixed layer behind the columns, so a texture never repeats per column or page. */
.text-reader-view {
  --color-action: var(--color-ink);
  --color-action-hover: var(--color-ink-soft);
  --color-on-action: var(--reader-bg, var(--color-surface));
}

.reader-paper {
  position: absolute;
  inset: 4.75rem 0 0;
  z-index: 0;
  background-color: var(--reader-bg, var(--color-surface));
  pointer-events: none;
}

.text-reader-view[data-reader-theme="paper"] .reader-paper {
  background-image:
    radial-gradient(ellipse at 50% 45%, transparent 58%, rgb(96 70 40 / 11%) 100%),
    var(--paper-grain, none);
  background-size: 100% 100%, 192px 192px;
}

.text-reader-view[data-reader-theme="sepia"] .reader-paper {
  background-image: radial-gradient(ellipse at 50% 45%, transparent 62%, rgb(110 70 30 / 9%) 100%);
}

/* Folio and link colours already use tokens; the engine's own chrome needs nothing else. */

/* --- appearance panel ---------------------------------------------------- */

.reader-prefs-panel {
  position: absolute;
  z-index: 40;
  top: 4.9rem;
  right: var(--space-4);
  display: grid;
  gap: var(--space-4);
  width: min(23rem, calc(100% - 2 * var(--space-4)));
  max-height: calc(100% - 6rem);
  padding: var(--space-4);
  overflow-y: auto;
  color: var(--color-ink);
  background: var(--color-surface-raised);
  border: 1px solid var(--color-line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sheet);
  font-family: var(--font-ui);
}

.reader-prefs-panel[hidden] {
  display: none;
}

.rp-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rp-close {
  width: 2.75rem;
  height: 2.75rem;
  color: var(--color-muted);
  background: transparent;
  border: 0;
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-size: 1.4rem;
  line-height: 1;
}

.rp-close:hover {
  color: var(--color-ink);
  background: var(--color-paper-deep);
}

.rp-group {
  display: grid;
  gap: var(--space-2);
}

.rp-font {
  display: grid;
  grid-template-columns: 2.4rem 1fr auto;
  gap: var(--space-2);
  align-items: baseline;
  padding: var(--space-2) var(--space-3);
  color: var(--color-ink);
  text-align: left;
  background: transparent;
  border: 1px solid var(--color-line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.rp-font-sample {
  font-size: 1.4rem;
}

.rp-font-name {
  font-size: var(--text-md);
}

.rp-font small {
  color: var(--color-muted);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
}

.rp-themes {
  grid-template-columns: repeat(4, 1fr);
}

.rp-theme {
  display: grid;
  gap: var(--space-1);
  justify-items: center;
  padding: var(--space-2) 0;
  color: var(--color-ink);
  background: transparent;
  border: 1px solid var(--color-line);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-xs);
}

.rp-swatch {
  display: grid;
  place-items: center;
  width: 2.6rem;
  height: 2.2rem;
  border: 1px solid var(--color-line);
  border-radius: var(--radius-sm);
  font: 600 1.1rem var(--font-reading);
}

.rp-theme[data-theme="paper"] .rp-swatch { color: #2b2521; background: #f4eee2; }
.rp-theme[data-theme="sepia"] .rp-swatch { color: #3a2d1d; background: #efe0c2; }
.rp-theme[data-theme="night"] .rp-swatch { color: #d9d3c7; background: #1d1c21; }
.rp-theme[data-theme="oled"] .rp-swatch { color: #cdc7ba; background: #000; }

.rp-font.is-active,
.rp-theme.is-active {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 1px var(--color-accent);
}

.rp-font:focus-visible,
.rp-theme:focus-visible,
.rp-close:focus-visible,
.rp-link:focus-visible,
.rp-row input:focus-visible,
.rp-check input:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.rp-row {
  display: grid;
  grid-template-columns: 6.2rem 1fr 3rem;
  gap: var(--space-3);
  align-items: center;
}

.rp-label {
  font-size: var(--text-sm);
}

.rp-row input[type="range"] {
  width: 100%;
  accent-color: var(--color-accent);
}

.rp-value {
  color: var(--color-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.rp-checks {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-5);
}

.rp-check {
  display: inline-flex;
  gap: var(--space-2);
  align-items: center;
  font-size: var(--text-sm);
  cursor: pointer;
}

.rp-check input {
  accent-color: var(--color-accent);
}

.rp-note {
  margin: 0;
  color: var(--color-muted);
  font-size: var(--text-xs);
  line-height: 1.4;
}

.rp-link {
  justify-self: start;
  padding: 0;
  color: var(--color-accent);
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: var(--text-sm);
  text-decoration: underline;
}

@media (max-width: 700px) {
  /* A bottom sheet on a phone. */
  .reader-prefs-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: 58%;
    gap: var(--space-3);
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }

  /* Fonts two to a row and without the caption, so the page stays visible above the sheet. */
  .rp-group:not(.rp-themes) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .rp-font {
    grid-template-columns: 2rem minmax(0, 1fr);
  }

  .rp-font-name {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .rp-font small {
    display: none;
  }
}

/* ---------------------------------------------------------------------------
   Notes and highlights on reflow text (handoff 12).  Highlights are drawn
   with the CSS Custom Highlight API (text-engine.js drawHighlights): no
   element in the text, so they never move a line.  Colours are the five
   annotation colours, translucent so every theme's ink stays readable.
   ------------------------------------------------------------------------- */

::highlight(shelf-hl-amber) { background-color: rgb(214 154 38 / 0.34); }
::highlight(shelf-hl-rose) { background-color: rgb(207 102 121 / 0.32); }
::highlight(shelf-hl-sky) { background-color: rgb(77 155 201 / 0.32); }
::highlight(shelf-hl-mint) { background-color: rgb(71 167 126 / 0.32); }
::highlight(shelf-hl-violet) { background-color: rgb(132 109 204 / 0.32); }

.text-reader-view .tx-selection-bar {
  position: absolute;
  z-index: 44;
  display: flex;
  gap: var(--space-2, 0.5rem);
  padding: 0.35rem;
  border: 1px solid var(--color-line, rgb(0 0 0 / 0.12));
  border-radius: 0.6rem;
  background: var(--color-surface, #fff);
  box-shadow: 0 6px 22px rgb(0 0 0 / 0.18);
}

.text-reader-view .tx-selection-bar[hidden] { display: none; }

.text-reader-view .tx-selection-bar .button {
  min-height: 2rem;
  padding: 0.2rem 0.75rem;
}

.reader-note-edit {
  padding: 0;
  color: var(--color-accent-strong, inherit);
  background: transparent;
  cursor: pointer;
  font-size: var(--text-xs, 0.8rem);
}

.reader-note-elsewhere {
  justify-self: start;
  font-size: var(--text-xs, 0.8rem);
}

/* Panels and the composer inherit ``color`` as a computed value from the app's
   own theme; give them the reader theme's ink so a dark app theme does not
   leave pale text on a Paper page. */
.text-reader-view .reader-notes-panel,
.text-reader-view .annotation-composer,
.text-reader-view .reader-ask-panel {
  color: var(--color-ink);
}

.text-reader-view .reader-note-edit { margin-right: var(--space-3, 0.75rem); }

/* ---------------------------------------------------------------------------
   Touch and phone width (handoff 13).  Swipes are read by touch-paging.js; the
   stage lets the browser keep vertical pans and pinch-zoom and leaves
   horizontal movement to the page.  Safe-area insets keep the chrome and the
   folio clear of a notch, the home indicator and a landscape cut-out.
   ------------------------------------------------------------------------- */

.text-reader-view .tx-stage {
  touch-action: pan-y pinch-zoom;
  -webkit-tap-highlight-color: transparent;
}

.text-reader-view .tx-frame {
  will-change: transform;
}

/* The toolbar takes its text colour from the reader theme, not from the app's
   (a dark app theme would otherwise leave a pale title on a Paper toolbar). */
.text-reader-view .reader-toolbar {
  color: var(--color-ink);
}

/* The tools wrapper is invisible on desktop: its children lay out as before (handoff 31). */
.text-reader-view .reader-tools { display: contents; }
.text-reader-view .reader-controls > .reader-layout-toggle { order: 1; }
.text-reader-view .reader-tools > .reader-appearance-button { order: 2; }

.text-reader-view .reader-bookmark-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

/* Ask close button: shown at every width. */
.ask-close {
  min-width: 2.75rem;
  min-height: 2.75rem;
  color: var(--color-muted);
  background: transparent;
  border: 0;
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-size: 1.5rem;
  line-height: 1;
}

.ask-close:hover {
  color: var(--color-ink);
  background: var(--color-paper-deep);
}

.ask-close:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}

.text-reader-view .text-viewer {
  bottom: env(safe-area-inset-bottom, 0px);
}

/* The sheet's grid must not grow wider than the sheet (the slider values were clipped at 375px). */
.reader-prefs-panel {
  grid-template-columns: minmax(0, 1fr);
}

.rp-row {
  grid-template-columns: 6.2rem minmax(0, 1fr) 3rem;
}

@media (max-width: 700px) {
  .reader-prefs-panel {
    padding-bottom: max(var(--space-4), env(safe-area-inset-bottom, 0px));
    padding-inline: max(var(--space-4), env(safe-area-inset-left, 0px)) max(var(--space-4), env(safe-area-inset-right, 0px));
  }

  .rp-row {
    grid-template-columns: 5.4rem minmax(0, 1fr) 2.75rem;
    gap: var(--space-2);
  }

  /* Dialogs and sheets over the page: comfortable padding, clear of the home indicator. */
  .text-reader-view .annotation-form {
    padding: var(--space-5) var(--space-5) max(var(--space-5), env(safe-area-inset-bottom, 0px));
  }

  .text-reader-view .annotation-composer {
    max-height: calc(100dvh - 2 * var(--space-4));
    overflow-y: auto;
  }

  /* shell.css (handoff 29) gives `main` side gutters on a phone; the reader is full-bleed
     (otherwise it sits 20px to the right and the page grows 20px wider than the screen). */
  body.reader-open main {
    margin-inline: 0;
  }

  /* One 44px top row: close, title, Reflow | Original.  The tools move to a bottom bar (handoff 31).
     No backdrop-filter or transform here: either would make this the containing block of the fixed bottom bar. */
  .text-reader-view .reader-toolbar {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.5rem;
    min-height: 0;
    padding: max(env(safe-area-inset-top, 0px), 0.375rem) max(env(safe-area-inset-right, 0px), 0.5rem) 0.375rem max(env(safe-area-inset-left, 0px), 0.5rem);
    background: var(--color-surface-raised);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    box-shadow: none;
    transform: none;
    transition: opacity var(--dur-slow) var(--ease-out);
  }

  .text-reader-view .reader-controls {
    display: contents;
  }

  .text-reader-view .reader-book-identity {
    order: 0;
    flex: 1 1 0;
    min-width: 0;
  }

  .text-reader-view .reader-book-identity > div {
    display: block;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .text-reader-view .reader-book-identity strong {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: var(--text-sm, 0.875rem);
  }

  .text-reader-view .reader-close {
    min-width: 2.75rem;
    min-height: 2.75rem;
  }

  .text-reader-view .reader-close > span:last-child {
    display: none;
  }

  .text-reader-view .reader-layout-toggle {
    order: 1;
    display: inline-flex;
  }

  .text-reader-view .reader-toolbar::after {
    content: none;
  }

  /* The bottom bar: contents (takes the free width), Ask, Note, bookmark, Notes, Aa. */
  .text-reader-view .reader-tools {
    position: fixed;
    z-index: 1;
    inset: auto 0 0 0;
    display: flex;
    align-items: center;
    gap: var(--space-1);
    padding: 0.375rem max(env(safe-area-inset-right, 0px), 0.5rem) max(env(safe-area-inset-bottom, 0px), 0.375rem) max(env(safe-area-inset-left, 0px), 0.5rem);
    background: var(--color-surface-raised);
    border-top: 1px solid var(--color-line);
  }

  .text-reader-view .reader-chapter {
    display: block;
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
    max-width: none;
    min-height: 2.75rem;
    padding: 0 1.375rem 0 0.5rem;
    font-size: var(--text-sm, 0.875rem);
    text-overflow: ellipsis;
    /* Own chevron (the native one eats the label's width): currentColor, so every theme works. */
    appearance: none;
    -webkit-appearance: none;
    background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
    background-position: calc(100% - 0.85rem) 50%, calc(100% - 0.55rem) 50%;
    background-size: 0.3rem 0.3rem;
    background-repeat: no-repeat;
  }

  .text-reader-view .reader-tools > .reader-icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.75rem;
    min-height: 2.75rem;
    padding-inline: 0.25rem;
    color: var(--color-ink-soft);
    font-size: var(--text-sm, 0.875rem);
  }

  .text-reader-view .reader-tools > .reader-icon-button:active:not(.is-active) {
    background: color-mix(in srgb, var(--color-ink) 10%, transparent);
  }

  .text-reader-view .reader-tools > .reader-page-button,
  .text-reader-view .reader-tools > .reader-font-button,
  .text-reader-view .reader-tools > .reader-font-value,
  .text-reader-view .reader-tools > .reader-epub-location {
    display: none;
  }

  .text-reader-view .reader-layout-toggle button {
    min-height: 2.5rem;
  }

  /* The viewer keeps its size when the chrome is hidden (a tap must never move a page break). */
  .text-reader-view .text-viewer {
    top: calc(max(env(safe-area-inset-top, 0px), 0.375rem) + 3.125rem);
    right: max(calc(var(--reader-margin, 0) * 100%), env(safe-area-inset-right, 0px));
    left: max(calc(var(--reader-margin, 0) * 100%), env(safe-area-inset-left, 0px));
  }

  .text-reader-view .reader-paper {
    inset: 0;
  }

  .text-reader-view.reader-toolbar-hidden .reader-toolbar {
    transform: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur-slow) var(--ease-out), visibility 0s linear var(--dur-slow);
  }

  /* Pills sit above the bottom bar, and drop beside the page number when the bars hide. */
  .text-reader-view .reader-feed-pill,
  .text-reader-view .reader-context-pill {
    top: auto;
    min-height: 2.5rem;
    transform: none;
    bottom: calc(max(env(safe-area-inset-bottom, 0px), 0.375rem) + 3.625rem);
    transition: bottom var(--dur-slow) var(--ease-out);
  }

  .text-reader-view .reader-feed-pill {
    left: max(var(--space-4), env(safe-area-inset-left, 0px));
  }

  .text-reader-view .reader-context-pill {
    right: max(var(--space-4), env(safe-area-inset-right, 0px));
    left: auto;
  }

  .text-reader-view.reader-toolbar-hidden .reader-feed-pill,
  .text-reader-view.reader-toolbar-hidden .reader-context-pill {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 0.25rem);
  }

  /* Notes and Ask rise from the bottom over the bar. */
  .text-reader-view .reader-notes-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: 78%;
    padding: var(--space-5) var(--space-5) max(var(--space-5), env(safe-area-inset-bottom, 0px));
    border-left: 0;
    border-top: 1px solid var(--color-line);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-sheet);
  }

  .text-reader-view .reader-ask-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: 86dvh;
    padding-bottom: max(var(--space-5), env(safe-area-inset-bottom, 0px));
    border-width: 1px 0 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }

  .text-reader-view .ask-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: var(--space-2) var(--space-3);
  }

  .text-reader-view .ask-header select {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    max-width: none;
    min-height: 2.75rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .text-reader-view .reader-toolbar {
    transition: none;
  }
}

/* Page turn: slide or curl (handoff 24). */
.rp-turns {
  grid-template-columns: 1fr 1fr;
}

.rp-turn {
  padding: var(--space-2) var(--space-3);
}
