:root {
    --notify-width: min(380px, calc(100vw - 20px));
    --notify-gap: 10px;
    --notify-radius: 4px;
    --notify-shadow: 0 16px 40px color-mix(in oklab, var(--shadow-color, rgba(0, 0, 0, .28)) 100%, transparent);
    --font-size-modal: 13px;
}

.ace-dark,
.ace {
    --font-size-modal: 15px;

}

/* ---------------------------------------------------------
     Backdrop
     --------------------------------------------------------- */

.custom-modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100000;
    display: grid;
    justify-items: center;
    align-content: start;
    padding: 20px;
    padding-top: 40vh;
    background: color-mix(in oklab, black 52%, transparent);
}

.custom-modal-backdrop.alert-flash {
    background: color-mix(in oklab, black 52%, transparent);
}

/* ---------------------------------------------------------
     Modal shell
     --------------------------------------------------------- */

.custom-modal-container {
    position: relative;
    width: min(520px, calc(100vw - 32px));
    max-width: 100%;
    min-width: 280px;
    color: var(--fg);
    background: var(--panel);
    border: 1px solid var(--context-border, var(--border));
    border-radius: 4px;
    box-shadow: 0 24px 80px color-mix(in oklab, var(--shadow-color, rgba(0, 0, 0, .3)) 100%, transparent);
    overflow: hidden;
    transform: translateY(-50%);
    transform-origin: center center;
    font-family: var(--font-family, inherit);
    font-size: var(--font-size-modal);
    line-height: var(--line-height, 1.5);
}

.custom-modal-container.modal-alert {
    --_type-accent: var(--accent);
}

.custom-modal-container.modal-confirm {
    --_type-accent: #f59e0b;
}

.custom-modal-container.modal-prompt {
    --_type-accent: var(--accent);
}

.custom-modal-container::before {
    content: "";
    position: absolute;
    inset: 0 0 auto 0;
    height: calc(var(--border-weight) + 1px);
    background: linear-gradient(90deg,
            color-mix(in oklab, var(--_type-accent), white 15%),
            color-mix(in oklab, var(--_type-accent), transparent 12%));
    pointer-events: none;
}

/* ---------------------------------------------------------
     Header
     --------------------------------------------------------- */

.custom-modal-header {
    min-height: 34px;
    padding: 0 18px;
    background: color-mix(in oklab, var(--header), var(--hover, var(--header)) 55%);
    display: flex;
    align-items: center;
    border-bottom: thin solid var(--context-border);
    /* display: none; */
}

.custom-modal-title {
    font-size: var(--font-size-modal);
    font-weight: 500;

    /* color: var(--fg); */
}

/* ---------------------------------------------------------
     Body
     --------------------------------------------------------- */

.custom-modal-content {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 14px;
    align-items: start;
    padding: 18px 18px 12px;
}

.custom-modal-content strong {
    font-weight: 500;
}

.modal-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    font-size: 20px;
    line-height: 1;
    color: color-mix(in oklab, var(--_type-accent), white 10%);
    /* background: color-mix(in oklab, var(--_type-accent), transparent 88%);
    border: 1px solid color-mix(in oklab, var(--_type-accent), transparent 75%);
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .08),
        0 1px 0 rgba(0, 0, 0, .12); */
}

.ace .modal-icon {
    background-color: var(--workspace);
}

.custom-modal-text {
    min-width: 0;
    color: var(--fg);
    font-size: var(--font-size-modal);
    /* line-height: 1; */
}

.custom-modal-text a,
.alert-message a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid color-mix(in oklab, var(--accent), transparent 55%);
}

.custom-modal-text a:hover,
.alert-message a:hover {
    color: var(--accent-hover, var(--accent));
    border-bottom-color: currentColor;
}

.alert-message {
    color: var(--fg);
    line-height: 1.55;
    word-break: break-word;
}

/* ---------------------------------------------------------
     Prompt input
     --------------------------------------------------------- */

