/* ============================================
   GLITCH EFFECTS & CRT STYLES
   Effetti videocassetta rovinata anni '90
   ============================================ */

/* CRT Monitor Effect Overlay */
.crt-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9999;
    background: 
        repeating-linear-gradient(
            0deg,
            rgba(0, 0, 0, 0.15),
            rgba(0, 0, 0, 0.15) 1px,
            transparent 1px,
            transparent 2px
        );
}

/* Scanlines Effect */
.scanlines {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9998;
    background: linear-gradient(
        to bottom,
        transparent 50%,
        rgba(0, 0, 0, 0.25) 50%
    );
    background-size: 100% 4px;
    animation: scanlines-move 8s linear infinite;
}

@keyframes scanlines-move {
    0% { transform: translateY(0); }
    100% { transform: translateY(10px); }
}

/* VHS Distortion */
.vhs-distortion {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9997;
    opacity: 0.9;
    mix-blend-mode: color-dodge;
    animation: vhs-distortion 0.3s infinite;
}

.vhs-distortion::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        to bottom,
        rgba(255, 0, 0, 0.03) 0%,
        transparent 10%,
        transparent 90%,
        rgba(0, 255, 255, 0.03) 100%
    );
    animation: vhs-shift 4s ease-in-out infinite;
}

@keyframes vhs-distortion {
    0% { transform: translateX(0) skewY(0deg); }
    20% { transform: translateX(-2px) skewY(0deg); }
    40% { transform: translateX(-2px) skewY(0deg); }
    60% { transform: translateX(2px) skewY(0deg); }
    80% { transform: translateX(2px) skewY(0deg); }
    100% { transform: translateX(0) skewY(0deg); }
}

@keyframes vhs-shift {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
}

/* Static Noise */
.static-noise {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 9996;
    opacity: 0.02;
    animation: static-animation 0.2s infinite;
}

.static-noise::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 200%;
    height: 200%;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><filter id="noise"><feTurbulence baseFrequency="0.9" numOctaves="4" /></filter><rect width="100" height="100" filter="url(%23noise)" opacity="1"/></svg>');
}

@keyframes static-animation {
    0%, 100% { transform: translate(0, 0); }
    10% { transform: translate(-5%, -5%); }
    20% { transform: translate(-10%, 5%); }
    30% { transform: translate(5%, -10%); }
    40% { transform: translate(-5%, 15%); }
    50% { transform: translate(-10%, 5%); }
    60% { transform: translate(15%, 0); }
    70% { transform: translate(0, 10%); }
    80% { transform: translate(-15%, 0); }
    90% { transform: translate(10%, 5%); }
}

/* TV Static Background */
.tv-static {
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: 
        repeating-radial-gradient(
            circle at center,
            transparent 0,
            rgba(255, 255, 255, 0.05) 1px,
            transparent 2px,
            transparent 3px
        );
    animation: tv-static-move 0.5s steps(5) infinite;
    mix-blend-mode: overlay;
}

@keyframes tv-static-move {
    0% { transform: translate(0, 0) scale(1); }
    100% { transform: translate(-50px, -50px) scale(1.1); }
}

/* RGB Shift Effect */
.rgb-shift {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: inherit;
    animation: rgb-shift 10s ease-in-out infinite;
    mix-blend-mode: screen;
    opacity: 0.03;
}

@keyframes rgb-shift {
    0%, 100% {
        filter: hue-rotate(0deg) saturate(100%) brightness(1);
    }
    50% {
        filter: hue-rotate(180deg) saturate(200%) brightness(1.2);
    }
}

/* Glitch Text Effect */
.glitch {
    position: relative;
    display: inline-block;
    animation: glitch-skew 4s infinite;
}

.glitch-clone {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.8;
}

.glitch-clone:first-child {
    color: var(--color-red-glitch);
    animation: glitch-1 0.5s infinite;
    z-index: -1;
}

.glitch-clone:last-child {
    color: var(--color-cyan-glitch);
    animation: glitch-2 0.5s infinite;
    z-index: -2;
}

