/* ===================================
   Food System Infographic - Scrollytelling
   Mobile-first, accessible design
   =================================== */

/* Reset and Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Current System Colors - Muted, professional */
    --color-policy: #2c5282;
    --color-beneficiary: #6b46c1;
    --color-production: #b7791f;
    --color-health: #c53030;
    --color-environment: #c24a1a;
    --color-economic: #c53030;
    --color-fragility: #9b2c2c;
    --color-collapse: #742a2a;

    /* Alternative System Colors - Softer greens/teals */
    --color-alt-primary: #319795;
    --color-alt-positive: #2f855a;
    --color-alt-hope: #3182ce;
    --color-alt-beneficiary: #48bb78;
    --color-alt-production: #38a169;

    /* Neutrals */
    --color-text: #1a1a2e;
    --color-text-secondary: #475569;
    --color-text-muted: #64748b;
    --color-text-subtle: #94a3b8;
    --color-bg: #ffffff;
    --color-bg-subtle: #f8fafc;
    --color-border: #e2e8f0;

    /* Timing */
    --transition-fast: 200ms;
    --transition-normal: 300ms;
    --transition-slow: 500ms;
    --transition-morph: 800ms;

    /* Panel Transition */
    --panel-transition: 500ms;
    --panel-slide: 100px;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    font-size: 16px;
}

/* Skip Link for Accessibility */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--color-policy);
    color: white;
    padding: 8px 16px;
    z-index: 1000;
    text-decoration: none;
    font-weight: 500;
}

.skip-link:focus {
    top: 0;
}

/* ===================================
   Hero Section
   =================================== */
.hero {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: linear-gradient(180deg, var(--color-bg) 0%, var(--color-bg-subtle) 100%);
}

.hero-content {
    text-align: center;
    max-width: 720px;
}

.hero h1 {
    font-size: clamp(1.75rem, 5vw, 3rem);
    font-weight: 700;
    color: var(--color-text);
    line-height: 1.2;
    margin-bottom: 20px;
    letter-spacing: -0.02em;
}

.hero-tagline {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    color: var(--color-text-secondary);
    margin-bottom: 12px;
}

.hero-subtext {
    font-size: 0.95rem;
    color: var(--color-text-muted);
    margin-bottom: 40px;
}

/* Scroll Indicator */
.scroll-indicator {
    display: flex;
    justify-content: center;
}

.scroll-arrow {
    display: block;
    width: 24px;
    height: 24px;
    border-right: 2px solid var(--color-text-subtle);
    border-bottom: 2px solid var(--color-text-subtle);
    transform: rotate(45deg);
    animation: bounce 2s infinite;
}

@keyframes bounce {
    0%, 20%, 50%, 80%, 100% { transform: translateY(0) rotate(45deg); }
    40% { transform: translateY(8px) rotate(45deg); }
    60% { transform: translateY(4px) rotate(45deg); }
}

/* ===================================
   Scrollytelling Layout
   =================================== */
.scrollytelling {
    position: relative;
    display: flex;
    flex-direction: column;
}

/* Sticky Visualization Container */
.sticky-container {
    position: sticky;
    top: 0;
    height: 100vh;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-bg);
    z-index: 1;
    margin-bottom: -100vh; /* Pull scroll sections up to overlap */
}

.visualization-wrapper {
    position: relative;
    width: 100%;
    max-width: 600px;
    padding: 16px;
}

#visualization {
    width: 100%;
    height: auto;
    max-height: 60vh;
}

/* System Toggle */
.system-toggle {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-normal) ease, visibility var(--transition-normal) ease;
}

.system-toggle.visible {
    opacity: 1;
    visibility: visible;
}

.toggle-container {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--color-bg);
    padding: 8px 16px;
    border-radius: 40px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.toggle-label {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--color-text-subtle);
    transition: color var(--transition-normal) ease;
    white-space: nowrap;
}

.toggle-label.active {
    color: var(--color-text);
}

.current-label.active {
    color: var(--color-collapse);
}

.alt-label.active {
    color: var(--color-alt-positive);
}

