:root {
  color-scheme: light;
  --ink: #1c1915;
  --muted: #6d645b;
  --line: #e4dcd2;
  --paper: #f6f1ea;
  --accent: #b5452d;
  --accent-ink: #fff8f4;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Songti SC", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, #fff, transparent 42%),
    var(--paper);
}

main, #working {
  width: min(760px, calc(100% - 32px));
  margin: 0 auto;
  padding: 40px 0 64px;
}

.eyebrow {
  margin: 0 0 8px;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
}

h1, h2 {
  margin: 0;
  font-weight: 650;
  letter-spacing: -0.03em;
}

h1 { font-size: 40px; }

.lead, .status {
  font-family: "Avenir Next", "PingFang SC", sans-serif;
}

.lead {
  max-width: 62ch;
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.6;
}

label {
  display: block;
  margin: 28px 0 8px;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 13px;
}

textarea {
  width: 100%;
  min-height: 220px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  font: 16px/1.6 "Avenir Next", "PingFang SC", sans-serif;
  resize: vertical;
}

button {
  height: 46px;
  margin-top: 16px;
  padding: 0 18px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font: 600 15px/1 "Avenir Next", "PingFang SC", sans-serif;
  cursor: pointer;
}

button:disabled { opacity: 0.45; cursor: default; }

.status {
  min-height: 1.4em;
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.status.error { color: var(--accent); }

#player {
  width: min(1100px, calc(100% - 24px));
  margin: 16px auto 32px;
}

.frame {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: 18px;
  background: #1c1915;
  cursor: pointer;
}

.leave {
  position: absolute;
  z-index: 6;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 44px;
  margin: 0;
  padding: 0 14px 0 6px;
  border: 2px solid rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  background: rgba(255, 246, 238, 0.95);
  color: #9a3b28;
  box-shadow: 0 8px 18px rgba(28, 18, 12, 0.28);
  font: 700 14px/1 "Avenir Next", "PingFang SC", sans-serif;
}

.leave-face {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #ffd3be;
  animation: leave-bob 2.8s ease-in-out infinite;
}

.leave-face::before {
  content: "";
  position: absolute;
  top: -6px;
  left: 3px;
  width: 10px;
  height: 12px;
  border-radius: 50% 50% 42% 42%;
  background: #ffd3be;
  box-shadow: 16px 0 0 #ffd3be;
}

.leave-face::after {
  content: "";
  position: absolute;
  left: 11px;
  bottom: 7px;
  width: 10px;
  height: 5px;
  border-bottom: 2px solid #d46b62;
  border-radius: 0 0 10px 10px;
}

.leave-face i {
  position: absolute;
  top: 11px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #5c3a32;
}

.leave-face i:first-child { left: 8px; }
.leave-face i:last-child { right: 8px; }

.ending {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(22, 16, 12, 0.46);
}

.ending[hidden] { display: none; }

.ending-card {
  width: min(320px, 100%);
  padding: 22px 22px 18px;
  border: 2px solid rgba(255, 255, 255, 0.92);
  border-radius: 28px;
  background: rgba(255, 246, 238, 0.97);
  color: #9a3b28;
  text-align: center;
  box-shadow: 0 16px 36px rgba(28, 18, 12, 0.35);
}

.ending-face {
  position: relative;
  display: inline-block;
  width: 64px;
  height: 64px;
  margin-bottom: 8px;
  border-radius: 50%;
  background: #ffd3be;
  animation: leave-bob 2.8s ease-in-out infinite;
}

.ending-face::before {
  content: "";
  position: absolute;
  top: -12px;
  left: 8px;
  width: 18px;
  height: 22px;
  border-radius: 50% 50% 42% 42%;
  background: #ffd3be;
  box-shadow: 30px 0 0 #ffd3be;
}

.ending-face::after {
  content: "";
  position: absolute;
  left: 22px;
  bottom: 14px;
  width: 20px;
  height: 10px;
  border-bottom: 3px solid #d46b62;
  border-radius: 0 0 16px 16px;
}

.ending-face i {
  position: absolute;
  top: 22px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #5c3a32;
}

.ending-face i:first-child { left: 16px; }
.ending-face i:last-child { right: 16px; }

.ending-card h2 {
  margin: 0;
  font-size: 28px;
  color: #1c1915;
}

.ending-card p {
  margin: 8px 0 0;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 14px;
  color: var(--muted);
}

#ending-close {
  height: 40px;
  margin: 16px 0 0;
  padding: 0 22px;
}

@keyframes leave-bob {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  50% { transform: translateY(-2px) rotate(-6deg); }
}

