/* The Wrong Sort of Bees — an animated edition of Chapter I.
   Layout: a one-screen app. The book is a physical object (cloth cover, fixed paper
   colours); the room around it follows the viewer's theme. */
:root {
  --room: #e9dfc3;         /* the table the book lies on */
  --room-2: #ddd0ae;
  --chrome: #2b241a;       /* bar text */
  --chrome-soft: #6a5c42;
  --line: #cdbd90;
  --honey: #a8650a;
  --focus: #2f6db3;

  /* the book itself: same in both themes */
  --leaf: #fbf6e5;
  --leaf-edge: #e9dfc0;
  --leaf-ink: #2b241a;
  --leaf-soft: #6a5c42;
  --leaf-accent: #a8650a;
  --cloth: #3f5a3a;
  --cloth-dark: #2c4029;
  --gilt: #dcb75e;
  --sky: #6f99c8;

  --f-display: "IM Fell English", "Iowan Old Style", Palatino, Georgia, serif;
  --f-display-sc: "IM Fell English SC", "IM Fell English", Georgia, serif;
  --f-body: "Spectral", "Iowan Old Style", Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --room: #16140f; --room-2: #211d16; --chrome: #efe5ca; --chrome-soft: #b0a283; --line: #3d3628;
    --honey: #e4a93c; --focus: #8fb4df;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --room: #16140f; --room-2: #211d16; --chrome: #efe5ca; --chrome-soft: #b0a283; --line: #3d3628;
  --honey: #e4a93c; --focus: #8fb4df;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(ellipse at 50% 45%, var(--room) 0%, var(--room-2) 100%);
  background-color: var(--room);
  color: var(--chrome);
  font: 400 16px/1.5 var(--f-body);
  overflow: hidden;
}
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; border-radius: 3px; }
button { font: inherit; color: inherit; }
img { max-width: 100%; display: block; }

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-columns: minmax(0, 1fr);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 0 max(10px, env(safe-area-inset-bottom, 0px));
}