@keyframes glitch-skew {
    0%, 94% { transform: skew(0deg); }
    95% { transform: skew(-2deg); }
    96% { transform: skew(1deg); }
    97% { transform: skew(-1deg); }
    98% { transform: skew(2deg); }
    99% { transform: skew(0deg); }
    100% { transform: skew(0deg); }
}

@keyframes glitch-1 {
    0%, 100% {
        clip-path: inset(0 0 0 0);
        transform: translate(0);
    }
    20% {
        clip-path: inset(20% 0 30% 0);
        transform: translate(-2px, 2px);
    }
    40% {
        clip-path: inset(50% 0 20% 0);
        transform: translate(2px, -2px);
    }
    60% {
        clip-path: inset(10% 0 60% 0);
        transform: translate(-2px, 1px);
    }
    80% {
        clip-path: inset(70% 0 10% 0);
        transform: translate(1px, -1px);
    }
}

@keyframes glitch-2 {
    0%, 100% {
        clip-path: inset(0 0 0 0);
        transform: translate(0);
    }
    20% {
        clip-path: inset(60% 0 10% 0);
        transform: translate(2px, -1px);
    }
    40% {
        clip-path: inset(10% 0 70% 0);
        transform: translate(-2px, 1px);
    }
    60% {
        clip-path: inset(30% 0 40% 0);
        transform: translate(1px, 2px);
    }
    80% {
        clip-path: inset(40% 0 30% 0);
        transform: translate(-1px, -2px);
    }
}

/* Flicker Effect */
.flicker {
    animation: flicker 3s linear infinite;
}

@keyframes flicker {
    0%, 100% { opacity: 1; }
    92.5% { opacity: 0.8; }
    93% { opacity: 1; }
    94% { opacity: 0.7; }
    94.5% { opacity: 1; }
    96% { opacity: 0.9; }
    96.5% { opacity: 1; }
}

/* Tracking Distortion */
.tracking-distortion {
    animation: tracking 5s ease-in-out infinite;
}

@keyframes tracking {
    0%, 100% { letter-spacing: 0.1em; }
    50% { letter-spacing: 0.3em; }
}

/* Corrupted Text */
.corrupted-text {
    position: relative;
}

.corrupted-text::before,
.corrupted-text::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
}

.corrupted-text::before {
    animation: corruption-1 3s infinite;
    color: var(--color-red-glitch);
    z-index: -1;
}

.corrupted-text::after {
    animation: corruption-2 3s infinite;
    color: var(--color-cyan-glitch);
    z-index: -2;
}

@keyframes corruption-1 {
    0%, 90%, 100% {
        opacity: 0;
        transform: translateX(0);
    }
    91% {
        opacity: 1;
        transform: translateX(2px) translateY(-1px);
    }
    92% {
        opacity: 0;
        transform: translateX(-2px) translateY(1px);
    }
}

@keyframes corruption-2 {
    0%, 94%, 100% {
        opacity: 0;
        transform: translateX(0);
    }
    95% {
        opacity: 1;
        transform: translateX(-2px) translateY(1px);
    }
    96% {
        opacity: 0;
        transform: translateX(2px) translateY(-1px);
    }
}

/* VHS Box Effect */
.vhs-box {
    position: relative;
    overflow: hidden;
}

.vhs-box::before {
    content: "";
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.05),
        transparent
    );
    animation: vhs-scan 8s linear infinite;
}

@keyframes vhs-scan {
    0% { left: -100%; }
    100% { left: 200%; }
}

/* Glitch Card */
.glitch-card {
    position: relative;
}

.card-glitch-effect {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        45deg,
        transparent 30%,
        rgba(184, 134, 11, 0.02) 50%,
        transparent 70%
    );
    animation: glitch-card-scan 6s ease-in-out infinite;
    pointer-events: none;
}

@keyframes glitch-card-scan {
    0%, 100% { transform: translateX(-100%); }
    50% { transform: translateX(100%); }
}

/* Datamosh Button Effect */
.datamosh-btn {
    position: relative;
    overflow: hidden;
}

.datamosh-btn .btn-glitch {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    color: var(--color-red-glitch);
    transform: translateX(-2px);
}

.datamosh-btn:hover .btn-glitch {
    animation: datamosh 0.3s ease-in-out;
}

