body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    overscroll-behavior: none;
}

:root {
    --app-bg: #0f172a;
    --panel-bg: #1e293b;
    --panel-bg-strong: rgba(15, 23, 42, 0.72);
    --panel-border: #334155;
    --muted-text: #94a3b8;
    --body-text: #e2e8f0;
    --canvas-bg: #0b1120;
    --selection: #ffffff;
    --selection-fill: rgba(255, 255, 255, 0.12);
    --object-selection: #818cf8;
    --object-selection-fill: rgba(129, 140, 248, 0.12);
    --handle-fill: #ffffff;
    --handle-stroke: #6366f1;
    --palette-border: #ffffff;
    --palette-border-selected: 2.5px;
    --compact-header-h: 3.25rem;
    --mini-footer-h: 2.25rem;
    --mobile-toolbar-gap: 0.75rem;
}

body.theme-light {
    --app-bg: #e2e8f0;
    --panel-bg: #f8fafc;
    --panel-bg-strong: rgba(255, 255, 255, 0.9);
    --panel-border: #cbd5e1;
    --muted-text: #475569;
    --body-text: #0f172a;
    --canvas-bg: #ffffff;
    --selection: #0f172a;
    --selection-fill: rgba(15, 23, 42, 0.12);
    --palette-border: #0f172a;
}

.app-shell {
    background: radial-gradient(circle at top left, rgba(99, 102, 241, 0.14), transparent 22%), var(--app-bg);
    color: var(--body-text);
}

.app-header,
.app-footer {
    background: var(--panel-bg-strong);
    border-color: var(--panel-border);
}

.app-header {
    border-bottom: 1px solid var(--panel-border);
    position: sticky;
    top: 0;
    z-index: 200;
    flex-shrink: 0;
}

.app-main {
    position: relative;
    z-index: 0;
    isolation: isolate;
}

.app-footer {
    position: relative;
    z-index: 50;
    flex-shrink: 0;
}

.app-footer {
    border-top: 1px solid var(--panel-border);
}

.app-muted,
.app-console-text {
    color: var(--muted-text);
}

.header-controls {
    justify-content: flex-end;
    flex-wrap: nowrap;
}

.toolbar-row {
    flex-wrap: nowrap;
    white-space: nowrap;
    scrollbar-width: none;
}

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

.control-card {
    border: 1px solid var(--panel-border);
    background: var(--panel-bg-strong);
    border-radius: 0.9rem;
    padding: 0.55rem 0.7rem;
    min-height: 4.5rem;
}

.control-stack {
    justify-content: space-between;
}

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

.text-input {
    width: 100%;
    min-height: 4.75rem;
    resize: vertical;
    border: 1px solid var(--panel-border);
    border-radius: 0.6rem;
    background: rgba(15, 23, 42, 0.18);
    color: var(--body-text);
    padding: 0.55rem 0.7rem;
    font-size: 0.75rem;
    line-height: 1.4;
    outline: none;
}

.text-input::placeholder {
    color: var(--muted-text);
}

.text-input:focus {
    border-color: rgba(129, 140, 248, 0.8);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.18);
}

.text-editor-popup {
    position: absolute;
    z-index: 40;
    min-width: 20rem;
    max-width: min(24rem, calc(100% - 1.5rem));
}

.text-editor-popup.hidden {
    display: none;
}

.text-editor-card {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 0.85rem;
    border: 1px solid var(--panel-border);
    border-radius: 0.9rem;
    background: var(--panel-bg-strong);
    box-shadow: 0 18px 40px rgba(15, 23, 42, 0.25);
    white-space: normal;
}

.text-size-input {
    width: 4.5rem;
    border: 1px solid var(--panel-border);
    border-radius: 0.45rem;
    background: rgba(15, 23, 42, 0.18);
    color: var(--body-text);
    padding: 0.25rem 0.45rem;
    outline: none;
}

.text-size-input:focus {
    border-color: rgba(129, 140, 248, 0.8);
}

.text-preview-panel {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.text-preview-label {
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--muted-text);
}

.text-preview-content {
    min-height: 5rem;
    max-height: 10rem;
    overflow: auto;
    border: 1px solid var(--panel-border);
    border-radius: 0.6rem;
    background: rgba(15, 23, 42, 0.12);
    color: var(--body-text);
    padding: 0.65rem 0.75rem;
    font-size: 0.85rem;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}

