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

/* ── Theme variables ────────────────────────────────────── */
:root {
  --bg:         #f5e6d3;
  --bg2:        #ffffff;
  --text:       #3d2914;
  --text2:      #654321;
  --accent:     #8B4513;
  --accent2:    #654321;
  --border:     rgba(0,0,0,0.08);
  --header-bg:  linear-gradient(135deg, #8B4513 0%, #654321 100%);
  --card-bg:    #ffffff;
  --tab-bg:     rgba(249,245,240,0.98);
  --tab-color:  #8e8e93;
  --tab-active: #8B4513;
}

body.theme-sepia {
  --bg:         #f0e6d0;
  --bg2:        #f9f0e0;
  --text:       #3d2914;
  --text2:      #654321;
  --accent:     #8B4513;
  --accent2:    #6b4020;
  --border:     rgba(61,41,20,0.1);
  --card-bg:    #f9f0e0;
  --tab-bg:     rgba(240,230,208,0.94);
  --tab-color:  #9a7a5a;
  --tab-active: #8B4513;
}

body.theme-dark {
  --bg:         #1a1a1a;
  --bg2:        #242424;
  --text:       #e0e0e0;
  --text2:      #b0a090;
  --accent:     #c9a66b;
  --accent2:    #a07840;
  --border:     rgba(255,255,255,0.08);
  --header-bg:  linear-gradient(135deg, #2a1a0a 0%, #1a1005 100%);
  --card-bg:    #242424;
  --tab-bg:     rgba(26,26,26,0.94);
  --tab-color:  #888;
  --tab-active: #c9a66b;
}

html {
  height: 100%;
  width: 100%;
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  transition: background 0.3s, color 0.3s;
}

header {
  background: var(--header-bg);
  color: white;
  text-align: center;
  padding: 2rem 1rem;
  padding-top: calc(2rem + env(safe-area-inset-top));
}

header h1 {
  font-size: 1.8rem;
  font-weight: normal;
  margin-bottom: 0.3rem;
}

.subtitle {
  opacity: 0.9;
  font-size: 1rem;
}

main {
  padding: 1.5rem;
  max-width: 600px;
  margin: 0 auto;
  padding-bottom: calc(80px + env(safe-area-inset-bottom));
}

section {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: background 0.3s;
}

.bookshelf {
  background: url('/images/books-bg.png') center/cover no-repeat;
  position: relative;
}

.bookshelf::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--card-bg);
  opacity: 0.85;
  border-radius: 12px;
}

.bookshelf > * {
  position: relative;
  z-index: 1;
}

.welcome {
  text-align: center;
}

.tibetan-text {
  font-size: 1.5rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

h2 {
  font-size: 1.2rem;
  margin-bottom: 1rem;
  color: var(--text2);
}

.placeholder {
  color: #999;
  font-style: italic;
  text-align: center;
  padding: 2rem;
}

.install-prompt {
  background: #8B4513;
  color: white;
  text-align: center;
}

.install-prompt p {
  margin-bottom: 1rem;
}

/* Banner: "Add to Home Screen" for app-like full screen (no browser bar) */
.install-banner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: #654321;
  color: #f5e6d3;
  padding: 0.75rem 1rem;
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  margin: 0 -1.5rem -1rem;
  border-radius: 0 0 12px 12px;
  position: relative;
}

.install-banner-text {
  font-size: 0.85rem;
  line-height: 1.35;
  margin: 0;
  flex: 1;
}

.install-banner-dismiss {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255,255,255,0.2);
  color: inherit;
  font-size: 1.25rem;
  line-height: 1;
  border-radius: 50%;
  cursor: pointer;
  padding: 0;
}

#installAddBtn, .install-banner-add {
  flex-shrink: 0;
  background: rgba(255,255,255,0.25);
  color: inherit;
  border: 1px solid rgba(255,255,255,0.35);
  padding: 0.45rem 0.7rem;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
  font-weight: 700;
}

#installAddBtn:active, .install-banner-add:active {
  transform: translateY(1px);
}

/* iOS "Add to Home Screen" instruction modal */
.install-howto-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 2001;
  padding: 16px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.install-howto-card {
  width: 100%;
  max-width: 520px;
  max-height: min(88vh, 640px);
  background: var(--card-bg);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  padding: 1rem 1rem calc(1.1rem + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.install-howto-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.install-howto-header h3 {
  font-size: 1rem;
  color: var(--accent);
}

.install-howto-close {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(0,0,0,0.03);
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--text2);
}

.install-howto-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin-top: 0.5rem;
  padding-right: 2px;
}

