/* ============================================
   undo.sh - Terminal Noir Design
   Colors from DESIGN.md:
     Deep Terminal Black: #0a0f0a
     Phosphor Green:      #33ff66
     Dim Phosphor:        #1a4a2a
     Ghost Text:          #0d1f0d
     Warm Amber:          #cc8833
     Paper White:         `#e8e8d8`.
     Scanline Gray:       #141f14
     Secondary dark:      #2a3a2a
   Fonts:
     "IBM Plex Mono" (Google Fonts) - Regular 400, Semi-Bold 600
     "Space Grotesk" (Google Fonts) - Bold 700, geometric sans-serif
   Typography:
     IBM Plex Mono Regular at body scale in Paper White
     The tagline should evoke the philosophical weight of undoing:
     something like "every action leaves a trace. every trace can be erased."
     After typing completes, the cursor resumes blinking.
     Reverse-scroll reveal: IntersectionObserver as each line enters the
     viewport. The stagger delay between lines is 40ms.
     Space Grotesk Bold at hero scale.
     Space Grotesk geometric sans to avoid the monoculture.
   ============================================ */

/* --- CSS Custom Properties --- */
:root {
    --bg-deep: #0a0f0a;
    --green: #33ff66;
    --dim-green: #1a4a2a;
    --ghost: #0d1f0d;
    --amber: #cc8833;
    --paper: #e8e8d8;
    --scanline: #141f14;
    --gutter: #2a3a2a;

    --font-mono: 'IBM Plex Mono', 'Courier New', monospace;
    --font-display: 'Space Grotesk', 'Arial', sans-serif;

    --ease-main: cubic-bezier(0.25, 0.46, 0.45, 0.94);
    --ease-crt: cubic-bezier(0.55, 0.085, 0.68, 0.53);

    --body-size: clamp(0.9rem, 1.8vw, 1.1rem);
    --prompt-size: clamp(1rem, 2.2vw, 1.35rem);
    --hero-size: clamp(3rem, 10vw, 7rem);
    --section-numeral-size: clamp(1.5rem, 3vw, 2.5rem);
}

/* --- Reset --- */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    background: var(--bg-deep);
    color: var(--paper);
    font-family: var(--font-mono);
    font-size: var(--body-size);
    font-weight: 400;
    line-height: 1.75;
    letter-spacing: 0.02em;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

/* --- CRT Container (barrel distortion) --- */
#crt-container {
    position: relative;
    min-height: 100vh;
    overflow: hidden;
    border-radius: 12px / 8px;
}

/* --- Scanline Overlay --- */
#scanline-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1000;
    background: repeating-linear-gradient(
        to bottom,
        transparent 0px,
        transparent 1px,
        rgba(20, 31, 20, 0.5) 1px,
        rgba(20, 31, 20, 0.5) 2px
    );
}

/* --- Screen Flicker --- */
@keyframes screen-flicker {
    0%, 96% { opacity: 1; }
    97% { opacity: 0.97; }
    98% { opacity: 1; }
    99% { opacity: 0.97; }
    100% { opacity: 1; }
}

#crt-container {
    animation: screen-flicker 12s infinite;
}

/* --- Blinking Cursor --- */
@keyframes cursor-blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

.cursor.blink {
    display: inline-block;
    color: var(--green);
    animation: cursor-blink 1.06s step-end infinite;
    text-shadow: 0 0 8px rgba(51, 255, 102, 0.25);
}

/* --- Line Number Gutter --- */
#line-gutter {
    position: fixed;
    left: 0;
    top: 0;
    width: 48px;
    height: 100vh;
    z-index: 50;
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--gutter);
    line-height: 1.75;
    padding-top: 1rem;
    overflow: hidden;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    padding-right: 8px;
}

.gutter-line {
    display: block;
    opacity: 0.6;
}

/* --- Main Terminal --- */
#terminal {
    max-width: calc(72ch + 48px);
    margin: 0 auto;
    padding-left: 56px;
    padding-right: 1rem;
    position: relative;
    z-index: 10;
}

/* --- Command Blocks --- */
.command-block {
    min-height: 100svh;
    display: flex;
    align-items: flex-start;
    padding-top: 20vh;
    padding-bottom: 10vh;
}

.block-content {
    width: 100%;
    max-width: 72ch;
}

.block-content-wide {
    max-width: none;
    width: 100%;
}

/* --- Prompt Lines --- */
.prompt-char {
    color: var(--green);
    font-weight: 600;
    font-size: var(--prompt-size);
}

.command-prompt {
    font-family: var(--font-mono);
    font-size: var(--prompt-size);
    font-weight: 600;
    color: var(--green);
    margin-bottom: 1.5rem;
}

.cmd-text {
    color: var(--green);
}

/* --- Loading Dots --- */
.loading-dots {
    font-size: var(--body-size);
    color: var(--dim-green);
    margin-bottom: 2rem;
}