/* Toggle Switch */
.switch {
    position: relative;
    width: 52px;
    height: 28px;
    flex-shrink: 0;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: linear-gradient(135deg, #ef4444, #f97316);
    transition: var(--transition-normal) ease;
    border-radius: 28px;
    box-shadow: 0 2px 6px rgba(239, 68, 68, 0.3);
}

.slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background: white;
    transition: var(--transition-normal) ease;
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

input:checked + .slider {
    background: linear-gradient(135deg, #22c55e, #16a34a);
    box-shadow: 0 2px 6px rgba(34, 197, 94, 0.3);
}

input:checked + .slider:before {
    transform: translateX(24px);
}

input:focus + .slider {
    outline: 2px solid var(--color-policy);
    outline-offset: 2px;
}

/* Legend */
.legend {
    display: flex;
    justify-content: center;
    gap: 16px;
    flex-wrap: wrap;
    padding: 12px 0;
    opacity: 0;
    transition: opacity var(--transition-normal) ease;
}

.legend.visible {
    opacity: 1;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.legend-dot.policy { background: var(--color-policy); }
.legend-dot.beneficiary { background: var(--color-beneficiary); }
.legend-dot.production { background: var(--color-production); }
.legend-dot.externality { background: var(--color-health); }
.legend-dot.outcome { background: var(--color-collapse); }

/* Legend Colors - Alternative System
   NOTE: .alt class is toggled on .visualization-wrapper (shared ancestor of SVG and legend).
   See toggleSystem() in script.js. */
.alt .legend-dot.beneficiary { background: var(--color-alt-beneficiary); }
.alt .legend-dot.production { background: var(--color-alt-production); }
.alt .legend-dot.externality { background: #0ea5e9; }
.alt .legend-dot.outcome { background: var(--color-alt-positive); }

/* ===================================
   Scroll Sections
   =================================== */
.scroll-sections {
    position: relative;
    z-index: 2;
    pointer-events: none;  /* Allow mouse events to pass through to SVG */
}

.scroll-section {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 24px;
    padding-right: 40px;
}

.section-content {
    background: white;
    padding: 24px 28px;
    border-radius: 16px;
    max-width: 360px;
    width: 100%;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
    border: 1px solid var(--color-border);
    opacity: 0;
    transform: translateY(20px);
    transition: opacity var(--transition-slow) ease, transform var(--transition-slow) ease;
    pointer-events: auto;  /* Restore interactivity for content cards */
}

.scroll-section.is-active .section-content {
    opacity: 1;
    transform: translateY(0);
}

.scroll-section.was-active .section-content {
    opacity: 0.15;
    transform: translateY(-10px);
}

.section-number {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--color-text-subtle);
    letter-spacing: 0.1em;
    margin-bottom: 8px;
}

.section-content h2 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 10px;
    line-height: 1.2;
}

.section-stat {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--color-policy);
    margin-bottom: 18px;
}

.section-content p {
    font-size: 1rem;
    color: var(--color-text-secondary);
    margin-bottom: 14px;
    line-height: 1.6;
}

.section-content p:last-child {
    margin-bottom: 0;
}

.section-detail {
    font-size: 0.95rem;
    color: var(--color-text-muted);
    line-height: 1.5;
}

.section-cta {
    font-size: 1rem;
    color: var(--color-alt-primary);
}

/* ===================================
   Dual Scroll Sections Layout
   =================================== */
.scroll-sections-wrapper {
    position: relative;
    z-index: 2;
    pointer-events: none;  /* Allow mouse events to pass through to SVG */
}

.scroll-sections-current {
    /* Existing right-side positioning - keep current styles */
}

.scroll-sections-alt {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    width: 45%;
    max-width: 420px;
    margin-right: auto;  /* Force left alignment */
    margin-left: 0;      /* Override inherited margin-left: auto */
    padding-left: 40px;
    visibility: hidden;
    pointer-events: none;
}

.scroll-sections-alt .scroll-section {
    justify-content: flex-start;
}

/* Transition to Alternative */
.scroll-sections-wrapper[data-transition="to-alt"] .scroll-sections-current {
    animation: slideOutRight var(--panel-transition) ease-out forwards;
}
.scroll-sections-wrapper[data-transition="to-alt"] .scroll-sections-alt {
    visibility: visible;
    pointer-events: auto;
    animation: slideInLeft var(--panel-transition) ease-out 150ms forwards;
}

/* Transition to Current */
.scroll-sections-wrapper[data-transition="to-current"] .scroll-sections-alt {
    animation: slideOutLeft var(--panel-transition) ease-out forwards;
}
.scroll-sections-wrapper[data-transition="to-current"] .scroll-sections-current {
    animation: slideInRight var(--panel-transition) ease-out 150ms forwards;
}

/* Active states */
.scroll-sections-wrapper[data-active-system="alt"] .scroll-sections-current {
    visibility: hidden;
    pointer-events: none;
    position: absolute;  /* Remove from document flow so alt sections start at top */
}
.scroll-sections-wrapper[data-active-system="alt"] .scroll-sections-alt {
    position: relative;
    visibility: visible;
    pointer-events: auto;
    margin-left: 0;       /* Force left alignment */
    margin-right: auto;   /* Force left alignment */
}

/* Keyframes for panel transitions */
@keyframes slideOutRight {
    to { opacity: 0; transform: translateX(var(--panel-slide)); }
}
@keyframes slideInRight {
    from { opacity: 0; transform: translateX(var(--panel-slide)); }
    to { opacity: 1; transform: translateX(0); }
}
@keyframes slideOutLeft {
    to { opacity: 0; transform: translateX(calc(var(--panel-slide) * -1)); }
}
@keyframes slideInLeft {
    from { opacity: 0; transform: translateX(calc(var(--panel-slide) * -1)); }
    to { opacity: 1; transform: translateX(0); }
}

/* Externality Stats Grid */
.externality-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    margin-bottom: 16px;
}

