:root {
    --bg: #0a0e1a;
    --panel: #0f1628;
    --panel2: #111c35;
    --border: #1e3a6e;
    --accent: #1565c0;
    --accent2: #1976d2;
    --accent3: #42a5f5;
    --glow: #1e88e5;
    --text: #cfd8dc;
    --text-dim: #546e7a;
    --text-bright: #e3f2fd;
    --green: #00e676;
    --red: #ff1744;
    --switch-on: #1565c0;
    --switch-off: #1a2744;
    --switch-body: #0d47a1;
    --white: #f5f5f5;
    --font-mono: 'Share Tech Mono', monospace;
    --font-display: 'Orbitron', monospace;
}

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

body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-mono);
    min-height: 100vh;
    background-image:
        radial-gradient(ellipse at 20% 10%, rgba(21, 101, 192, 0.08) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 90%, rgba(25, 118, 210, 0.06) 0%, transparent 60%),
        repeating-linear-gradient(0deg, transparent, transparent 39px, rgba(30, 58, 110, 0.12) 39px, rgba(30, 58, 110, 0.12) 40px),
        repeating-linear-gradient(90deg, transparent, transparent 39px, rgba(30, 58, 110, 0.08) 39px, rgba(30, 58, 110, 0.08) 40px);
}

header {
    padding: 24px 32px 20px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(13, 71, 161, 0.15) 0%, transparent 100%);
    display: flex;
    align-items: center;
    gap: 20px;
}

.cpu-icon {
    width: 44px;
    height: 44px;
    background: var(--accent);
    border: 2px solid var(--accent3);
    border-radius: 6px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 3px;
    padding: 6px;
    box-shadow: 0 0 18px rgba(66, 165, 245, 0.35);
    flex-shrink: 0;
}

.cpu-dot {
    background: var(--text-bright);
    border-radius: 1px;
    opacity: 0.8;
}

.header-text h1 {
    font-family: var(--font-display);
    font-size: clamp(14px, 3vw, 22px);
    font-weight: 900;
    color: var(--text-bright);
    letter-spacing: 3px;
    text-transform: uppercase;
}

.header-text p {
    font-size: 11px;
    color: var(--text-dim);
    letter-spacing: 1.5px;
    margin-top: 3px;
    text-transform: uppercase;
}

main {
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px 20px 60px;
    display: flex;
    flex-direction: column;
    gap: 28px;
}

/* Editor. */
.editor-section {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 32px rgba(0, 0, 0, 0.5);
}

.section-header {
    padding: 12px 18px;
    background: var(--panel2);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
}

.section-title {
    font-family: var(--font-display);
    font-size: 11px;
    letter-spacing: 2px;
    color: var(--accent3);
    text-transform: uppercase;
}

.badge {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 99px;
    background: rgba(21, 101, 192, 0.25);
    border: 1px solid var(--accent);
    color: var(--accent3);
    letter-spacing: 1px;
}

.editor-wrapper {
    display: flex;
    min-height: 240px;
    position: relative;
}

.line-numbers {
    position: sticky;
    left: 0;
    padding: 16px 12px 16px 16px;
    text-align: right;
    color: var(--text-dim);
    font-size: 13px;
    line-height: 22px;
    user-select: none;
    min-width: 44px;
    border-right: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.2);
    overflow: hidden;
    white-space: pre;
    pointer-events: none;
    flex-shrink: 0;
    /* height must match textarea. */
    align-self: flex-start;
}

textarea#editor {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-bright);
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 22px;
    padding: 16px 16px;
    resize: vertical;
    min-height: 240px;
    tab-size: 2;
    white-space: pre;
    overflow-wrap: normal;
    overflow: auto;
    caret-color: var(--accent3);
}

textarea#editor::placeholder {
    color: var(--text-dim);
}

/* Instruction reference. */
.ref-toggle {
    background: none;
    border: 1px solid var(--border);
    color: var(--accent3);
    font-family: var(--font-mono);
    font-size: 11px;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    letter-spacing: 1px;
    transition: background 0.2s;
}

.ref-toggle:hover {
    background: rgba(21, 101, 192, 0.2);
}

.ref-table {
    display: none;
    padding: 14px 18px;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.15);
}

.ref-table.open {
    display: block;
}

