/* ==========================================================================
   continuum.quest // Probe 7-of-7
   Strict duotone, Z-pattern, wave-form protagonist.
   --------------------------------------------------------------------------
   Implementation notes (per DESIGN.md):
     - Intersection Observer (threshold 0.5) flips `stroke-dashoffset: 0` with
       the cubic-bezier transition on intersect. The `IntersectionObserver`
       state is also queried by the once-per-30s flicker (setInterval(30000)).
     - Display word reveal uses chromatic-aberration text-shadow.
   ========================================================================== */

:root {
    /* The five-color palette - exact hex from DESIGN.md */
    --ink-primary: #0B0F1A;     /* deep prussian-graphite */
    --ink-secondary: #F2EBDC;   /* warm cream-paper */
    --paper-pure: #FFFFFF;      /* pure white reference */
    --probe-trail: #5C7A8C;     /* desaturated dusk-teal (HUD osc) */
    --glitch-accent: #FF3B57;   /* coral-red - rare, three appearances */

    /* Per-panel surface tokens flipped on .panel--inverted */
    --bg: var(--ink-secondary);
    --fg: var(--ink-primary);

    /* Type sizes */
    --type-display: clamp(72px, 14vw, 156px);
    --type-narration: 16px;
    --type-mono: 13px;

    /* Layout */
    --gutter: 24px;
    --hud-h: 32px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    background: var(--ink-secondary);
    color: var(--ink-primary);
    font-family: 'Inter', sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
}

html {
    scroll-behavior: smooth;
}

body {
    line-height: 1.5;
    min-height: 100vh;
}

/* ==========================================================================
   HUD - probe-status bar
   ========================================================================== */

.hud {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--hud-h);
    z-index: 50;
    background: var(--ink-primary);
    color: var(--ink-secondary);
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    align-items: center;
    padding: 0 var(--gutter);
    font-family: 'DM Mono', monospace;
    font-weight: 500;
    font-size: var(--type-mono);
    letter-spacing: 0.12em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--probe-trail);
}

.hud__ticker {
    display: flex;
    gap: 10px;
    align-items: center;
    white-space: nowrap;
    overflow: hidden;
}

.hud__brand {
    color: var(--ink-secondary);
}

.hud__sep {
    color: var(--probe-trail);
}

.hud__role {
    color: var(--ink-secondary);
}

.hud__signal {
    color: var(--probe-trail);
    margin-left: 6px;
}

.hud__waveform {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 8px;
}

.hud__waveform svg {
    width: 100%;
    height: 22px;
    display: block;
}

#hud-osc-path {
    fill: none;
    stroke: var(--probe-trail);
    stroke-width: 1.25;
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
}

.hud__clock {
    text-align: right;
    color: var(--ink-secondary);
    font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   Main container
   ========================================================================== */

main {
    display: block;
    margin-top: var(--hud-h);
}

/* ==========================================================================
   Panel - 16-col Fibonacci-leaning subgrid, Z-anchors
   ========================================================================== */

.panel {
    position: relative;
    min-height: calc(100vh - var(--hud-h));
    height: calc(100vh - var(--hud-h));
    width: 100%;
    background: var(--bg);
    color: var(--fg);
    display: grid;
    /* Fibonacci-leaning 16-col rhythm: 1,1,2,3,5,8,5,3,2,1,1,2,3,5,8,5 */
    grid-template-columns: 1fr 1fr 2fr 3fr 5fr 8fr 5fr 3fr 2fr 1fr 1fr 2fr 3fr 5fr 8fr 5fr;
    grid-template-rows: 1fr 1fr;
    column-gap: var(--gutter);
    padding: 56px var(--gutter) 56px;
    overflow: hidden;
    transition: background 0.6s ease, color 0.6s ease;
}

.panel--inverted {
    --bg: var(--ink-primary);
    --fg: var(--ink-secondary);
}

/* Z-anchor 1: panel id (top-left) */
.panel__id {
    grid-column: 1 / 5;
    grid-row: 1 / 2;
    align-self: start;
    justify-self: start;
    font-family: 'DM Mono', monospace;
    font-weight: 500;
    font-size: var(--type-mono);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--fg);
    z-index: 4;
    padding-top: 8px;
}

/* Z-anchor 2: zone display word (top-right) */
.panel__display {
    grid-column: 11 / 17;
    grid-row: 1 / 2;
    align-self: start;
    justify-self: end;
    font-family: 'Fraunces', 'Lora', serif;
    font-weight: 700;
    font-variation-settings: "opsz" 144, "SOFT" 100, "wght" 700;
    font-size: var(--type-display);
    line-height: 0.95;
    letter-spacing: -0.02em;
    color: var(--fg);
    text-align: right;
    z-index: 4;
    padding-top: 8px;
}

