:root {
    --panel-strong: #252525;
    --panel-soft: #303030;
    --muted: #a8b0bd;
    --violet: #8b5cf6;
}

body.lab-mode {
    overflow: hidden;
}

.lab-layout {
    display: grid;
    grid-template-columns: 300px minmax(420px, 1fr) 340px;
    grid-template-rows: auto minmax(0, 1fr) clamp(130px, 13vh, 140px);
    height: 100vh;
    min-height: 0;
    width: 100%;
}

.lab-header {
    grid-column: 1 / 4;
    box-sizing: border-box;
    min-height: 64px;
    padding: 0.5rem 1.25rem;
}

.lab-header h1,
.panel-heading h2,
.analysis-copy h2,
.compact-heading h2 {
    margin: 0;
    color: #f8fafc;
}

.lab-header h1 {
    font-size: 1.25rem;
}

.lab-kicker,
.eyebrow {
    margin: 0 0 0.25rem;
    color: #7dd3fc;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.hub-link {
    color: #93c5fd;
    text-decoration: none;
}

.hub-link:hover,
.hub-link:focus-visible {
    text-decoration: underline;
}

.lab-controls,
.lab-metrics {
    padding: 0.75rem;
    min-width: 0;
    min-height: 0;
    overflow-y: auto;
}

.lab-controls {
    grid-column: 1;
    grid-row: 2;
}

.lab-metrics {
    grid-column: 3;
    grid-row: 2;
}

.panel-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 0.65rem;
}

.panel-heading h2,
.analysis-copy h2,
.compact-heading h2 {
    font-size: 1rem;
}

select,
input,
button {
    font: inherit;
}

select {
    width: 100%;
    padding: 0.55rem 0.65rem;
    border: 1px solid #525252;
    border-radius: 6px;
    background: #202020;
    color: #f8fafc;
}

#mode {
    width: auto;
    min-width: 118px;
}

.guidance,
.method-note,
.status-copy,
.comparison-card p,
.late-card p,
.analysis-copy p,
.control-group small {
    color: var(--muted);
    line-height: 1.45;
}

.guidance {
    margin: 0 0 0.7rem;
    padding: 0.55rem 0.65rem;
    border-left: 3px solid var(--lab-accent);
    background: rgba(59, 130, 246, 0.08);
    font-size: 0.82rem;
}

.control-group {
    margin-bottom: 0.7rem;
}

.control-group label {
    margin-bottom: 0.25rem;
    color: #e5e7eb;
    font-size: 0.84rem;
}

.control-group small {
    display: block;
    margin-top: 0.3rem;
    font-size: 0.7rem;
}

.control-emphasis {
    padding: 0.6rem;
    border: 1px solid rgba(139, 92, 246, 0.65);
    border-radius: 8px;
    background: rgba(139, 92, 246, 0.08);
}

output {
    color: #c4b5fd;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-weight: 700;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--violet);
}

.control-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.65rem;
}

button {
    width: 100%;
    padding: 0.72rem 0.9rem;
    border: 0;
    border-radius: 7px;
    background: var(--lab-accent);
    color: #fff;
    cursor: pointer;
    font-weight: 700;
}

button:hover {
    background: #2563eb;
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
.hub-link:focus-visible {
    outline: 3px solid #fbbf24;
    outline-offset: 2px;
}

button:disabled,
select:disabled,
input:disabled {
    cursor: not-allowed;
    opacity: 0.58;
}

.status-copy {
    min-height: 2em;
    margin: 0.45rem 0 0;
    font-size: 0.78rem;
}

.lab-viewport {
    grid-column: 2;
    grid-row: 2;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

.lab-viewport canvas {
    display: block;
    max-width: 100%;
}

#loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: rgba(5, 5, 16, 0.9);
    color: #e5e7eb;
    text-align: center;
}

#loading-overlay[hidden] {
    display: none !important;
}

#labels-container {
    position: absolute;
    inset: 0;
    z-index: 5;
    pointer-events: none;
}

.route-label {
    position: absolute;
    z-index: 20;
    padding: 3px 6px;
    transform: translate(-50%, -150%);
    border: 1px solid #3b82f6;
    border-radius: 4px;
    background: rgba(10, 25, 47, 0.9);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
    color: #fff;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-size: 0.68rem;
    pointer-events: none;
    transition: opacity 0.2s;
    white-space: nowrap;
}

.sample-badge {
    padding: 0.25rem 0.5rem;
    border: 1px solid #475569;
    border-radius: 999px;
    color: #cbd5e1;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-size: 0.72rem;
    white-space: nowrap;
}

.live-indicator {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 0.45rem;
    align-items: center;
    margin-bottom: 0.5rem;
    padding: 0.45rem 0.55rem;
    border: 1px solid rgba(16, 185, 129, 0.45);
    border-radius: 7px;
    background: rgba(16, 185, 129, 0.08);
    color: #d1fae5;
    font-size: 0.7rem;
}

.live-indicator strong {
    min-width: 0;
}

.live-indicator > span:last-child {
    color: #a7f3d0;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    white-space: nowrap;
}

.live-dot {
    width: 0.55rem;
    height: 0.55rem;
    border-radius: 50%;
    background: #10b981;
    box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.55);
    animation: livePulse 1.8s infinite;
}

@keyframes livePulse {
    0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.55); }
    70% { box-shadow: 0 0 0 7px rgba(16, 185, 129, 0); }
    100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}

.decomposition,
.comparison-card,
.late-card {
    margin-bottom: 0.55rem;
    padding: 0.6rem;
    border: 1px solid var(--lab-border);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.18);
}

