:root {
  --night: 0;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --ink: #3a2c1c;
  --ink-soft: #7a6448;
  --paper: #f7eed7;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #141c44;
  font-family: var(--serif);
  -webkit-tap-highlight-color: transparent;
}

#scene {
  position: fixed;
  inset: 0;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
}

.hint {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top, 0px) + 4.5vh);
  transform: translateX(-50%);
  margin: 0;
  width: max-content;
  max-width: 88vw;
  text-align: center;
  color: #fff8e8;
  font-size: clamp(1rem, 2.4vh, 1.25rem);
  font-style: italic;
  letter-spacing: 0.02em;
  line-height: 1.45;
  text-shadow: 0 1px 12px rgba(0, 0, 0, 0.55), 0 0 2px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  transition: opacity 0.8s ease, transform 0.8s ease;
  animation: hint-in 2.2s 0.4s both;
}

.hint.hidden {
  opacity: 0;
  transform: translate(-50%, -6px);
}

@keyframes hint-in {
  from {
    opacity: 0;
  }
}

.corner {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 50%;
  background: rgba(20, 24, 40, 0.22);
  color: rgba(255, 252, 240, 0.9);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  cursor: pointer;
  z-index: 3;
  transition: background 0.2s;
}

.corner:hover,
.corner:focus-visible {
  background: rgba(20, 24, 40, 0.45);
}

.corner svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

/* ---- rub the coin ---------------------------------------------------------- */

