:root {
    --bg-color: #050505;
    --text-color: #e0e0e0;
    --accent-color: #8b0000;
    --font-main: 'Crimson Text', serif;
    --box-bg: rgba(20, 20, 20, 0.9);
    --box-border: rgba(139, 0, 0, 0.3);
    --current-bg: url('ghostly_wife.png');
}

@import url('https://fonts.googleapis.com/css2?family=Crimson+Text:ital,wght@0,400;0,700;1,400&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body, html {
    width: 100%;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto; /* Allow scrolling for puzzle */
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    scroll-behavior: smooth;
}

body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    width: 110%;
    height: 110%;
    margin: -5%;
    background-image: var(--current-bg);
    background-size: cover;
    background-position: center;
    opacity: 0.45;
    filter: grayscale(0.7) contrast(1.5) brightness(0.6) blur(1px);
    z-index: -1;
    pointer-events: none;
    transition: background-image 1.5s ease-in-out, filter 1s ease;
    animation: background-pulse 25s ease-in-out infinite alternate;
}

body.section-stories { --current-bg: url('ghostly_wife.png'); }
body.section-puzzle { --current-bg: url('cellar_empty.png'); }
body.section-cliches { --current-bg: url('basement_stairs.png'); }

body.glitch-active::before {
    animation: intense-flicker 0.2s infinite;
    filter: grayscale(1) contrast(3) brightness(1.2) invert(0.1);
    opacity: 0.5;
}

body.bg-update-flash::before {
    animation: bg-flash-anim 0.8s ease-out forwards;
}

@keyframes bg-flash-anim {
    0% { filter: grayscale(0) brightness(2) contrast(2) blur(10px); opacity: 0.9; }
    50% { filter: grayscale(0.3) brightness(1.4) contrast(1.6) blur(3px); opacity: 0.7; }
    100% { filter: grayscale(0.7) contrast(1.5) brightness(0.6) blur(1px); opacity: 0.45; }
}

#horror-glitch-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(139, 0, 0, 0.1);
    z-index: 1000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.1s;
}

body.glitch-active #horror-glitch-overlay {
    opacity: 1;
    animation: overlay-flash 0.1s infinite;
}

@keyframes overlay-flash {
    0% { background: rgba(139, 0, 0, 0.2); }
    50% { background: rgba(0, 0, 0, 0.4); }
    100% { background: rgba(139, 0, 0, 0.1); }
}

@keyframes intense-flicker {
    0% { transform: translate(2px, 2px) scale(1.1); }
    25% { transform: translate(-2px, -2px) scale(1.1); }
    50% { transform: translate(-3px, 3px) scale(1.1); }
    75% { transform: translate(3px, -3px) scale(1.1); }
    100% { transform: translate(0, 0) scale(1.1); }
}

body::after {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: repeating-linear-gradient(
        0deg,
        rgba(0, 0, 0, 0.05),
        rgba(0, 0, 0, 0.05) 1px,
        transparent 1px,
        transparent 2px
    );
    pointer-events: none;
    z-index: 150;
    opacity: 0.3;
}

@keyframes background-pulse {
    0% { transform: scale(1.0); opacity: 0.35; }
    100% { transform: scale(1.2); opacity: 0.55; }
}

@keyframes background-flicker {
    0%, 100% { filter: grayscale(1) contrast(1.5) brightness(0.6); }
    92% { filter: grayscale(1) contrast(1.5) brightness(0.6); }
    93% { filter: grayscale(1) contrast(1.8) brightness(0.8); opacity: 0.3; }
    94% { filter: grayscale(1) contrast(1.5) brightness(0.6); }
    96% { filter: grayscale(1) contrast(2.0) brightness(0.4); opacity: 0.1; }
    97% { filter: grayscale(1) contrast(1.5) brightness(0.6); }
}

#app {
    position: relative;
    width: 100%;
}

/* Global Nav Styles */
#global-nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 200;
    background: linear-gradient(to bottom, rgba(0,0,0,0.9) 0%, transparent 100%);
    padding: 1rem;
    display: flex;
    justify-content: center;
    transition: background 0.5s ease;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(5px);
    padding: 0.5rem 2rem;
    border-radius: 50px;
    border: 1px solid rgba(139, 0, 0, 0.2);
}

