@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&family=VT323&display=swap');

:root {
    --color-bg: #030a05;
    --color-terminal: #ffffff;
    --color-terminal-dark: #e0e0e0;
    --color-terminal-dim: #a0a0a0;
    --color-terminal-light: #f5f5f5;
    --color-accent: #ff6b35;
    --color-danger: #ff3b30;
    --font-game: 'VT323', monospace;
    --font-ui: 'Share Tech Mono', monospace;
    --cell-size: 32px;
}

@supports (font-display: swap) {
    body {
        font-display: swap;
    }
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
}

body {
    background-color: var(--color-bg);
    color: var(--color-terminal);
    font-family: var(--font-ui);
    height: 100dvh;
    overflow: hidden;
    display: flex;
    position: relative;
}

a {
    color: #e0e0e0;
}

.crt-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
    background-size: 100% 4px, 6px 100%;
    z-index: 999;
    pointer-events: none;
    opacity: 0.85;
}

.crt-glow {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-shadow: inset 0 0 80px rgba(255, 255, 255, 0.15);
    pointer-events: none;
    z-index: 1000;
}

@keyframes crt-flicker {
    0% { opacity: 0.985; }
    50% { opacity: 0.995; }
    100% { opacity: 0.985; }
}

body::after {
    content: " ";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgba(18, 16, 16, 0.1);
    opacity: 0;
    z-index: 1001;
    pointer-events: none;
    animation: crt-flicker 0.15s infinite;
}

@media (prefers-reduced-motion: reduce) {
    body::after {
        animation: none;
    }

    .modal-content {
        animation: none;
    }

    #sidebar {
        transition: none;
    }

    .tool-btn,
    .control-btn,
    .modal-btn,
    .menu-item {
        transition: none;
    }
}

#app-container {
    display: block;
    width: 100vw;
    height: 100dvh;
    overflow: hidden;
    position: relative;
}

#sidebar {
    --sidebar-drag-x: -2px;
    --sidebar-drag-y: 0px;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 260px;
    height: 100%;
    background-color: rgba(3, 10, 5, 0.95);
    border-left: 2px solid var(--color-terminal);
    border-right: 2px solid var(--color-terminal);
    display: flex;
    flex-direction: column;
    z-index: 10;
    padding: 15px;
    box-shadow: 5px 0 15px rgba(255, 255, 255, 0.1);
    overflow-y: auto;
    transform: translate3d(var(--sidebar-drag-x, 0), var(--sidebar-drag-y, 0), 0);
    transition: transform 0.18s ease;
    will-change: transform;
}

#sidebar::-webkit-scrollbar,
#level-info-box::-webkit-scrollbar {
    width: 6px;
}
#sidebar::-webkit-scrollbar-track,
#level-info-box::-webkit-scrollbar-track {
    background: var(--color-bg);
}
#sidebar::-webkit-scrollbar-thumb,
#level-info-box::-webkit-scrollbar-thumb {
    background:    var(--color-terminal-dim);
    border-radius: 3px;
}
#sidebar::-webkit-scrollbar-thumb:hover,
#level-info-box::-webkit-scrollbar-thumb:hover {
    background: var(--color-terminal);
}

.brand {
    font-family: var(--font-game);
    font-size: 2.2rem;
    text-align: center;
    margin-bottom: 5px;
    text-shadow: 0 0 10px var(--color-terminal);
    border-bottom: 2px dashed var(--color-terminal);
    padding-bottom: 10px;
    cursor: grab;
    touch-action: none;
}

#sidebar.is-sidebar-dragging {
    transition: none;
}

#sidebar.is-sidebar-dragging .brand {
    cursor: grabbing;
}

.section-title {
    font-size: 0.85rem;
    letter-spacing: 2px;
    color: var(--color-terminal-dim);
    margin: 15px 0 8px 0;
    text-transform: uppercase;
}

#level-select {
    background: var(--color-bg);
    color: var(--color-terminal);
    border: 1px solid var(--color-terminal);
    font-family: var(--font-ui);
    padding: 8px;
    width: 100%;
    outline: none;
    cursor: pointer;
    margin-bottom: 10px;
    text-shadow: 0 0 2px var(--color-terminal);
}
#level-select option {
    background: var(--color-bg);
    color: var(--color-terminal);
}

#level-info-box {
    border: 1px dashed var(--color-terminal-dim);
    padding: 10px;
    font-size: 0.85rem;
    line-height: 1.4;
    margin-bottom: 15px;
    min-height: 80px;
    background: rgba(200, 200, 200, 0.1);
    overflow-y: auto;
}

