:root {
  --bg: #fbf6f1;
  --paper: #fffdfa;
  --text: #2b2024;
  --muted: #8a7479;
  --line: #ecdfe0;
  --accent: #c2185b;
  --accent-soft: #fbe4ec;
  --shadow: 0 6px 24px rgba(120, 40, 70, 0.08);
  --font-size: 19px;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f1e7d6;
  --paper: #f8efdf;
  --text: #3d2f22;
  --muted: #8b7861;
  --line: #e2d3b8;
  --accent: #a0522d;
  --accent-soft: #efdcc4;
}
:root[data-theme="dark"] {
  --bg: #141013;
  --paper: #1d171b;
  --text: #ece2e5;
  --muted: #a08f95;
  --line: #33282d;
  --accent: #f06292;
  --accent-soft: #3a1f2a;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Sinhala", system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }

.wrap { max-width: 720px; margin: 0 auto; padding: 0 16px 80px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar .title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer;
  min-width: 40px; height: 40px; border-radius: 20px; font-size: 18px;
}
.icon-btn:hover { background: var(--accent-soft); }

.cover {
  margin: 28px 0 20px; padding: 32px 24px; text-align: center;
  background: linear-gradient(160deg, var(--accent-soft), var(--paper));
  border-radius: 20px; box-shadow: var(--shadow);
}
.cover .heart { font-size: 34px; }
.cover h1 { font-family: "Playfair Display", "Noto Serif Sinhala", Georgia, serif; font-size: 34px; line-height: 1.2; margin: 8px 0 4px; }
.cover .author { color: var(--muted); }
.cover .desc { margin: 14px auto 0; max-width: 520px; white-space: pre-line; }
.cover .start { margin-top: 18px; }

.btn {
  display: inline-block; border: 0; cursor: pointer; text-decoration: none;
  background: var(--accent); color: #fff; padding: 10px 20px; border-radius: 24px; font-weight: 600;
}
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.btn.small { padding: 6px 14px; font-size: 14px; }
.btn.danger { background: #c62828; }
.btn:disabled { opacity: 0.5; cursor: default; }

.list { list-style: none; padding: 0; margin: 0; background: var(--paper); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.list a, .list .row {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; color: inherit; text-decoration: none;
}
.list a:hover { background: var(--accent-soft); }
.num { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 28px; }
.grow { flex: 1; min-width: 0; }
.sub { color: var(--muted); font-size: 13px; }
.badge { background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; }
.badge.grey { background: var(--muted); }
.read-mark { color: var(--muted); font-size: 13px; }

.reader { background: var(--paper); border-radius: 16px; box-shadow: var(--shadow); padding: 28px 22px; margin-top: 20px; }
.reader h2 { font-family: "Playfair Display", "Noto Serif Sinhala", Georgia, serif; text-align: center; margin: 0 0 4px; font-size: 28px; line-height: 1.25; }
.reader .chapter-no { text-align: center; color: var(--accent); font-size: 13px; letter-spacing: 0.15em; text-transform: uppercase; }
.reader .divider { text-align: center; color: var(--accent); margin: 14px 0 22px; }
.reader .body { font-family: "Noto Serif Sinhala", Georgia, "Times New Roman", serif; font-size: var(--font-size); line-height: 1.85; }
.reader .body p { margin: 0 0 1em; }
.nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 24px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--accent); color: #fff; padding: 12px 20px; border-radius: 24px;
  box-shadow: 0 6px 24px rgba(0,0,0,.2); cursor: pointer; z-index: 20; max-width: calc(100% - 32px);
}
.sheet {
  position: fixed; inset: auto 0 0 0; z-index: 15; background: var(--paper); border-top: 1px solid var(--line);
  padding: 16px; box-shadow: 0 -8px 24px rgba(0,0,0,.1);
}
.sheet .row { display: flex; align-items: center; gap: 10px; margin: 8px auto; max-width: 720px; flex-wrap: wrap; }
.sheet .row > span { min-width: 90px; color: var(--muted); }
.chip { border: 1px solid var(--line); background: var(--bg); border-radius: 18px; padding: 6px 14px; cursor: pointer; }
.chip.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.progress { position: fixed; top: 0; left: 0; height: 3px; background: var(--accent); z-index: 10; width: 0; }

/* Admin */
.card { background: var(--paper); border-radius: 16px; box-shadow: var(--shadow); padding: 18px; margin-top: 18px; }
.card h3 { margin: 0 0 12px; }
label { display: block; margin: 10px 0 4px; font-size: 14px; color: var(--muted); }
input[type="text"], input[type="password"], textarea {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px;
}
textarea { min-height: 120px; resize: vertical; line-height: 1.6; }
textarea.editor { min-height: 55vh; font-family: "Noto Serif Sinhala", Georgia, serif; font-size: 17px; }
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.error { color: #c62828; margin-top: 10px; }
.ok { color: #2e7d32; }
.switch { display: inline-flex; align-items: center; gap: 8px; color: var(--text); margin: 0; cursor: pointer; }

/* Library */
.continue {
  display: flex; align-items: center; gap: 12px; margin-top: 18px; padding: 12px 14px;
  background: var(--paper); border-radius: 16px; box-shadow: var(--shadow); color: inherit; text-decoration: none;
}
.continue .go { color: var(--accent); font-size: 20px; }
.chips { display: flex; gap: 8px; overflow-x: auto; margin: 18px -16px 0; padding: 2px 16px 6px; scrollbar-width: none; }
.chips .chip { white-space: nowrap; }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 18px 14px; margin-top: 18px; }
.book-card { position: relative; color: inherit; text-decoration: none; min-width: 0; }
.book-card .book-title { font-weight: 600; margin-top: 8px; line-height: 1.3; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.badge.corner { position: absolute; top: 8px; right: 8px; }

.cover-img {
  display: block; width: 100%; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 10px;
  box-shadow: 0 8px 20px rgba(60, 20, 40, 0.18), inset 0 0 0 1px rgba(0,0,0,.04); background: var(--accent-soft);
}
.cover-img.drawn {
  display: grid; place-items: center; padding: 14px; text-align: center; color: #fff;
  background: linear-gradient(160deg, hsl(var(--hue) 55% 52%), hsl(calc(var(--hue) + 40) 60% 30%));
  font-family: "Playfair Display", "Noto Serif Sinhala", Georgia, serif; font-size: 17px; line-height: 1.25;
}
.cover-img.drawn span { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; }
.cover-img.big { width: 130px; flex: none; }
.cover-img.tiny { width: 38px; flex: none; border-radius: 5px; font-size: 0; padding: 0; }
.book-head { display: flex; gap: 18px; align-items: flex-end; margin-top: 24px; }
.book-head h1 { font-family: "Playfair Display", "Noto Serif Sinhala", Georgia, serif; font-size: 26px; line-height: 1.2; margin: 4px 0; }
.book-head .author { color: var(--muted); }
.book-head .start { margin-top: 12px; }
.desc { white-space: pre-line; margin: 18px 0; }
.book-head + .list, .desc + .list { margin-top: 18px; }

/* Likes + comments */
.like-row { display: flex; align-items: center; gap: 12px; margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--line); }
.like {
  border: 1px solid var(--accent); background: transparent; color: var(--accent); border-radius: 24px;
  padding: 6px 16px; font-size: 18px; cursor: pointer; display: inline-flex; gap: 6px; align-items: center;
}
.like.on { background: var(--accent); color: #fff; }
.like.on .heart-icon { animation: pop .3s ease; }
@keyframes pop { 50% { transform: scale(1.4); } }
.comments h3 { display: flex; gap: 8px; align-items: baseline; }
.comments form { display: grid; gap: 8px; }
.comments textarea { min-height: 70px; }
.comment { padding: 12px 0; border-top: 1px solid var(--line); }
.comment > div { white-space: pre-line; margin-top: 2px; }

/* Install bar */
.install {
  position: fixed; left: 12px; right: 12px; bottom: 12px; z-index: 12; max-width: 560px; margin: 0 auto;
  display: flex; align-items: center; gap: 10px; padding: 10px 10px 10px 12px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 10px 30px rgba(0,0,0,.18);
  line-height: 1.35;
}
.install img { border-radius: 10px; }
.share-icon { vertical-align: -3px; color: #0a84ff; }

/* Author stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin-top: 18px; }
.stat { background: var(--paper); border-radius: 14px; box-shadow: var(--shadow); padding: 14px; }
.stat b { display: block; font-size: 24px; color: var(--accent); }
.mini-stats { color: var(--muted); font-size: 13px; white-space: nowrap; }
.order-btns { display: flex; flex-direction: column; }
.order-btns .icon-btn { height: 24px; min-width: 32px; font-size: 13px; }
.list .row input[type="checkbox"] { width: 18px; height: 18px; }
.bulk { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
.cover-edit { display: flex; gap: 16px; align-items: flex-start; }
.cover-edit .cover-img { width: 110px; flex: none; }

[hidden] { display: none !important; }
.list .row a.grow { display: block; padding: 0; }
.list .row a.grow:hover { background: none; text-decoration: underline; }

/* Welcome screen */
.splash {
  position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 32px 24px; text-align: center; color: #fff;
  background: radial-gradient(circle at 50% 35%, #e2457f 0%, #a3134b 55%, #5c0a2b 100%);
  transition: opacity .4s ease;
}
.splash img { border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,.3); }
.splash > :not(img) { opacity: 0; transform: translateY(8px); transition: opacity .5s ease, transform .5s ease; }
.splash.ready > :not(img) { opacity: 1; transform: none; }
.splash-name { font-family: "Playfair Display", "Noto Serif Sinhala", Georgia, serif; font-size: 30px; font-weight: 600; }
.splash-welcome { font-family: "Noto Serif Sinhala", Georgia, serif; font-size: 19px; line-height: 1.7; max-width: 340px; white-space: pre-line; }
.splash-phone { color: #ffd6e2; font-size: 13px; text-decoration: none; letter-spacing: .03em; }
.splash .btn { background: #fff; color: #a3134b; margin-top: 10px; }
.splash.gone { opacity: 0; pointer-events: none; }
.footer { text-align: center; color: var(--muted); font-size: 13px; margin: 36px 0 0; white-space: pre-line; }
.footer a { color: var(--muted); text-decoration: none; }
.splash-owner { font-family: "Playfair Display", Georgia, serif; font-size: 18px; font-style: italic; color: #ffe3ec; }
.splash-owner:empty { display: none; }
.reader .body p.scene-break { text-align: center; color: var(--accent); margin: 1.4em 0; letter-spacing: .3em; }
