:root {
    --cosmos-space-xs: 4px;
    --cosmos-space-sm: 8px;
    --cosmos-space-md: 16px;
    --cosmos-space-lg: 24px;
    --cosmos-control-height: 32px;
    --cosmos-bar-height: 52px;
    --cosmos-dock-width: 208px;
    --cosmos-dock-width-sm: 184px;
    --cosmos-r-xs: 4px;
    --cosmos-r-sm: 8px;
    --cosmos-r-md: 12px;
    --cosmos-r-lg: 18px;
    --cosmos-r-xl: 26px;
    --cosmos-r-soft: 30%;
    --cosmos-r-balanced: 22%;
    --cosmos-r-firm: 16%;
    --cosmos-r-structural: 8%;
    --cosmos-bg: hsl(0, 0%, 5%);
    --cosmos-surface-1: hsl(0, 0%, 9%);
    --cosmos-surface-2: hsl(0, 0%, 13%);
    --cosmos-surface-3: hsl(0, 0%, 18%);
    --cosmos-surface-4: hsl(0, 0%, 24%);
    --cosmos-border: hsla(0, 0%, 100%, 0.15);
    --cosmos-border-subtle: hsla(0, 0%, 100%, 0.08);
    --cosmos-border-active: hsla(0, 0%, 100%, 0.40);
    --cosmos-text-dim: hsl(0, 0%, 28%);
    --cosmos-text-secondary: hsl(0, 0%, 46%);
    --cosmos-text-primary: hsl(0, 0%, 76%);
    --cosmos-text-bright: hsl(0, 0%, 96%);
    --cosmos-ease: cubic-bezier(0.25, 0.1, 0.25, 1.0);
    --cosmos-t-state: 100ms var(--cosmos-ease);
    --cosmos-t-surface: 180ms var(--cosmos-ease);
}

body {
    font-family: ui-monospace, 'Cascadia Code', 'Segoe UI Mono', 'Roboto Mono', monospace;
    font-size: 13px;
    color: var(--cosmos-text-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    background: var(--cosmos-bg);
}

#app {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    background: var(--cosmos-bg);
}

#viewport {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
}

#render-canvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

#manipulation-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--cosmos-bar-height);
    background: hsla(0, 0%, 9%, 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--cosmos-border);
    border-radius: var(--cosmos-r-md) var(--cosmos-r-md) 0 0;
    display: flex;
    align-items: center;
    padding: 0 var(--cosmos-space-lg);
    gap: var(--cosmos-space-md);
    z-index: 10;
}

.bar-group {
    display: flex;
    align-items: center;
    gap: var(--cosmos-space-sm);
    flex-shrink: 0;
}

.bar-group-inner {
    display: flex;
    align-items: center;
    gap: var(--cosmos-space-sm);
}

.bar-sep {
    width: 1px;
    height: 18px;
    background: var(--cosmos-border);
    flex-shrink: 0;
}

.bar-label {
    font-size: 9px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--cosmos-text-dim);
    white-space: nowrap;
    user-select: none;
}

.bar-sublabel {
    font-size: 8px;
    color: var(--cosmos-text-dim);
    opacity: 0.6;
    white-space: nowrap;
    user-select: none;
    letter-spacing: 0.4px;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 2px;
    background: var(--cosmos-surface-4);
    border-radius: 1px;
    cursor: pointer;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--cosmos-text-primary);
    cursor: pointer;
    border: none;
    box-shadow: 0 0 0 2px var(--cosmos-surface-1);
    transition: background var(--cosmos-t-state), transform var(--cosmos-t-state);
}

input[type="range"]:hover::-webkit-slider-thumb {
    background: var(--cosmos-text-bright);
    transform: scale(1.15);
    box-shadow: 0 0 0 4px var(--cosmos-border-subtle);
}

input[type="range"]:active::-webkit-slider-thumb {
    transform: scale(0.9);
}