.custom-modal-input {
    display: block;
    width: calc(100% - 36px);
    margin: 0 18px 14px;
    height: 40px;
    padding: 0 12px;
    border-radius: 4px;
    border: 1px solid var(--context-border);
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--hover), var(--panel) 35%),
            color-mix(in oklab, var(--active), var(--panel) 45%));
    color: var(--text);
    box-shadow:
        inset 0 1px 1px rgba(0, 0, 0, .08),
        0 0 0 0 transparent;
    transition:
        border-color 80ms ease,
        box-shadow 80ms ease,
        background 80ms ease;
    font: inherit;
}

.custom-modal-input::placeholder {
    color: var(--fg-muted, var(--muted));
}

.custom-modal-input:hover {
    border-color: color-mix(in oklab, var(--accent), var(--context-border) 75%);
}

.custom-modal-input:focus,
.custom-modal-input:focus-visible {
    outline: none;
    border-color: color-mix(in oklab, var(--accent), white 8%);
    box-shadow:
        0 0 0 3px var(--focus-ring, var(--focus-color, #2563eb33)),
        inset 0 1px 1px rgba(0, 0, 0, .08);
}

/* ---------------------------------------------------------
     Buttons row
     --------------------------------------------------------- */

.custom-modal-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 0 18px 18px;
}

.custom-modal-button,
.alert-actions button,
.btn-primary,
.btn-secondary {
    appearance: none;
    border: 1px solid transparent;
    border-radius: 4px;
    min-height: 32px;
    padding: 0 12px;
    font: inherit;
    font-size: var(--font-size-modal);
    font-weight: 600;
    letter-spacing: 0.01em;
    cursor: pointer;
    user-select: none;
    transition:
        background 80ms ease,
        border-color 80ms ease,
        color 80ms ease,
        box-shadow 80ms ease,
        opacity 80ms ease;
}

.custom-modal-button:active,
.alert-actions button:active,
.btn-primary:active,
.btn-secondary:active {
    transform: none;
}

.custom-modal-button:focus-visible,
.alert-actions button:focus-visible,
.btn-primary:focus-visible,
.btn-secondary:focus-visible {
    outline: none;
    /* box-shadow: 0 0 0 3px var(--focus-ring, var(--focus-color, #2563eb33)); */
}

.custom-modal-button {
    color: var(--text);
    /* background: linear-gradient(180deg,
            color-mix(in oklab, var(--hover), white 3%),
            color-mix(in oklab, var(--active), transparent 10%)); */
    border-color: var(--context-border);
    background-color: var(--tertiary);
    /* box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .05),
        0 1px 0 rgba(0, 0, 0, .12); */
}

.ace-dark .custom-modal-button,
.ace .custom-modal-button {
    clip-path: polygon(0px calc(100% - 2px),
            2px calc(100% - 2px),
            2px 100%,
            calc(100% - 2px) 100%,
            calc(100% - 2px) calc(100% - 2px),
            100% calc(100% - 2px),
            100% 2px,
            calc(100% - 2px) 2px,
            calc(100% - 2px) 0px,
            2px 0px,
            2px 2px,
            0px 2px);
}

.custom-modal-button:hover {
    /* background: linear-gradient(180deg,
            color-mix(in oklab, var(--hover), white 7%),
            color-mix(in oklab, var(--active), transparent 0%)); */
    border-color: color-mix(in oklab, var(--accent), var(--context-border) 75%);
    color: var(--fg-active);
}

.custom-modal-button:first-of-type {
    background-color: var(--accent);
    border-color: var(--accent);
    color: var(--panel) !important;
}

.custom-modal-button:first-of-type:hover {
    background-color: var(--accent-hover);
    border-color: var(--accent);
    color: var(--panel) !important;
}


.ace .custom-modal-button:first-of-type {
    background-color: var(--accent);
    border-color: var(--panel);
    color: var(--fg-active) !important;
}

.ace .custom-modal-button:first-of-type:hover {
    background-color: var(--accent-hover);
    border-color: var(--accent);
    color: var(--fg-active) !important;
}

.ace-dark .custom-modal-button:first-of-type {
    background-color: var(--accent);
    border-color: var(--accent);
    color: var(--workspace) !important;
}

.ace-dark .custom-modal-button:first-of-type:hover {
    background-color: var(--accent-hover);
    border-color: var(--accent);
    color: var(--panel) !important;
}

.alert-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 14px;
}

