/* ============================================================
   Life Mastery � main.css (Cleaned & Consolidated)
   Consolidated theme variables and fixed layout conflicts.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,600;1,400&family=Inter:wght@400;500;600&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,400;0,700;1,400&family=Roboto+Slab:wght@400;600&family=Nunito:wght@400;600&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;0,8..60,600;1,8..60,400&display=swap');

/* -- Custom Properties --------------------------------------- */
:root {
  --bg:           #fafaf8;
  --surface:      #ffffff;
  --surface-alt:  #f0ede8;
  --text:         #1a1714;
  --text-muted:   #6b6560;
  --border:       #e2dcd4;
  --accent:       #2c6e49;
  --accent-fg:    #ffffff;
  --accent-soft:  #e8f5ee;
  --shadow-sm:    0 1px 3px rgba(0,0,0,0.07), 0 2px 8px rgba(0,0,0,0.04);
  --shadow-md:    0 4px 16px rgba(0,0,0,0.10);
  --header-h:     52px;
  --toc-w:        256px;
  --reader-max:   68ch;
  --radius:       8px;
  --radius-sm:    5px;
  --ease:         220ms ease;
  --font-body:    'Lora', Georgia, 'Times New Roman', serif;
  --font-ui:      'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono:    'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  /* Reader typography (overridable by settings) */
  --rd-font-size:   18px;
  --rd-line-height: 1.8;
  --rd-font-body:   'Lora', Georgia, 'Times New Roman', serif;
}

[data-theme="sepia"] {
  --bg:          #f5ede0;
  --surface:     #fdf6ec;
  --surface-alt: #eee0cb;
  --text:        #3b2f1e;
  --text-muted:  #7a6650;
  --border:      #d9c9b2;
  --accent:      #8b5a2b;
  --accent-soft: #f2e2cc;
}

[data-theme="dark"] {
  --bg:          #191917;
  --surface:     #252421;
  --surface-alt: #302e2b;
  --text:        #e8e2d9;
  --text-muted:  #9a9490;
  --border:      #3a3835;
  --accent:      #4ade80;
  --accent-fg:   #0a1a0f;
  --accent-soft: #1a2e20;
  --shadow-sm:   0 1px 3px rgba(0,0,0,0.3), 0 2px 8px rgba(0,0,0,0.2);
  --shadow-md:   0 4px 20px rgba(0,0,0,0.4);
}

/* -- Reset --------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  transition: background var(--ease), color var(--ease);
  -webkit-font-smoothing: antialiased;
  overflow: hidden; 
}

a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
a:hover { opacity: 0.8; }
img { max-width: 100%; height: auto; }

/* -- Progress bar -------------------------------------------- */
#progress-bar {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 0;
  background: var(--accent);
  z-index: 300;
  transition: width 0.12s linear;
  border-radius: 0 2px 2px 0;
  pointer-events: none;
}

/* -- Header -------------------------------------------------- */
#app-header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  transition: background var(--ease), border-color var(--ease);
}

.hd-left, .hd-right {
  display: flex;
  align-items: center;
  gap: 6px;
}
.hd-left { min-width: 0; flex: 1; }
.hd-right { flex-shrink: 0; }

#hd-breadcrumb {
  font-size: 0.875rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bc-link { color: var(--text-muted); text-decoration: none; }
.bc-link:hover { color: var(--text); }
.bc-sep  { margin: 0 4px; opacity: 0.5; }
.bc-cur  { color: var(--text); font-weight: 500; }

/* -- Buttons ------------------------------------------------- */
.btn-primary {
  display: inline-flex;
  align-items: center;
  font-family: var(--font-ui);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 7px 14px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: opacity var(--ease);
  text-decoration: none;
  white-space: nowrap;
}
.btn-primary:hover { opacity: 0.85; }

