/* 书房 — a quiet reader in the spirit of Apple Books */

:root {
  --ui-font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Segoe UI", system-ui, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Songti SC", "Noto Serif SC", "Source Han Serif SC", SimSun, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Segoe UI", system-ui, sans-serif;
  --tint: #007aff;
  --radius: 12px;
  --top-h: 48px;
  --bottom-h: 104px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* reading themes; the UI chrome follows them */
body[data-theme="white"] { --bg: #ffffff; --fg: #1c1c1e; --sub: #8e8e93; --hair: rgba(60, 60, 67, 0.16); --fill: rgba(118, 118, 128, 0.12); --hl: rgba(255, 204, 0, 0.32); --card: #f2f2f7; }
body[data-theme="sepia"] { --bg: #f8f1e3; --fg: #4f3b26; --sub: #9c8a73; --hair: rgba(79, 59, 38, 0.16); --fill: rgba(79, 59, 38, 0.08); --hl: rgba(214, 156, 66, 0.30); --card: #efe6d3; }
body[data-theme="gray"]  { --bg: #4a4a4d; --fg: #ececec; --sub: #a5a5aa; --hair: rgba(255, 255, 255, 0.14); --fill: rgba(255, 255, 255, 0.1); --hl: rgba(255, 214, 10, 0.26); --card: #5a5a5e; --tint: #0a84ff; }
body[data-theme="night"] { --bg: #000000; --fg: #c7c7cc; --sub: #8e8e93; --hair: rgba(255, 255, 255, 0.14); --fill: rgba(255, 255, 255, 0.1); --hl: rgba(255, 214, 10, 0.24); --card: #1c1c1e; --tint: #0a84ff; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
/* iOS home-screen apps paint the status-bar strip with the <html> background; left transparent it turns black
   after the file picker closes. app.js keeps this in step with the reading theme. */
html { background: #ffffff; }
body { background: var(--bg); color: var(--fg); font-family: var(--ui-font); -webkit-font-smoothing: antialiased; overflow: hidden; transition: background 0.25s ease, color 0.25s ease; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.view { position: fixed; inset: 0; }

.icon-btn { width: 36px; height: 36px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: var(--fg); opacity: 0.85; transition: background 0.15s ease, opacity 0.15s ease; }
.icon-btn:hover { background: var(--fill); opacity: 1; }
.text-btn { font-family: var(--serif); font-size: 17px; font-weight: 500; }
.pill-btn { height: 30px; padding: 0 12px; border-radius: 999px; background: var(--fill); font-size: 13px; font-weight: 600; letter-spacing: 0.02em; color: var(--fg); white-space: nowrap; }

/* ── login ── */
#login { display: flex; align-items: center; justify-content: center; }
.login-card { width: min(320px, 86vw); display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-mark { font-family: var(--serif); font-size: 34px; letter-spacing: 0.2em; margin-bottom: 18px; }
.login-card input { height: 46px; border-radius: var(--radius); border: 0; padding: 0 16px; background: var(--fill); color: var(--fg); font: inherit; font-size: 16px; outline: none; }
.login-card button { height: 46px; border-radius: var(--radius); background: var(--fg); color: var(--bg); font-weight: 600; font-size: 16px; }
.login-err { min-height: 1.2em; color: #ff3b30; font-size: 13px; margin: 0; }

/* ── library ── */
#library { overflow-y: auto; padding: calc(var(--safe-top) + 20px) max(20px, 5vw) calc(var(--safe-bottom) + 40px); }
.lib-head { display: flex; align-items: flex-end; justify-content: space-between; margin: 12px 0 22px; }
.lib-head h1 { font-size: 34px; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
.lib-actions { display: flex; gap: 4px; }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 30px 22px; }
.book { display: flex; flex-direction: column; gap: 8px; cursor: pointer; }
.cover { position: relative; aspect-ratio: 2 / 3; border-radius: 4px 6px 6px 4px; overflow: hidden; background: var(--card); box-shadow: 0 1px 2px rgba(0,0,0,0.12), 0 8px 18px rgba(0,0,0,0.12); transition: transform 0.2s ease, box-shadow 0.2s ease; }
.book:hover .cover { transform: translateY(-3px); box-shadow: 0 2px 4px rgba(0,0,0,0.12), 0 14px 26px rgba(0,0,0,0.16); }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, rgba(0,0,0,0.14) 0, rgba(255,255,255,0.08) 2.5%, rgba(0,0,0,0) 6%); pointer-events: none; }
.cover-gen { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between; padding: 14px 12px; font-family: var(--serif); color: #fff; }
.cover-gen .t { font-size: 17px; line-height: 1.2; font-weight: 600; }
.cover-gen .a { font-size: 11px; opacity: 0.8; }
.book-meta { display: flex; align-items: center; justify-content: space-between; gap: 6px; font-size: 12px; color: var(--sub); }
.book-meta .zh { font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--fill); }
.book-menu { width: 26px; height: 22px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; color: var(--sub); }
.book-menu svg { width: 16px; height: 16px; }
.book-menu:hover { background: var(--fill); }
.shelf-empty { color: var(--sub); text-align: center; margin-top: 18vh; font-size: 15px; }

/* ── reader ── */
.r-top { position: absolute; top: 0; left: 0; right: 0; height: calc(var(--top-h) + var(--safe-top)); padding: var(--safe-top) 8px 0; display: flex; align-items: center; gap: 6px; z-index: 5; background: var(--bg); transition: opacity 0.25s ease; }
.r-title { flex: 1; min-width: 0; text-align: center; font-size: 13px; font-weight: 600; color: var(--sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-top-actions { display: flex; align-items: center; gap: 2px; }
#reader.chrome-off .r-top { opacity: 0; pointer-events: none; }

.r-stage { position: absolute; left: 0; right: 0; top: calc(var(--top-h) + var(--safe-top)); bottom: calc(var(--bottom-h) + var(--safe-bottom)); }
.r-viewport { position: absolute; inset: 18px var(--margin, 28px) 6px; overflow: hidden; }
.r-flow {
  position: relative;   /* sentence offsetLeft is measured against the flow */
  height: 100%;
  column-fill: auto;
  font-family: var(--serif);
  font-size: var(--fs, 19px);
  line-height: 1.62;
  text-align: justify;
  hyphens: auto; -webkit-hyphens: auto;
  will-change: transform;
  transition: transform 0.32s cubic-bezier(0.2, 0.7, 0.2, 1);
}
.r-flow.no-anim { transition: none; }
body[data-font="sans"] .r-flow { font-family: var(--sans); }
.r-flow p { margin: 0; text-indent: 1.4em; }
.r-flow p + p { margin-top: 0.15em; }
.r-flow h2 { font-size: 1.35em; font-weight: 600; text-align: center; text-indent: 0; margin: 1.2em 0 1.4em; line-height: 1.3; letter-spacing: 0.01em; break-after: avoid; }
.r-flow.lang-zh p { text-indent: 2em; text-align: justify; }
.r-flow .s { border-radius: 3px; transition: background 0.2s ease; cursor: text; }
.r-flow .s.cur { background: var(--hl); box-shadow: 0 0 0 2px var(--hl); }
.r-flow .s.mark { background: var(--fill); }

.tap-zone { position: absolute; top: 0; bottom: 0; z-index: 2; }
.tap-prev { left: 0; width: 28%; cursor: w-resize; }
.tap-next { right: 0; width: 28%; cursor: e-resize; }
.tap-mid { left: 28%; right: 28%; pointer-events: none; }

.r-bottom { position: absolute; left: 0; right: 0; bottom: 0; height: calc(var(--bottom-h) + var(--safe-bottom)); padding: 0 14px var(--safe-bottom); display: flex; flex-direction: column; justify-content: flex-start; background: var(--bg); z-index: 5; }
.r-progress { height: 22px; display: flex; align-items: center; justify-content: space-between; font-size: 11px; color: var(--sub); padding: 0 max(0px, calc(var(--margin, 28px) - 14px)); font-variant-numeric: tabular-nums; }
.r-progress span:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 10px; }
.player { height: 56px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; border-top: 0.5px solid var(--hair); }
.player > .voice-pill { justify-self: start; }
.player-right { justify-self: end; display: flex; align-items: center; gap: 2px; }
.player-right #p-speed { margin-right: 4px; }

/* volume: a speaker that shows the level, opening a small slider above it */
.vol-wrap { position: relative; }
#p-vol .vol-x { display: none; }
#p-vol.mid .vol-w2, #p-vol.low .vol-w2, #p-vol.low .vol-w1 { opacity: 0.25; }
#p-vol.mute .vol-w1, #p-vol.mute .vol-w2 { display: none; }
#p-vol.mute .vol-x { display: inline; }
.vol-pop {
  position: absolute; right: -6px; bottom: 44px; z-index: 8;
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 14px;
  background: var(--bg); border: 0.5px solid var(--hair); box-shadow: 0 8px 28px rgba(0, 0, 0, 0.16);
  animation: fade 0.15s ease;
}
.vol-pop span { width: 34px; text-align: right; font-size: 12px; color: var(--sub); font-variant-numeric: tabular-nums; }
.vol-pop input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 150px; height: 4px; border-radius: 2px; margin: 0; cursor: pointer;
  background: linear-gradient(90deg, var(--fg) 0 var(--v, 100%), var(--hair) var(--v, 100%) 100%);
}
.vol-pop input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #fff; border: 0.5px solid rgba(0, 0, 0, 0.12); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
.vol-pop input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: #fff; border: 0.5px solid rgba(0, 0, 0, 0.12); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
.player-main { display: flex; align-items: center; gap: 14px; }
.play-btn { width: 44px; height: 44px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; background: var(--fg); color: var(--bg); transition: transform 0.12s ease; }
.play-btn svg { fill: currentColor; stroke: none; width: 20px; height: 20px; }
.play-btn .i-play { margin-left: 3px; }
.play-btn:active { transform: scale(0.93); }
.play-btn .i-pause, .playing .play-btn .i-play { display: none; }
.playing .play-btn .i-pause { display: block; }
.play-btn.loading { animation: breathe 1.2s ease-in-out infinite; }
@keyframes breathe { 50% { opacity: 0.45; } }
.p-status { height: 18px; text-align: center; font-size: 11px; color: var(--sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── sheets (bottom panels) ── */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.28); z-index: 20; animation: fade 0.2s ease; }
.sheet { position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); width: min(560px, 100vw); max-height: 82vh; overflow-y: auto; z-index: 21; background: var(--bg); color: var(--fg); border-radius: 16px 16px 0 0; padding: 10px 18px calc(var(--safe-bottom) + 22px); box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.18); animation: rise 0.28s cubic-bezier(0.2, 0.7, 0.2, 1); }
.sheet::before { content: ""; display: block; width: 36px; height: 5px; border-radius: 3px; background: var(--hair); margin: 0 auto 12px; }
.sheet h3 { font-size: 17px; font-weight: 600; margin: 4px 0 14px; text-align: center; }
.sheet .group { background: var(--fill); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; }
.sheet .row { min-height: 46px; padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 15px; }
.sheet .row + .row { border-top: 0.5px solid var(--hair); }
.sheet .row.btn { cursor: pointer; }
.sheet .row.btn:hover { background: var(--fill); }
.sheet .row.danger { color: #ff3b30; }
.sheet .row .sub { color: var(--sub); font-size: 13px; }
.sheet .note { font-size: 12px; color: var(--sub); margin: -6px 4px 14px; line-height: 1.5; }
.sheet input[type="text"], .sheet input[type="password"], .sheet textarea { width: 100%; border: 0; border-radius: 8px; background: var(--bg); color: var(--fg); padding: 8px 10px; font: inherit; font-size: 13px; outline: none; }
.sheet textarea { min-height: 220px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
.sheet .primary { width: 100%; height: 44px; border-radius: var(--radius); background: var(--fg); color: var(--bg); font-weight: 600; }
.seg { display: flex; background: var(--fill); border-radius: 9px; padding: 2px; gap: 2px; }
.seg button { flex: 1; height: 30px; border-radius: 7px; font-size: 13px; font-weight: 500; }
.seg button.on { background: var(--bg); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); font-weight: 600; }
.themes { display: flex; gap: 14px; justify-content: center; }
.themes button { width: 46px; height: 46px; border-radius: 50%; border: 0.5px solid var(--hair); font-family: var(--serif); font-size: 16px; }
.themes button.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--tint); }
.sizes { display: flex; align-items: center; gap: 10px; }
.sizes button { flex: 1; height: 38px; border-radius: 9px; background: var(--fill); font-family: var(--serif); }
.toc-item { cursor: pointer; }
.toc-item.on { color: var(--tint); font-weight: 600; }
.toc-item .sub { flex-shrink: 0; }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button { width: 30px; height: 30px; border-radius: 8px; background: var(--bg); font-size: 18px; line-height: 1; }
.bar { height: 4px; border-radius: 2px; background: var(--hair); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--tint); width: 0; transition: width 0.3s ease; }

.toast { position: fixed; left: 50%; top: calc(var(--safe-top) + 14px); transform: translateX(-50%); z-index: 40; max-width: 88vw; padding: 9px 16px; border-radius: 999px; background: rgba(30, 30, 32, 0.88); color: #fff; font-size: 13px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2); animation: fade 0.2s ease; -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translate(-50%, 30px); opacity: 0; } }

@media (max-width: 600px) {
  .player-main { gap: 8px; }
  .player-right #p-speed { padding: 0 9px; margin-right: 0; }
  .shelf { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 24px 16px; }
}
@media (prefers-reduced-motion: reduce) { .r-flow { transition: none; } }

/* emotion (temperature) slider rows */
.temp-ctl { flex: 1; display: flex; align-items: center; gap: 8px; justify-content: flex-end; }
.temp-ctl .temp-val { width: 2.6em; text-align: right; font-variant-numeric: tabular-nums; }
.temp-range { -webkit-appearance: none; appearance: none; flex: 1; max-width: 200px; height: 4px; border-radius: 2px; background: var(--hair); margin: 0; cursor: pointer; }
.temp-range::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #fff; border: 0.5px solid rgba(0, 0, 0, 0.12); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
.temp-range::-moz-range-thumb { width: 20px; height: 20px; border-radius: 50%; background: #fff; border: 0.5px solid rgba(0, 0, 0, 0.12); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }

/* storage sheet */
.st-book { display: flex; justify-content: space-between; align-items: baseline; margin: 16px 4px 6px; font-size: 14px; font-weight: 600; }
.st-tools { display: flex; gap: 8px; justify-content: flex-end; margin: 4px 0 -4px; }
.st-row { cursor: pointer; justify-content: flex-start !important; }
.st-row input { width: 20px; height: 20px; margin: 0; accent-color: var(--tint); flex-shrink: 0; }
.st-name { flex: 1; display: flex; flex-direction: column; gap: 1px; }
.sheet .primary:disabled { opacity: 0.35; cursor: default; }