body.theme-light .text-preview-content {
    background: rgba(255, 255, 255, 0.72);
}

.text-preview-content .MathJax,
.text-preview-content mjx-container {
    color: inherit !important;
}

.smart-toggle {
    transform: scale(0.65);
    transform-origin: left center;
}

.tool-btn,
.action-btn,
.swatch-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    border-radius: 0.75rem;
    border: 1px solid var(--panel-border);
    transition: background-color 0.2s ease, border-color 0.2s ease, transform 0.2s ease, color 0.2s ease;
}

.tool-btn,
.action-btn {
    padding: 0.45rem 0.65rem;
    font-size: 0.8rem;
    font-weight: 600;
    background: rgba(15, 23, 42, 0.38);
}

.action-btn-icon {
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
}

.action-btn-icon i {
    margin: 0;
}

.tool-btn:hover,
.action-btn:hover,
.swatch-btn:hover {
    transform: translateY(-1px);
}

.tool-btn.is-active {
    background: rgba(99, 102, 241, 0.2);
    border-color: rgba(129, 140, 248, 0.7);
    color: #c7d2fe;
}

.action-btn-danger {
    background: rgba(225, 29, 72, 0.88);
    border-color: rgba(254, 205, 211, 0.9);
    color: #ffffff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.action-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

.swatch-btn {
    width: 1.45rem;
    height: 1.45rem;
    background: #ffffff;
    box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.08);
}

.swatch-btn-dark {
    background: #020617;
}

.swatch-btn.is-active {
    outline: 2px solid var(--selection);
    outline-offset: 2px;
}

.palette-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 0.25rem;
    min-width: 6.25rem;
}

.palette-grid button {
    width: 1.25rem;
    height: 1.25rem;
}

.palette-color-btn {
    border: 1px solid var(--palette-border);
    box-sizing: border-box;
}

.palette-color-btn:hover {
    border-color: var(--palette-border);
}

.palette-color-btn.is-selected {
    border-width: var(--palette-border-selected);
}

.slider-compact {
    max-width: 7rem;
}

.slider-fill {
    max-width: none;
}

.canvas-stage {
    background: var(--canvas-bg);
    min-height: 420px;
    position: relative;
    z-index: 1;
}

#liveCanvas {
    z-index: 2;
}

#mainCanvas {
    z-index: 1;
}

.text-editor-popup {
    z-index: 150;
}

.status-inline {
    flex: 1;
    min-width: 220px;
    margin: 0 1rem;
}

#liveCanvas.is-selecting,
#liveCanvas.is-drawing,
#liveCanvas.is-text,
#liveCanvas.is-editing,
#liveCanvas.is-cropping {
    cursor: crosshair;
}

#liveCanvas.is-editing {
    cursor: default;
}

.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

.mobile-zoom-bar {
    display: none;
    align-items: center;
    gap: 0.35rem;
    margin-left: auto;
}

.zoom-level-label {
    font-size: 0.75rem;
    font-weight: 700;
    min-width: 2.75rem;
    text-align: center;
    color: var(--muted-text);
}

.zoom-inline {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    min-width: 0;
}

.zoom-slider {
    width: 7rem;
}

.header-brand {
    position: relative;
    z-index: 1;
}

.active-tool-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 0.5rem;
    background: rgba(99, 102, 241, 0.2);
    color: #c7d2fe;
    font-size: 0.8rem;
}

.desktop-toolbar-mount {
    display: none;
    flex: 1;
    justify-content: flex-end;
    align-items: stretch;
    gap: 0.75rem;
    min-width: 0;
}

.mobile-toolbar-mount {
    display: none;
}

.mobile-toolbar-nav {
    display: none;
}

@media (max-width: 1279px) {
    .canvas-stage {
        min-height: 60vh;
    }
}

