* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    margin: 0;
}

body {
    background: var(--bg-0);
    color: var(--text);
    font-family: monospace;
}

button {
    width: 42px;
    height: 42px;
    border: none;
    border-radius: var(--radius-md);
    background: var(--button-bg);
    color: var(--button-text);
    font-size: 18px;
    font-family: monospace;
    cursor: pointer;
    box-shadow: var(--button-shadow), var(--button-inner-shadow);
    transition: transform 0.05s, background 0.1s;
}

button:hover {
    background: var(--button-hover-bg);
}

button:active {
    transform: translateY(3px);
    box-shadow: var(--button-active-shadow), var(--button-active-inner-shadow);
}

img {
    width: 250px;
    height: auto;
    border-radius: var(--radius-md);
    border: 2px solid var(--border-strong);
}

a {
    color: var(--link-text);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.code {
    font-family: monospace;
    font-size: 13px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 2px 6px;
}

/* ===== App Shell ===== */
.skip-link {
    position: absolute;
    top: -100%;
    left: 0;
    z-index: 9999;
    padding: 0.5rem 1rem;
    background: #000;
    color: #fff;
    text-decoration: none;
}

.skip-link:focus {
    top: 0;
    left: 0;
    outline: 2px solid #fbbf24;
    outline-offset: 2px;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

#mobile-nav {
    display: none;
}

#workspace {
    flex: 1;
    display: flex;
    min-height: 0;
}

.panel {
    min-height: 0;
    padding: 14px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: linear-gradient(to bottom, var(--bg-2), var(--panel-bg));
    border-right: 2px solid var(--panel-border);
    box-shadow: inset 0 0 20px var(--panel-shadow-faint);
}

#left,
#center,
#right {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    min-width: 350px;
    gap: 16px;
}

.section {
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.section-header {
    font-size: 11px;
    letter-spacing: 2px;
    color: var(--section-header-text);
    padding: 6px 2px;
    border-bottom: 1px solid var(--border-soft);
    text-transform: uppercase;
}

/* ===== I/O and Display Areas ===== */
#output-wrap {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex-shrink: 0;
}

#text-display,
#num-display,
#grid-display {
    background: linear-gradient(to bottom, var(--display-bg-start), var(--surface));
    border: 2px solid var(--border-strong);
    border-radius: var(--radius-md);
    padding: 12px;
    box-shadow: inset 0 0 12px rgba(0, 0, 0, 0.9), 0 0 12px var(--display-shadow-color);
    position: relative;
}

#output-row {
    display: flex;
    gap: 12px;
    max-width: 400px;
}

#text-display,
#num-display {
    flex: 1;
    height: 40px;
    color: var(--accent);
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#grid-display {
    aspect-ratio: 1;
    background-color: var(--grid-bg);
    max-height: 375px;
    max-width: 375px;
    overflow: auto;
}

#pixel-grid {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(32, 1fr);
    grid-template-rows: repeat(32, 1fr);
    gap: 1px;
}

.pixel {
    background: var(--pixel-bg);
}

.pixel.active {
    background: var(--pixel-active-bg);
}

#input-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto;
    padding: 12px;
    background: var(--input-bg);
    border-radius: var(--radius-xl);
    border: 2px solid var(--border);
    flex-shrink: 0;
    max-width: 400px;
}

#dpad-wrap {
    width: 120px;
    height: 120px;
    position: relative;
}

#dpad-wrap button {
    position: absolute;
    white-space: nowrap;
    overflow: hidden;
}

#btn-up {
    top: 0;
    left: 40px;
}

#btn-dwn {
    bottom: 0;
    left: 40px;
}

#btn-lft {
    left: 0;
    top: 40px;
}

#btn-rgt {
    right: 0;
    top: 40px;
}

#ctr-btns-wrap {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-top: 40px;
    flex-shrink: 0;
}

#rgt-btns-wrap {
    position: relative;
    width: 120px;
    height: 80px;
    flex-shrink: 0;
}

