/*
 * Cavern Run on rodesia.org (deploy/theme/page-cavernrun.html). Uses the Card
 * theme's tokens (--card-border, --shadow, --accent...) with fallbacks.
 */

.cavernrun-game {
    margin: 0 0 3.5rem;
}

/* With room to spare, the arena widens past the text column. */
@media (min-width: 64rem) {
    .cavernrun-game {
        margin-inline: calc((100% - 60rem) / 2);
    }
}

.cavernrun-stage {
    position: relative;
    /* The view is 256 x 158 world units (cavern + HUD); keep it whole. */
    width: min(100%, calc(80vh * 256 / 158));
    aspect-ratio: 256 / 158;
    margin-inline: auto;
    overflow: hidden;
    border: 1px solid var(--card-border, rgb(22 22 29 / 0.08));
    border-radius: 14px;
    background: #08060c;
    box-shadow: var(--shadow, 0 8px 24px -12px rgb(22 22 29 / 0.3));
}

.cavernrun-stage:has(canvas:focus-visible) {
    outline: 2px solid var(--accent, #1a7f4b);
    outline-offset: 3px;
}

.cavernrun-stage canvas {
    display: block;
    width: 100%;
    height: 100%;
    outline: none;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Phones held upright: a taller stage, the arena at the top and the
   touch buttons below it (the game pins the arena when it sees touch). */
@media (pointer: coarse) and (orientation: portrait) {
    .cavernrun-stage {
        width: 100%;
        aspect-ratio: auto;
        /* Nearly a screen tall: the cavern fills it down to the controls. */
        height: calc(100svh - 1.5rem);
        max-height: 210vw;
    }

    .cavernrun-fullscreen {
        right: auto;
        left: calc(50% - 1.125rem);
    }
}

/* Full screen, and the same look where the browser has no full-screen API
   (iPhone): the game fills the screen; held upright, the arena sits at the
   top with the controls below it. */
/* Phones on their side: the controls take the bottom corners, so the
   full-screen button moves up beside the pause button. */
@media (pointer: coarse) and (orientation: landscape) {
    .cavernrun-fullscreen {
        top: 0.6rem;
        bottom: auto;
        right: 4.75rem;
    }
}

.cavernrun-stage:fullscreen,
.cavernrun-stage.is-max {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    aspect-ratio: auto;
    border: 0;
    border-radius: 0;
    outline: none;
}

.cavernrun-stage.is-max {
    position: fixed;
    inset: 0;
    z-index: 1000;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    box-sizing: border-box;
}

html.cavernrun-locked,
html.cavernrun-locked body {
    overflow: hidden;
    overscroll-behavior: none;
}

.cavernrun-stage:fullscreen .cavernrun-fullscreen,
.cavernrun-stage.is-max .cavernrun-fullscreen {
    bottom: max(0.6rem, env(safe-area-inset-bottom));
}

.cavernrun-offer:focus {
    outline: none;
}

/* "New best!" for signed-in players, over the top of the arena. */
/* A new record: a crest, the rank and the score over the arena, with sparks. */
.cavernrun-cheer {
    position: absolute;
    inset: 0;
    z-index: 3;
    display: grid;
    place-items: center;
    padding: 1rem;
    overflow: hidden;
    background: radial-gradient(circle at 50% 45%, rgb(50 26 10 / 0.72), rgb(8 4 14 / 0.88) 70%);
    cursor: pointer;
    animation: cavernrun-fade-in 0.35s ease-out;
    transition: opacity 0.45s;
}

.cavernrun-cheer[hidden] {
    display: none;
}

.cavernrun-cheer.out {
    opacity: 0;
}

.cavernrun-cheer-rays {
    position: absolute;
    inset: -50%;
    background: repeating-conic-gradient(from 0deg, rgb(242 201 76 / 0.13) 0deg 8deg, transparent 8deg 22deg);
    mask-image: radial-gradient(circle, #000 0, transparent 55%);
    -webkit-mask-image: radial-gradient(circle, #000 0, transparent 55%);
    animation: cavernrun-spin 24s linear infinite;
}

.cavernrun-cheer-card {
    position: relative;
    display: grid;
    justify-items: center;
    gap: 0.2rem;
    max-width: min(26rem, 100%);
    padding: 1.4rem 1.6rem 1.3rem;
    border: 1px solid rgb(242 201 76 / 0.6);
    border-radius: 18px;
    background: linear-gradient(rgb(36 22 40 / 0.96), rgb(18 10 24 / 0.96));
    color: #f6e7c1;
    text-align: center;
    box-shadow: 0 0 0 4px rgb(242 201 76 / 0.12), 0 24px 60px -18px rgb(0 0 0 / 0.8);
    animation: cavernrun-cheer-in 0.7s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.cavernrun-cheer-crest {
    width: 4.2rem;
    margin-top: -3.4rem;
    filter: drop-shadow(0 6px 10px rgb(0 0 0 / 0.5));
    animation: cavernrun-crest 1.6s ease-in-out infinite alternate;
}

.cavernrun-cheer p {
    margin: 0;
}

.cavernrun-cheer-kicker {
    color: rgb(242 201 76 / 0.85);
    font: 700 0.75rem/1.4 var(--sans, system-ui, sans-serif);
    letter-spacing: 0.22em;
    text-transform: uppercase;
}

.cavernrun-cheer-title {
    background: linear-gradient(#fff6c8, #f2c94c 50%, #c8901f);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    font: 800 clamp(1.7rem, 7vmin, 2.6rem)/1.1 var(--serif, Georgia, serif);
    text-wrap: balance;
}

.cavernrun-cheer-score {
    color: #fff;
    font: 700 clamp(2rem, 9vmin, 3.2rem)/1.05 var(--serif, Georgia, serif);
    font-variant-numeric: tabular-nums;
    text-shadow: 0 0 24px rgb(255 179 71 / 0.55);
}

.cavernrun-cheer-score:empty {
    display: none;
}

.cavernrun-cheer-score::after {
    content: attr(data-wave);
    display: block;
    color: rgb(246 231 193 / 0.7);
    font: 600 0.8rem/1.6 var(--sans, system-ui, sans-serif);
    text-shadow: none;
}

.cavernrun-cheer-text {
    max-width: 22rem;
    margin: 0.3rem 0 0.9rem !important;
    font: 500 1rem/1.45 var(--sans, system-ui, sans-serif);
    text-wrap: pretty;
}

.cavernrun-cheer-sparks i {
    position: absolute;
    top: 55%;
    left: 50%;
    width: 7px;
    height: 7px;
    border-radius: 2px;
    background: hsl(var(--h) 90% 62%);
    box-shadow: 0 0 8px hsl(var(--h) 95% 60% / 0.8);
    opacity: 0;
    animation: cavernrun-spark 1.9s cubic-bezier(0.15, 0.7, 0.4, 1) var(--d) 2 both;
}

/* Held upright the stage includes the control deck: lift the card into the arena. */
@media (pointer: coarse) and (orientation: portrait) {
    .cavernrun-cheer {
        grid-template-rows: 1fr auto 1.8fr;
        place-items: center;
    }

    .cavernrun-cheer-card {
        grid-row: 2;
    }
}

.cavernrun-cheer.is-champion .cavernrun-cheer-card {
    border-color: #f2c94c;
}

@keyframes cavernrun-fade-in {
    from { opacity: 0; }
}

@keyframes cavernrun-cheer-in {
    from { opacity: 0; transform: scale(0.6) translateY(20px); }
}

@keyframes cavernrun-crest {
    to { transform: translateY(-4px) rotate(-3deg); }
}

@keyframes cavernrun-spin {
    to { transform: rotate(360deg); }
}

@keyframes cavernrun-spark {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(0.4); }
    70% { opacity: 1; }
    100% { opacity: 0; transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y) + 30vmin)) rotate(320deg) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
    .cavernrun-cheer, .cavernrun-cheer-card, .cavernrun-cheer-crest, .cavernrun-cheer-rays { animation: none; }
    .cavernrun-cheer-sparks { display: none; }
}

/* "A place on the high-score table": over the arena, so it shows in full
   screen too. */
.cavernrun-offer {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: rgb(8 4 14 / 0.55);
}

.cavernrun-offer[hidden] {
    display: none;
}

.cavernrun-offer-card {
    max-width: 22rem;
    padding: 1.25rem 1.5rem;
    border: 1px solid rgb(242 201 76 / 0.45);
    border-radius: 14px;
    background: rgb(20 12 28 / 0.92);
    color: #f6e7c1;
    text-align: center;
    box-shadow: 0 18px 40px -16px rgb(0 0 0 / 0.7);
}

.cavernrun-offer-score {
    margin: 0;
    color: #f2c94c;
    font: 700 2.2rem/1.1 var(--serif, Georgia, serif);
}

.cavernrun-offer-text {
    margin: 0.4rem 0 1rem;
    font: 500 0.95rem/1.4 var(--sans, system-ui, sans-serif);
}

.cavernrun-offer-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
}

.cavernrun-btn {
    padding: 0.55rem 1rem;
    border: 1px solid rgb(246 231 193 / 0.35);
    border-radius: 999px;
    background: transparent;
    color: #f6e7c1;
    font: 600 0.9rem/1 var(--sans, system-ui, sans-serif);
    cursor: pointer;
}

.cavernrun-btn-primary {
    border-color: transparent;
    background: linear-gradient(#f2c94c, #c8901f);
    color: #1a0f14;
}

/* Thumb-sized buttons on touch screens. */
@media (pointer: coarse) {
    .cavernrun-btn {
        min-height: 48px;
        padding: 0.7rem 1.4rem;
        font-size: 1rem;
    }

    .cavernrun-offer-text {
        font-size: 1.05rem;
    }
}

.cavernrun-btn:disabled {
    opacity: 0.7;
    cursor: progress;
}

/* Under the game: who is signed in, and news from signing in. */
.cavernrun-status {
    max-width: 36rem;
    margin: -2.25rem auto 3rem;
    text-align: center;
}

.cavernrun-scores-me {
    margin: 0;
    color: var(--muted, #5f6170);
    font-size: 0.9rem;
}

.cavernrun-scores-me:empty {
    display: none;
}

.cavernrun-link {
    padding: 0;
    border: 0;
    background: none;
    color: var(--accent, #1a7f4b);
    font: inherit;
    text-decoration: underline;
    cursor: pointer;
}

.cavernrun-scores-note {
    margin: 0 0 0.5rem;
    padding: 0.6rem 0.9rem;
    border: 1px solid var(--card-border, rgb(22 22 29 / 0.08));
    border-radius: 10px;
    background: var(--card, rgb(255 255 255 / 0.78));
    color: var(--text, #16161d);
}

.cavernrun-loading {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    margin: 0;
    color: #ffc94a;
    font: 600 1.1rem/1.2 var(--serif, Georgia, serif);
    letter-spacing: 0.06em;
}

.cavernrun-fullscreen {
    position: absolute;
    right: 0.6rem;
    bottom: 0.6rem;
    display: grid;
    place-items: center;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    border: 1px solid rgb(246 231 193 / 0.25);
    border-radius: 999px;
    background: rgb(11 10 20 / 0.55);
    color: rgb(246 231 193 / 0.8);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.cavernrun-fullscreen:hover,
.cavernrun-fullscreen:focus-visible {
    background: rgb(11 10 20 / 0.8);
    color: #fff;
}

.cavernrun-noscript {
    color: var(--muted, #5f6170);
    text-align: center;
}

/* The game page opens with the arena, not a big headline. */
.card-page:has(.cavernrun-game) {
    padding-top: clamp(1.25rem, 4vh, 2.25rem);
}

.card-page:has(.cavernrun-game) .hero {
    margin-bottom: 2rem;
}
