/* Audio synchronization add-on; load after the original CSS. */
.glitch-wrapper.audio-sync-active .glitch,
.glitch-wrapper.audio-sync-active .glitch.small-text {
    animation: shine 5.3s linear infinite;
    transform: translate(var(--as-x, 0px), var(--as-y, 0px)) skewX(var(--as-sk, 0deg));
}
.glitch-wrapper.audio-sync-active .glitch.small-text {
    transform: translate(calc(var(--as-x, 0px) * -.45), calc(var(--as-y, 0px) * .5)) skewX(calc(var(--as-sk, 0deg) * -.5));
}
.glitch-wrapper.audio-sync-active .glitch::before,
.glitch-wrapper.audio-sync-active .glitch::after {
    animation: none;
    background: none;
    -webkit-text-fill-color: currentColor;
    text-shadow: none;
}
.glitch-wrapper.audio-sync-active .glitch::before {
    color: #00d9ff;
    opacity: var(--as-c, 0);
    transform: translate(calc(var(--as-x, 0px) * -1.4), var(--as-y, 0px));
    clip-path: polygon(0 var(--as-a, 0%), 42% var(--as-a, 0%), 47% calc(var(--as-a, 0%) + 5%), 100% var(--as-a, 0%), 100% var(--as-b, 100%), 56% var(--as-b, 100%), 51% calc(var(--as-b, 100%) - 4%), 0 var(--as-b, 100%));
}
.glitch-wrapper.audio-sync-active .glitch::after {
    color: #ff00ff;
    opacity: var(--as-m, 0);
    transform: translate(calc(var(--as-x, 0px) * 1.2), calc(var(--as-y, 0px) * -1));
    clip-path: inset(var(--as-d, 0%) 0 var(--as-e, 100%) 0);
}
.glitch-wrapper.audio-sync-active::after { animation: none; }
.glitch-wrapper.audio-sync-active .glitch-pixels,
.glitch-wrapper.audio-sync-active .glitch-pixels::before,
.glitch-wrapper.audio-sync-active .glitch-pixels::after { animation: none; opacity: 0; }
.audio-sync-controls {
    position: fixed; bottom: max(12px, env(safe-area-inset-bottom)); right: max(12px, env(safe-area-inset-right));
    z-index: 20; display: flex; flex-direction: column; align-items: flex-end; gap: 5px;
    font: 11px/1.4 system-ui, sans-serif; max-width: calc(100vw - 24px); color: #aaa;
}
.audio-sync-controls button { font: inherit; letter-spacing: .1em; color: #ddd; border: 1px solid #555; background: #111; padding: 8px 12px; cursor: pointer; }
.audio-sync-controls button:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
.audio-sync-controls button:disabled { opacity: .55; cursor: default; }
.audio-sync-controls a { color: #aaa; }
@media(prefers-reduced-motion: reduce) {
    .glitch-wrapper.audio-sync-active .glitch,
    .glitch-wrapper.audio-sync-active .glitch.small-text,
    .glitch-wrapper.audio-sync-active .glitch::before,
    .glitch-wrapper.audio-sync-active .glitch::after { animation: none !important; transform: none !important; }
    .glitch-wrapper.audio-sync-active .glitch::before,
    .glitch-wrapper.audio-sync-active .glitch::after { opacity: 0 !important; }
}


/* Audio volume and mute controls */
.audio-control-row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
}

.audio-volume {
    width: 110px;
    accent-color: #a60000;
    cursor: pointer;
}

.audio-volume-label {
    min-width: 36px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: #aaa;
}

.audio-control-row button[aria-pressed="true"] {
    border-color: #a60000;
    color: #ff2a2a;
}

.audio-volume:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 3px;
}

@media (max-width: 600px) {
    .audio-volume {
        width: 80px;
    }

    .audio-control-row {
        gap: 5px;
    }
}

/* V02 audio-reactive: short RGB separation and scanline displacement. */
@media (prefers-reduced-motion: no-preference) {
    .glitch-wrapper.audio-sync-active .glitch {
        filter: brightness(var(--as-bright, 1));
    }
    .glitch-wrapper.audio-sync-active .glitch::before {
        text-shadow: var(--as-rgb, 0px) 0 #008cff;
    }
    .glitch-wrapper.audio-sync-active .glitch::after {
        text-shadow: calc(var(--as-rgb, 0px) * -1) 0 #428afe;
    }
    .glitch-wrapper.audio-sync-active::after {
        transform: translateY(var(--as-scan, 0%));
    }
}
.audio-volume:disabled { opacity: .55; cursor: default; }

/* V03 measured profiles: no CSS interpolation of audio-driven transforms. */
@media (prefers-reduced-motion: no-preference) {
    .glitch-wrapper.audio-sync-active .glitch,
    .glitch-wrapper.audio-sync-active .glitch::before,
    .glitch-wrapper.audio-sync-active .glitch::after,
    .glitch-wrapper.audio-sync-active::after {
        transition: none;
    }
}