#ctr-btn-a,
#ctr-btn-b {
    position: absolute;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    font-weight: bold;
    color: var(--button-text);
    border: 1px solid rgba(255, 255, 255, 0.14);
    background: var(--control-button-bg);
    box-shadow: 0 5px 0 rgba(0, 0, 0, 0.28), inset 0 1px 1px rgba(255, 255, 255, 0.18), 0 0 14px rgba(255, 255, 255, 0.08);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
    white-space: nowrap;
    overflow: hidden;
}

#ctr-btn-a:hover,
#ctr-btn-b:hover {
    background: var(--control-button-hover-bg);
}

#ctr-btn-a {
    right: 0;
    top: 0;
}

#ctr-btn-b {
    left: 0;
    bottom: 0;
}

/* ===== Controls and Status ===== */
#control-wrap {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 14px;
    background: linear-gradient(to bottom, var(--control-bg-start), var(--control-bg-end));
    border: 2px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: inset 0 0 10px var(--control-shadow);
    flex-shrink: 0;
}

#control-buttons {
    display: flex;
    gap: 10px;
}

#control-buttons button {
    flex: 1;
    min-height: 0;
    height: 46px;
    font-size: 14px;
    letter-spacing: 0.5px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
}

#control-buttons .material-symbols-outlined {
    display: block;
    font-size: 1.1em;
    line-height: 1;
}

#speed-control {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#speed-header {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: var(--muted);
}

#program-speed-slider {
    width: 100%;
    appearance: none;
    height: 18px;
    border-radius: 999px;
    background: transparent;
    outline: none;
    margin: 0;
    padding: 0;
    display: block;
}

#program-speed-slider::-webkit-slider-runnable-track {
    height: 8px;
    border-radius: 999px;
    background: var(--slider-track-bg);
}

#program-speed-slider::-webkit-slider-thumb {
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--slider-thumb-bg);
    cursor: pointer;
    border: 2px solid var(--slider-thumb-border);
    margin-top: -5px;
}

#program-speed-slider::-moz-range-track {
    height: 8px;
    border-radius: 999px;
    background: var(--slider-track-bg);
}

#program-speed-slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--slider-thumb-bg);
    cursor: pointer;
    border: 2px solid var(--slider-thumb-border);
    margin-top: -5px;
}

#program-speed-slider::-ms-track {
    width: 100%;
    height: 8px;
    color: transparent;
    background: transparent;
    border-color: transparent;
}

#program-speed-slider::-ms-fill-lower,
#program-speed-slider::-ms-fill-upper {
    background: var(--slider-track-bg);
    border-radius: 999px;
}

#program-speed-slider::-ms-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--slider-thumb-bg);
    border: 2px solid var(--slider-thumb-border);
    margin-top: 0;
}

#cpu-status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: var(--status-bar-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    color: var(--muted);
}

#cpu-status-text {
    font-weight: bold;
}

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

#cpu-status-text:not(.status-running) {
    color: var(--danger);
}

#control-wrap,
#flags-panel,
#stack-panel {
    flex-shrink: 0;
}

#flags-panel,
#stack-panel {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    background: linear-gradient(to bottom, var(--surface), var(--surface-2));
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: inset 0 0 8px var(--panel-shadow-small);
}

#flags-wrap,
#stack-display-wrap {
    display: flex;
    gap: 12px;
}

.flag-box,
.stack-box {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px;
    border-radius: var(--radius-sm);
    transition: border-color 0.15s, box-shadow 0.15s, color 0.15s;
}

.flag-box {
    background: linear-gradient(to bottom, var(--surface), var(--surface-2));
    border: 2px solid var(--border);
}

.flag-box.active {
    border-color: var(--accent-amber);
    box-shadow: 0 0 10px var(--flag-active-shadow-color);
    color: var(--accent-amber);
}

.flag-box:not(.active) {
    color: var(--flag-inactive-text);
}

.flag-label {
    font-size: 12px;
    letter-spacing: 1px;
}

.flag-value {
    font-size: 20px;
    font-weight: bold;
}

.stack-box {
    color: var(--accent);
    font-size: 14px;
    font-weight: bold;
    background: linear-gradient(to bottom, var(--surface), var(--surface-2));
    border: 2px solid var(--border);
}

