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

:root {
    --wood-light: #a88050;
    --wood-mid: #8b6530;
    --wood-dark: #6b4c18;
    --wood-shadow: #3a2a0a;
    --bezel: #4a4540;
    --bezel-light: #5a5550;
    --screen-black: #080808;
    --chrome: #c8b89a;
    --amber: #ffb000;
    --amber-dim: #7a5500;
    --green-led: #00ff41;
    --red-led: #ff2020;
    --bg: #12100e;
    --surface: #1a1714;
    --mono: "SF Mono", "Cascadia Code", "Fira Code", "JetBrains Mono", monospace;
}

html { font-size: 15px; }

body {
    background: var(--bg);
    background-image:
        radial-gradient(ellipse at 50% 30%, rgba(40,30,20,0.4) 0%, transparent 70%);
    color: var(--chrome);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    line-height: 1.5;
    min-height: 100vh;
    padding: 1.5rem 1rem 2rem;
}

/* ─── Header ─── */

header {
    text-align: center;
    margin-bottom: 2rem;
}

header h1 {
    font-size: 2.4rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--amber);
    text-shadow: 0 0 30px rgba(255,176,0,0.3);
}

header .tagline {
    color: #8a7a60;
    font-size: 0.95rem;
    margin-top: 0.2rem;
}

header .wasm-size {
    color: var(--amber);
    font-family: var(--mono);
    font-size: 0.8rem;
    margin-top: 0.15rem;
    opacity: 0.7;
}

/* ─── TV Setup Layout ─── */

.tv-setup {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
    margin: 0 auto 2.5rem;
    max-width: 1100px;
}

/* ─── TV Body ─── */

.tv {
    flex-shrink: 0;
}

.tv-body {
    background:
        repeating-linear-gradient(
            92deg,
            transparent,
            transparent 2px,
            rgba(0,0,0,0.06) 2px,
            rgba(0,0,0,0.06) 3px
        ),
        repeating-linear-gradient(
            88deg,
            transparent,
            transparent 7px,
            rgba(255,255,255,0.025) 7px,
            rgba(255,255,255,0.025) 11px
        ),
        linear-gradient(
            180deg,
            var(--wood-light) 0%,
            var(--wood-mid) 25%,
            #7a5a22 55%,
            var(--wood-dark) 80%,
            var(--wood-shadow) 100%
        );
    border-radius: 14px;
    padding: 16px 18px 10px;
    box-shadow:
        0 8px 30px rgba(0,0,0,0.6),
        0 2px 6px rgba(0,0,0,0.4),
        inset 0 1px 0 rgba(255,255,255,0.08);
    position: relative;
}

/* ─── Bezel ─── */

.tv-bezel {
    background: linear-gradient(180deg, var(--bezel-light) 0%, var(--bezel) 40%, #3a3530 100%);
    border-radius: 10px;
    padding: 10px;
    box-shadow:
        inset 0 2px 6px rgba(0,0,0,0.5),
        0 1px 0 rgba(255,255,255,0.05);
}

/* ─── CRT Screen ─── */

.tv-screen {
    background: var(--screen-black);
    border-radius: 6px;
    overflow: hidden;
    position: relative;
    aspect-ratio: 4/3;
    width: 320px;
    box-shadow: inset 0 0 50px rgba(0,0,0,0.9);
}

.tv-screen::after {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(ellipse at 25% 15%, rgba(255,255,255,0.05) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 90%, rgba(255,255,255,0.015) 0%, transparent 35%);
    pointer-events: none;
    z-index: 10;
    border-radius: 6px;
}

.tv-screen video {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    z-index: 2;
    opacity: 0;
    transition: opacity 0.5s ease-in;
}

.tv-screen video.visible {
    opacity: 1;
}

.tv-screen .static-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    image-rendering: pixelated;
    opacity: 0;
    transition: opacity 0.3s;
}

.tv-screen .static-canvas.active {
    opacity: 1;
}

/* ─── CRT Warm-up Effect ─── */

.warmup-line {
    position: absolute;
    left: 5%;
    right: 5%;
    top: 50%;
    height: 2px;
    background: #fff;
    border-radius: 1px;
    transform: translateY(-50%) scaleX(0);
    opacity: 0;
    z-index: 5;
    box-shadow: 0 0 20px #fff, 0 0 60px rgba(255,255,255,0.5);
    transition: none;
}

.tv-screen.warming-up .warmup-line {
    animation: warmup 1.2s ease-out forwards;
}

