:root {
  --bg: #f2e8d5;
  --fg: #3a2f22;
  --muted: #9c8a6e;
  --accent: #a16207;
  --panel: color-mix(in srgb, var(--fg) 5%, var(--bg));
  --panel-strong: color-mix(in srgb, var(--fg) 10%, var(--bg));
  --border: color-mix(in srgb, var(--fg) 16%, var(--bg));
  --shadow: 0 10px 30px -12px rgba(0,0,0,.35);
  --radius: 14px;
  --font-ui: 'Outfit', system-ui, -apple-system, sans-serif;
  --font-read: 'Spectral', Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  transition: background .3s ease, color .3s ease;
  overflow: hidden;
}

.app { height: 100vh; display: flex; flex-direction: column; }

/* ---------- topbar ---------- */
.topbar {
  height: 58px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, var(--fg) 0%);
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 600; letter-spacing: .2px; }
.brand-mark { color: var(--accent); font-size: 19px; }
.brand-name { font-size: 17px; }
.brand-thin { font-weight: 300; opacity: .55; margin-left: 3px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  transition: background .15s, transform .1s, border-color .15s;
}
.icon-btn:hover { background: var(--panel-strong); }
.icon-btn:active { transform: scale(.94); }

.btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  padding: 9px 16px;
  border-radius: 10px;
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, transform .1s, border-color .15s, opacity .15s;
  display: inline-flex; align-items: center; gap: 7px;
}
.btn:hover { background: var(--panel-strong); }
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 85%, #000); }
.btn-ghost { border-color: transparent; }
.btn-ghost:hover { background: var(--panel-strong); }
.btn-small { padding: 6px 11px; font-size: 13px; }
.btn.danger, .btn-primary.danger { background: #b3382f; border-color: #b3382f; color: #fff; }

.user-chip { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 8px; border-radius: 10px; transition: background .15s; }
.user-chip:hover { background: var(--panel-strong); }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 14px; }
.avatar.big { width: 44px; height: 44px; font-size: 19px; }
.avatar.ghost { background: var(--panel-strong); color: var(--muted); }
.user-name { font-size: 14px; font-weight: 500; }

/* ---------- view root ---------- */
.view-root { flex: 1; overflow: auto; position: relative; }

/* ---------- library ---------- */
.library { max-width: 1180px; margin: 0 auto; padding: 34px 22px 60px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 26px; gap: 16px; flex-wrap: wrap; }
.section-head h2 { margin: 0 0 4px; font-size: 25px; font-weight: 600; letter-spacing: -.3px; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 12.5px; }

.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 24px; }
.book-grid.dragging { outline: 2px dashed var(--accent); outline-offset: 12px; }