/* ---------- Bar ---------- */
.bar { display: flex; align-items: center; gap: 1rem; padding-block: 0.7rem; }
.bar-home { display: inline-flex; align-items: center; gap: 0.3rem; color: var(--chrome-soft); text-decoration: none; font-family: var(--f-display-sc); letter-spacing: 0.04em; white-space: nowrap; }
.bar-home:hover { color: var(--chrome); }
.bar-home svg { width: 1.1rem; height: 1.1rem; }
.bar-title { margin: 0 auto; font-family: var(--f-display); font-style: italic; color: var(--chrome-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.bar-tools { display: flex; gap: 0.5rem; }
.tool {
  display: inline-flex; align-items: center; gap: 0.4rem; cursor: pointer;
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 0.4rem 0.8rem; font-family: var(--f-display-sc); letter-spacing: 0.04em; color: var(--chrome);
}
.tool svg { width: 1.05rem; height: 1.05rem; }
.tool.icon { width: 2.2rem; height: 2.2rem; padding: 0; justify-content: center; }
.tool[aria-pressed="true"] { background: var(--chrome); color: var(--room); border-color: var(--chrome); }
@media (max-width: 560px) {
  .bar-title { display: none; }
  .bar-tools { margin-left: auto; }
  .bar-home span { display: none; }
}

/* ---------- Stage & book ---------- */
.stage { position: relative; display: grid; place-items: center; min-height: 0; min-width: 0; perspective: 2600px; }
.book.single .page { font-size: calc(var(--pw) * 4.6 / 100 * var(--fit, 1)); }
.book {
  position: relative;
  width: var(--book-w, 600px);
  height: var(--book-h, 440px);
  transform-style: preserve-3d;
  transition: transform 0.9s cubic-bezier(.45,.05,.25,1);
}
.book.spread.at-start { transform: translateX(-25%); }
.book.spread.at-end { transform: translateX(25%); }

.leaf {
  position: absolute; top: 0; left: 50%; width: 50%; height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform 0.9s cubic-bezier(.45,.05,.25,1), opacity 0.35s ease 0.55s;
  cursor: pointer;
}
.book.single .leaf { left: 0; width: 100%; }
.leaf.flipped { transform: rotateY(-180deg); }
.book.single .leaf.flipped { opacity: 0; pointer-events: none; }
.book.single .leaf:not(.flipped) { transition: transform 0.9s cubic-bezier(.45,.05,.25,1), opacity 0.2s ease; }
.book.single .leaf.flipped .side { box-shadow: none; }

.side {
  position: absolute; inset: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  overflow: hidden;
  background: var(--leaf);
  color: var(--leaf-ink);
}
.side.back { transform: rotateY(180deg); }
/* paper: a little grain and the curve toward the spine */
.side.front { border-radius: 0 4px 4px 0; box-shadow: inset 1px 0 0 rgba(0,0,0,0.05), 6px 8px 22px -12px rgba(30, 20, 0, 0.55); }
.side.back { border-radius: 4px 0 0 4px; box-shadow: -6px 8px 22px -12px rgba(30, 20, 0, 0.55); }
.side.front::after, .side.back::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 5;
}
.side.front::after { background: linear-gradient(to right, rgba(60,40,0,0.16), rgba(60,40,0,0.03) 7%, transparent 14%), radial-gradient(ellipse at 70% 40%, rgba(255,255,255,0.35), transparent 60%); }
.side.back::after { background: linear-gradient(to left, rgba(60,40,0,0.16), rgba(60,40,0,0.03) 7%, transparent 14%), radial-gradient(ellipse at 30% 40%, rgba(255,255,255,0.35), transparent 60%); }
.book.single .side.front::after { background: linear-gradient(to right, rgba(60,40,0,0.08), transparent 5%); }
/* a shadow that sweeps across a page while it turns */
.leaf .side::before {
  content: ""; position: absolute; inset: 0; z-index: 6; pointer-events: none; opacity: 0;
  background: linear-gradient(to left, rgba(20,10,0,0.28), transparent 70%);
  transition: opacity 0.45s ease;
}
.leaf.turning .side::before { opacity: 1; }

/* ---------- Page content ---------- */
.page {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; justify-content: center;
  padding: calc(var(--ph) * 9 / 100) calc(var(--pw) * 10 / 100) calc(var(--ph) * 6 / 100);
  gap: calc(var(--ph) * 2.2 / 100);
  font-size: calc(calc(var(--pw) * 4.15 / 100) * var(--fit, 1));
  line-height: 1.5;
}
.side.back .page { padding-left: calc(var(--pw) * 10 / 100); padding-right: calc(var(--pw) * 10 / 100); }
.runhead {
  position: absolute; top: calc(var(--ph) * 3.6 / 100); left: 0; right: 0; text-align: center;
  font: 400 calc(var(--pw) * 3.1 / 100)/1 var(--f-display-sc); letter-spacing: 0.12em; color: var(--leaf-soft);
}
.folio {
  position: absolute; bottom: calc(var(--ph) * 3 / 100); left: 0; right: 0; text-align: center;
  font: 400 calc(var(--pw) * 3.4 / 100)/1 var(--f-display); color: var(--leaf-soft); font-variant-numeric: oldstyle-nums;
}
.art { position: relative; flex: 0 0 auto; display: flex; align-items: center; justify-content: center; gap: calc(var(--pw) * 0.04); }
.art img { max-width: 100%; max-height: calc(var(--ph) * 0.4 * var(--fit, 1)); width: auto; height: auto; object-fit: contain; position: relative; z-index: 1; }
.art.small img { max-height: calc(var(--ph) * 0.27 * var(--fit, 1)); }
.art.tall img { max-height: calc(var(--ph) * 0.62 * var(--fit, 1)); }
.art.stack { flex-direction: column; gap: calc(var(--ph) * 0.02); }
.art.stack img { max-height: calc(var(--ph) * 0.2 * var(--fit, 1)); }
.endpaper .art img { max-height: calc(var(--ph) * 0.7); }
.text { flex: 0 1 auto; display: flex; flex-direction: column; gap: 0.7em; min-height: 0; }
.text p { margin: 0; text-wrap: pretty; hyphens: auto; -webkit-hyphens: auto; }
.text .dropcap::first-letter { font-family: var(--f-display); float: left; font-size: 3.3em; line-height: 0.82; padding: 0.05em 0.08em 0 0; color: var(--leaf-accent); }
.text .song { font-family: var(--f-display); font-style: italic; text-align: center; line-height: 1.35; font-size: 1.08em; }
.text .aside { color: var(--leaf-soft); }
.text .big { font-family: var(--f-display); font-size: 2.6em; line-height: 1; text-align: center; color: var(--leaf-accent); letter-spacing: 0.02em; }
.text .center { text-align: center; }
.whisper { font-style: italic; font-weight: 600; color: var(--leaf-accent); display: inline-block; }
.bump { display: inline-block; font-style: italic; }
.end-mark { text-align: center; font: 400 1.2em var(--f-display-sc); letter-spacing: 0.2em; color: var(--leaf-accent); }

/* Chapter opening page */
.chapter-head { text-align: center; display: grid; gap: calc(var(--ph) * 1.6 / 100); }
.chapter-head .ch { font: 400 calc(var(--pw) * 5.4 / 100)/1 var(--f-display-sc); letter-spacing: 0.2em; color: var(--leaf-accent); }
.chapter-head .in-which { font: italic 400 calc(var(--pw) * 4.3 / 100)/1.35 var(--f-display); text-wrap: balance; }
.chapter-head .rule { width: calc(var(--pw) * 18 / 100); height: 1px; background: var(--leaf-soft); margin: 0 auto; opacity: 0.5; }
.chapter-head .note { font-size: calc(var(--pw) * 3 / 100); color: var(--leaf-soft); }

/* Cover */
.side.cover, .side.backcover {
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.025) 0 2px, transparent 2px 4px),
    repeating-linear-gradient(-45deg, rgba(0,0,0,0.05) 0 2px, transparent 2px 5px),
    radial-gradient(ellipse at 40% 30%, var(--cloth), var(--cloth-dark));
  color: var(--gilt);
  border-radius: 3px 8px 8px 3px;
}
.side.backcover { border-radius: 8px 3px 3px 8px; }
.cover-inner {
  position: absolute; inset: calc(var(--pw) * 5 / 100); border: 1px solid rgba(220,183,94,0.55); outline: 1px solid rgba(220,183,94,0.35); outline-offset: -2.calc(var(--pw) * 4 / 100);
  display: flex; flex-direction: column; align-items: center; justify-content: space-between; text-align: center; padding: calc(var(--ph) * 10 / 100) calc(var(--pw) * 8 / 100) calc(var(--ph) * 8 / 100);
}
.cover-title { font: 400 calc(var(--pw) * 9.4 / 100)/1.05 var(--f-display-sc); letter-spacing: 0.06em; text-shadow: 0 1px 0 rgba(0,0,0,0.35); }
.cover-by { font: italic 400 calc(var(--pw) * 4.6 / 100)/1.3 var(--f-display); opacity: 0.9; }
.cover-art {
  width: calc(var(--pw) * 56 / 100); aspect-ratio: 439 / 300; position: relative;
  background: linear-gradient(160deg, #f0d68a, var(--gilt) 45%, #b88f3a);
  -webkit-mask: url("../illustrations/ink/illus12.webp") center / contain no-repeat;
  mask: url("../illustrations/ink/illus12.webp") center / contain no-repeat;
}
.cover-badge { font: 400 calc(var(--pw) * 3.4 / 100)/1.3 var(--f-display-sc); letter-spacing: 0.18em; }
.cover-badge b { display: block; font: 400 calc(var(--pw) * 7 / 100)/1 var(--f-display); letter-spacing: 0.04em; }
.backcover .cover-inner { justify-content: center; gap: calc(var(--ph) * 4 / 100); }
.backcover p { margin: 0; font: italic calc(var(--pw) * 4.2 / 100)/1.4 var(--f-display); }

/* Endpapers */
.side.endpaper { background: #efe6c8; }
.endpaper .page { padding: calc(var(--ph) * 7 / 100) calc(var(--pw) * 7 / 100); }
.endpaper .caption { text-align: center; font: italic calc(var(--pw) * 3.4 / 100)/1.3 var(--f-display); color: var(--leaf-soft); }

/* Birthday page */
.birthday .page { justify-content: center; text-align: center; }
.birthday h2 { margin: 0; font: 400 calc(var(--pw) * 8 / 100)/1.05 var(--f-display); color: var(--leaf-ink); text-wrap: balance; }
.birthday .years { font: 400 calc(var(--pw) * 4 / 100)/1 var(--f-display-sc); letter-spacing: 0.2em; color: var(--leaf-accent); }
.birthday .pot { width: calc(var(--pw) * 42 / 100); margin: 0 auto; }
.birthday .links { display: flex; flex-direction: column; gap: calc(var(--ph) * 1.2 / 100); align-items: center; font-size: 0.9em; }
.birthday a, .birthday button.linkish { color: var(--leaf-accent); font: inherit; background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; text-underline-offset: 0.18em; }
.flame { transform-origin: 50% 100%; }
.live .flame { animation: flicker 0.18s ease-in-out infinite alternate; }
@keyframes flicker { from { transform: scale(1, 1) rotate(-2deg); } to { transform: scale(0.92, 1.08) rotate(2deg); } }

/* ---------- Live effects (only on the pages in view) ---------- */
.fx { position: absolute; inset: 0; pointer-events: none; z-index: 4; overflow: hidden; }
.fx .bee { position: absolute; width: calc(var(--pw) * 3.6 / 100); height: calc(var(--pw) * 3.6 / 100); color: #2b241a; }
.fx .bee svg { width: 100%; height: 100%; display: block; }
.fx .drop { position: absolute; top: -10%; width: 1px; height: calc(var(--ph) * 4 / 100); background: linear-gradient(to bottom, transparent, rgba(70,100,140,0.65)); }
.fx .note { position: absolute; font-family: var(--f-display); color: var(--leaf-accent); font-size: calc(var(--pw) * 6 / 100); }
.fx .balloon { position: absolute; bottom: -25%; width: calc(var(--pw) * 14 / 100); }
.fx .pop { position: absolute; font: 400 calc(var(--pw) * 9 / 100)/1 var(--f-display-sc); color: var(--leaf-accent); letter-spacing: 0.05em; }

.live .text > * { animation: ink-in 0.7s ease both; animation-delay: calc(0.25s + var(--n, 0) * 0.32s); }
.live .art img { animation: sketch-in 1.1s cubic-bezier(.3,.6,.3,1) both; }
@keyframes ink-in { from { opacity: 0; transform: translateY(0.4em); filter: blur(1.5px); } to { opacity: 1; transform: none; filter: none; } }
@keyframes sketch-in { from { clip-path: inset(0 100% 0 0); opacity: 0.4; } to { clip-path: inset(0 0 0 0); opacity: 1; } }

.live[data-fx~="float"] .art img { animation: sketch-in 1.1s both, bob 4.5s ease-in-out 1.1s infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-1.2deg); } 50% { transform: translateY(calc(var(--ph) * -0.04)) rotate(1.2deg); } }
.live[data-fx~="climb"] .art img { animation: sketch-in 1.1s both, climb 6s ease-in-out 1.1s infinite alternate; }
@keyframes climb { from { transform: translateY(calc(var(--ph) * 3 / 100)); } to { transform: translateY(calc(var(--ph) * -0.03)); } }
.live[data-fx~="fall"] .art img { animation: fall 1.4s cubic-bezier(.5,0,.6,1) 0.9s both; }
@keyframes fall {
  0% { transform: translateY(-40%) rotate(-25deg); opacity: 0; }
  55% { transform: translateY(4%) rotate(8deg); opacity: 1; }
  75% { transform: translateY(-3%) rotate(-3deg); }
  100% { transform: none; }
}
.live[data-fx~="fall"] .big { animation: ink-in 0.3s both, shake 0.5s ease 0.3s 2; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-4%) rotate(-3deg); } 40% { transform: translateX(4%) rotate(2deg); } 60% { transform: translateX(-3%); } 80% { transform: translateX(2%); } }
.live[data-fx~="tumble"] .text > * { animation-name: tumble; animation-duration: 0.8s; animation-delay: calc(0.3s + var(--n, 0) * 0.55s); }
@keyframes tumble { from { opacity: 0; transform: translateY(-1.4em) rotate(-4deg); } 60% { opacity: 1; transform: translateY(0.15em) rotate(1deg); } to { opacity: 1; transform: none; } }
.live .bump { animation: bump 0.55s ease-in-out infinite; animation-delay: calc(var(--b, 0) * 0.18s); }
@keyframes bump { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-0.35em); } 60% { transform: translateY(0.05em); } }
.live .whisper { animation: whisper 2.4s ease-in-out 2s infinite; }
@keyframes whisper { 0%, 100% { transform: scale(1); text-shadow: none; } 50% { transform: scale(1.12); text-shadow: 0 0 0.6em rgba(232,170,40,0.55); } }
.live[data-fx~="walk"] .art img:last-child { animation: walk 3.4s steps(9, end) 0.8s both; }
@keyframes walk { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
.fx-back { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.fx-back .inflate { position: absolute; border-radius: 50%; translate: -50% -50%;
  background: radial-gradient(circle at 36% 32%, #e2eef9, var(--sky) 72%); }
.sky-wash { position: absolute; inset: 0; z-index: 0; background: radial-gradient(ellipse at 50% 30%, rgba(111,153,200,0.28), transparent 70%); opacity: 0; transition: opacity 2s ease; }
.live .sky-wash { opacity: 1; }

/* ---------- Controls ---------- */
.controls { display: flex; align-items: center; justify-content: center; gap: 1rem; padding-top: 0.6rem; }
.turn {
  width: 2.8rem; height: 2.8rem; border-radius: 50%; border: 1px solid var(--line); background: transparent; cursor: pointer;
  display: grid; place-items: center; color: var(--chrome);
}
.turn:hover:not(:disabled) { background: var(--chrome); color: var(--room); }
.turn:disabled { opacity: 0.35; cursor: default; }
.turn svg { width: 1.2rem; height: 1.2rem; }
.progress { display: grid; gap: 0.35rem; justify-items: center; min-width: 9rem; }
.progress span:first-child { font: italic 400 0.95rem/1 var(--f-display); color: var(--chrome-soft); font-variant-numeric: tabular-nums; }
.track { width: 9rem; height: 2px; background: var(--line); border-radius: 2px; overflow: hidden; }
.fill { display: block; height: 100%; width: 0; background: var(--honey); transition: width 0.6s ease; }
.hint { text-align: center; margin: 0.35rem 0 0; font-size: 0.82rem; color: var(--chrome-soft); }

@media (prefers-reduced-motion: reduce) {
  .book, .leaf { transition-duration: 0.01s !important; }
  .live *, .live .art img, .fx * { animation: none !important; }
  .fx, .fx-back { display: none; }
}
