/* Design system: warm charcoal, cream, gold — warmer feel with transitioning gradients */
/* Inter = UI/headings, Lora = reading content */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Lora:ital,wght@0,400;0,600;0,700;1,400&family=Merriweather:ital,wght@0,400;0,700;1,400&display=swap');

:root {
  --charcoal: #1a1814;
  --charcoal-deep: #141210;
  --off-white: #e8e4dc;
  --gold: #c9a227;
  --gold-hover: #b8921f;
  --gold-muted: rgba(201, 162, 39, 0.18);
  --warm-1: #2c261e;
  --warm-2: #1f1c17;
  --warm-3: #251f18;
  --warm-glow: rgba(201, 162, 39, 0.08);
  /* Safe areas for notched / rounded devices */
  --safe-top: env(safe-area-inset-top, 0);
  --safe-right: env(safe-area-inset-right, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-left: env(safe-area-inset-left, 0);
}

* { box-sizing: border-box; }

html {
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  /* App-like: use dynamic viewport on mobile */
  min-height: 100dvh;
  min-height: -webkit-fill-available;
}

body {
  font-family: 'Inter', system-ui, sans-serif;
  background-color: var(--charcoal);
  color: var(--off-white);
  margin: 0;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom);
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

/* Transitioning warm gradients (layered, no image) */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -2;
  background: linear-gradient(
    165deg,
    var(--charcoal-deep) 0%,
    var(--warm-2) 25%,
    var(--warm-3) 50%,
    var(--charcoal) 75%,
    var(--warm-1) 100%
  );
  background-size: 100% 200%;
  animation: gradient-shift 18s ease-in-out infinite alternate;
}

body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: radial-gradient(
    ellipse 120% 80% at 50% -20%,
    var(--warm-glow) 0%,
    transparent 50%
  );
  pointer-events: none;
}

@keyframes gradient-shift {
  0% { background-position: 0% 0%; }
  100% { background-position: 0% 100%; }
}

/* Sticky header: respect safe area at top (notched devices) */
header:first-of-type {
  padding-top: var(--safe-top);
}

main { flex: 1; }

a { color: inherit; text-decoration: none; }
a:hover { color: var(--gold); }

/* Skip link: visible when focused (keyboard/screen reader users) */
.skip-link {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 200;
  padding: 0.75rem 1.25rem;
  background: var(--gold);
  color: var(--charcoal);
  font-weight: 600;
  text-decoration: none;
  border-radius: 0 0 0.375rem 0;
  transform: translateY(-100%);
  transition: transform 0.2s ease;
}
.skip-link:focus {
  transform: translateY(0);
  outline: 2px solid var(--off-white);
  outline-offset: 2px;
}

/* Visible focus for keyboard users (don’t rely on color alone) */
a:focus-visible,
button:focus-visible,
summary:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* Respect reduced motion — no shifting gradient */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .skip-link { transition: none; }
  body::before {
    animation: none;
    background-position: 0% 50%;
  }
}

/* Buttons */
.btn-primary {
  display: inline-block;
  background: var(--gold);
  color: var(--charcoal);
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border-radius: 0.375rem;
  border: none;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.2s;
}
.btn-primary:hover { background: var(--gold-hover); }

.btn-secondary {
  display: inline-block;
  background: transparent;
  color: var(--gold);
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border-radius: 0.375rem;
  border: 2px solid var(--gold);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.2s;
}
.btn-secondary:hover { background: var(--gold-muted); }

/* Form inputs */
.input-field {
  width: 100%;
  background: var(--charcoal);
  border: 1px solid rgba(232, 228, 220, 0.3);
  color: var(--off-white);
  padding: 0.75rem 1rem;
  border-radius: 0.375rem;
  font-family: inherit;
}
.input-field::placeholder { color: rgba(232, 228, 220, 0.5); }
.input-field:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 2px var(--gold-muted);
}

/* Reader content */
.reader-content {
  font-family: 'Lora', Georgia, serif;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.8;
}