/* V04 independent spectral lanes; audio-derived transform and subtle texture. */
@media (prefers-reduced-motion: no-preference) {
    .glitch-wrapper.audio-sync-active .glitch {
        transform: translate(var(--as-x,0px),var(--as-y,0px)) skewX(var(--as-sk,0deg)) scale(var(--as-scale,1));
        opacity: var(--as-flicker,1);
    }
    .glitch-wrapper.audio-sync-active .glitch.small-text {
        transform: translate(calc(var(--as-x,0px) * -.45),calc(var(--as-y,0px) * .5)) skewX(calc(var(--as-sk,0deg) * -.5));
    }
    .glitch-wrapper.audio-sync-active .glitch::before {
        transform: translate(var(--as-cx,0px),var(--as-y,0px));
    }
    .glitch-wrapper.audio-sync-active .glitch::after {
        transform: translate(var(--as-mx,0px),calc(var(--as-y,0px) * -1));
    }
    .glitch-wrapper.audio-sync-active::before {
        content: ""; position: absolute; inset: 0; z-index: 9; pointer-events: none;
        background: repeating-linear-gradient(0deg, transparent 0 3px, rgba(160,180,190,.24) 3px 4px);
        opacity: var(--as-grain,0); transition: none;
    }
}

/* Mobile layout fix for audio controls vs. CyberFur entry */
@media (max-width: 700px) {
    .audio-sync-controls {
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        align-items: center;
        text-align: center;
        width: min(94vw, 420px);
        max-width: min(94vw, 420px);
        bottom: calc(env(safe-area-inset-bottom, 0px) + 132px);
        gap: 8px;
    }

    .audio-control-row {
        width: 100%;
        justify-content: center;
        gap: 6px;
    }

    .audio-sync-controls button {
        padding: 8px 10px;
    }

    .audio-volume {
        width: min(34vw, 130px);
    }

    .audio-volume-label {
        min-width: 42px;
        text-align: left;
    }

    .audio-sync-controls small {
        display: block;
        width: 100%;
        max-width: 100%;
        font-size: 10px;
        line-height: 1.35;
        text-align: center;
        word-break: break-word;
    }

    .cf-entry {
        width: min(280px, calc(100% - 24px));
        bottom: max(18px, calc(env(safe-area-inset-bottom, 0px) + 6px));
    }
}

/* V04.1 - Responsive Smartphone and Tablet Layout */

@media (max-width: 1024px),
       (hover: none) and (pointer: coarse) {

    body.cf-portal-page {
        display: flex;
        flex-direction: column;
        min-height: 100vh;
        min-height: 100dvh;
        height: auto;
        overflow-x: hidden;
        overflow-y: auto;
    }

    /* Dynamic viewport and normal document flow */
    .cf-portal-page .glitch-wrapper {
        flex: 1 0 auto;
        width: 100%;
        height: auto;
        min-height: 250px;
        box-sizing: border-box;
        justify-content: center;
        overflow: hidden;

        padding: 24px
            max(16px, env(safe-area-inset-right))
            16px
            max(16px, env(safe-area-inset-left));
    }

    .glitch-wrapper > .glitch:first-child {
        margin-top: auto;
    }

    .glitch-wrapper > .glitch.small-text {
        margin-bottom: auto;
        max-width: 100%;
    }

    /* CyberFur portal */
    .cf-entry {
        position: relative;
        inset: auto;
        transform: none;
        flex-shrink: 0;
        margin-top: 24px;
        width: min(100%, 400px);
        gap: 8px;
    }

    .cf-entry-link {
        min-height: 48px;
    }

    /* Independent audio HUD */
    .audio-sync-controls {
        position: relative;
        inset: auto;
        transform: none;
        align-self: center;
        align-items: center;
        flex-shrink: 0;

        box-sizing: border-box;
        width: min(100%, 520px);
        max-width: 100%;

        padding: 12px
            max(16px, env(safe-area-inset-right))
            max(16px, env(safe-area-inset-bottom))
            max(16px, env(safe-area-inset-left));

        border-top: 1px solid #292929;
        background: #111;
        text-align: center;
        gap: 10px;
    }

    .audio-control-row {
        display: grid;
        grid-template-columns:
            auto auto minmax(0, 1fr) 44px;
        align-items: center;
        width: 100%;
        gap: 10px;
    }

    .audio-sync-controls button {
        min-height: 44px;
        padding: 10px;
        font-size: 12px;
        white-space: nowrap;
    }

    .audio-volume {
        width: 100%;
        min-width: 0;
        height: 32px;
        margin: 0;
        accent-color: #a60000;
    }

    .audio-volume-label {
        min-width: 0;
        font-size: 12px;
        text-align: right;
    }

    .audio-sync-controls small {
        display: block;
        width: 100%;
        font-size: 10px;
        line-height: 1.5;
        text-align: center;
        overflow-wrap: anywhere;
    }
}

/* Smartphone - two-row control layout */

@media (max-width: 600px) {

    .audio-control-row {
        grid-template-columns:
            minmax(0, 1fr) minmax(0, 1fr) 44px;
        gap: 8px;
    }

    .audio-control-row button:first-child {
        grid-column: 1;
        grid-row: 1;
    }

    .audio-control-row button:nth-child(2) {
        grid-column: 2 / 4;
        grid-row: 1;
    }

    .audio-volume {
        grid-column: 1 / 3;
        grid-row: 2;
    }

    .audio-volume-label {
        grid-column: 3;
        grid-row: 2;
    }

    .cf-entry {
        width: min(100%, 320px);
    }
}

/* Short landscape displays */

@media (max-height: 500px) and (pointer: coarse) {

    .cf-portal-page .glitch-wrapper {
        min-height: 200px;
        padding-top: 16px;
        padding-bottom: 8px;
    }

    .cf-entry {
        margin-top: 16px;
    }

    .audio-sync-controls {
        padding-top: 8px;
        gap: 6px;
    }
}