.ref-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 6px 20px;
    font-size: 11px;
    color: var(--text-dim);
}

.ref-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.ref-mnem {
    color: var(--accent3);
    min-width: 100px;
}

.ref-op {
    color: var(--text-dim);
}

/* Error message section. */
#error-box {
    display: none;
    background: rgba(255, 23, 68, 0.08);
    border: 1px solid rgba(255, 23, 68, 0.4);
    border-radius: 8px;
    padding: 14px 18px;
}

#error-box.visible {
    display: block;
}

#error-box h3 {
    font-family: var(--font-display);
    font-size: 10px;
    letter-spacing: 2px;
    color: var(--red);
    margin-bottom: 8px;
    text-transform: uppercase;
}

.error-item {
    font-size: 12px;
    color: #ef9a9a;
    margin-bottom: 4px;
    padding-left: 12px;
    border-left: 2px solid var(--red);
    line-height: 1.5;
}

/* Output / DIP switches (Try to match the TD4 PCB components). */
.output-section {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 32px rgba(0, 0, 0, 0.5);
}

#switches-container {
    padding: 20px 16px 24px;
}

.empty-state {
    padding: 48px 20px;
    text-align: center;
    color: var(--text-dim);
    font-size: 12px;
    letter-spacing: 1.5px;
    text-transform: uppercase;
}

.empty-state::before {
    content: '◈';
    display: block;
    font-size: 28px;
    margin-bottom: 12px;
    opacity: 0.3;
}

/* Instruction rows. */
.instr-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid rgba(30, 58, 110, 0.4);
    flex-wrap: wrap;
}

.instr-row:last-child {
    border-bottom: none;
}

.instr-addr {
    font-family: var(--font-display);
    font-size: 10px;
    color: var(--text-dim);
    letter-spacing: 1px;
    min-width: 32px;
    text-align: center;
}

.instr-mnem {
    font-size: 12px;
    color: var(--text-bright);
    min-width: 140px;
    white-space: nowrap;
}

/* DIP switch. */
.dip-switch {
    display: flex;
    gap: 0;
    background: #0a1428;
    border: 2px solid #0d47a1;
    border-radius: 8px;
    padding: 8px 10px;
    box-shadow: 0 0 12px rgba(13, 71, 161, 0.4), inset 0 1px 0 rgba(66, 165, 245, 0.1);
    position: relative;
    flex-shrink: 0;
}

/* Group separator between nibbles. */
.dip-switch::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 6px;
    bottom: 6px;
    width: 1px;
    background: rgba(66, 165, 245, 0.2);
    transform: translateX(-50%);
}

.switch-slot {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    width: 28px;
    padding: 0 2px;
}

.switch-slot:nth-child(4) {
    margin-right: 2px;
}

.switch-slot:nth-child(5) {
    margin-left: 2px;
}

.switch-label {
    font-size: 8px;
    color: var(--text-dim);
    letter-spacing: 0;
    font-family: var(--font-mono);
    line-height: 1;
}

.switch-body {
    width: 20px;
    height: 40px;
    background: linear-gradient(180deg, #0d47a1 0%, #0a3880 100%);
    border: 1px solid #1565c0;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    position: relative;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
    cursor: default;
    overflow: hidden;
}

.switch-track {
    position: absolute;
    left: 3px;
    right: 3px;
    top: 3px;
    bottom: 3px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 2px;
}

.switch-knob {
    position: absolute;
    left: 3px;
    right: 3px;
    height: 16px;
    background: var(--white);
    border-radius: 2px;
    transition: top 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5), 0 1px 0 rgba(255, 255, 255, 0.3);
}

.switch-knob::after {
    content: '';
    position: absolute;
    left: 3px;
    right: 3px;
    top: 50%;
    height: 1px;
    background: rgba(0, 0, 0, 0.15);
    transform: translateY(-50%);
}

/* ON = top, OFF = bottom. */
.switch-body.sw-on .switch-knob {
    top: 3px;
}

.switch-body.sw-off .switch-knob {
    top: calc(100% - 19px);
}

.switch-body.sw-on {
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3), 0 0 8px rgba(66, 165, 245, 0.5);
    border-color: var(--accent3);
}

.switch-pos-label {
    font-size: 8px;
    color: var(--text-dim);
    font-family: var(--font-mono);
    line-height: 1;
}