.rub-open {
  position: fixed;
  left: calc(env(safe-area-inset-left, 0px) + 14px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  z-index: 3;
  padding: 7px 14px;
  font: inherit;
  font-size: 0.88rem;
  font-style: italic;
  letter-spacing: 0.02em;
  color: rgba(255, 252, 240, 0.9);
  background: rgba(20, 24, 40, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  cursor: pointer;
  transition: opacity 0.5s ease, visibility 0.5s, background 0.2s;
  animation: hint-in 2.2s 0.8s both;
}

.rub-open:hover,
.rub-open:focus-visible {
  background: rgba(20, 24, 40, 0.45);
}

.rub-open.away {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.rub-layer {
  position: fixed;
  inset: 0;
  z-index: 4;
  background: radial-gradient(ellipse at 50% 47%, rgba(30, 22, 12, 0.55), rgba(8, 8, 18, 0.82) 75%);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: rub-in 0.5s ease both;
}

.rub-layer[hidden] {
  display: none;
}

@keyframes rub-in {
  from {
    opacity: 0;
  }
}

.rub-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}

.rub-done {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 5vh);
  transform: translateX(-50%);
  padding: 10px 22px;
  font: inherit;
  font-size: 1rem;
  color: #fff8e8;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 240, 200, 0.4);
  border-radius: 999px;
  cursor: pointer;
}

.rub-done:hover,
.rub-done:focus-visible {
  background: rgba(255, 255, 255, 0.16);
}

/* ---- slip ---------------------------------------------------------------- */

.slip-layer {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: calc(env(safe-area-inset-top, 0px) + 64px) 18px calc(env(safe-area-inset-bottom, 0px) + 28px);
  background: radial-gradient(ellipse at center, rgba(10, 8, 20, 0.28), rgba(10, 8, 20, 0.05) 70%);
}

.slip-layer[hidden] {
  display: none;
}

.slip {
  --half: 160px;
  --from: 200px;
  position: relative;
  width: min(86vw, 29rem);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  filter: drop-shadow(0 18px 30px rgba(0, 0, 0, 0.4));
}

.paper {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: calc(100dvh - 140px);
  color: var(--ink);
  background:
    radial-gradient(ellipse at 20% 15%, rgba(255, 255, 255, 0.55), transparent 55%),
    radial-gradient(ellipse at 85% 90%, rgba(186, 146, 84, 0.22), transparent 60%),
    repeating-linear-gradient(0deg, rgba(120, 90, 40, 0.025) 0 2px, transparent 2px 5px),
    var(--paper);
  box-shadow: inset 0 0 40px rgba(140, 100, 40, 0.22), inset 0 0 3px rgba(120, 80, 30, 0.35);
  border-radius: 2px;
}

.paper-scroll {
  overflow-y: auto;
  padding: 30px 28px 6px;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(122, 100, 72, 0.4) transparent;
}

.roll {
  position: absolute;
  left: -7px;
  right: -7px;
  height: 13px;
  border-radius: 7px;
  background: linear-gradient(180deg, #e6d6b2, #fbf3dd 35%, #c9b083 75%, #a88c5c);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  z-index: 1;
}

.roll-top {
  top: -6px;
}

.roll-bottom {
  bottom: -6px;
}

.slip-heading {
  margin: 0 0 14px;
  text-align: center;
  font-variant: small-caps;
  letter-spacing: 0.12em;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.slip-heading[hidden] {
  display: none;
}

.slip-text p {
  margin: 0 0 0.8em;
  white-space: pre-line;
  text-wrap: pretty;
  line-height: 1.55;
}

.slip[data-size="l"] .slip-text {
  font-size: clamp(1.3rem, 5.2vw, 1.6rem);
  text-align: center;
}

.slip[data-size="m"] .slip-text {
  font-size: clamp(1.08rem, 4.2vw, 1.22rem);
}

.slip[data-size="s"] .slip-text {
  font-size: clamp(0.98rem, 3.8vw, 1.06rem);
}

.slip-author {
  margin: 18px 0 2px;
  text-align: right;
  font-style: italic;
  font-size: 1.05rem;
}

.slip-cite {
  margin: 0 0 8px;
  text-align: right;
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--ink-soft);
}

.slip-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 20px 20px;
}

.slip-actions button {
  font: inherit;
  font-size: 0.9rem;
  color: var(--ink);
  background: transparent;
  border: 1px solid rgba(90, 64, 34, 0.35);
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
}

.slip-actions button:hover,
.slip-actions button:focus-visible {
  background: rgba(120, 90, 40, 0.1);
}

.slip-actions button[hidden] {
  visibility: hidden;
  display: block;
}

.seal {
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  background:
    linear-gradient(#f7eed7, #f7eed7) center / 7px 7px no-repeat,
    radial-gradient(circle, transparent 54%, #b3362a 56% 70%, transparent 72%),
    radial-gradient(circle, #b3362a 0 100%);
  opacity: 0.78;
  transform: rotate(-8deg);
}

/* arrival: rises from the well, then unrolls */
.slip.arriving {
  animation: slip-rise 0.8s cubic-bezier(0.2, 0.8, 0.25, 1) both;
}
.slip.arriving .paper {
  animation: paper-open 0.7s 0.75s cubic-bezier(0.3, 0.7, 0.3, 1) both;
}
.slip.arriving .roll-top {
  animation: roll-top-open 0.7s 0.75s cubic-bezier(0.3, 0.7, 0.3, 1) both;
}
.slip.arriving .roll-bottom {
  animation: roll-bottom-open 0.7s 0.75s cubic-bezier(0.3, 0.7, 0.3, 1) both;
}
.slip.arriving .paper-scroll,
.slip.arriving .slip-actions {
  animation: ink-in 1s 1.2s ease-out both;
}

/* departure: rolls up, sinks back into the well */
.slip.leaving {
  animation: slip-sink 0.55s 0.42s cubic-bezier(0.6, 0, 0.8, 0.4) both;
}
.slip.leaving .paper {
  animation: paper-close 0.42s ease-in both;
}
.slip.leaving .roll-top {
  animation: roll-top-close 0.42s ease-in both;
}
.slip.leaving .roll-bottom {
  animation: roll-bottom-close 0.42s ease-in both;
}
.slip.leaving .paper-scroll,
.slip.leaving .slip-actions {
  opacity: 0;
  transition: opacity 0.2s;
}

@keyframes slip-rise {
  from {
    transform: translateY(var(--from)) scale(0.12);
    opacity: 0;
  }
  35% {
    opacity: 1;
  }
}
@keyframes slip-sink {
  to {
    transform: translateY(var(--from)) scale(0.12);
    opacity: 0;
  }
}
@keyframes paper-open {
  from {
    clip-path: inset(var(--half) 0 var(--half) 0);
  }
  to {
    clip-path: inset(0 0 0 0);
  }
}
@keyframes paper-close {
  from {
    clip-path: inset(0 0 0 0);
  }
  to {
    clip-path: inset(var(--half) 0 var(--half) 0);
  }
}
@keyframes roll-top-open {
  from {
    transform: translateY(var(--half));
  }
}
@keyframes roll-bottom-open {
  from {
    transform: translateY(calc(-1 * var(--half)));
  }
}
@keyframes roll-top-close {
  to {
    transform: translateY(var(--half));
  }
}
@keyframes roll-bottom-close {
  to {
    transform: translateY(calc(-1 * var(--half)));
  }
}
@keyframes ink-in {
  from {
    opacity: 0;
    filter: blur(3px);
  }
}

/* ---- settings -------------------------------------------------------------- */

.settings {
  width: min(92vw, 26rem);
  max-height: min(86dvh, 40rem);
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  color: #f5efe0;
  background: rgba(24, 26, 44, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  font-family: var(--serif);
}

.settings::backdrop {
  background: rgba(8, 10, 20, 0.45);
}

.settings form {
  padding: 6px 20px 20px;
}

.settings header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0 6px;
  background: inherit;
}

.settings h2 {
  margin: 0;
  font-size: 1.3rem;
  font-weight: normal;
  letter-spacing: 0.03em;
}

.settings h3 {
  margin: 18px 0 8px;
  font-size: 0.78rem;
  font-weight: normal;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: #c9bfa6;
}

.settings .x {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  font-size: 1.7rem;
  line-height: 1;
  cursor: pointer;
}

.settings .x:hover,
.settings .x:focus-visible {
  background: rgba(255, 255, 255, 0.08);
}

.collections {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
}

.collections label,
.opt {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.collections label:hover,
.opt:hover {
  background: rgba(255, 255, 255, 0.06);
}

.collections span {
  display: grid;
  gap: 2px;
}

.collections strong {
  font-weight: normal;
  font-size: 1.02rem;
}

.collections small {
  font-size: 0.8rem;
  color: #b9b09a;
  line-height: 1.35;
}

.settings input[type="checkbox"] {
  flex: none;
  width: 20px;
  height: 20px;
  margin: 2px 0 0;
  accent-color: #d9a441;
}

.warn {
  margin: 8px 10px 0;
  font-size: 0.85rem;
  color: #f0b37a;
}

.warn[hidden] {
  display: none;
}

.note {
  margin: 0 10px 10px;
  font-size: 0.88rem;
  line-height: 1.45;
  color: #d8d0bc;
}

.ghost {
  margin-left: 10px;
  font: inherit;
  font-size: 0.9rem;
  color: inherit;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 999px;
  padding: 7px 14px;
  cursor: pointer;
}

.ghost:disabled {
  opacity: 0.4;
  cursor: default;
}

@media (prefers-reduced-motion: reduce) {
  .slip.arriving,
  .slip.arriving *,
  .slip.leaving,
  .slip.leaving * {
    animation-duration: 0.01s !important;
    animation-delay: 0s !important;
  }
}