.install-howto-intro {
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--text);
  margin-bottom: 1rem;
}

.install-howto-step {
  display: flex;
  gap: 0.65rem;
  margin-bottom: 1.1rem;
  align-items: flex-start;
}

.install-howto-step-num {
  flex-shrink: 0;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 0.15rem;
}

.install-howto-step-main {
  flex: 1;
  min-width: 0;
}

.install-howto-illustration {
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg2);
  border: 1px solid var(--border);
  margin-bottom: 0.45rem;
}

.install-howto-svg {
  display: block;
  width: 100%;
  height: auto;
}

.install-howto-step-title {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
  margin: 0 0 0.25rem;
  line-height: 1.35;
}

.install-howto-step-body {
  font-size: 0.86rem;
  line-height: 1.55;
  color: var(--text2);
  margin: 0;
}

.install-howto-apple-link {
  display: inline-block;
  margin-top: 0.25rem;
  margin-bottom: 0.35rem;
  font-size: 0.86rem;
  color: var(--accent);
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-word;
}

.install-howto-note {
  flex-shrink: 0;
  margin-top: 0.6rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  color: var(--text2);
  opacity: 0.85;
  font-size: 0.8rem;
  line-height: 1.45;
}

#installBtn {
  background: white;
  color: #8B4513;
  border: none;
  padding: 0.8rem 2rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
}

footer {
  text-align: center;
  padding: 1rem;
  color: #666;
  font-size: 0.9rem;
}

/* Books grid for later */
.books {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 1rem;
}

.book-card {
  display: block;
  background: linear-gradient(135deg, #8B4513 0%, #654321 100%);
  border-radius: 8px;
  padding: 1rem;
  text-align: center;
  cursor: pointer;
  transition: transform 0.2s;
  text-decoration: none;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.book-card:hover, .book-card:active {
  transform: scale(1.02);
}

.book-cover {
  font-size: 3rem;
  margin-bottom: 0.5rem;
}

.book-title-text {
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 0.3rem;
  line-height: 1.3;
}

.book-author {
  font-size: 0.75rem;
  opacity: 0.8;
}

/* iOS PWA: paint tab-bar colour below the bar to cover the safe-area gap */
@media (display-mode: standalone) {
  body::after {
    content: '';
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50px;
    background: var(--tab-bg);
    z-index: 999;
  }
}

/* iOS-style bottom tab bar */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  justify-content: space-around;
  background: var(--tab-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--border);
  padding: 8px 0;
  padding-bottom: 12px;
  transition: background 0.3s;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-decoration: none;
  color: var(--tab-color);
  font-size: 10px;
  padding: 4px 12px;
  min-width: 64px;
  transition: color 0.2s;
}

.tab-item.active {
  color: var(--tab-active);
}

.tab-icon {
  font-size: 24px;
  margin-bottom: 2px;
}

.tab-label {
  font-weight: 500;
}

/* Hide debug footer in standalone mode */
.debug-footer {
  text-align: center;
  padding: 0.5rem;
  color: #999;
  font-size: 0.75rem;
  margin-bottom: 80px;
}

@media (display-mode: standalone) {
  .debug-footer {
    display: none;
  }
  .install-banner {
    display: none !important;
  }
}

/* ── Search tab ─────────────────────────────────────────── */
.search-input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1.5px solid #c9a87a;
  border-radius: 10px;
  font-size: 1rem;
  outline: none;
  background: #fdf8f4;
  color: #3d2914;
  margin-top: 0.5rem;
}
.search-input:focus {
  border-color: #8B4513;
}