/* ===== Memory and Registers ===== */
#memory-display-wrap {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    gap: 12px;
    overflow: hidden;
}

#reg-file-display,
#data-memory-display {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    max-height: 300px;
    background: var(--memory-panel-bg);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px;
    scrollbar-width: thin;
    scrollbar-color: rgba(128, 128, 128, 0.4) transparent;
    overscroll-behavior: contain;
}

#reg-file-display::-webkit-scrollbar,
#data-memory-display::-webkit-scrollbar {
    width: 6px;
}

#reg-file-display::-webkit-scrollbar-track,
#data-memory-display::-webkit-scrollbar-track {
    background: transparent;
}

#reg-file-display::-webkit-scrollbar-thumb,
#data-memory-display::-webkit-scrollbar-thumb {
    border: 1px solid transparent;
    border-radius: 999px;
    background: rgba(128, 128, 128, 0.4);
    background-clip: padding-box;
}

#reg-file-display::-webkit-scrollbar-thumb:hover,
#data-memory-display::-webkit-scrollbar-thumb:hover {
    background: rgba(128, 128, 128, 0.65);
    background-clip: padding-box;
}

#reg-file-display {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    align-content: start;
}

#data-memory-display {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    align-content: start;
}

.reg-cell,
.mem-cell {
    background: linear-gradient(to bottom, var(--surface), var(--cell-bg-end));
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px;
    text-align: center;
    box-shadow: inset 0 0 4px var(--panel-shadow-small);
    min-width: 0;
}

.reg-cell {
    color: var(--accent-blue);
    font-size: 11px;
}

.mem-cell {
    color: var(--accent-red);
    font-size: 10px;
}

.reg-bin,
.mem-bin {
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1;
    gap: 1px;
    margin-top: 2px;
}

.bin-top,
.bin-bottom {
    font-size: 10px;
    letter-spacing: 1px;
}

/* ===== Editor Panel ===== */
.editor-shell {
    display: flex;
    width: 100%;
    height: 100%;
    min-height: 0;
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--editor-bg);
    box-shadow: inset 0 0 12px var(--editor-shadow-color);
}

.editor-gutter {
    flex: 0 0 75px;
    padding: 18px 2px;
    color: var(--editor-gutter-text);
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    text-align: right;
    background: var(--editor-gutter-bg);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    overflow-x: auto;
    scrollbar-width: none;
    user-select: none;
    box-sizing: border-box;
}

.editor-gutter-line {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3px;
    align-items: center;
    height: 1.6em;
    line-height: 1.6;
    white-space: nowrap;
}

.editor-gutter-line .gutter-line-number,
.editor-gutter-line .gutter-instruction-number {
    display: block;
    text-align: right;
}

.editor-gutter-line .gutter-line-number {
    color: var(--editor-gutter-line-number);
}

.editor-gutter-line .gutter-instruction-number {
    color: var(--editor-gutter-instruction-number);
    padding-left: 2px;
}

.editor-gutter.hide-line-numbers {
    display: none;
}

.editor-gutter::-webkit-scrollbar {
    display: none;
}

.editor-text-area {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
}

.editor-highlight {
    position: absolute;
    inset: 0;
    margin: 0;
    padding: 18px;
    color: var(--code-text);
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    white-space: pre;
    overflow: hidden;
    pointer-events: none;
    box-sizing: border-box;
}

.editor-highlight-line {
    display: block;
    min-height: 1.6em;
}

.editor-highlight-line-current {
    background: color-mix(in srgb, var(--code-text) 10%, transparent);
}

.syntax-opcode {
    color: var(--syntax-opcode);
    font-weight: 700;
}

.syntax-register {
    color: var(--syntax-register);
}

.syntax-number {
    color: var(--syntax-number);
}

.syntax-comment {
    color: var(--syntax-comment);
    font-style: italic;
}

