/* The pop-up book menu: a small cloth-bound pop-up book on a white page. The book and its paper figures are flat HTML
   panels placed in 3D with CSS (preserve-3d); the script works out the angle of every fold from the one angle of the
   book's opening. */
:root {
  --page: #ffffff;          /* the page the book lies on */
  --cloth: #2f4a3e;         /* the cloth of the cover */
  --cloth-dark: #1f332a;
  --foil: #e2c27a;          /* the gold blocked title */
  --paper: #f6f0e3;         /* the pages */
  --paper-dark: #e7dcc6;
  --ink: #2b241c;           /* the printed words */
  --ink-soft: #5b4a3a;
  --sky-top: #c9d9e6;       /* the evening sky printed inside the cover */
  --sky-low: #f4e3cc;
  --hills: #b9c8a6;
  --shop: #2f5d50;          /* the shopfront */
  --shop-top: #24493f;
  --awning-a: #f3ede0;
  --awning-b: #c4473a;
  --glass: #f2d9a2;         /* lit windows */
  --stage-frame: #8c2f2b;   /* the little theatre */
  --stage-top: #6e2421;
  --stage-dark: #2d1c22;
  --curtain: #b73a35;
  --stage-floor: #9a6b3f;
  --gold: #d9b45f;
  --book-a: #2d4b6e;        /* the spines of the stair of books */
  --book-b: #c9902f;
  --book-c: #3f7266;
  --book-d: #7d2e3a;
  --roof: #b5523b;          /* the house */
  --roof-dark: #8e3d2c;
  --wall: #efe2c8;
  --window: #f2d9a2;
  --door: #3c5b7a;
  --chimney: #8e3d2c;
  --stone: #d8cdb8;         /* the arch */
  --stone-dark: #b9ab92;
  --ivy: #6d8f5a;
}

* {
  box-sizing: border-box;
}

/* a tap is a click at once: no wait for a double tap */
html {
  touch-action: manipulation;
}

html,
body {
  margin: 0;
  overflow-x: clip;
}

body {
  min-height: 100svh;
  background: var(--page);
  color: var(--ink);
  font-family: 'Instrument Sans Variable', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.stage {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

/* ---------------------------------------------------------------- the book and its 3D scene */
/* the scene is drawn at 660 by 520 and scaled to the room there is */
.book {
  position: relative;
  width: min(660px, 100%);
  aspect-ratio: 660 / 520;
  margin-bottom: 56px;
  -webkit-tap-highlight-color: transparent;
}

.book__fit {
  position: absolute;
  left: 0;
  top: 0;
  width: 660px;
  height: 520px;
  transform-origin: 0 0;
}

.scene {
  position: absolute;
  inset: 0;
  perspective: 1500px;
  perspective-origin: 50% 0%;
}

.world {
  position: absolute;
  left: 330px;
  top: 330px;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
}

.world > *,
.fig > * {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 100%;
}

.fig {
  transform-style: preserve-3d;
}

/* the shading of every panel: the script sets how much light it is turned away from */
.page::after,
.panel::after,
.cover::after,
.edge::after {
  content: '';
  position: absolute;
  inset: 0;
  background: #2a1a08;
  opacity: var(--shade, 0);
  pointer-events: none;
}

/* ---------------------------------------------------------------- the pages */
.page {
  width: 600px;
  height: 300px;
  overflow: hidden;
  background: var(--paper);
}

.page--lower {
  transform-origin: 0 0;
  box-shadow: inset 0 18px 24px -18px rgba(70, 50, 20, 0.35);
}

.page--upper {
  backface-visibility: hidden;
  box-shadow: inset 0 -18px 24px -18px rgba(70, 50, 20, 0.3);
}

.page__print {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.print__line {
  fill: none;
  stroke: var(--paper-dark);
  stroke-width: 2;
}

.print__line--soft {
  stroke-dasharray: 2 7;
  stroke-linecap: round;
}

.print__grass {
  fill: none;
  stroke: var(--hills);
  stroke-width: 1.6;
  stroke-linecap: round;
}

.print__moon {
  fill: #fbf1d6;
}

.print__cloud {
  fill: #f8f2e6;
  opacity: 0.85;
}

.print__hills {
  fill: var(--hills);
}

.print__roofs {
  fill: #a3b591;
}

.page__note {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 18px;
  margin: 0;
  text-align: center;
  font-family: 'Newsreader Variable', Georgia, serif;
  font-style: italic;
  font-size: 15px;
  color: var(--ink-soft);
}

.shadows > span {
  position: absolute;
  inset: 0;
  background: rgba(60, 40, 15, 0.24);
  filter: blur(6px);
}

/* the slots in the upper page the paper tabs run into */
.slots > span {
  position: absolute;
  height: 3px;
  border-radius: 2px;
  background: rgba(60, 40, 20, 0.55);
  transform: translateX(-50%);
}

/* the front edge of the book: the boards and the block of pages */
.edge {
  width: 612px;
  height: 14px;
  background:
    linear-gradient(var(--cloth-dark), var(--cloth-dark)) top / 100% 3px no-repeat,
    linear-gradient(var(--cloth-dark), var(--cloth-dark)) bottom / 100% 3px no-repeat,
    repeating-linear-gradient(180deg, var(--paper) 0 1px, var(--paper-dark) 1px 2px);
  border-radius: 2px;
}

/* ---------------------------------------------------------------- the cover */
.cover {
  width: 612px;
  height: 306px;
  backface-visibility: hidden;
  border-radius: 6px;
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.035) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.06) 0 1px, transparent 1px 3px),
    linear-gradient(160deg, var(--cloth), var(--cloth-dark));
  display: grid;
  place-items: center;
}