.decomposition h3,
.comparison-card h3 {
    margin: 0 0 0.45rem;
    font-size: 0.85rem;
}

.decomposition dl {
    margin: 0;
}

.decomposition dl div {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.18rem 0;
    color: #cbd5e1;
    font-size: 0.78rem;
}

.decomposition dd {
    margin: 0;
    color: #f8fafc;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
}

.decomposition .total {
    margin-top: 0.35rem;
    padding-top: 0.5rem;
    border-top: 1px solid #475569;
    font-weight: 700;
}

.method-note {
    margin: 0.45rem 0 0;
    font-size: 0.7rem;
}

.metric-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
    margin-bottom: 0.55rem;
}

.metric-card {
    min-width: 0;
    padding: 0.5rem;
    border: 1px solid #3f3f46;
    border-radius: 7px;
    background: var(--panel-strong);
}

.metric-card span,
.late-card span {
    display: block;
    color: var(--muted);
    font-size: 0.7rem;
}

.metric-card strong,
.late-card strong {
    display: block;
    margin-top: 0.2rem;
    color: #f8fafc;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-size: 1rem;
}

.metric-card.metric-stable {
    border-style: dashed;
}

.metric-causality {
    margin: -0.1rem 0 0.55rem;
    padding: 0.45rem 0.55rem;
    border-left: 3px solid #8b5cf6;
    background: rgba(139, 92, 246, 0.08);
    color: #ddd6fe;
    font-size: 0.72rem;
    line-height: 1.35;
}

.metric-changed {
    animation: metricChanged 0.9s ease-out;
}

@keyframes metricChanged {
    0% {
        border-color: #fbbf24;
        background: rgba(245, 158, 11, 0.35);
        box-shadow: 0 0 0 4px rgba(245, 158, 11, 0.18);
        transform: translateY(-2px);
    }
    100% {
        box-shadow: none;
        transform: translateY(0);
    }
}

.late-card {
    border-left: 4px solid var(--lab-success);
}

.late-card.has-late {
    border-left-color: var(--lab-error);
}

.late-card p,
.comparison-card p {
    margin: 0.35rem 0 0;
    font-size: 0.74rem;
}

.lab-analysis {
    grid-column: 1 / 3;
    grid-row: 3;
    display: grid;
    grid-template-columns: minmax(210px, 270px) minmax(0, 1fr);
    gap: 0.8rem;
    align-items: center;
    min-width: 0;
    min-height: 0;
    padding: 0.7rem 1rem;
    border-top: 1px solid var(--lab-border);
    background: #202020;
}

.analysis-copy p {
    margin: 0.3rem 0 0;
    font-size: 0.72rem;
}

.analysis-copy h2 {
    font-size: 0.9rem;
}

.chart-progress {
    display: inline-block;
    margin-top: 0.45rem;
    color: #93c5fd;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-size: 0.68rem;
}

#delay-chart {
    display: block;
    width: 100%;
    height: 94px;
    min-height: 0;
    overflow: hidden;
}

.lab-timeline {
    grid-column: 3;
    grid-row: 3;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    padding: 0.7rem 0.85rem;
    border-top: 1px solid var(--lab-border);
    border-left: 1px solid var(--lab-border);
}

.compact-heading {
    align-items: end;
    margin-bottom: 0.6rem;
}

.compact-heading span {
    color: var(--muted);
    font-size: 0.72rem;
}

#event-log {
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    padding: 0.65rem;
    border: 1px solid #3f3f46;
    border-radius: 7px;
    background: #09090b;
    color: #d4d4d8;
    font-family: "Cascadia Mono", "JetBrains Mono", monospace;
    font-size: 0.75rem;
    line-height: 1.55;
}

#event-log [data-tone="success"] {
    color: #6ee7b7;
}

#event-log [data-tone="warning"] {
    color: #fcd34d;
}

#event-log [data-tone="error"] {
    color: #fca5a5;
}

@media (max-width: 1120px) {
    body.lab-mode {
        overflow: auto;
    }

    .lab-layout {
        grid-template-columns: 280px minmax(0, 1fr);
        grid-template-rows: auto minmax(500px, 1fr) auto auto auto;
        height: auto;
        min-height: 100vh;
    }

    .lab-header,
    .lab-analysis,
    .lab-timeline {
        grid-column: 1 / 3;
    }

    .lab-metrics {
        grid-column: 1 / 3;
        grid-row: 3;
        display: grid;
        grid-template-columns: 1.2fr 1.5fr 1fr;
        gap: 0.8rem;
    }

    .lab-metrics > .panel-heading {
        grid-column: 1 / 4;
    }

    .lab-analysis {
        grid-column: 1 / 3;
        grid-row: 4;
    }

    .lab-timeline {
        grid-column: 1 / 3;
        grid-row: 5;
        min-height: 150px;
        border-left: 0;
    }
}

@media (max-width: 760px) {
    .lab-layout {
        display: block;
    }

    .lab-header {
        align-items: flex-start;
        gap: 0.75rem;
    }

    .lab-controls,
    .lab-metrics,
    .lab-analysis,
    .lab-timeline {
        display: block;
        border: 0;
        border-bottom: 1px solid var(--lab-border);
    }

    .lab-viewport {
        min-height: 430px;
    }

    .lab-analysis {
        padding: 1rem;
    }

    #delay-chart {
        height: auto;
        min-height: 150px;
    }

    .analysis-copy {
        margin-bottom: 0.8rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
    }
}