.loading-dots .dot {
    opacity: 0;
    transition: opacity 0.3s var(--ease-main);
}

.loading-dots .dot.visible {
    opacity: 1;
}

/* --- Command Output --- */
.command-output {
    font-family: var(--font-mono);
    font-size: var(--body-size);
    font-weight: 400;
    line-height: 1.75;
    color: var(--paper);
}

/* --- Output Lines (reverse-scroll reveal) --- */
.output-line {
    transform: translateY(1.5rem);
    opacity: 0;
    transition: transform 0.4s var(--ease-main), opacity 0.4s var(--ease-main);
}

.output-line.revealed {
    transform: translateY(0);
    opacity: 1;
}

/* --- Text Color Classes --- */
.text-green {
    color: var(--green);
}

.text-dim {
    color: var(--dim-green);
}

.text-amber {
    color: var(--amber);
}

/* --- Hero Title (Block 1) --- */
#block-init {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-top: 0;
}

#init-cursor-area {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 100svh;
    font-size: 2rem;
}

#init-title-area {
    min-height: 80svh;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    padding-top: 20vh;
}

.hero-title {
    font-family: var(--font-display);
    font-size: var(--hero-size);
    font-weight: 700;
    letter-spacing: -0.04em;
    color: var(--green);
    line-height: 1.1;
    text-shadow: 0 0 20px rgba(51, 255, 102, 0.3), 0 0 60px rgba(51, 255, 102, 0.1);
    margin-bottom: 1.5rem;
}

.hero-title .cursor {
    font-size: 0.6em;
    vertical-align: baseline;
}

.hero-tagline {
    font-family: var(--font-mono);
    font-size: var(--body-size);
    font-weight: 400;
    color: var(--paper);
    line-height: 1.75;
    margin-bottom: 3rem;
    opacity: 0;
    transition: opacity 0.6s var(--ease-main);
}

.hero-tagline.visible {
    opacity: 1;
}

/* --- ASCII Dividers --- */
.ascii-divider {
    padding: 1rem 0;
    color: var(--dim-green);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    overflow: hidden;
    height: 2rem;
}

.divider-text {
    white-space: nowrap;
    display: block;
}

/* --- Ghost Gaps --- */
.ghost-gap {
    padding: 6rem 0 8rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.ghost-text {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    color: var(--ghost);
    opacity: 0.06;
    transition: opacity 0.4s var(--ease-main), color 0.4s var(--ease-main);
    cursor: default;
}

.ghost-text:hover {
    opacity: 0.5;
    color: var(--dim-green);
}

/* --- DAG Diagram --- */
.dag-container {
    width: 65%;
    max-width: 800px;
    margin: 2rem auto;
    aspect-ratio: 16 / 10;
}

#dag-svg {
    width: 100%;
    height: 100%;
}

.dag-path {
    stroke-dasharray: 1000;
    stroke-dashoffset: 1000;
    transition: stroke-dashoffset 0.8s var(--ease-main);
}

.dag-path.animated {
    stroke-dashoffset: 0;
}

.dag-node {
    opacity: 0;
    transition: opacity 0.3s var(--ease-main);
}

.dag-node.visible {
    opacity: 1;
}

@keyframes node-pulse {
    0%, 100% { filter: drop-shadow(0 0 0px rgba(51, 255, 102, 0)); }
    50% { filter: drop-shadow(0 0 6px rgba(51, 255, 102, 0.5)); }
}

.dag-node.pulse {
    animation: node-pulse 1.5s ease-in-out 1;
}

.dag-label {
    font-family: var(--font-mono);
    opacity: 0;
    transition: opacity 0.4s var(--ease-main);
}

.dag-label.visible {
    opacity: 1;
}

.dag-annotations {
    margin-top: 3rem;
    padding-left: 1rem;
}

.annotation-line {
    font-size: 0.9rem;
}

/* --- Error Block (Block 4 - Amber) --- */
.error-block {
    margin: 2rem 0;
    padding: 1.5rem;
    border-left: 2px solid var(--amber);
    background: rgba(204, 136, 51, 0.03);
}

.error-prompt {
    color: var(--paper) !important;
}

.error-prompt .prompt-char {
    color: var(--green);
}

.error-message {
    color: var(--amber) !important;
    font-weight: 600;
}

.error-detail {
    color: var(--amber) !important;
    opacity: 0.7;
}

/* --- Reflect Lines --- */
.reflect-line {
    max-width: 50ch;
}

/* --- Exit Lines --- */
.exit-line {
    color: var(--dim-green);
}

.exit-final {
    padding-top: 4rem;
    font-size: 1.5rem;
}

/* --- CRT Power-Off --- */
#crt-poweroff {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--bg-deep);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

