/* ============================================
   The Case Against You — Styles
   ============================================ */

/* --- Fonts --- */
@font-face {
  font-family: 'Crimson Pro';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('../fonts/CrimsonPro-Regular.woff2') format('woff2');
}

@font-face {
  font-family: 'Crimson Pro';
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/CrimsonPro-Italic.woff2') format('woff2');
}

/* --- Custom Properties --- */
:root {
  --font-body: 'Crimson Pro', Georgia, 'Times New Roman', serif;
  --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  --fs-small: 1.05rem;
  --fs-medium: 1.2rem;
  --fs-large: 1.38rem;
  --fs-body: var(--fs-medium);

  --lh: 1.65;
  --content-width: 680px;
  --sidebar-width: 300px;

  /* Light theme (default) */
  --bg: #faf8f5;
  --bg-secondary: #f0ede8;
  --text: #2c2a26;
  --text-secondary: #6b6760;
  --text-muted: #9a948c;
  --border: #ddd8d0;
  --accent: #8B2020;
  --link: #8B2020;
  --link-hover: #6a1818;
  --hr: #d4cfc7;
  --nav-highlight: #f5edee;
  --nav-active: #8B2020;
}

/* Dark theme */
[data-theme="dark"] {
  --bg: #181a1c;
  --bg-secondary: #22252a;
  --text: #d4d6da;
  --text-secondary: #92969c;
  --text-muted: #62666c;
  --border: #32363c;
  --accent: #c45050;
  --link: #c45050;
  --link-hover: #d97070;
  --hr: #32363c;
  --nav-highlight: #2a1f1f;
  --nav-active: #c45050;
}

/* System theme — use media query */
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #181a1c;
    --bg-secondary: #22252a;
    --text: #d4d6da;
    --text-secondary: #92969c;
    --text-muted: #62666c;
    --border: #32363c;
    --accent: #c45050;
    --link: #c45050;
    --link-hover: #d97070;
    --hr: #32363c;
    --nav-highlight: #2a1f1f;
    --nav-active: #c45050;
  }
}

/* Font sizes */
[data-fontsize="small"] { --fs-body: var(--fs-small); }
[data-fontsize="medium"] { --fs-body: var(--fs-medium); }
[data-fontsize="large"] { --fs-body: var(--fs-large); }

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
  /* scroll-padding-top handled by JS scrollToId */
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-body);
  font-size: var(--fs-body);
  line-height: var(--lh);
  color: var(--text);
  background: var(--bg);
  transition: background-color 0.3s ease, color 0.3s ease;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Reading Progress Bar --- */
.reading-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: var(--accent);
  transition: width 0.1s linear;
}

/* --- Homepage: hide header bar, float controls --- */
.is-home .site-header {
  border-bottom: none;
  background: transparent;
  position: absolute;
  top: 0;
  right: 0;
  left: auto;
  width: auto;
  padding: 1rem 1.5rem;
}

.is-home .site-title {
  display: none;
}

/* --- Site Header --- */
.site-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 100;
  transition: background-color 0.3s ease;
}

.site-header a {
  text-decoration: none;
  color: var(--text);
}

.site-title {
  font-family: var(--font-body);
  font-size: 1.1rem;
  font-weight: 400;
  letter-spacing: 0.02em;
}

