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

:root {
    --bg: #faf9f7;
    --text: #2d2d2d;
    --text-light: #9d8189;
    --accent: #f4acb7;
    --accent-dark: #9d8189;
    --border: #d8e2dc;
    --code-bg: #1e1e1e;
    --highlight-bg: #ffe5d9;
    --note-bg: #d8e2dc;
    --shadow-sm: 0 2px 8px rgba(157, 129, 137, 0.08);
    --shadow-md: 0 4px 16px rgba(157, 129, 137, 0.12);
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    background-image:
        radial-gradient(circle at 20% 50%, rgba(255, 229, 217, 0.3) 0%, transparent 50%),
        radial-gradient(circle at 80% 80%, rgba(216, 226, 220, 0.4) 0%, transparent 50%);
    background-attachment: fixed;
}

/* Header */
header {
    padding: 3rem 0 2rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 2rem;
    background: rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(10px);
}

.header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 2rem;
}

.back-link {
    color: var(--text-light);
    text-decoration: none;
    font-size: 0.9rem;
    display: inline-block;
    margin-bottom: 1rem;
    transition: color 0.2s;
}

.back-link:hover {
    color: var(--accent);
}

header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    background: linear-gradient(135deg, var(--text), var(--accent-dark));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-light);
    font-size: 1rem;
}

/* Navigation */
.toc {
    position: sticky;
    top: 0;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    padding: 1rem 0;
    margin-bottom: 3rem;
    z-index: 100;
    display: flex;
    justify-content: center;
    gap: 2rem;
    flex-wrap: wrap;
    box-shadow: var(--shadow-sm);
}

.toc a {
    color: var(--text-light);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.3s ease;
    position: relative;
    padding: 0.5rem 1rem;
    border-radius: 20px;
}

.toc a:hover {
    color: var(--accent);
    background: rgba(244, 172, 183, 0.1);
    transform: translateY(-2px);
}

.toc a:after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 2px;
    background: var(--accent);
    transition: all 0.3s ease;
    transform: translateX(-50%);
}

.toc a:hover:after {
    width: 80%;
}

/* Main Content */
main {
    max-width: 90%;
    margin: 0 auto;
    padding: 0 2rem 4rem;
}

section {
    margin-bottom: 5rem;
    animation: fadeInUp 0.6s ease-out;
}

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

h2 {
    font-size: 1.8rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    padding-top: 2rem;
    position: relative;
    display: inline-block;
}

h2:after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 0;
    width: 60px;
    height: 3px;
    background: linear-gradient(90deg, var(--accent), transparent);
    border-radius: 2px;
}

h3 {
    font-size: 1.3rem;
    font-weight: 600;
    margin: 3rem 0 1.5rem;
    padding-top: 1rem;
}

/* Add separator for non-first h3 elements */
section > h3:not(:first-of-type) {
    border-top: 1px solid var(--border);
    padding-top: 2rem;
}

/* First h3 in section needs less top margin */
section > p + h3:first-of-type,
section > .note-box + h3 {
    margin-top: 2rem;
}

p {
    margin-bottom: 1rem;
    color: var(--text-light);
}

/* Code Blocks */
.code-section {
    margin: 2rem 0;
}

.code-comparison {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin: 2rem auto;
    width: 100%;
    max-width: 100%;
}

.code-comparison.three-col {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1rem;
}

.code-block {
    margin-bottom: 1.5rem;
    min-width: 0;
    overflow: hidden;
}

.code-block.full-width {
    width: 100%;
}

.code-block h4 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text);
}

/* Collapsible code blocks */
.code-block.collapsible {
    border: 1px solid var(--border);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
    box-shadow: var(--shadow-sm);
    transition: all 0.3s ease;
}

.code-block.collapsible:hover {
    box-shadow: var(--shadow-md);
}

.code-toggle {
    background: linear-gradient(135deg, rgba(248, 249, 250, 0.8), rgba(240, 241, 243, 0.6));
    padding: 0.75rem 1rem;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: all 0.3s ease;
    margin: 0;
    border-radius: 12px 12px 0 0;
}

.code-toggle:hover {
    background: linear-gradient(135deg, rgba(244, 172, 183, 0.15), rgba(240, 241, 243, 0.8));
    transform: translateX(4px);
}

.toggle-icon {
    font-size: 0.8rem;
    width: 1rem;
    display: inline-block;
}

.code-content {
    border-top: 1px solid var(--border);
}

.code-content pre {
    margin: 0;
    border-radius: 0 0 8px 8px;
}

pre {
    background: var(--code-bg) !important;
    border-radius: 8px;
    padding: 1rem !important;
    overflow-x: auto;
    font-size: 0.85rem;
    max-width: 100%;
    position: relative;
}

/* Enable horizontal scrolling for code */
pre code {
    display: block;
    overflow-x: auto;
    white-space: pre;
}