.book-card { display: flex; flex-direction: column; gap: 12px; }
.book-cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  background: var(--panel-strong);
  box-shadow: var(--shadow);
  transition: transform .22s ease, box-shadow .22s ease;
}
.book-cover:hover { transform: translateY(-5px) scale(1.01); box-shadow: 0 18px 40px -14px rgba(0,0,0,.5); }
.book-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-fallback { position: absolute; inset: 0; display: grid; place-items: center; font-size: 54px; font-family: var(--font-read); color: var(--accent); }
.cover-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 5px; background: rgba(0,0,0,.28); }
.cover-progress span { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.book-meta h3 { margin: 0 0 2px; font-size: 15px; font-weight: 600; cursor: pointer; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-meta h3:hover { color: var(--accent); }
.book-meta p { font-size: 13px; margin-bottom: 8px; }
.book-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 9px; }
.tag { font-size: 11.5px; padding: 3px 8px; border-radius: 999px; background: var(--panel-strong); color: var(--muted); }
.tag-warn { background: color-mix(in srgb, #d99b4a 26%, var(--panel-strong)); color: var(--fg); }
.book-card.is-meta .book-cover { opacity: .62; cursor: default; }
.book-card.is-meta .book-cover:hover { transform: none; box-shadow: var(--shadow); }
.book-card.is-meta .book-meta h3 { cursor: default; }
.book-card.is-meta .book-meta h3:hover { color: var(--fg); }
.btn-remove { opacity: 0; transition: opacity .18s; padding: 5px 10px; font-size: 12px; }
.book-card:hover .btn-remove { opacity: 1; }

/* dropzone */
.dropzone {
  max-width: 560px; margin: 8vh auto 0; text-align: center;
  padding: 60px 30px; border: 2px dashed var(--border); border-radius: 20px;
  background: var(--panel); transition: border-color .2s, background .2s;
}
.dropzone.dragging { border-color: var(--accent); background: var(--panel-strong); }
.dz-glyph { font-size: 52px; color: var(--accent); margin-bottom: 12px; }
.dropzone h2 { margin: 0 0 8px; font-size: 22px; font-weight: 600; }
.dz-hint { margin-top: 18px !important; font-size: 13px; }

/* ---------- reader ---------- */
body.reader-active .view-root { overflow: hidden; }
.reader { display: flex; height: 100%; }

.reader-side {
  width: 268px; flex: 0 0 auto;
  border-right: 1px solid var(--border);
  background: var(--panel);
  padding: 18px 16px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 16px;
}
.back-btn { align-self: flex-start; }
.side-book { display: flex; gap: 12px; align-items: center; }
.side-book img { width: 48px; height: 70px; object-fit: cover; border-radius: 6px; }
.side-cover-fallback { width: 48px; height: 70px; border-radius: 6px; background: var(--panel-strong); display: grid; place-items: center; color: var(--accent); font-size: 22px; }
.side-book h3 { margin: 0 0 2px; font-size: 14.5px; font-weight: 600; line-height: 1.25; }
.side-book p { font-size: 12.5px; }
.side-progress { display: flex; align-items: center; gap: 14px; }
.ring {
  --p: 0; --sz: 56px;
  width: var(--sz); height: var(--sz); border-radius: 50%; flex: 0 0 auto;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--panel-strong) 0);
  display: grid; place-items: center; position: relative;
}
.ring::after { content: ''; position: absolute; inset: 6px; border-radius: 50%; background: var(--panel); }
.ring span { position: relative; font-size: 13px; font-weight: 600; }
.side-title { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 4px 0 0; font-weight: 600; }
.toc { display: flex; flex-direction: column; gap: 2px; }
.toc-item {
  display: flex; gap: 10px; align-items: baseline; text-align: left;
  background: transparent; border: none; color: var(--fg);
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font-family: var(--font-ui); font-size: 13.5px;
  transition: background .15s;
}
.toc-item:hover { background: var(--panel-strong); }
.toc-item.active { background: color-mix(in srgb, var(--accent) 18%, var(--panel)); font-weight: 600; }
.toc-num { font-size: 11px; color: var(--muted); min-width: 18px; }
.toc-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.reader-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.reader-toolbar {
  flex: 0 0 auto; height: 54px; display: flex; align-items: center; gap: 12px;
  padding: 0 18px; border-bottom: 1px solid var(--border);
}
.toolbar-spacer { flex: 1; }
.chapter-title { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40%; }
.mode-switch { display: flex; background: var(--panel-strong); border-radius: 10px; padding: 3px; }
.seg {
  border: none; background: transparent; color: var(--muted);
  padding: 7px 15px; border-radius: 8px; cursor: pointer; font-family: var(--font-ui);
  font-size: 13.5px; font-weight: 500; transition: all .16s;
}
.seg.active { background: var(--bg); color: var(--fg); box-shadow: 0 1px 4px rgba(0,0,0,.15); }
.seg-group { display: inline-flex; background: var(--panel-strong); border-radius: 9px; padding: 3px; gap: 2px; }
.seg-group .seg { padding: 6px 13px; font-size: 13px; }

.reader-body { flex: 1; overflow: hidden; position: relative; }

/* flow mode */
.reader-scroll { height: 100%; overflow-y: auto; scroll-behavior: auto; }
.prose {
  margin: 0 auto; padding: 40px 28px 120px;
  color: var(--fg);
  font-family: var(--font-read);
}
.prose p { margin: 0 0 1.15em; text-indent: 0; }
.prose h1, .prose h2, .prose h3 { font-family: var(--font-ui); line-height: 1.25; margin: 1.6em 0 .7em; }
.prose img { max-width: 100%; height: auto; border-radius: 6px; }
.prose blockquote { border-left: 3px solid var(--accent); margin: 1.2em 0; padding-left: 16px; opacity: .9; font-style: italic; }
.hl-word { transition: background .12s, color .12s; border-radius: 3px; }
.hl-word.hl-now { background: color-mix(in srgb, var(--accent) 65%, transparent); color: var(--bg); }

/* reader controls */
.reader-controls {
  flex: 0 0 auto; display: flex; align-items: center; gap: 22px; flex-wrap: wrap;
  padding: 11px 20px; border-top: 1px solid var(--border);
  background: var(--panel);
}
.ctl-btn {
  display: flex; align-items: center; gap: 8px;
  background: transparent; border: 1px solid var(--border); color: var(--fg);
  padding: 8px 15px; border-radius: 10px; cursor: pointer; font-family: var(--font-ui);
  font-size: 13.5px; font-weight: 500; transition: background .15s, transform .1s, border-color .15s;
}
.ctl-btn:hover { background: var(--panel-strong); }
.ctl-btn:active { transform: scale(.96); }
.ctl-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.ctl-slider { display: flex; align-items: center; gap: 10px; }
.ctl-slider.wide input[type=range] { width: 220px; }
.ctl-label { font-size: 12.5px; color: var(--muted); }
.ctl-val { font-size: 12.5px; color: var(--muted); min-width: 52px; font-variant-numeric: tabular-nums; }
.ctl-group { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.ctl-icon { background: transparent; border: 1px solid var(--border); color: var(--fg); width: 34px; height: 34px; border-radius: 8px; cursor: pointer; font-size: 12px; }
.ctl-icon:hover { background: var(--panel-strong); }
.ctl-pageno { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

input[type=range] {
  -webkit-appearance: none; appearance: none; height: 4px; background: var(--border);
  border-radius: 4px; outline: none; width: 130px; cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 2px solid var(--bg);
}
input[type=range]::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 2px solid var(--bg); cursor: pointer; }

/* ---------- RSVP ---------- */
.rsvp-wrap {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 26px; padding: 24px; position: relative;
}
.rsvp-bar-top { position: absolute; top: 18px; left: 24px; right: 24px; display: flex; justify-content: space-between; font-size: 12.5px; }
.rsvp-stage {
  position: relative; width: 100%; max-width: 860px; height: 220px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; user-select: none;
}
.orp-line { position: absolute; left: calc(50% - 60px); top: 34px; bottom: 34px; width: 1px; background: color-mix(in srgb, var(--fg) 22%, transparent); }
.orp-marker { position: absolute; top: 22px; left: calc(50% - 60px); transform: translateX(-50%); color: var(--accent); font-size: 11px; }
.orp-marker::before { content: '▾'; }
.rsvp-word {
  position: absolute; left: calc(50% - 60px);
  transform: translateX(0);
  font-family: var(--font-read); font-weight: 400;
  white-space: nowrap; line-height: 1;
  display: flex;
}
.rsvp-word .l { text-align: right; }
.rsvp-word .o { color: var(--accent); }
.rsvp-word .r { text-align: left; }
.rsvp-word .mw.dim { opacity: .5; }
.rsvp-hint { position: absolute; bottom: 6px; font-size: 12px; }
.rsvp-progress { width: 100%; max-width: 860px; height: 5px; background: var(--panel-strong); border-radius: 4px; overflow: hidden; }
.rsvp-progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .12s linear; }
.rsvp-scrub { width: 100%; max-width: 860px; }
.rsvp-scrub input[type=range] { width: 100%; }
.rsvp-side { display: none; }

/* ---------- modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
  animation: fade .18s ease;
}
.modal-backdrop.center { align-items: center; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--bg); border: 1px solid var(--border); border-radius: 18px;
  width: 100%; max-width: 520px; max-height: 88vh; display: flex; flex-direction: column;
  box-shadow: 0 30px 70px -20px rgba(0,0,0,.6);
  animation: rise .22s cubic-bezier(.2,.8,.3,1);
}
.modal-wide { max-width: 600px; }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; font-weight: 600; }
.settings-scroll, .search-results, .modal > .text-input { overflow-y: auto; }
.modal > .text-input { margin: 16px; }
.settings-scroll { padding: 18px 20px 26px; }
.settings-scroll h4 { margin: 22px 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 600; }
.settings-scroll h4:first-of-type { margin-top: 16px; }
.set-account { padding-bottom: 6px; }
.acct { display: flex; gap: 14px; align-items: center; }
.acct strong { font-size: 14.5px; }
.acct p { margin: 2px 0 8px; font-size: 13px; }

.text-input {
  width: 100%; padding: 11px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel);
  color: var(--fg); font-family: var(--font-ui); font-size: 14px;
}
.text-input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 1px; }

.theme-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.theme-swatch {
  display: flex; align-items: center; gap: 8px; padding: 9px 11px;
  border: 1.5px solid var(--border); border-radius: 10px; cursor: pointer;
  background: var(--sw-bg); color: var(--sw-fg); font-family: var(--font-ui); font-size: 13px;
  transition: transform .12s, border-color .15s;
}
.theme-swatch:hover { transform: translateY(-2px); }
.theme-swatch.active { border-color: var(--sw-accent); box-shadow: 0 0 0 2px var(--sw-accent); }
.sw-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--sw-accent); flex: 0 0 auto; }

.set-row { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.set-row.col { flex-direction: column; align-items: stretch; gap: 9px; }
.set-row.wrap { flex-wrap: wrap; }
.set-row label { font-size: 13.5px; min-width: 110px; color: var(--fg); }
.set-range { flex: 1; width: auto !important; }
.set-val { font-size: 12.5px; color: var(--muted); min-width: 60px; text-align: right; font-variant-numeric: tabular-nums; }
.check-row { display: flex; align-items: center; gap: 10px; font-size: 13.5px; cursor: pointer; margin-bottom: 14px; }
.check-row input { width: 17px; height: 17px; accent-color: var(--accent); }
.about { margin-top: 20px; line-height: 1.5; }

.search-results { padding: 8px 16px 20px; display: flex; flex-direction: column; gap: 4px; }
.empty { padding: 24px 0; text-align: center; }
.search-hit {
  display: flex; flex-direction: column; gap: 4px; text-align: left;
  background: transparent; border: none; border-radius: 9px; padding: 11px 13px;
  cursor: pointer; color: var(--fg); font-family: var(--font-ui); transition: background .14s;
}
.search-hit:hover { background: var(--panel-strong); }
.hit-ch { font-size: 11.5px; color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.hit-text { font-size: 13.5px; line-height: 1.5; font-family: var(--font-read); }
mark { background: color-mix(in srgb, var(--accent) 40%, transparent); color: var(--fg); border-radius: 3px; padding: 0 2px; }

/* voice picker */
select.text-input { cursor: pointer; appearance: none; -webkit-appearance: none; padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%239c8a6e' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; }
select.text-input option, select.text-input optgroup { background: var(--bg); color: var(--fg); }
.voice-warn {
  font-size: 13px; line-height: 1.5; margin: 0 0 14px; padding: 11px 13px;
  border-radius: 10px; border: 1px solid color-mix(in srgb, #d99b4a 55%, var(--border));
  background: color-mix(in srgb, #d99b4a 16%, var(--panel)); color: var(--fg);
}
#voicePreviewBtn { margin-top: 2px; }
/* loading / confirm */
.loading-card, .confirm-card {
  background: var(--bg); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 34px; text-align: center; box-shadow: var(--shadow); max-width: 380px;
}
.loading-card p { margin: 16px 0 0; font-size: 14px; }
.spinner {
  width: 34px; height: 34px; margin: 0 auto; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.confirm-card p { margin: 0 0 20px; font-size: 15px; line-height: 1.5; }
.confirm-actions { display: flex; gap: 10px; justify-content: center; }

/* toast */
.toast-root { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  background: var(--fg); color: var(--bg); padding: 11px 20px; border-radius: 10px;
  font-size: 13.5px; font-weight: 500; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(12px); transition: opacity .3s, transform .3s;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: none; }

.mobile-only { display: none; }

/* ---------- responsive: tablet ---------- */
@media (max-width: 900px) {
  .reader-side {
    position: absolute; top: 0; left: 0; bottom: 0; z-index: 30;
    transform: translateX(-100%); transition: transform .26s ease;
    box-shadow: var(--shadow); width: 280px;
  }
  .reader-side.open { transform: none; }
  .mobile-only { display: grid; }
  .chapter-title { max-width: 30%; }
}

/* ---------- responsive: mobile ---------- */
@media (max-width: 640px) {
  .library { padding: 22px 16px 50px; }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px; }
  .btn-remove { opacity: 1; }
  .section-head h2 { font-size: 21px; }
  .reader-controls { gap: 12px; padding: 10px 12px; }
  .ctl-slider.wide input[type=range] { width: 120px; }
  input[type=range] { width: 96px; }
  .ctl-btn span { display: none; }
  .ctl-btn { padding: 9px 12px; }
  .ctl-group { margin-left: 0; width: 100%; justify-content: space-between; }
  .reader-toolbar { height: 50px; padding: 0 12px; gap: 8px; }
  .chapter-title { display: none; }
  .mode-switch .seg { padding: 7px 11px; font-size: 13px; }
  .prose { padding: 26px 18px 100px; }
  .rsvp-word { left: 50%; }
  .orp-line, .orp-marker { left: 50%; }
  .rsvp-stage { height: 170px; }
  .set-row label { min-width: 88px; font-size: 13px; }
  .modal { max-height: 92vh; }
  .modal-head { padding: 15px 16px; }
  .settings-scroll { padding: 14px 16px 22px; }
  .toast { font-size: 13px; }
}

@media (max-width: 400px) {
  .book-grid { grid-template-columns: repeat(2, 1fr); gap: 14px; }
  .brand-thin { display: none; }
}