@keyframes warmup {
    0%   { transform: translateY(-50%) scaleX(0) scaleY(1); opacity: 1; }
    20%  { transform: translateY(-50%) scaleX(0.8) scaleY(1); opacity: 1; }
    40%  { transform: translateY(-50%) scaleX(1) scaleY(1); opacity: 1; }
    60%  { transform: translateY(-50%) scaleX(1) scaleY(30); opacity: 0.7; }
    80%  { transform: translateY(-50%) scaleX(1) scaleY(80); opacity: 0.4; }
    100% { transform: translateY(-50%) scaleX(1) scaleY(120); opacity: 0; }
}

/* ─── OSD Text ─── */

.screen-osd {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255,255,255,0.6);
    font-family: var(--mono);
    font-size: 0.9rem;
    font-weight: 700;
    letter-spacing: 0.15em;
    z-index: 8;
    text-shadow: 0 0 8px rgba(255,255,255,0.3);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.screen-osd.visible {
    opacity: 1;
}

/* ─── TV Chrome (bottom bar) ─── */

.tv-chrome {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 4px 2px;
}

.tv-badge {
    display: flex;
    align-items: center;
    gap: 4px;
}

.badge-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    display: inline-block;
}

.badge-name {
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    color: #baa882;
    margin-left: 6px;
    text-transform: uppercase;
}

.brand-text {
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    color: #baa882;
    font-style: italic;
}

/* ─── Power Button ─── */

.power-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid #5a5040;
    background: linear-gradient(180deg, #3a3530 0%, #2a2520 100%);
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.3s, box-shadow 0.3s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}

.power-btn:hover {
    border-color: #8a7a60;
}

.power-btn.on {
    border-color: var(--green-led);
    box-shadow: 0 0 10px rgba(0,255,65,0.3);
}

.power-ring {
    position: absolute;
    inset: 2px;
    border-radius: 50%;
    border: 1px solid transparent;
    transition: border-color 0.3s;
}

.power-btn.on .power-ring {
    border-color: rgba(0,255,65,0.2);
}

.power-symbol {
    font-size: 0.75rem;
    color: #6a6050;
    transition: color 0.3s;
    line-height: 1;
}

.power-btn.on .power-symbol {
    color: var(--green-led);
    text-shadow: 0 0 6px var(--green-led);
}

/* ─── TV Stand / Feet ─── */

.tv-stand {
    display: flex;
    justify-content: center;
    gap: 60%;
    padding-top: 2px;
}

.foot {
    width: 20px;
    height: 8px;
    background: linear-gradient(180deg, #4a4035 0%, #2a2520 100%);
    border-radius: 0 0 4px 4px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}

/* ─── Encoder Unit ─── */

.encoder-unit {
    display: flex;
    align-items: center;
    align-self: center;
    flex-shrink: 0;
}

.cable {
    width: 30px;
    height: 4px;
    background: #2a2520;
    border-radius: 2px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.4);
    position: relative;
}

.cable::before,
.cable::after {
    content: '';
    position: absolute;
    width: 8px;
    height: 8px;
    background: #3a3530;
    border-radius: 50%;
    top: -2px;
    box-shadow: inset 0 1px 2px rgba(0,0,0,0.5);
}

.cable-left::before { left: -2px; }
.cable-left::after { right: -2px; }
.cable-right::before { left: -2px; }
.cable-right::after { right: -2px; }

