:root {
  --navy: #0b121c;
  --panel: #121a26;
  --paper: #f8f4e9;
  --hair: rgba(255,255,255,0.08);
  --accent: #73dbf2;
  --text: rgba(255,255,255,0.88);
  --muted: rgba(255,255,255,0.45);
  --ink: #29211a;
  --head: #1a2947;
  --yellow: #fadb59; --amber: #f5ad52; --pink: #f58cb3; --green: #8cd185; --blue: #73bdfa;
  --hl-yellow: rgba(252,224,71,0.72);
  --hl-amber: rgba(250,179,71,0.70);
  --hl-pink: rgba(250,140,184,0.62);
  --hl-green: rgba(140,214,122,0.62);
  --hl-blue: rgba(115,191,255,0.58);
  --note-yellow: #fff08c; --note-amber: #ffcc73; --note-pink: #ffbfd1; --note-green: #bdeba8; --note-blue: #b3dbff;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --side-w: 280px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--navy); color: var(--text); }
body {
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:disabled { cursor: default; opacity: 0.3; }
input { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; display: block; }
[hidden] { display: none !important; }

.app { display: flex; height: 100vh; height: 100dvh; width: 100%; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--side-w); flex: 0 0 var(--side-w);
  background: var(--panel); border-right: 1px solid var(--hair);
  display: flex; flex-direction: column; min-height: 0;
  padding-top: var(--sat); padding-left: var(--sal);
}
.side-head { padding: 16px 14px 10px; }
.side-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.side-title { font-size: 13px; font-weight: 600; color: rgba(255,255,255,0.9); }
.filter {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 8px;
  background: rgba(255,255,255,0.06); color: rgba(255,255,255,0.4);
}
.filter svg { width: 13px; height: 13px; flex: none; }
.filter input, .search-box input {
  flex: 1; min-width: 0; background: transparent; border: 0; outline: none; font-size: 13px; color: var(--text);
  -webkit-appearance: none; appearance: none;
}
.filter input::placeholder, .search-box input::placeholder { color: rgba(255,255,255,0.35); }
input[type=search]::-webkit-search-cancel-button { filter: invert(0.7); }

.library-list { flex: 1; overflow-y: auto; padding: 8px 10px; -webkit-overflow-scrolling: touch; }
.book { margin-bottom: 6px; }
.book-head { display: flex; align-items: center; gap: 4px; padding: 2px 0 2px 2px; }
.chev {
  width: 22px; height: 30px; flex: none; display: grid; place-items: center; color: rgba(255,255,255,0.45);
  font-size: 11px; transition: transform .16s ease;
}
.chev.open { transform: rotate(90deg); }
.book-open { flex: 1; min-width: 0; text-align: left; padding: 2px 0; }
.book-title { font-size: 13px; font-weight: 600; color: rgba(255,255,255,0.82); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book.current .book-title { color: #fff; }
.book-sub { font-size: 11px; color: rgba(255,255,255,0.42); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.part-label { font-size: 10px; font-weight: 500; color: rgba(255,255,255,0.35); padding: 8px 0 2px 30px; text-transform: none; letter-spacing: .02em; }
.chapter-row {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  padding: 6px 8px 6px 22px; border-radius: 6px; font-size: 13px; color: rgba(255,255,255,0.78);
  min-height: 32px;
}
.chapter-row:hover { background: rgba(255,255,255,0.04); }
.chapter-row.selected { background: rgba(255,255,255,0.08); color: #fff; font-weight: 500; }
.dot { width: 6px; height: 6px; border-radius: 50%; flex: none; background: transparent; }
.chapter-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-ind { width: 9px; height: 10px; flex: none; border-radius: 1.5px; background: rgba(255,255,255,0.35); }
.page-lbl { font-size: 11px; color: rgba(255,255,255,0.32); font-variant-numeric: tabular-nums; }
.chapter-row.selected .page-lbl { color: var(--accent); }
.marks-head { display: flex; align-items: center; gap: 4px; width: 100%; padding-top: 8px; color: rgba(255,255,255,0.85); font-size: 13px; font-weight: 600; text-align: left; }
.marks-head .count { margin-left: auto; font-size: 11px; font-weight: 400; color: rgba(255,255,255,0.4); padding-right: 8px; }
.mark-row { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; padding: 6px 8px 6px 24px; font-size: 12px; color: rgba(255,255,255,0.8); border-radius: 6px; min-height: 30px; }
.mark-row:hover { background: rgba(255,255,255,0.04); }
.mark-row .chapter-name { flex: 1; }
.mark-row .page-lbl { color: rgba(255,255,255,0.4); font-size: 12px; }
.mark-row .book-tag { color: rgba(255,255,255,0.35); font-size: 11px; }
.empty-hint { font-size: 12px; color: rgba(255,255,255,0.35); padding: 10px 8px 6px 30px; }

.side-foot { border-top: 1px solid var(--hair); padding: 12px 16px calc(12px + var(--sab)); }
.import-btn { font-size: 12px; font-weight: 500; color: rgba(255,255,255,0.75); width: 100%; text-align: left; padding: 4px 0; }
.import-btn span { font-size: 15px; margin-right: 4px; }
.status { font-size: 11px; color: rgba(115,219,242,0.9); margin-top: 4px; min-height: 0; }
.status:empty { display: none; }

/* ---------- Reader ---------- */
.reader { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; position: relative; }
.toolbar {
  background: rgba(18,26,38,0.96); border-bottom: 1px solid var(--hair);
  padding: calc(10px + var(--sat)) calc(20px + var(--sar)) 10px 20px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.tb-row { display: flex; align-items: center; gap: 12px; min-width: 0; }
.tb-main { flex: 1 1 260px; }
.tb-tools { flex: 0 1 auto; flex-wrap: wrap; justify-content: flex-end; }
.titles { flex: 1; min-width: 0; }
.chapter-title { font: 600 16px/1.25 "New York", "Iowan Old Style", Georgia, "Times New Roman", serif; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.where-line { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-actions { display: flex; gap: 8px; align-items: center; }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 8px; color: rgba(255,255,255,0.8); font-size: 22px; line-height: 1; }
.icon-btn:hover { background: rgba(255,255,255,0.06); }
.round-btn { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,0.05); color: rgba(255,255,255,0.75); }
.round-btn:hover { background: rgba(255,255,255,0.1); }
.round-btn.on { color: var(--accent); background: rgba(255,255,255,0.1); }
.sticky-icon { width: 15px; height: 16px; background: #ffeb6b; border-radius: 2px; position: relative; box-shadow: 0 0.5px 1px rgba(0,0,0,.4); }
.sticky-icon::after { content: ""; position: absolute; right: 0; bottom: 0; border-style: solid; border-width: 0 0 6px 6px; border-color: transparent transparent #dbb82e transparent; }

.pen-tray { display: flex; gap: 4px; padding: 3px 6px; border-radius: 999px; background: rgba(255,255,255,0.05); }
.pen { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; border: 1.5px solid transparent; }
.pen i { width: 14px; height: 14px; border-radius: 50%; display: block; }
.pen.on { border-color: rgba(255,255,255,0.9); }
.tool-capsule { display: flex; padding: 3px; border-radius: 999px; background: rgba(255,255,255,0.05); }
.tool-btn { width: 32px; height: 26px; border-radius: 999px; display: grid; place-items: center; color: rgba(255,255,255,0.62); }
.tool-btn svg { width: 15px; height: 15px; }
.tool-btn.on { color: var(--accent); background: rgba(255,255,255,0.1); }
.search-box { display: flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 999px; background: rgba(255,255,255,0.05); color: rgba(255,255,255,0.75); }
.search-box > svg { width: 13px; height: 13px; flex: none; opacity: .55; }
.search-box input { width: 128px; flex: 1 1 128px; }
.search-count { font-size: 10px; color: rgba(255,255,255,0.45); font-variant-numeric: tabular-nums; white-space: nowrap; }
.mini-btn { min-width: 24px; height: 24px; border-radius: 6px; display: grid; place-items: center; color: rgba(255,255,255,0.75); font-size: 13px; }
.mini-btn:hover { background: rgba(255,255,255,0.08); }
.mini-btn.flip { transform: rotate(180deg); }
.mini-btn.wide { padding: 0 10px; font-size: 12px; background: rgba(255,255,255,0.08); }
.search-box:not(.has-query) .search-count, .search-box:not(.has-query) .mini-btn { display: none; }

.well { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; background: var(--navy); }
.sheet-wrap { max-width: 820px; margin: 0 auto; padding: 32px 72px 40px; }
.sheet {
  position: relative; background: var(--paper); border-radius: 6px; min-height: calc(100% - 20px);
  box-shadow: 0 8px 36px rgba(0,0,0,0.35); color: var(--ink);
  padding: 32px 44px 28px; min-height: 60vh;
}
.page-text {
  font: 18px/1.62 "New York", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  letter-spacing: 0.1px; hyphens: auto; -webkit-hyphens: auto; overflow-wrap: break-word;
  -webkit-user-select: text; user-select: text; -webkit-touch-callout: none;
}
.page-text p { margin: 0 0 12px; white-space: pre-wrap; }
.page-text p.heading { text-align: center; font-size: 15px; font-weight: 600; color: var(--head); letter-spacing: 0.8px; margin: 4px 0 10px; line-height: 1.4; }
.page-text ::selection { background: rgba(115,219,242,0.45); }
.hl { border-radius: 2px; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
.hl-yellow { background: var(--hl-yellow); } .hl-amber { background: var(--hl-amber); } .hl-pink { background: var(--hl-pink); }
.hl-green { background: var(--hl-green); } .hl-blue { background: var(--hl-blue); }
.find { background: rgba(140,219,242,0.55) !important; text-decoration: underline; text-underline-offset: 3px; }
.page-foot { text-align: center; font: 12px Georgia, serif; color: rgba(41,33,26,0.45); margin-top: 18px; }
.ribbon { position: absolute; top: 0; right: 28px; width: 14px; height: 58px; z-index: 2; pointer-events: none; filter: drop-shadow(0 1px 2px rgba(0,0,0,.25)); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 84%, 0 100%); }

/* Sticky notes */
.note-layer { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.note {
  position: absolute; pointer-events: auto; display: flex; flex-direction: column;
  border-radius: 3px; box-shadow: 0 3px 12px rgba(0,0,0,0.22); overflow: hidden;
  color: #2e2414;
}
.note-head { height: 26px; display: flex; align-items: center; gap: 4px; padding: 0 4px 0 0; flex: none; }
.note-drag { flex: 1; height: 100%; cursor: grab; touch-action: none; }
.note-drag:active { cursor: grabbing; }
.note-colors { display: flex; gap: 3px; align-items: center; }
.note-color { width: 16px; height: 18px; display: grid; place-items: center; }
.note-color i { width: 9px; height: 9px; border-radius: 50%; display: block; border: 1px solid transparent; }
.note-color.on i { border-color: rgba(0,0,0,0.45); }
.note-del { width: 20px; height: 20px; display: grid; place-items: center; font-size: 11px; font-weight: 700; color: rgba(0,0,0,0.45); }
.note textarea {
  flex: 1; resize: none; border: 0; outline: none; background: transparent; padding: 6px 8px 14px;
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; color: #2e2414;
}
.note textarea::placeholder { color: rgba(0,0,0,0.32); }
.note-resize { position: absolute; right: 0; bottom: 0; width: 22px; height: 22px; touch-action: none; cursor: nwse-resize; display: grid; place-items: center; color: rgba(0,0,0,0.35); font-size: 10px; font-weight: 700; }

/* Selection action bar */
.sel-bar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(64px + var(--sab)); z-index: 20;
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 999px;
  background: rgba(24,34,50,0.97); box-shadow: 0 6px 24px rgba(0,0,0,.45); border: 1px solid var(--hair);
}
.sel-label { font-size: 12px; color: var(--muted); }
.sel-pens { display: flex; gap: 4px; }
.sel-pens .pen { width: 32px; height: 32px; }
.sel-pens .pen i { width: 18px; height: 18px; }

/* Navigation bar */
.navbar {
  position: relative; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--panel); border-top: 1px solid var(--hair);
  padding: 8px calc(20px + var(--sar)) calc(8px + var(--sab)) 20px; color: rgba(255,255,255,0.88);
}
.chapter-jump { font-size: 12px; color: rgba(255,255,255,0.7); max-width: 220px; min-width: 0; flex: 1 1 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 8px 0; }
.chapter-jump.left { text-align: left; }
.chapter-jump.right { text-align: right; }
.page-nav { display: flex; align-items: center; gap: 10px; flex: none; }
.page-jump { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.page-jump span { color: var(--muted); }
.page-jump input { width: 50px; text-align: center; padding: 5px 4px; border-radius: 6px; border: 0; background: rgba(255,255,255,0.08); outline: none; font-size: 14px; }

.scrim { display: none; }
.only-mobile { display: none !important; }

/* ---------- Tablet / phone ---------- */
@media (max-width: 1100px) {
  .sheet-wrap { padding: 24px 32px 32px; }
}
@media (max-width: 860px) {
  .only-mobile { display: grid !important; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 50; width: min(86vw, 320px);
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: 8px 0 30px rgba(0,0,0,.4);
  }
  .app.drawer-open .sidebar { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 40; }
  .toolbar { padding: calc(6px + var(--sat)) calc(10px + var(--sar)) 8px calc(10px + var(--sal)); gap: 6px; }
  .tb-main { flex: 1 1 100%; gap: 8px; }
  .tb-tools { flex: 1 1 100%; justify-content: space-between; gap: 8px; }
  .search-box { display: none; flex: 1 1 100%; order: 3; }
  .toolbar.searching .search-box { display: flex; }
  .search-box input { width: auto; font-size: 16px; }
  .chapter-title { font-size: 15px; }
  .sheet-wrap { padding: 12px 10px 24px; }
  .sheet { padding: 22px 20px 20px; min-height: 70vh; }
  .page-text { font-size: 17.5px; line-height: 1.6; }
  .ribbon { right: 16px; }
  .navbar { padding: 6px calc(10px + var(--sar)) calc(6px + var(--sab)) calc(10px + var(--sal)); }
  .chapter-jump { font-size: 11px; max-width: none; }
  .page-jump input { font-size: 16px; width: 54px; }
  .icon-btn { width: 40px; height: 40px; }
  .pen { width: 32px; height: 32px; }
  .pen i { width: 17px; height: 17px; }
  .tool-btn { width: 38px; height: 32px; }
  .filter input { font-size: 16px; }
  .chapter-row { min-height: 40px; }
  .mark-row { min-height: 38px; }
}
@media (max-width: 380px) {
  .pen { width: 28px; height: 28px; }
  .tool-btn { width: 32px; }
  .chapter-jump { display: none; }
  .navbar { justify-content: center; }
}
@media (hover: none) {
  .chapter-row:hover, .mark-row:hover, .icon-btn:hover, .mini-btn:hover { background: none; }
  .chapter-row.selected { background: rgba(255,255,255,0.08); }
}
