:root {
    /* Interactions are tactile story actions: tugging threads. */
    --ink-mulberry: #19111F;
    --candle-parchment: #F1D9A7;
    --blood-thread: #C83E4D;
    --lichen-green: #6F8F5F;
    --blue-ghost: #6DAEDB;
    --burnt-umber: #7A4A2A;
    --plum-shadow: #3A203F;
    --room-progress: 0;
    --needle-x: 50vw;
    --needle-y: 50vh;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    background: var(--ink-mulberry);
    color: var(--candle-parchment);
    overflow: hidden;
    cursor: none;
}

body {
    font-family: "Bricolage Grotesque", Inter, sans-serif;
    background:
        radial-gradient(circle at 62% 32%, rgba(241, 217, 167, 0.12), transparent 28rem),
        radial-gradient(circle at 18% 80%, rgba(58, 32, 63, 0.9), transparent 32rem),
        linear-gradient(135deg, #19111F 0%, #211426 46%, #120C17 100%);
}

button {
    font: inherit;
    cursor: none;
}

.paper-grain,
.paper-grain::before,
.paper-grain::after {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 20;
}

.paper-grain {
    opacity: 0.34;
    mix-blend-mode: overlay;
    background-image:
        repeating-linear-gradient(14deg, rgba(241, 217, 167, 0.035) 0 1px, transparent 1px 9px),
        repeating-linear-gradient(96deg, rgba(122, 74, 42, 0.065) 0 1px, transparent 1px 11px);
}

.paper-grain::before {
    content: "";
    background:
        radial-gradient(circle at 12% 16%, rgba(241, 217, 167, 0.08) 0 1px, transparent 2px),
        radial-gradient(circle at 72% 44%, rgba(241, 217, 167, 0.06) 0 1px, transparent 2px),
        radial-gradient(circle at 46% 76%, rgba(111, 143, 95, 0.05) 0 1px, transparent 2px);
    background-size: 97px 83px, 131px 117px, 171px 149px;
}

.paper-grain::after {
    content: "";
    opacity: 0.5;
    background: radial-gradient(circle at var(--needle-x) var(--needle-y), rgba(241, 217, 167, 0.18), transparent 16rem);
}

.lamp-halo {
    position: fixed;
    left: var(--needle-x);
    top: var(--needle-y);
    width: 28rem;
    height: 28rem;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: radial-gradient(circle, rgba(241, 217, 167, 0.22), rgba(200, 62, 77, 0.07) 32%, transparent 68%);
    filter: blur(8px);
    pointer-events: none;
    z-index: 4;
    transition: opacity 500ms ease;
}

.needle-cursor {
    position: fixed;
    left: var(--needle-x);
    top: var(--needle-y);
    width: 42px;
    height: 42px;
    z-index: 50;
    pointer-events: none;
    transform: translate(-12px, -11px) rotate(-32deg);
}

.needle-cursor::before {
    content: "";
    position: absolute;
    width: 38px;
    height: 4px;
    left: 0;
    top: 18px;
    border-radius: 99px;
    background: linear-gradient(90deg, #F1D9A7, #7A4A2A 72%, #3A203F);
    box-shadow: 0 0 14px rgba(241, 217, 167, 0.32);
}

.needle-cursor span {
    position: absolute;
    left: -28px;
    top: 20px;
    width: 36px;
    height: 2px;
    background: var(--blood-thread);
    transform-origin: right center;
}

.loom-stage {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    isolation: isolate;
}

.room {
    position: absolute;
    inset: 0;
    opacity: 0;
    transform: scale(1.02) rotate(calc((var(--room-progress) - 2) * 0.2deg));
    transition: opacity 900ms ease, transform 900ms cubic-bezier(.2,.8,.1,1);
    pointer-events: none;
}

.room.active {
    opacity: 1;
    transform: scale(1) rotate(0deg);
    pointer-events: auto;
}

.mulberry-table {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 7vw;
    filter: url(#paper-warp);
}

.catalog-mark {
    position: absolute;
    left: 7vw;
    top: 7vh;
    font-family: "DM Mono", monospace;
    font-size: clamp(0.65rem, 1vw, 0.8rem);
    color: rgba(241, 217, 167, 0.58);
    letter-spacing: 0.18em;
    text-transform: uppercase;
}

h1 {
    position: absolute;
    left: 9vw;
    bottom: 11vh;
    margin: 0;
    max-width: 8ch;
    font-family: Besley, serif;
    font-size: clamp(4.2rem, 10vw, 11rem);
    line-height: 0.82;
    letter-spacing: -0.07em;
    color: rgba(241, 217, 167, 0.1);
    text-shadow: 0 2px 0 rgba(122, 74, 42, 0.38);
}

.loose-fragment {
    position: relative;
    width: min(68vw, 880px);
    margin: 0;
    font-family: Besley, serif;
    font-size: clamp(2.4rem, 6vw, 6.8rem);
    line-height: 1.02;
    color: var(--candle-parchment);
    text-shadow: 0 0 28px rgba(241, 217, 167, 0.2), 0 4px 0 rgba(58, 32, 63, 0.78);
    transform: translate(-6vw, -4vh) rotate(-2deg);
    animation: fragmentDrift 9s ease-in-out infinite alternate;
}

.loose-fragment::first-letter {
    color: var(--blood-thread);
    font-size: 1.24em;
}

.loose-thread {
    position: absolute;
    left: 58vw;
    top: 55vh;
    width: 26vw;
    height: 2px;
    background: var(--blood-thread);
    transform: rotate(14deg) scaleX(calc(0.35 + var(--room-progress) * 0.13));
    transform-origin: left center;
    box-shadow: 0 0 16px rgba(200, 62, 77, 0.7);
}

.loose-thread::after {
    content: "";
    position: absolute;
    right: -10px;
    top: -5px;
    width: 12px;
    height: 12px;
    border: 2px solid var(--candle-parchment);
    border-radius: 50%;
    background: var(--burnt-umber);
}

.gesture-note {
    position: absolute;
    right: 7vw;
    bottom: 8vh;
    max-width: 18rem;
    color: rgba(241, 217, 167, 0.62);
    font: 600 0.82rem/1.4 "Bricolage Grotesque", sans-serif;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.loom-graph {
    position: absolute;
    inset: -7vh -8vw;
    opacity: calc((var(--room-progress) - 0.15) * 1.2);
    transform: translate(calc((var(--room-progress) - 3) * -1.2vw), calc((3 - var(--room-progress)) * 0.8vh)) rotate(calc(-9deg + var(--room-progress) * 1.7deg));
    transition: opacity 700ms ease, transform 900ms cubic-bezier(.2,.8,.1,1);
    z-index: 6;
    pointer-events: none;
}

.thread-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.main-thread,
.memory-arc,
.rumor-trail {
    fill: none;
    vector-effect: non-scaling-stroke;
}

.shadow-thread {
    stroke: rgba(122, 74, 42, 0.36);
    stroke-width: 17;
    stroke-linecap: round;
}

.draw-thread {
    stroke: var(--blood-thread);
    stroke-width: 5;
    stroke-linecap: round;
    filter: url(#threadGlow);
    stroke-dasharray: 1800;
    stroke-dashoffset: calc(1800 - (var(--room-progress) / 5) * 1800);
    transition: stroke-dashoffset 900ms cubic-bezier(.18,.8,.18,1);
}

.memory-arc {
    stroke: var(--blue-ghost);
    stroke-width: 3;
    opacity: calc(max(0, (var(--room-progress) - 2) * 0.32));
    stroke-dasharray: 12 20;
    filter: drop-shadow(0 0 9px rgba(109, 174, 219, 0.35));
}

.rumor-trail {
    stroke: var(--lichen-green);
    stroke-width: 4;
    opacity: calc(max(0, (var(--room-progress) - 1) * 0.26));
    stroke-dasharray: 2 18;
    stroke-linecap: round;
}

.stitch-holes circle {
    fill: var(--plum-shadow);
    stroke: var(--candle-parchment);
    stroke-width: 2;
    opacity: 0.78;
}

.node {
    position: absolute;
    width: clamp(64px, 7vw, 108px);
    height: clamp(64px, 7vw, 108px);
    border-radius: 48% 52% 45% 55%;
    background:
        radial-gradient(circle at 34% 28%, rgba(241, 217, 167, 0.6), transparent 18%),
        radial-gradient(circle, var(--blood-thread) 0 34%, var(--plum-shadow) 36% 54%, var(--burnt-umber) 55% 100%);
    box-shadow: inset 0 0 0 9px rgba(25, 17, 31, 0.42), 0 0 24px rgba(200, 62, 77, 0.44);
    transform: translate(-50%, -50%) scale(calc(0.7 + var(--room-progress) * 0.06));
    transition: transform 700ms cubic-bezier(.2,1.4,.2,1), filter 500ms ease;
    pointer-events: auto;
}

.node::before,
.node::after {
    content: attr(data-label);
    position: absolute;
    font-family: "DM Mono", monospace;
    font-size: 0.62rem;
    letter-spacing: 0.12em;
    color: var(--candle-parchment);
}

.node::before {
    right: -2.4rem;
    top: 0.2rem;
    transform: rotate(8deg);
}

.node::after {
    content: "";
    left: 50%;
    top: 50%;
    width: 70%;
    height: 70%;
    border: 1px dashed rgba(241, 217, 167, 0.42);
    border-radius: 50%;
    transform: translate(-50%, -50%);
}

.node span {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-family: "DM Mono", monospace;
    font-size: 0.72rem;
    color: var(--ink-mulberry);
}

.node.active {
    filter: brightness(1.25) saturate(1.25);
    transform: translate(-50%, -50%) scale(1.18) rotate(8deg);
}

.knot-origin { left: 12%; top: 82%; }
.knot-fork { left: 32%; top: 67%; }
.knot-echo { left: 46%; top: 45%; }
.knot-reversal { left: 66%; top: 31%; }
.knot-return { left: 84%; top: 63%; }

.paper-tab {
    position: absolute;
    pointer-events: auto;
    padding: 1.05rem 1.25rem;
    min-width: 9rem;
    border: 0;
    color: var(--plum-shadow);
    background:
        linear-gradient(135deg, rgba(241, 217, 167, 0.95), rgba(241, 217, 167, 0.72)),
        radial-gradient(circle at 12% 18%, rgba(122, 74, 42, 0.18), transparent 30%);
    clip-path: polygon(4% 6%, 94% 0, 100% 82%, 84% 100%, 2% 92%);
    box-shadow: 0 18px 32px rgba(0, 0, 0, 0.32), inset 0 -5px 0 rgba(122, 74, 42, 0.13);
    font: 700 0.76rem/1.15 "Bricolage Grotesque", sans-serif;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    transform-origin: 12% 0;
    transform: rotate(var(--tab-rot, -5deg)) scaleY(0.72);
    opacity: calc(max(0, var(--room-progress) - 0.5));
    transition: transform 500ms cubic-bezier(.25,1.5,.25,1), background 300ms ease;
}

.paper-tab.open,
.paper-tab:hover {
    transform: rotate(calc(var(--tab-rot, -5deg) + 7deg)) scaleY(1.08);
    background: linear-gradient(135deg, #F1D9A7, rgba(109, 174, 219, 0.72));
}

.tab-origin { left: 16%; top: 58%; --tab-rot: -9deg; }
.tab-fork { left: 35%; top: 38%; --tab-rot: 7deg; }
.tab-echo { left: 51%; top: 24%; --tab-rot: -4deg; }
.tab-reversal { left: 70%; top: 43%; --tab-rot: 10deg; }
.tab-return { left: 79%; top: 76%; --tab-rot: -8deg; }

.bead,
.word-bead {
    position: absolute;
    border-radius: 50%;
    pointer-events: none;
}

.bead {
    width: 24px;
    height: 24px;
    offset-path: path("M118 628 C224 500 308 530 398 409 S574 242 708 318 S878 508 1036 126");
    offset-rotate: 0deg;
    box-shadow: inset -5px -6px 0 rgba(25, 17, 31, 0.38), 0 0 18px currentColor;
    offset-distance: calc(var(--room-progress) * 14% + var(--bead-shift));
    transition: offset-distance 800ms cubic-bezier(.2,.9,.2,1);
}

.bead-red { color: #C83E4D; background: #C83E4D; --bead-shift: 8%; }
.bead-green { color: #6F8F5F; background: #6F8F5F; --bead-shift: 21%; width: 19px; height: 19px; }
.bead-blue { color: #6DAEDB; background: #6DAEDB; --bead-shift: 37%; width: 16px; height: 16px; }

.word-bead {
    left: calc(41% + var(--room-progress) * 6%);
    top: calc(48% - var(--room-progress) * 4%);
    padding: 0.42rem 0.7rem;
    width: auto;
    height: auto;
    border: 1px solid rgba(241, 217, 167, 0.5);
    background: rgba(58, 32, 63, 0.84);
    color: var(--candle-parchment);
    font: italic 1.05rem Alegreya, serif;
    box-shadow: 0 0 18px rgba(109, 174, 219, 0.22);
    transform: rotate(-7deg);
}

.moth {
    position: absolute;
    color: rgba(241, 217, 167, 0.34);
    font-family: Besley, serif;
    font-size: 4rem;
    pointer-events: none;
    animation: mothFloat 8s ease-in-out infinite alternate;
}

.moth-one { left: 22%; top: 22%; }
.moth-two { right: 17%; bottom: 20%; animation-delay: -3s; }

.chapter-slip {
    position: absolute;
    width: min(28rem, 82vw);
    padding: clamp(1.3rem, 3vw, 2.2rem);
    color: var(--plum-shadow);
    background:
        radial-gradient(circle at 90% 10%, rgba(122, 74, 42, 0.13), transparent 7rem),
        linear-gradient(145deg, rgba(241, 217, 167, 0.96), rgba(241, 217, 167, 0.82));
    clip-path: polygon(0 8%, 92% 0, 100% 88%, 78% 100%, 7% 94%);
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.4), inset 0 0 0 1px rgba(122, 74, 42, 0.24);
    filter: url(#paper-warp);
}

.chapter-index {
    display: block;
    margin-bottom: 0.75rem;
    color: var(--burnt-umber);
    font: 700 0.72rem "DM Mono", monospace;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.chapter-slip h2 {
    margin: 0 0 0.8rem;
    font-family: Besley, serif;
    font-size: clamp(2.25rem, 4.8vw, 5.8rem);
    line-height: 0.9;
    letter-spacing: -0.055em;
    color: var(--plum-shadow);
}

.chapter-slip p {
    margin: 0;
    font-family: Alegreya, serif;
    font-size: clamp(1.13rem, 1.55vw, 1.55rem);
    line-height: 1.36;
}

.origin-slip { left: 8vw; top: 12vh; transform: rotate(-4deg); }
.fork-slip { right: 8vw; top: 14vh; transform: rotate(5deg); }
.echo-slip { left: 10vw; bottom: 9vh; transform: rotate(3deg); }
.reversal-slip { right: 9vw; bottom: 10vh; transform: rotate(-6deg); }
.return-slip { left: 50%; top: 50%; transform: translate(-50%, -50%) rotate(2deg); }

.spool-index {
    position: fixed;
    right: 2.1rem;
    top: 50%;
    transform: translateY(-50%);
    z-index: 30;
    display: grid;
    gap: 0.8rem;
}

.spool-loop {
    position: relative;
    width: 38px;
    height: 38px;
    border: 2px solid rgba(241, 217, 167, 0.5);
    border-radius: 50%;
    background: radial-gradient(circle, rgba(58, 32, 63, 0.95), rgba(25, 17, 31, 0.86));
    box-shadow: inset 0 0 0 7px rgba(122, 74, 42, 0.2);
    transition: transform 300ms ease, border-color 300ms ease;
}

.spool-loop::after {
    content: "";
    position: absolute;
    inset: 8px;
    border-radius: 50%;
    border: 2px solid var(--blood-thread);
    transform: scale(0.4);
    opacity: 0.25;
    transition: transform 400ms ease, opacity 400ms ease;
}

.spool-loop span {
    position: absolute;
    right: 48px;
    top: 50%;
    transform: translateY(-50%) rotate(-2deg);
    white-space: nowrap;
    color: rgba(241, 217, 167, 0.76);
    font: 700 0.72rem "Bricolage Grotesque", sans-serif;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0;
    transition: opacity 250ms ease;
}

.spool-loop.active,
.spool-loop:hover {
    transform: rotate(38deg) scale(1.12);
    border-color: var(--candle-parchment);
}

.spool-loop.active::after,
.spool-loop:hover::after {
    transform: scale(1);
    opacity: 1;
}

.spool-loop.active span,
.spool-loop:hover span {
    opacity: 1;
}

.annotation-rail {
    position: fixed;
    left: 2rem;
    bottom: 1.6rem;
    z-index: 30;
    width: min(22rem, calc(100vw - 4rem));
    padding: 1rem 1.15rem;
    border-left: 2px solid var(--lichen-green);
    color: rgba(241, 217, 167, 0.72);
    background: rgba(25, 17, 31, 0.56);
    backdrop-filter: blur(10px);
}

.rail-title {
    margin: 0 0 0.3rem;
    color: var(--lichen-green);
    font: 700 0.7rem "DM Mono", monospace;
    letter-spacing: 0.13em;
    text-transform: uppercase;
}

.annotation-rail p:last-child {
    margin: 0;
    font: 300 0.82rem "DM Mono", monospace;
}

body[data-room="0"] .loom-graph {
    opacity: 0;
}

body[data-room="2"] .draw-thread { stroke-width: 6; }
body[data-room="3"] .draw-thread { stroke: #6DAEDB; }
body[data-room="4"] .loom-graph { transform: translate(-4vw, -3vh) rotate(4deg) scale(1.05); }
body[data-room="5"] .loom-graph { transform: translate(-8vw, -6vh) rotate(8deg) scale(0.92); }

@keyframes fragmentDrift {
    from { transform: translate(-6vw, -4vh) rotate(-2deg); }
    to { transform: translate(-4vw, -2vh) rotate(1deg); }
}

@keyframes mothFloat {
    from { transform: translate3d(0, 0, 0) rotate(-15deg); }
    to { transform: translate3d(2.2rem, -1.5rem, 0) rotate(12deg); }
}

@media (max-width: 760px) {
    html,
    body {
        overflow: hidden;
    }

    .loose-fragment {
        width: 82vw;
        transform: translate(0, -8vh) rotate(-2deg);
    }

    h1 {
        left: 6vw;
        bottom: 18vh;
        font-size: clamp(3.6rem, 18vw, 7rem);
    }

    .loom-graph {
        inset: 4vh -72vw -6vh -38vw;
        transform: rotate(74deg) scale(0.78) translateY(4vh);
    }

    .chapter-slip {
        width: 76vw;
        left: 8vw !important;
        right: auto !important;
        top: auto !important;
        bottom: 14vh !important;
        transform: rotate(-2deg) !important;
    }

    .spool-index {
        right: 1rem;
        gap: 0.45rem;
    }

    .spool-loop {
        width: 30px;
        height: 30px;
    }

    .spool-loop span {
        display: none;
    }

    .annotation-rail {
        display: none;
    }
}
