:root {
  --bg: #fff8ee;
  --surface: #ffffff;
  --text: #2b2118;
  --muted: #8a7a6a;
  --accent: #f26b3a;
  --accent-soft: #ffe2d4;
  --line: #f0e3d2;
  --shadow: 0 6px 18px rgba(80, 50, 20, .12);
  --font: "Baloo 2", system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1914;
    --surface: #2a241d;
    --text: #f3ead9;
    --muted: #a8998a;
    --accent: #ff8a5c;
    --accent-soft: #4a2f22;
    --line: #3a3128;
    --shadow: 0 6px 18px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
/* App chrome isn't text: no I-beam cursor or text caret on titles, buttons, bars.
   (Book text lives in the reader's iframes and stays selectable.) */
body { -webkit-user-select: none; user-select: none; cursor: default; -webkit-tap-highlight-color: transparent; }
input { -webkit-user-select: text; user-select: text; cursor: text; }
button, a { cursor: pointer; }
button:disabled { cursor: default; }
button:focus:not(:focus-visible), a:focus:not(:focus-visible) { outline: none; }

/* ---------- Password gate ---------- */
.gate { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.gate-card {
  width: min(360px, 100%);
  background: var(--surface);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 32px 24px;
  text-align: center;
  display: grid;
  gap: 12px;
}
.gate-emoji { font-size: 56px; line-height: 1; }
.gate-card h1 { margin: 0; font-size: 32px; }
.gate-card p { margin: 0; color: var(--muted); }
.gate-card input {
  padding: 14px 16px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  font-size: 18px;
  text-align: center;
}
.gate-card input:focus { outline: none; border-color: var(--accent); }
.gate-card button {
  padding: 12px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
}
.gate-error { color: var(--accent) !important; }

/* ---------- Library ---------- */
#app { max-width: 1200px; margin: 0 auto; padding: 0 16px 48px; }
.top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px 0 8px;
  flex-wrap: wrap;
}
.brand { margin: 0; font-size: 34px; flex: 1 0 auto; white-space: nowrap; }
.brand a { color: inherit; text-decoration: none; }
#search {
  flex: 1 1 220px;
  max-width: 360px;
  padding: 10px 16px;
  border-radius: 999px;
  border: 2px solid var(--line);
  background: var(--surface);
  font-size: 16px;
}
#search:focus { outline: none; border-color: var(--accent); }

.shelf h2 { font-size: 24px; margin: 24px 0 12px; }
.shelf h2 small { color: var(--muted); font-size: 16px; font-weight: 500; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 20px 16px;
}
#continue .row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 130px;
  gap: 16px;
  overflow-x: auto;
  padding-bottom: 8px;
  scroll-snap-type: x mandatory;
}
#continue .card { scroll-snap-align: start; }

.card { color: inherit; text-decoration: none; display: block; min-width: 0; }
.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: transform .15s ease;
}
.card:hover .cover, .card:focus-visible .cover { transform: translateY(-4px) rotate(-1deg); }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.fallback {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 12px;
  text-align: center;
  font-weight: 700;
  font-size: 18px;
  line-height: 1.2;
  color: #fff;
  background: linear-gradient(150deg, hsl(var(--h) 70% 58%), hsl(calc(var(--h) + 40) 65% 45%));
}
.badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.progress { position: absolute; left: 0; right: 0; bottom: 0; height: 6px; background: rgba(0, 0, 0, .25); }
.progress i { display: block; height: 100%; background: var(--accent); }
.card .title {
  margin-top: 8px;
  font-weight: 700;
  line-height: 1.2;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card .author { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { color: var(--muted); text-align: center; margin-top: 64px; }

/* ---------- Reader ---------- */
body.reader {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  overflow: hidden;
}
.reader-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.reader-title { flex: 1; min-width: 0; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.font-controls { display: flex; gap: 4px; }
.icon-btn {
  min-width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--text);
  text-decoration: none;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
}
.icon-btn:disabled { opacity: .4; cursor: default; }
.text-btn { padding: 0 12px; font-size: 15px; white-space: nowrap; }
.reader-body { position: relative; display: flex; min-height: 0; }
.view-host { flex: 1; min-width: 0; position: relative; }
foliate-view { display: block; width: 100%; height: 100%; }
.status { text-align: center; color: var(--muted); margin-top: 30vh; }
.status p { margin: 12px 0 0; }
.spinner {
  width: 44px;
  height: 44px;
  margin: 0 auto;
  border-radius: 50%;
  border: 4px solid var(--accent-soft);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
.spinner.overlay {
  position: absolute;
  inset: 0;
  margin: auto;
  z-index: 5;
  pointer-events: none;
  box-shadow: 0 0 0 10px var(--surface), var(--shadow);
  background: var(--surface);
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
.turn {
  flex: 0 0 48px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 44px;
  cursor: pointer;
}
.turn:hover { color: var(--accent); background: var(--accent-soft); }
@media (max-width: 600px) {
  .turn { flex-basis: 28px; font-size: 32px; }
  #search { max-width: none; }
}
.reader-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 14px;
}
.reader-foot .bar { flex: 1; height: 6px; border-radius: 999px; background: var(--line); overflow: hidden; }
.reader-foot .bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
