:root {
    --bg-0: #090d13;
    --bg-1: #101722;
    --bg-2: #162130;
    --panel: rgba(15, 23, 35, 0.92);
    --border: #2f445b;
    --text: #d6e2ef;
    --text-dim: #8ea4bb;
    --accent: #57c5ff;
    --accent-strong: #8cdcff;
    --ok: #7ad79a;
    --err: #ff7a7a;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--text);
    font-family: "JetBrains Mono", monospace;
    background:
        radial-gradient(circle at 10% 10%, rgba(87, 197, 255, 0.12), transparent 30%),
        radial-gradient(circle at 90% 0%, rgba(0, 130, 170, 0.12), transparent 35%),
        repeating-linear-gradient(
            0deg,
            rgba(255, 255, 255, 0.015) 0,
            rgba(255, 255, 255, 0.015) 1px,
            transparent 1px,
            transparent 28px
        ),
        linear-gradient(180deg, var(--bg-1), var(--bg-0));
}

.app-shell {
    width: min(1500px, calc(100% - 2rem));
    margin: 1rem auto;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 14px 32px rgba(0, 0, 0, 0.4);
}

.topbar {
    padding: 1rem 1.25rem;
    margin-bottom: 1rem;
}

.top-actions {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-bottom: 0.85rem;
}

.brand {
    margin: 0;
    font-family: "Chakra Petch", sans-serif;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--accent-strong);
    font-size: clamp(1.35rem, 4vw, 2.25rem);
}

.subtitle {
    margin: 0.45rem 0 0;
    color: var(--text-dim);
    font-size: 0.92rem;
}

.typewriter {
    width: max-content;
    border-right: 2px solid var(--accent);
    padding-right: 0.4rem;
    animation: cursor-blink 1s steps(1, end) infinite;
}

@keyframes cursor-blink {
    0%,
    49% {
        border-color: var(--accent);
    }
    50%,
    100% {
        border-color: transparent;
    }
}

.layout {
    display: grid;
    grid-template-columns: 2.3fr 1fr;
    gap: 1rem;
    align-items: start;
}

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

.machine-panel {
    padding: 1rem;
}

.machine-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.8rem;
    margin-bottom: 0.7rem;
}

.machine-header h2 {
    margin: 0;
    font-size: 0.96rem;
    font-family: "Chakra Petch", sans-serif;
    color: var(--accent-strong);
}

.machine-header span {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.machine-stage {
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 24px;
}

.machine-paper-zone {
    width: min(88%, 760px);
    margin-bottom: -18px;
    position: relative;
    z-index: 2;
}

.paper-sheet {
    min-height: 180px;
    max-height: 380px;
    overflow-y: auto;
    border: 1px solid #3b4f62;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02)),
        #d5dbe2;
    color: #1f2f41;
    border-radius: 8px 8px 2px 2px;
    padding: 1rem 1rem 1.4rem;
    font-size: 0.95rem;
    line-height: 1.4;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.28);
}

.paper-sheet.ejecting {
    animation: sheet-eject 0.55s ease-in;
}

@keyframes sheet-eject {
    0% {
        transform: translateY(0);
        opacity: 1;
    }
    65% {
        transform: translateY(-120px);
        opacity: 0.65;
    }
    100% {
        transform: translateY(-220px);
        opacity: 0;
    }
}

.paper-sheet pre {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: "JetBrains Mono", monospace;
}

.paper-cursor {
    display: inline-block;
    color: #405f7c;
    animation: paper-blink 1s step-end infinite;
}