#code-editor {
    width: 100%;
    height: 100%;
    resize: none;
    background: transparent;
    color: transparent;
    -webkit-text-fill-color: transparent;
    caret-color: var(--code-text);
    outline: none;
    padding: 18px;
    font-family: monospace;
    font-size: 15px;
    line-height: 1.6;
    overflow-y: auto;
    flex-grow: 1;
    min-height: 0;
    box-sizing: border-box;
    position: relative;
    z-index: 2;
    white-space: pre;
    scrollbar-width: thin;
    scrollbar-color: rgba(128, 128, 128, 0.4) transparent;
    overscroll-behavior: contain;
}

#code-editor::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

#code-editor::-webkit-scrollbar-track {
    background: transparent;
}

#code-editor::-webkit-scrollbar-thumb {
    border: 2px solid transparent;
    border-radius: 999px;
    background: rgba(128, 128, 128, 0.4);
    background-clip: padding-box;
}

#code-editor::-webkit-scrollbar-thumb:hover {
    background: rgba(128, 128, 128, 0.65);
    background-clip: padding-box;
}



/* ===== Bottom Panel ===== */
#bottom-panel {
    display: flex;
    flex-direction: column;
    border-top: 2px solid var(--bottom-border);
    background: var(--bg-1);
    flex-shrink: 0;
}

@media (max-width: 920px) {
    #bottom-panel {
        display: none;
        position: fixed;
        left: 12px;
        right: 12px;
        bottom: 18px;
        z-index: 1100;
        border-radius: 16px;
        overflow: hidden;
        border: 1px solid var(--panel-border);
        box-shadow: 0 -12px 28px rgba(0, 0, 0, 0.22);
    }

    #bottom-panel.open {
        display: flex;
    }

    #bottom-panel.open #bottom-toolbar {
        display: none;
    }
}

#bottom-toolbar {
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 12px;
    gap: 10px;
}

#bottom-toolbar>button,
#bottom-buttons>button {
    width: auto;
    height: auto;
    min-height: 30px;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    letter-spacing: 0.5px;
    background: var(--bottom-button-bg);
    border: 1px solid var(--bottom-button-border);
    box-shadow: none;
    color: var(--bottom-button-text);
}

#bottom-toolbar>button:hover,
#bottom-buttons>button:hover {
    background: var(--bottom-button-hover-bg);
}

#bottom-toolbar>button:active,
#bottom-buttons>button:active {
    transform: translateY(1px);
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4);
}

#bottom-buttons {
    display: flex;
    gap: 8px;
}

.repo-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
}

.github-icon {
    width: 1.1em;
    height: 1.1em;
    display: block;
    flex-shrink: 0;
}

#problems-panel {
    display: none;
    height: 180px;
    overflow: auto;
    border-top: 1px solid var(--border);
    background: var(--problems-panel-bg);
}

#problems-toggle.has-problems {
    color: var(--problems-toggle-text);
    border-color: var(--problem-error-border);
}

.problem {
    padding: 8px 10px;
    border-left: 3px solid transparent;
    color: var(--problems-text);
    border-bottom: 1px solid var(--problems-border);
}

.problem.error {
    border-left-color: var(--problem-error-border);
    color: var(--problem-error-text);
}

.problem.warning {
    border-left-color: var(--problem-warning-border);
    color: var(--problem-warning-text);
}

#bottom-panel.open #problems-panel {
    display: block;
}

#mobile-problems-button {
    display: none;
    width: 100%;
    min-height: 40px;
    margin-top: auto;
}