#crt-dot {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--green);
    box-shadow: 0 0 10px var(--green), 0 0 30px rgba(51, 255, 102, 0.5);
    opacity: 1;
}

@keyframes crt-collapse-v {
    from { transform: scaleY(1); }
    to { transform: scaleY(0.002); }
}

@keyframes crt-collapse-h {
    from { transform: scaleY(0.002) scaleX(1); }
    to { transform: scaleY(0.002) scaleX(0); }
}

@keyframes crt-dot-fade {
    from { opacity: 1; }
    to { opacity: 0; }
}

#crt-poweroff.phase-1 {
    animation: crt-collapse-v 600ms var(--ease-crt) forwards;
}

#crt-poweroff.phase-2 {
    transform: scaleY(0.002);
    animation: crt-collapse-h 300ms var(--ease-crt) forwards;
}

#crt-poweroff.phase-3 {
    transform: scaleY(0.002) scaleX(0);
}

#crt-poweroff.phase-3 #crt-dot {
    animation: crt-dot-fade 2s var(--ease-main) forwards;
}

/* --- Command Palette --- */
#command-palette {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s var(--ease-main);
}

#command-palette.visible {
    opacity: 1;
    pointer-events: auto;
}

#command-palette.hidden {
    display: none;
}

.palette-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(10, 15, 10, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.palette-panel {
    position: relative;
    background: rgba(13, 31, 13, 0.95);
    border: 1px solid var(--dim-green);
    border-radius: 4px;
    padding: 2rem;
    width: 90%;
    max-width: 480px;
    transform: scale(0.95);
    transition: transform 0.2s var(--ease-main);
}

#command-palette.visible .palette-panel {
    transform: scale(1);
}

.palette-header {
    font-family: var(--font-mono);
    font-size: var(--prompt-size);
    font-weight: 600;
    color: var(--green);
    margin-bottom: 1.5rem;
}

.palette-commands {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.palette-cmd {
    font-family: var(--font-display);
    font-size: 1rem;
    font-weight: 400;
    color: var(--paper);
    text-decoration: none;
    padding: 0.5rem 0.75rem;
    border-radius: 2px;
    transition: background 0.15s var(--ease-main), color 0.15s var(--ease-main);
    display: block;
}

.palette-cmd:hover {
    background: var(--dim-green);
    color: var(--green);
}

.palette-cmd .prompt-char {
    font-size: 1rem;
    margin-right: 0.5rem;
}

.palette-hint {
    margin-top: 1.5rem;
    font-size: 0.75rem;
    color: var(--gutter);
}

.key-hint {
    display: inline-block;
    border: 1px solid var(--gutter);
    border-radius: 2px;
    padding: 0 0.35rem;
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--dim-green);
}

/* --- Mobile Palette Trigger --- */
#palette-trigger {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1500;
    background: transparent;
    border: 1px solid var(--dim-green);
    color: var(--green);
    font-family: var(--font-mono);
    font-size: 1rem;
    font-weight: 600;
    padding: 0.35rem 0.6rem;
    cursor: pointer;
    border-radius: 2px;
    transition: background 0.2s var(--ease-main);
}

#palette-trigger:hover {
    background: var(--dim-green);
}

/* --- Undo History Sidebar --- */
#undo-sidebar {
    position: fixed;
    right: 0;
    top: 0;
    width: 180px;
    height: 100vh;
    z-index: 40;
    overflow: hidden;
    padding: 1rem 0.75rem;
    display: none;
}

.sidebar-title {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--gutter);
    text-align: center;
    margin-bottom: 1rem;
    letter-spacing: 0.1em;
}

.sidebar-stack {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.sidebar-entry {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--ghost);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.15s var(--ease-main), transform 0.15s var(--ease-main);
}

@keyframes sidebar-pop {
    0% { color: var(--green); transform: scale(1); opacity: 1; }
    80% { color: var(--green); transform: scale(0.7); opacity: 0.5; }
    100% { color: var(--ghost); transform: scale(0); opacity: 0; }
}

.sidebar-entry.popping {
    animation: sidebar-pop 150ms var(--ease-main) forwards;
}

.sidebar-entry.flash {
    color: var(--green);
}

/* --- Utility Classes --- */
.hidden-initially {
    display: none;
}

.hidden-initially.show {
    display: block;
}

/* --- Responsive --- */
@media (min-width: 1200px) {
    #undo-sidebar {
        display: block;
    }

    #terminal {
        max-width: calc(72ch + 48px + 180px);
        margin-right: 180px;
    }
}

@media (max-width: 768px) {
    #terminal {
        padding-left: 16px;
        padding-right: 16px;
    }

    #line-gutter {
        display: none;
    }

    .dag-container {
        width: 95%;
    }

    .command-block {
        padding-top: 10vh;
    }

    .hero-title {
        font-size: clamp(2rem, 12vw, 4rem);
    }
}
