:root {
    --p-1: #22d3ee;
    --p-2: #a855f7;
    --p-3: #f472b6;
    --panel-bg: rgba(8, 10, 20, 0.86);
    --panel-border: rgba(255, 255, 255, 0.1);
    --glow-color: rgba(168, 85, 247, 0.38);
    --text-primary: rgba(255, 255, 255, 0.95);
    --text-secondary: rgba(255, 255, 255, 0.52);
    --accent-gradient: linear-gradient(135deg, var(--p-1), var(--p-2));
    --bg-deep: #05060d;
    --header-fade: linear-gradient(to bottom, rgba(5, 6, 13, 0.78), transparent);
    --chrome: rgba(255, 255, 255, 0.1);
    --chrome-border: rgba(255, 255, 255, 0.14);
}

[data-theme="dark"] {
    --p-1: #22d3ee;
    --p-2: #a855f7;
    --p-3: #f472b6;
    --panel-bg: rgba(8, 10, 20, 0.86);
    --panel-border: rgba(255, 255, 255, 0.1);
    --glow-color: rgba(168, 85, 247, 0.38);
    --text-primary: rgba(255, 255, 255, 0.95);
    --text-secondary: rgba(255, 255, 255, 0.52);
    --accent-gradient: linear-gradient(135deg, var(--p-1), var(--p-2));
    --bg-deep: #05060d;
    --header-fade: linear-gradient(to bottom, rgba(5, 6, 13, 0.78), transparent);
    --chrome: rgba(255, 255, 255, 0.1);
    --chrome-border: rgba(255, 255, 255, 0.14);
}

[data-theme="light"] {
    --p-1: #0891b2;
    --p-2: #7c3aed;
    --p-3: #db2777;
    --panel-bg: rgba(255, 255, 255, 0.88);
    --panel-border: rgba(15, 18, 40, 0.1);
    --glow-color: rgba(124, 58, 237, 0.28);
    --text-primary: rgba(12, 16, 32, 0.94);
    --text-secondary: rgba(12, 16, 32, 0.5);
    --accent-gradient: linear-gradient(135deg, var(--p-1), var(--p-2));
    --bg-deep: #e7ebf8;
    --header-fade: linear-gradient(to bottom, rgba(231, 235, 248, 0.88), transparent);
    --chrome: rgba(255, 255, 255, 0.72);
    --chrome-border: rgba(15, 18, 40, 0.1);
}

html {
    overflow-x: clip;
}

body {
    font-family: 'Oxanium', Outfit, -apple-system, BlinkMacSystemFont, sans-serif;
    padding: 0 !important;
    display: block !important;
    overflow: hidden;
    overflow-x: clip;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    background: var(--bg-deep);
    color: var(--text-primary);
    user-select: none;
    -webkit-user-select: none;
}

.container {
    position: relative;
    width: 100%;
    height: 100%;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
}

.stage {
    position: absolute;
    inset: 0;
    z-index: 1;
}

#canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
    touch-action: none;
    z-index: 2;
}

header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 20;
    padding: max(0.85rem, env(safe-area-inset-top, 0px))
        max(1.1rem, env(safe-area-inset-right, 0px))
        0.85rem
        max(1.1rem, env(safe-area-inset-left, 0px)) !important;
    background: var(--header-fade);
    margin: 0 !important;
    display: grid !important;
    grid-template-columns: auto minmax(0, 1fr) auto !important;
    align-items: center !important;
    gap: 0.75rem !important;
    max-width: none !important;
    width: auto !important;
    pointer-events: none;
}

header > * {
    pointer-events: auto;
}

header .back-link {
    background: var(--chrome);
    border: 1px solid var(--chrome-border);
    color: var(--text-primary);
    min-height: 44px;
    min-width: 44px;
}

header .back-link:hover {
    background: color-mix(in srgb, var(--p-2) 22%, transparent);
    border-color: color-mix(in srgb, var(--p-2) 45%, transparent);
    color: var(--p-2);
}