.machine-body {
    width: min(94%, 840px);
    background: linear-gradient(165deg, #273140, #1a2431);
    border: 1px solid #42566e;
    border-radius: 16px;
    box-shadow:
        0 20px 35px rgba(0, 0, 0, 0.45),
        inset 0 4px 9px rgba(255, 255, 255, 0.06);
    padding: 36px 18px 16px;
    position: relative;
    z-index: 1;
}

.machine-roller {
    position: absolute;
    top: -12px;
    left: 4%;
    width: 92%;
    height: 30px;
    border-radius: 10px;
    background: linear-gradient(180deg, #121921, #0d1219);
    border: 1px solid #263443;
    box-shadow: inset 0 2px 3px rgba(255, 255, 255, 0.06);
}

.machine-keyboard {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}

.machine-row {
    display: flex;
    gap: 8px;
    width: 100%;
    justify-content: center;
}

.machine-key {
    min-width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid #5a6f87;
    background: linear-gradient(180deg, #e7edf3, #b4c2d1);
    color: #1e2732;
    font-size: 0.86rem;
    font-weight: 700;
    font-family: "JetBrains Mono", monospace;
    box-shadow:
        0 5px 0 #7f92a7,
        0 8px 10px rgba(0, 0, 0, 0.35);
    cursor: pointer;
    transition: transform 0.05s ease, box-shadow 0.05s ease;
}

.machine-key:hover {
    filter: brightness(1.03);
}

.machine-key.pressed {
    transform: translateY(5px);
    box-shadow:
        0 0 0 #7f92a7,
        0 2px 4px rgba(0, 0, 0, 0.35);
}

.machine-key.space {
    min-width: 280px;
    border-radius: 10px;
}

.machine-key.enter,
.machine-key.backspace {
    min-width: 92px;
    border-radius: 18px;
}

@keyframes paper-blink {
    0%,
    49% {
        opacity: 1;
    }
    50%,
    100% {
        opacity: 0;
    }
}

.controls {
    padding: 1rem;
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.tab-btn,
.inline-btn,
.primary-btn,
.ghost-btn {
    border: 1px solid var(--border);
    background: #101a28;
    color: var(--text);
    font-family: inherit;
    font-size: 0.9rem;
    border-radius: 8px;
    padding: 0.62rem 0.9rem;
    cursor: pointer;
    transition: 0.2s ease;
}

.tab-btn:hover,
.inline-btn:hover,
.primary-btn:hover,
.ghost-btn:hover {
    border-color: var(--accent);
    color: #f4fbff;
}

.tab-btn.active {
    background: rgba(87, 197, 255, 0.2);
    border-color: var(--accent);
    color: #ecf7ff;
}

.ghost-btn {
    background: #0f1622;
}

.setup-grid {
    display: grid;
    grid-template-columns: 1fr 1.6fr;
    gap: 1rem;
    margin-bottom: 1rem;
}

.config-col,
.matrix-col,
.io-block {
    background: rgba(10, 15, 22, 0.8);
    border: 1px solid #26384b;
    border-radius: 8px;
    padding: 0.8rem;
}

.config-col label,
.matrix-col label,
.io-block label {
    display: block;
    margin-bottom: 0.35rem;
    color: var(--text-dim);
    font-size: 0.84rem;
}

select,
textarea,
.matrix-cell {
    width: 100%;
    border: 1px solid #2c3f55;
    background: #09111c;
    color: var(--text);
    font-family: inherit;
    border-radius: 8px;
    outline: none;
}

select,
textarea {
    padding: 0.62rem 0.72rem;
}

textarea {
    resize: vertical;
}

select:focus,
textarea:focus,
.matrix-cell:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(87, 197, 255, 0.2);
}

.config-col select,
.config-col button,
.config-col small {
    margin-bottom: 0.65rem;
}

.matrix-container {
    max-height: 360px;
    overflow: auto;
    border: 1px dashed #35526d;
    border-radius: 8px;
    padding: 0.72rem;
    background: #0a131f;
}

.matrix-grid {
    display: grid;
    gap: 0.35rem;
}

.matrix-cell {
    text-align: center;
    padding: 0.45rem;
    font-size: 0.88rem;
}

.status {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.8rem;
}

.status-ok {
    color: var(--ok);
}

.status-error {
    color: var(--err);
}

.io-stack {
    display: grid;
    gap: 0.8rem;
}

.label-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 0.42rem;
}

.inline-actions {
    display: flex;
    gap: 0.45rem;
}

.inline-btn.mini {
    padding: 0.35rem 0.6rem;
    font-size: 0.78rem;
}

.primary-btn {
    background: rgba(87, 197, 255, 0.18);
    border-color: var(--accent);
    font-weight: 700;
}

.primary-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.dictionary {
    padding: 1rem;
    max-height: calc(100vh - 2rem);
    position: sticky;
    top: 1rem;
    display: flex;
    flex-direction: column;
}

.dictionary h2 {
    margin: 0;
    font-size: 1rem;
    font-family: "Chakra Petch", sans-serif;
    color: var(--accent-strong);
}

.dictionary p {
    margin: 0.42rem 0 0.8rem;
    color: var(--text-dim);
    font-size: 0.8rem;
}

.table-wrap {
    overflow: auto;
    border: 1px solid #264157;
    border-radius: 8px;
    background: #090f18;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

th,
td {
    padding: 0.45rem;
    border-bottom: 1px solid #23384a;
    text-align: left;
}

th {
    position: sticky;
    top: 0;
    background: #122033;
    color: var(--accent-strong);
}

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

#viz-panel {
    padding: 1rem;
}

#viz-panel h2 {
    margin: 0 0 0.8rem;
    font-size: 1rem;
    font-family: "Chakra Petch", sans-serif;
    color: var(--accent-strong);
}

.viz-content {
    display: grid;
    gap: 0.8rem;
}

.viz-step {
    background: rgba(12, 19, 28, 0.86);
    border: 1px solid #274258;
    border-left: 3px solid var(--accent);
    border-radius: 8px;
    padding: 0.7rem;
}

.viz-step h3 {
    margin: 0 0 0.5rem;
    color: #dff4ff;
    font-size: 0.9rem;
}

.viz-step p {
    margin: 0;
    color: var(--text-dim);
    font-size: 0.8rem;
}

.render-matrix {
    --cols: 1;
    display: grid;
    grid-template-columns: repeat(var(--cols), minmax(30px, auto));
    gap: 0.35rem;
    width: max-content;
    border-left: 2px solid var(--accent);
    border-right: 2px solid var(--accent);
    padding: 0.32rem 0.48rem;
    margin: 0.3rem 0;
}

.render-cell {
    min-width: 30px;
    text-align: center;
    color: #d8ebff;
    font-size: 0.82rem;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(3, 6, 9, 0.75);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rem;
    z-index: 30;
}

.modal-card {
    width: min(720px, 100%);
    background: #0d1521;
    border: 1px solid #35516d;
    border-radius: 10px;
    padding: 1rem;
}

.modal-card h2 {
    margin: 0 0 0.45rem;
    color: var(--accent-strong);
    font-family: "Chakra Petch", sans-serif;
}

.modal-card p {
    margin: 0 0 0.7rem;
    color: var(--text-dim);
    font-size: 0.82rem;
    line-height: 1.45;
}

.modal-actions {
    margin-top: 0.7rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.55rem;
}

.hidden {
    display: none !important;
}

@media (max-width: 1180px) {
    .layout {
        grid-template-columns: 1fr;
    }

    .dictionary {
        position: static;
        max-height: none;
    }
}

@media (max-width: 860px) {
    .app-shell {
        width: calc(100% - 1rem);
        margin: 0.5rem auto;
    }

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

    .top-actions {
        flex-direction: row;
    }

    .typewriter {
        width: 100%;
    }
}