.encoder-box {
    background: linear-gradient(180deg, #3a3835 0%, #2a2825 40%, #222020 100%);
    border: 1px solid #4a4845;
    border-radius: 6px;
    padding: 10px 14px;
    min-width: 150px;
    box-shadow:
        0 4px 16px rgba(0,0,0,0.5),
        inset 0 1px 0 rgba(255,255,255,0.05);
}

.encoder-top {
    text-align: center;
    border-bottom: 1px solid #3a3835;
    padding-bottom: 6px;
    margin-bottom: 8px;
}

.encoder-brand {
    display: block;
    font-size: 0.9rem;
    font-weight: 800;
    color: var(--amber);
    letter-spacing: 0.08em;
    text-shadow: 0 0 10px rgba(255,176,0,0.3);
}

.encoder-model {
    display: block;
    font-size: 0.5rem;
    font-weight: 600;
    letter-spacing: 0.2em;
    color: #6a6560;
    text-transform: uppercase;
}

/* ─── LEDs ─── */

.encoder-leds {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 8px;
}

.led-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.led-group span {
    font-size: 0.45rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    color: #5a5855;
    text-transform: uppercase;
}

.led {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #1a1815;
    border: 1px solid #3a3835;
    transition: background 0.15s, box-shadow 0.15s;
}

.led.on-green {
    background: var(--green-led);
    box-shadow: 0 0 6px var(--green-led);
}

.led.on-red {
    background: var(--red-led);
    box-shadow: 0 0 6px var(--red-led);
}

.led.on-amber {
    background: var(--amber);
    box-shadow: 0 0 6px var(--amber);
}

.led.blink {
    animation: led-blink 0.3s ease-in-out;
}

@keyframes led-blink {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

/* ─── Encoder Controls ─── */

.encoder-controls {
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-bottom: 8px;
}

.ctrl {
    display: flex;
    align-items: center;
    gap: 6px;
}

.ctrl label {
    font-size: 0.55rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    color: #8a8580;
    min-width: 50px;
    text-transform: uppercase;
    font-family: var(--mono);
}

.ctrl label span {
    color: var(--amber);
}

.ctrl input[type="range"] {
    width: 70px;
    height: 4px;
    accent-color: var(--amber);
}

.ctrl select {
    background: #1a1815;
    color: var(--amber);
    border: 1px solid #3a3835;
    border-radius: 3px;
    padding: 1px 4px;
    font-size: 0.6rem;
    font-family: var(--mono);
}

/* ─── Encoder Action Buttons ─── */

.encoder-actions {
    display: flex;
    gap: 4px;
    justify-content: center;
    flex-wrap: wrap;
}

.encoder-actions button,
.encoder-actions .btn-dl {
    font-family: var(--mono);
    font-size: 0.6rem;
    font-weight: 700;
    padding: 3px 8px;
    border: 1px solid #3a3835;
    border-radius: 3px;
    background: #1a1815;
    color: #8a8580;
    cursor: pointer;
    text-decoration: none;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    transition: border-color 0.2s, color 0.2s, background 0.2s;
}

.encoder-actions button:hover:not(:disabled) {
    border-color: var(--amber);
    color: var(--amber);
}

.encoder-actions button:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

#btn-record {
    color: var(--red-led);
    border-color: rgba(255,32,32,0.3);
}

#btn-record:hover:not(:disabled) {
    border-color: var(--red-led);
    background: rgba(255,32,32,0.1);
}

#btn-stop {
    color: #aaa;
}

.btn-dl {
    display: inline-block;
    color: var(--green-led) !important;
    border-color: rgba(0,255,65,0.3) !important;
}

.btn-dl:hover {
    background: rgba(0,255,65,0.1) !important;
}

/* ─── Telemetry Panel ─── */

.telemetry {
    max-width: 750px;
    margin: 0 auto 1.5rem;
    background: #0c0c0a;
    border: 1px solid #2a2820;
    border-radius: 8px;
    padding: 12px 16px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.telemetry-header {
    font-family: var(--mono);
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.2em;
    color: var(--amber-dim);
    text-transform: uppercase;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.telemetry-led {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--amber-dim);
    transition: background 0.3s, box-shadow 0.3s;
}

body.encoding .telemetry-led {
    background: var(--amber);
    box-shadow: 0 0 6px var(--amber);
}

.telemetry-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.tele-cell {
    text-align: center;
    padding: 6px 4px;
    background: #0a0a08;
    border: 1px solid #1a1a18;
    border-radius: 4px;
}

.tele-val {
    display: block;
    font-family: var(--mono);
    font-size: 1rem;
    font-weight: 700;
    color: var(--amber);
    text-shadow: 0 0 8px rgba(255,176,0,0.2);
    min-height: 1.3em;
}

.tele-lbl {
    display: block;
    font-family: var(--mono);
    font-size: 0.5rem;
    font-weight: 600;
    color: #5a5540;
    letter-spacing: 0.1em;
    margin-top: 2px;
    text-transform: uppercase;
}

.chart-area {
    position: relative;
}

.chart-label {
    font-family: var(--mono);
    font-size: 0.5rem;
    font-weight: 600;
    letter-spacing: 0.15em;
    color: #5a5540;
    text-transform: uppercase;
}

#frame-chart {
    width: 100%;
    height: 100px;
    border-radius: 3px;
    margin-top: 4px;
}

/* ─── Signal Path ─── */