input[type="range"]::-moz-range-thumb {
    width: 10px;
    height: 10px;
    border: none;
    border-radius: 50%;
    background: var(--cosmos-text-primary);
    cursor: pointer;
    box-shadow: 0 0 0 2px var(--cosmos-surface-1);
}

input[type="range"]::-moz-range-track {
    height: 2px;
    background: var(--cosmos-surface-4);
    border-radius: 1px;
}

#manipulation-bar input[type="range"] {
    width: 110px;
}

#manipulation-bar output {
    font-size: 11px;
    color: var(--cosmos-text-primary);
    min-width: 26px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.toggle-btn {
    height: var(--cosmos-control-height);
    padding: 0 10px;
    border-radius: var(--cosmos-r-sm);
    background: var(--cosmos-surface-2);
    color: var(--cosmos-text-secondary);
    font-size: 9px;
    font-family: inherit;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    border: 1px solid var(--cosmos-border);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    transition: background var(--cosmos-t-state), color var(--cosmos-t-state), border-color var(--cosmos-t-state), transform var(--cosmos-t-state);
}

.toggle-btn:hover {
    background: var(--cosmos-surface-3);
    color: var(--cosmos-text-primary);
    transform: scale(0.98);
}

.toggle-btn:active {
    background: var(--cosmos-surface-4);
    transform: scale(0.96);
}

.toggle-btn.active {
    color: var(--cosmos-text-bright);
    border-color: var(--cosmos-border-active);
    background: var(--cosmos-surface-3);
}

#deep-access {
    width: var(--cosmos-control-height);
    padding: 0;
    text-align: center;
    font-size: 15px;
    letter-spacing: 0;
    flex-shrink: 0;
}

#deep-group {
    position: relative;
}

.bayer-panel {
    position: absolute;
    bottom: calc(100% + 20px);
    right: 0;
    background: var(--cosmos-surface-1);
    border: 1px solid var(--cosmos-border-active);
    border-radius: var(--cosmos-r-md);
    padding: 10px var(--cosmos-space-md);
    display: none;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--cosmos-space-sm);
    z-index: 20;
    opacity: 0;
    transform: translateY(10px);
    box-shadow: 0 8px 24px hsla(0, 0%, 0%, 0.4);
    white-space: nowrap;
}

.bayer-panel select {
    background: var(--cosmos-surface-2);
    border: 1px solid var(--cosmos-border);
    border-radius: var(--cosmos-r-xs);
    padding: 4px 8px;
    color: var(--cosmos-text-primary);
    font-size: 10px;
    font-family: inherit;
    cursor: pointer;
    outline: none;
}

.bayer-panel select:hover {
    border-color: var(--cosmos-border-active);
}

