:root {
  --paper: #f6f1e7;
  --paper-2: #efe7d8;
  --card: #fffdf8;
  --ink: #1d2a26;
  --ink-2: #55615c;
  --line: #ddd3c1;
  --green: #1f4d43;
  --green-2: #2c6a5c;
  --clay: #d9774a;
  --clay-2: #b85f36;
  --danger: #b3372b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(29, 42, 38, .06), 0 6px 20px rgba(29, 42, 38, .08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
h1, h2, h3, .serif { font-family: Fraunces, Georgia, "Times New Roman", serif; font-weight: 700; letter-spacing: -.01em; }

.splash { min-height: 100vh; display: grid; place-items: center; color: var(--ink-2); }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 999px; border: 1px solid transparent;
  background: var(--green); color: #fff; font-weight: 600; white-space: nowrap;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--green-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.clay { background: var(--clay); }
.btn.clay:hover { background: var(--clay-2); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--paper-2); }
.btn.danger { background: var(--danger); }
.btn.small { min-height: 36px; padding: 0 14px; font-size: 14px; }
.icon-btn {
  width: 44px; height: 44px; display: inline-grid; place-items: center; flex: none;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink);
}
.icon-btn:hover { background: var(--paper-2); }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.icon-btn svg, .btn svg { width: 20px; height: 20px; }
.link { background: none; border: 0; padding: 0; color: var(--green); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }

