/* === MATRIX "INFECTED" GLITCH LAYER ===
   Subtle by default — does NOT disturb reading. An occasional, brief screen
   "crack" reveals green katakana digit-rain (painted on #matrixCanvas by
   matrix.js), with light scanlines + RGB-split for a "virus" feel. Hovering
   the hero name or the Agent Smith card raises the glitch rate.
   All colours come from the --matrix-* tokens in base.css (no hardcoding).
   Pure CSS + one throttled canvas → negligible cost; canvas only paints
   during a glitch burst. Respects prefers-reduced-motion. */

/* Digit-rain canvas — full screen, above content but click-through. Now
   ALWAYS painting a faint Matrix rain in one corner (the page is permanently
   "infected"); during a glitch burst it additionally paints jagged cracks
   that leak rain. JS owns opacity (1 when live, 0 in ?static / reduced). */
#matrixCanvas {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 90;             /* above page content (z3) */
    pointer-events: none;
    opacity: 0;              /* JS sets this to 1 once it confirms it's running */
}

/* Always-on CRT scanlines — extremely faint so reading is unaffected, with a
   slow continuous downward drift so the "screen" feels alive/infected rather
   than static. The drift is 1px-fine and very slow → not distracting. */
.crt-scanlines {
    position: fixed;
    inset: 0;
    z-index: 92;
    pointer-events: none;
    background: repeating-linear-gradient(
        to bottom,
        rgba(var(--matrix-rgb), 0.035) 0px,
        rgba(var(--matrix-rgb), 0.035) 1px,
        transparent 1px,
        transparent 3px
    );
    background-size: 100% 3px;
    opacity: 0.16;
    mix-blend-mode: screen;
    transition: opacity 0.6s ease;
    animation: scanDrift 7s linear infinite;
}
/* During a burst the scanlines firm up — but only to 0.34 (not a harsh flash),
   and they ease in/out over the burst so it reads as "intensifying", calm. */
body.glitching .crt-scanlines { opacity: 0.34; }

@keyframes scanDrift {
    from { background-position-y: 0; }
    to   { background-position-y: 3px; }   /* one scanline period — seamless loop */
}

/* RGB-split / chromatic-aberration fringe. Reworked from a 0.32s twitch into a
   slow ~1.8s drift that eases in, lingers, and eases out — a sustained
   "signal degraded" feel instead of a flash. Two faint offset colour edges. */
.glitch-rgb {
    position: fixed;
    inset: 0;
    z-index: 91;
    pointer-events: none;
    opacity: 0;
    mix-blend-mode: screen;
    background:
        linear-gradient(90deg, rgba(255, 0, 60, 0.05), transparent 9%),
        linear-gradient(270deg, rgba(0, 200, 255, 0.05), transparent 9%);
    will-change: opacity, transform;
}
body.glitching .glitch-rgb {
    animation: rgbDrift 1.9s ease-in-out 1;
}

@keyframes rgbDrift {
    0%   { opacity: 0;    transform: translateX(0); }
    18%  { opacity: 0.5;  transform: translateX(-2px); }
    50%  { opacity: 0.42; transform: translateX(2px); }
    82%  { opacity: 0.5;  transform: translateX(-1px); }
    100% { opacity: 0;    transform: translateX(0); }
}

/* Whole-page wobble during a glitch — softened from a steps() shake to a
   gentle sub-pixel sway + faint contrast pulse over the burst. No clip-path
   jumps, no harsh stepping → "queasy/infected", not "seizure". */
body.glitching .page {
    animation: pageDrift 1.9s ease-in-out 1;
}
@keyframes pageDrift {
    0%   { transform: translate(0, 0);      filter: none; }
    25%  { transform: translate(-0.6px, 0); filter: contrast(1.06) saturate(1.05); }
    50%  { transform: translate(0.6px, -0.4px); filter: contrast(1.04) saturate(1.04); }
    75%  { transform: translate(-0.4px, 0.4px); filter: contrast(1.06) saturate(1.05); }
    100% { transform: translate(0, 0);      filter: none; }
}

@media (prefers-reduced-motion: reduce) {
    #matrixCanvas, .glitch-rgb { display: none !important; }
    .crt-scanlines { opacity: 0.08 !important; animation: none !important; }
    body.glitching .page { animation: none !important; }
}

/* Screenshot static mode: freeze the infection (no flicker) but keep
   scanlines faint so the aesthetic still reads. */
