:root {
    --bg: #f3f4f6;
    --panel: #ffffff;
    --border: #d4d7dd;
    --text: #111827;
    --muted: #6b7280;
    --primary: #111827;
    --primary-text: #ffffff;
    --button: #ffffff;
    --button-border: #c7ccd4;
    --editor-bg: #fbfbfc;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: "Trebuchet MS", Verdana, sans-serif;
    overflow-x: auto;
}

.app-shell {
    width: min(1500px, calc(100% - 32px));
    min-width: 980px;
    margin: 16px auto;
    display: grid;
    grid-template-columns: minmax(420px, 0.92fr) minmax(460px, 1.08fr);
    gap: 16px;
    align-items: start;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
}

.editor-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.emulator-panel {
    position: sticky;
    top: 16px;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.panel-header h1,
.panel-header h2 {
    margin: 0;
    font-size: 20px;
    font-weight: 700;
}

.editor-panel-header {
    flex-direction: column;
    align-items: flex-start;
}

.editor-panel-header h1 {
    font-size: 17px;
}

.toolbar {
    display: flex;
    gap: 3px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.editor-panel-header .toolbar {
    width: 100%;
    justify-content: flex-start;
}

button {
    border: 1px solid var(--button-border);
    border-radius: 8px;
    padding: 10px 14px;
    background: var(--button);
    color: var(--text);
    font: inherit;
    cursor: pointer;
}

.toolbar button {
    padding: 4px 7px;
    font-size: 11px;
    line-height: 1.1;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--primary-text);
}

.btn-secondary {
    background: var(--button);
}

.filename-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    color: var(--muted);
    font-size: 14px;
}

.filename-row input {
    width: 140px;
    padding: 9px 12px;
    border: 1px solid var(--button-border);
    border-radius: 8px;
    background: #ffffff;
    color: var(--text);
    font-family: "Lucida Console", Consolas, monospace;
}

.filename-send-button {
    padding: 6px 10px;
    font-size: 12px;
    line-height: 1.1;
}

.run-options {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.option-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
    font-size: 14px;
    user-select: none;
}

.option-toggle input {
    width: 16px;
    height: 16px;
    margin: 0;
}

#basic-source-code {
    width: 100%;
    flex: 0 0 auto;
    min-height: 180px;
    height: clamp(220px, 34vh, 360px);
    border: none;
    outline: none;
    resize: vertical;
    padding: 18px;
    background: var(--editor-bg);
    color: var(--text);
    font-family: "Lucida Console", Consolas, monospace;
    font-size: 14px;
    line-height: 1.55;
}

.status-bar {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: 13px;
}

.status-bar .status-error {
    color: #b42318;
}

.status-bar .status-success {
    color: #0f766e;
}

.emulator-status {
    padding: 6px 10px;
    border: 1px solid var(--button-border);
    border-radius: 999px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    background: #ffffff;
    transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.emulator-status-busy {
    background: #b42318;
    border-color: #b42318;
    color: #ffffff;
}

.emulator-status-ready {
    background: #0f766e;
    border-color: #0f766e;
    color: #ffffff;
}

.emulator-status-error {
    background: #991b1b;
    border-color: #991b1b;
    color: #ffffff;
}

.emulator-shell {
    background: #000000;
    aspect-ratio: 4 / 3;
    min-height: 320px;
}

#emulator-frame {
    width: 100%;
    height: 100%;
    border: none;
    display: block;
}

.hidden {
    display: none !important;
}

.token-editor-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(17, 24, 39, 0.48);
    backdrop-filter: blur(4px);
}

.token-editor-window {
    width: min(1120px, 100%);
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 32px 64px rgba(15, 23, 42, 0.24);
    overflow: hidden;
}

.token-editor-header {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    padding: 18px 20px;
    border-bottom: 1px solid var(--border);
}

.token-editor-heading h2 {
    margin: 0 0 6px;
    font-size: 18px;
}

.token-editor-heading p {
    margin: 0;
    max-width: 720px;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

.token-editor-header-actions,
.token-editor-footer-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: flex-start;
}