label.field { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
.input {
  width: 100%; min-height: 46px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: #fff; font-size: 16px; /* 16px stops iOS zoom */
}
.input:focus { outline: 2px solid var(--green-2); outline-offset: 1px; border-color: transparent; }
.error { color: var(--danger); font-size: 14px; min-height: 1.2em; margin: 0; }

/* ---------- auth ---------- */
.auth {
  min-height: 100vh; display: grid; place-items: center;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
  background:
    radial-gradient(circle at 15% 10%, rgba(217, 119, 74, .18), transparent 40%),
    radial-gradient(circle at 90% 90%, rgba(31, 77, 67, .16), transparent 45%),
    var(--paper);
}
.auth-card { width: min(420px, 100%); background: var(--card); border-radius: 22px; padding: 28px 24px; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.brand img { width: 44px; height: 44px; }
.brand h1 { margin: 0; font-size: 28px; }
.brand p { margin: 0; color: var(--ink-2); font-size: 14px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--paper-2); border-radius: 999px; padding: 4px; margin-bottom: 18px; }
.tabs button { border: 0; background: transparent; border-radius: 999px; min-height: 40px; font-weight: 600; color: var(--ink-2); }
.tabs button[aria-selected="true"] { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.auth form { display: grid; gap: 14px; }
.auth .fine { color: var(--ink-2); font-size: 13px; margin: 16px 0 0; }

/* ---------- library ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: rgba(246, 241, 231, .92); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar .brand { margin: 0; flex: 1; min-width: 0; }
.topbar .brand img { width: 34px; height: 34px; }
.topbar .brand h1 { font-size: 22px; white-space: nowrap; }
@media (max-width: 420px) { .topbar .add-label { display: none; } .topbar .btn.clay { padding: 0 14px; } }
.library { max-width: 1180px; margin: 0 auto; padding: 22px 16px calc(40px + var(--safe-bottom)); }
.lib-head { display: flex; align-items: end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.lib-head h2 { margin: 0; font-size: 30px; }
.lib-head p { margin: 2px 0 0; color: var(--ink-2); }
.search { max-width: 320px; }
.grid { display: grid; gap: 18px; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
@media (max-width: 520px) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; } .lib-head h2 { font-size: 26px; } }

.book { position: relative; display: flex; flex-direction: column; gap: 8px; }
.cover {
  position: relative; aspect-ratio: 3 / 4; border-radius: 6px 12px 12px 6px; overflow: hidden;
  border: 0; padding: 16px 14px 14px 20px; text-align: left; color: #fff;
  display: flex; flex-direction: column; justify-content: space-between;
  box-shadow: inset 6px 0 0 rgba(0,0,0,.18), inset 7px 0 0 rgba(255,255,255,.12), var(--shadow);
  background-image: linear-gradient(145deg, rgba(255,255,255,.14), rgba(0,0,0,.18));
  transition: transform .15s;
}
.cover:hover { transform: translateY(-3px); }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: top; }
.cover.has-img { justify-content: flex-end; box-shadow: var(--shadow); padding: 0; }
.cover.has-img .title { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.cover.has-img .meta { position: relative; padding: 28px 12px 10px; background: linear-gradient(transparent, rgba(0,0,0,.72)); }
.cover .title { font-family: Fraunces, Georgia, serif; font-weight: 700; font-size: 18px; line-height: 1.2; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; text-shadow: 0 1px 2px rgba(0,0,0,.25); }
.cover .meta { display: block; font-size: 12px; opacity: .95; }
.progress { display: block; height: 4px; background: rgba(255,255,255,.3); border-radius: 4px; overflow: hidden; margin-top: 6px; }
.progress > i { display: block; height: 100%; background: #fff; }
.book-row { display: flex; align-items: center; gap: 4px; }
.book-row .when { flex: 1; font-size: 12px; color: var(--ink-2); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-row .icon-btn { width: 36px; height: 36px; }

.empty { text-align: center; padding: 60px 16px; border: 2px dashed var(--line); border-radius: 20px; color: var(--ink-2); }
.empty h3 { color: var(--ink); font-size: 22px; margin: 0 0 6px; }

.uploading { display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; box-shadow: var(--shadow); }
.uploading .bar { flex: 1; height: 6px; background: var(--paper-2); border-radius: 6px; overflow: hidden; }
.uploading .bar i { display: block; height: 100%; background: var(--clay); transition: width .2s; }

/* ---------- sheet / dialog ---------- */
.scrim { position: fixed; inset: 0; background: rgba(20, 28, 25, .45); z-index: 50; display: grid; place-items: end center; }
@media (min-width: 640px) { .scrim { place-items: center; } }
.sheet {
  width: min(480px, 100%); max-height: 90vh; overflow: auto; background: var(--card);
  border-radius: 22px 22px 0 0; padding: 20px 18px calc(20px + var(--safe-bottom));
  animation: up .2s ease-out;
}
@media (min-width: 640px) { .sheet { border-radius: 22px; } }
@keyframes up { from { transform: translateY(24px); opacity: 0; } }
.sheet h3 { margin: 0 0 14px; font-size: 22px; }
.sheet .stack { display: grid; gap: 16px; }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { width: 38px; height: 38px; border-radius: 50%; border: 3px solid transparent; box-shadow: inset 0 0 0 1px rgba(0,0,0,.1); }
.swatch[aria-pressed="true"] { border-color: var(--ink); }
.swatch.custom { position: relative; overflow: hidden; background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); }
.swatch.custom input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.muted { color: var(--ink-2); font-size: 14px; }

/* ---------- reader ---------- */
.reader { position: fixed; inset: 0; z-index: 40; display: grid; grid-template-rows: auto 1fr; background: #e9e3d6; }
.rbar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(6px + var(--safe-top)) 8px 6px;
  background: var(--card); border-bottom: 1px solid var(--line); min-width: 0;
}
.rbar .rtitle { flex: 1; min-width: 0; padding: 0 6px; }
.rbar .rtitle b { display: block; font-family: Fraunces, Georgia, serif; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rbar .rtitle span { display: block; font-size: 12px; color: var(--ink-2); white-space: nowrap; }
.pagebox { display: flex; align-items: center; gap: 4px; font-size: 14px; color: var(--ink-2); }
.pagebox input { width: 58px; min-height: 36px; padding: 4px 6px; text-align: center; border-radius: 8px; border: 1px solid var(--line); font-size: 16px; }
.divider { width: 1px; height: 26px; background: var(--line); margin: 0 4px; }
@media (max-width: 700px) { .hide-sm { display: none !important; } }

.rbody { position: relative; display: grid; grid-template-columns: auto 1fr; min-height: 0; }

.marks {
  width: 280px; display: flex; flex-direction: column; min-height: 0;
  background: var(--card); border-right: 1px solid var(--line);
}
.marks.closed { display: none; }
.marks-head { padding: 14px 14px 10px; border-bottom: 1px solid var(--line); }
.marks-head h3 { margin: 0; font-size: 18px; display: flex; align-items: center; justify-content: space-between; }
.marks-head p { margin: 2px 0 10px; font-size: 13px; color: var(--ink-2); }
.marks-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.marks-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 8px; margin: 0; list-style: none; }
.mark { display: flex; align-items: center; gap: 6px; border-radius: 10px; padding: 4px 4px 4px 10px; border: 1px solid transparent; }
.mark:hover { background: var(--paper); }
.mark.here { background: #fbeee6; border-color: #f0c9b2; }
.mark .go { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 6px 0; }
.mark .go b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mark .go span { font-size: 12px; color: var(--ink-2); }
.mark .icon-btn { width: 34px; height: 34px; color: var(--ink-2); }
.mark .icon-btn svg { width: 17px; height: 17px; }
.mark input.input { min-height: 38px; padding: 6px 10px; }
.marks-empty { padding: 18px 12px; color: var(--ink-2); font-size: 14px; text-align: center; }
.marks-foot { padding: 10px 14px calc(10px + var(--safe-bottom)); border-top: 1px solid var(--line); font-size: 12px; color: var(--ink-2); }

@media (max-width: 860px) {
  .rbody { grid-template-columns: 1fr; }
  .marks {
    position: absolute; z-index: 30; top: 0; bottom: 0; left: 0; width: min(320px, 86vw);
    box-shadow: 8px 0 30px rgba(0,0,0,.18); animation: slide .18s ease-out;
  }
  .marks-scrim { position: absolute; inset: 0; z-index: 29; background: rgba(20,28,25,.3); }
}
@keyframes slide { from { transform: translateX(-24px); opacity: 0; } }

.viewer { position: relative; overflow: auto; -webkit-overflow-scrolling: touch; min-height: 0; touch-action: pan-x pan-y; }
.pages { position: relative; margin: 0 auto; padding: 16px 0 calc(40px + var(--safe-bottom)); }
.page {
  position: relative; margin: 0 auto 14px; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 6px 18px rgba(0,0,0,.06);
}
.page canvas { display: block; width: 100%; height: 100%; }
.page canvas ~ .pnum { display: none; }
.page .pnum { position: absolute; top: 50%; left: 0; right: 0; text-align: center; color: #b8b0a0; font-size: 13px; }
.page .flag { position: absolute; top: -2px; right: 18px; width: 22px; height: 30px; background: var(--clay); clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 75%, 0 100%); }
.status { position: absolute; left: 50%; bottom: calc(14px + var(--safe-bottom)); transform: translateX(-50%); background: rgba(29,42,38,.85); color: #fff; padding: 6px 12px; border-radius: 999px; font-size: 12px; pointer-events: none; z-index: 10; }
.loading-book { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-2); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translateX(-50%); z-index: 100;
  display: flex; align-items: center; gap: 14px;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px); font-size: 14px;
}
.toast button { background: none; border: 0; color: #f6b08a; font-weight: 700; padding: 4px; }
@media (max-width: 520px) {
  .rbar .rtitle > * { display: none; }
  .rbar .icon-btn { width: 40px; }
}
