/* Herd's cards: the end of a field, "start over?", the diary, and the badge pop-up.
   They sit in a layer over the field, in the site's style: an ink panel over a striped copy
   of itself. The layer is a size container, so cards adapt to the toy, not the window. */

/* ---- The layer ---- */

.herd-layer {
    --ink: #030712;
    --line: #71717a;
    --paper: #f4efe6;
    --muted: #9ca3af;
    --soft: #d1d5db;
    --gold: #f2c14e;
    --cork: #c69c6d;
    --cork-ink: #3b2a1a;
    --mono: ui-monospace, Menlo, monospace;
    --spring: cubic-bezier(0.2, 0.9, 0.3, 1.25);

    position: absolute;
    inset: 0;
    z-index: 5;
    display: grid;
    grid-template: minmax(0, 1fr) / minmax(0, 1fr);
    place-items: center;
    padding: 60px 12px 76px; /* clear of the corner buttons at the top, the toolbar at the bottom */
    container: herd / size;
    pointer-events: none; /* the field underneath still gets taps; cards switch this back on */
    font-family: Sora, system-ui, sans-serif;
}

/* Set from script on short screens, where every pixel of height counts. */
.herd-layer.short {
    padding: 56px 12px 72px;
}

.herd-layer *,
.herd-layer *::before,
.herd-layer *::after {
    box-sizing: border-box;
}

.herd-defs {
    position: absolute;
}

/* ---- Cards ---- */

.herd-card {
    position: relative;
    isolation: isolate;
    display: flex;
    flex-direction: column;
    width: min(360px, 100%);
    max-height: 100cqh;
    padding: 20px 22px 18px;
    border-radius: 24px;
    color: #fff;
    text-align: center;
    pointer-events: auto;
    opacity: 0;
    transform: scale(0.92);
    transition:
        opacity 0.3s,
        transform 0.4s var(--spring);
    scrollbar-width: thin;
    scrollbar-color: #4b5563 transparent;
}

.herd-card.shown {
    opacity: 1;
    transform: none;
}

.herd-card.wide {
    width: min(560px, 100%);
}

/* The ink surface, and the striped copy behind it, shifted left and down. */
.herd-card::after,
.herd-card::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
}

.herd-card::after {
    z-index: -1;
    background: var(--ink);
}

.herd-card::before {
    z-index: -2;
    border: 1px solid var(--line);
    background: repeating-linear-gradient(-45deg, var(--line) 0 1px, var(--paper) 1px 5px);
    transform: translate(-8px, 8px);
}

/* A card's parts stack in one column, with the buttons always last. The wrappers only
   matter on short screens, where the parts sit side by side. */
.herd-diary-side,
.herd-end-main {
    display: contents;
}

.herd-board,
.herd-end-won {
    order: 1;
}

.herd-card .herd-actions {
    order: 2;
}

/* ---- Words ---- */

.herd-eyebrow,
.herd-facts {
    margin: 0;
    font: 500 11px/1.5 var(--mono);
    color: var(--muted);
}

.herd-facts {
    color: var(--soft);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.herd-title {
    margin: 6px 0 10px;
    font-size: 22px;
    font-weight: 300;
}

.herd-title.small {
    font-size: 18px;
}

.herd-title b {
    font-weight: 700;
}

.herd-detail {
    margin: 0 0 4px;
    font-size: 14px;
    color: var(--soft);
}

/* ---- Buttons ---- */

.herd-actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 16px;
}

.herd-button {
    padding: 9px 16px;
    border: 1px solid #4b5563;
    border-radius: 999px;
    background: transparent;
    color: #fff;
    font:
        500 14px Sora,
        system-ui,
        sans-serif;
    white-space: nowrap;
    cursor: pointer;
}

.herd-button.solid {
    border-color: #fff;
    background: #fff;
    color: var(--ink);
    font-weight: 600;
}

.herd-button:focus-visible {
    outline: 2px solid var(--gold);
    outline-offset: 2px;
}

/* ---- End of a field ---- */

.herd-stars {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 4px 0 12px;
}

.herd-stars span {
    width: 28px;
    height: 28px;
    fill: rgb(255 255 255 / 0.18);
}

.herd-stars span.lit {
    fill: var(--gold);
    animation: herd-star 0.45s backwards;
}