.btn-secondary {
  font-family: var(--font-ui);
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 7px 14px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--ease), color var(--ease);
}
.btn-secondary:hover { background: var(--surface-alt); color: var(--text); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 6px;
  cursor: pointer;
  transition: background var(--ease), color var(--ease);
  flex-shrink: 0;
  text-decoration: none;
}
.icon-btn:hover { background: var(--surface-alt); color: var(--text); }

/* -- App layout ---------------------------------------------- */
#app-layout {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

/* -- TOC sidebar (Reader Context) ---------------------------- */
#toc-sidebar {
  display: none;
  flex-direction: column;
  width: var(--toc-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  background: var(--bg);
  transition: background var(--ease), border-color var(--ease);
}
#app-layout.has-toc #toc-sidebar { display: flex; }

.toc-header {
  padding: 18px 16px 8px;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  flex-shrink: 0;
}

#toc-nav { padding: 0 0 24px; }
.toc-link {
  display: block;
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 5px 16px;
  border-left: 2px solid transparent;
  transition: color var(--ease), border-color var(--ease), background var(--ease);
}
.toc-link:hover { color: var(--text); background: var(--surface-alt); }
.toc-link.is-active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-soft);
  font-weight: 500;
}
.toc-h3 { padding-left: 28px; font-size: 0.75rem; }

/* -- Main Area ----------------------------------------------- */
#app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-width: 0;
}

/* -- Library View --------------------------------------------- */
.library-container {
  display: flex;
  height: 100%;
}

.library-toc-sidebar {
  display: flex;
  flex-direction: column;
  width: var(--toc-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  background: var(--bg);
}

.lib-toc-list { list-style: none; }
.lib-toc-link {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 8px 16px;
  transition: background var(--ease), color var(--ease);
}
.lib-toc-link:hover { background: var(--surface-alt); color: var(--text); }
.lib-toc-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.library-wrap {
  flex: 1;
  padding: clamp(24px, 4vw, 48px) clamp(16px, 4vw, 48px);
  overflow-y: auto;
}

.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
  max-width: 1000px;
}

/* -- Book Card ----------------------------------------------- */
.book-card {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform var(--ease), box-shadow var(--ease);
}
.book-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.book-spine { width: 10px; background: var(--c, var(--accent)); }
.book-body { flex: 1; padding: 16px 14px; min-width: 0; }
.book-title { font-family: var(--font-body); font-size: 1rem; font-weight: 600; margin-bottom: 6px; }
.book-desc { font-size: 0.8125rem; color: var(--text-muted); line-height: 1.5; margin-bottom: 10px; }
.book-meta { font-size: 0.75rem; color: var(--text-muted); display: flex; gap: 10px; }

/* -- Book Detail View ---------------------------------------- */
.book-view { padding-bottom: 48px; }
.book-hero {
  display: flex; gap: 24px;
  padding: clamp(24px, 4vw, 40px) clamp(16px, 4vw, 48px);
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
}
.book-cover {
  width: 72px; min-height: 108px; flex-shrink: 0;
  background: var(--c, var(--accent));
  border-radius: 2px 6px 6px 2px;
  box-shadow: -3px 4px 10px rgba(0,0,0,0.2);
}
.bh-title { font-family: var(--font-body); font-size: 1.8rem; margin-bottom: 8px; }
.bh-desc { color: var(--text-muted); margin-bottom: 16px; }
.bh-stats { display: flex; gap: 16px; font-size: 0.875rem; margin-bottom: 20px; }

.chapter-list { list-style: none; padding: 0 clamp(16px, 4vw, 48px); }
.ch-item { border-bottom: 1px solid var(--border); }
.ch-link { display: flex; align-items: center; gap: 16px; padding: 14px 12px; text-decoration: none; color: var(--text); }
.ch-num { width: 28px; text-align: right; color: var(--text-muted); font-size: 0.8rem; }
.ch-title { font-weight: 500; flex: 1; }
.ch-meta { font-size: 0.75rem; color: var(--text-muted); }