.nav-link {
    color: #777;
    text-decoration: none;
    font-size: 1.2rem;
    letter-spacing: 0.3rem;
    font-weight: bold;
    transition: all 0.3s ease;
    text-transform: uppercase;
    text-shadow: 0 0 5px rgba(0,0,0,0.8);
}

.nav-link:hover, .nav-link.active {
    color: var(--accent-color);
    text-shadow: 0 0 10px var(--accent-color);
}

.nav-divider {
    color: rgba(139, 0, 0, 0.4);
    font-size: 1.2rem;
}

@media (max-width: 600px) {
    .nav-links {
        gap: 0.8rem;
        padding: 0.5rem 1.2rem;
    }
    .nav-link {
        font-size: 0.9rem;
        letter-spacing: 0.15rem;
    }
    .nav-divider {
        font-size: 0.9rem;
    }
}

#experience-container {
    width: 100%;
    height: 100vh;
    position: relative;
    overflow: hidden; /* Slide logic requires container to be fixed height */
}

.slide {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 1.5s ease-in-out, visibility 1.5s;
    z-index: 1;
}

.slide.active {
    opacity: 1;
    visibility: visible;
    z-index: 2;
}

.background-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    transition: transform 10s linear, filter 2s ease;
    filter: contrast(1.15) brightness(0.85);
}

.slide.active .background-image {
    transform: scale(1.05);
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, transparent 20%, rgba(0,0,0,0.8) 100%);
}

.content {
    position: relative;
    z-index: 10;
    text-align: center;
    padding: 2rem;
    max-width: 80%;
}

h1 {
    font-size: 3rem;
    letter-spacing: 0.5rem;
    margin-bottom: 1rem;
    color: var(--accent-color);
    text-transform: uppercase;
}

.story-text {
    font-size: 1.5rem;
    line-height: 1.6;
    font-style: italic;
    text-shadow: 0 0 10px rgba(0,0,0,1);
    animation: flicker 4s infinite;
}

.story-image-container {
    width: 100%;
    max-width: 280px;
    margin: 0 auto 1.5rem;
    border: 1px solid rgba(139, 0, 0, 0.3);
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.8);
    position: relative;
    overflow: hidden;
    background: #000;
}

.story-image {
    width: 100%;
    height: auto;
    display: block;
    filter: brightness(0.7) contrast(1.2);
    transition: filter 0.5s ease;
}

.slide.active .story-image {
    filter: brightness(0.9) contrast(1.1);
}

.story-label {
    display: block;
    font-size: 1.8rem;
    letter-spacing: 0.5rem;
    color: #ff0000;
    margin-bottom: 2.5rem;
    font-weight: 900;
    text-transform: uppercase;
    text-shadow: 0 0 20px rgba(0,0,0,1), 0 0 10px rgba(139, 0, 0, 0.8);
}

.final {
    font-size: 2rem;
    color: #666;
}

.tap-hint {
    font-size: 0.9rem;
    letter-spacing: 0.2rem;
    opacity: 0.6;
    margin-top: 1rem;
    text-transform: uppercase;
}

.nav-trigger {
    margin: 2rem auto 0;
    width: 80px;
    height: 80px;
    cursor: pointer;
    transition: transform 0.3s ease;
    filter: drop-shadow(0 0 5px rgba(139, 0, 0, 0.5));
    animation: throb 3s infinite ease-in-out;
}

.nav-icon {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

@keyframes throb {
    0%, 100% { transform: scale(1); opacity: 0.7; }
    50% { transform: scale(1.1); opacity: 1; }
}

#restart-btn {
    margin-top: 2rem;
    background: transparent;
    border: 1px solid #444;
    color: #888;
    padding: 0.8rem 1.5rem;
    font-family: inherit;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.3s;
}

#vignette {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    background: radial-gradient(circle, transparent 50%, black 150%);
    z-index: 100;
}

.story-index {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    margin: 1.5rem auto;
    max-width: 280px;
}

