/* ============================================================
 * Neurotype V2 — Children's Storybook Component
 * Reusable page-flip storybook with felt texture pages.
 * All values use brand system tokens.
 * ========================================================== */

/* Let the storybook break out of the .section max-width.
   Horizontal padding so the book's shadow + corners stay visible at viewport edges. */
.storybook-container {
  width: 100vw;
  max-width: 1800px;
  padding-inline: clamp(var(--space-4), 4vw, var(--space-8));
  box-sizing: border-box;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

.storybook {
  width: 100%;
  /* Aspect ratio matches page-flip config (700×500 per page, two-page spread) */
  aspect-ratio: 1400 / 500;
  position: relative;
  overflow: hidden;
  container-type: inline-size;
}

/* Book spine / centre shadow */
.storybook::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  background: linear-gradient(
    to right,
    transparent,
    rgba(0, 0, 0, 0.06) 20%,
    rgba(0, 0, 0, 0.12) 45%,
    rgba(0, 0, 0, 0.15) 50%,
    rgba(0, 0, 0, 0.12) 55%,
    rgba(0, 0, 0, 0.06) 80%,
    transparent
  );
  pointer-events: none;
  z-index: 10;
  transition: opacity 0.25s ease 0.05s;
}

.storybook.is-flipping::after,
.storybook.on-cover::after {
  opacity: 0;
  transition: opacity 0s;
}

.storybook.on-cover {
  box-shadow: none;
}

/* Subtle outer book shadow for depth */
.storybook-container .storybook {
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.08),
    0 8px 24px rgba(0, 0, 0, 0.06),
    0 16px 48px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
}

/* Slight page curl shadow on inner edges */
.storybook-page {
  box-shadow: inset -1px 0 3px rgba(0, 0, 0, 0.04);
}

.storybook-page {
  /* Padding scales with book width via container queries (1cqi = 1% of book width) */
  padding: clamp(var(--space-3), 3cqi, var(--space-8)) clamp(var(--space-4), 3.5cqi, var(--space-8));
  background: var(--bg-primary);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  border: 1px solid var(--border-subtle);
}

.storybook-page::before {
  content: '';
  position: absolute;
  inset: 0;
  background: url('/assets/felt-texture-tile.jpg') center / cover no-repeat;
  opacity: 0.2;
  pointer-events: none;
  z-index: 0;
}

.storybook-page > * {
  position: relative;
  z-index: 1;
}

.storybook-page h3 {
  font-family: var(--font-heading);
  font-size: clamp(1.5rem, 3.25cqi, 2.75rem);
  font-weight: 500;
  line-height: 1.2;
  color: var(--neurotype-accent-primary);
  margin-bottom: clamp(var(--space-4), 3.5cqi, var(--space-7));
}

.storybook-page p {
  font-size: clamp(1.15rem, 2.35cqi, 1.9rem);
  line-height: 1.6;
  color: var(--text-secondary);
  margin-bottom: clamp(var(--space-3), 1.75cqi, var(--space-5));
}

.storybook-page p:last-child {
  margin-bottom: 0;
}

.storybook-page ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: clamp(var(--space-2), 1cqi, var(--space-3));
}

.storybook-page ul li {
  font-size: clamp(1.15rem, 2.35cqi, 1.9rem);
  line-height: 1.6;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.storybook-page ul li::before {
  content: '';
  width: var(--dot-sm);
  height: var(--dot-sm);
  background: var(--neurotype-accent-primary);
  border-radius: 50%;
  flex-shrink: 0;
}

/* Dense text pages (more content than average) */
.storybook-page-dense {
  padding: clamp(var(--space-3), 2cqi, var(--space-7)) clamp(var(--space-4), 3.5cqi, var(--space-8));
}

.storybook-page-dense p {
  font-size: clamp(1.05rem, 1.9cqi, 1.55rem);
  line-height: 1.5;
  margin-bottom: clamp(var(--space-2), 1.25cqi, var(--space-3));
}

.storybook-page-dense ul {
  gap: clamp(var(--space-1), 0.5cqi, var(--space-2));
}

.storybook-page-dense ul li {
  font-size: clamp(1.05rem, 1.9cqi, 1.55rem);
  line-height: 1.5;
}

/* Illustration pages (full-bleed image) */
.storybook-page-illustration {
  padding: 0;
}

.storybook-page-illustration .storybook-illustration {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  display: block;
}

.storybook-illustration-placeholder {
  width: 100%;
  height: 100%;
  background: var(--bg-secondary);
}

/* Subtle felt-patch watermark on illustration pages */
.storybook-page-illustration::after {
  content: '';
  position: absolute;
  bottom: clamp(var(--space-2), 2.2cqi, var(--space-4));
  right: clamp(var(--space-2), 2.2cqi, var(--space-4));
  width: clamp(32px, 5.5cqi, 64px);
  aspect-ratio: 600 / 403;
  background: url('/assets/felt-patch-logo-base.webp') center / contain no-repeat;
  opacity: 0.4;
  pointer-events: none;
  z-index: 2;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25));
}