.level-title {
    font-weight: bold;
    margin-bottom: 4px;
    color: var(--color-terminal-light);
}

#level-desc {
    white-space: pre-line;
}

.tools-section {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

.tool-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-bottom: 15px;
}

.tool-btn {
    background: var(--color-bg);
    color: var(--color-terminal);
    border: 1px solid var(--color-terminal);
    padding: 6px 4px;
    font-family: var(--font-ui);
    font-size: 0.7rem;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    min-height: 60px;
    overflow: hidden;
    width: 100%;
}

.tool-btn span {
    white-space: nowrap;
    overflow: hidden;
    max-width: 100%;
}

.tool-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.3);
}

.tool-btn.active {
    background: var(--color-terminal);
    color: var(--color-bg);
    box-shadow: 0 0 12px var(--color-terminal);
}

.tool-char {
    font-family: var(--font-game);
    font-size: 1.8rem;
    line-height: 1;
    margin-bottom: 2px;
}

.tips-box .temporary {
    font-size: 0.72rem;
    color: var(--color-terminal-dim);
    line-height: 1.35;
    margin-bottom: 15px;
    border-top: 1px dashed var(--color-terminal-dim);
    padding-top: 8px;
    overflow-y: auto;
    flex-grow: 1;
}

.control-panel {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-btn {
    background: var(--color-bg);
    color: var(--color-terminal);
    border: 1px solid var(--color-terminal);
    padding: 10px;
    cursor: pointer;
    font-family: var(--font-ui);
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.2s;
}

.control-icon {
    line-height: 1;
}

.control-label {
    line-height: 1.2;
}

.control-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

#btn-play.active {
    background: #ffffff;
    color: #030a05;
    box-shadow: 0 0 10px #ffffff;
}

#btn-reset {
    border-color: var(--color-accent);
    color: var(--color-accent);
}
#btn-reset:hover {
    background: rgba(255, 107, 53, 0.2);
}

.control-btn--danger {
    border-color: var(--color-danger);
    color: var(--color-danger);
}

#canvas-container {
    position: absolute;
    inset: 0;
    overflow: hidden;
    width: 100%;
    height: 100%;
    z-index: 0;
}

#canvas-container canvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
    contain: strict;
}

.hud-overlay {
    position: absolute;
    top: 15px;
    right: 15px;
    background: rgba(3, 10, 5, 0.85);
    border: 1px solid var(--color-terminal);
    padding: 10px 15px;
    font-family: var(--font-ui);
    font-size: 0.9rem;
    pointer-events: auto;
    cursor: pointer;
    text-shadow: 0 0 3px var(--color-terminal);
    z-index: 5;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.hud-overlay::before {
    content: '';
    display: none;
}

.hud-overlay.is-collapsed {
    padding: 6px 10px;
    min-width: 0;
}

.hud-overlay.is-collapsed::before {
    content: '≡';
    display: block;
    color: var(--color-terminal-light);
    font-size: 1.2rem;
    line-height: 1;
}

.hud-overlay.is-collapsed .hud-row {
    display: none;
}

.hud-row {
    display: flex;
    justify-content: space-between;
    gap: 20px;
}

.hud-val {
    color: var(--color-terminal-light);
    font-weight: bold;
}

#hud-status {
    color: var(--color-accent);
}

#context-menu {
    position: absolute;
    background: rgba(3, 10, 5, 0.95);
    border: 2px solid var(--color-terminal);
    padding: 10px;
    font-family: var(--font-ui);
    font-size: 0.85rem;
    z-index: 100;
    display: none;
    box-shadow: 0 0 15px rgba(255, 255, 255, 0.3);
    pointer-events: auto;
    white-space: nowrap;
}

.menu-title {
    border-bottom: 1px dashed var(--color-terminal);
    margin-bottom: 8px;
    padding: 4px 6px;
    font-size: 0.8rem;
    color: var(--color-terminal-dim);
}

.menu-item {
    padding: 4px 6px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    gap: 15px;
    transition: background 0.2s;
    white-space: nowrap;
}

.menu-item:hover {
    background: rgba(255, 255, 255, 0.15);
    color: var(--color-terminal-light);
}

.menu-shortcut {
    color: var(--color-terminal-dim);
}

.menu-item--danger {
    color: var(--color-danger);
}

.game-modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 12px;
    background: rgba(3, 10, 5, 0.88);
    z-index: 200;
    display: none;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
}

.game-modal--overlay {
    z-index: 300;
}

.noscript-warning {
    position: fixed;
    inset: 0;
    z-index: 2147483647;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: #000;
    color: #fff;
    font-family: system-ui, sans-serif;
    font-size: 1.1rem;
    line-height: 1.5;
    text-align: center;
    padding: 24px;
    box-sizing: border-box;
}