.dock-tab {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 44px;
    background: var(--cosmos-surface-2);
    border: 1px solid var(--cosmos-border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    font-weight: 500;
    letter-spacing: 0.5px;
    color: var(--cosmos-text-dim);
    cursor: pointer;
    z-index: 15;
    user-select: none;
    transition: background var(--cosmos-t-state), color var(--cosmos-t-state), border-color var(--cosmos-t-state);
}

.dock-tab:hover {
    background: var(--cosmos-surface-3);
    color: var(--cosmos-text-secondary);
    border-color: var(--cosmos-border-active);
}

.dock-tab-left {
    left: 0;
    border-left: none;
    border-radius: 0 var(--cosmos-r-sm) var(--cosmos-r-sm) 0;
}

.dock-tab-right {
    right: 0;
    border-right: none;
    border-radius: var(--cosmos-r-sm) 0 0 var(--cosmos-r-sm);
}

.dock {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: var(--cosmos-surface-1);
    border: 1px solid var(--cosmos-border);
    border-radius: var(--cosmos-r-lg);
    padding: var(--cosmos-space-md);
    display: none;
    flex-direction: column;
    gap: var(--cosmos-space-sm);
    z-index: 20;
    opacity: 0;
    box-shadow: 0 12px 40px hsla(0, 0%, 0%, 0.5);
}

.dock-left {
    left: 20px;
    width: var(--cosmos-dock-width);
    transform: translateX(-20px) translateY(-50%);
}

.dock-right {
    right: 20px;
    width: var(--cosmos-dock-width-sm);
    transform: translateX(20px) translateY(-50%);
}

.dock-title {
    font-size: 8px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: var(--cosmos-text-dim);
    padding-bottom: var(--cosmos-space-xs);
    border-bottom: 1px solid var(--cosmos-border-subtle);
    margin-bottom: var(--cosmos-space-xs);
}

.dock-row {
    display: flex;
    align-items: center;
    gap: var(--cosmos-space-sm);
}

.dock input[type="color"] {
    width: 28px;
    height: 22px;
    border: 1px solid var(--cosmos-border);
    border-radius: var(--cosmos-r-xs);
    background: transparent;
    cursor: pointer;
    padding: 1px 2px;
    transition: border-color var(--cosmos-t-state);
}

.dock input[type="color"]:hover {
    border-color: var(--cosmos-border-active);
}

.dock input[type="range"] {
    flex: 1;
}

.dock output {
    font-size: 10px;
    color: var(--cosmos-text-primary);
    min-width: 28px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.dock .toggle-btn {
    width: 100%;
    text-align: center;
}

#svg-slots {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    margin-bottom: var(--cosmos-space-xs);
}

.svg-slot {
    aspect-ratio: 1;
    background: var(--cosmos-surface-2);
    border: 1px dashed var(--cosmos-border);
    border-radius: var(--cosmos-r-balanced);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    color: var(--cosmos-text-dim);
    cursor: pointer;
    position: relative;
    overflow: hidden;
    user-select: none;
    transition: background var(--cosmos-t-state), border-color var(--cosmos-t-state), transform var(--cosmos-t-state);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

.svg-slot:hover {
    background-color: var(--cosmos-surface-3);
    border-color: var(--cosmos-border-active);
    border-style: solid;
    transform: scale(0.98);
}

.svg-slot.filled {
    border-style: solid;
    border-color: var(--cosmos-border-active);
}

.svg-slot input[type="file"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    font-size: 0;
    z-index: 1;
}

.slot-num {
    font-size: 9px;
    color: var(--cosmos-text-dim);
    pointer-events: none;
    user-select: none;
    line-height: 1;
}

.svg-slot.filled .slot-num {
    position: absolute;
    bottom: 4px;
    right: 5px;
    font-size: 8px;
    color: var(--cosmos-text-bright);
    opacity: 0.45;
    text-shadow: 0 1px 3px hsla(0, 0%, 0%, 0.9);
    z-index: 3;
}

.slot-clear {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: hsla(0, 0%, 0%, 0.65);
    border: 1px solid hsla(0, 0%, 100%, 0.15);
    color: var(--cosmos-text-secondary);
    font-size: 11px;
    line-height: 1;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 4;
    padding: 0;
    transition: background var(--cosmos-t-state), color var(--cosmos-t-state);
}

.slot-clear:hover {
    background: hsla(0, 0%, 0%, 0.85);
    color: var(--cosmos-text-bright);
}

.svg-slot.filled:hover .slot-clear {
    display: flex;
}

.slot-luminance-hint {
    grid-column: 1 / -1;
    height: 2px;
    background: linear-gradient(to right, hsl(0, 0%, 12%), hsl(0, 0%, 88%));
    border-radius: 1px;
    opacity: 0.22;
    margin-top: 2px;
}

.interval-group {
    display: none;
    align-items: center;
    gap: var(--cosmos-space-sm);
    padding-left: var(--cosmos-space-sm);
    opacity: 0;
}

.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 30;
    opacity: 0;
    pointer-events: none;
}

.overlay.is-visible {
    pointer-events: all;
}

.overlay-scrim {
    position: absolute;
    inset: 0;
    background: hsla(0, 0%, 0%, 0.90);
    pointer-events: none;
}

.overlay-surface {
    background: var(--cosmos-surface-1);
    border: 1px solid var(--cosmos-border-active);
    border-radius: var(--cosmos-r-xl);
    background-clip: padding-box;
    box-shadow: 0 0 0 1px hsla(0, 0%, 100%, 0.05), 0 20px 60px hsla(0, 0%, 0%, 0.6);
    padding: var(--cosmos-space-lg);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--cosmos-space-md);
    min-width: 300px;
    max-width: 380px;
    width: 90vw;
    position: relative;
    z-index: 1;
    box-shadow: 0 20px 60px hsla(0, 0%, 0%, 0.6);
}