#preview {
  margin-top: 18px;
  cursor: default;
}

#preview[hidden] { display: none; }

#preview-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#preview-actor {
  position: absolute;
  left: 50%;
  bottom: 0;
  height: 92%;
  max-width: 48%;
  transform: translateX(-50%);
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 12px 18px rgba(0, 0, 0, 0.35));
}

#preview-critter {
  position: absolute;
  left: 8%;
  bottom: 8%;
  height: 24%;
  max-width: 22%;
  object-fit: contain;
  object-position: center bottom;
  animation: critter-hop 1.1s ease-in-out infinite;
}

#bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 0;
}

#bg.is-drift { animation: drift 22s ease-in-out infinite alternate; }
#bg.is-push { animation: cam-push 16s ease-in-out infinite alternate; }
#bg.is-pull { animation: cam-pull 16s ease-in-out infinite alternate; }
#bg.is-pan-left { animation: cam-left 14s ease-in-out infinite alternate; }
#bg.is-pan-right { animation: cam-right 14s ease-in-out infinite alternate; }
#bg.is-sway { animation: cam-sway 7s ease-in-out infinite; }

#actor-wrap.is-bow { animation: actor-bow 0.9s ease; }
#actor-wrap.is-lean { animation: actor-lean 1.1s ease; }
#actor-wrap.is-recoil { animation: actor-recoil 0.45s ease; }
#actor-wrap.is-step { animation: actor-step 0.7s ease; }

#glow,
#fx,
#flash {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#glow {
  z-index: 1;
  background: radial-gradient(ellipse at 50% 78%, rgba(255, 148, 64, 0.34), transparent 58%);
  opacity: 0;
  transition: opacity 0.45s ease;
}

#glow.is-on { opacity: 1; }

#fx { z-index: 2; }

#flash {
  z-index: 4;
  background: #fff8ee;
  opacity: 0;
}

#flash.is-on { animation: flash 0.45s ease; }

#critters {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.critter {
  position: absolute;
  bottom: 10%;
  height: 26%;
  max-width: 22%;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, 0.28));
}

.critter.is-hop { animation: critter-hop 1.1s ease-in-out infinite; }
.critter.is-fly { animation: critter-fly 7s ease-in-out infinite alternate; }
.critter.is-swim { animation: critter-swim 5.5s ease-in-out infinite; }
.critter.is-walk { animation: critter-walk 8s linear infinite; }
.critter.is-scurry { animation: critter-scurry 3.4s linear infinite; }

@keyframes critter-hop {
  0%, 100% { transform: translateY(0); }
  45% { transform: translateY(-26px); }
}

@keyframes critter-fly {
  from { transform: translate3d(0, 8px, 0); }
  to { transform: translate3d(180px, -70px, 0); }
}

@keyframes critter-swim {
  0% { transform: translateX(0) translateY(0); }
  50% { transform: translateX(120px) translateY(-12px); }
  100% { transform: translateX(0) translateY(0); }
}

@keyframes critter-walk {
  0% { transform: translateX(0); }
  100% { transform: translateX(160px); }
}

@keyframes critter-scurry {
  0% { transform: translateX(0) translateY(0); }
  25% { transform: translateX(40px) translateY(-10px); }
  50% { transform: translateX(90px) translateY(0); }
  75% { transform: translateX(130px) translateY(-8px); }
  100% { transform: translateX(170px) translateY(0); }
}

#actor-wrap {
  position: absolute;
  left: 50%;
  bottom: 0;
  z-index: 2;
  height: 92%;
  max-width: 48%;
  transform: translateX(-50%);
  pointer-events: none;
}

#actor-wrap.is-enter { animation: arrive 0.65s ease; }

#actor {
  height: 100%;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 12px 18px rgba(0, 0, 0, 0.35));
  animation: bob 3.6s ease-in-out infinite;
}

.frame.is-shake { animation: shake 0.42s linear; }

.dialogue {
  position: absolute;
  z-index: 3;
  left: 16px;
  right: 16px;
  bottom: 16px;
  padding: 14px 16px 12px;
  border-radius: 14px;
  background: rgba(22, 16, 12, 0.82);
  color: #f6f1ea;
}

#speaker {
  margin: 0 0 6px;
  color: #f0b7a4;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 13px;
  font-weight: 650;
}

#line {
  margin: 0;
  min-height: 3.2em;
  padding-right: 118px;
  font-size: 18px;
  line-height: 1.6;
}