header .app-logo {
    justify-self: center;
    display: flex;
    align-items: center;
    gap: 0.45rem;
    font-size: clamp(1rem, 2.6vw, 1.35rem);
    font-weight: 700;
    background: var(--accent-gradient) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    background-clip: text !important;
    filter: drop-shadow(0 0 18px var(--glow-color));
    letter-spacing: 0.02em;
    max-width: min(100%, calc(100vw - 11rem));
    overflow: hidden;
    text-overflow: ellipsis;
}

header .app-logo svg {
    stroke: var(--p-1);
    flex-shrink: 0;
    filter: drop-shadow(0 0 8px var(--p-1));
    -webkit-text-fill-color: initial;
}

.header-actions {
    display: flex;
    gap: 0.45rem;
    align-items: center;
}

.icon-btn {
    background: var(--chrome);
    border: 1px solid var(--chrome-border);
    color: var(--text-primary);
    padding: 0;
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border-radius: 12px;
    cursor: pointer;
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background: color-mix(in srgb, var(--p-2) 22%, transparent);
    border-color: color-mix(in srgb, var(--p-2) 45%, transparent);
    color: var(--p-2);
}

.icon-btn.muted {
    color: var(--p-3);
    border-color: color-mix(in srgb, var(--p-3) 45%, transparent);
}

.hud {
    position: absolute;
    top: max(4.6rem, calc(env(safe-area-inset-top, 0px) + 3.6rem));
    left: 50%;
    transform: translateX(-50%);
    z-index: 12;
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.35rem 0.85rem;
    border-radius: 999px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(18px) saturate(160%);
    -webkit-backdrop-filter: blur(18px) saturate(160%);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
    pointer-events: none;
    white-space: nowrap;
    max-width: calc(100% - 1.5rem);
}

#hudPreset {
    color: var(--p-1);
    overflow: hidden;
    text-overflow: ellipsis;
}

.hud-dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--p-2);
    box-shadow: 0 0 8px var(--p-2);
    flex-shrink: 0;
}

#hudMeta {
    font-variant-numeric: tabular-nums;
    overflow: hidden;
    text-overflow: ellipsis;
}

.floating-toolbar {
    position: fixed;
    bottom: max(1.25rem, calc(env(safe-area-inset-bottom, 0px) + 0.75rem));
    left: 50%;
    transform: translateX(-50%);
    z-index: 15;
    background: var(--panel-bg);
    backdrop-filter: blur(24px) saturate(180%);
    -webkit-backdrop-filter: blur(24px) saturate(180%);
    border: 1px solid var(--panel-border);
    padding: 8px 10px;
    border-radius: 999px;
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: calc(100% - 1.5rem);
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28), 0 0 24px var(--glow-color);
}

.tool-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border-radius: 50%;
    cursor: pointer;
    transition: color 0.2s ease, background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.tool-btn:hover {
    color: var(--text-primary);
    background: color-mix(in srgb, var(--p-1) 14%, transparent);
    transform: translateY(-1px);
}

.tool-btn[aria-pressed="true"] {
    color: #fff;
    background: var(--accent-gradient);
    box-shadow: 0 4px 14px var(--glow-color);
}

[data-theme="light"] .tool-btn[aria-pressed="true"] {
    color: #fff;
}

.tool-btn svg {
    display: block;
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.tool-btn svg[hidden],
.icon-btn svg[hidden] {
    display: none !important;
}

.preset-btn {
    width: auto;
    border-radius: 999px;
    padding: 0 16px 0 12px;
    color: var(--p-1);
    background: color-mix(in srgb, var(--p-1) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--p-1) 22%, transparent);
}

.preset-btn:hover {
    border-color: color-mix(in srgb, var(--p-1) 45%, transparent);
    box-shadow: 0 4px 16px var(--glow-color);
}

