:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #f1effa;
  --text: #17123a;
  --text-2: #6b6590;
  --primary: #6d28d9;
  --primary-soft: #ede9fe;
  --accent: #f59e0b;
  --success: #10b981;
  --danger: #ef4444;
  --radius: 18px;
  --shadow: 0 8px 30px rgba(23, 18, 58, .08);
  --nav-h: 64px;
}

[data-theme="dark"] {
  --bg: #12101f;
  --surface: #1c1930;
  --surface-2: #262242;
  --text: #efedfb;
  --text-2: #9d97c2;
  --primary: #a78bfa;
  --primary-soft: #2e2752;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: 'Plus Jakarta Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  transition: background .25s ease, color .25s ease;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; }
img { display: block; }

.view {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--nav-h) + 28px);
  animation: fadein .25s ease;
}

@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.hidden { display: none !important; }

/* ---------- Auth ---------- */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 32px 8px;
}

.auth-hero {
  text-align: center;
  margin-bottom: 28px;
}

.auth-hero .logo {
  width: 72px; height: 72px;
  margin: 0 auto 16px;
  border-radius: 22px;
  background: linear-gradient(135deg, #7c3aed, #db2777);
  display: flex; align-items: center; justify-content: center;
  font-size: 34px;
  box-shadow: var(--shadow);
}

.auth-hero h1 { font-size: 26px; font-weight: 800; letter-spacing: -.5px; }
.auth-hero p { color: var(--text-2); margin-top: 6px; font-size: 14.5px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
}

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1.5px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
}
.field input:focus { border-color: var(--primary); }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
  padding: 14px;
  border-radius: 14px;
  font-size: 15px;
  font-weight: 700;
  transition: transform .1s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: linear-gradient(135deg, #7c3aed, #a855f7); color: #fff; }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn:disabled { opacity: .6; }

.auth-switch { text-align: center; margin-top: 16px; font-size: 14px; color: var(--text-2); }
.auth-switch a { color: var(--primary); font-weight: 700; text-decoration: none; }

.form-error {
  background: #fee2e2; color: #b91c1c;
  border-radius: 10px; padding: 10px 12px;
  font-size: 13.5px; margin-bottom: 14px;
}
[data-theme="dark"] .form-error { background: #451a1a; color: #fca5a5; }

/* ---------- Header ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 0 16px; }
.page-head h1 { font-size: 22px; font-weight: 800; letter-spacing: -.4px; }
.page-head .sub { color: var(--text-2); font-size: 13.5px; margin-top: 2px; }

.icon-btn {
  width: 42px; height: 42px;
  border-radius: 13px;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}

/* ---------- Search & chips ---------- */
.search-bar {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}
.search-bar input {
  flex: 1; border: none; outline: none; background: transparent;
  font-size: 15px; color: var(--text);
}
.search-bar .s-icon { color: var(--text-2); }

.chips {
  display: flex; gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 14px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  padding: 8px 15px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: var(--shadow);
  white-space: nowrap;
}
.chip.active { background: var(--primary); color: #fff; }

/* ---------- Book grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}
@media (min-width: 480px) { .grid { grid-template-columns: repeat(3, 1fr); } }

.book-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  text-align: left;
  display: flex; flex-direction: column;
  position: relative;
  transition: transform .12s ease;
}
.book-card:active { transform: scale(.97); }

.book-cover { position: relative; aspect-ratio: 3/4; background: var(--surface-2); }
.book-cover img { width: 100%; height: 100%; object-fit: cover; }

.badge {
  position: absolute; top: 8px; left: 8px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11px; font-weight: 700;
  color: #fff;
}
.badge.reading { background: var(--accent); }
.badge.finished { background: var(--success); }

.fav-dot {
  position: absolute; top: 8px; right: 8px;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: rgba(0,0,0,.45);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
}

.book-info { padding: 10px 12px 12px; }
.book-info .t { font-size: 13.5px; font-weight: 700; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-info .a { font-size: 12px; color: var(--text-2); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.load-more { margin-top: 18px; }

.empty-state { text-align: center; padding: 48px 20px; color: var(--text-2); }
.empty-state .big { font-size: 40px; margin-bottom: 10px; }

.skeleton { border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.skeleton .sk-cover { aspect-ratio: 3/4; }
.sk-shimmer {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Reco ---------- */
.reco-card {
  display: flex; gap: 14px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  text-align: left;
  width: 100%;
}
.reco-card img { width: 74px; height: 100px; object-fit: cover; border-radius: 10px; flex: 0 0 auto; background: var(--surface-2); }
.reco-card .t { font-weight: 700; font-size: 14.5px; line-height: 1.3; }
.reco-card .a { font-size: 12.5px; color: var(--text-2); margin-top: 2px; }
.reco-reason {
  display: inline-block;
  margin-top: 8px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 12px; font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
}

.ai-banner {
  background: linear-gradient(135deg, #7c3aed, #db2777);
  color: #fff;
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.ai-banner h2 { font-size: 17px; font-weight: 800; }
.ai-banner p { font-size: 13px; opacity: .9; margin-top: 4px; }

/* ---------- Detail ---------- */
.detail-hero { display: flex; gap: 16px; margin-bottom: 18px; }
.detail-hero img { width: 118px; height: 160px; object-fit: cover; border-radius: 14px; box-shadow: var(--shadow); background: var(--surface-2); }
.detail-hero h1 { font-size: 19px; font-weight: 800; line-height: 1.25; letter-spacing: -.3px; }
.detail-hero .author { color: var(--text-2); font-size: 14px; margin-top: 5px; }
.detail-cats { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.cat-tag { background: var(--primary-soft); color: var(--primary); font-size: 11.5px; font-weight: 600; padding: 4px 9px; border-radius: 999px; }

.action-row { display: grid; grid-template-columns: 1fr 1fr 46px; gap: 10px; margin-bottom: 18px; }
.action-row .btn { padding: 12px 8px; font-size: 13.5px; border-radius: 13px; }
.btn-status-active { background: var(--success) !important; color: #fff !important; }
.btn-reading-active { background: var(--accent) !important; color: #fff !important; }
.fav-btn { border-radius: 13px; background: var(--surface); box-shadow: var(--shadow); font-size: 18px; display:flex; align-items:center; justify-content:center; }
.fav-btn.active { background: #fda4af33; color: #e11d48; }

/* ---------- TTS bar ---------- */
.tts-bar {
  position: sticky; top: 8px; z-index: 5;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 16px;
}
.tts-controls { display: flex; align-items: center; gap: 12px; }
.tts-play {
  width: 46px; height: 46px; flex: 0 0 auto;
  border-radius: 50%;
  background: linear-gradient(135deg, #7c3aed, #a855f7);
  color: #fff; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
}
.tts-meta { flex: 1; min-width: 0; }
.tts-meta .l1 { font-size: 13px; font-weight: 700; }
.tts-meta .l2 { font-size: 11.5px; color: var(--text-2); margin-top: 1px; }
.tts-rate { background: var(--surface-2); border-radius: 999px; padding: 7px 12px; font-size: 12.5px; font-weight: 700; }
.tts-progress { height: 4px; border-radius: 4px; background: var(--surface-2); margin-top: 10px; overflow: hidden; }
.tts-progress .fill { height: 100%; width: 0; background: var(--primary); transition: width .3s linear; }

/* ---------- Reader ---------- */
.reader {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 18px;
  box-shadow: var(--shadow);
  font-size: 15.5px;
  line-height: 1.75;
}
.reader h2, .reader h3 { margin: 22px 0 10px; line-height: 1.35; font-size: 18px; letter-spacing: -.2px; }
.reader p { margin-bottom: 13px; }
.reader img { max-width: 100%; height: auto; border-radius: 12px; margin: 10px 0; }
.reader .photo { text-align: center; }
.reader mark.tts-hl { background: var(--primary-soft); color: inherit; border-radius: 3px; }

.back-btn { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 14px; font-weight: 600; margin-bottom: 14px; }

/* ---------- Profile ---------- */
.profile-head { text-align: center; padding: 18px 0 22px; }
.avatar {
  width: 84px; height: 84px;
  margin: 0 auto 12px;
  border-radius: 50%;
  background: linear-gradient(135deg, #7c3aed, #db2777);
  color: #fff; font-size: 32px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.profile-head h1 { font-size: 20px; font-weight: 800; }
.profile-head p { color: var(--text-2); font-size: 13.5px; margin-top: 3px; }

.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 18px; }
.stat-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 10px; text-align: center; }
.stat-card .n { font-size: 22px; font-weight: 800; color: var(--primary); }
.stat-card .l { font-size: 12px; color: var(--text-2); margin-top: 3px; }

.menu-list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.menu-item {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; padding: 16px;
  font-size: 14.5px; font-weight: 600;
  border-bottom: 1px solid var(--surface-2);
  text-align: left;
}
.menu-item:last-child { border-bottom: none; }
.menu-item .danger { color: var(--danger); }

/* ---------- Tabs (library) ---------- */
.seg {
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-radius: 14px;
  padding: 4px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.seg button {
  padding: 9px 4px;
  font-size: 12.5px; font-weight: 700;
  border-radius: 11px;
  color: var(--text-2);
}
.seg button.active { background: var(--primary); color: #fff; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  box-shadow: 0 -4px 24px rgba(23, 18, 58, .10);
  display: grid; grid-template-columns: repeat(4, 1fr);
  z-index: 20;
  max-width: 640px;
  margin: 0 auto;
  border-radius: 20px 20px 0 0;
}
.nav-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 10.5px; font-weight: 700;
  color: var(--text-2);
}
.nav-btn.active { color: var(--primary); }
.nav-icon { font-size: 20px; line-height: 1; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 18px);
  left: 50%; transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 13.5px; font-weight: 600;
  z-index: 50;
  box-shadow: var(--shadow);
  animation: fadein .2s ease;
  max-width: 86vw;
  text-align: center;
}
