:root {
    --bg: #000;
    --fg: #fff;
    --hue-shift: 0deg;
    --slider-fill: var(--fg);
    --shadow-inset: none;
    --waveform-line: var(--fg);
    --always-white: #fff;
}

body.light-mode {
    --bg: #fff;
    --fg: #000;
    --slider-fill: var(--fg);
    --shadow-inset: none;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    background: var(--bg);
    color: var(--fg);
    font-family: "SF Mono", "Menlo", "Consolas", "Liberation Mono", monospace;
    font-size: 1rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.page {
    position: relative;
    width: 100%;
    max-width: 480px;
    padding: 10rem 1rem 2rem;
}

.theme-toggle {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 2000;
    width: 3rem;
    height: 3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-size: 1.2rem;
    background: var(--bg);
    border: 2px solid var(--fg);
    color: var(--fg);
}

.processing-warning {
    text-align: center;
    font-size: 0.9rem;
    font-weight: 800;
    margin-bottom: 1rem;
    color: #f00;
    animation: flash-warning 1s infinite linear;
    line-height: 1.3;
    padding: 0 1rem;
}

.section-divider {
    display: none;
    width: 100%;
    border-top: 2px solid var(--fg);
    margin: 1.25rem 0;
}

.page.file-loaded .section-divider { display: block; }

.presets-wrapper {
    margin-bottom: 0.75rem;
}

.preset-select {
    border-color: var(--fg);
    font-style: italic;
    opacity: 0.8;
}

.external-link-notice {
    text-align: center;
    font-size: 1rem;
    margin-bottom: 1.25rem;
    padding: 1rem;
    border: 2px solid var(--fg);
    opacity: 1;
    line-height: 1.4;
    background: var(--bg);
}

.external-link-notice a {
    color: var(--fg);
    text-decoration: underline;
    display: inline-block;
    margin-top: 0.25rem;
    font-weight: 800;
}

.upload-wrapper {
    text-align: center;
    margin-bottom: 1rem;
}

.button-group {
    display: flex;
    gap: 0.75rem;
}

.button-group .upload-button {
    flex: 1;
}

/* === Единая база всех контролов === */
button,
select,
input,
.loading-impact-container {
    appearance: none;
    -webkit-appearance: none;
    border-radius: 0;
    border: 2px solid var(--fg);
    background: var(--bg);
    color: var(--fg);
    box-shadow: var(--shadow-inset);
    font-family: inherit;
    font-size: inherit;
    font-weight: inherit;
    text-transform: inherit;
    letter-spacing: inherit;
    cursor: pointer;
    height: 2.8rem;
    transition: opacity 0.15s;
}

button:hover,
select:hover,
input:hover,
.loading-impact-container:hover {
    opacity: 0.7;
}

button:active,
select:active,
input:active,
.loading-impact-container:active {
    opacity: 0.5;
}

button:disabled,
select:disabled,
input:disabled,
.loading-impact-container:disabled {
    cursor: default;
}

input[type="file"] { display: none; }

.file-name {
    margin-top: 0.75rem;
    word-break: break-all;
    opacity: 0.6;
}

.controls {
    margin-top: 1rem;
    display: none;
    flex-direction: column;
    gap: 0.75rem;
}

.controls-row {
    display: flex;
    gap: 0.75rem;
}

.controls-row select, .full-width-select { flex: 1; min-width: 0; }
.full-width-select { width: 100%; }

select, .upload-button, .play-button, .audio-toggle-btn {
    padding: 0.75rem 1rem;
}

.shader-control { padding: 0.4rem; font-size: 0.9rem; }

.play-controls-group {
    display: none;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1.5rem;
}

.play-controls {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
}

.play-controls > * {
    min-width: 0;
    height: 2.8rem;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#speed-input {
    width: 100%;
    text-align: center;
    cursor: text;
}

#speed-input::placeholder { opacity: 0.4; }

.snail-button {
    position: relative;
    overflow: hidden;
}

.snail-button.active {
    background: var(--fg);
    color: var(--bg);
}

.snail-button.active.is-playing .snail-emoji {
    opacity: 0.85;
}

.snail-button:not(.active) .snail-emoji {
    opacity: 0.25;
}

.snail-speed-text {
    opacity: 1;
}

.snail-emoji {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    pointer-events: none;
}

.snail-speed-text {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.display-section {
    margin-top: 1rem;
    display: flex;
    flex-direction: column;
}

.tabs-nav {
    display: flex;
    gap: -2px; /* overlap borders */
}

.tab-btn {
    flex: 1;
    border-bottom: none !important;
    height: 2.8rem !important;
    padding: 0 !important;
    opacity: 0.5;
}

.tab-btn.active {
    opacity: 1;
    background: var(--fg) !important;
    color: var(--bg) !important;
}

.tabs-content {
    border: 2px solid var(--fg);
    position: relative;
    background: var(--bg);
    aspect-ratio: 1;
    width: 100%;
    overflow: hidden;
}

.images-container {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: relative;
    touch-action: none;
    cursor: ns-resize;
    background: var(--bg);
    user-select: none;
    -webkit-user-select: none;
}

.images-inner {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 1;
}

.tutorial-message {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    text-align: center;
    opacity: 0.7;
    pointer-events: none;
    line-height: 1.2;
}

.tutorial-block {
    display: inline-block;
    max-width: 280px;
}

.warning-flash {
    margin-bottom: 2rem;
    color: #f00;
    font-weight: 900;
    animation: flash-warning 3s ease-in-out infinite;
}

@keyframes flash-warning {
    0%, 100% { color: var(--fg); opacity: 0.4; }
    50% { color: #f00; opacity: 1; }
}

.bg-background {
    position: absolute;
    top: -2px; left: -2px;
    width: calc(100% + 4px);
    height: calc(100% + 4px);
    pointer-events: none;
    z-index: 0;
    overflow: hidden;
}

.bg-background img, .image-wrapper img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    image-rendering: pixelated;
    display: block;
    pointer-events: none;
    -webkit-user-drag: none;
    user-select: none;
}

.image-wrapper { width: 100%; background: transparent; }
.image-wrapper img { height: auto; object-fit: unset; }

.page[data-color-mode="gray"] .image-wrapper img,
.page[data-color-mode="gray"] .bg-background img { filter: grayscale(1); }

.page[data-color-mode="bw"] .image-wrapper img,
.page[data-color-mode="bw"] .bg-background img { filter: grayscale(1) contrast(999%); }

.page[data-color-mode="invert"] .image-wrapper img,
.page[data-color-mode="invert"] .bg-background img { filter: invert(1); }

.page[data-color-mode="hue"] .image-wrapper img,
.page[data-color-mode="hue"] .bg-background img { filter: hue-rotate(var(--hue-shift)); }

.play-marker {
    position: absolute;
    pointer-events: none;
    z-index: 2;
    border: 1px solid var(--fg);
    mix-blend-mode: difference;
}

.bit-info {
    display: none;
    margin-top: 1.5rem;
    line-height: 1.4;
    min-height: 2.2em;
    word-break: break-all;
    white-space: pre-wrap;
    opacity: 0.6;
}

.ascii-history-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    min-width: 0;
}

.ascii-history {
    flex: 1;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0.75rem;
    font-size: 1rem;
    line-height: 1.2;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    opacity: 0.6;
    user-select: text;
}

.perf-info {
    margin-top: 1.5rem;
    line-height: 1.4;
    white-space: pre-wrap;
    opacity: 0.6;
}

.hint { margin-top: 0.5rem; opacity: 0.4; }
.spacer { height: 8rem; }

.loading-impact-container {
    display: flex;
    align-items: center;
    height: 2.8rem;
    position: relative;
}

.slider-wrapper {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    height: 100%;
    overflow: hidden;
}

.slider-filler {
    position: absolute;
    inset: 0;
    background: var(--slider-fill);
    pointer-events: none;
}

.loading-slider {
    width: 100%;
    height: 100%;
    appearance: none;
    background: transparent;
    border: none;
    box-shadow: none;
    position: relative;
    z-index: 2;
    margin: 0;
}

.loading-slider::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 100%;
    background: transparent;
    cursor: pointer;
}