body.static #matrixCanvas,
body.static .glitch-rgb { opacity: 0 !important; animation: none !important; }
body.static .crt-scanlines { opacity: 0.12 !important; }

/* =====================================================================
   AGENT SMITH — the one "infected" card. Flips local palette to Matrix
   green via scoped vars so it reads as hacked against the gold site.
   ===================================================================== */
.project-card--matrix {
    --accent: var(--matrix-green);
    --accent-dim: var(--matrix-green-faint);
    background: linear-gradient(135deg, var(--matrix-bg) 0%, var(--matrix-bg-soft) 100%);
    border: 1px solid var(--matrix-green-faint);
    border-radius: 16px;
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 0 40px rgba(var(--matrix-rgb), 0.06);
}
/* Faint digit-rain texture inside the card (static SVG, no JS needed). */
.project-card--matrix::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0.16;
    background-image: repeating-linear-gradient(
        90deg,
        rgba(var(--matrix-rgb), 0.10) 0 1px,
        transparent 1px 13px
    );
    mask-image: linear-gradient(to bottom, black, transparent 85%);
    -webkit-mask-image: linear-gradient(to bottom, black, transparent 85%);
}
.project-card--matrix .project-name {
    color: var(--matrix-green);
    text-shadow: var(--matrix-glow);
    font-family: 'Courier New', ui-monospace, monospace;
    letter-spacing: 0.04em;
}
.project-card--matrix .project-desc { color: rgba(var(--matrix-rgb), 0.62); }
.project-card--matrix .project-arrow { color: var(--matrix-green); }
.project-card--matrix:hover {
    border-color: var(--matrix-green-dim);
    box-shadow: 0 0 28px rgba(var(--matrix-rgb), 0.18),
                inset 0 0 50px rgba(var(--matrix-rgb), 0.10);
}
.matrix-logo {
    width: 56px; height: 56px;
    border-radius: 14px;
    border: 1px solid var(--matrix-green-faint);
    background: #000;
    display: flex; align-items: center; justify-content: center;
    box-shadow: var(--matrix-glow);
    flex-shrink: 0;
}
/* New shades SVG is wider than tall (viewBox 64x40) — size by width, keep ratio. */
.matrix-logo svg.ms-shades { width: 40px; height: auto; }
.matrix-logo svg { width: 32px; height: 32px; }

/* ── Agent shades v2 — Blinde "Cube 136005" screen-accurate: small rectangular
   lenses, thin wire frame, hinge joints + bridge bolts, dark glass + green
   rain reflection. All colour from --matrix-* tokens (no hardcoded hex). ── */