@media (max-width: 920px) {
    #app {
        height: 100dvh;
    }

    #workspace {
        display: block;
        padding-bottom: 0;
    }

    #mobile-nav~#workspace {
        padding-bottom: 0;
    }

    #left,
    #center,
    #right {
        display: none;
        min-width: 0;
        width: 100%;
        min-height: 100dvh;
        height: 100dvh;
        padding: 12px 12px 24px;
    }

    #left.active,
    #center.active,
    #right.active {
        display: flex;
    }

    #mobile-nav {
        position: fixed;
        left: 10px;
        right: 10px;
        bottom: 10px;
        z-index: 1000;
        display: block;
        pointer-events: auto;
        overflow: hidden;
        height: 22px;
    }

    #mobile-nav.closed {
        height: 22px;
    }

    #mobile-nav.open {
        height: auto;
        max-height: min(72vh, 420px);
    }

    #mobile-nav-content {
        display: none;
        background: linear-gradient(to bottom, var(--bg-2), var(--bg-1));
        border: 1px solid var(--panel-border);
        border-radius: 18px 18px 12px 12px;
        overflow: hidden;
        box-shadow: 0 -10px 28px rgba(0, 0, 0, 0.28);
    }

    #mobile-nav.open #mobile-nav-content {
        display: block;
    }

    #mobile-nav.closed #mobile-nav-content {
        display: none;
    }

    #mobile-nav-handle {
        width: 58px;
        height: 14px;
        margin: 0 auto 6px;
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.12);
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.08);
        cursor: pointer;
        position: relative;
        z-index: 2;
    }

    #mobile-nav-handle span {
        display: block;
        width: 34px;
        height: 4px;
        border-radius: 999px;
        background: var(--bottom-button-text);
        opacity: 0.75;
    }

    #mobile-nav-buttons {
        display: flex;
        gap: 8px;
        justify-content: space-between;
        padding: 12px 12px 8px;
    }

    .mobile-nav-panel-button,
    .mobile-nav-action-button,
    .mobile-editor-problems-button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-height: 34px;
        height: auto;
        width: auto;
        max-width: none;
        padding: 8px 12px;
        border-radius: 10px;
        border: 1px solid var(--bottom-button-border);
        background: var(--bottom-button-bg);
        color: var(--bottom-button-text);
        font-size: 11px;
        letter-spacing: 0.5px;
        line-height: 1.1;
        box-shadow: none;
        white-space: nowrap;
        transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
    }

    .mobile-nav-panel-button {
        flex: 0 0 auto;
        min-height: 42px;
        min-width: 0;
        border-radius: 12px;
    }

    .mobile-nav-panel-button.active {
        background: var(--button-hover-bg);
        border-color: var(--accent);
    }

    .mobile-nav-action-button {
        min-width: 0;
        flex: 0 0 auto;
        border-radius: 999px;
    }

    .mobile-nav-action-button:hover,
    .mobile-nav-panel-button:hover,
    .mobile-editor-problems-button:hover {
        background: var(--bottom-button-hover-bg);
    }

    .mobile-nav-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        padding: 0 12px 12px;
        align-items: center;
        justify-content: center;
    }

    #mobile-problems-button,
    .mobile-editor-problems-button {
        display: none;
        width: 100%;
        min-height: 40px;
        margin-top: auto;
    }

    #bottom-panel {
        display: none;
        background: transparent;
        border-top: none;
        box-shadow: none;
    }

    #bottom-panel.open {
        display: flex;
        position: fixed;
        left: 12px;
        right: 12px;
        bottom: 20px;
        z-index: 1100;
        border-radius: 16px;
        overflow: hidden;
        border: 1px solid var(--panel-border);
        box-shadow: 0 -12px 28px rgba(0, 0, 0, 0.22);
        background: var(--bg-1);
    }

    #bottom-panel.open #problems-panel {
        display: block;
        height: 170px;
        border-top: 1px solid var(--border);
    }

    #right {
        position: relative;
        transition: transform 0.2s ease;
    }

    #right.active .editor-shell,
    #right.active .editor-text-area,
    #right.active #code-editor {
        min-height: clamp(360px, 62vh, 600px);
        height: clamp(360px, 62vh, 600px);
    }

    #app.mobile-sheet-open #right.active,
    #app.mobile-problems-open #right.active {
        transform: translateY(calc(-1 * min(24vh, 170px)));
    }

    #app.mobile-sheet-open #right.active .editor-shell,
    #app.mobile-sheet-open #right.active .editor-text-area,
    #app.mobile-sheet-open #right.active #code-editor,
    #app.mobile-problems-open #right.active .editor-shell,
    #app.mobile-problems-open #right.active .editor-text-area,
    #app.mobile-problems-open #right.active #code-editor {
        min-height: clamp(360px, 72vh, 700px);
        height: clamp(360px, 72vh, 700px);
    }

    #mobile-problems-button,
    .mobile-editor-problems-button {
        display: inline-flex;
    }

    #bottom-toolbar {
        height: auto;
        flex-wrap: wrap;
        justify-content: center;
        padding: 8px 12px 0;
    }

    #bottom-toolbar>button,
    #bottom-buttons>button {
        min-height: 32px;
        padding: 6px 10px;
        font-size: 11px;
    }

    #bottom-toolbar>button {
        width: 100%;
    }

    #bottom-buttons {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    #problems-panel {
        height: 170px;
        border-top: 1px solid var(--border);
    }
}