#continue {
  position: absolute;
  right: 10px;
  bottom: 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 44px;
  margin: 0;
  padding: 0 14px 0 6px;
  border: 2px solid rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  background: rgba(255, 246, 238, 0.95);
  color: #9a3b28;
  box-shadow: 0 8px 18px rgba(28, 18, 12, 0.28);
  font: 700 14px/1 "Avenir Next", "PingFang SC", sans-serif;
}

#continue[hidden] { display: none; }

.char {
  color: rgba(246, 241, 234, 0.42);
}

.char.is-read {
  color: #f6f1ea;
}

.char.is-current {
  color: #ffb087;
}

.player-bar,
.share,
.music-credit {
  display: none;
}

.player-bar button { margin-top: 0; }

#back, #speech, #music {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

.music-credit {
  margin: 10px 0 0;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 13px;
  color: var(--muted);
}

.music-credit a {
  color: inherit;
}

#voice,
#bgm { display: none; }

.share {
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin: 12px 0 0;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 13px;
  color: var(--muted);
}

.share a {
  color: var(--accent);
  word-break: break-all;
}

#copy { margin-top: 0; }

#editor.is-home {
  width: min(1080px, calc(100% - 28px));
}

#editor.is-home #library { margin-top: 8px; }
#editor.is-home #library h2 { display: none; }

#library { margin-top: 36px; }

#library h2 {
  font-size: 22px;
}

#templates {
  display: grid;
  grid-template-columns: repeat(auto-fit, 134px);
  align-items: end;
  justify-content: center;
  gap: 36px 0;
  margin: 22px 0 0;
  padding: 28px 18px 26px;
  list-style: none;
  background:
    linear-gradient(#4a2f20, #4a2f20) 0 0 / 16px 100% no-repeat,
    linear-gradient(#4a2f20, #4a2f20) 100% 0 / 16px 100% no-repeat,
    linear-gradient(#7a5136, #5c3b26);
  overflow: hidden;
  border-radius: 6px;
  box-shadow:
    inset 0 16px 0 #3d2618,
    inset 0 -18px 0 #3d2618,
    0 18px 36px rgba(48, 26, 12, 0.16);
}

#templates:empty { display: none; }

#templates li.book {
  --cloth: #8d3228;
  position: relative;
  justify-self: center;
  width: 118px;
  margin: 0;
  padding: 0 0 10px;
  overflow: visible;
  background: transparent;
}

#templates li.book:nth-child(7n+2) { --cloth: #1e3a4c; }
#templates li.book:nth-child(7n+3) { --cloth: #2c4a38; }
#templates li.book:nth-child(7n+4) { --cloth: #a15c32; }
#templates li.book:nth-child(7n+5) { --cloth: #6b3f55; }
#templates li.book:nth-child(7n+6) { --cloth: #8a6a32; }
#templates li.book:nth-child(7n) { --cloth: #3d342c; }

#templates li.book:nth-child(5n+1) .book-link { height: 168px; }
#templates li.book:nth-child(5n+2) .book-link { height: 186px; }
#templates li.book:nth-child(5n+3) .book-link { height: 176px; }
#templates li.book:nth-child(5n+4) .book-link { height: 194px; }
#templates li.book:nth-child(5n) .book-link { height: 160px; }

#templates li.book::after {
  content: "";
  position: absolute;
  z-index: 0;
  left: -12px;
  right: -12px;
  bottom: 0;
  height: 16px;
  background: linear-gradient(#f4e0b8, #e2c088 28%, #b07d45 62%, #6d4528);
  box-shadow:
    0 8px 0 #3e2718,
    0 14px 12px rgba(28, 14, 6, 0.32);
}

.book-link {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: stretch;
  height: 176px;
  color: var(--ink);
  text-decoration: none;
  filter: drop-shadow(5px 8px 4px rgba(22, 10, 4, 0.38));
  transition: transform 0.18s ease;
}

.book-link:hover { transform: translateY(-10px); }

.book-spine {
  width: 16px;
  flex: none;
  border-radius: 3px 0 0 2px;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.4), transparent 42%, rgba(255, 255, 255, 0.2) 68%, rgba(0, 0, 0, 0.28)),
    var(--cloth);
  box-shadow: inset -1px 0 0 rgba(255, 255, 255, 0.18);
}

.book-spine::before,
.book-spine::after {
  content: "";
  display: block;
  height: 2px;
  margin: 14px 3px 0;
  background: rgba(255, 236, 210, 0.55);
}

.book-spine::after { margin-top: 5px; }

.book-cover {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  border-radius: 0 3px 3px 0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.16), transparent 28%),
    var(--cloth);
}

