/* Independent chapter companion. Loaded after the main site stylesheet. */

.chapter-companion[hidden],
.chapter-companion__note[hidden] {
  display: none !important;
}

.chapter-companion {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 34;
  width: clamp(4.75rem, 8.5vw, 7rem);
  height: clamp(4.75rem, 8.5vw, 7rem);
  pointer-events: none;
  contain: layout style;
  opacity: 0;
  transform: translateY(0.5rem) scale(0.96);
  transition:
    opacity 360ms ease,
    transform 520ms cubic-bezier(0.22, 1, 0.36, 1);
}

.chapter-companion.is-ready {
  opacity: 1;
  transform: none;
}

.chapter-companion.is-dragging {
  transition: none;
}

.chapter-companion__handle {
  position: relative;
  display: grid;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0.2rem;
  place-items: end center;
  border: 0;
  border-radius: 42% 42% 46% 46%;
  color: inherit;
  background: transparent;
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.chapter-companion__handle::before {
  content: "";
  position: absolute;
  z-index: -1;
  right: 8%;
  bottom: 2%;
  left: 8%;
  height: 22%;
  border-radius: 50%;
  background: rgb(3 8 20 / 38%);
  filter: blur(0.5rem);
  opacity: 0.72;
}

.chapter-companion__handle:active,
.chapter-companion.is-dragging .chapter-companion__handle {
  cursor: grabbing;
}

.chapter-companion__handle:focus-visible {
  outline: 0.15rem solid #f7ecd8;
  outline-offset: 0.22rem;
  box-shadow:
    0 0 0 0.28rem rgb(24 34 58 / 78%),
    0 0.8rem 2.2rem rgb(3 8 20 / 26%);
}

.chapter-companion__image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 0.7rem 0.8rem rgb(3 8 20 / 42%));
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

.chapter-companion__note {
  position: absolute;
  left: 0;
  bottom: calc(100% + 0.7rem);
  width: max-content;
  min-width: min(15rem, calc(100vw - 2rem));
  max-width: min(20rem, calc(100vw - 2rem));
  padding: 0.9rem 1rem 0.95rem;
  border: 1px solid rgb(247 236 216 / 25%);
  border-radius: 1rem 1rem 1rem 0.34rem;
  color: #f7ecd8;
  background:
    linear-gradient(145deg, rgb(32 42 65 / 96%), rgb(15 23 41 / 98%)),
    rgb(15 23 41 / 98%);
  box-shadow: 0 1.2rem 3rem rgb(3 8 20 / 38%);
  backdrop-filter: blur(0.8rem);
  pointer-events: auto;
}

.chapter-companion__note::after {
  content: "";
  position: absolute;
  left: 1.5rem;
  top: 100%;
  width: 0.8rem;
  height: 0.8rem;
  border-right: 1px solid rgb(247 236 216 / 20%);
  border-bottom: 1px solid rgb(247 236 216 / 20%);
  background: rgb(15 23 41 / 98%);
  transform: translateY(-0.42rem) rotate(45deg);
}

.chapter-companion--align-end .chapter-companion__note {
  right: 0;
  left: auto;
  border-radius: 1rem 1rem 0.34rem 1rem;
}

.chapter-companion--align-end .chapter-companion__note::after {
  right: 1.5rem;
  left: auto;
}

.chapter-companion--note-below .chapter-companion__note {
  top: calc(100% + 0.7rem);
  bottom: auto;
}

.chapter-companion--note-below .chapter-companion__note::after {
  top: auto;
  bottom: 100%;
  border: 0;
  border-top: 1px solid rgb(247 236 216 / 20%);
  border-left: 1px solid rgb(247 236 216 / 20%);
  transform: translateY(0.42rem) rotate(45deg);
}

.chapter-companion__note-label,
.chapter-companion__note-text {
  margin: 0;
}

.chapter-companion__note-label {
  margin-bottom: 0.35rem;
  color: #e8b766;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.chapter-companion__note-text {
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", serif;
  font-size: clamp(0.88rem, 1.5vw, 1rem);
  line-height: 1.72;
  text-wrap: pretty;
}

@media (max-width: 42rem) {
  .chapter-companion {
    width: clamp(4.25rem, 20vw, 5.25rem);
    height: clamp(4.25rem, 20vw, 5.25rem);
  }

  .chapter-companion__note {
    max-width: min(17.5rem, calc(100vw - 1.5rem));
    padding: 0.78rem 0.86rem 0.82rem;
  }

  .chapter-companion__note-text {
    font-size: 0.86rem;
    line-height: 1.65;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chapter-companion,
  .chapter-companion.is-ready,
  .chapter-companion__image {
    transition: none !important;
    animation: none !important;
    transform: none !important;
  }
}

@media (forced-colors: active) {
  .chapter-companion__handle:focus-visible {
    outline: 2px solid ButtonText;
    box-shadow: none;
  }

  .chapter-companion__note {
    border: 1px solid CanvasText;
    color: CanvasText;
    background: Canvas;
    backdrop-filter: none;
  }
}