@keyframes datamosh {
    0%, 100% { 
        opacity: 0;
        transform: translateX(0);
    }
    50% { 
        opacity: 0.7;
        transform: translateX(-4px) scaleX(1.1);
    }
}

/* Terminal Cursor */
.cursor-blink {
    animation: terminal-cursor 1s step-end infinite;
    font-weight: bold;
}

@keyframes terminal-cursor {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* Typing Effect */
.typing-effect {
    overflow: visible !important;  
    white-space: normal !important;  
    word-wrap: break-word;
    animation: none;  
    min-height: 1.5em;  
}

/* Assicura che il testo nel box collaborazioni vada a capo */
.collab-intro .terminal-text {
    white-space: normal !important;
    word-wrap: break-word;
    overflow-wrap: break-word;
    display: block;
    width: 100%;
}

@keyframes typing {
    from { width: 0; }
    to { width: 100%; }
}

.typing-sequence-1 {
    overflow: hidden;
    white-space: normal;
    animation: typing-seq-1 2s steps(40, end) forwards;
}

.typing-sequence-2 {
    overflow: hidden;
    white-space: normal;
    opacity: 0;
    animation: typing-seq-2 2s steps(40, end) 2.5s forwards;
}

@keyframes typing-seq-1 {
    0% { 
        width: 0;
        opacity: 1;
    }
    100% { 
        width: 100%;
        opacity: 1;
    }
}

@keyframes typing-seq-2 {
    0% { 
        width: 0;
        opacity: 0;
    }
    1% {
        opacity: 1;
    }
    100% { 
        width: 100%;
        opacity: 1;
    }
}

/* CRT Input Effect */
.crt-input {
    position: relative;
}

.input-scanline {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: var(--color-brass);
    transform: scaleX(0);
    transition: transform 0.3s;
}

.form-input:focus + .input-scanline {
    transform: scaleX(1);
    animation: scanline-pulse 1s ease-in-out infinite;
}

@keyframes scanline-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Corruption Button */
.corruption-btn {
    position: relative;
}

.btn-corruption {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    opacity: 0;
    font-family: 'Courier New', monospace;
    color: var(--color-green-terminal);
    text-transform: uppercase;
    letter-spacing: 0.2em;
}

.corruption-btn:hover .btn-corruption {
    animation: transmit 2s ease-in-out;
}

@keyframes transmit {
    0%, 100% { 
        opacity: 0;
        transform: translate(-50%, -50%) scale(1);
    }
    50% { 
        opacity: 1;
        transform: translate(-50%, -50%) scale(1.1);
    }
}

/* Scrolling Text */
.scrolling-text {
    display: inline-block;
    animation: scroll-text 15s linear infinite;
}

@keyframes scroll-text {
    0% { transform: translateX(100%); }
    100% { transform: translateX(-100%); }
}

/* CRT Lines in Footer */
.crt-lines {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 2px,
        rgba(0, 255, 0, 0.03) 2px,
        rgba(0, 255, 0, 0.03) 4px
    );
    pointer-events: none;
    animation: crt-lines-move 10s linear infinite;
}

@keyframes crt-lines-move {
    0% { background-position: 0 0; }
    100% { background-position: 0 10px; }
}

/* Glitch Button Hover States */
.glitch-btn {
    position: relative;
}

.glitch-btn::before,
.glitch-btn::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
}

.glitch-btn::before {
    color: var(--color-red-glitch);
    animation: glitch-btn-1 0.3s infinite;
    animation-play-state: paused;
}

.glitch-btn::after {
    color: var(--color-cyan-glitch);
    animation: glitch-btn-2 0.3s infinite;
    animation-play-state: paused;
}

.glitch-btn:hover::before,
.glitch-btn:hover::after {
    animation-play-state: running;
}

@keyframes glitch-btn-1 {
    0%, 100% {
        opacity: 0;
        transform: translate(0);
    }
    33% {
        opacity: 0.5;
        transform: translate(2px, -2px);
    }
}

@keyframes glitch-btn-2 {
    0%, 100% {
        opacity: 0;
        transform: translate(0);
    }
    66% {
        opacity: 0.5;
        transform: translate(-2px, 2px);
    }
}

/* Performance Optimization */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