/* ===== Modal and Documentation ===== */
#modal-overlay,
#diagram-modal-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--modal-overlay-bg);
    z-index: 1000;
}

#modal-overlay.hidden,
#diagram-modal-overlay.hidden {
    display: none;
}

#modal-window,
#diagram-modal-window {
    width: min(900px, 90vw);
    height: min(700px, 85vh);
    display: flex;
    flex-direction: column;
    background: var(--modal-window-bg);
    border: 2px solid var(--modal-window-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: 0 20px 60px var(--modal-window-shadow);
}

#modal-header,
#diagram-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 18px;
    background: var(--modal-header-bg);
    border-bottom: 1px solid var(--border);
}

#modal-title,
#diagram-modal-title {
    font-size: 18px;
    font-weight: bold;
}

#modal-body,
#diagram-modal-body {
    flex: 1;
    overflow: auto;
    padding: 20px;
    color: var(--text);
    line-height: 1.6;
    font-size: 15px;
}

#modal-body,
#diagram-modal-body,
.cpu-diagram-layout>.cpu-diagram-details {
    scrollbar-width: thin;
    scrollbar-color: rgba(128, 128, 128, 0.45) transparent;
}

#modal-body::-webkit-scrollbar,
#diagram-modal-body::-webkit-scrollbar,
.cpu-diagram-layout>.cpu-diagram-details::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

#modal-body::-webkit-scrollbar-track,
#diagram-modal-body::-webkit-scrollbar-track,
.cpu-diagram-layout>.cpu-diagram-details::-webkit-scrollbar-track {
    background: transparent;
}

#modal-body::-webkit-scrollbar-thumb,
#diagram-modal-body::-webkit-scrollbar-thumb,
.cpu-diagram-layout>.cpu-diagram-details::-webkit-scrollbar-thumb {
    border: 2px solid transparent;
    border-radius: 999px;
    background: rgba(128, 128, 128, 0.45);
    background-clip: padding-box;
}

#modal-body::-webkit-scrollbar-thumb:hover,
#diagram-modal-body::-webkit-scrollbar-thumb:hover,
.cpu-diagram-layout>.cpu-diagram-details::-webkit-scrollbar-thumb:hover {
    background: rgba(128, 128, 128, 0.7);
    background-clip: padding-box;
}

#diagram-modal-body {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#modal-body h1,
#modal-body h2,
#modal-body h3,
#modal-body p,
#modal-body pre,
#modal-body table,
#diagram-modal-body h1,
#diagram-modal-body h2,
#diagram-modal-body h3,
#diagram-modal-body p,
#diagram-modal-body pre,
#diagram-modal-body table {
    margin: 0;
}

#modal-body h1,
#diagram-modal-body h1 {
    margin-bottom: 18px;
    color: var(--modal-title-text);
}

#modal-body h2,
.doc-card h2,
.doc-title {
    color: var(--modal-title-highlight);
    font-size: 22px;
}

#modal-body h2,
#diagram-modal-body h2 {
    margin-bottom: 8px;
}

#modal-body h3,
#diagram-modal-body h3,
.doc-card h3 {
    margin: 0 0 8px;
    color: var(--accent-warning);
    font-size: 15px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

#modal-body p,
#diagram-modal-body p,
.doc-card p {
    margin: 10px 0;
    line-height: 1.6;
}

#modal-body pre,
#diagram-modal-body pre,
.doc-code {
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--doc-code-text);
    overflow-x: auto;
}

#modal-body table,
#diagram-modal-body table,
.doc-table {
    width: 100%;
    border-collapse: collapse;
}