/* Scrollbar styling for better visibility */
pre::-webkit-scrollbar {
    height: 8px;
}

pre::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

pre::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 4px;
}

pre::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5);
}

code {
    font-family: 'JetBrains Mono', monospace;
}

/* Results with Image Layout */
.results-with-image {
    display: grid;
    grid-template-columns: 1fr 1.5fr;
    gap: 2rem;
    align-items: start;
    margin: 2rem 0;
}

.left-content {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.right-content figure {
    margin: 0;
}

.right-content img {
    width: 100%;
    border-radius: 8px;
    border: 1px solid var(--border);
}

/* Stacked Images for Finite Difference */
.stacked-images {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.stacked-images figure {
    margin: 0;
}

.stacked-images img {
    width: 100%;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.stacked-images figcaption {
    margin-top: 0.5rem;
    font-size: 0.85rem;
    color: var(--text-light);
}

/* Accuracy Details */
.accuracy-details {
    background: #f8f8f8;
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.accuracy-details h4 {
    font-size: 0.95rem;
    margin-bottom: 0.75rem;
    color: var(--text);
}

.accuracy-details p {
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
    color: var(--text);
}

.accuracy-details p.note {
    margin-top: 0.75rem;
    color: var(--text-light);
    font-style: italic;
}

/* Performance Table */
.performance-table {
    overflow-x: auto;
}

.performance-table table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

.performance-table th {
    background: #f8f8f8;
    padding: 0.75rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.9rem;
    border-bottom: 2px solid var(--border);
}

.performance-table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.performance-table tr:last-child td {
    border-bottom: none;
}

.time-slow { color: var(--error); font-weight: 600; }
.time-med { color: var(--warning); font-weight: 600; }
.time-fast { color: var(--success); font-weight: 600; }

/* Images */
.image-grid {
    display: grid;
    gap: 1.5rem;
    margin: 2rem 0;
}

.image-grid.three-col {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
}

figure {
    margin: 0;
}

figure img {
    width: 100%;
    border-radius: 12px;
    cursor: zoom-in;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

figure img:hover {
    transform: translateY(-4px) scale(1.01);
    box-shadow: var(--shadow-md);
}

figcaption {
    margin-top: 0.5rem;
    font-size: 0.9rem;
    color: var(--text-light);
    text-align: center;
}

/* Note Box */
.note-box {
    background: #f0f9ff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    padding: 1.5rem;
    margin: 2rem 0;
}

.note-box code {
    display: block;
    background: var(--code-bg);
    color: #fff;
    padding: 0.75rem;
    border-radius: 4px;
    margin-bottom: 0.75rem;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.9rem;
    white-space: pre;
}

.note-box p {
    margin: 0;
    color: var(--text);
}

/* Observation Box */
.observation-box {
    background: linear-gradient(135deg, rgba(255, 229, 217, 0.5), rgba(255, 229, 217, 0.2));
    border-left: 4px solid var(--accent);
    padding: 1.5rem 2rem;
    margin: 2rem 0;
    border-radius: 0 12px 12px 0;
    box-shadow: var(--shadow-sm);
    transition: all 0.3s ease;
    animation: slideInLeft 0.5s ease-out;
}

.observation-box:hover {
    transform: translateX(4px);
    box-shadow: var(--shadow-md);
}

@keyframes slideInLeft {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.observation-box h4 {
    color: var(--text);
    margin-bottom: 1rem;
    font-size: 1rem;
    font-weight: 600;
}

.observation-box p {
    color: var(--text);
    margin-bottom: 0.5rem;
}

.observation-box p:last-child {
    margin-bottom: 0;
}

.observation-box ul {
    list-style: none;
    padding: 0;
    margin: 0.5rem 0;
}

.observation-box li {
    color: var(--text);
    margin-bottom: 0.75rem;
    line-height: 1.7;
    padding-left: 1.5rem;
    position: relative;
}

.observation-box li:before {
    content: "—";
    position: absolute;
    left: 0;
    color: var(--accent);
}

.observation-box li:last-child {
    margin-bottom: 0;
}

/* Implementation Note Box */
.implementation-note {
    background: linear-gradient(135deg, rgba(216, 226, 220, 0.6), rgba(216, 226, 220, 0.3));
    border-left: 4px solid var(--accent-dark);
    padding: 1.5rem 2rem;
    margin: 2rem 0;
    border-radius: 0 12px 12px 0;
    box-shadow: var(--shadow-sm);
    transition: all 0.3s ease;
    animation: slideInLeft 0.5s ease-out;
}

.implementation-note:hover {
    transform: translateX(4px);
    box-shadow: var(--shadow-md);
}

.implementation-note h4 {
    color: var(--text);
    margin-bottom: 1rem;
    font-size: 1rem;
    font-weight: 600;
}

.implementation-note p {
    color: var(--text);
    margin-bottom: 0.5rem;
}

.implementation-note p:last-child {
    margin-bottom: 0;
}

.implementation-note ul {
    list-style: none;
    padding: 0;
    margin: 0.5rem 0;
}

.implementation-note li {
    color: var(--text);
    margin-bottom: 0.75rem;
    line-height: 1.7;
    padding-left: 1.5rem;
    position: relative;
}

.implementation-note li:before {
    content: "—";
    position: absolute;
    left: 0;
    color: var(--accent-dark);
}

.implementation-note li:last-child {
    margin-bottom: 0;
}

/* Final result container for Part 1.2 */
.final-result-container {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 2rem;
    margin: 2rem 0;
    align-items: start;
}

.result-image {
    margin: 0;
}

.result-image img {
    width: 100%;
    height: auto;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.result-analysis {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.result-analysis .observation-box {
    margin: 0;
}

/* Responsive layout */
@media (max-width: 768px) {
    .final-result-container {
        grid-template-columns: 1fr;
    }
}

/* Highlight Result */
.highlight-result {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 2rem;
    align-items: center;
    background: linear-gradient(135deg, rgba(255, 229, 217, 0.4), rgba(255, 202, 212, 0.2));
    padding: 2.5rem;
    margin: 2rem 0;
    border-radius: 16px;
    box-shadow: var(--shadow-md);
    transition: all 0.3s ease;
}

.highlight-result:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(157, 129, 137, 0.15);
}

.highlight-result img {
    width: 100%;
    max-width: 500px;
    border: 1px solid var(--border);
}

/* Combined Highlight Result with Trade-off */
.highlight-result-with-tradeoff {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3rem;
    align-items: center;
    background: linear-gradient(135deg, rgba(255, 229, 217, 0.4), rgba(255, 202, 212, 0.2));
    padding: 2.5rem;
    margin: 2rem 0;
    border-radius: 16px;
    box-shadow: var(--shadow-md);
    transition: all 0.3s ease;
}

.highlight-result-with-tradeoff:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(157, 129, 137, 0.15);
}

.highlight-result-with-tradeoff img {
    width: 100%;
    border: 1px solid var(--border);
}

.right-panel {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.tradeoff-items {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
}

.tradeoff-items .tradeoff-item {
    padding: 0.75rem;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 8px;
    text-align: center;
    font-size: 0.85rem;
}

.tradeoff-items .tradeoff-item strong {
    display: block;
    font-size: 0.9rem;
    margin: 0.5rem 0 0.25rem;
}

.tradeoff-items .tradeoff-item p {
    margin: 0;
    font-size: 0.8rem;
}

.tradeoff-items .tradeoff-item.optimal {
    background: rgba(34, 197, 94, 0.15);
    border: 2px solid var(--success);
}

.result-info h4 {
    font-size: 1.6rem;
    margin-bottom: 1rem;
    color: var(--text);
    font-weight: 700;
}

.result-info p {
    margin: 0 0 1rem 0;
    line-height: 1.8;
    color: var(--text-light);
}

.result-info p:last-child {
    margin-bottom: 0;
}

/* Trade-off Grid */
.tradeoff-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin: 2rem 0;
}

.tradeoff-grid.boxed {
    background: #e3f2fd;
    border: 2px solid #1976d2;
    border-radius: 12px;
    padding: 2rem;
}

.tradeoff-item {
    text-align: center;
    padding: 1.5rem;
    background: #f8f8f8;
    border-radius: 8px;
    transition: transform 0.2s;
}

.tradeoff-item:hover {
    transform: translateY(-2px);
}

.tradeoff-item.optimal {
    background: linear-gradient(135deg, #dcfce7, #bbf7d0);
    border: 2px solid var(--success);
}

.tradeoff-item .icon {
    display: block;
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.tradeoff-item strong {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--text);
}

.tradeoff-item p {
    margin: 0;
    font-size: 0.9rem;
}

/* Lightbox */
.lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.lightbox img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
}

.lightbox .close {
    position: absolute;
    top: 2rem;
    right: 2rem;
    color: white;
    font-size: 2rem;
    cursor: pointer;
    transition: opacity 0.2s;
}

.lightbox .close:hover {
    opacity: 0.7;
}

/* Responsive */
@media (max-width: 768px) {
    header h1 {
        font-size: 1.8rem;
    }

    .code-comparison {
        grid-template-columns: 1fr;
    }

    .code-comparison.three-col {
        grid-template-columns: 1fr;
    }

    .results-with-image {
        grid-template-columns: 1fr;
    }

    .toc {
        padding: 0.75rem;
        gap: 1rem;
        font-size: 0.85rem;
    }

    .highlight-result {
        grid-template-columns: 1fr;
    }

    .tradeoff-grid {
        grid-template-columns: 1fr;
    }

    main {
        padding: 0 1rem 3rem;
    }
}