/* ========== Reader settings: theme, font size, font ========== */
.reader-settings-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 12rem;
}
.reader-settings-panel label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: inherit;
  opacity: 0.8;
}
.reader-settings-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.reader-settings-panel select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 0.375rem;
  background: inherit;
  color: inherit;
  cursor: pointer;
  font-family: inherit;
}
body.reader-page .reader-settings-panel select {
  border-color: rgba(232,228,220,0.3);
}
/* Settings dropdown panel - always dark (in header) */
body.reader-page .reader-settings-panel { background: #1a1814; border-color: rgba(232,228,220,0.15); color: #e8e4dc; }
body.reader-page .reader-settings-panel select { background: #2c261e; color: #e8e4dc; }

/* Reader page: body, header, footer always dark */
body.reader-page { background: #1a1814 !important; color: #e8e4dc; }
body.reader-page .reader-header { background: rgba(26,24,20,0.95) !important; border-color: rgba(232,228,220,0.1) !important; color: #e8e4dc; }
body.reader-page .reader-footer { background: #1a1814 !important; border-color: rgba(232,228,220,0.1) !important; color: #e8e4dc; }
body.reader-page .reader-footer a[href] { color: #c9a227; }

/* Reader panel: always dark, just a scroll wrapper */
#reader-panel { flex: 1; min-height: 0; background: #1a1814; }

/* Reader content card: the text box that receives theme background */
.reader-content {
  max-width: 42rem;
  margin: 0 auto;
  padding: 2rem 2.25rem;
  border-radius: 0.75rem;
  line-height: 1.75;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Theme backgrounds apply to .reader-content only */
#reader-panel.reader-theme-default .reader-content { background: #232019; color: #e8e4dc; }
#reader-panel.reader-theme-default .reader-content em { color: rgba(232,228,220,0.9); }

#reader-panel.reader-theme-white .reader-content { background: #ffffff; color: #1a1a1a; }
#reader-panel.reader-theme-white .reader-content em { color: #333; }

#reader-panel.reader-theme-sepia .reader-content { background: #f4ecd8; color: #2c2419; }
#reader-panel.reader-theme-sepia .reader-content em { color: #3d3225; }

/* Font size - on panel */
#reader-panel.reader-size-small .reader-content { font-size: 1rem !important; }
#reader-panel.reader-size-small .reader-content h2 { font-size: 1.125rem !important; }
#reader-panel.reader-size-medium .reader-content { font-size: 1.125rem !important; }
#reader-panel.reader-size-medium .reader-content h2 { font-size: 1.25rem !important; }
#reader-panel.reader-size-large .reader-content { font-size: 1.25rem !important; }
#reader-panel.reader-size-large .reader-content h2 { font-size: 1.375rem !important; }

/* Font family - on panel */
#reader-panel.reader-font-lora .reader-content { font-family: 'Lora', Georgia, serif !important; }
#reader-panel.reader-font-georgia .reader-content { font-family: Georgia, 'Times New Roman', serif !important; }
#reader-panel.reader-font-inter .reader-content { font-family: 'Inter', system-ui, sans-serif !important; }
#reader-panel.reader-font-merriweather .reader-content { font-family: 'Merriweather', Georgia, serif !important; }

/* Hide mobile menu by default; show when details open */
nav .mobile-menu { display: none; }
@media (max-width: 767px) {
  nav .desktop-links { display: none !important; }
  nav .mobile-menu {
    display: block;
    position: relative;
  }
  nav .mobile-trigger { display: inline-flex; }
  /* Dropdown panel: overlay below header, no stretch */
  nav .mobile-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.25rem;
    min-width: 12rem;
    padding: 0.5rem 0;
    background: var(--charcoal);
    border: 1px solid rgba(232, 228, 220, 0.15);
    border-radius: 0.5rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
    z-index: 60;
  }
}
nav .mobile-trigger { display: none; }

.news-card {
  border: 1px solid rgba(224, 224, 224, 0.1);
  border-radius: 0.5rem;
  padding: 1.5rem;
  background: var(--charcoal);
  transition: border-color 0.2s, background 0.2s;
}
.news-card:hover {
  border-color: rgba(201, 162, 39, 0.45);
  background: var(--gold-muted);
}

.chapter-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid rgba(224, 224, 224, 0.05);
  transition: background 0.2s;
}
.chapter-row:hover { background: var(--gold-muted); }
.chapter-row a { color: var(--off-white); }
.chapter-row:hover a { color: var(--gold); }

/* ========== Mobile & small screens — App-like styling ========== */

/* App shell on mobile: smooth scroll */
@media (max-width: 767px) {
  main {
    -webkit-overflow-scrolling: touch;
  }
}

/* Touch-friendly targets and spacing (phones) */
@media (max-width: 767px) {
  nav {
    padding: 0.75rem 1rem !important;
  }

  .mobile-trigger {
    min-height: 44px;
    min-width: 44px;
    padding: 0.75rem !important;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-dropdown a {
    padding: 0.75rem 1rem !important;
    min-height: 44px;
    display: flex !important;
    align-items: center;
    -webkit-tap-highlight-color: transparent;
  }

  .btn-primary,
  .btn-secondary {
    min-height: 48px;
    padding: 0.875rem 1.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
  }

  .chapter-row {
    min-height: 48px;
    padding: 0.875rem 1rem;
    flex-wrap: wrap;
    -webkit-tap-highlight-color: transparent;
  }

  .chapter-row span:first-of-type {
    flex: 1;
    min-width: 0;
  }

  main {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }

  .reader-content {
    padding: 0 0.25rem;
    font-size: 1.0625rem;
  }

  /* App-like header: rounded bottom, subtle shadow */
  body > header:first-of-type {
    border-radius: 0 0 1rem 1rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  }

  /* Main content: comfortable app padding */
  main {
    padding-left: 1.25rem !important;
    padding-right: 1.25rem !important;
    padding-bottom: 1.5rem !important;
  }

  /* Cards: app-style rounded corners */
  .news-card,
  .chapter-row {
    border-radius: 0.75rem;
  }
  .news-card {
    padding: 1.25rem;
  }
}

/* Reader pages: full app-like reading experience */
@media (max-width: 767px) {
  /* html height needed so body height:100% works for flex scroll */
  html.reader-page-html {
    height: 100%;
  }
  /* Reader layout: header + scrollable panel + sticky bottom nav */
  body.reader-page {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100dvh;
    overflow: hidden;
    -webkit-overflow-scrolling: touch;
  }
  body.reader-page > header {
    flex-shrink: 0;
    border-radius: 0 0 1rem 1rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
  }
  /* Scrollable content area - reader-panel wraps article */
  body.reader-page > #reader-panel {
    flex: 1;
    min-height: 0; /* Critical: allows flex child to shrink and scroll */
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
  }
  body.reader-page #reader-panel article {
    padding: 1.5rem 1.25rem 2rem !important;
    padding-bottom: calc(2rem + var(--safe-bottom)) !important;
  }
  body.reader-page > footer,
  body.reader-page > .reader-footer {
    flex-shrink: 0;
    position: sticky;
    bottom: 0;
    padding: 1rem 1.25rem !important;
    padding-bottom: calc(1rem + var(--safe-bottom)) !important;
    background: var(--charcoal);
    border-top: 1px solid rgba(232, 228, 220, 0.1);
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.2);
    border-radius: 1rem 1rem 0 0;
  }

  /* Reader content card: mobile sizing */
  .reader-content {
    font-size: 1.125rem !important;
    line-height: 1.85 !important;
    max-width: 100%;
    margin: 0 auto;
    padding: 1.5rem 1.25rem !important;
    border-radius: 0.625rem;
  }
  .reader-content p {
    margin-bottom: 1.25rem;
  }
  .reader-content h2 {
    font-size: 1.25rem !important;
    margin-bottom: 1.25rem !important;
  }

  /* Reader settings: prevent overflow on small screens */
  body.reader-page .reader-settings-panel {
    max-width: calc(100vw - 2rem);
    right: 0;
    left: auto;
  }
  /* Reader nav: app-style bottom bar */
  body.reader-page > footer > div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    max-width: 100%;
  }
  body.reader-page > footer a {
    padding: 0.5rem 0.75rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
}

/* Form inputs: prevent zoom on focus (iOS) and comfortable touch size */
.input-field {
  min-height: 48px;
  font-size: 16px;
}
@media (max-width: 767px) {
  .input-field {
    min-height: 48px;
    padding: 0.875rem 1rem;
  }

  textarea.input-field {
    min-height: 120px;
  }

  select.input-field {
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23e8e4dc' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    padding-right: 2.5rem;
  }
}

/* Forms: stack and full width on small screens */
@media (max-width: 480px) {
  form {
    flex-direction: column !important;
  }

  form .input-field,
  form input[type="email"],
  form button[type="submit"] {
    max-width: none !important;
    width: 100% !important;
    min-width: 0 !important;
  }
}

/* Very small phones: tighter spacing, single-column layout */
@media (max-width: 480px) {
  body {
    font-size: 15px;
  }

  section {
    padding-top: 2rem !important;
    padding-bottom: 2rem !important;
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }

  h1 {
    font-size: 1.5rem !important;
    line-height: 1.3 !important;
  }

  h2 {
    font-size: 1.375rem !important;
  }

  footer,
  footer > div {
    flex-direction: column;
    text-align: center;
    padding: 1.5rem 1rem !important;
    gap: 1rem;
  }

  footer a {
    padding: 0.25rem 0;
  }
}

/* Reader content on non-reader pages (e.g. book-info): mobile */
@media (max-width: 767px) {
  body:not(.reader-page) .reader-content {
    font-size: 1.0625rem;
    line-height: 1.75;
  }
  body:not(.reader-page) article {
    padding: 1.5rem 1rem !important;
  }
}

/* Prevent long words from breaking layout */
.reader-content p,
.news-card p,
.news-card h3 {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Blog intro popup: blurred overlay, shown when URL has #about-blog */
.blog-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgba(26, 24, 20, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
  pointer-events: none;
}
.blog-overlay:target {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}
.blog-overlay__card {
  max-width: 28rem;
  width: 100%;
  padding: 2rem;
  background: var(--charcoal);
  border: 1px solid rgba(224, 224, 224, 0.15);
  border-radius: 0.5rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}
.blog-overlay__card h2 {
  margin: 0 0 1rem;
  font-size: 1.5rem;
  color: var(--off-white);
}
.blog-overlay__card p {
  margin: 0 0 1.5rem;
  line-height: 1.7;
  color: rgba(232, 228, 220, 0.9);
}
.blog-overlay__card .btn-primary {
  display: inline-block;
}

.blog-overlay__card h2:first-child {
  margin-top: 0;
}