.overlay-prompt {
    font-size: 12px;
    color: var(--cosmos-text-primary);
    letter-spacing: 0.3px;
}

.overlay-or {
    font-size: 9px;
    color: var(--cosmos-text-dim);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.overlay-actions {
    display: flex;
    gap: var(--cosmos-space-sm);
    flex-wrap: wrap;
    justify-content: center;
}

.action-btn {
    height: var(--cosmos-control-height);
    padding: 0 14px;
    border-radius: var(--cosmos-r-sm);
    background: var(--cosmos-surface-2);
    color: var(--cosmos-text-primary);
    font-size: 10px;
    font-family: inherit;
    font-weight: 500;
    letter-spacing: 0.4px;
    border: 1px solid var(--cosmos-border);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: relative;
    background-clip: padding-box;
    box-shadow: inset 0 0 0 0.5px hsla(0, 0%, 100%, 0.02);
    white-space: nowrap;
    user-select: none;
    text-transform: uppercase;
    transition: background var(--cosmos-t-state), border-color var(--cosmos-t-state), transform var(--cosmos-t-state);
}

.action-btn:hover {
    background: var(--cosmos-surface-3);
    border-color: var(--cosmos-border-active);
    transform: scale(0.98);
}

.action-btn:active {
    background: var(--cosmos-surface-4);
    transform: scale(0.96);
}

.action-btn input[type="file"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    font-size: 0;
    width: 100%;
}

#aspect-toggle {
    display: flex;
    gap: var(--cosmos-space-xs);
}

.aspect-btn {
    padding: 0 12px;
    height: 26px;
    border: 1px solid var(--cosmos-border);
    border-radius: var(--cosmos-r-xs);
    font-size: 9px;
    font-family: inherit;
    font-weight: 500;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    color: var(--cosmos-text-secondary);
    background: transparent;
    cursor: pointer;
    user-select: none;
    transition: color var(--cosmos-t-state), border-color var(--cosmos-t-state), background var(--cosmos-t-state);
}

.aspect-btn:hover {
    color: var(--cosmos-text-primary);
    background: var(--cosmos-surface-2);
}

.aspect-btn.active {
    color: var(--cosmos-text-bright);
    border-color: var(--cosmos-border-active);
    background: var(--cosmos-surface-2);
}

.export-indicator {
    position: absolute;
    top: var(--cosmos-space-md);
    right: var(--cosmos-space-md);
    font-size: 9px;
    font-family: inherit;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.9px;
    color: var(--cosmos-text-dim);
    cursor: pointer;
    z-index: 15;
    padding: 5px 9px;
    border-radius: var(--cosmos-r-xs);
    border: 1px solid transparent;
    user-select: none;
    transition: color var(--cosmos-t-state), border-color var(--cosmos-t-state);
}

.export-indicator:hover {
    color: var(--cosmos-text-secondary);
    border-color: var(--cosmos-border);
}

.export-panel {
    position: absolute;
    top: 48px;
    right: var(--cosmos-space-md);
    background: var(--cosmos-surface-1);
    border: 1px solid var(--cosmos-border-active);
    border-radius: var(--cosmos-r-md);
    padding: var(--cosmos-space-md);
    display: none;
    flex-direction: column;
    gap: var(--cosmos-space-sm);
    z-index: 25;
    min-width: 160px;
    opacity: 0;
    box-shadow: 0 12px 36px hsla(0, 0%, 0%, 0.5);
}

.export-video-row {
    display: flex;
    align-items: center;
    gap: var(--cosmos-space-sm);
}

