        :root {
            --bg: #181818;
            --panel: #202020;
            --border: #333;
            --text: #ddd;
            --text-secondary: #aaa;
            --text-primary: #fff;
            --bg-card: #252526;
            --bg-secondary: #1e1e1e;
            --panel-hover: #2a2a2a;
            --accent: #007acc;
            --danger: #d9534f;
            --handle-size: 16px;
            --left-bar-width: 50px;
        }

        /* Light-theme tweak: when host is light and an image is loaded (.workspace.active),
           show a gentle, very-light checkered background (no pure black). */
        :root[data-host-theme="light"] .workspace.active {
            background-color: #fafafa;
            background-image:
                linear-gradient(45deg, rgba(0, 0, 0, 0.03) 25%, rgba(0, 0, 0, 0) 25%),
                linear-gradient(-45deg, rgba(0, 0, 0, 0.03) 25%, rgba(0, 0, 0, 0) 25%),
                linear-gradient(45deg, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0.02) 75%),
                linear-gradient(-45deg, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0.02) 75%);
            background-size: 20px 20px;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
        }

        * {
            box-sizing: border-box;
            outline: none;
        }

        body {
            margin: 0;
            min-height: 100vh;
            /* use min-height to avoid mobile 100vh resize issues */
            width: 100vw;
            background-color: var(--bg);
            color: var(--text);
            font-family: 'Segoe UI', sans-serif;
            display: flex;
            flex-direction: row;
            /* allow normal scrolling behavior on mobile; workspace controls its own overflow */
            user-select: none;
            overscroll-behavior: none;
        }

        /* --- LEFT SIDEBAR --- */
        .left-bar {
            width: var(--left-bar-width);
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            background: var(--panel);
            border-right: 1px solid var(--border);
            display: flex;
            flex-direction: column;
            align-items: center;
            padding-top: 10px;
            gap: 15px;
            z-index: 5000;
            /* above toolbar */
        }

        /* --- MAIN APP WRAPPER --- */
        .main-app {
            flex: 1;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow: hidden;
            min-width: 0;
            padding-top: 50px;
            /* reserve space for fixed toolbar */
            margin-left: var(--left-bar-width);
            /* avoid left-bar overlap */
        }

        /* --- TOP TOOLBAR --- */
        .toolbar {
            height: 50px;
            background: var(--panel);
            border-bottom: 1px solid var(--border);
            display: flex;
            align-items: center;
            padding: 0 15px;
            gap: 10px;
            z-index: 4000;
            /* above modal overlays/drawers */
            position: fixed;
            /* pin to viewport top so it cannot be hidden by viewport chrome changes */
            top: 0;
            left: var(--left-bar-width);
            /* account for left-bar width */
            right: 0;
            flex-shrink: 0;
            overflow-x: auto;
            white-space: nowrap;
        }

        .toolbar::-webkit-scrollbar {
            display: none;
        }

        /* --- FILENAME INFO BAR --- */
        .filename-bar {
            height: 22px;
            background: var(--bg-secondary);
            border-bottom: 1px solid var(--border);
            display: none;
            align-items: center;
            padding: 0 10px;
            font-size: 11px;
            color: var(--text-secondary);
            flex-shrink: 0;
            overflow: hidden;
            cursor: default;
        }
        .filename-bar.visible {
            display: flex;
        }
        .filename-bar-text {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            flex: 1;
            min-width: 0;
        }
        .filename-bar-text.scrolling {
            overflow: visible;
            text-overflow: unset;
            animation: filename-scroll 4s linear forwards;
        }
        @keyframes filename-scroll {
            0% { transform: translateX(0); }
            10% { transform: translateX(0); }
            90% { transform: translateX(var(--scroll-dist)); }
            100% { transform: translateX(var(--scroll-dist)); }
        }

        .icon-btn {
            background: transparent;
            border: 1px solid transparent;
            cursor: pointer;
            padding: 6px;
            border-radius: 4px;
            fill: var(--text);
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .icon-btn:hover {
            background: var(--panel-hover);
            fill: var(--text);
            border-color: var(--border);
        }

        .icon-btn.active {
            background: var(--accent);
            fill: white;
        }

        .icon-btn:disabled {
            opacity: 0.3;
            cursor: default;
        }

        .icon-btn svg {
            width: 24px;
            height: 24px;
        }

        .sep {
            width: 1px;
            height: 24px;
            background: var(--border);
            margin: 0 5px;
            flex-shrink: 0;
        }

        /* --- MERGED INPUT MODULE (Toolbar) --- */
        .inputs-area {
            display: flex;
            align-items: center;
            gap: 0;
            /* Zero gap for merged look */
            font-size: 13px;
            margin-left: auto;
            /* Pushes to right on desktop */
        }

        /* Specific styling for inputs inside the toolbar module */
        .inputs-area input[type="number"] {
            background: var(--panel);
            border: 1px solid var(--border);
            color: var(--text);
            width: 55px;
            height: 30px;
            /* Match button height */
            padding: 4px;
            text-align: center;
            font-size: 12px;
            margin: 0;
            border-radius: 0;
            /* Square by default */
            position: relative;
            z-index: 1;
        }

        /* Round the far left corner (Width) */
        .inputs-area input[type="number"]:first-child {
            border-top-left-radius: 4px;
            border-bottom-left-radius: 4px;
            border-right: none;
            /* Merge with button */
        }

        /* Round the far right corner (Height) */
        .inputs-area input[type="number"]:last-child {
            border-top-right-radius: 4px;
            border-bottom-right-radius: 4px;
            border-left: none;
            /* Merge with button */
        }

        /* The Link Button in the middle */
        .inputs-area #btn-fix-ratio {
            margin: 0;
            padding: 0;
            width: 25px;
            height: 30px;
            border-radius: 0;
            border: 1px solid var(--border);
            background: var(--panel);
            /* Match input bg */
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .inputs-area #btn-fix-ratio:hover {
            background: var(--panel-hover);
        }

        /* Generic Number Input Fallback (for modals, etc) */
        input[type="number"]:not(.inputs-area input) {
            background: var(--panel);
            border: 1px solid var(--border);
            color: var(--text);
            padding: 4px;
            border-radius: 3px;
        }

        /* Global theme-aware defaults for frequently themed UI blocks */
        .btn-close {
            color: var(--text);
            background: transparent;
            border: none;
        }

        /* Make many tool panels, buttons and inputs follow the theme variables */
        .crop-controls-panel,
        .settings-area,
        .preview-area,
        .fmt-desc,
        .btn-secondary,
        .btn-primary,
        .dt-btn,
        .collage-btn,
        .collage-trans-btn,
        .btn-clear-selection,
        .btn-replace,
        input[type="color"],
        .file-info,
        .popup-box,
        .popup-header,
        .popup-body {
            background: var(--panel);
            color: var(--text);
            border-color: var(--border);
        }

        /* Light theme variable overrides (use light backgrounds and dark text) */
        :root[data-host-theme="light"] {
            --bg: #fafafa;
            --panel: #ffffff;
            --bg-card: #ffffff;
            --bg-secondary: #f5f5f5;
            --text: #111111;
            --text-primary: #000000;
            --text-secondary: #333333;
            --border: #e6e6e6;
            --panel-hover: #f2f2f2;
            --preview-bg: #ffffff;
        }

        #status-link {
            transition: fill 0.2s;
        }

        .btn-intact #status-link {
            fill: var(--accent);
        }

        .btn-broken #status-link {
            fill: var(--danger);
        }

        /* --- WORKSPACE --- */
        .workspace {
            flex: 1;
            position: relative;
            overflow: hidden;
            background-color: var(--bg-secondary);
            cursor: default;
            touch-action: none;
        }

        .workspace.active {
            background-image:
                linear-gradient(45deg, var(--bg-card) 25%, transparent 25%),
                linear-gradient(-45deg, var(--bg-card) 25%, transparent 25%),
                linear-gradient(45deg, transparent 75%, var(--bg-card) 75%),
                linear-gradient(-45deg, transparent 75%, var(--bg-card) 75%);
            background-size: 20px 20px;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
            cursor: grab;
        }

        .workspace.active:active {
            cursor: grabbing;
        }

        .empty-state {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            color: var(--text-secondary);
            border: 2px dashed var(--border);
            padding: 40px;
            border-radius: 10px;
            width: 80%;
            cursor: pointer;
        }

        .stage {
            position: absolute;
            transform-origin: 0 0;
            box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
            display: none;
            width: 0;
            height: 0;
        }

        canvas {
            display: block;
            width: 100%;
            height: 100%;
            pointer-events: none;
        }

        /* --- HANDLES --- */
        .handle {
            position: absolute;
            width: var(--handle-size);
            height: var(--handle-size);
            background: var(--text-primary);
            border: 1px solid var(--border);
            z-index: 10;
            box-sizing: border-box;
            border-radius: 2px;
        }

        .handle::after {
            content: '';
            position: absolute;
            top: -15px;
            left: -15px;
            right: -15px;
            bottom: -15px;
        }

        .nw {
            top: 0;
            left: 0;
            cursor: nw-resize;
            transform: translate(-50%, -50%);
        }

        .ne {
            top: 0;
            right: 0;
            cursor: ne-resize;
            transform: translate(50%, -50%);
        }

        .sw {
            bottom: 0;
            left: 0;
            cursor: sw-resize;
            transform: translate(-50%, 50%);
        }

        .se {
            bottom: 0;
            right: 0;
            cursor: se-resize;
            transform: translate(50%, 50%);
        }

        .n {
            top: 0;
            left: 50%;
            cursor: n-resize;
            transform: translate(-50%, -50%);
        }

        .s {
            bottom: 0;
            left: 50%;
            cursor: s-resize;
            transform: translate(-50%, 50%);
        }

        .w {
            top: 50%;
            left: 0;
            cursor: w-resize;
            transform: translate(-50%, -50%);
        }

        .e {
            top: 50%;
            right: 0;
            cursor: e-resize;
            transform: translate(50%, -50%);
        }

        .h-mid {
            background: var(--accent);
            border-color: #fff;
        }

        /* --- MODAL SYSTEM --- */
        .modal-overlay {
            position: fixed;
            top: 50px;
            left: 50px;
            width: calc(100vw - 50px);
            height: calc(100vh - 50px);
            background: var(--bg);
            z-index: 1500;
            display: none;
            flex-direction: row;
            overflow: hidden;
        }

        .modal-overlay.open {
            display: flex;
        }

        .modal-header {
            position: absolute;
            top: 0;
            right: 0;
            width: 320px;
            height: 50px;
            background: var(--panel);
            border-bottom: 1px solid var(--border);
            border-left: 1px solid var(--border);
            display: flex;
            align-items: center;
            padding: 0 15px;
            flex-shrink: 0;
            justify-content: space-between;
            z-index: 11;
        }

        .modal-body {
            flex: 1;
            display: flex;
            flex-direction: row;
            overflow: hidden;
            min-height: 0;
            position: relative;
        }

        .preview-area {
            flex: 1;
            background-color: var(--preview-bg, #111);
            background-image:
                linear-gradient(45deg, rgba(0, 0, 0, 0.12) 25%, transparent 25%),
                linear-gradient(-45deg, rgba(0, 0, 0, 0.12) 25%, transparent 25%),
                linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.12) 75%),
                linear-gradient(-45deg, transparent 75%, rgba(0, 0, 0, 0.12) 75%);
            background-size: 20px 20px;
            position: relative;
            overflow: hidden;
            cursor: grab;
            min-width: 0;
            touch-action: none;
        }

        .preview-area:active {
            cursor: grabbing;
        }

        .settings-area {
            position: absolute;
            top: 0;
            right: 0;
            width: 320px;
            height: 100%;
            flex-shrink: 0;
            background: var(--panel);
            border-left: 1px solid var(--border);
            padding: 60px 20px 20px 20px;
            display: flex;
            flex-direction: column;
            gap: 20px;
            overflow-y: auto;
            box-sizing: border-box;
            z-index: 10;
        }

        .preview-reset-btn {
            position: absolute;
            bottom: 15px;
            right: 15px;
            background: var(--panel-hover);
            color: var(--text);
            border: 1px solid var(--border);
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            z-index: 10;
        }

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

        .control-group label {
            font-size: 12px;
            color: var(--text-secondary);
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        select,
        input[type="text"],
        input[type="range"] {
            background: var(--bg-card);
            border: 1px solid var(--border);
            color: var(--text);
            padding: 10px;
            border-radius: 4px;
            width: 100%;
        }

        input[type="color"] {
            background: var(--bg-card);
            border: 1px solid var(--border);
            width: 100%;
            height: 35px;
            padding: 2px;
        }

        .fmt-desc {
            font-size: 11px;
            color: var(--text-secondary);
            line-height: 1.4;
            background: var(--bg-card);
            padding: 8px;
            border-radius: 4px;
        }

        .fmt-desc strong {
            color: var(--text-primary);
        }

        .btn-primary {
            background: var(--accent);
            color: white;
            border: none;
            padding: 12px;
            border-radius: 4px;
            font-weight: bold;
            cursor: pointer;
            margin-top: auto;
        }

        .btn-secondary {
            background: var(--panel-hover);
            color: white;
            border: none;
            padding: 12px;
            border-radius: 4px;
            cursor: pointer;
        }

        .btn-close {
            background: transparent;
            border: none;
            color: var(--text-primary);
            font-size: 18px;
            cursor: pointer;
        }

        .file-info {
            background: var(--bg-card);
            padding: 10px;
            border-radius: 4px;
            font-size: 13px;
            color: var(--text-secondary);
            border: 1px solid var(--border);
        }

        .info-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
        }

        .val {
            color: var(--text);
            font-weight: bold;
        }

        .layer-el.hidden {
            display: none;
        }

        /* --- MOBILE LAYOUT OVERRIDES --- */
        @media(max-width: 1100px) {
            .modal-overlay {
                position: fixed;
                top: 50px;
                left: 44px;
                width: calc(100vw - 44px);
                height: calc(100vh - 50px);
                flex-direction: column;
            }

            .modal-header {
                position: static;
                width: 100%;
                height: 50px;
                border-left: none;
                border-bottom: 1px solid var(--border);
                z-index: 11;
                flex-shrink: 0;
            }

            .modal-body {
                flex: 1;
                display: flex;
                flex-direction: column;
                height: 100%;
                width: 100%;
                position: relative;
                overflow: hidden;
                min-height: 0;
            }

            .preview-area {
                flex: 1 1 auto;
                min-height: 100px;
                max-height: none !important;
                order: 1;
                position: relative;
                border-bottom: 1px solid var(--border);
            }

            .settings-area {
                position: relative !important;
                width: 100% !important;
                height: auto !important;
                flex: 0 0 auto !important;
                min-height: 0 !important;
                max-height: 30vh !important;
                border-left: none !important;
                border-top: 1px solid var(--border);
                border-bottom: none;
                overflow-y: auto !important;
                overflow-x: hidden !important;
                -webkit-overflow-scrolling: touch;
                padding: 15px !important;
                gap: 12px !important;
                order: 2;
                display: flex !important;
                flex-direction: column !important;
            }

            .control-group {
                flex-shrink: 0 !important;
            }

            .control-group label {
                font-size: 10px !important;
                margin-bottom: 2px !important;
            }

            select,
            input[type="text"],
            input[type="range"] {
                padding: 8px !important;
                font-size: 12px !important;
                margin: 2px 0 !important;
            }

            input[type="color"] {
                height: 35px !important;
                padding: 2px !important;
            }

            .btn-primary,
            .btn-secondary {
                padding: 12px !important;
                font-size: 14px !important;
                margin-top: 8px !important;
                flex-shrink: 0 !important;
            }

            .btn-primary {
                margin-top: auto !important;
            }

            .left-bar {
                width: 44px;
                padding-top: 8px;
                gap: 10px;
            }

            .toolbar {
                padding: 0 8px;
                gap: 5px;
            }

            .sep {
                display: none;
            }

            /* Mobile overrides for the merged input module */
            .inputs-area {
                margin-left: auto;
                /* <--- FIXED: Forces it to the right on mobile too */
            }

            .icon-btn {
                padding: 4px;
            }

            .icon-btn svg {
                width: 20px;
                height: 20px;
            }

            /* Layers panel: let everything scroll together on mobile */
            #layers-modal .settings-area {
                max-height: 45vh !important;
            }

            /* Bigger resize handles on mobile */
            .handle {
                width: 28px;
                height: 28px;
                border-radius: 4px;
            }

            .handle::after {
                top: -14px;
                left: -14px;
                right: -14px;
                bottom: -14px;
            }
        }

        /* --- DRAWER MENU STYLES --- */
        .menu-btn-sep {
            width: 30px;
            height: 1px;
            background: var(--border);
            margin: 5px 0 10px 0;
        }

        .app-drawer {
            position: absolute;
            top: 10px;
            left: 60px;
            width: 200px;
            background: var(--panel);
            border: 1px solid var(--border);
            border-radius: 6px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5);
            display: flex;
            flex-direction: column;
            padding: 5px;
            z-index: 3000;
            opacity: 0;
            pointer-events: none;
            transform: translateX(-10px);
            transition: opacity 0.2s, transform 0.2s;
        }

        .app-drawer.open {
            opacity: 1;
            pointer-events: auto;
            transform: translateX(0);
        }

        .drawer-item {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 10px 12px;
            background: transparent;
            border: none;
            color: var(--text);
            cursor: pointer;
            border-radius: 4px;
            text-align: left;
            font-family: 'Segoe UI', sans-serif;
            font-size: 14px;
            transition: background 0.2s;
        }

        .drawer-item:hover {
            background: var(--panel-hover);
            color: var(--text);
        }

        .drawer-item svg {
            width: 20px;
            height: 20px;
            fill: var(--text);
        }

        .drawer-item:hover svg {
            fill: var(--text);
        }

        .drawer-sep {
            height: 1px;
            background: var(--border);
            margin: 5px 0;
            width: 100%;
        }

        /* Toast */
        .toast {
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%) translateY(100px);
            background: var(--bg-card, #252526);
            color: var(--text-primary, #fff);
            padding: 12px 24px;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
            border: 1px solid var(--border, #333);
            z-index: 10000;
            opacity: 0;
            transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.3s;
            pointer-events: none;
            font-size: 14px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .toast.show {
            transform: translateX(-50%) translateY(0);
            opacity: 1;
        }

        /* Custom Popup Modal (Distinct from full-screen tool modals) */
        .popup-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(4px);
            /* Premium feel */
            z-index: 20000;
            /* Top most */
            display: flex;
            align-items: center;
            justify-content: center;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.2s ease;
        }

        .popup-overlay.open {
            opacity: 1;
            pointer-events: auto;
        }

        .popup-box {
            background: var(--bg-card, #252526);
            border: 1px solid var(--border, #444);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
            border-radius: 8px;
            width: 320px;
            max-width: 90vw;
            padding: 0;
            transform: scale(0.95);
            transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            overflow: hidden;
        }

        .popup-overlay.open .popup-box {
            transform: scale(1);
        }

        .popup-header {
            padding: 16px 20px;
            font-weight: 600;
            border-bottom: 1px solid var(--border, #444);
            color: var(--text-primary, #fff);
            font-size: 16px;
        }

        .popup-body {
            padding: 20px;
            color: var(--text-secondary, #ccc);
            font-size: 14px;
            line-height: 1.5;
        }

        .popup-actions {
            display: flex;
            justify-content: flex-end;
            padding: 12px 20px;
            gap: 10px;
            background: rgba(0, 0, 0, 0.1);
        }

        .popup-btn {
            padding: 8px 16px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            border: none;
            transition: opacity 0.2s;
        }

        .popup-btn:hover {
            opacity: 0.8;
        }

        .popup-btn-cancel {
            background: transparent;
            color: var(--text-secondary, #aaa);
            border: 1px solid transparent;
        }

        .popup-btn-cancel:hover {
            color: var(--text-primary, #fff);
            background: rgba(255, 255, 255, 0.05);
        }

        .popup-btn-confirm {
            background: var(--accent, #007bff);
            color: #fff;
        }

