/* Endpaper polish: small layout fixes and gentle motion on top of ui.css.
   Uses the shell's own tokens (green accent, serif display, DM Sans UI). */

@media (max-width:700px), (max-width:900px) and (pointer:coarse) {
  /* More: account card and inset grouped lists with tinted icon tiles. */
  body:not(.reader-active) .more-account { display: flex; align-items: center; gap: 14px; margin: 0 0 22px; padding: 14px 16px; border: 1px solid var(--line); border-radius: 16px; background: var(--paper-card); }
  body:not(.reader-active) .more-account-avatar { display: grid; place-items: center; width: 52px; height: 52px; border-radius: 50%; background: var(--gold-bright); color: var(--on-accent, #fff); font: 500 22px var(--font-ui); flex: none; }
  body:not(.reader-active) .more-account-identity strong { display: block; font: 600 19px/1.25 var(--font-ui); color: var(--ink); }
  body:not(.reader-active) .more-account-identity span { color: var(--ink-soft); font: 400 14px var(--font-ui); }
  body:not(.reader-active) .mobile-more-list { row-gap: 28px; }
  body:not(.reader-active) .more-section { margin: 0; }
  body:not(.reader-active) .more-section h2 { margin: 0 0 8px 16px; padding: 0; border: 0; color: var(--ink-soft); font: 500 12.5px/1.3 var(--font-ui); text-transform: uppercase; letter-spacing: 0.06em; }
  body:not(.reader-active) .more-section-actions { overflow: hidden; border: 1px solid var(--line); border-radius: 16px; background: var(--paper-card); }
  body:not(.reader-active) .more-section-actions > button { display: flex; align-items: center; gap: 14px; width: 100%; min-height: 50px; margin: 0; padding: 0 16px; border: 0 !important; border-radius: 0; box-shadow: none !important; background: transparent; color: var(--ink); font: 400 16px var(--font-ui); text-align: left; position: relative; }
  body:not(.reader-active) .more-section-actions > button + button::before { content: ''; position: absolute; top: 0; left: 59px; right: 0; border-top: 1px solid var(--line); }
  body:not(.reader-active) .more-section-actions > button > .endpaper-icon:first-child { width: 29px; height: 29px; padding: 6px; border-radius: 8px; background: var(--gold-bright); color: var(--on-accent, #fff); flex: none; }
  body:not(.reader-active) .more-section-actions > button > .endpaper-icon:last-child { width: 15px; height: 15px; margin-left: auto; color: var(--ink-soft); opacity: .7; }
  body:not(.reader-active) .more-section-actions > button:active { background: color-mix(in srgb, var(--ink) 7%, transparent); }
  body:not(.reader-active) .more-action-label { flex: 1; }

  /* The reading calendar fits the screen, so the newest week is always visible. */
  body:not(.reader-active) .reading-heatmap { grid-template-rows: repeat(7, auto); grid-auto-columns: minmax(0, 1fr); overflow: visible; }
  body:not(.reader-active) .reading-heatmap .heat-cell { width: auto; height: auto; aspect-ratio: 1; }

  /* Search fields: a quiet round clear button instead of WebKit's default glyph. */
  body:not(.reader-active) .mobile-search-input::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; width: 17px; height: 17px; cursor: pointer; opacity: .55; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='11' fill='%23808a82'/%3E%3Cpath d='m8 8 8 8m0-8-8 8' stroke='white' stroke-width='2.2' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat center / contain; }

  /* Covers and rows give way slightly under a finger. */
  body:not(.reader-active) :is(.mobile-book, .mobile-continue):active .mobile-cover { transform: scale(.97); }
  body:not(.reader-active) .mobile-secondary-action:active { background: color-mix(in srgb, var(--ink) 6%, transparent); }
}


/* ---------- Book alignment ---------- */
/* Book cards are buttons, and WebKit centres a button's contents vertically, so cards with
   shorter text pushed their covers down. Stack them from the top so covers line up in rows. */
body:not(.reader-active) .mobile-book.mobile-grid { display: flex; flex-direction: column; justify-content: flex-start; align-items: stretch; text-align: left; }
body:not(.reader-active) .mobile-books-grid { align-items: start; }
body:not(.reader-active) .mobile-rail { align-items: flex-start; }
/* Desktop shelf: ratings and progress sit at the foot of every card, level across a row
   even when one card carries a series line. */
body:not(.reader-active) #shelf .book-card { height: 100%; }
body:not(.reader-active) #shelf .book-meta-under { display: flex; flex-direction: column; flex: 1; }
body:not(.reader-active) #shelf .book-meta-under .shelf-rating-widget { margin-top: auto; }

/* ---------- Motion ---------- */
@media (prefers-reduced-motion: no-preference) {
  /* A page arrives with a short fade and rise; its first blocks follow one after another.
     .route-enter is set by renderMobileShell only when the route changes, not on every re-render. */
  body:not(.reader-active) #mobile-content.route-enter > * { animation: page-rise 280ms var(--motion-ease, ease-out) both; }
  body:not(.reader-active) #mobile-content.route-enter > :nth-child(2) { animation-delay: 30ms; }
  body:not(.reader-active) #mobile-content.route-enter > :nth-child(3) { animation-delay: 60ms; }
  body:not(.reader-active) #mobile-content.route-enter > :nth-child(4) { animation-delay: 90ms; }
  body:not(.reader-active) #mobile-content.route-enter > :nth-child(5) { animation-delay: 120ms; }
  body:not(.reader-active) #mobile-content.route-enter > :nth-child(n+6) { animation-delay: 150ms; }

  /* Progress and goal bars fill in from the left when a page opens. */
  body:not(.reader-active) #mobile-content.route-enter :is(.goal-meter-fill, .mobile-progress-track span) { transform-origin: left center; animation: fill-grow 650ms 120ms var(--motion-ease, ease-out) both; }

  /* The tab you switch to gives a small lift. */
  body:not(.reader-active) #mobile-tabbar button.active svg { animation: tab-lift 300ms var(--motion-ease, ease-out); }

  /* A star you set pops once. */
  body:not(.reader-active) :is(.mobile-star, .star-btn).filled svg { animation: star-pop 260ms var(--motion-ease, ease-out); }


  /* Desktop pages arrive the same way when you switch section. */
  body:not(.reader-active) #shelf-view.route-enter > :not(#desktop-topbar) { animation: page-rise 280ms var(--motion-ease, ease-out) both; }

  /* Covers fade in as their image arrives instead of popping. */
  body:not(.reader-active) :is(.cover-img, .mobile-cover img).cover-arrived { animation: cover-in 320ms ease-out; }

  /* Press feedback for what ui.css leaves out (it already covers phone books, pills and primary buttons). */
  body:not(.reader-active) :is(.book-card, .smart-book):active { transform: scale(.985); }
  body:not(.reader-active) :is(.mobile-secondary-action, .mobile-detail-actions button):active { transform: scale(.97); }

  /* The "…" menu on a desktop card turns a quarter when hovered. */
  body:not(.reader-active) .book-menu-btn svg { transition: transform var(--motion-feedback, 140ms) var(--motion-ease, ease-out); }
  body:not(.reader-active) .book-menu-btn:hover svg { transform: rotate(90deg); }
}

@keyframes page-rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes fill-grow { from { transform: scaleX(0); } to { transform: scaleX(1); } }
@keyframes tab-lift { 0% { transform: translateY(0); } 45% { transform: translateY(-3px) scale(1.06); } 100% { transform: none; } }
@keyframes star-pop { 0% { transform: scale(.8); } 60% { transform: scale(1.18); } 100% { transform: none; } }
@keyframes cover-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Reader: Themes & Settings sheet, laid out like Apple Books ---------- */
/* Round close on the left beside a bold title (no banded header), a sans A/A pill,
   no section label, rounder translucent sheet, filled Customise pill. */
@media (max-width:700px), (max-width:900px) and (pointer:coarse) {
  body.reader-active #settings-drawer { left: 10px !important; right: 10px !important; bottom: calc(8px + min(env(safe-area-inset-bottom), 34px)); padding: 16px 16px 20px; border: 1px solid color-mix(in srgb, var(--reader-ink) 10%, transparent); border-radius: 34px; background: color-mix(in srgb, color-mix(in srgb, var(--reader-page-bg) 80%, var(--reader-ink)) 96%, transparent); box-shadow: 0 18px 50px rgba(0, 0, 0, .35); -webkit-backdrop-filter: blur(30px) saturate(160%); backdrop-filter: blur(30px) saturate(160%); }
  body.reader-active #settings-drawer .drawer-title { position: static; flex-direction: row-reverse; justify-content: flex-end; gap: 12px; min-height: 44px; margin: 0 0 16px; padding: 0; background: transparent; color: var(--reader-ink); font: 700 17px/1.2 var(--font-ui); }
  body.reader-active #settings-drawer .drawer-title button { width: 42px; height: 42px; border: 0; background: color-mix(in srgb, var(--reader-ink) 14%, transparent); color: var(--reader-ink); }
  body.reader-active #settings-drawer .drawer-title button svg { width: 18px; height: 18px; }
  body.reader-active #settings-drawer .mobile-reading-quick { gap: 0; margin: 0 0 18px; background: color-mix(in srgb, var(--reader-ink) 13%, transparent); }
  body.reader-active #settings-drawer .mobile-reading-quick button { min-height: 50px; background: transparent; color: var(--reader-ink); font: 600 15px var(--font-ui); }
  body.reader-active #settings-drawer .mobile-reading-quick button + button { border-left: 1px solid color-mix(in srgb, var(--reader-ink) 18%, transparent); font-size: 22px; }
  body.reader-active #settings-drawer .mobile-reading-quick button:active { background: color-mix(in srgb, var(--reader-ink) 10%, transparent); }
  body.reader-active #settings-drawer .mobile-theme-group > .setting-label { display: none; }
  body.reader-active #settings-drawer .theme-swatches { gap: 10px; }
  body.reader-active #settings-drawer .theme-swatch { height: 96px; border: 3px solid transparent; border-radius: 18px; outline: 0; box-shadow: none; }
  body.reader-active #settings-drawer .theme-swatch.active { border-color: var(--reader-ink); outline: 0; }
  body.reader-active #settings-drawer #mobile-reading-customize { min-height: 50px; border: 0; background: color-mix(in srgb, var(--reader-ink) 13%, transparent); font: 600 16px var(--font-ui); }
}

@media (prefers-reduced-motion: no-preference) {
  /* The sheet rises into place, and a theme you pick settles with a small press. */
  body.reader-active #settings-drawer.open { animation: sheet-rise 320ms var(--motion-ease, cubic-bezier(.16, 1, .3, 1)); }
  body.reader-active #settings-drawer .theme-swatch { transition: transform 140ms ease, border-color 160ms ease; }
  body.reader-active #settings-drawer .theme-swatch:active { transform: scale(.96); }
}
@keyframes sheet-rise { from { opacity: .4; transform: translateY(24px); } to { opacity: 1; transform: none; } }

/* The status bar is translucent (black-translucent), so the reading page runs up behind the clock
   the way Books does. Keep the text itself below the clock; iOS web apps cannot hide the status bar. */
@media (max-width:700px), (max-width:900px) and (pointer:coarse) {
  body.reader-active #reader-view { background: var(--reader-page-bg); }
  body.reader-active #viewer-wrap { top: min(env(safe-area-inset-top), 60px); }
  body.reader-active #reader-view.scrolled #viewer-wrap { top: min(env(safe-area-inset-top), 60px); }
}