/* Endpapers */
.storybook-endpaper {
  background: var(--bg-secondary);
  border: none;
}

/* Cover pages */
.storybook-cover {
  background: var(--bg-inverted);
  text-align: center;
  align-items: center;
  border: none;
  position: relative;
  overflow: hidden;
}

.storybook-cover h3 {
  font-size: var(--text-h2);
  color: var(--text-on-accent);
  margin-bottom: var(--space-3);
}

.storybook-cover p {
  color: rgba(255, 255, 255, 0.8);
  font-size: var(--text-body);
}

.storybook-cover .storybook-brand {
  font-family: var(--font-heading);
  font-size: var(--text-h4);
  font-weight: 500;
  color: var(--neurotype-accent-primary);
  margin-top: var(--space-5);
}

/* Illustrated front cover */
.storybook-cover-front {
  padding: 0;
}

.storybook-cover-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center bottom;
}

.storybook-cover-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: var(--space-6) var(--space-5);
  height: 100%;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.45) 0%,
    rgba(0, 0, 0, 0.2) 50%,
    transparent 100%
  );
}

.storybook-cover-front h3 {
  font-size: var(--text-h3);
  color: var(--text-on-accent);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  margin-bottom: var(--space-2);
  line-height: 1.3;
}

.storybook-cover-front p {
  color: rgba(255, 255, 255, 0.9);
  font-size: var(--text-small);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  margin-bottom: 0;
}

.storybook-cover-logo-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: var(--space-3);
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.2));
}

.storybook-cover-logo {
  width: 173px;
  height: auto;
}

/* Illustrated back cover */
.storybook-cover-back {
  padding: 0;
}

.storybook-cover-content-bottom {
  justify-content: flex-end;
  background: linear-gradient(
    to top,
    rgba(0, 0, 0, 0.6) 0%,
    rgba(0, 0, 0, 0.4) 40%,
    rgba(0, 0, 0, 0.15) 70%,
    transparent 100%
  );
}

.storybook-cover-back h3 {
  font-size: var(--text-h3);
  color: var(--text-on-accent);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
  margin-bottom: var(--space-2);
}

.storybook-cover-back p {
  color: rgba(255, 255, 255, 0.95);
  font-size: var(--text-small);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
}

.storybook-cover-logo-text {
  position: absolute;
  top: calc(50% + 5px);
  transform: translateY(-50%);
  font-family: var(--font-heading);
  font-size: var(--text-small);
  font-weight: 500;
  color: var(--text-heading);
  letter-spacing: 0.02em;
}

/* ---- Fullscreen header (hidden outside fullscreen) ---- */
.storybook-fs-header {
  display: none;
}

/* Fullscreen mode */
.storybook-container.storybook-fullscreen {
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: none;
  background: var(--bg-primary);
  position: relative;
}

.storybook-container.storybook-fullscreen::before {
  content: '';
  position: absolute;
  inset: 0;
  background: url('/assets/topography-background.jpg') repeat-y center top;
  background-size: 100% auto;
  opacity: 0.11;
  pointer-events: none;
  z-index: 0;
}

.storybook-container.storybook-fullscreen .storybook-fs-header {
  display: block;
  position: absolute;
  top: var(--space-6);
  left: var(--space-6);
  z-index: 10;
}

/* Felt patch logo (mirrors nav-logo from layout.css) */
.storybook-fs-logo {
  position: relative;
  display: inline-block;
  padding: var(--space-3) var(--space-6);
  padding-top: var(--space-4);
  text-decoration: none;
}

.storybook-fs-logo::before {
  content: '';
  position: absolute;
  inset: -23px -60px -49px;
  background: image-set(
      url('/assets/felt-patch-logo-base.webp') type('image/webp'),
      url('/assets/felt-patch-logo-base.png') type('image/png'))
    center / contain no-repeat;
  z-index: -1;
}

.storybook-fs-logo-text {
  position: relative;
  z-index: 1;
  font-family: var(--font-heading);
  font-size: 0.945em;
  top: 15px;
  color: var(--text-heading);
}


.storybook-container.storybook-fullscreen .storybook {
  width: 60%;
  max-width: 1000px;
  margin: 0 auto;
  position: relative;
  z-index: 1;
}

.storybook-container.storybook-fullscreen .storybook-nav {
  flex-shrink: 0;
  padding: 0 var(--space-5) var(--space-4);
  position: relative;
  z-index: 1;
  gap: clamp(1.5rem, 3vw, 3rem);
}

.storybook-container.storybook-fullscreen .storybook-arrow {
  padding: clamp(8px, 0.8vw, 14px) clamp(24px, 2.5vw, 40px);
}

.storybook-container.storybook-fullscreen .storybook-arrow svg {
  width: clamp(24px, 2vw, 32px);
  height: clamp(24px, 2vw, 32px);
}

.storybook-container.storybook-fullscreen .storybook-progress {
  max-width: clamp(200px, 20vw, 400px);
}

.storybook-container.storybook-fullscreen .storybook-progress-track {
  height: clamp(6px, 0.6vh, 10px);
}