.ext-stat {
    text-align: center;
    padding: 12px 8px;
    background: var(--color-bg-subtle);
    border-radius: 8px;
}

.ext-label {
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 4px;
}

.ext-value {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text);
}

/* ===================================
   SVG Visualization Styles
   =================================== */
.orbital-ring {
    fill: none;
    stroke: var(--color-border);
    stroke-width: 1;
    stroke-dasharray: 4 4;
    opacity: 0.5;
    transition: opacity var(--transition-normal) ease;
}

svg.alt .orbital-ring {
    opacity: 0 !important;
}

.node-group {
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-normal) ease;
}

.node-group.visible {
    opacity: 1;
    pointer-events: auto;
}

.node-group.dimmed {
    opacity: 0.15;
}

.node-circle {
    transition: all var(--transition-normal) ease;
    filter: drop-shadow(0 3px 6px rgba(0, 0, 0, 0.1));
}

.node-group:hover .node-circle,
.node-group:focus .node-circle {
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.15));
    transform: scale(1.05);
}

.node-circle.highlighted {
    filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.2));
}

.node-label {
    font-family: 'Inter', sans-serif;
    font-size: 46px;
    font-weight: 700;
    fill: white;
    text-anchor: middle;
    pointer-events: none;
}

.node-sublabel {
    font-family: 'Inter', sans-serif;
    font-size: 40px;
    font-weight: 500;
    fill: rgba(255, 255, 255, 0.95);
    text-anchor: middle;
    pointer-events: none;
}

.node-stat {
    font-family: 'Inter', sans-serif;
    font-size: 24px;
    font-weight: 600;
    fill: rgba(255, 255, 255, 0.9);
    text-anchor: middle;
    pointer-events: none;
}

/* Node Colors - Current System */
.node-policy { fill: var(--color-policy); }
.node-beneficiary { fill: var(--color-beneficiary); }
.node-production { fill: var(--color-production); }
.node-externality-health { fill: var(--color-health); }
.node-externality-env { fill: var(--color-environment); }
.node-externality-econ { fill: var(--color-economic); }
.node-fragility { fill: var(--color-fragility); }
.node-collapse { fill: var(--color-collapse); }