.herd-won-label {
    margin: 14px 0 0;
    font: 500 11px var(--mono);
    color: var(--gold);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.herd-won {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 2px;
    margin: 6px 0 0;
    padding: 0;
    list-style: none;
}

.herd-won .herd-badge {
    width: 68px;
    animation: herd-reveal 1s var(--spring) backwards;
}

.herd-won .herd-rosette {
    width: 52px;
    height: 60px;
}

.herd-won .herd-badge b {
    font-size: 11px;
    font-weight: 600;
    line-height: 1.25;
    color: #fff;
    overflow-wrap: anywhere;
}

.herd-won .herd-badge span {
    display: none;
}

/* ---- The diary ---- */

.herd-times {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin: 6px 0 12px;
}

.herd-times div {
    padding: 6px 4px;
    border: 1px solid #374151;
    border-radius: 14px;
}

.herd-times dt {
    font-size: 11px;
    color: var(--muted);
}

.herd-times dd {
    margin: 2px 0 0;
    font-size: 17px;
    font-weight: 600;
}

/* The cork board with every badge pinned on as a rosette. Only the badges scroll, so the
   buttons stay in view. */
.herd-board {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    margin-top: 14px;
    padding: 16px 12px 12px;
    border-radius: 18px;
    background: var(--cork) radial-gradient(rgb(90 60 30 / 0.25) 1px, transparent 1.5px) 0 0 / 9px 9px;
}

.herd-progress {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 4px 12px;
    font: 500 11px var(--mono);
    color: var(--cork-ink);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.herd-progress-bar {
    flex: 1;
    height: 7px;
    border-radius: 99px;
    background: rgb(59 42 26 / 0.2);
    overflow: hidden;
}

.herd-progress-bar i {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--cork-ink);
}

.herd-badges {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    gap: 10px 6px;
    margin: 0;
    padding: 0;
    list-style: none;
    overflow: auto;
    scrollbar-width: thin;
    scrollbar-color: rgb(59 42 26 / 0.5) transparent;
}

/* ---- Badges ---- */

.herd-badge {
    text-align: center;
}

.herd-badge b {
    display: block;
    font-size: 12.5px;
    color: #2a1d10;
}

.herd-badge span {
    display: block;
    margin-top: 2px;
    font-size: 11px;
    line-height: 1.35;
    color: #4a3624;
}

.herd-badge.locked b,
.herd-badge.locked span {
    opacity: 0.55;
}

.herd-rosette {
    display: block;
    width: 84px;
    height: 97px;
    margin: 0 auto 4px;
    overflow: visible;
}

/* A streak of light that sweeps across a new rosette once. */
.herd-shine {
    transform: translateX(110px);
    animation: herd-shine 1.4s 0.5s ease-out backwards;
}

/* ---- A badge won mid-field: a small ink card at the top ---- */

.herd-pops {
    position: absolute;
    top: 60px;
    left: 50%;
    display: grid;
    gap: 8px;
    width: max-content;
    max-width: calc(100% - 24px);
    transform: translateX(-50%);
}

.herd-pop {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 18px 8px 8px;
    border-radius: 22px;
    background: var(--ink);
    color: #fff;
    opacity: 0;
    transform: translateY(-16px);
    transition:
        opacity 0.3s,
        transform 0.4s var(--spring);
}

.herd-pop.shown {
    opacity: 1;
    transform: none;
}

.herd-pop.shown .herd-pop-art {
    animation: herd-reveal 0.9s var(--spring) backwards;
}

.herd-pop .herd-rosette {
    width: 54px;
    height: 62px;
    margin: 0;
}

.herd-pop-words {
    text-align: left;
}

.herd-pop-words p {
    margin: 0;
}

.herd-pop-words b {
    display: block;
    font-size: 15px;
}

.herd-pop-words span {
    font-size: 12px;
    color: var(--muted);
}

/* Confetti: bits that fly out to --x, --y (set from script) and fade. */
.herd-burst {
    position: absolute;
    pointer-events: none;
}

.herd-burst i {
    position: absolute;
    width: 7px;
    height: 7px;
    border-radius: 2px;
    animation: herd-fly 0.9s ease-out forwards;
}

/* ---- Animations ---- */

@keyframes herd-star {
    0% {
        transform: scale(0.3);
        fill: rgb(255 255 255 / 0.18);
    }
    60% {
        transform: scale(1.25);
    }
}

@keyframes herd-reveal {
    0% {
        transform: scale(0) rotate(-25deg);
        opacity: 0;
    }
    55% {
        transform: scale(1.15) rotate(6deg);
        opacity: 1;
    }
    100% {
        transform: none;
    }
}

@keyframes herd-shine {
    0% {
        transform: translateX(-90px) skewX(-20deg);
        opacity: 0;
    }
    30% {
        opacity: 0.9;
    }
    100% {
        transform: translateX(110px) skewX(-20deg);
        opacity: 0;
    }
}

@keyframes herd-fly {
    to {
        transform: translate(var(--x), var(--y)) rotate(320deg);
        opacity: 0;
    }
}

/* ---- Small and short screens ---- */

/* Narrow (a phone held upright): tighter cards and smaller type. */
@container herd (max-width: 420px) {
    .herd-card {
        padding: 18px 16px 16px;
    }

    .herd-title {
        font-size: 20px;
    }

    .herd-times {
        gap: 6px;
    }

    .herd-times dd {
        font-size: 15px;
    }

    .herd-badges {
        grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
    }

    .herd-button {
        padding: 9px 14px;
    }
}

/* Any short screen: a card may have to scroll, so its ink goes on the card itself; a
   pseudo-element would scroll away with the content. */
@container herd (max-height: 460px) {
    .herd-card {
        overflow: auto;
        background: var(--ink);
    }

    .herd-card::before,
    .herd-card::after {
        display: none;
    }
}

/* Short and wide (a phone on its side): the parts of a card sit side by side. */
@container herd (max-height: 460px) and (min-width: 400px) {
    .herd-card.wide {
        display: grid;
        grid-template: minmax(0, 1fr) / minmax(205px, 0.9fr) 1.5fr;
        gap: 16px;
        width: min(900px, 100%);
        height: 100cqh;
        text-align: left;
    }

    .herd-card.has-won {
        display: grid;
        grid-template-columns: 1fr 1fr;
        align-items: center;
        gap: 12px;
        width: min(660px, 100%);
    }

    .herd-diary-side,
    .herd-card.has-won .herd-end-main {
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .herd-diary-side {
        overflow: auto;
    }

    .herd-diary-side .herd-eyebrow {
        display: none;
    }

    .herd-diary-side .herd-title,
    .herd-card.has-won .herd-title {
        margin: 0 0 6px;
        font-size: 19px;
    }

    /* Best times as a short list instead of boxes. */
    .herd-diary-side .herd-times {
        grid-template-columns: 1fr;
        gap: 2px;
        margin: 2px 0 8px;
    }

    .herd-diary-side .herd-times div {
        display: flex;
        justify-content: space-between;
        padding: 0;
        border: 0;
    }

    .herd-diary-side .herd-times dd {
        margin: 0;
        font-size: 13px;
    }

    .herd-diary-side .herd-actions {
        justify-content: flex-start;
        gap: 6px;
        margin-top: auto;
        padding-top: 10px;
    }

    .herd-diary-side .herd-button,
    .herd-card.has-won .herd-button {
        padding: 6px 11px;
        font-size: 12.5px;
    }

    .herd-card.wide .herd-board {
        margin-top: 0;
    }

    .herd-card.has-won .herd-won .herd-badge {
        width: 60px;
    }

    .herd-card.has-won .herd-won .herd-rosette {
        width: 44px;
        height: 51px;
    }
}

/* Very short: best times in two columns, no running total, and new badges as a row of
   rosettes without names. */
@container herd (max-height: 230px) and (min-width: 400px) {
    .herd-diary-side .herd-times {
        grid-template-columns: 1fr 1fr;
        column-gap: 14px;
    }

    .herd-diary-side .herd-facts {
        display: none;
    }

    .herd-card.has-won .herd-stars {
        margin: 0 0 6px;
    }

    .herd-card.has-won .herd-stars span {
        width: 22px;
        height: 22px;
    }

    .herd-card.has-won .herd-won .herd-badge {
        width: 46px;
    }

    .herd-card.has-won .herd-won .herd-badge b {
        display: none;
    }
}

/* Small both ways: the whole card scrolls, with its buttons held at the bottom. */
@container herd (max-height: 460px) and (max-width: 399.9px) {
    .herd-board,
    .herd-end-won {
        flex: none;
    }

    .herd-card.wide .herd-badges {
        overflow: visible;
    }

    .herd-card .herd-actions {
        position: sticky;
        bottom: -16px; /* the card's own bottom padding */
        margin: 12px -16px -16px;
        padding: 10px 0 12px;
        background: var(--ink);
    }
}

@media (prefers-reduced-motion: reduce) {
    .herd-card,
    .herd-pop,
    .herd-pop-art,
    .herd-stars span.lit,
    .herd-won .herd-badge,
    .herd-shine {
        transition: none;
        animation: none;
    }

    .herd-burst {
        display: none;
    }
}