.sw-on-label,
.sw-off-label {
    position: absolute;
    font-size: 6px;
    font-family: var(--font-mono);
    color: rgba(255, 255, 255, 0.35);
    left: 50%;
    transform: translateX(-50%);
    line-height: 1;
    pointer-events: none;
}

.sw-on-label {
    top: 4px;
}

.sw-off-label {
    bottom: 4px;
}

.bit-value {
    font-size: 9px;
    color: var(--accent3);
    font-family: var(--font-mono);
    line-height: 1;
    opacity: 0;
    transition: opacity 0.2s;
}

.instr-row:hover .bit-value {
    opacity: 1;
}

.instr-byte {
    font-size: 11px;
    color: var(--text-dim);
    letter-spacing: 2px;
    white-space: nowrap;
    padding: 2px 8px;
    background: rgba(21, 101, 192, 0.1);
    border-radius: 3px;
    border: 1px solid rgba(30, 58, 110, 0.5);
}

.instr-byte .hi {
    color: #64b5f6;
}

.instr-byte .lo {
    color: #90caf9;
}

.nibble-labels {
    display: flex;
    gap: 0;
    padding: 0 10px;
    margin-bottom: 2px;
    flex-shrink: 0;
}

.nibble-label-group {
    display: flex;
    width: 136px;
    /* 4 x 28px + padding */
}

.nibble-label-group span {
    font-size: 8px;
    text-align: center;
    width: 32px;
    color: var(--text-dim);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

/* Legend. */
.legend {
    display: flex;
    gap: 16px;
    padding: 10px 18px;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.15);
    font-size: 10px;
    color: var(--text-dim);
    flex-wrap: wrap;
}

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

.legend-swatch {
    width: 10px;
    height: 10px;
    border-radius: 2px;
}

.leg-on {
    background: var(--white);
    box-shadow: 0 0 4px rgba(66, 165, 245, 0.6);
}

.leg-off {
    background: #0d47a1;
    border: 1px solid #1565c0;
}

@media (max-width: 600px) {
    header {
        padding: 16px;
    }

    main {
        padding: 16px 12px 40px;
    }

    .instr-row {
        gap: 8px;
    }

    .instr-mnem {
        min-width: 100px;
        font-size: 11px;
    }

    .switch-slot {
        width: 22px;
    }

    .switch-body {
        width: 16px;
        height: 34px;
    }

    .switch-knob {
        height: 13px;
    }

    .switch-body.sw-off .switch-knob {
        top: calc(100% - 16px);
    }

    .dip-switch {
        padding: 6px 8px;
    }

    .instr-byte {
        font-size: 10px;
    }
}

/* Footer. */
footer {
    border-top: 1px solid var(--border);
    background: linear-gradient(0deg, rgba(13, 71, 161, 0.1) 0%, transparent 100%);
    padding: 20px 32px 24px;
    margin-top: auto;
}

.footer-inner {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
}

.footer-name {
    display: flex;
    align-items: center;
    gap: 10px;
}

.footer-callsign {
    font-family: var(--font-display);
    font-size: 13px;
    font-weight: 700;
    color: var(--accent3);
    letter-spacing: 2px;
    text-shadow: 0 0 12px rgba(66, 165, 245, 0.5);
}

.footer-sep {
    color: var(--border);
    font-size: 16px;
}

.footer-fullname {
    font-family: var(--font-mono);
    font-size: 13px;
    color: var(--text);
    letter-spacing: 0.5px;
}

.footer-links {
    display: flex;
    gap: 12px;
    align-items: center;
}

.footer-link {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    color: var(--text-dim);
    text-decoration: none;
    font-size: 12px;
    letter-spacing: 1px;
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: 6px;
    transition: color 0.2s, border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.footer-link:hover {
    color: var(--accent3);
    border-color: var(--accent2);
    background: rgba(21, 101, 192, 0.15);
    box-shadow: 0 0 10px rgba(66, 165, 245, 0.2);
}

.link-icon {
    width: 15px;
    height: 15px;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.footer-link:hover .link-icon {
    opacity: 1;
}

.footer-copy {
    max-width: 1200px;
    margin: 12px auto 0;
    font-size: 10px;
    color: var(--text-dim);
    letter-spacing: 1px;
    text-transform: uppercase;
    opacity: 0.5;
}