/* Node Colors - Alternative System */
.alt .node-policy { fill: var(--color-policy); }
.alt .node-beneficiary { fill: var(--color-alt-beneficiary); }
.alt .node-production { fill: var(--color-alt-production); }
.alt .node-externality-health { fill: #0ea5e9; }
.alt .node-externality-env { fill: var(--color-alt-hope); }
.alt .node-externality-econ { fill: #0ea5e9; }
.alt .node-fragility { fill: var(--color-alt-primary); }
.alt .node-collapse { fill: var(--color-alt-positive); }

/* Connection Lines */
.connection {
    fill: none;
    stroke: none;
    stroke-width: 2;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-normal) ease, stroke var(--transition-normal) ease;
}

.connection.visible {
    stroke: var(--color-border);
    opacity: 0.6;
    visibility: visible;
}

.connection.highlighted {
    stroke: var(--color-policy);
    stroke-width: 2.5;
    opacity: 1;
}

/* Connection draw animation */
.connection.drawing {
    stroke-dasharray: 1000;
    stroke-dashoffset: 1000;
    animation: drawLine var(--transition-slow) ease forwards;
}

@keyframes drawLine {
    to { stroke-dashoffset: 0; }
}

/* Center node subtle pulse */
.center-node .node-circle {
    animation: subtlePulse 4s ease-in-out infinite;
}

@keyframes subtlePulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.02); }
}

/* ===================================
   Detail Panel
   =================================== */
.detail-panel {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.95);
    background: white;
    border-radius: 16px;
    padding: 28px;
    max-width: 420px;
    width: calc(100% - 32px);
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition-normal) ease;
    z-index: 1000;
}

.detail-panel.active {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, -50%) scale(1);
}

.close-btn {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 36px;
    height: 36px;
    border: none;
    background: var(--color-bg-subtle);
    border-radius: 50%;
    font-size: 1.3rem;
    cursor: pointer;
    color: var(--color-text-muted);
    transition: all var(--transition-fast) ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.close-btn:hover,
.close-btn:focus {
    background: var(--color-border);
    color: var(--color-text);
}

.close-btn:focus {
    outline: 2px solid var(--color-policy);
    outline-offset: 2px;
}

#detail-title {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 16px;
    padding-right: 40px;
}

#detail-content {
    font-size: 0.95rem;
    color: var(--color-text-secondary);
}

#detail-content p {
    margin-bottom: 12px;
}

#detail-content ul {
    margin: 12px 0;
    padding-left: 20px;
}

#detail-content li {
    margin-bottom: 6px;
}

#detail-content strong {
    color: var(--color-text);
}

#detail-content .stat-highlight {
    background: var(--color-bg-subtle);
    padding: 12px 16px;
    border-radius: 8px;
    border-left: 4px solid var(--color-policy);
    margin: 16px 0;
    font-weight: 500;
}

#detail-content .source {
    font-size: 0.85rem;
    color: var(--color-text-subtle);
    font-style: italic;
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--color-border);
}

/* ===================================
   Call to Action Section
   =================================== */
.cta-section {
    background: linear-gradient(180deg, var(--color-bg) 0%, var(--color-bg-subtle) 100%);
    padding: 60px 24px;
    text-align: center;
}

.cta-content {
    max-width: 600px;
    margin: 0 auto;
}

.cta-section h2 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 32px;
}

.cta-tier {
    margin-bottom: 28px;
    padding: 24px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
    border: 1px solid var(--color-border);
}

.cta-tier:last-child {
    margin-bottom: 0;
}

.cta-tier h3 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--color-alt-positive);
    margin-bottom: 8px;
}

.cta-tier p {
    font-size: 0.95rem;
    color: var(--color-text-secondary);
    margin-bottom: 12px;
    line-height: 1.5;
}

.cta-tier p:last-child {
    margin-bottom: 0;
}

.cta-links {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
}

.cta-links a {
    display: inline-block;
    padding: 10px 20px;
    background: var(--color-alt-positive);
    color: white;
    text-decoration: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    transition: background var(--transition-fast) ease, transform var(--transition-fast) ease;
}

.cta-links a:hover,
.cta-links a:focus {
    background: var(--color-alt-primary);
    transform: translateY(-1px);
}

