/* Side chapter navigation: white circular markers at center-left */
#chapter-nav {
  position: fixed;
  left: 24px;
  display: flex;
  flex-direction: column;
  gap: 0;
  z-index: 1200;
  pointer-events: auto;
  align-items: flex-end;
  justify-content: space-evenly;
  height: 96vh;
  height: 96dvh; /* Dynamic viewport height for mobile browsers */
  top: 2vh;
  top: 2dvh;
  bottom: 2vh;
  bottom: 2dvh;
}

.chapter-item {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  /* Use transparent border instead of margin for gaps - makes entire area clickable */
  border-bottom: 1px solid transparent;
}

/* Longer gap before chapter headings for visual separation */
.chapter-heading-item {
  border-top: 1rem solid transparent;
}

.chapter-heading-item:first-child {
  border-top: 0;
}

.chapter-dot {
  width: 12px;
  height: 4px;
  background: #ffffff;
  border: 2px solid rgba(0,0,0,0.08);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
  cursor: pointer;
  opacity: 0.5;
  transition: width 180ms ease, opacity 180ms ease, transform 180ms ease;
  display: inline-block;
  padding: 0;
}

/* Smaller dots for sections */
.section-dot {
  width: 12px;
  height: 4px;
  border-width: 1px;
}

.chapter-item:hover .chapter-dot { transform: scale(1.06); }

/* Progress bar style: visited/past sections are full opacity */
.chapter-dot.visited {
  opacity: 1;
}

/* Future sections stay at 50% */
.chapter-dot.future {
  opacity: 0.5;
}

/* Current/active section is wider to stand out */
.chapter-dot.current {
  width: 24px;
}

.chapter-label {
  position: absolute;
  left: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0,0,0,0.78);
  color: #fff;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: .8em;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 140ms ease, transform 140ms ease;
  transform-origin: left center;
}

/* Show labels only on hover (works for both mouse hover and tap on mobile) */
.chapter-item:hover .chapter-label {
  opacity: 1;
  transform: translateY(-50%) translateX(0);
}

/* Timestamp items (sections with H3/H4) show labels by default */
.chapter-item.timestamp .chapter-label {
  opacity: 1;
  transform: translateY(-50%) translateX(0);
}

/* Desktop only: smaller timestamp labels by default, full size on hover */
@media (min-width: 801px) and (hover: hover) and (pointer: fine) {
  .chapter-item.timestamp .chapter-label {
    transform: translateY(-50%) translateX(0) scale(0.75);
    font-size: .75em;
  }

  /* When any nav item is hovered, hide all timestamp labels */
  #chapter-nav:hover .chapter-item.timestamp .chapter-label {
    opacity: 0;
  }

  /* But show the timestamp label if its own item is hovered, at full size */
  #chapter-nav .chapter-item.timestamp:hover .chapter-label {
    opacity: 1;
    transform: translateY(-50%) translateX(0) scale(1);
    font-size: .8em;
  }
}

/* small arrow */
.chapter-label::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 50%;
  transform: translateY(-50%);
  width: 0; height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 6px solid rgba(0,0,0,0.78);
}

/* Mobile: full-height touch-friendly rail */
@media (max-width: 800px) {
  #chapter-nav {
    top: 0;
    left: 16px; /* More space from edge */
    height: 100vh;
    height: 100dvh; /* Use dynamic viewport height for mobile browsers */
    transform: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4vh 0; /* Proportional padding */
    gap: 1px; /* Default 1px gap between all items */
    /* Prevent the page from scrolling while swiping the rail */
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    z-index: 100;
  }

  /* Make each section button fill available height maximally */
  #chapter-nav .chapter-item {
    flex: 1 1 0;
    min-height: 0; /* Allow items to shrink below content size */
    width: 48px; /* Larger hit area */
    display: flex;
    align-items: center;
    justify-content: center;
    /* Override desktop borders/margins - gaps already handled by parent gap property */
    border: 0;
    margin: 0;
  }

  /* Larger dots on mobile for better visibility */
  #chapter-nav .chapter-dot {
    width: 18px;
    height: 6px;
    border-width: 2px;
  }

  /* Larger labels on mobile */
  #chapter-nav .chapter-label {
    font-size: 16px; /* Increased from 13px */
    padding: 8px 14px; /* More generous padding */
    left: calc(100% + 16px); /* More space from nav */
    border-radius: 6px;
  }

  #chapter-nav .chapter-label::before {
    left: -8px; /* Adjust arrow position */
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-right: 8px solid rgba(0,0,0,0.78);
  }

  /* When finger hovers an item (via JS), show its label */
  #chapter-nav .chapter-item.touch-hover .chapter-label {
    opacity: 1;
    transform: translateY(-50%) translateX(0);
  }

  #chapter-nav .chapter-item.touch-hover .chapter-dot {
    transform: scale(1.15);
    width: 32px; /* Wider when active */
  }

  /* A transparent overlay that captures touch events across the full rail */
  #chapter-nav .nav-touch-rail {
    position: absolute;
    left: -12px; /* Extended hit area */
    top: 0;
    width: 72px; /* Wider touch target */
    height: 100%;
    pointer-events: auto;
    background: transparent;
    z-index: 1; /* Ensure it's above dots */
  }

  /* Chapter headings: same flex-grow as sections, but with transparent border for gap */
  #chapter-nav .chapter-item.chapter-heading-item {
    flex: 1 1 0;
    border-top: 7px solid transparent; /* 8px total gap = 7px + 1px from parent gap */
    border-bottom: 0;
    margin: 0;
  }

  #chapter-nav .chapter-item.chapter-heading-item:first-child {
    border-top: 0; /* No extra gap before first chapter */
  }
  
  /* Larger dots for chapter headings too */
  #chapter-nav .chapter-heading-item .chapter-dot {
    width: 20px;
    height: 6px;
  }
}
