:root {
  --bar-h: 60px;
  --ink: #2b2118;
  --paper: #f3e5bd;
  --accent: #a05c1e;
  --accent-soft: #e8b93c;
  --card: #ffffff;
  --line: #e3ddd2;
  --shadow: 0 6px 24px rgba(43, 33, 24, .18);
  font-size: 16px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: #f5efe0;
  overflow: hidden;
}
#app, #library-view, #reader-view { height: 100%; }
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ===================== LIBRARY ===================== */
#library-view {
  overflow-y: auto;
  padding: 0 0 48px;
  background:
    radial-gradient(1200px 500px at 20% -10%, #fbf6e9 0%, transparent 60%),
    #f5efe0;
}
.lib-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 20px 24px 8px; max-width: 1100px; margin: 0 auto;
}
.lib-title-wrap { display: flex; align-items: center; gap: 12px; }
.lib-logo { width: 44px; height: 44px; }
.lib-header h1 { font-size: 1.55rem; margin: 0; letter-spacing: .2px; }
.lib-hint { max-width: 1100px; margin: 6px auto 18px; padding: 0 24px; color: #7a6c58; font-size: .92rem; }
.storage-warning {
  max-width: 1052px; margin: 0 auto 14px; padding: 12px 16px;
  background: #fff4d6; border: 1px solid #ecd28a; border-radius: 12px; font-size: .92rem;
}
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  border: none; border-radius: 12px; padding: 12px 18px;
  font-size: 1rem; font-weight: 600;
}
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 3px 10px rgba(160,92,30,.35); }
.btn.primary:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--accent); border: 1.5px solid var(--accent); margin: 10px 16px 4px; }
.btn-ic { width: 18px; height: 18px; }

#book-grid {
  display: grid; gap: 20px; padding: 6px 24px;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  max-width: 1100px; margin: 0 auto;
}
.book-card {
  position: relative; background: var(--card); border-radius: 14px;
  box-shadow: 0 3px 12px rgba(43,33,24,.10); overflow: hidden;
  display: flex; flex-direction: column; border: 1px solid var(--line);
  text-align: left; padding: 0;
}
.book-card:active { transform: scale(.985); }
.book-thumb-wrap {
  background: #efe8d6; display: flex; align-items: center; justify-content: center;
  aspect-ratio: 3 / 4; overflow: hidden;
}
.book-thumb { width: 100%; height: 100%; object-fit: cover; object-position: top center; }
.book-meta { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.book-name {
  font-weight: 650; font-size: .98rem; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  word-break: break-word;
}
.book-progress-text { color: #7a6c58; font-size: .82rem; }
.book-progress { height: 5px; border-radius: 3px; background: #eee5d2; overflow: hidden; }
.book-progress > div { height: 100%; background: var(--accent-soft); border-radius: 3px; }
.book-del {
  position: absolute; top: 8px; right: 8px; width: 34px; height: 34px;
  border-radius: 50%; border: none; background: rgba(255,255,255,.92);
  color: #8c7a5f; font-size: 1rem; box-shadow: 0 1px 5px rgba(0,0,0,.18);
  display: flex; align-items: center; justify-content: center;
}
.lib-empty { text-align: center; color: #7a6c58; margin-top: 60px; line-height: 1.7; }

/* ===================== READER ===================== */
#reader-view { display: flex; flex-direction: column; background: #d9d4cb; }
#reader-bar {
  height: var(--bar-h); flex: none;
  display: flex; align-items: center; gap: 8px;
  background: #2b2118; color: #f5efe0; padding: 0 10px;
  z-index: 30; position: relative; box-shadow: 0 2px 10px rgba(0,0,0,.25);
}
#reader-title {
  flex: 1; min-width: 0; font-weight: 600; font-size: 1.02rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  width: 46px; height: 46px; flex: none; border: none; border-radius: 12px;
  background: transparent; color: inherit;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active { background: rgba(255,255,255,.14); }
.icon-btn.active { color: var(--accent-soft); }
.bar-actions { display: flex; align-items: center; gap: 2px; }
.page-ind {
  flex: none; border: 1px solid rgba(245,239,224,.35); color: inherit;
  background: transparent; border-radius: 10px; padding: 8px 12px; font-size: .95rem;
  font-variant-numeric: tabular-nums;
}

#page-scroll {
  flex: 1; overflow: auto; position: relative;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 22px 12px 40px;
  transition: background .25s ease;
}
#page-stage { display: flex; justify-content: center; min-width: min-content; perspective: 1800px; }
#page-wrap {
  position: relative; background: #fff; border-radius: 3px;
  box-shadow: var(--shadow); overflow: hidden; flex: none;
}
#page-canvas { display: block; }
#paper-tint {
  position: absolute; inset: 0; pointer-events: none;
  background: var(--paper); mix-blend-mode: multiply; opacity: .92;
  display: none;
}
#reader-view.paper { background: #e7dcc3; }
#reader-view.paper #paper-tint { display: block; }
#reader-view.paper #page-wrap { box-shadow: 0 6px 24px rgba(94, 74, 40, .30); }
#reader-view.paper #page-scroll { background: transparent; }
#text-layer { position: absolute; inset: 0; overflow: hidden; line-height: 1; }
#text-layer span, #text-layer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0 0; }
#text-layer ::selection { background: rgba(232, 185, 60, .45); color: transparent; }
#hl-layer { position: absolute; inset: 0; pointer-events: none; }
.hl-rect { position: absolute; background: rgba(255, 214, 64, .42); border-radius: 2px; mix-blend-mode: multiply; }
.hl-rect.note { background: rgba(120, 190, 255, .40); }
.note-dot {
  position: absolute; width: 24px; height: 24px; border-radius: 50%;
  background: #2b6cb0; color: #fff; display: flex; align-items: center; justify-content: center;
  pointer-events: auto; box-shadow: 0 1px 4px rgba(0,0,0,.35); transform: translate(-50%, -50%);
}
.note-dot svg { width: 14px; height: 14px; }
.page-note-flag {
  position: absolute; top: 8px; right: 8px; width: 28px; height: 28px; border-radius: 50%;
  background: #2b6cb0; color: #fff; display: flex; align-items: center; justify-content: center;
  pointer-events: auto; box-shadow: 0 1px 4px rgba(0,0,0,.35);
}
.page-note-flag svg { width: 15px; height: 15px; }