.btn-primary {
    color: var(--fg-accent, white);
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--accent), white 10%),
            var(--accent));
    border-color: color-mix(in oklab, var(--accent), black 14%);
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .16),
        0 4px 14px color-mix(in oklab, var(--accent), transparent 75%);
}

.btn-primary:hover {
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--accent-hover, var(--accent)), white 8%),
            var(--accent-hover, var(--accent)));
    border-color: color-mix(in oklab, var(--accent-hover, var(--accent)), black 18%);
}

.btn-secondary {
    color: var(--text);
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--secondary), white 2%),
            color-mix(in oklab, var(--primary), transparent 0%));
    border-color: var(--context-border);
}

.btn-secondary:hover {
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--hover), white 3%),
            color-mix(in oklab, var(--secondary), transparent 0%));
    border-color: color-mix(in oklab, var(--accent), var(--context-border) 78%);
}

/* ---------------------------------------------------------
     Notifications
     --------------------------------------------------------- */

.notification-wrapper {
    position: fixed;
    top: 45px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100001;

    display: flex;
    flex-direction: column;
    align-items: center;
    /* important for centered stack */
    gap: var(--notify-gap);

    width: var(--notify-width);
    max-width: calc(100vw - 20px);
    pointer-events: none;
}

.notification {
    position: relative;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    align-items: start;
    width: 100%;
    overflow: hidden;
    pointer-events: auto;
    border-radius: var(--notify-radius);
    border: 1px solid color-mix(in oklab, var(--context-border), transparent 2%);
    background: var(--panel);
    color: var(--text);
    box-shadow: var(--notify-shadow);
    backdrop-filter: blur(10px) saturate(110%);
    -webkit-backdrop-filter: blur(10px) saturate(110%);
}

.notification::before {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: 3px;
    background: var(--_notify-accent, var(--accent));
}

.notification-info {
    --_notify-accent: var(--accent);
}

.notification-success {
    --_notify-accent: #22c55e;
}

.notification-warning {
    --_notify-accent: #f59e0b;
}

.notification-error {
    --_notify-accent: #ef4444;
}

.notification-content {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 10px;
    align-items: start;
    min-width: 0;
    padding: 12px 10px 12px 12px;
}

.notification-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    margin-top: 1px;
    color: var(--_notify-accent, var(--accent));
    flex: 0 0 auto;
}

.notification-icon svg {
    width: 16px;
    height: 16px;
    display: block;
}

.modal-icon svg {
    width: 20px;
    height: 20px;
    display: block;
}

.notification-message {
    min-width: 0;
    font-size: var(--font-size-modal);
    line-height: 1.45;
    color: var(--text);
    word-break: break-word;
}

.notification-message a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid color-mix(in oklab, var(--accent), transparent 55%);
}

.notification-message a:hover {
    border-bottom-color: currentColor;
}

.notification-close {
    align-self: start;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin: 8px 8px 0 0;
    padding: 0;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--fg-muted);
    cursor: pointer;
    transition:
        background 80ms ease,
        color 80ms ease;
}

.notification-close svg {
    width: 16px;
    height: 16px;
    display: block;
    flex: 0 0 auto;
}

.notification-close:hover {
    background: color-mix(in oklab, var(--hover), transparent 8%);
    color: var(--fg-active, var(--text));
}

.notification-close:active {
    transform: none;
}