.noscript-warning p {
    margin: 0;
    max-width: 28em;
}

.modal-content {
    background: var(--color-bg);
    border: 3px double var(--color-terminal);
    padding: 40px;
    max-width: 450px;
    width: 90%;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
    box-sizing: border-box;
    text-align: center;
    box-shadow: 0 0 30px rgba(255, 255, 255, 0.4);
    animation: modal-glow 2s infinite alternate;
}

@keyframes modal-glow {
    from { box-shadow: 0 0 20px rgba(255, 255, 255, 0.3); }
    to { box-shadow: 0 0 40px rgba(255, 255, 255, 0.6); }
}

.modal-title {
    font-family: var(--font-game);
    font-size: 3rem;
    margin-top: -7.5px;
    margin-bottom: 15px;
    color: var(--color-terminal-light);
    text-shadow: 0 0 10px var(--color-terminal);
}

.modal-desc {
    font-size: 1rem;
    margin-bottom: 25px;
    line-height: 1.5;
}

.modal-btn {
    background: var(--color-bg);
    color: var(--color-terminal);
    border: 1px solid var(--color-terminal);
    padding: 10px 20px;
    font-family: var(--font-ui);
    cursor: pointer;
    text-transform: uppercase;
    font-weight: bold;
    transition: all 0.2s;
}

.modal-btn:hover {
    background: var(--color-terminal);
    color: var(--color-bg);
    box-shadow: 0 0 10px var(--color-terminal);
}

.modal-btn--danger {
    border-color: var(--color-danger);
    color: var(--color-danger);
}

.modal-btn--danger:hover {
    background: var(--color-danger);
    color: var(--color-terminal-light);
    box-shadow: 0 0 10px var(--color-danger);
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
    width: 100%;
}

.modal-actions--single {
    justify-content: center;
}

.modal-actions .modal-btn {
    flex: 1 1 8.5rem;
    min-width: 0;
    box-sizing: border-box;
}

.grid-preview {
    position: absolute;
    border: 1px dashed rgba(255, 255, 255, 0.4);
    background: rgba(255, 255, 255, 0.05);
    pointer-events: none;
    display: none;
    z-index: 8;
    color: rgba(255, 255, 255, 0.7);
    font-family: var(--font-ui);
    font-size: 10px;
    align-items: center;
    justify-content: center;
    text-shadow: 0 0 2px #000;
}

.debug-overlay {
    position: fixed;
    right: 10px;
    bottom: 10px;
    z-index: 2147483647;
    color: #ffffff;
    font-family: var(--font-ui);
    font-size: 12px;
    line-height: 1.35;
    white-space: pre;
    pointer-events: none;
    text-shadow: 0 0 2px #000;
}

.debug-overlay--2 {
    white-space: normal;
    background: rgba(0, 0, 0, 0.55);
    padding: 6px 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.debug-overlay__chart-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 4px;
    font-family: var(--font-ui);
    white-space: nowrap;
}

.debug-overlay__chart-label {
    width: 3.2em;
    flex-shrink: 0;
}

.debug-overlay__chart-value {
    width: 2.8em;
    text-align: right;
    flex-shrink: 0;
}

.debug-spark {
    display: block;
    flex-shrink: 0;
    background: rgba(0, 0, 0, 0.35);
}

.debug-overlay__text {
    white-space: pre;
    line-height: 1.35;
}

#selection-box {
    position: absolute;
    border: 1px dashed var(--color-terminal);
    background: rgba(255, 255, 255, 0.08);
    pointer-events: none;
    display: none;
    z-index: 9;
}

.tips-box {
    font-size: 0.72rem;
    color: var(--color-terminal-dim);
    line-height: 1.35;
    margin-top: 10px;
    margin-bottom: 8px;
    border-top: 1px dashed var(--color-terminal-dim);
    padding-top: 8px;
}