#modal-body th,
#modal-body td,
#diagram-modal-body th,
#diagram-modal-body td,
.doc-table th,
.doc-table td {
    padding: 8px;
}

#modal-body th,
#diagram-modal-body th,
.doc-table th {
    text-align: left;
    color: var(--accent-warning);
    background: var(--table-head-bg);
}

#modal-body tr:nth-child(even),
#diagram-modal-body tr:nth-child(even),
.doc-table tr:nth-child(even) {
    background: var(--table-row-alt-bg);
}

.doc-page {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.doc-intro {
    color: var(--muted);
    line-height: 1.6;
}

.doc-card {
    padding: 18px;
    background: linear-gradient(to bottom, var(--modal-card-bg-start), var(--bg-1));
    border: 1px solid var(--modal-card-border);
    border-radius: var(--radius-lg);
    box-shadow: inset 0 0 10px var(--panel-shadow-small), 0 2px 8px var(--modal-window-shadow);
}

.doc-card+.doc-card {
    margin-top: 22px;
}

.doc-card h2 {
    margin: 0 0 12px;
    color: var(--modal-title-text);
}

.doc-card h3 {
    margin: 18px 0 8px;
    color: var(--accent-warning);
}

.doc-card p {
    margin: 10px 0;
}

.doc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 24px;
    flex-wrap: wrap;
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--bottom-border);
}

.doc-header h1,
.doc-header p {
    margin: 0;
}

.doc-header h1 {
    flex: 0 1 auto;
}

.doc-header p {
    flex: 1 1 280px;
}

.doc-title {
    margin: 0;
}

.doc-category,
.doc-badge,
.doc-badge-button {
    padding: 4px 10px;
    border-radius: 999px;
    background: var(--modal-card-badge-bg);
    color: var(--modal-card-badge-text);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    white-space: nowrap;
}

.doc-badge-button {
    display: inline-flex;
    width: auto;
    height: auto;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.doc-badge-button:hover {
    background: var(--modal-card-badge-bg);
    opacity: 0.8;
}

.doc-section {
    margin-top: 18px;
}

.doc-card-header+.doc-section {
    margin-top: 0;
}

.doc-flags,
.doc-tags {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.doc-flag,
.doc-tag {
    padding: 5px 10px;
    border-radius: 999px;
    background: var(--modal-card-tag-bg);
    border: 1px solid var(--modal-card-tag-border);
    color: var(--accent-warning);
    font-weight: bold;
    font-size: 13px;
}

.doc-table {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    margin-top: 10px;
}

.doc-table th {
    background: var(--table-head-bg);
    color: var(--table-head-color);
}

.doc-table td,
.doc-table th {
    padding: 10px;
}

.doc-table tr:not(:last-child) {
    border-bottom: 1px solid var(--border);
}

.io-access {
    display: inline-block;
    min-width: 42px;
    padding: 2px 8px;
    margin-right: 10px;
    border-radius: 999px;
    text-align: center;
    font-size: 11px;
    font-weight: bold;
    color: var(--io-access-text);
}

.io-access.read {
    background: var(--io-access-read-bg);
}

.io-access.write {
    background: var(--io-access-write-bg);
}

.io-access.read-write {
    background: var(--io-access-read-write-bg);
}

.doc-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.doc-card-header h2 {
    margin: 0;
    flex: 1 1 auto;
}

.doc-card-header .doc-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin: 2px 0;
}

.doc-code {
    margin: 0;
}

.settings-section {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 18px 0 0;
}

.settings-meta {
    flex: 1;
    min-width: 220px;
}

.settings-label {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 6px;
    color: var(--text);
}

.settings-description {
    font-size: 13px;
    line-height: 1.5;
    color: var(--muted);
}

.settings-checkbox {
    width: 20px;
    height: 20px;
    accent-color: var(--accent-blue);
    flex: 0 0 auto;
}

.settings-shortcuts {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.settings-reset-button {
    align-self: flex-start;
    width: auto;
    height: auto;
    min-height: 34px;
    padding: 7px 12px;
    border: 1px solid var(--bottom-button-border);
    border-radius: var(--radius-sm);
    background: var(--bottom-button-bg);
    color: var(--bottom-button-text);
    box-shadow: none;
    font-size: 12px;
}

.settings-reset-button:hover {
    background: var(--bottom-button-hover-bg);
}

.settings-shortcut-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.settings-shortcut-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-height: 34px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border-soft);
}