.notification-close:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--focus-ring, var(--focus-color, #2563eb33));
}

.notification-progress {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 2px;
    transform-origin: left center;
    background: color-mix(in oklab, var(--_notify-accent, var(--accent)), white 8%);
    opacity: 0.95;
}

/* ---------------------------------------------------------
     Subtle style polish
     --------------------------------------------------------- */

.custom-modal-container,
.notification {
    will-change: auto;
}

.custom-modal-button,
.btn-primary,
.btn-secondary,
.notification-close {
    -webkit-tap-highlight-color: transparent;
}

/* ---------------------------------------------------------
     Light theme refinements
     --------------------------------------------------------- */

body.light .custom-modal-backdrop,
body.light .notification-wrapper {
    --_light-backdrop: color-mix(in oklab, black 26%, transparent);
}

body.light .custom-modal-backdrop {
    background: var(--_light-backdrop);
}

body.light .custom-modal-container,
body.light .notification {
    box-shadow:
        0 22px 60px rgba(0, 0, 0, .16),
        0 2px 10px rgba(0, 0, 0, .08);
}

body.light .custom-modal-button,
body.light .btn-secondary {
    /* box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .55),
        0 1px 0 rgba(0, 0, 0, .04); */
}

body.light .custom-modal-input {
    background: linear-gradient(180deg,
            color-mix(in oklab, white, var(--secondary) 30%),
            color-mix(in oklab, var(--secondary), white 10%));
}

/* ---------------------------------------------------------
     Modern dark themes refinement
     --------------------------------------------------------- */
body.spritesheep-dark .custom-modal-container {
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--workspace), white 1.5%),
            color-mix(in oklab, var(--workspace), black 1%));
}

body.spritesheep-dark .notification {
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--context-primary), white 1.5%),
            color-mix(in oklab, var(--context-primary), black 1%));
}

body.default-dark .custom-modal-container,
body.grey .custom-modal-container,
body.one-dark .custom-modal-container,
body.midnight .custom-modal-container,
body.affinity .custom-modal-container,
body.default-dark .notification,
body.grey .notification,
body.one-dark .notification,
body.midnight .notification,
body.affinity .notification {
    background: linear-gradient(180deg,
            color-mix(in oklab, var(--panel), white 1.5%),
            color-mix(in oklab, var(--panel), black 1%));
}

/* ---------------------------------------------------------
     ACE variants
     --------------------------------------------------------- */

.ace .custom-modal-container,
.ace-dark .custom-modal-container,
.ace .notification,
.ace-dark .notification,
.ace .custom-modal-input,
.ace-dark .custom-modal-input,
.ace .custom-modal-button,
.ace-dark .custom-modal-button,
.ace .btn-primary,
.ace-dark .btn-primary,
.ace .btn-secondary,
.ace-dark .btn-secondary {
    border-radius: 0;
}

.ace .custom-modal-container,
.ace-dark .custom-modal-container {
    clip-path: polygon(0px calc(100% - 2px),
            2px calc(100% - 2px),
            2px 100%,
            calc(100% - 2px) 100%,
            calc(100% - 2px) calc(100% - 2px),
            100% calc(100% - 2px),
            100% 2px,
            calc(100% - 2px) 2px,
            calc(100% - 2px) 0px,
            2px 0px,
            2px 2px,
            0px 2px);
}

.ace .custom-modal-container,
.ace .notification {
    border-color: color-mix(in oklab, var(--border-alt), var(--border) 70%);
    border-width: 2px;
    box-shadow:
        0 0 0 1px color-mix(in oklab, white, transparent 90%),
        0 12px 32px rgba(0, 0, 0, .18);
    background-color: var(--header);
}

.ace-dark .custom-modal-container,
.ace-dark .notification {
    border-color: color-mix(in oklab, var(--border), black 8%);
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, .02),
        0 14px 36px rgba(0, 0, 0, .42);
}