.site-title:hover {
  color: var(--accent);
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.control-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-family: var(--font-ui);
  font-size: 0.75rem;
  padding: 0.3rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.control-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.control-btn.active {
  border-color: var(--accent);
  color: var(--accent);
}

/* --- Homepage --- */
.home-container {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 4rem 1.5rem 3rem;
}

.home-title {
  font-family: var(--font-body);
  font-size: 2.4rem;
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: 0.08em;
  margin-bottom: 1rem;
  color: var(--text);
}

.home-title::after {
  content: '';
  display: block;
  width: 3em;
  height: 2px;
  background: var(--accent);
  margin: 0.8rem auto 0;
}

.home-subtitle {
  font-family: var(--font-body);
  font-size: 1.15rem;
  font-weight: 400;
  font-style: italic;
  text-align: center;
  color: var(--text-secondary);
  margin-bottom: 2.5rem;
}

.home-hero {
  font-size: var(--fs-body);
  color: var(--text-secondary);
  text-align: left;
  line-height: var(--lh);
  margin-bottom: 2.5rem;
}

.home-hero p {
  margin-bottom: 1em;
}

.home-hero p:last-child {
  margin-bottom: 0;
}

.home-actions {
  text-align: center;
  margin-bottom: 3rem;
  font-size: var(--fs-body);
}

.home-actions a {
  color: var(--link);
  text-decoration: none;
}

.home-actions a:hover {
  color: var(--link-hover);
}

.action-separator {
  color: var(--text-muted);
  margin: 0 0.75em;
}

.home-author {
  font-size: var(--fs-body);
  color: var(--text-muted);
  text-align: left;
  line-height: var(--lh);
  border-top: 1px solid var(--bg-secondary);
  padding-top: 2rem;
}

.toc a {
  text-decoration: none;
  color: var(--text);
  transition: color 0.2s ease;
}

.toc a:hover {
  color: var(--accent);
}

.toc-appendix-heading {
  font-size: 0.8em;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-top: 1.5rem;
  margin-bottom: 0.25rem;
}

.toc-appendix a {
  padding-left: 1rem;
}

/* --- Chapter Layout --- */
.chapter-layout {
  display: flex;
  min-height: calc(100vh - 60px);
}

/* Section navbar / sidebar */
.section-nav {
  width: var(--sidebar-width);
  flex-shrink: 0;
  padding: 2rem 1rem 2rem 1.5rem;
  position: sticky;
  top: 60px;
  height: calc(100vh - 60px);
  overflow-y: auto;
  border-right: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  line-height: 1.5;
  scrollbar-width: thin;
}

.section-nav::-webkit-scrollbar {
  width: 4px;
}

.section-nav::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.nav-chapter-link {
  display: block;
  padding: 0.35rem 0.5rem;
  margin-bottom: 0.15rem;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: 3px;
  transition: all 0.15s ease;
}

.nav-chapter-link:hover {
  color: var(--text);
  background: var(--nav-highlight);
}

.nav-chapter-link.nav-current {
  color: var(--text);
  font-weight: 600;
}

.nav-chapter-link.nav-subsection {
  padding-left: 1.2rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.nav-chapter-link.nav-subsection:hover {
  color: var(--text-secondary);
}

.nav-chapter-link.active {
  color: var(--nav-active);
  font-weight: 600;
}

.nav-divider {
  height: 1px;
  background: var(--border);
  margin: 0.75rem 0;
}

.nav-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  padding: 0.35rem 0.5rem;
  margin-top: 0.25rem;
}

.nav-prev-next {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.nav-prev-next a {
  display: block;
  padding: 0.35rem 0.5rem;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.78rem;
  border-radius: 3px;
}

.nav-prev-next a:hover {
  color: var(--text);
  background: var(--nav-highlight);
}

/* Mobile nav toggle */
.nav-toggle {
  display: none;
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 1.2rem;
  cursor: pointer;
  z-index: 200;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.2s ease;
}

.nav-toggle:hover {
  border-color: var(--accent);
  color: var(--text);
}

/* Chapter content area */
.chapter-content {
  flex: 1;
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 3rem 1.5rem 4rem;
}

/* --- Typography (chapter content) --- */
.chapter-content h1 {
  font-family: var(--font-body);
  font-size: 2rem;
  font-weight: 400;
  text-align: center;
  margin-bottom: 2.5rem;
  letter-spacing: 0.02em;
  color: var(--text);
}

.chapter-number {
  display: block;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  text-align: center;
}

.chapter-content h2 {
  font-family: var(--font-body);
  font-size: 1.5em;
  font-weight: 700;
  text-align: center;
  margin-top: 2em;
  margin-bottom: 0.3em;
  color: var(--text);
}

.chapter-content h2 + hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin-top: 0.5em;
  margin-bottom: 1.5em;
}

.chapter-content h3 {
  font-family: var(--font-body);
  font-size: 1.35em;
  font-weight: 400;
  font-style: italic;
  margin-top: 2.5rem;
  margin-bottom: 1.2rem;
  color: var(--text);
  position: sticky;
  top: 62px;
  background: var(--bg);
  padding: 0.4rem 1rem;
  margin-left: -1rem;
  margin-right: -1rem;
  border-bottom: 1px solid var(--border);
  z-index: 10;
  transition: background-color 0.3s ease;
}

.chapter-content p {
  margin-top: 0;
  margin-bottom: 0;
  text-indent: 1.5em;
}

.chapter-content a {
  color: var(--link);
  text-decoration: none;
}

.chapter-content a:hover {
  color: var(--link-hover);
}

/* No indent on first paragraph after a heading or hr */
.chapter-content h1 + p,
.chapter-content h2 + p,
.chapter-content h3 + p,
.chapter-content hr + p {
  text-indent: 0;
}

/* Endmatter pages: no indent, space between paragraphs */
.chapter-content.is-endmatter p {
  text-indent: 0;
  margin-top: 0.6em;
  margin-bottom: 0.6em;
}

/* Appendix pages: no paragraph indent, space between consecutive paragraphs */
.chapter-content.is-appendix p {
  text-indent: 0;
}

.chapter-content.is-appendix p + p {
  margin-top: 0.8rem;
}

.chapter-content em {
  font-style: italic;
}

.chapter-content strong {
  font-weight: 700;
}

.chapter-content hr {
  border: none;
  height: 1px;
  background: var(--hr);
  margin: 2rem 0;
}

.chapter-content ul {
  list-style: disc;
  margin: 0.8rem 0;
  padding-left: 2em;
}

.chapter-content ol {
  list-style: decimal;
  margin: 0.8rem 0;
  padding-left: 2em;
}

.chapter-content ul ul,
.chapter-content ol ol {
  margin: 0.2rem 0;
}

.chapter-content li {
  margin-bottom: 0.3rem;
  text-indent: 0;
}

/* --- Chapter Navigation (bottom) --- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4rem;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
}

.chapter-nav a {
  color: var(--text-secondary);
  text-decoration: none;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  transition: color 0.2s ease;
}

.chapter-nav a:hover {
  color: var(--accent);
}

.chapter-nav .prev::before {
  content: '\2190\00a0';
}

.chapter-nav .next::after {
  content: '\00a0\2192';
}

/* --- Footer --- */
.site-footer {
  text-align: left;
  padding: 0;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

.site-footer p {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

/* Footer inside chapter pages — inherits content offset */
.chapter-footer {
  margin-top: 4rem;
  border-top: none;
}

.chapter-footer p {
  max-width: none;
  padding: 2rem 0;
  text-indent: 0;
  margin: 0;
}

.site-footer a {
  color: var(--text-muted);
  text-decoration: none;
}

.site-footer a:hover {
  color: var(--text-secondary);
}

/* --- About Page --- */
.about-container {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 3rem 1.5rem 4rem;
}

.about-container h1 {
  font-family: var(--font-body);
  font-size: 2rem;
  font-weight: 400;
  text-align: center;
  margin-bottom: 2rem;
}

.about-container p {
  margin-bottom: 1.2rem;
  color: var(--text);
}

.about-container a {
  color: var(--link);
}

.about-container a:hover {
  color: var(--link-hover);
}

/* --- Loading State --- */
.loading {
  text-align: center;
  color: var(--text-muted);
  padding: 4rem 0;
  font-style: italic;
}

/* --- Responsive --- */
@media (max-width: 900px) {
  .section-nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    height: 100vh;
    padding-top: 1.5rem;
    background: var(--bg);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    z-index: 150;
    box-shadow: 2px 0 12px rgba(0,0,0,0.1);
  }

  .section-nav.open {
    transform: translateX(0);
  }

  .nav-toggle {
    display: flex;
  }

  .nav-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
    z-index: 140;
  }

  .nav-overlay.open {
    display: block;
  }

  .chapter-content {
    padding: 2rem 1.25rem 3rem;
  }
}

@media (max-width: 600px) {
  .home-title {
    font-size: 1.6rem;
  }

  .home-subtitle {
    font-size: 1rem;
  }

  .home-container {
    padding: 2.5rem 1.25rem 2rem;
  }

  .home-actions {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5em;
  }

  .action-separator {
    display: none;
  }


  .chapter-content h1 {
    font-size: 1.6rem;
  }

  .site-header {
    padding: 0.75rem 1rem;
  }

  .site-title {
    font-size: 0.95rem;
  }

  .control-btn {
    font-size: 0.7rem;
    padding: 0.25rem 0.5rem;
  }

  .chapter-content h3 {
    top: 44px;
  }
}

/* --- Print --- */
@media print {
  .site-header,
  .section-nav,
  .nav-toggle,
  .header-controls,
  .chapter-nav {
    display: none;
  }

  body {
    font-size: 11pt;
    color: #000;
    background: #fff;
  }

  .chapter-content {
    max-width: 100%;
    padding: 0;
  }
}