.display-word {
    display: inline-block;
    position: relative;
    transform: translateY(20px);
    opacity: 0;
    transition: transform 1.0s cubic-bezier(0.16, 1.08, 0.36, 1.0),
                opacity 1.0s cubic-bezier(0.16, 1.08, 0.36, 1.0);
}

.panel.is-active .display-word {
    transform: translateY(0);
    opacity: 1;
}

/* Glitch flicker on display word (driven by .is-flickering class from JS).
   This is the second of the three sanctioned glitch-accent appearances. */
.display-word.is-flickering {
    animation: chromatic-flicker 60ms steps(2, end) 1;
}

@keyframes chromatic-flicker {
    0% {
        text-shadow: 1px 0 0 var(--glitch-accent), -1px 0 0 var(--probe-trail);
        transform: translate(0, 0);
    }
    50% {
        text-shadow: 2px 0 0 var(--glitch-accent), -2px 0 0 var(--probe-trail);
        transform: translate(-1px, 0);
    }
    100% {
        text-shadow: none;
        transform: translate(0, 0);
    }
}

/* Initial chromatic-aberration on first reveal */
.display-word.is-revealing {
    animation: reveal-chroma 0.7s ease-out 1;
}

@keyframes reveal-chroma {
    0% { text-shadow: 4px 0 0 var(--glitch-accent), -4px 0 0 var(--probe-trail); }
    60% { text-shadow: 2px 0 0 var(--glitch-accent), -2px 0 0 var(--probe-trail); }
    100% { text-shadow: none; }
}

/* Z-anchor 3: probe narration (bottom-left) */
.panel__narration {
    grid-column: 1 / 7;
    grid-row: 2 / 3;
    align-self: end;
    justify-self: start;
    font-family: 'Inter', 'Commissioner', sans-serif;
    font-weight: 400;
    font-size: var(--type-narration);
    line-height: 1.7;
    letter-spacing: -0.005em;
    color: var(--fg);
    max-width: 56ch;
    z-index: 4;
}

.panel__narration em {
    font-style: normal;
    font-weight: 700;
    font-variation-settings: "opsz" 14;
}

.panel__narration strong {
    font-weight: 700;
    font-style: italic;
    font-family: 'DM Mono', monospace;
    font-size: 0.9em;
    letter-spacing: 0.06em;
}

/* Z-anchor 4: glyph (bottom-right) */
.panel__glyph {
    grid-column: 13 / 17;
    grid-row: 2 / 3;
    align-self: end;
    justify-self: end;
    width: 120px;
    height: 120px;
    z-index: 4;
}

.glyph {
    width: 100%;
    height: 100%;
    display: block;
    transition: transform 0.2s ease;
}

.glyph__strokes {
    fill: none;
    stroke: var(--fg);
    stroke-width: 1.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: stroke-width 0.2s ease;
}

.glyph__strokes text {
    fill: var(--fg);
    stroke: none;
}

.panel__glyph:hover .glyph__strokes {
    stroke-width: 2.5;
}

.panel__glyph:hover .glyph__strokes {
    filter: drop-shadow(1px 0 0 transparent);
}

/* hover micro-glitch toggle is handled in JS, but we provide the class here */
.glyph.is-glitched .glyph__strokes {
    filter: drop-shadow(1.5px 0 0 var(--glitch-accent)) drop-shadow(-1.5px 0 0 var(--probe-trail));
}

/* Waveform tracing the diagonal of the Z */
.waveform {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;
}

.waveform__path {
    fill: none;
    stroke: var(--fg);
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
    /* dash animation primed via inline style + JS */
    stroke-dasharray: var(--len, 4000);
    stroke-dashoffset: var(--len, 4000);
    transition: stroke-dashoffset 1.4s cubic-bezier(0.16, 1.08, 0.36, 1.0);
}

.waveform__path--thin {
    stroke-width: 1;
    opacity: 0.55;
}

.panel.is-active .waveform__path {
    stroke-dashoffset: 0;
}

/* ==========================================================================
   Interstitial - duotone-flip + glitch-band sweep
   ========================================================================== */

.interstitial {
    position: relative;
    width: 100%;
    height: 40vh;
    background: var(--ink-primary);
    overflow: hidden;
}