.cover__frame {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 26px 64px 24px;
  border: 1.5px solid var(--foil);
  outline: 1px solid color-mix(in srgb, var(--foil) 60%, transparent);
  outline-offset: 5px;
  color: var(--foil);
  text-align: center;
}

.cover__small {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.24em;
  text-transform: uppercase;
}

.cover__title {
  font-family: 'Newsreader Variable', Georgia, serif;
  font-size: 92px;
  font-weight: 500;
  font-style: italic;
  line-height: 0.95;
  letter-spacing: -0.01em;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}

.cover__rule {
  width: 120px;
  height: 10px;
}

.cover__rule path {
  fill: var(--foil);
  stroke: var(--foil);
  stroke-width: 1.2;
}

/* ---------------------------------------------------------------- the paper figures */
.panel {
  overflow: visible;
}

.riser svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.cut {
  fill: var(--paper);
}

.cut-line {
  fill: none;
  stroke: rgba(70, 45, 20, 0.35);
  stroke-width: 1;
}

.fold {
  fill: none;
  stroke: rgba(60, 10, 10, 0.35);
  stroke-width: 1.4;
  stroke-linecap: round;
}

.mullion {
  fill: none;
  stroke: var(--wall);
  stroke-width: 2.4;
}

.joint {
  fill: none;
  stroke: var(--stone-dark);
  stroke-width: 1.2;
}

/* a cut-out and its plain paper back: each shows only from its own side */
.riser {
  backface-visibility: hidden;
}

.riser--back svg * {
  fill: var(--paper-dark) !important;
  stroke: none !important;
}

.riser--back.riser--book {
  background: var(--paper-dark);
}

.riser--back .spine {
  display: none;
}

/* the riser's shade stays inside the paper shapes, not the box round them */
.riser:not(.riser--book)::after {
  display: none;
}

.riser--back:not(.riser--book) svg {
  filter: brightness(calc(1 - var(--shade, 0) * 0.9));
}

.riser svg {
  filter: brightness(calc(1 - var(--shade, 0) * 0.9));
}

.tread {
  background: var(--top, var(--paper-dark));
}

.tread--pages {
  background: repeating-linear-gradient(90deg, var(--paper) 0 2px, var(--paper-dark) 2px 3px);
}

.tab {
  background: var(--paper-dark);
  border-radius: 2px 2px 0 0;
}

/* the stair of books: each riser is the spine of a book */
.riser--book {
  background: var(--spine);
  border-radius: 2px;
}

.spine {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg, transparent 10px, rgba(226, 194, 122, 0.85) 10px 12px, transparent 12px calc(100% - 12px), rgba(226, 194, 122, 0.85) calc(100% - 12px) calc(100% - 10px), transparent calc(100% - 10px)),
    linear-gradient(180deg, rgba(255, 255, 255, 0.12), transparent 40%, rgba(0, 0, 0, 0.18));
}

/* the entries: printed on the figures */
.label {
  position: absolute;
  transform: translate(-50%, -50%);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Newsreader Variable', Georgia, serif;
  font-size: 19px;
  font-weight: 600;
  line-height: 1.1;
  white-space: nowrap;
  text-decoration: none;
  color: var(--ink);
  outline: none;
  transition: color 0.25s ease, background-color 0.25s ease;
}

.label--sign {
  color: #fbf3df;
}

.label--stone {
  color: var(--ink);
  font-size: 18px;
}

.label--spine {
  color: var(--foil);
  font-size: 17px;
  letter-spacing: 0.02em;
}

.label--gable {
  color: #fbf3df;
  font-size: 18px;
}

.label[aria-current='page'] {
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 3px;
}

.label:focus-visible {
  box-shadow: 0 0 0 2px var(--ink), 0 0 0 4px #ffffff;
}

/* ---------------------------------------------------------------- the real buttons */
.book__open {
  position: absolute;
  z-index: 2;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.book__open:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 4px;
}

.book__open[hidden] {
  display: none;
}

.book__close {
  position: absolute;
  left: 50%;
  top: calc(100% + 8px);
  z-index: 2;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px 0 12px;
  border: 1px solid #d9d0bf;
  border-radius: 999px;
  background: #ffffff;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transform: translateX(-50%);
  transition: background-color 0.2s ease, border-color 0.2s ease, transform 0.25s cubic-bezier(0.34, 1.6, 0.5, 1);
  -webkit-tap-highlight-color: transparent;
}

.book__close svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

@media (hover: hover) and (pointer: fine) {
  .book__close:hover {
    background: var(--paper);
    border-color: #c9bda6;
  }
}

.book__close:active {
  transform: translateX(-50%) scale(0.96);
}

.book__close:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}