.loading-slider::-moz-range-thumb {
    width: 20px;
    background: transparent;
    border: none;
    cursor: pointer;
}

.knob-text, .slider-wrapper .label {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1;
    pointer-events: none;
    white-space: nowrap;
    color: var(--always-white);
    mix-blend-mode: difference;
}

.knob-text { left: 50%; transform: translate(-50%, -50%); }
.label--left { left: 0.75rem; }
.label--right { right: 0.75rem; }

/* Audio */
.audio-sonification-container {
    margin-top: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.audio-controls-row { display: flex; gap: 0.75rem; align-items: center; }

.audio-controls-row--top {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

.audio-toggle-btn {
    height: 2.8rem;
    padding: 0;
    white-space: nowrap;
}

.audio-toggle-btn.active {
    background: var(--fg);
    color: var(--bg);
}

.audio-toggle-btn:not(.active) {
}

.oscilloscope {
    grid-column: span 2;
    width: 100%;
    height: 2.8rem;
    display: block;
    border: 2px solid var(--fg);
    box-shadow: none;
    background: var(--bg);
}

.audio-control-item {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    height: 2.8rem;
    min-width: 0;
}

.audio-mini-label { font-size: 0.85rem; opacity: 0.5; }

.audio-select {
    flex: 1;
    height: 100%;
    width: 100%;
    padding: 0 0.5rem;
    text-align-last: center;
}

.audio-volume-container { height: 2.8rem; }

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    inset: 0;
    background: rgba(0,0,0,0.8);
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.open { display: flex; }

.modal-content {
    background: var(--bg);
    border: 2px solid var(--fg);
    width: 100%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
}

.modal-header {
    padding: 1rem;
    border-bottom: 2px solid var(--fg);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-close {
    border: none;
    background: none;
    font-size: 1.5rem;
    padding: 0;
    height: auto;
    width: auto;
    color: var(--fg);
}

.modal-body {
    padding: 1rem;
    max-height: 60vh;
    overflow-y: auto;
    font-size: 0.9rem;
    line-height: 1.5;
    text-transform: none;
}

.modal-body p { margin: 0 0 1rem; }
.modal-body b, .modal-body strong { color: var(--fg); }

/* Warning Modal Styling */
.modal--warning .modal-content {
    border-color: #f00;
}

.modal--warning .modal-header {
    border-bottom-color: #f00;
    color: #f00;
}

.modal--warning .modal-close {
    color: #f00;
}

.modal--warning .upload-button {
    border-color: #f00;
    color: #f00;
}

.modal--warning .upload-button:hover {
    background: #f00;
    color: #fff;
}

.images-inner, .bg-background, .bg-background img,
.image-wrapper, .image-wrapper img {
    border: none !important;
    box-shadow: none !important;
    outline: none !important;
}

/* Warning Flash for UI Elements */
.warning-flash-ui {
    animation: flash-ui-red 3s ease-in-out infinite !important;
}

.warning-flash-ui .slider-filler {
    animation: flash-bg-red 3s ease-in-out infinite !important;
}

@keyframes flash-ui-red {
    0%, 100% { color: var(--fg); border-color: var(--fg); opacity: 0.7; }
    50% { color: #f00; border-color: #f00; opacity: 1; }
}

@keyframes flash-bg-red {
    0%, 100% { background: var(--slider-fill); }
    50% { background: #f00; }
}