@media (max-width: 768px) {
    body {
        flex-direction: column;
        height: 100dvh;
        overflow: hidden;
    }

    #app-container {
        height: 100dvh;
    }

    #sidebar {
        --sidebar-drag-x: 0px;
        --sidebar-drag-y: 2px;
        top: auto;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 200px;
        max-height: 200px;
        border-left: none;
        border-right: none;
        border-top: 2px solid var(--color-terminal);
        border-bottom: 2px solid var(--color-terminal);
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 12px 16px;
        gap: 20px;
        align-items: stretch;
        box-shadow: 0 -5px 15px rgba(255, 255, 255, 0.1);
    }

    .brand {
        font-size: 2.2rem;
        margin-bottom: 0;
        border-bottom: none;
        border-left: 2px dashed var(--color-terminal);
        padding-left: 16px;
        padding-right: 0;
        writing-mode: vertical-rl;
        text-orientation: mixed;
        transform: rotate(180deg);
        align-self: stretch;
        display: flex;
        justify-content: center;
        align-items: center;
        min-width: 32px;
        flex-shrink: 0;
    }

    .levels-group {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex-shrink: 0;
        min-width: 150px;
        max-width: 180px;
    }

    .levels-group section {
        min-width: unset !important;
        max-width: unset !important;
        width: 100%;
    }

    #sidebar section {
        flex-shrink: 0;
        min-width: 120px;
        max-width: 160px;
    }

    .section-title {
        font-size: 0.7rem;
        margin: 0 0 4px 0;
        letter-spacing: 1px;
    }

    #level-info-box {
        font-size: 0.75rem;
        width: 180px;
        height: 95px;
        overflow-y: auto;
        margin-bottom: 0;
    }

    .tools-section {
        flex-grow: 0;
        flex-shrink: 0;
        min-width: 340px;
        max-width: unset !important;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-self: stretch;
    }

    .tool-grid {
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: repeat(3, minmax(42px, 1fr));
        gap: 4px;
        margin-bottom: 0;
        margin-top: 0;
        flex: 1;
    }

    .tool-btn {
        min-height: 42px;
        padding: 4px 2px;
        font-size: 0.6rem;
        width: 100%;
    }

    .tool-char {
        font-size: 1.1rem;
        margin-bottom: 0px;
    }

    .tips-box {
        display: none;
    }

    .control-panel {
        margin-top: 0;
        display: grid;
        grid-template-rows: repeat(3, minmax(42px, 1fr));
        flex-shrink: 0;
        min-width: 110px;
        gap: 4px;
        align-self: stretch;
        padding-top: 17px;
    }

    .control-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-height: 42px;
        padding: 4px 2px;
        font-size: 0.6rem;
        letter-spacing: 0;
        width: 100%;
        height: 100%;
    }

    .control-btn .control-icon {
        font-family: var(--font-game);
        font-size: 1.1rem;
    }

    .control-btn .control-label {
        white-space: nowrap;
    }

    #canvas-container {
        height: 100%;
        min-height: 0;
    }
}

.sandbox-io-panel {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.sandbox-io-panel--single {
    grid-template-rows: minmax(42px, 1fr);
}

@media (max-width: 768px) {
    .sandbox-io-panel {
        margin-top: 0;
        display: grid;
        grid-template-rows: repeat(2, minmax(42px, 1fr));
        flex-shrink: 0;
        min-width: 110px;
        gap: 4px;
        align-self: stretch;
        padding-top: 17px;
    }
}

.modal-content--form {
    max-width: 550px !important;
    width: min(90%, 550px) !important;
    text-align: left !important;
}

.modal-content--form .modal-title {
    text-align: center;
}

.form-group {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-group--checkbox {
    flex-direction: row;
    align-items: center;
}

.form-group label {
    font-size: 0.8rem;
    color: var(--color-terminal-dim);
    text-transform: uppercase;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group textarea {
    background: var(--color-bg);
    color: var(--color-terminal);
    border: 1px solid var(--color-terminal);
    font-family: var(--font-ui);
    padding: 8px;
    outline: none;
    font-size: 0.9rem;
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
}

.form-group textarea {
    height: 80px;
    resize: none;
}

#edit-json-text {
    min-height: 320px;
    resize: vertical;
}

.form-row {
    display: flex;
    gap: 12px;
}

.form-row .form-group {
    flex: 1;
    min-width: 0;
}

.form-group-separator {
    text-align: left;
    font-size: 0.8rem;
    color: var(--color-terminal-dim);
    margin: 8px 0;
    border-top: 1px dashed var(--color-terminal-dim);
    padding-top: 8px;
}

#import-file {
    color: var(--color-terminal);
    font-family: var(--font-ui);
    font-size: 0.85rem;
}

#import-movable {
    accent-color: var(--color-terminal);
}

@media (max-width: 420px), (max-height: 720px) {
    .game-modal {
        padding: 10px;
    }

    .modal-content {
        width: 100%;
        padding: 24px;
    }

    .modal-title {
        font-size: 2.4rem;
    }
}

@media (max-width: 420px) {
    .form-row {
        flex-direction: column;
        gap: 0;
    }

    .modal-actions {
        gap: 10px;
    }

    .modal-actions .modal-btn {
        flex-basis: 100%;
    }
}