/* paper tint strength slider */
#tint-wrap {
  display: none;
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 26;
  background: rgba(43,33,24,.82); color: #f5efe0; border-radius: 16px;
  padding: 10px 18px 8px; backdrop-filter: blur(3px);
  flex-direction: column; align-items: center; gap: 2px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
#reader-view.paper #tint-wrap { display: flex; }
.tint-label { font-size: .82rem; font-weight: 600; letter-spacing: .2px; }
#tint-slider {
  -webkit-appearance: none; appearance: none;
  width: 240px; height: 34px; background: transparent; margin: 0;
}
#tint-slider::-webkit-slider-runnable-track {
  height: 8px; border-radius: 4px;
  background: linear-gradient(to right, #fdf9ee, #f3e5bd, #ecc95f);
}
#tint-slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 30px; height: 30px; border-radius: 50%; margin-top: -11px;
  background: #fff; border: 3px solid #e8b93c; box-shadow: 0 1px 5px rgba(0,0,0,.4);
}
#tint-slider::-moz-range-track {
  height: 8px; border-radius: 4px;
  background: linear-gradient(to right, #fdf9ee, #f3e5bd, #ecc95f);
}
#tint-slider::-moz-range-thumb {
  width: 26px; height: 26px; border-radius: 50%;
  background: #fff; border: 3px solid #e8b93c;
}
.tint-ends { display: flex; justify-content: space-between; width: 240px; font-size: .72rem; color: #d8cdb8; }

.swipe-hint {
  text-align: center; color: #8b8172; font-size: .85rem; padding: 14px 0 0;
}
#reader-view.paper .swipe-hint { color: #97835f; }

.turn-zone {
  position: absolute; bottom: 84px; z-index: 20;
  width: 54px; height: 54px; border-radius: 50%; border: none;
  background: rgba(43,33,24,.55); color: #f5efe0;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(2px);
}
.turn-zone svg { width: 26px; height: 26px; }
.turn-zone.left { left: 14px; }
.turn-zone.right { right: 14px; }
.turn-zone:active { background: rgba(43,33,24,.8); }

.flip-snap {
  position: absolute; inset: 0; z-index: 15;
  transform-style: preserve-3d; backface-visibility: hidden;
  box-shadow: var(--shadow); border-radius: 3px; background: #fff;
}
.flip-snap canvas { display: block; border-radius: 3px; }

/* selection toolbar */
#sel-toolbar {
  position: fixed; z-index: 60; display: flex; gap: 4px;
  background: #2b2118; color: #fff; border-radius: 12px; padding: 5px;
  box-shadow: 0 6px 20px rgba(0,0,0,.35);
}
.tb-btn {
  display: inline-flex; align-items: center; gap: 7px;
  border: none; background: transparent; color: #fff;
  padding: 10px 14px; font-size: .95rem; font-weight: 600; border-radius: 9px;
}
.tb-btn:active { background: rgba(255,255,255,.15); }
.tb-swatch { width: 15px; height: 15px; border-radius: 4px; background: #ffd640; display: inline-block; }
.tb-ic { width: 17px; height: 17px; }

/* bottom sheet */
#sheet-overlay { position: fixed; inset: 0; background: rgba(30,22,14,.45); z-index: 70; }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 71;
  max-height: 62vh; display: flex; flex-direction: column;
  background: #fbf8f1; border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.3);
}
@media (min-width: 900px) {
  #sheet { left: 50%; right: auto; transform: translateX(-50%); width: 640px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 0 16px; }
.sheet-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.sheet-tab {
  border: none; background: transparent; padding: 10px 12px; font-size: .95rem;
  color: #7a6c58; border-radius: 10px; font-weight: 600;
}
.sheet-tab.active { color: var(--ink); background: #efe6d2; }
#sheet .btn.ghost { align-self: flex-start; }
#sheet-list { overflow-y: auto; padding: 4px 10px 18px; }
.mark-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  border: none; background: #fff; border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; margin-top: 8px; text-align: left; color: var(--ink);
}
.mark-page {
  flex: none; background: #efe6d2; color: #6b5433; font-weight: 700;
  border-radius: 8px; padding: 5px 9px; font-size: .82rem; font-variant-numeric: tabular-nums;
}
.mark-icon { flex: none; width: 22px; height: 22px; color: #a05c1e; }
.mark-icon svg { width: 22px; height: 22px; }
.mark-text { flex: 1; min-width: 0; font-size: .93rem; line-height: 1.35; }
.mark-text .mark-note { color: #2b6cb0; display: block; margin-top: 2px; }
.mark-del { flex: none; border: none; background: transparent; color: #a29070; font-size: 1.05rem; width: 40px; height: 40px; border-radius: 10px; }
.mark-del:active { background: #f1e9d8; }
.sheet-empty { padding: 26px 22px 34px; color: #7a6c58; text-align: center; line-height: 1.6; }

/* dialog */
#dialog-overlay {
  position: fixed; inset: 0; z-index: 80; background: rgba(30,22,14,.5);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
#dialog {
  width: 100%; max-width: 520px; background: #fbf8f1; border-radius: 16px;
  padding: 18px; box-shadow: 0 14px 44px rgba(0,0,0,.4);
}
#dialog-title { font-weight: 700; font-size: 1.05rem; margin-bottom: 8px; }
#dialog-quote {
  font-size: .9rem; color: #6b5433; background: #f3ead6; border-left: 3px solid var(--accent-soft);
  padding: 8px 10px; border-radius: 0 8px 8px 0; margin-bottom: 10px;
  max-height: 110px; overflow-y: auto; line-height: 1.45;
}
#dialog-quote:empty { display: none; }
#dialog-note {
  width: 100%; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px;
  font: inherit; font-size: .95rem; resize: vertical; background: #fff; color: var(--ink);
}
#dialog-buttons { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
#dialog-buttons .btn { padding: 11px 16px; font-size: .95rem; }
#dialog-buttons .btn.primary { background: var(--accent); }
#dialog-buttons .btn.plain { background: #ece4d2; color: var(--ink); }
#dialog-buttons .btn.danger { background: transparent; color: #b3372f; margin-left: auto; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  z-index: 90; background: #2b2118; color: #f5efe0; padding: 12px 18px;
  border-radius: 12px; font-size: .95rem; box-shadow: 0 6px 20px rgba(0,0,0,.35);
  max-width: min(90vw, 480px); text-align: center;
}

@media (max-width: 560px) {
  .zoom-btn { display: none; }
  #reader-title { font-size: .95rem; }
}