.book-cover::after {
  content: "";
  position: absolute;
  z-index: 1;
  top: 2px;
  right: 0;
  bottom: 2px;
  width: 5px;
  background: repeating-linear-gradient(90deg, #f7f1e6 0 1px, #ddd4c6 1px 2px);
}

.book-cover img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover.is-blank {
  display: flex;
  align-items: center;
  justify-content: center;
}

#templates .card-title {
  position: absolute;
  z-index: 2;
  right: 7px;
  bottom: 0;
  left: 0;
  padding: 28px 8px 8px;
  background: linear-gradient(transparent, rgba(20, 12, 8, 0.9));
  color: #fff8f2;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.3;
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.book-cover.is-blank .card-title {
  position: static;
  display: block;
  padding: 10px 12px 10px 8px;
  background: none;
  text-align: center;
  color: #fff6ee;
}

.card-actions {
  position: absolute;
  z-index: 3;
  top: 6px;
  right: 6px;
  left: 20px;
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

#templates li.book:hover .card-actions,
#templates li.book:focus-within .card-actions {
  opacity: 1;
}

.card-actions button,
.card-title-input {
  height: 32px;
  margin: 0;
  border-radius: 999px;
  font: 600 13px/1 "Avenir Next", "PingFang SC", sans-serif;
}

.card-actions button {
  height: 24px;
  padding: 0 7px;
  border: 1px solid rgba(255, 248, 242, 0.7);
  background: rgba(255, 248, 242, 0.94);
  color: var(--ink);
  font-size: 11px;
}

.card-delete { color: var(--accent); }

.card-title:has(input) {
  display: block;
  -webkit-line-clamp: unset;
}

.card-title-input {
  width: 100%;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

#progress {
  margin-left: auto;
  font-family: "Avenir Next", "PingFang SC", sans-serif;
  font-size: 13px;
  color: var(--muted);
}

@keyframes drift {
  from { transform: scale(1.05); }
  to { transform: scale(1.12) translate3d(-1.6%, -1%, 0); }
}

@keyframes cam-push {
  from { transform: scale(1.04); }
  to { transform: scale(1.18); }
}

@keyframes cam-pull {
  from { transform: scale(1.16); }
  to { transform: scale(1.04); }
}

@keyframes cam-left {
  from { transform: scale(1.1) translateX(2%); }
  to { transform: scale(1.1) translateX(-3%); }
}

@keyframes cam-right {
  from { transform: scale(1.1) translateX(-2%); }
  to { transform: scale(1.1) translateX(3%); }
}

@keyframes cam-sway {
  0%, 100% { transform: scale(1.06) rotate(-0.6deg); }
  50% { transform: scale(1.06) rotate(0.6deg); }
}

@keyframes actor-bow {
  0%, 100% { transform: translateX(-50%) translateY(0) rotate(0deg); }
  40% { transform: translateX(-50%) translateY(16px) rotate(7deg); }
}

@keyframes actor-lean {
  0%, 100% { transform: translateX(-50%) rotate(0deg); }
  50% { transform: translateX(-46%) rotate(-4deg); }
}

@keyframes actor-recoil {
  0%, 100% { transform: translateX(-50%); }
  35% { transform: translateX(-62%) translateY(6px); }
}

@keyframes actor-step {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  45% { transform: translateX(-42%) translateY(8px); }
}

@keyframes arrive {
  from { opacity: 0; transform: translateX(-50%) translateY(28px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

@keyframes flash {
  0% { opacity: 0; }
  18% { opacity: 0.5; }
  100% { opacity: 0; }
}

@media (hover: none) {
  .card-actions { opacity: 1; }
}

@media (max-width: 760px) {
  h1 { font-size: 32px; }
  .frame { aspect-ratio: 3 / 4; }
  #actor-wrap { height: 70%; max-width: 78%; }
  #line { font-size: 16px; }
  #templates {
    grid-template-columns: repeat(auto-fit, 108px);
    gap: 28px 0;
    padding: 22px 14px 20px;
  }
  #templates li.book { width: 92px; margin: 0; }
  #templates li.book:nth-child(5n+1) .book-link { height: 140px; }
  #templates li.book:nth-child(5n+2) .book-link { height: 156px; }
  #templates li.book:nth-child(5n+3) .book-link { height: 148px; }
  #templates li.book:nth-child(5n+4) .book-link { height: 164px; }
  #templates li.book:nth-child(5n) .book-link { height: 136px; }
  #templates .card-title { font-size: 12px; }
}