.index-btn {
    background: rgba(139, 0, 0, 0.2);
    border: 1px solid rgba(139, 0, 0, 0.5);
    color: var(--text-color);
    padding: 8px;
    font-family: var(--font-main);
    font-size: 1.1rem;
    cursor: pointer;
}

/* Cliches Section Styles */
#cliches-section {
    position: relative;
    background-color: #0a0a0a;
    color: var(--text-color);
    padding: 8rem 1.5rem 4rem;
    overflow: hidden;
    z-index: 9;
    border-top: 1px solid rgba(139, 0, 0, 0.1);
}

.cliches-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('cellar_empty.png');
    background-size: cover;
    background-position: center;
    opacity: 0.1;
    filter: grayscale(1);
    pointer-events: none;
}

.cliches-content {
    position: relative;
    max-width: 1000px;
    margin: 0 auto;
    z-index: 2;
}

#cliches-section h1 {
    text-align: center;
    margin-bottom: 0.5rem;
    font-size: 4rem;
}

.guide-title {
    text-align: center;
    font-size: 1.5rem;
    color: #666;
    margin-bottom: 4rem;
    text-transform: uppercase;
    letter-spacing: 0.2rem;
}

.guide-stack {
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
    margin-bottom: 4rem;
}

.formula-steps-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 2rem;
}

.weaver-header {
    text-align: center;
    margin-bottom: 2.5rem;
}

.weaver-header p {
    color: #888;
    font-style: italic;
}

.weaver-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
}

@media (min-width: 900px) {
    .weaver-grid {
        grid-template-columns: 1fr 1fr 1fr;
    }
}

.grid-list {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.5rem;
}

@media (min-width: 600px) and (max-width: 899px) {
    .grid-list {
        grid-template-columns: 1fr 1fr;
    }
}

.guide-card {
    background: rgba(15, 15, 15, 0.95);
    border: 1px solid rgba(139, 0, 0, 0.3);
    padding: 3rem;
    border-radius: 8px;
    box-shadow: 0 15px 50px rgba(0,0,0,0.8);
    transition: transform 0.3s ease, border-color 0.3s ease, box-shadow 0.3s;
}

.guide-card:hover {
    box-shadow: 0 15px 60px rgba(139, 0, 0, 0.2);
}

.weaver-card {
    border-color: rgba(139, 0, 0, 0.5);
    background: linear-gradient(180deg, rgba(15, 15, 15, 0.95) 0%, rgba(5, 5, 5, 1) 100%);
}

.guide-card:hover {
    transform: translateY(-5px);
    border-color: rgba(139, 0, 0, 0.5);
}

.guide-card h3 {
    color: var(--accent-color);
    font-size: 1.8rem;
    margin-bottom: 2rem;
    border-bottom: 1px solid rgba(139, 0, 0, 0.3);
    padding-bottom: 0.5rem;
}

.formula-step {
    margin-bottom: 1.5rem;
}

.step-label {
    display: block;
    color: #fff;
    font-weight: bold;
    margin-bottom: 0.4rem;
    font-size: 1.1rem;
}

.formula-step p {
    font-size: 1rem;
    line-height: 1.5;
    color: #aaa;
}

.cliche-group {
    margin-bottom: 2rem;
}

.cliche-group h4 {
    color: #888;
    text-transform: uppercase;
    font-size: 0.9rem;
    letter-spacing: 0.1rem;
    margin-bottom: 0.8rem;
}

.cliche-group ul {
    list-style: none;
    padding-left: 0.5rem;
}

.cliche-group li {
    position: relative;
    padding-left: 1.5rem;
    margin-bottom: 0.4rem;
    color: #ddd;
    font-size: 1rem;
}

.cliche-group li::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 10px;
    height: 10px;
    background-image: url('skeletal_hand_nav.png');
    background-size: contain;
    background-repeat: no-repeat;
    opacity: 0.3;
    transition: opacity 0.3s;
}

.selectable {
    cursor: pointer;
    transition: all 0.3s ease;
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 0.2rem 0.5rem 0.2rem 1.5rem !important;
}