/* ===================================
   Footer
   =================================== */
footer {
    background: var(--color-bg-subtle);
    padding: 40px 24px;
    text-align: center;
    border-top: 1px solid var(--color-border);
}

.footer-content {
    max-width: 600px;
    margin: 0 auto;
}

.footer-note {
    font-size: 0.8rem;
    color: var(--color-text-subtle);
    margin-bottom: 8px;
}

.footer-cta {
    font-size: 0.9rem;
    color: var(--color-text-muted);
}

/* ===================================
   Mobile: increase spacing between scroll sections
   =================================== */
@media (max-width: 767px) {
    .scroll-section {
        min-height: 160vh;
    }

    .scroll-sections-alt {
        width: 100%;
        padding-left: 0;
        padding-right: 0;
    }
}

/* ===================================
   Desktop Enhancements (768px+)
   =================================== */
@media (min-width: 768px) {
    .visualization-wrapper {
        max-width: 800px;
        padding: 24px;
    }

    #visualization {
        max-height: 70vh;
    }

    .scroll-sections {
        margin-left: auto;
        width: 45%;
        max-width: 420px;
        padding-right: 40px;
    }

    .scroll-sections-current {
        margin-left: auto;
    }

    .scroll-sections-alt {
        margin-left: 0 !important;  /* Override base .scroll-sections margin-left: auto */
        margin-right: auto;
        padding-left: 40px;
        padding-right: 0;
    }

    .scroll-section {
        padding: 40px 24px;
    }

    .section-content {
        padding: 28px;
        max-width: none;
    }

    .section-content h2 {
        font-size: 1.75rem;
    }

    .toggle-container {
        padding: 10px 20px;
    }

    .toggle-label {
        font-size: 0.9rem;
    }

    .legend-item {
        font-size: 0.8rem;
    }

    .legend-dot {
        width: 12px;
        height: 12px;
    }
}

/* Large Desktop (1024px+) */
@media (min-width: 1024px) {
    .visualization-wrapper {
        max-width: 900px;
    }

    .scroll-sections {
        width: 40%;
        max-width: 450px;
    }

    .scroll-sections-alt {
        width: 40%;
        max-width: 450px;
    }
}

/* ===================================
   Accessibility: Reduced Motion
   =================================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    html {
        scroll-behavior: auto;
    }

    .scroll-arrow {
        animation: none;
    }

    .center-node .node-circle {
        animation: none;
    }

    .connection.drawing {
        animation: none;
        stroke-dasharray: none;
        stroke-dashoffset: 0;
    }

    /* Panel transitions - instant for reduced motion */
    .scroll-sections-wrapper[data-transition="to-alt"] .scroll-sections-current,
    .scroll-sections-wrapper[data-transition="to-alt"] .scroll-sections-alt,
    .scroll-sections-wrapper[data-transition="to-current"] .scroll-sections-alt,
    .scroll-sections-wrapper[data-transition="to-current"] .scroll-sections-current {
        animation: none;
    }
}

/* ===================================
   Focus Styles for Keyboard Navigation
   =================================== */
.node-group:focus {
    outline: none;
}

.node-group:focus .node-circle {
    stroke: var(--color-policy);
    stroke-width: 3;
}

button:focus-visible,
input:focus-visible,
a:focus-visible {
    outline: 2px solid var(--color-policy);
    outline-offset: 2px;
}

/* ===================================
   Morph Transition (System Toggle)
   =================================== */
.morphing .node-circle {
    transition: fill var(--transition-morph) ease, transform var(--transition-morph) ease;
}

.morphing .node-label,
.morphing .node-sublabel,
.morphing .node-stat {
    transition: opacity var(--transition-slow) ease;
}

.morphing .connection {
    transition: stroke var(--transition-morph) ease, opacity var(--transition-normal) ease;
}

/* ===================================
   Print Styles
   =================================== */
@media print {
    .hero {
        min-height: auto;
        page-break-after: always;
    }

    .sticky-container {
        position: relative;
        height: auto;
    }

    .scroll-sections {
        display: none;
    }

    .system-toggle {
        display: none;
    }
}