.token-editor-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 20px;
    overflow: auto;
    background: #f8fafc;
}

.token-editor-row {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 14px;
}

.token-editor-row-error {
    border-color: #b42318;
    box-shadow: 0 0 0 1px rgba(180, 35, 24, 0.12);
}

.token-editor-row-hidden {
    border-color: #93c5fd;
    background: #f5f9ff;
}

.token-editor-row-head {
    display: grid;
    grid-template-columns: 132px 112px 136px minmax(0, 1fr);
    gap: 12px;
    align-items: start;
}

.token-editor-field {
    display: flex;
    flex-direction: column;
    gap: 5px;
    color: var(--muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.token-editor-field input,
.token-editor-bytes {
    width: 100%;
    border: 1px solid var(--button-border);
    border-radius: 9px;
    background: #fbfbfc;
    color: var(--text);
    font-family: "Lucida Console", Consolas, monospace;
}

.token-editor-field input {
    padding: 9px 10px;
    font-size: 13px;
}

.token-editor-record-length {
    display: flex;
    align-items: center;
    min-height: 39px;
    padding: 0 10px;
    border: 1px dashed var(--button-border);
    border-radius: 9px;
    background: #f8fafc;
    color: var(--text);
    font-family: "Lucida Console", Consolas, monospace;
    font-size: 13px;
}

.token-editor-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 39px;
    padding: 0 10px;
    border: 1px dashed var(--button-border);
    border-radius: 9px;
    background: #f8fafc;
    color: var(--text);
    font-size: 13px;
    text-transform: none;
    letter-spacing: 0;
}

.token-editor-toggle input {
    width: auto;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    accent-color: #1d4ed8;
}

.token-editor-source {
    min-height: 39px;
    padding: 9px 10px;
    border: 1px solid var(--button-border);
    border-radius: 9px;
    background: #f8fafc;
    color: var(--muted);
    font-family: "Lucida Console", Consolas, monospace;
    font-size: 12px;
    line-height: 1.4;
    white-space: nowrap;
    overflow-x: auto;
}

.token-editor-decoded {
    color: var(--text);
    background: #f1f5f9;
}

.token-editor-decoded-error {
    color: #b42318;
    border-color: #f0c4be;
    background: #fff7f6;
}

.token-editor-bytes {
    padding: 11px 12px;
    font-size: 13px;
    line-height: 1.5;
}

.token-editor-footer {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: center;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
    background: #ffffff;
}

.token-editor-footer-copy {
    display: flex;
    flex-direction: column;
    gap: 4px;
    color: var(--muted);
    font-size: 12px;
}

#token-editor-status.status-error {
    color: #b42318;
}

#token-editor-status.status-success {
    color: #0f766e;
}

@media (max-width: 1100px) {
    .emulator-panel {
        position: static;
    }

    #basic-source-code {
        min-height: 180px;
        height: clamp(220px, 32vh, 320px);
    }
}

@media (max-width: 760px) {
    .app-shell {
        width: min(1500px, calc(100% - 16px));
        margin: 8px auto;
        gap: 12px;
    }

    .panel-header,
    .status-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar {
        justify-content: stretch;
    }

    .toolbar button {
        width: 100%;
    }

    .filename-row {
        flex-direction: column;
        align-items: stretch;
    }

    .filename-row input {
        width: 100%;
    }

    .option-toggle {
        margin-left: 0;
    }

    .run-options {
        margin-left: 0;
        width: 100%;
        justify-content: flex-start;
    }

    .token-editor-overlay {
        padding: 12px;
    }

    .token-editor-window {
        max-height: calc(100vh - 24px);
    }

    .token-editor-header,
    .token-editor-footer {
        flex-direction: column;
        align-items: stretch;
    }

    .token-editor-row-head {
        grid-template-columns: 1fr;
    }

    #basic-source-code {
        min-height: 170px;
        height: clamp(200px, 30vh, 280px);
    }
}