.record-timer {
    font-size: 10px;
    color: hsl(0, 65%, 55%);
    font-variant-numeric: tabular-nums;
    min-width: 36px;
    animation: timer-pulse 1s infinite alternate var(--cosmos-ease);
}

@keyframes timer-pulse {
    0% { opacity: 0.5; }
    100% { opacity: 1; }
}

#export-video.recording {
    border-color: hsl(0, 50%, 35%);
    color: hsl(0, 65%, 55%);
}

body.drag-active #viewport::after {
    content: "";
    position: absolute;
    inset: 6px;
    border: 1px dashed var(--cosmos-border-active);
    border-radius: var(--cosmos-r-md);
    pointer-events: none;
    z-index: 5;
}

body.recording #viewport {
    outline: 1px solid hsl(0, 55%, 30%);
    outline-offset: -1px;
}

#viewport[data-media-type]::before {
    content: attr(data-media-type);
    position: absolute;
    bottom: calc(var(--cosmos-bar-height) + 10px);
    left: 14px;
    font-size: 9px;
    font-family: ui-monospace, 'Cascadia Code', 'Segoe UI Mono', 'Roboto Mono', monospace;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1.4px;
    color: var(--cosmos-text-dim);
    user-select: none;
    pointer-events: none;
    z-index: 5;
}

#media-overlay.is-visible {
    opacity: 1;
}

@media (max-width: 1024px) {
    .dock-left { left: var(--cosmos-space-sm); }
    .dock-right { right: var(--cosmos-space-sm); }
}

/* TABLET & LANDSCAPE MOBILE */
@media (max-width: 900px) and (orientation: landscape) {
    :root {
        --cosmos-bar-height: 48px;
    }
    #manipulation-bar {
        height: var(--cosmos-bar-height);
        overflow-x: auto;
        flex-direction: row;
        justify-content: flex-start;
        padding: 0 var(--cosmos-space-md);
        gap: var(--cosmos-space-lg);
        -webkit-overflow-scrolling: touch;
    }
    #manipulation-bar::-webkit-scrollbar { display: none; }
    .bar-group { flex-shrink: 0; }
    #manipulation-bar input[type="range"] { width: 80px; }
}

/* PORTAL MOBILE (Stacking) */
@media (max-width: 600px) and (orientation: portrait) {
    :root {
        --cosmos-bar-height: auto;
        --cosmos-space-lg: 12px;
        --cosmos-space-md: 8px;
    }

    #manipulation-bar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        flex-direction: column;
        align-items: stretch;
        padding: var(--cosmos-space-md);
        gap: var(--cosmos-space-sm);
        border-radius: 16px 16px 0 0;
    }

    .bar-sep { display: none; }

    .bar-group {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: var(--cosmos-space-sm);
        width: 100%;
    }

    #grid-group {
        display: grid;
        grid-template-columns: 35px 1fr 35px;
        align-items: center;
    }

    #transform-group {
        display: flex;
        flex-direction: column;
        gap: var(--cosmos-space-sm);
    }

    #transform-group .toggle-btn {
        width: 100%;
    }

    #transform-group .bar-group-inner {
        display: grid;
        grid-template-columns: 35px 1fr 35px;
        align-items: center;
        gap: var(--cosmos-space-sm);
    }

    #deep-group {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }

    #deep-group .toggle-btn {
        width: 100%;
    }

    .bar-label { font-size: 8px; }
    #manipulation-bar output { font-size: 10px; }

    #manipulation-bar input[type="range"] {
        width: 100%;
    }

    .dock { 
        width: calc(100vw - 24px) !important;
        left: 12px !important;
        right: 12px !important;
        bottom: 220px;
        max-height: 40vh;
    }

    .overlay-surface {
        width: 90vw;
        padding: var(--cosmos-space-md);
    }
}

/* Safe area fixes */
@supports (padding: env(safe-area-inset-bottom)) {
    #manipulation-bar {
        padding-bottom: calc(var(--cosmos-space-md) + env(safe-area-inset-bottom));
    }
}