.ace .custom-modal-header,
.ace-dark .custom-modal-header {
    background: var(--panel);
    color: white !important;
    border-bottom: 2px solid var(--context-border);
}

.ace-dark .custom-modal-header {
    background: var(--tertiary);
}

.ace .modal-icon,
.ace-dark .modal-icon {
    border-radius: 0;
    clip-path: polygon(0px calc(100% - 2px),
            2px calc(100% - 2px),
            2px 100%,
            calc(100% - 2px) 100%,
            calc(100% - 2px) calc(100% - 2px),
            100% calc(100% - 2px),
            100% 2px,
            calc(100% - 2px) 2px,
            calc(100% - 2px) 0px,
            2px 0px,
            2px 2px,
            0px 2px);
}

.ace .custom-modal-input,
.ace-dark .custom-modal-input {
    background: color-mix(in oklab, var(--tertiary), var(--header) 22%);
    border-color: var(--context-border);
    border-width: 2px;
}

.ace .custom-modal-button,
.ace-dark .custom-modal-button,
.ace .btn-secondary,
.ace-dark .btn-secondary {
    background: color-mix(in oklab, var(--tertiary), var(--header) 12%);
    border-color: var(--context-border);
    border-width: 2px;
}

.ace .custom-modal-button:hover,
.ace-dark .custom-modal-button:hover,
.ace .btn-secondary:hover,
.ace-dark .btn-secondary:hover {
    background: color-mix(in oklab, var(--hover), var(--panel) 10%);
    border-color: var(--accent);
}

.ace .btn-primary,
.ace-dark .btn-primary {
    box-shadow: none;
}

.ace .notification::before,
.ace-dark .notification::before {
    width: 2px;
}

.ace .notification-close,
.ace-dark .notification-close {
    border-radius: 0;
}

.ace-dark .custom-modal-text,
.ace-dark .alert-message,
.ace-dark .notification-message {
    color: white;
}

.ace .custom-modal-text a,
.ace-dark .custom-modal-text a,
.ace .alert-message a,
.ace-dark .alert-message a,
.ace .notification-message a,
.ace-dark .notification-message a {
    text-decoration: underline;
    border-bottom: none;
}

/* ---------------------------------------------------------
     Smaller screens
     --------------------------------------------------------- */

@media (max-width: 640px) {
    .custom-modal-backdrop {
        padding: 12px;
    }

    .custom-modal-container {
        width: min(100%, calc(100vw - 24px));
    }

    .custom-modal-content {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .modal-icon {
        width: 34px;
        height: 34px;
    }

    .custom-modal-input {
        width: calc(100% - 24px);
        margin-left: 12px;
        margin-right: 12px;
    }

    .custom-modal-buttons {
        padding: 0 12px 12px;
        flex-wrap: wrap;
    }

    .custom-modal-button,
    .btn-primary,
    .btn-secondary {
        flex: 1 1 auto;
    }

    .notification-wrapper {
        top: 10px;
        right: 10px;
        left: 10px;
        width: auto;
        align-items: stretch;
    }
}

/* ---------------------------------------------------------
     Reduced motion
     --------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {

    .custom-modal-input,
    .custom-modal-button,
    .alert-actions button,
    .btn-primary,
    .btn-secondary,
    .notification-close,
    .notification-progress {
        transition: none !important;
    }
}


.modal-confirm-swatch-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-confirm-swatch-copy {
    min-width: 0;
}

.modal-swatch-preview {
    --swatch-outline-width: 1px;

    width: 24px;
    min-width: 24px;
    max-width: 24px;
    height: 24px;
    min-height: 24px;
    max-height: 24px;
    padding-bottom: 0;
    flex: 0 0 24px;
}

.modal-swatch-preview .modal-swatch-fill {
    position: absolute;
    inset: var(--swatch-outline-width);
    display: block;
    z-index: 0;
    pointer-events: none;
}

.modal-swatch-preview.transp .modal-swatch-fill {
    background: transparent !important;
}