/* -- Reader View --------------------------------------------- */
.chapter-view {
  max-width: var(--reader-max);
  margin: 0 auto;
  padding: 40px 24px 100px;
}
.cv-header { margin-bottom: 40px; border-bottom: 1px solid var(--border); padding-bottom: 24px; }
.cv-eyebrow { font-size: 0.75rem; font-weight: 600; color: var(--accent); text-transform: uppercase; margin-bottom: 8px; }
.cv-title { font-family: var(--rd-font-body); font-size: 2.2rem; margin-bottom: 12px; }
.cv-body { font-family: var(--rd-font-body); font-size: var(--rd-font-size); line-height: var(--rd-line-height); color: var(--text); }
.cv-body h2 { border-top: 1px solid var(--border); padding-top: 1.5rem; margin-top: 2.5rem; }

.ch-nav { display: flex; justify-content: space-between; margin-top: 60px; padding-top: 30px; border-top: 1px solid var(--border); }
.chn-btn { display: flex; flex-direction: column; padding: 16px; background: var(--surface-alt); border-radius: var(--radius); text-decoration: none; color: var(--text); width: 48%; }
.chn-label { font-size: 0.7rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; }

/* -- Mobile -------------------------------------------------- */
@media (max-width: 767px) {
  #toc-sidebar, .library-toc-sidebar { display: none !important; }
  .book-hero { flex-direction: column; align-items: center; text-align: center; }
  .chn-btn { width: 100%; }
  .ch-nav { flex-direction: column; }
}

/* ============================================================
   Reader Settings Panel
   ============================================================ */

/* -- Backdrop ------------------------------------------------ */
.settings-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 190;
  opacity: 0;
  transition: opacity var(--ease);
}
.settings-backdrop:not([hidden]) {
  opacity: 1;
}

/* -- Panel --------------------------------------------------- */
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 300px;
  max-width: 100vw;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  z-index: 200;
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: translateX(100%);
  transition: transform 280ms cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  padding-bottom: 24px;
}
.settings-panel.open {
  transform: translateX(0);
}

/* -- Panel Header -------------------------------------------- */
.sp-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.sp-title {
  font-family: var(--font-ui);
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
}
.sp-close {
  color: var(--text-muted);
}

/* -- Sections ----------------------------------------------- */
.sp-section {
  padding: 18px 16px 10px;
  border-bottom: 1px solid var(--border);
}
.sp-section:last-child {
  border-bottom: none;
}
.sp-label {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 12px;
}

/* -- Font Size Row ------------------------------------------- */
.sp-font-size-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.sp-size-btn {
  font-family: var(--font-ui);
  font-weight: 600;
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
  transition: background var(--ease), border-color var(--ease), opacity var(--ease);
  flex-shrink: 0;
  font-size: 0.9rem;
}
.sp-size-large {
  font-size: 1.1rem;
}
.sp-size-btn:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.sp-size-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}
.sp-size-label {
  flex: 1;
  text-align: center;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  color: var(--text);
  font-weight: 500;
}

/* -- Font Grid ---------------------------------------------- */
.sp-font-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.sp-font-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 8px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
  transition: background var(--ease), border-color var(--ease);
  min-width: 0;
}
.sp-font-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.sp-font-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
[data-theme="dark"] .sp-font-btn.active {
  color: var(--accent);
}
.sp-font-sample {
  font-size: 1.5rem;
  line-height: 1;
  font-weight: 400;
}
.sp-font-name {
  font-family: var(--font-ui);
  font-size: 0.7rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* -- Spacing Row -------------------------------------------- */
.sp-spacing-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.sp-spacing-btn {
  font-family: var(--font-ui);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 8px 4px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
  transition: background var(--ease), border-color var(--ease);
  text-align: center;
}
.sp-spacing-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.sp-spacing-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* -- Settings mobile ---------------------------------------- */
@media (max-width: 400px) {
  .settings-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    top: var(--header-h);
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .sp-font-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  .sp-spacing-row {
    grid-template-columns: repeat(2, 1fr);
  }
}