.matrix-logo svg.agent { width: 46px; height: auto; }
body.matrix-page .ms-logo .agent { width: 74px; height: auto; }
.agent { filter: drop-shadow(0 0 5px rgba(var(--matrix-rgb), 0.5)); }
.agent .lens { fill: var(--matrix-bg); }
.agent .lens-tint { fill: rgba(var(--matrix-rgb), 0.10); }
.agent .frame { stroke: var(--matrix-green); stroke-width: 0.9; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.agent .sheen { stroke: var(--matrix-green-bright); stroke-width: 0.4; fill: none; opacity: 0.55; }
.agent .joint { fill: var(--matrix-green-bright); }
.agent .bolt { fill: var(--matrix-green); }
.agent .reflect { fill: rgba(var(--matrix-rgb), 0.16); }
.agent .rain text { fill: var(--matrix-green); }
.agent .rain .head { fill: var(--matrix-green-bright); }
@media (max-width: 768px) { .matrix-logo svg.agent { width: 38px; } }
/* Digit-rain columns reflected inside the lenses. The outer <g> sets X via an
   SVG attribute; this inner <g> only animates Y so the CSS transform never
   clobbers the column's horizontal position. */
.ms-col {
    animation: msLensFall var(--d, 1.6s) linear infinite;
    animation-delay: var(--dl, 0s);
}
@keyframes msLensFall {
    from { transform: translateY(-10px); }
    to   { transform: translateY(16px); }
}
@media (max-width: 768px) {
    .matrix-logo { width: 40px; height: 40px; }
    .matrix-logo svg.ms-shades { width: 32px; height: auto; }
    .matrix-logo svg { width: 24px; height: 24px; }
}
@media (prefers-reduced-motion: reduce) {
    .ms-col { animation: none; }
}

/* =====================================================================
   AGENT SMITH DETAIL PAGE — fully "infected" (black + green + rain).
   Activated by <body class="matrix-page">. Tokens overridden at body scope.
   ===================================================================== */
body.matrix-page {
    --bg: var(--matrix-bg);
    --text: #c8ffd6;
    --muted: rgba(var(--matrix-rgb), 0.45);
    --accent: var(--matrix-green);
    --accent-dim: var(--matrix-green-faint);
    background: var(--matrix-bg);
    color: var(--text);
    font-family: 'Courier New', ui-monospace, monospace;
}
body.matrix-page .ms-canvas {
    position: fixed; inset: 0;
    width: 100%; height: 100%;
    z-index: 0; pointer-events: none;
    opacity: 0.22;
}
body.matrix-page .ms-wrap {
    position: relative; z-index: 3;
    max-width: 760px;
    margin: 0 auto;
    padding: 4rem 1.5rem 6rem;
}
body.matrix-page .ms-back {
    display: inline-block;
    color: var(--matrix-green-dim);
    text-decoration: none;
    font-size: 0.72rem; letter-spacing: 0.2em; text-transform: uppercase;
    margin-bottom: 3rem;
    transition: color 0.25s ease;
}
body.matrix-page .ms-back:hover { color: var(--matrix-green); }
body.matrix-page .ms-eyebrow {
    color: var(--matrix-green);
    font-size: 0.72rem; letter-spacing: 0.34em; text-transform: uppercase;
    text-shadow: var(--matrix-glow);
    margin-bottom: 1.4rem;
}
body.matrix-page .ms-title {
    font-size: clamp(2.6rem, 7vw, 4.6rem);
    font-weight: 700;
    line-height: 0.95;
    letter-spacing: -0.01em;
    color: var(--matrix-green);
    text-shadow: 0 0 24px rgba(var(--matrix-rgb), 0.45);
    margin-bottom: 0.4rem;
}
body.matrix-page .ms-title .cursor {
    display: inline-block;
    width: 0.6ch; margin-left: 0.1ch;
    background: var(--matrix-green);
    animation: msBlink 1.05s steps(1) infinite;
    box-shadow: var(--matrix-glow);
}
@keyframes msBlink { 0%, 50% { opacity: 1; } 50.01%, 100% { opacity: 0; } }
body.matrix-page .ms-sub {
    color: rgba(var(--matrix-rgb), 0.6);
    font-size: 1.05rem; line-height: 1.7;
    max-width: 540px;
    margin: 1.4rem 0 3rem;
}
body.matrix-page .ms-rule {
    width: 60px; height: 1px; background: var(--matrix-green-dim);
    margin: 2.5rem 0; box-shadow: var(--matrix-glow);
}
body.matrix-page .ms-feats {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem; margin-top: 2rem;
}
body.matrix-page .ms-feat {
    border: 1px solid var(--matrix-green-faint);
    border-radius: 12px;
    padding: 1.3rem 1.4rem;
    background: rgba(var(--matrix-rgb), 0.03);
    transition: border-color 0.25s ease, box-shadow 0.25s ease;
}
body.matrix-page .ms-feat:hover {
    border-color: var(--matrix-green-dim);
    box-shadow: inset 0 0 30px rgba(var(--matrix-rgb), 0.06);
}
body.matrix-page .ms-feat h3 {
    color: var(--matrix-green);
    font-size: 0.95rem; letter-spacing: 0.02em; margin-bottom: 0.5rem;
    text-shadow: var(--matrix-glow);
}
body.matrix-page .ms-feat p {
    color: rgba(var(--matrix-rgb), 0.55);
    font-size: 0.86rem; line-height: 1.6;
}
body.matrix-page .ms-quote {
    margin-top: 3.5rem;
    font-size: 1.15rem; font-style: italic;
    color: rgba(var(--matrix-rgb), 0.7);
    border-left: 2px solid var(--matrix-green-dim);
    padding-left: 1.2rem;
}
body.matrix-page .ms-foot {
    margin-top: 4rem;
    font-size: 0.72rem; letter-spacing: 0.18em; text-transform: uppercase;
    color: rgba(var(--matrix-rgb), 0.3);
}

/* =====================================================================
   AGENT SMITH DETAIL PAGE — interactions (logo, wake/voice demo,
   typewriter, and the cyber-horror dressing). All scoped to
   body.matrix-page so the rest of the site is untouched. Matrix tokens.
   ===================================================================== */

/* Shades logo + hover easter egg */
body.matrix-page .ms-logo {
    position: relative;
    display: inline-flex; align-items: center; gap: 0.9rem;
    margin-bottom: 1.8rem;
    cursor: pointer;
}
body.matrix-page .ms-logo .ms-shades {
    width: 64px; height: auto;
    filter: drop-shadow(0 0 10px rgba(var(--matrix-rgb), 0.45));
    transition: filter 0.3s ease;
}
body.matrix-page .ms-logo:hover .ms-shades {
    filter: drop-shadow(0 0 18px rgba(var(--matrix-rgb), 0.8));
}
/* Hover accelerates the lens digit-rain. */
body.matrix-page .ms-logo:hover .ms-col { animation-duration: 0.5s !important; }
body.matrix-page .ms-microtext {
    font-family: 'Courier New', ui-monospace, monospace;
    font-size: 0.8rem; letter-spacing: 0.06em;
    color: rgba(var(--matrix-rgb), 0.5);
    text-shadow: var(--matrix-glow);
    opacity: 0; transform: translateX(-4px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    pointer-events: none; white-space: nowrap;
}
body.matrix-page .ms-logo:hover .ms-microtext { opacity: 0.85; transform: translateX(0); }

/* Typewriter caret on the manifesto while typing */
body.matrix-page .ms-sub.ms-typing::after {
    content: ""; display: inline-block;
    width: 0.55ch; height: 1em; margin-left: 0.12ch;
    vertical-align: -0.12em;
    background: var(--matrix-green);
    box-shadow: var(--matrix-glow);
    animation: msBlink 1.05s steps(1) infinite;
}

/* Wake-word / voice demo */
body.matrix-page .ms-wake { margin: 2.2rem 0 0.5rem; }
body.matrix-page .ms-wake-btn {
    font-family: 'Courier New', ui-monospace, monospace;
    font-size: 0.95rem; letter-spacing: 0.02em;
    color: var(--matrix-green);
    background: rgba(var(--matrix-rgb), 0.05);
    border: 1px solid var(--matrix-green-faint);
    border-radius: 10px;
    padding: 0.7rem 1.1rem;
    cursor: pointer;
    transition: border-color 0.25s ease, box-shadow 0.25s ease, background 0.25s ease;
    -webkit-tap-highlight-color: transparent;
}
body.matrix-page .ms-wake-btn:hover {
    border-color: var(--matrix-green-dim);
    box-shadow: 0 0 20px rgba(var(--matrix-rgb), 0.22), inset 0 0 22px rgba(var(--matrix-rgb), 0.06);
    background: rgba(var(--matrix-rgb), 0.08);
}
body.matrix-page .ms-prompt { color: var(--matrix-green-bright); }
body.matrix-page .ms-ww { text-shadow: var(--matrix-glow); }
body.matrix-page .ms-wake-caret {
    display: inline-block; width: 0.55ch; height: 1em; margin-left: 0.18ch;
    vertical-align: -0.12em; background: var(--matrix-green);
    box-shadow: var(--matrix-glow);
    animation: msBlink 1.05s steps(1) infinite;
}
/* Listening state */
body.matrix-page .ms-wake-btn.ms-listening {
    animation: msListen 0.9s ease-in-out infinite;
}
body.matrix-page .ms-wake-btn.ms-listening .ms-wake-caret { display: none; }
body.matrix-page .ms-wake-btn.ms-listening::after {
    content: "  \25CF listening\2026";
    color: var(--matrix-green-bright);
}
@keyframes msListen {
    0%, 100% { box-shadow: 0 0 10px rgba(var(--matrix-rgb), 0.2); border-color: var(--matrix-green-faint); }
    50%      { box-shadow: 0 0 26px rgba(var(--matrix-rgb), 0.5); border-color: var(--matrix-green-dim); }
}
/* Reveal stage (waveform + reply) */
body.matrix-page .ms-wake-stage {
    overflow: hidden;
    max-height: 0; opacity: 0;
    transition: max-height 0.45s ease, opacity 0.45s ease, margin 0.45s ease;
    margin-top: 0;
}
body.matrix-page .ms-wake-stage.ms-show { max-height: 180px; opacity: 1; margin-top: 1rem; }
body.matrix-page .ms-wave {
    display: block; width: 100%; max-width: 360px; height: 56px;
}
body.matrix-page .ms-wake-reply {
    font-family: 'Courier New', ui-monospace, monospace;
    color: var(--matrix-green); font-size: 1rem; letter-spacing: 0.02em;
    text-shadow: var(--matrix-glow); margin-top: 0.6rem; min-height: 1.2em;
}

/* Wake "jolt" — a brief, restrained glitch when Smith answers. */
body.matrix-page.ms-jolt .ms-wrap { animation: msJolt 0.55s ease-in-out 1; }
@keyframes msJolt {
    0%   { transform: translate(0,0);    filter: none; }
    30%  { transform: translate(-1px,0); filter: contrast(1.12) brightness(1.08); }
    60%  { transform: translate(1px,0);  filter: contrast(1.08) saturate(1.2); }
    100% { transform: translate(0,0);    filter: none; }
}

/* Corner console leaking creepy logs */
body.matrix-page .ms-term {
    position: fixed; left: 16px; bottom: 16px; z-index: 5;
    width: 300px; max-width: 44vw;
    font-family: 'Courier New', ui-monospace, monospace;
    background: rgba(0, 10, 2, 0.55);
    border: 1px solid var(--matrix-green-faint);
    border-radius: 8px;
    box-shadow: inset 0 0 24px rgba(var(--matrix-rgb), 0.05);
    opacity: 0.52; pointer-events: none;
    overflow: hidden;
}
body.matrix-page .ms-term-bar {
    font-size: 9px; letter-spacing: 0.08em; text-transform: uppercase;
    color: rgba(var(--matrix-rgb), 0.5);
    padding: 5px 9px; border-bottom: 1px solid var(--matrix-green-faint);
    display: flex; align-items: center; gap: 6px;
}
body.matrix-page .ms-term-dot {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--matrix-green); box-shadow: var(--matrix-glow);
    animation: msBlink 1.6s steps(1) infinite;
}
body.matrix-page .ms-term-body { padding: 7px 9px; }
body.matrix-page .ms-term-line {
    font-size: 11px; line-height: 1.55;
    color: rgba(var(--matrix-rgb), 0.62);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    animation: msTermIn 0.3s ease 1;
}
@keyframes msTermIn { from { opacity: 0; } to { opacity: 1; } }
@media (max-width: 640px) { body.matrix-page .ms-term { display: none; } }

/* Brief FATAL error flash */
body.matrix-page .ms-error {
    position: fixed; inset: 0; z-index: 95;
    display: flex; align-items: center; justify-content: center;
    font-family: 'Courier New', ui-monospace, monospace;
    font-size: clamp(1rem, 3.5vw, 1.7rem); letter-spacing: 0.06em;
    /* Pure Matrix green — no red. The chromatic "RGB-split" is now a green-only
       double-offset (still reads as a glitch/aberration, stays on-theme). */
    color: var(--matrix-green-bright);
    text-shadow: 2px 0 rgba(var(--matrix-rgb), 0.55), -2px 0 rgba(var(--matrix-rgb), 0.3);
    background: rgba(0, 12, 2, 0.22);
    opacity: 0; pointer-events: none;
    transition: opacity 0.12s ease;
}
body.matrix-page .ms-error.ms-show { opacity: 1; animation: msErrShake 0.85s steps(2) 1; }
@keyframes msErrShake {
    0% { transform: translate(0,0); }
    25% { transform: translate(-3px, 1px); }
    50% { transform: translate(3px, -1px); }
    75% { transform: translate(-2px, 0); }
    100% { transform: translate(0,0); }
}

/* Ghost cursor */
body.matrix-page .ms-ghost {
    position: fixed; top: 0; left: 0; z-index: 96;
    width: 0; height: 0;
    border-left: 9px solid var(--matrix-green);
    border-bottom: 6px solid transparent;
    border-top: 6px solid transparent;
    filter: drop-shadow(0 0 6px rgba(var(--matrix-rgb), 0.9));
    transform: translate(-100px, -100px) rotate(-32deg);
    opacity: 0; pointer-events: none;
    transition: opacity 0.5s ease;
}
body.matrix-page .ms-ghost.ms-show { opacity: 0.5; }

@media (prefers-reduced-motion: reduce) {
    body.matrix-page .ms-wake-caret,
    body.matrix-page .ms-term-dot,
    body.matrix-page .ms-sub.ms-typing::after { animation: none !important; }
    body.matrix-page .ms-term { opacity: 0.4; }
}