@media (max-width: 768px) {
    .mobile-only {
        display: flex;
    }

    .desktop-only {
        display: none !important;
    }

    .app-header {
        padding: 0.5rem 0.75rem;
        padding-top: max(0.5rem, env(safe-area-inset-top));
    }

    .app-footer {
        padding: 0.45rem 0.75rem;
        padding-bottom: max(0.45rem, env(safe-area-inset-bottom));
        flex-wrap: nowrap;
    }

    .toolbar-row {
        gap: 0.5rem;
        overflow: visible;
        flex-wrap: nowrap;
        align-items: center;
    }

    .header-brand {
        flex: 1 1 auto;
        min-width: 0;
    }

    .header-brand h1 {
        font-size: 0.95rem;
    }

    .mobile-zoom-bar {
        display: flex;
        flex-shrink: 0;
    }

    .zoom-inline {
        display: none;
    }

    .desktop-toolbar-mount {
        display: none;
    }

    .status-inline {
        order: 0;
        width: 100%;
        margin: 0;
        text-align: left;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .canvas-stage {
        min-height: calc(100dvh - var(--compact-header-h) - var(--mini-footer-h));
    }

    .mobile-toolbar-mount {
        display: block;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 30;
        pointer-events: none;
        padding: 0 0 0.25rem;
        background: linear-gradient(to top, rgba(2, 6, 23, 0.82), rgba(2, 6, 23, 0));
    }

    .mobile-toolbar-nav {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        top: calc(50% + 0.2rem);
        transform: translateY(-50%);
        z-index: 35;
        width: 2rem;
        height: 3rem;
        border-radius: 999px;
        background: rgba(15, 23, 42, 0.68);
        border: 1px solid rgba(148, 163, 184, 0.35);
        color: rgba(226, 232, 240, 0.88);
        box-shadow: 0 10px 24px rgba(15, 23, 42, 0.28);
        pointer-events: auto;
        transition: opacity 0.2s ease, transform 0.2s ease, background-color 0.2s ease;
    }

    .mobile-toolbar-mount .header-controls {
        display: flex;
        flex-direction: row;
        align-items: stretch;
        gap: var(--mobile-toolbar-gap);
        width: 100%;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 1.4rem 0.75rem max(0.75rem, env(safe-area-inset-bottom));
        scroll-snap-type: x mandatory;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
        pointer-events: auto;
    }

    .mobile-toolbar-nav-left {
        left: 0.4rem;
    }

    .mobile-toolbar-nav-right {
        right: 0.4rem;
    }

    .mobile-toolbar-nav:not(:disabled) {
        animation: mobile-hint-pulse 1.7s ease-in-out infinite;
    }

    .mobile-toolbar-nav:disabled {
        opacity: 0.28;
        animation: none;
    }

    .mobile-toolbar-nav[hidden] {
        display: none;
    }

    .mobile-toolbar-mount .header-controls::-webkit-scrollbar {
        display: none;
    }

    .mobile-toolbar-mount .control-card {
        flex: 0 0 calc(100vw - 1.5rem);
        width: calc(100vw - 1.5rem);
        min-height: auto;
        scroll-snap-align: start;
        border-radius: 1rem;
        backdrop-filter: blur(14px);
        box-shadow: 0 12px 30px rgba(15, 23, 42, 0.28);
    }

    .mobile-toolbar-mount .control-card-sliders {
        gap: 0.9rem;
    }

    .mobile-toolbar-mount #toolGroup {
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 0.1rem;
        scrollbar-width: none;
    }

    .mobile-toolbar-mount #toolGroup::-webkit-scrollbar {
        display: none;
    }

    .mobile-toolbar-mount .palette-grid {
        min-width: 0;
        grid-template-columns: repeat(6, minmax(0, 1fr));
    }

    .mobile-toolbar-mount .control-card:last-child .flex-wrap {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
    }

    .mobile-toolbar-mount .control-card:last-child .flex-wrap::-webkit-scrollbar {
        display: none;
    }

    .palette-grid {
        min-width: 0;
        grid-template-columns: repeat(6, 1fr);
    }

    .text-editor-popup {
        left: 0.75rem !important;
        right: 0.75rem;
        max-width: none;
        min-width: 0;
    }
}

@keyframes mobile-hint-pulse {
    0%, 100% { opacity: 0.82; }
    50% { opacity: 1; }
}

@media (min-width: 769px) {
    .desktop-toolbar-mount {
        display: flex;
    }

    .desktop-toolbar-mount .header-controls {
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: stretch;
        width: auto;
    }
}