.storybook-container.storybook-fullscreen .storybook-page-count {
  font-size: 0.8rem;
}


/* Navigation controls */
.storybook-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-5);
  margin-top: var(--space-5);
}

.storybook-arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-2) var(--space-5);
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease-default), color var(--duration-fast) var(--ease-default);
  flex-shrink: 0;
}

.storybook-arrow:hover:not(:disabled) {
  border-color: var(--neurotype-accent-primary);
  color: var(--neurotype-accent-primary);
}

.storybook-arrow:focus-visible {
  outline: 2px solid var(--neurotype-accent-primary);
  outline-offset: 2px;
}

.storybook-arrow:disabled {
  opacity: 0.25;
  cursor: not-allowed;
  pointer-events: none;
}

/* ---- Progress pill / scrubber ---- */
.storybook-progress {
  flex: 1;
  max-width: 200px;
  position: relative;
}

.storybook-progress-track {
  width: 100%;
  height: 6px;
  background: var(--border-subtle);
  border-radius: 3px;
  cursor: pointer;
  position: relative;
}

.storybook-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--neurotype-accent-primary);
  border-radius: 3px;
  transition: width 0.6s ease;
  pointer-events: none;
}

.storybook-progress-thumb {
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-50%, -50%);
  width: 16px;
  height: 16px;
  background: var(--bg-elevated);
  border: 2px solid var(--neurotype-accent-primary);
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
  pointer-events: none;
  transition: left 0.6s ease;
}

.storybook-page-count {
  display: block;
  text-align: center;
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 8px);
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  user-select: none;
}

/* Rotate-your-phone prompt (mobile portrait only) */
.storybook-rotate-prompt {
  display: none;
}

@media (max-width: 950px) {
  .storybook-container {
    display: none;
  }
}

@media (max-width: 950px) and (orientation: portrait) and (pointer: coarse) {
  .storybook-rotate-prompt {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-4);
    padding: var(--space-7) var(--space-5);
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
    text-align: center;
  }

  .storybook-rotate-prompt svg {
    color: var(--neurotype-accent-primary);
  }

  .storybook-rotate-prompt p {
    color: var(--text-muted);
    font-size: var(--text-body);
    line-height: 1.5;
    max-width: 26ch;
    margin: 0;
  }
}

/* Mobile landscape: promote the book to a CSS overlay so Safari chrome
   doesn't eat the spread. Two-page layout preserved. */
@media (max-width: 950px) and (orientation: landscape) and (pointer: coarse) {
  .storybook-rotate-prompt {
    display: none;
  }

  .storybook-container.storybook-mobile-overlay {
    position: fixed;
    inset: 0;
    left: 0;
    width: 100vw;
    max-width: none;
    transform: none;
    z-index: 100;
    background: var(--bg-primary);
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  .storybook-container.storybook-mobile-overlay .storybook {
    width: 90%;
    max-width: calc(90dvh * 2.8);
    max-height: 90dvh;
    margin: 0 auto;
  }

  /* Hide the nav entirely on phone landscape; users swipe or tap a corner. */
  .storybook-container.storybook-mobile-overlay .storybook-nav {
    display: none;
  }

  /* Tighter page typography so dense pages fit at this scale. */
  .storybook-container.storybook-mobile-overlay .storybook-page {
    padding: var(--space-3) var(--space-4);
  }

  .storybook-container.storybook-mobile-overlay .storybook-page h3 {
    font-size: 1.1rem;
    line-height: 1.15;
    margin-bottom: var(--space-2);
  }

  .storybook-container.storybook-mobile-overlay .storybook-page p,
  .storybook-container.storybook-mobile-overlay .storybook-page ul li {
    font-size: 0.85rem;
    line-height: 1.4;
    margin-bottom: var(--space-2);
  }

  .storybook-container.storybook-mobile-overlay .storybook-page-dense {
    padding: var(--space-2) var(--space-3);
  }

  .storybook-container.storybook-mobile-overlay .storybook-page-dense p,
  .storybook-container.storybook-mobile-overlay .storybook-page-dense ul li {
    font-size: 0.75rem;
    line-height: 1.35;
    margin-bottom: 4px;
  }

  .storybook-container.storybook-mobile-overlay .storybook-page-dense ul {
    gap: 3px;
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover-content {
    padding: var(--space-3) var(--space-4);
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover h3 {
    font-size: 1.5rem;
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover p {
    font-size: 0.9rem;
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover-logo {
    width: 90px;
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover-logo-wrap {
    margin-top: var(--space-2);
  }

  .storybook-container.storybook-mobile-overlay .storybook-cover-logo-text {
    font-size: 0.65rem;
  }
}

/* Responsive */
@media (max-width: 768px) {
  .storybook-page {
    padding: var(--space-4);
  }

  .storybook-page h3 {
    font-size: var(--text-h4);
  }

  .storybook-page p,
  .storybook-page ul li {
    font-size: var(--text-small);
  }

  .storybook-cover h3 {
    font-size: var(--text-h3);
  }
}