.preset-name {
    font-size: 0.82rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    max-width: 7.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.divider {
    width: 1px;
    height: 22px;
    background: var(--panel-border);
    margin: 0 2px;
    flex-shrink: 0;
}

.instructions {
    position: absolute;
    top: 46%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    pointer-events: none;
    z-index: 5;
    animation: instructionsFade 7s forwards;
    padding: 0 1.25rem;
    max-width: min(34rem, calc(100% - 2rem));
}

.instructions p {
    font-size: clamp(0.92rem, 2.6vw, 1.12rem);
    color: var(--text-primary);
    text-shadow: 0 0 28px var(--glow-color);
    letter-spacing: 0.02em;
    font-weight: 600;
}

.instructions p.sub {
    margin-top: 0.45rem;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-shadow: none;
}

kbd {
    display: inline-block;
    padding: 0.05rem 0.35rem;
    border-radius: 5px;
    border: 1px solid var(--panel-border);
    background: var(--panel-bg);
    font-family: inherit;
    font-size: 0.72em;
    font-weight: 700;
}

@keyframes instructionsFade {
    0%, 72% { opacity: 1; }
    100% { opacity: 0; visibility: hidden; }
}

footer {
    position: absolute;
    left: max(1rem, env(safe-area-inset-left, 0px));
    bottom: max(1rem, env(safe-area-inset-bottom, 0px));
    z-index: 8;
    margin: 0 !important;
    padding: 0 !important;
    width: auto !important;
    max-width: none !important;
    border: none !important;
    font-size: 0.68rem;
    color: var(--text-secondary);
}

@media (max-width: 768px) {
    header .app-logo {
        font-size: 1.05rem;
    }

    .hud {
        top: max(4.15rem, calc(env(safe-area-inset-top, 0px) + 3.25rem));
        font-size: 0.64rem;
        gap: 0.4rem;
        padding: 0.3rem 0.7rem;
    }

    .instructions {
        top: 40%;
    }

    .preset-name {
        font-size: 0.75rem;
        max-width: 5.5rem;
    }

    footer {
        display: none;
    }
}

@media (max-width: 420px) {
    header {
        padding: max(0.7rem, env(safe-area-inset-top, 0px))
            max(0.55rem, env(safe-area-inset-right, 0px))
            0.7rem
            max(0.55rem, env(safe-area-inset-left, 0px)) !important;
        gap: 0.35rem !important;
    }

    header .app-logo {
        font-size: 0.95rem;
        gap: 0.3rem;
    }

    .header-actions {
        gap: 0.25rem;
    }

    .icon-btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
        border-radius: 10px;
    }

    header .theme-toggle {
        width: 48px;
        min-width: 48px;
        height: 28px;
    }

    header .theme-toggle-thumb {
        width: 22px;
        height: 22px;
    }

    [data-theme="dark"] header .theme-toggle-thumb {
        transform: translateX(20px);
    }

    #fullscreen {
        display: none;
    }

    .floating-toolbar {
        width: calc(100% - 1.25rem);
        justify-content: space-between;
        padding: 6px 8px;
    }
}

@media (max-height: 520px) and (orientation: landscape) {
    header {
        padding: max(0.4rem, env(safe-area-inset-top, 0px))
            max(0.8rem, env(safe-area-inset-right, 0px))
            0.4rem
            max(0.8rem, env(safe-area-inset-left, 0px)) !important;
    }

    header .app-logo {
        font-size: 0.95rem;
    }

    .hud {
        display: none;
    }

    .floating-toolbar {
        bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
        padding: 4px 8px;
        gap: 4px;
    }

    .preset-name {
        display: inline;
        max-width: 5.5rem;
    }

    .tool-btn,
    .icon-btn {
        width: 40px;
        height: 40px;
        min-width: 40px;
        min-height: 40px;
    }

    .instructions {
        display: none;
    }

    footer {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .instructions {
        animation: none;
        opacity: 0.85;
    }

    .tool-btn,
    .icon-btn,
    header .back-link {
        transition: none;
    }
}