.signal-path {
    max-width: 750px;
    margin: 0 auto 1rem;
    background: #0c0c0a;
    border: 1px solid #2a2820;
    border-radius: 8px;
    padding: 10px 16px 12px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.signal-header {
    font-family: var(--mono);
    font-size: 0.55rem;
    font-weight: 700;
    letter-spacing: 0.2em;
    color: var(--amber-dim);
    text-transform: uppercase;
    margin-bottom: 8px;
}

.signal-nodes {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
}

.sig-node {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    padding: 6px 10px;
    border: 1px solid #1a1a18;
    border-radius: 4px;
    background: #0a0a08;
    min-width: 75px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.sig-node.active {
    border-color: var(--amber);
    box-shadow: 0 0 8px rgba(255,176,0,0.15);
}

.sig-icon {
    font-size: 0.9rem;
    line-height: 1;
    color: #4a4530;
    transition: color 0.2s;
}

.sig-node.active .sig-icon {
    color: var(--amber);
}

.sig-label {
    font-family: var(--mono);
    font-size: 0.5rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: #5a5540;
    white-space: nowrap;
}

.sig-stat {
    font-family: var(--mono);
    font-size: 0.55rem;
    color: var(--amber);
    opacity: 0.7;
    min-height: 1em;
    white-space: nowrap;
}

.sig-wire {
    width: 24px;
    height: 1px;
    background: #2a2820;
    position: relative;
    flex-shrink: 0;
}

.sig-dot {
    display: block;
    width: 4px;
    height: 4px;
    background: var(--amber);
    border-radius: 50%;
    position: absolute;
    top: -1.5px;
    left: 0;
    opacity: 0;
    transition: opacity 0.2s;
}

body.encoding .sig-wire {
    background: rgba(255,176,0,0.3);
}

body.encoding .sig-dot {
    opacity: 1;
    animation: sig-flow 0.5s linear infinite;
}

@keyframes sig-flow {
    0% { left: 0; }
    100% { left: 20px; }
}

/* ─── Steps Guide ─── */

.steps {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    max-width: 750px;
    margin: 0 auto 1.5rem;
    padding: 8px 12px;
    background: rgba(12,12,10,0.6);
    border: 1px solid #1a1a18;
    border-radius: 6px;
}

.step {
    font-family: var(--mono);
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    color: #3a3830;
    transition: color 0.3s;
    white-space: nowrap;
}

.step.active {
    color: var(--amber);
    text-shadow: 0 0 8px rgba(255,176,0,0.2);
}

.step.done {
    color: var(--green-led);
    text-shadow: 0 0 6px rgba(0,255,65,0.15);
}

.step-arrow {
    color: #2a2820;
    font-size: 0.7rem;
    transition: color 0.3s;
}

/* ─── Footer ─── */

footer {
    text-align: center;
    color: #5a5040;
    font-size: 0.75rem;
    max-width: 750px;
    margin: 0 auto;
}

footer a {
    color: var(--amber-dim);
    text-decoration: none;
}

footer a:hover {
    color: var(--amber);
    text-decoration: underline;
}

.footer-link {
    margin-top: 0.3rem;
}

.footer-link a {
    font-family: var(--mono);
    font-size: 0.7rem;
}

/* ─── Error ─── */

.error-banner {
    background: rgba(255,32,32,0.08);
    border: 1px solid rgba(255,32,32,0.3);
    border-radius: 6px;
    color: var(--red-led);
    padding: 0.8rem;
    text-align: center;
    max-width: 750px;
    margin: 0 auto 1.5rem;
    font-size: 0.85rem;
    font-family: var(--mono);
}

/* ─── Info Banner ─── */

.info-banner {
    background: rgba(255,176,0,0.08);
    border: 1px solid rgba(255,176,0,0.3);
    border-radius: 6px;
    color: var(--amber);
    padding: 0.8rem;
    text-align: center;
    max-width: 750px;
    margin: 0 auto 1.5rem;
    font-size: 0.85rem;
    font-family: var(--mono);
}

/* ─── Responsive ─── */

@media (max-width: 900px) {
    .tv-setup {
        flex-direction: column;
        gap: 1rem;
    }

    .encoder-unit {
        flex-direction: row;
    }

    .cable {
        width: 20px;
    }

    .telemetry-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .signal-nodes {
        flex-wrap: wrap;
        gap: 4px;
    }

    .sig-wire {
        width: 12px;
    }

    .steps {
        flex-wrap: wrap;
        gap: 4px 8px;
    }

    .step-arrow {
        display: none;
    }
}

@media (max-width: 480px) {
    html { font-size: 14px; }

    body { padding: 1rem 0.5rem; }

    .tv-screen {
        width: calc(100vw - 80px);
        max-width: 320px;
    }

    .encoder-box {
        min-width: 120px;
    }

    .cable {
        width: 12px;
    }

    .telemetry-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .signal-nodes {
        justify-content: flex-start;
        overflow-x: auto;
        flex-wrap: nowrap;
        padding-bottom: 6px;
        -webkit-overflow-scrolling: touch;
    }

    .sig-node {
        min-width: 60px;
        padding: 4px 6px;
    }

    .encoder-actions button,
    .encoder-actions .btn-dl {
        padding: 6px 12px;
        font-size: 0.7rem;
    }

    .power-btn {
        width: 40px;
        height: 40px;
    }

    .step {
        font-size: 0.6rem;
    }
}