.settings-shortcut {
    width: auto;
    min-width: 110px;
    height: auto;
    min-height: 32px;
    padding: 6px 10px;
    border: 1px solid var(--bottom-button-border);
    border-radius: var(--radius-sm);
    background: var(--bottom-button-bg);
    color: var(--bottom-button-text);
    box-shadow: none;
    font-size: 12px;
}

.settings-shortcut:hover {
    background: var(--bottom-button-hover-bg);
}

.settings-select {
    min-width: 220px;
    max-width: 100%;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-family: monospace;
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.settings-select:focus {
    border-color: var(--accent-blue);
    box-shadow: 0 0 0 3px rgba(0, 187, 255, 0.14);
}

.cpu-diagram-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(220px, 280px);
    grid-template-rows: minmax(0, 1fr);
    gap: 16px;
    flex: 1;
    min-height: 0;
    align-items: stretch;
}

.cpu-diagram-page {
    flex: 1;
    min-height: 0;
}

.diagram-container {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

#cpu-diagram-canvas {
    display: block;
    width: 100%;
    aspect-ratio: 20 / 11;
    flex: 1;
    min-height: 0;
    cursor: pointer;
}

.cpu-diagram-layout>.doc-card {
    min-height: 0;
}

.cpu-diagram-component-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 14px;
    min-width: 0;
}

.cpu-diagram-component-button {
    width: auto;
    max-width: 100%;
    min-height: 36px;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    white-space: normal;
}

.cpu-diagram-component-button:hover,
.cpu-diagram-component-button[aria-pressed="true"] {
    border-color: var(--accent-blue);
}

.cpu-diagram-details h2 {
    margin: 14px 0 8px;
}

.cpu-diagram-layout>.cpu-diagram-details {
    margin-top: 0;
    min-width: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
}

#cpu-diagram-details-text p {
    color: var(--muted);
    margin: 0 0 0.9em;
    line-height: 1.6;
    overflow-wrap: anywhere;
}

#cpu-diagram-details-text p:last-child {
    margin-bottom: 0;
}

@media (max-width: 700px) {
    .cpu-diagram-layout {
        display: flex;
        flex-direction: column;
        flex: none;
        min-height: 0;
    }

    .cpu-diagram-page {
        flex: none;
        min-height: auto;
    }

    .diagram-container {
        flex: none;
    }

    #cpu-diagram-canvas {
        flex: none;
        height: auto;
    }

    .cpu-diagram-layout>.cpu-diagram-details {
        flex: none;
        max-height: min(30vh, 220px);
        overflow-y: auto;
    }
}

.swal2-popup {
    background: var(--swal-bg) !important;
    color: var(--text) !important;
    border: 2px solid var(--border-strong) !important;
    border-radius: 12px;
}

.toast-popup {
    border: 2px solid var(--border-strong) !important;
    border-radius: var(--radius-md) !important;
    box-shadow:
        0 8px 30px rgba(0, 0, 0, 0.45),
        inset 0 0 10px rgba(255, 255, 255, 0.02);
    font-family: monospace !important;
}

.toast-popup .swal2-title {
    font-size: 14px !important;
    font-weight: normal !important;
}

.swal2-actions {
    gap: 12px;
}

.swal2-confirm,
.swal2-deny,
.swal2-cancel {
    min-width: 140px;
    min-height: 42px;
    height: auto;
    font-family: monospace;
    padding: 10px 18px;
}

.swal2-confirm,
.swal2-cancel {
    font-size: 14px;
}

.swal2-confirm {
    background: var(--swal-confirm-bg) !important;
}

.swal2-deny {
    font-size: 12px;
}

.swal2-cancel,
.swal2-deny {
    background: var(--swal-secondary-bg) !important;
}