.selectable:hover {
    background: rgba(139, 0, 0, 0.1);
    color: #fff;
}

.selectable.selected {
    background: rgba(139, 0, 0, 0.3);
    border-color: var(--accent-color);
    color: #fff;
    font-weight: bold;
}

.selectable.selected::before {
    opacity: 1;
    filter: drop-shadow(0 0 5px var(--accent-color));
}

.generator-controls {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid rgba(139, 0, 0, 0.2);
    text-align: center;
}

#generate-story-btn {
    background: #222;
    color: #666;
    border: 1px solid #444;
    padding: 0.8rem 1.5rem;
    font-family: inherit;
    font-size: 1rem;
    cursor: not-allowed;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    transition: all 0.3s;
}

#generate-story-btn.ready {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
    cursor: pointer;
    box-shadow: 0 0 15px rgba(139, 0, 0, 0.4);
}

#generate-story-btn.ready:hover {
    background: #b00;
    box-shadow: 0 0 25px rgba(139, 0, 0, 0.6);
}

#story-generator-output {
    margin-top: 2rem;
    min-height: 100px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
}

.nightmare-result {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    width: 100%;
}

.generated-text {
    font-style: italic;
    font-size: 1.2rem;
    line-height: 1.6;
    color: #fff;
    text-align: center;
    max-width: 90%;
    text-shadow: 0 0 10px rgba(139, 0, 0, 0.5);
    animation: fadeIn 1s ease;
}

.generated-image-container {
    width: 100%;
    max-width: 400px;
    border: 1px solid var(--accent-color);
    box-shadow: 0 0 30px rgba(139, 0, 0, 0.3);
    animation: fadeIn 2s ease;
    overflow: hidden;
    background: #000;
}

.generated-image {
    width: 100%;
    height: auto;
    display: block;
    filter: sepia(0.2) contrast(1.1) brightness(0.9);
}

#ritual-progress-container {
    width: 100%;
    max-width: 300px;
    margin: 1.5rem auto;
}

#ritual-progress-bar {
    width: 0%;
    height: 4px;
    background: var(--accent-color);
    box-shadow: 0 0 10px var(--accent-color);
    transition: width 1s linear;
}

.ritual-status {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.2rem;
    color: #666;
    margin-top: 0.5rem;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(139, 0, 0, 0.2);
    border-top: 3px solid var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.hidden {
    display: none;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 800px) {
    .guide-grid {
        grid-template-columns: 1fr;
    }
}

/* Section Divider */
.section-divider {
    height: 30vh;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 2rem;
    background: linear-gradient(to bottom, #050505, #080808);
    position: relative;
    z-index: 10;
    padding: 0 2rem;
}

.divider-line {
    flex-grow: 1;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--accent-color), transparent);
    opacity: 0.5;
}

.divider-icon {
    width: 60px;
    height: 60px;
    opacity: 0.4;
    transform: rotate(90deg);
}

.divider-icon img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Puzzle Styles */
#puzzle-section {
    padding: 6rem 1.5rem;
    background-color: #080808;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    z-index: 10;
    border-top: 1px solid rgba(139, 0, 0, 0.1);
}

.puzzle-header {
    text-align: center;
    margin-bottom: 3rem;
    max-width: 600px;
}

.puzzle-header h2 {
    color: var(--accent-color);
    font-size: 2.5rem;
    letter-spacing: 0.3rem;
    margin-bottom: 1rem;
}

.puzzle-header p {
    font-style: italic;
    opacity: 0.8;
}

.puzzle-grid {
    width: 100%;
    max-width: 1200px;
}

.column-labels {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 10px;
    margin-bottom: 1rem;
    text-align: center;
    font-weight: bold;
    letter-spacing: 0.1rem;
    color: #666;
}

.columns-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 15px;
}