.search-result-group {
  background: white;
  border-radius: 12px;
  padding: 1rem 1.5rem;
  margin-bottom: 0.75rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.search-result-group h2 {
  font-size: 0.95rem;
  color: #8B4513;
  margin-bottom: 0.5rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid #e8d5c4;
}
.search-result-item {
  display: block;
  padding: 0.5rem 0;
  border-bottom: 1px solid #f0e8de;
  text-decoration: none;
  color: inherit;
}
.search-result-item:last-of-type { border-bottom: none; }
.result-chapter {
  font-weight: 600;
  font-size: 0.9rem;
  color: #654321;
  margin-bottom: 0.2rem;
}
.result-snippet {
  font-size: 0.82rem;
  color: #7a6050;
  line-height: 1.5;
}
.result-more {
  font-size: 0.8rem;
  color: #aaa;
  text-align: center;
  padding-top: 0.4rem;
}

/* ── Reading History tab ────────────────────────────────── */
.history-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid #f0e8de;
  text-decoration: none;
  color: inherit;
}
.history-card:last-child { border-bottom: none; }
.history-icon { font-size: 2rem; }
.history-title {
  font-weight: 600;
  font-size: 0.95rem;
  color: #3d2914;
  margin-bottom: 0.2rem;
}
.history-meta { font-size: 0.8rem; color: #8a7060; }
.empty-state {
  text-align: center;
  color: #aaa;
  padding: 2rem 0;
  line-height: 1.8;
}

/* ── Language selector ──────────────────────────────────── */
.lang-selector {
  display: flex;
  gap: 0.4rem;
}
.lang-btn {
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  border: 1.5px solid #c9a87a;
  background: none;
  color: #654321;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.lang-btn.active {
  background: #8B4513;
  border-color: #8B4513;
  color: white;
}

/* ── Settings tab ───────────────────────────────────────── */
.setting-row-main {
  margin-bottom: 1.25rem;
}
.setting-row-main label {
  display: block;
  font-size: 0.9rem;
  color: #654321;
  margin-bottom: 0.4rem;
  font-weight: 500;
}
.setting-row-main input[type=range] {
  width: 100%;
  accent-color: #8B4513;
}
.theme-selector {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.25rem;
}
.theme-opt {
  flex: 1;
  padding: 0.5rem;
  border-radius: 8px;
  border: 2px solid transparent;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: border-color 0.15s;
}
.theme-opt.light { background: #fff; color: #333; border-color: #ddd; }
.theme-opt.sepia { background: #f5ead0; color: #5c4a2a; border-color: #d4b896; }
.theme-opt.dark { background: #1a1a2e; color: #e0e0e0; border-color: #444; }
.theme-opt.active { border-color: #8B4513 !important; }

/* ── Settings sub-pages ─────────────────────────────────── */
.settings-list {
  margin: 0.5rem -1.5rem -1.5rem;
}
.settings-row {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 0.9rem 1.5rem;
  background: none;
  border: none;
  border-top: 0.5px solid var(--border);
  cursor: pointer;
  text-align: left;
  color: var(--text);
}
.settings-row:first-child { border-top: none; }
.settings-row-label { flex: 1; font-size: 1rem; }
.settings-row-chevron { color: var(--text2); opacity: 0.4; font-size: 1.2rem; }
.settings-back {
  display: block;
  padding: 0.5rem 0 0.75rem;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 1rem;
  cursor: pointer;
}

/* ── Series sections ──────────────────────────────────────────── */
.series-section {
  grid-column: 1 / -1;
  background: var(--card, var(--bg2, #f5f0e8));
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  margin-bottom: 0.25rem;
}
.series-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  cursor: pointer;
  user-select: none;
}
.series-header:hover, .series-header:active {
  background: rgba(0,0,0,0.05);
}
.series-cover-thumb {
  width: 48px;
  height: 64px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}
.series-cover-placeholder {
  font-size: 2rem;
  width: 48px;
  text-align: center;
  flex-shrink: 0;
}
.series-meta {
  flex: 1;
  min-width: 0;
}
.series-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
  margin-bottom: 0.2rem;
}
.series-badge {
  font-size: 0.75rem;
  background: var(--accent, #8B4513);
  color: white;
  border-radius: 10px;
  padding: 0.1rem 0.5rem;
}
.series-chevron {
  font-size: 1.4rem;
  color: var(--text2);
  opacity: 0.5;
  flex-shrink: 0;
}
.series-volumes {
  border-top: 1px solid var(--border, rgba(0,0,0,0.1));
}
.volume-row {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  padding: 0.6rem 1rem;
  text-decoration: none;
  color: var(--text);
  border-bottom: 0.5px solid var(--border, rgba(0,0,0,0.06));
}
.volume-row:last-child { border-bottom: none; }
.volume-row:hover, .volume-row:active {
  background: rgba(0,0,0,0.05);
}
.volume-num {
  font-size: 0.7rem;
  color: var(--accent, #8B4513);
  font-weight: 600;
  flex-shrink: 0;
  min-width: 3rem;
}
.volume-title {
  flex: 1;
  font-size: 0.85rem;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.volume-author {
  font-size: 0.72rem;
  color: var(--text2);
  flex-shrink: 0;
  max-width: 8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Book card cover image ───────────────────────────────────── */
.book-cover-img {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 0.5rem;
  display: block;
}
