:root {
  color-scheme: light dark;
  --bg: #f5f5f7; --panel: #ffffff; --text: #1d1d1f; --muted: #6e6e73; --line: #d2d2d7;
  --accent: #0a66c2; --accent-text: #ffffff; --hover: #e8e8ed; --selected: #dbe7f5; --danger: #c0392b;
  --shadow: 0 1px 3px rgba(0,0,0,0.12);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1c1c1e; --panel: #2c2c2e; --text: #f2f2f7; --muted: #9a9aa0; --line: #3a3a3c; --accent: #4ea1ff; --accent-text: #0b0b0c;
          --hover: #3a3a3c; --selected: #2e4461; --shadow: 0 1px 3px rgba(0,0,0,0.5); }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
#app { height: 100%; display: flex; flex-direction: column; }
button, select, input { font: inherit; color: inherit; }
button { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
button.primary:hover { filter: brightness(1.1); background: var(--accent); }
button.danger { color: var(--danger); }
button.quiet { border-color: transparent; background: transparent; }
button.quiet:hover { background: var(--hover); }
button:disabled { opacity: 0.5; cursor: default; }
input[type=text], input[type=search], select { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 0.85em; }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.hidden { display: none !important; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Header */
header.bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
header.bar h1 { font-size: 1.1em; margin: 0 8px 0 0; font-weight: 600; }
header.bar input[type=search] { width: 260px; max-width: 100%; }

/* Library layout */
.library { display: grid; grid-template-columns: 220px 1fr; flex: 1; min-height: 0; }
.library.with-detail { grid-template-columns: 220px 1fr 340px; }
aside.sidebar { border-right: 1px solid var(--line); padding: 10px 8px; overflow: auto; background: var(--bg); }
aside.sidebar h3 { font-size: 0.75em; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 14px 10px 4px; }
aside.sidebar .item { display: flex; justify-content: space-between; gap: 6px; width: 100%; text-align: left; border: 0; background: transparent; border-radius: 6px; padding: 6px 10px; }
aside.sidebar .item:hover { background: var(--hover); }
aside.sidebar .item.active { background: var(--selected); }
aside.sidebar .item .count { color: var(--muted); font-size: 0.85em; }
main.books { overflow: auto; padding: 16px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px; }
.card { background: transparent; border: 0; padding: 6px; border-radius: 10px; text-align: left; cursor: pointer; display: flex; flex-direction: column; gap: 6px; }
.card:hover { background: var(--hover); }
.card.selected { background: var(--selected); }
.cover { width: 100%; aspect-ratio: 2 / 3; background: linear-gradient(160deg, #6b7a8f, #3c4858); border-radius: 6px; box-shadow: var(--shadow); display: flex; align-items: flex-end; padding: 10px; color: #fff; font-weight: 600; overflow: hidden; }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; border-radius: 6px; }
.cover.placeholder span { font-size: 0.9em; text-shadow: 0 1px 2px rgba(0,0,0,0.6); }
.card .title { font-weight: 600; line-height: 1.25; }
.progressbar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progressbar i { display: block; height: 100%; background: var(--accent); }
.formats { display: flex; gap: 4px; flex-wrap: wrap; }
.badge { font-size: 0.72em; padding: 1px 6px; border: 1px solid var(--line); border-radius: 4px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.badge.epub { border-color: #2e8b57; color: #2e8b57; } .badge.pdf { border-color: #c0392b; color: #c0392b; }
.badge.docx { border-color: #2b579a; color: #2b579a; } .badge.html { border-color: #e67e22; color: #e67e22; } .badge.text { border-color: #7f8c8d; color: #7f8c8d; }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.85em; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.list td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.list tr { cursor: pointer; }
table.list tr:hover td { background: var(--hover); }
table.list tr.selected td { background: var(--selected); }
table.list .thumb { width: 32px; height: 48px; border-radius: 3px; object-fit: cover; background: #556; display: block; }
.empty { color: var(--muted); text-align: center; padding: 60px 20px; }
.empty p { max-width: 420px; margin: 8px auto; }

/* Detail panel */
aside.detail { border-left: 1px solid var(--line); padding: 16px; overflow: auto; background: var(--panel); }
aside.detail .cover { max-width: 180px; margin: 0 auto 14px; }
aside.detail h2 { font-size: 1.15em; margin: 0 0 2px; }
aside.detail h4 { font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 18px 0 6px; }
aside.detail .version { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
aside.detail .version .name { flex: 1; min-width: 0; }
aside.detail ul { list-style: none; margin: 0; padding: 0; }
aside.detail li { padding: 4px 0; }
aside.detail label.check { display: flex; align-items: center; gap: 8px; padding: 3px 0; cursor: pointer; }
.bookmark { display: flex; gap: 8px; align-items: center; }
.bookmark button.open { flex: 1; text-align: left; border: 0; background: transparent; padding: 4px 0; }
.bookmark button.open:hover { text-decoration: underline; background: transparent; }

/* Sign-in */
.signin-wrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.signin { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); }
.signin h1 { font-size: 1.2em; margin: 0 0 4px; }
.signin p { margin: 0 0 6px; }
header.bar select[title="Account"] { max-width: 160px; }

/* Dialogs */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 0; max-width: min(760px, 95vw); width: 720px; box-shadow: 0 10px 40px rgba(0,0,0,0.3); }
dialog::backdrop { background: rgba(0,0,0,0.4); }
dialog .head { padding: 16px 20px 10px; border-bottom: 1px solid var(--line); }
dialog .head h2 { margin: 0; font-size: 1.1em; }
dialog .body { padding: 14px 20px; max-height: 60vh; overflow: auto; }
dialog .foot { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.review-item { padding: 10px 0; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; }
.review-item .name { font-weight: 600; }
.review-item .explain { grid-column: 1 / -1; color: var(--muted); font-size: 0.9em; }
.review-item select { max-width: 280px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); z-index: 50; max-width: 90vw; }
.toast.error { background: var(--danger); color: #fff; }

/* Reader */
.reader { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.reader .toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.reader .toolbar .title { font-weight: 600; }
.reader .toolbar .where { color: var(--muted); font-size: 0.9em; }
.reader .stage { display: flex; flex: 1; min-height: 0; position: relative; }
.reader iframe { flex: 1; border: 0; background: var(--bg); width: 100%; height: 100%; }
.reader .drawer { width: 320px; border-right: 1px solid var(--line); background: var(--panel); overflow: auto; padding: 10px 12px; }
.drawer h3 { font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 10px 0 6px; }
.drawer ol { list-style: none; margin: 0; padding-left: 0; }
.drawer ol ol { padding-left: 14px; }
.drawer li button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 5px 6px; border-radius: 6px; }
.drawer li button:hover { background: var(--hover); }
.drawer li button.active { background: var(--selected); }
.drawer .tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.drawer .tabs button { flex: 1; padding: 5px; }
.drawer .tabs button.active { background: var(--selected); }
.hit { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 6px; border-radius: 6px; border-bottom: 1px solid var(--line); }
.hit:hover { background: var(--hover); }
.hit .chapter { color: var(--muted); font-size: 0.8em; }
.hit mark { background: rgba(255, 214, 10, 0.5); color: inherit; border-radius: 2px; }
.reader .footer { display: flex; align-items: center; gap: 10px; padding: 6px 12px; background: var(--panel); border-top: 1px solid var(--line); font-size: 0.9em; }
.reader .footer .progressbar { flex: 1; height: 6px; cursor: pointer; }
.pages { flex: 1; overflow: auto; padding: 16px; background: var(--bg); }
.page { position: relative; width: min(100%, 900px); margin: 0 auto 16px; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.35); }
.page img { width: 100%; height: 100%; display: block; }
.page .page-number { position: absolute; right: 8px; bottom: 6px; font-size: 0.75em; color: #666; background: rgba(255,255,255,0.8); padding: 1px 6px; border-radius: 4px; }
@media (prefers-color-scheme: dark) { .page img { filter: brightness(0.9); } }
.pdf-note { padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--line); color: var(--muted); font-size: 0.9em; }

/* Listening bar */
.listen { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--panel); border-top: 1px solid var(--line); flex-wrap: wrap; }
.listen .now { flex: 1; min-width: 160px; }
.listen .now .what { font-weight: 600; }
.listen .sentence { color: var(--muted); font-size: 0.9em; max-height: 2.6em; overflow: hidden; }
.listen input[type=range] { width: 120px; }

@media (max-width: 900px) {
  .library, .library.with-detail { grid-template-columns: 1fr; }
  aside.sidebar { display: flex; gap: 4px; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 6px; }
  aside.sidebar h3 { display: none; }
  aside.sidebar .item { white-space: nowrap; width: auto; }
  aside.detail { border-left: 0; border-top: 1px solid var(--line); }
  .reader .drawer { position: absolute; left: 0; top: 0; bottom: 0; z-index: 5; box-shadow: var(--shadow); }
}