.puzzle-column {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.puzzle-box {
    background: var(--box-bg);
    border: 1px solid var(--box-border);
    padding: 15px;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 0.95rem;
    border-radius: 4px;
    user-select: none;
    cursor: grab;
    transition: border-color 0.3s, background 0.3s, transform 0.2s;
    line-height: 1.4;
}

.puzzle-box:active {
    cursor: grabbing;
}

.static-column .puzzle-box {
    cursor: default;
    background: rgba(40, 0, 0, 0.2);
    border-color: rgba(139, 0, 0, 0.5);
    color: #fff;
    font-weight: bold;
}

.sortable-ghost {
    opacity: 0.4;
    background: var(--accent-color);
}

.sortable-drag {
    opacity: 1;
    transform: scale(1.05);
    box-shadow: 0 10px 20px rgba(0,0,0,0.5);
    background: #222 !important;
}

.puzzle-controls {
    margin-top: 3rem;
    text-align: center;
}

#check-puzzle-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 1rem 2.5rem;
    font-size: 1.2rem;
    font-family: inherit;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 0.2rem;
    transition: background 0.3s;
}

#check-puzzle-btn:hover {
    background: #b00;
}

#puzzle-feedback {
    margin-top: 1.5rem;
    font-size: 1.2rem;
    height: 1.5rem;
}

.correct-match {
    border-color: #00ff00 !important;
    background: rgba(0, 255, 0, 0.25) !important;
    box-shadow: 0 0 15px rgba(0, 255, 0, 0.3), inset 0 0 10px rgba(0, 255, 0, 0.2);
    color: #fff;
    text-shadow: 0 0 5px #00ff00;
}

.incorrect-match {
    border-color: #ff0000 !important;
    background: rgba(255, 0, 0, 0.25) !important;
    box-shadow: 0 0 15px rgba(255, 0, 0, 0.3), inset 0 0 10px rgba(255, 0, 0, 0.2);
}

.story-complete {
    background: rgba(0, 255, 0, 0.15) !important;
    border-color: rgba(0, 255, 0, 0.6) !important;
    color: #fff !important;
    text-shadow: 0 0 10px #00ff00;
}

#puzzle-feedback .success-text {
    color: #00ff00;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    text-shadow: 0 0 5px rgba(0, 255, 0, 0.5);
}

#puzzle-feedback .error-text {
    color: #ff4444;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    text-shadow: 0 0 5px rgba(255, 0, 0, 0.5);
}

.shake {
    animation: puzzle-shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes puzzle-shake {
    10%, 90% { transform: translate3d(-1px, 0, 0); }
    20%, 80% { transform: translate3d(2px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
    40%, 60% { transform: translate3d(4px, 0, 0); }
}

@media (max-width: 768px) {
    .columns-container, .column-labels {
        grid-template-columns: 1fr;
    }
    
    .label {
        display: none;
    }
    
    .puzzle-column {
        margin-bottom: 2rem;
    }
    
    .puzzle-column::before {
        content: attr(data-label);
        display: block;
        text-align: center;
        margin-bottom: 10px;
        color: var(--accent-color);
        font-weight: bold;
        text-transform: uppercase;
    }
}

/* Glitch and flicker animations preserved from original */
@keyframes flicker {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.8; }
    52% { opacity: 0.9; }
    54% { opacity: 0.7; }
}

.glitch { position: relative; }
.glitch::before, .glitch::after {
    content: attr(data-text);
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
}
.glitch::before {
    left: 2px; text-shadow: -2px 0 #ff00c1;
    clip: rect(44px, 450px, 56px, 0);
    animation: glitch-anim 5s infinite linear alternate-reverse;
}
.glitch::after {
    left: -2px; text-shadow: -2px 0 #00fff9, 2px 2px #ff00c1;
    animation: glitch-anim2 1s infinite linear alternate-reverse;
}

@keyframes glitch-anim {
    0% { clip: rect(31px, 9999px, 94px, 0); transform: skew(0.85deg); }
    10% { clip: rect(63px, 9999px, 33px, 0); transform: skew(0.18deg); }
    100% { clip: rect(67px, 9999px, 62px, 0); transform: skew(0.04deg); }
}
@keyframes glitch-anim2 {
    0% { clip: rect(65px, 9999px, 100px, 0); transform: skew(0.13deg); }
    100% { clip: rect(79px, 9999px, 13px, 0); transform: skew(0.97deg); }
}