/* ==========================================================================
   The fifteen-second story loop.
   One 15s cycle, four scenes of 3.75s each. Every animation shares the same
   duration and differs only by delay, which is what keeps them in step without
   a line of JavaScript.
   ========================================================================== */

.film {
    --film-cycle: 15s;
    --film-scene: 3.75s;
    position: relative;
    border-radius: var(--radius-lg);
    background: linear-gradient(160deg, var(--brand-50), var(--bg-surface) 65%);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-md);
    overflow: hidden;
}

.film__stage {
    position: relative;
    aspect-ratio: 16 / 9;
    inline-size: 100%;
}

.film__stage svg {
    position: absolute;
    inset: 0;
    inline-size: 100%;
    block-size: 100%;
}

/* ------------------------------------------------------------------ scenes */
.film__scene {
    opacity: 0;
    transform-origin: 50% 50%;
    animation: film-scene var(--film-cycle) infinite;
}

.film__scene--1 { animation-delay: 0s; }
.film__scene--2 { animation-delay: calc(var(--film-scene) * 1); }
.film__scene--3 { animation-delay: calc(var(--film-scene) * 2); }
.film__scene--4 { animation-delay: calc(var(--film-scene) * 3); }

@keyframes film-scene {
    0%        { opacity: 0; transform: translateY(14px) scale(.985); }
    3%, 21%   { opacity: 1; transform: none; }
    24%, 100% { opacity: 0; transform: translateY(-10px) scale(1.012); }
}

