#history-controls {
    position: fixed;
    z-index: 12;
    top: 12px;
    left: calc(50% + 105px);
    display: inline-flex;
    padding: 3px;
    gap: 2px;
    background: rgba(17, 24, 39, 0.92);
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

#history-controls button {
    width: 30px;
    height: 30px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: #f8fafc;
    border: 0;
    border-radius: 50%;
    font: 700 20px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    cursor: pointer;
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

#history-controls button:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.12);
}

#history-controls button:active:not(:disabled) {
    background: rgba(255, 255, 255, 0.2);
}

#history-controls button:focus-visible {
    outline: 2px solid #93c5fd;
    outline-offset: 1px;
}

#history-controls button:disabled {
    opacity: 0.32;
    cursor: default;
}

#home-link {
    position: fixed;
    z-index: 12;
    top: 12px;
    left: 12px;
    min-height: 36px;
    padding: 0 13px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    background: rgba(17, 24, 39, 0.92);
    color: #f8fafc;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 999px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
    font: 700 13px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.15s ease, transform 0.1s ease;
}

#home-link > span:first-child {
    font-size: 19px;
    line-height: 1;
}

#home-link:hover {
    background: #1f2937;
}

#home-link:active {
    transform: scale(0.96);
}

#home-link:focus-visible {
    outline: 2px solid #93c5fd;
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    #home-link {
        transition: none;
    }

    #home-link:active {
        transform: none;
    }
}

#snap-panel .sp-background {
    margin: 0 0 10px;
    padding: 8px 10px 10px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
}

#snap-panel .sp-background legend {
    padding: 0 4px;
    color: #111827;
    font-size: 13px;
    font-weight: 600;
}

#snap-panel .sp-background-modes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin-bottom: 8px;
}

#snap-panel .sp-background-modes label {
    display: flex;
    align-items: center;
    gap: 5px;
    min-height: 32px;
    padding: 4px 7px;
    background: #f3f4f6;
    border-radius: 6px;
    color: #374151;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

#snap-panel .sp-background-modes label:has(input:checked) {
    background: #eff6ff;
    color: #1d4ed8;
}

#snap-panel .sp-background-modes input {
    accent-color: #3b82f6;
}

#snap-panel .sp-background [hidden] {
    display: none;
}

#snap-panel .sp-color-row,
#snap-panel .sp-direction-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 36px;
    color: #374151;
    font-size: 12px;
    font-weight: 500;
}

#snap-panel .sp-gradient-controls .sp-color-row + .sp-color-row,
#snap-panel .sp-gradient-controls .sp-direction-row {
    margin-top: 4px;
}

#snap-panel input[type="color"] {
    width: 48px;
    height: 32px;
    padding: 2px;
    background: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: 7px;
    cursor: pointer;
}

#snap-panel #background-angle {
    min-height: 34px;
    max-width: 150px;
    padding: 5px 7px;
    background: #ffffff;
    color: #374151;
    border: 1px solid #cbd5e1;
    border-radius: 7px;
    font: 500 11px/1.2 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#snap-panel .sp-background input:focus-visible,
#snap-panel #background-angle:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

@media (min-width: 769px) and (min-height: 601px) {
    #logo-panel {
        top: 64px;
        left: 20px;
        width: 248px;
        max-height: calc(100vh - 84px);
    }

    #font-panel {
        top: 64px;
        right: 20px;
        width: 288px;
        max-height: calc(100vh - 84px);
    }

    #create-maket {
        top: auto;
        bottom: 12px;
        left: calc(50% + 184px);
        right: auto;
        margin: 0;
    }
}

@media (max-width: 768px), (orientation: landscape) and (max-height: 600px) {
    #home-link {
        width: 36px;
        height: 36px;
        min-height: 36px;
        padding: 0;
        border-radius: 50%;
    }

    #home-link .home-link-label {
        display: none;
    }

    #home-link > span:first-child {
        font-size: 20px;
    }

    #canvas-wrap {
        align-items: flex-start;
    }

    #canvas-label {
        margin-left: 60px;
        margin-right: max(12px, calc(100vw - 365px));
        color: #ffffff;
        background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
        border: 1px solid rgba(255, 255, 255, 0.25);
        box-shadow: 0 6px 18px rgba(37, 99, 235, 0.35),
                    0 0 0 1px rgba(37, 99, 235, 0.18);
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        transform: none;
    }

    #canvas-label:hover {
        background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
        box-shadow: 0 8px 22px rgba(37, 99, 235, 0.45),
                    0 0 0 1px rgba(96, 165, 250, 0.5);
    }

    #canvas-label:active {
        transform: scale(0.97);
    }

    #history-controls {
        /* Прижимаем справа рядом с #guide-toggle (правые 12px + 36px ширина
           + зазор 8px), чтобы undo/redo никогда не наезжали на «?» даже
           на самых узких экранах (≈320px). Раньше стояло
           `left: min(270px, calc(100vw - 92px))` — на 365px это давало
           правый край 335px, что перекрывало «?», сидящий в правых 48px. */
        top: 12px;
        left: auto;
        right: calc(12px + 36px + 8px);
    }

    #canvas-wrap .back-canvas-wrap,
    #front-canvas-wrap {
        align-self: center;
    }
}

@media (orientation: landscape) and (max-height: 600px) {
    #home-link {
        top: 12px;
        left: 12px;
    }

    #create-maket {
        left: auto;
        right: 60px;
        transform: none !important;
    }

    #create-maket:hover,
    #create-maket:active,
    #create-maket:focus,
    #create-maket:focus-visible {
        transform: none !important;
    }

    #front-toggle {
        width: 44px;
        height: 44px;
        padding: 0;
        border-radius: 50%;
        overflow: hidden;
        color: transparent;
        font-size: 0;
        justify-content: center;
    }

    #front-toggle::before {
        content: "⚙";
        color: #ffffff;
        font-size: 22px;
        line-height: 1;
    }

    #front-toggle.is-open::before {
        content: "✕";
        font-size: 20px;
    }

    #create-maket {
        top: 20px;
        bottom: auto;
        margin: 0;
    }
}