.interstitial[data-from="2"],
.interstitial[data-from="4"],
.interstitial[data-from="6"] {
    background: var(--ink-secondary);
}

.glitch-band {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

/* Glitch-band rects: difference blend, ±8px sweep.
   This is the FIRST of the sanctioned glitch-accent appearances. */
.glitch-band__a {
    fill: var(--glitch-accent);
    opacity: 0;
    mix-blend-mode: difference;
    transform: translateX(0);
}

.glitch-band__b {
    fill: var(--probe-trail);
    opacity: 0;
    mix-blend-mode: difference;
    transform: translateX(0);
}

.interstitial.is-glitching .glitch-band__a {
    animation: glitch-sweep-a 1s steps(8, end) 1;
}

.interstitial.is-glitching .glitch-band__b {
    animation: glitch-sweep-b 1s steps(8, end) 1;
}

@keyframes glitch-sweep-a {
    0%   { opacity: 0; transform: translateX(0); }
    10%  { opacity: 0.85; transform: translateX(-8px); }
    30%  { opacity: 0.9; transform: translateX(6px); }
    50%  { opacity: 0.7; transform: translateX(-4px); }
    70%  { opacity: 0.85; transform: translateX(8px); }
    100% { opacity: 0; transform: translateX(0); }
}

@keyframes glitch-sweep-b {
    0%   { opacity: 0; transform: translateX(0); }
    10%  { opacity: 0.85; transform: translateX(8px); }
    30%  { opacity: 0.9; transform: translateX(-6px); }
    50%  { opacity: 0.7; transform: translateX(4px); }
    70%  { opacity: 0.85; transform: translateX(-8px); }
    100% { opacity: 0; transform: translateX(0); }
}

/* ==========================================================================
   Terminator panel (7) - centered final
   ========================================================================== */

.panel--terminator {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto auto;
    justify-items: center;
    text-align: center;
    padding-top: 96px;
    padding-bottom: 96px;
}

.panel--terminator .panel__id {
    grid-column: 1;
    grid-row: 1;
    justify-self: center;
}

.panel--terminator .panel__display {
    grid-column: 1;
    grid-row: 2;
    justify-self: center;
    text-align: center;
    margin-top: 8px;
}

.panel--terminator .panel__narration {
    grid-column: 1;
    grid-row: 3;
    justify-self: center;
    margin-top: 32px;
    text-align: center;
}

.panel--terminator .panel__glyph {
    grid-column: 1;
    grid-row: 4;
    justify-self: center;
    margin-top: 36px;
}

.waveform--terminator {
    z-index: 1;
}

/* Recursion link - third sanctioned glitch-accent appearance */
.recursion-link {
    grid-column: 1;
    grid-row: 5;
    justify-self: center;
    margin-top: 36px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    color: var(--fg);
    text-decoration: none;
    font-family: 'DM Mono', monospace;
    font-size: var(--type-mono);
    letter-spacing: 0.12em;
    text-transform: uppercase;
    z-index: 4;
    cursor: pointer;
    padding: 12px 18px;
    border: 1px solid var(--fg);
}

.recursion-link__arrow {
    color: var(--glitch-accent);
    font-family: 'Fraunces', serif;
    font-size: 48px;
    line-height: 1;
    display: inline-block;
    transition: transform 0.6s cubic-bezier(0.16, 1.08, 0.36, 1.0);
}

.recursion-link:hover .recursion-link__arrow {
    transform: rotate(-360deg);
}

.recursion-link__text {
    max-width: 60ch;
    line-height: 1.5;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 900px) {
    .panel {
        grid-template-columns: repeat(8, 1fr);
        height: auto;
        min-height: 100vh;
        padding: 72px 20px 56px;
    }
    .panel__id {
        grid-column: 1 / 5;
    }
    .panel__display {
        grid-column: 1 / 9;
        justify-self: start;
        text-align: left;
        margin-top: 24px;
    }
    .panel__narration {
        grid-column: 1 / 9;
        max-width: 100%;
    }
    .panel__glyph {
        grid-column: 1 / 9;
        justify-self: end;
        margin-top: 32px;
    }
    .interstitial {
        height: 30vh;
    }
    .hud {
        font-size: 11px;
        padding: 0 10px;
    }
    .hud__ticker {
        gap: 6px;
    }
    .hud__signal {
        display: none;
    }
}

@media (max-width: 560px) {
    .hud {
        grid-template-columns: 1.4fr 1fr 1fr;
    }
    .hud__waveform {
        display: none;
    }
}