/* --------------------------------------------------------------- materials */
.film__card {
    fill: url(#filmSurface);
    stroke: var(--border-color);
    stroke-width: 2;
}

.film__cell { fill: var(--ink-100); }

.film__slot {
    fill: var(--brand-400);
    animation: film-slot var(--film-cycle) infinite;
}

@keyframes film-slot {
    0%, 4%   { fill: var(--ink-100); transform: scale(1); }
    7%, 24%  { fill: var(--brand-500); }
    100%     { fill: var(--brand-500); }
}

.film__chip {
    fill: var(--bg-surface);
    stroke: var(--brand-300);
    stroke-width: 2;
}

.film__chip--solid { fill: var(--brand-600); stroke: var(--brand-600); }

.film__token {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 22px;
    font-weight: 700;
    fill: var(--brand-700);
    text-anchor: middle;
    direction: ltr;
}

.film__token--light { fill: #ffffff; }
.film__token--danger { fill: var(--danger-700); font-size: 18px; }

.film__rx {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 54px;
    font-weight: 800;
    fill: var(--brand-600);
    direction: ltr;
}

/* ------------------------------------------------------------ scene detail */
.film__pop {
    opacity: 0;
    transform-origin: 50% 50%;
    animation: film-pop var(--film-cycle) infinite;
}

@keyframes film-pop {
    0%, 6%    { opacity: 0; transform: scale(.82); }
    11%, 23%  { opacity: 1; transform: scale(1); }
    25%, 100% { opacity: 0; transform: scale(1); }
}

/* Delays are scene start plus a beat, so detail lands after its scene appears. */
.film__scene--1 .film__pop--a { animation-delay: .7s; }
.film__scene--1 .film__pop--b { animation-delay: 1.3s; }
.film__scene--1 .film__pop--c { animation-delay: 1.8s; }
.film__scene--3 .film__pop--a { animation-delay: calc(var(--film-scene) * 2 + 1.1s); }
.film__scene--3 .film__pop--b { animation-delay: calc(var(--film-scene) * 2 + 1.7s); }
.film__scene--4 .film__pop--c { animation-delay: calc(var(--film-scene) * 3 + 1.9s); }

/* record lines typing themselves in */
.film__line {
    fill: var(--ink-200);
    transform-origin: left center;
    transform-box: fill-box;
    animation: film-line var(--film-cycle) infinite;
}

@keyframes film-line {
    0%, 5%    { transform: scaleX(0); opacity: 0; }
    10%, 100% { transform: scaleX(1); opacity: 1; }
}

.film__line--1 { animation-delay: calc(var(--film-scene) * 1 + .5s); }
.film__line--2 { animation-delay: calc(var(--film-scene) * 1 + .8s); }
.film__line--3 { animation-delay: calc(var(--film-scene) * 1 + 1.1s); }
.film__line--4 { animation-delay: calc(var(--film-scene) * 1 + 1.4s); }
.film__line--5 { animation-delay: calc(var(--film-scene) * 1 + 1.7s); }

.film__scope {
    stroke-dasharray: 220;
    stroke-dashoffset: 220;
    animation: film-draw var(--film-cycle) infinite;
    animation-delay: calc(var(--film-scene) * 1 + .4s);
}

.film__pulse-dot {
    animation: film-beat var(--film-cycle) infinite;
    animation-delay: calc(var(--film-scene) * 1 + 1.2s);
    transform-origin: center;
    transform-box: fill-box;
}

@keyframes film-beat {
    0%, 8%          { transform: scale(1); }
    10%             { transform: scale(1.45); }
    12%, 14%        { transform: scale(1); }
    16%             { transform: scale(1.45); }
    18%, 100%       { transform: scale(1); }
}

/* the trace draws itself across the card */
.film__ekg {
    stroke-dasharray: 620;
    stroke-dashoffset: 620;
    animation: film-draw var(--film-cycle) infinite;
    animation-delay: calc(var(--film-scene) * 2 + .3s);
}

@keyframes film-draw {
    0%        { stroke-dashoffset: var(--film-dash, 620); }
    14%, 100% { stroke-dashoffset: 0; }
}

.film__nodes line {
    stroke: var(--brand-300);
    stroke-width: 2;
    opacity: 0;
    animation: film-fade var(--film-cycle) infinite;
    animation-delay: calc(var(--film-scene) * 2 + .9s);
}

.film__nodes circle {
    fill: var(--brand-500);
    opacity: 0;
    animation: film-fade var(--film-cycle) infinite, film-float 3s ease-in-out infinite;
    animation-delay: calc(var(--film-scene) * 2 + 1.1s), 0s;
}

.film__nodes .film__nodes-out { fill: var(--brand-700); }

@keyframes film-fade {
    0%, 6%    { opacity: 0; }
    12%, 23%  { opacity: 1; }
    25%, 100% { opacity: 0; }
}

@keyframes film-float {
    0%, 100% { transform: translateY(0); }
    50%      { transform: translateY(-4px); }
}

.film__slide {
    opacity: 0;
    animation: film-slide var(--film-cycle) infinite;
}

.film__slide--left { animation-delay: calc(var(--film-scene) * 3 + .4s); }
.film__slide--right { animation-delay: calc(var(--film-scene) * 3 + .9s); }

@keyframes film-slide {
    0%, 4%    { opacity: 0; transform: translateY(26px); }
    10%, 23%  { opacity: 1; transform: none; }
    25%, 100% { opacity: 0; transform: translateY(-12px); }
}

/* ---------------------------------------------------------------- captions */
.film__captions {
    position: relative;
    min-block-size: 4.5rem;
    padding: .25rem 1.25rem 1.4rem;
}

.film__caption {
    position: absolute;
    inset-inline: 1.25rem;
    margin: 0;
    text-align: center;
    font-size: clamp(.95rem, 2vw, 1.15rem);
    font-weight: 700;
    color: var(--text-strong);
    opacity: 0;
    animation: film-scene var(--film-cycle) infinite;
}

.film__caption--1 { animation-delay: 0s; }
.film__caption--2 { animation-delay: calc(var(--film-scene) * 1); }
.film__caption--3 { animation-delay: calc(var(--film-scene) * 2); }
.film__caption--4 { animation-delay: calc(var(--film-scene) * 3); }

.film__progress {
    position: absolute;
    inset-block-end: 0;
    inset-inline: 0;
    block-size: 4px;
    background: var(--ink-100);
}

.film__progress span {
    display: block;
    block-size: 100%;
    background: linear-gradient(90deg, var(--brand-400), var(--brand-700));
    transform-origin: left center;
    animation: film-progress var(--film-cycle) linear infinite;
}

@keyframes film-progress {
    from { transform: scaleX(0); }
    to   { transform: scaleX(1); }
}

/* --------------------------------------------------------- presenting mode */
.film--stage {
    max-inline-size: min(100%, 80rem);
    margin-inline: auto;
    box-shadow: var(--shadow-lg);
}

.film-screen {
    min-block-size: 100dvh;
    display: grid;
    place-items: center;
    padding: clamp(1rem, 4vw, 3rem);
    background: radial-gradient(60rem 40rem at 50% -10%, var(--brand-100), var(--bg-body));
}

.film-screen__inner { inline-size: min(100%, 76rem); }

.film-screen__brand {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: .6rem;
    margin-block-end: 1.2rem;
    font-size: 1.1rem;
    font-weight: 800;
    color: var(--text-strong);
}

/* Someone who asked for less motion gets the first scene, held still. */
@media (prefers-reduced-motion: reduce) {
    .film *,
    .film__progress span {
        animation: none !important;
    }

    .film__scene--1,
    .film__caption--1 { opacity: 1; }

    .film__scene--2,
    .film__scene--3,
    .film__scene--4,
    .film__caption--2,
    .film__caption--3,
    .film__caption--4 { opacity: 0; }

    .film__line, .film__pop, .film__slide, .film__nodes circle, .film__nodes line { opacity: 1; transform: none; }
    .film__ekg, .film__scope { stroke-dashoffset: 0; }
}
