/* ==========================================================================
   Collage Maker — UI
   Десктоп: шаблоны | превью | настройки
   Мобилка: фото → шаблоны → превью → настройки (+ закреплённая кнопка скачать)
   ========================================================================== */

:root {
    --bg:            #0e1117;
    --bg-2:          #141923;
    --surface:       #171d29;
    --surface-2:     #1d2431;
    --line:          #262f3f;
    --line-soft:     #1f2735;

    --text:          #e8edf6;
    --text-dim:      #98a4b8;
    --text-faint:    #6b7789;

    --accent:        #7c6cff;
    --accent-2:      #4dd0e1;
    --accent-soft:   rgba(124, 108, 255, .16);
    --danger:        #ff6b6b;

    --r-sm: 8px;
    --r-md: 12px;
    --r-lg: 18px;
    --r-xl: 26px;

    --pad: clamp(14px, 2.2vw, 22px);
    --fmt-ratio: 1 / 1;

    --shadow-1: 0 2px 10px rgba(0,0,0,.28);
    --shadow-2: 0 18px 50px rgba(0,0,0,.45);

    color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

/* Гарантия: атрибут hidden всегда побеждает любые display-правила классов
   (актуально для модалки ячейки и кнопок, у которых свой display: flex/grid). */
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior-y: none;
}

body {
    min-height: 100dvh;
    background:
        radial-gradient(1100px 620px at 12% -10%, rgba(124,108,255,.16), transparent 60%),
        radial-gradient(900px 560px at 92% 4%, rgba(77,208,225,.10), transparent 62%),
        var(--bg);
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
button { font: inherit; color: inherit; }

/* Полосы прокрутки */
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
*::-webkit-scrollbar { width: 9px; height: 9px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--line); border-radius: 99px; border: 2px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-thumb:hover { background: #33405a; background-clip: content-box; }

/* --------------------------------------------------------------------------
   Каркас
   -------------------------------------------------------------------------- */
.app {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

/* ---------- Шапка ---------- */
.topbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px clamp(12px, 2.4vw, 24px);
    padding-top: max(10px, env(safe-area-inset-top));
    border-bottom: 1px solid var(--line-soft);
    background: color-mix(in srgb, var(--bg) 78%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    position: sticky;
    top: 0;
    z-index: 40;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand__mark {
    display: grid; place-items: center;
    width: 34px; height: 34px;
    border-radius: 10px;
    background: linear-gradient(140deg, var(--accent), #5a49e0);
    color: #fff;
    box-shadow: 0 4px 14px rgba(124,108,255,.38);
    flex: 0 0 auto;
}
.brand__mark svg { width: 19px; height: 19px; }
.brand__text { font-size: 16px; font-weight: 600; letter-spacing: -.015em; white-space: nowrap; }
.brand__text b { font-weight: 750; color: var(--text); }

.topbar__actions { display: flex; align-items: center; gap: 8px; }
body[data-stage="empty"] .topbar__actions { display: none; }

.chip {
    padding: 5px 11px;
    border-radius: 99px;
    background: var(--accent-soft);
    color: #c3baff;
    border: 1px solid rgba(124,108,255,.28);
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

/* ---------- Кнопки ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1px solid transparent;
    border-radius: var(--r-md);
    padding: 10px 16px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, border-color .15s, transform .12s, opacity .15s;
    -webkit-tap-highlight-color: transparent;
    min-height: 42px;
}
.btn:active { transform: scale(.975); }
.btn svg { width: 18px; height: 18px; flex: 0 0 auto; }

.btn--ghost { background: var(--surface); border-color: var(--line); color: var(--text-dim); }
.btn--ghost:hover { background: var(--surface-2); color: var(--text); border-color: #33405a; }

.btn--icon { padding: 10px; width: 42px; }
.btn--icon svg { width: 17px; height: 17px; }

.btn--primary {
    width: 100%;
    background: linear-gradient(135deg, var(--accent), #5b4ae2);
    color: #fff;
    box-shadow: 0 8px 22px rgba(92,74,226,.34);
    min-height: 50px;
    font-size: 16px;
    border-radius: 14px;
}
.btn--primary:hover { filter: brightness(1.07); }
.btn--primary[disabled] { opacity: .6; cursor: progress; box-shadow: none; }

/* --------------------------------------------------------------------------
   Экран 1 — зона загрузки
   -------------------------------------------------------------------------- */
.stage-empty {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 18px;
    padding: clamp(16px, 4vw, 48px);
    padding-bottom: max(clamp(16px, 4vw, 48px), env(safe-area-inset-bottom));
}
body[data-stage="editor"] .stage-empty { display: none; }

.dropzone {
    position: relative;
    width: min(760px, 100%);
    min-height: min(58dvh, 440px);
    display: grid;
    place-items: center;
    padding: clamp(24px, 5vw, 56px);
    border: 2px dashed var(--line);
    border-radius: var(--r-xl);
    background: linear-gradient(180deg, rgba(29,36,49,.62), rgba(23,29,41,.38));
    cursor: pointer;
    overflow: hidden;
    transition: border-color .2s, background .2s, transform .2s;
    -webkit-tap-highlight-color: transparent;
}
.dropzone:hover,
.dropzone:focus-visible { border-color: rgba(124,108,255,.55); background: linear-gradient(180deg, rgba(37,44,62,.7), rgba(26,32,46,.45)); outline: none; }
.dropzone.is-over { border-color: var(--accent); background: rgba(124,108,255,.12); transform: scale(1.008); }

.dropzone__glow {
    position: absolute;
    inset: -40% 20% auto 20%;
    height: 70%;
    background: radial-gradient(closest-side, rgba(124,108,255,.28), transparent);
    filter: blur(22px);
    pointer-events: none;
}
.dropzone__inner { position: relative; text-align: center; max-width: 440px; }

.dropzone__icon {
    width: 76px; height: 76px;
    margin: 0 auto 20px;
    display: grid; place-items: center;
    border-radius: 22px;
    background: linear-gradient(140deg, rgba(124,108,255,.22), rgba(77,208,225,.14));
    border: 1px solid rgba(124,108,255,.3);
    color: #b9b0ff;
}
.dropzone__icon svg { width: 34px; height: 34px; }

.dropzone__title { font-size: clamp(20px, 3.4vw, 28px); margin-bottom: 8px; }
.dropzone__sub { margin: 0 0 16px; color: var(--text-dim); font-size: 15px; }
.dropzone__hint {
    display: inline-block; margin: 0;
    padding: 6px 14px; border-radius: 99px;
    background: var(--surface); border: 1px solid var(--line);
    color: var(--text-faint); font-size: 13px;
}
.stage-empty__foot { margin: 0; color: var(--text-faint); font-size: 13.5px; text-align: center; }

/* --------------------------------------------------------------------------
   Экран 2 — рабочая область
   -------------------------------------------------------------------------- */
.workspace { display: none; }
body[data-stage="editor"] .workspace { display: grid; }

/* --- Мобильная раскладка (база, mobile-first) --- */
.workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
        "photos"
        "templates"
        "preview"
        "settings";
    gap: 10px;
    padding: 12px;
    padding-bottom: calc(86px + env(safe-area-inset-bottom));
}

.panel--photos    { grid-area: photos; }
.panel--templates { grid-area: templates; }
.panel--preview   { grid-area: preview; }
.panel--settings  { grid-area: settings; }

.panel {
    min-width: 0;
    background: color-mix(in srgb, var(--surface) 82%, transparent);
    border: 1px solid var(--line-soft);
    border-radius: var(--r-lg);
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.panel--preview { background: none; border: none; padding: 0; }

.panel__head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.panel__title {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .09em;
    color: var(--text-faint);
    font-weight: 700;
}
.panel__meta { font-size: 12px; color: var(--text-faint); }

/* ---------- Полоса фото ---------- */
.photostrip {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 4px;
    scrollbar-width: none;
}
.photostrip::-webkit-scrollbar { display: none; }

.photo-item {
    position: relative;
    flex: 0 0 auto;
    width: 60px; height: 60px;
    border-radius: var(--r-sm);
    overflow: hidden;
    border: 1px solid var(--line);
    background: var(--bg-2);
}
.photo-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-item__idx {
    position: absolute; left: 3px; top: 3px;
    min-width: 16px; height: 16px;
    padding: 0 4px;
    display: grid; place-items: center;
    border-radius: 5px;
    background: rgba(8,10,15,.72);
    font-size: 10px; font-weight: 700; color: #fff;
}
.photo-item__del {
    position: absolute; right: 3px; top: 3px;
    width: 19px; height: 19px;
    display: grid; place-items: center;
    border: none; border-radius: 6px;
    background: rgba(8,10,15,.72);
    color: #fff; font-size: 13px; line-height: 1;
    cursor: pointer; padding: 0;
    transition: background .15s;
}
.photo-item__del:hover { background: var(--danger); }

.photo-add {
    flex: 0 0 auto;
    width: 60px; height: 60px;
    display: grid; place-items: center;
    border: 1px dashed var(--line);
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--text-faint);
    font-size: 22px;
    cursor: pointer;
    transition: border-color .15s, color .15s, background .15s;
}
.photo-add:hover { border-color: var(--accent); color: #b9b0ff; background: var(--accent-soft); }

/* ---------- Галерея шаблонов ---------- */
.templates-scroll { min-height: 0; }

.templates {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 78px;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 6px;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
}
.templates::-webkit-scrollbar { display: none; }

.template-card {
    position: relative;
    padding: 0;
    border: 2px solid var(--line);
    border-radius: var(--r-md);
    background: var(--bg-2);
    cursor: pointer;
    overflow: hidden;
    scroll-snap-align: start;
    transition: border-color .15s, transform .12s;
    display: block;
    width: 100%;
    aspect-ratio: var(--fmt-ratio);
    -webkit-tap-highlight-color: transparent;
}
.template-card:hover { border-color: #3c4a66; }
.template-card:active { transform: scale(.97); }
.template-card.is-active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.template-card canvas { width: 100%; height: 100%; display: block; }

.template-card__badge {
    position: absolute; left: 4px; bottom: 4px;
    padding: 1px 6px;
    border-radius: 5px;
    background: rgba(8,10,15,.7);
    font-size: 10px; font-weight: 700; color: #cdd6e6;
    letter-spacing: .02em;
}

/* ---------- Превью ---------- */
.preview-stage {
    display: flex;
    align-items: center;
    justify-content: center;
    height: clamp(280px, 48dvh, 520px);
    padding: 6px;
}
.preview-frame {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    border-radius: 6px;
    overflow: hidden;
    box-shadow: var(--shadow-2);
    line-height: 0;
    background: var(--bg-2);
}
/* Размеры холста задаёт JS (layoutPreview) — здесь только подача */
.preview-frame canvas { display: block; }

/* Холст сам обрабатывает жесты (resize/swap/nudge на мыши, tap/long-press на
   тач) — запрещаем браузеру перехватывать его под скролл/зум/колбэк вида
   «Сохранить изображение», иначе тач-жесты будут дёргаться и прерываться. */
#previewCanvas {
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* ---------- Крестик удаления на ячейке (десктоп, наведение мышью) ---------- */
.cell-delete {
    position: absolute;
    width: 26px; height: 26px;
    display: none;
    align-items: center; justify-content: center;
    border: none; border-radius: 50%;
    background: rgba(8,10,15,.8);
    color: #fff;
    cursor: pointer;
    transform: translate(-50%, -50%);
    z-index: 5;
    padding: 0;
    transition: background .15s;
}
.cell-delete.is-visible { display: flex; }
.cell-delete svg { width: 13px; height: 13px; }
.cell-delete:hover { background: var(--danger); }

/* ---------- Подсказка режима сдвига фото внутри ячейки ---------- */
.edit-hint {
    position: absolute;
    left: 50%; bottom: 10px;
    transform: translateX(-50%);
    max-width: calc(100% - 24px);
    padding: 8px 14px;
    border-radius: 99px;
    background: rgba(8,10,15,.84);
    border: 1px solid var(--line);
    color: var(--text);
    font-size: 12.5px; font-weight: 600;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0; pointer-events: none;
    transition: opacity .15s;
    z-index: 6;
}
.edit-hint.is-visible { opacity: 1; }

/* ---------- Бейдж «своя раскладка» (появляется после resize границ мышью) ---------- */
.custom-badge {
    position: absolute;
    right: 8px; top: 8px;
    padding: 5px 10px;
    border: 1px solid rgba(124,108,255,.4);
    border-radius: 99px;
    background: rgba(124,108,255,.18);
    color: #d2ccff;
    font-size: 11.5px; font-weight: 700;
    display: none;
    align-items: center; gap: 6px;
    cursor: pointer;
    z-index: 6;
    -webkit-tap-highlight-color: transparent;
}
.custom-badge svg { width: 12px; height: 12px; }
.custom-badge.is-visible { display: inline-flex; }

/* ---------- Кнопка «Готово» — выход из тач-режима сдвига фото ---------- */
.nudge-done {
    position: absolute;
    right: 8px; top: 8px;
    z-index: 7;
    padding: 10px 18px;
    border: none; border-radius: 99px;
    background: var(--accent);
    color: #fff; font-size: 13.5px; font-weight: 700;
    box-shadow: 0 6px 16px rgba(92,74,226,.42);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.nudge-done:active { filter: brightness(.92); }

/* ---------- Настройки ---------- */
.settings { display: flex; flex-direction: column; gap: 16px; }

.field { display: flex; flex-direction: column; gap: 9px; }
.field__label {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    font-size: 13.5px; font-weight: 600; color: var(--text-dim);
}
.field__value {
    font-variant-numeric: tabular-nums;
    padding: 2px 9px;
    border-radius: 7px;
    background: var(--surface-2);
    border: 1px solid var(--line);
    color: var(--text);
    font-size: 12.5px; font-weight: 700;
}

/* Переключатель формата */
.segmented {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
}
.seg {
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 9px 6px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface-2);
    color: var(--text-dim);
    cursor: pointer;
    font-size: 13px; font-weight: 600;
    transition: border-color .15s, background .15s, color .15s;
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
}
.seg:hover { border-color: #3c4a66; color: var(--text); }
.seg small { font-size: 11px; color: var(--text-faint); font-weight: 600; }
.seg.is-active { background: var(--accent-soft); border-color: var(--accent); color: #d2ccff; }
.seg.is-active small { color: #a99dff; }

/* Ползунки */
input[type="range"] {
    -webkit-appearance: none; appearance: none;
    width: 100%; height: 26px;
    background: transparent;
    cursor: pointer;
    margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
    height: 6px; border-radius: 99px;
    background: linear-gradient(90deg, var(--accent) var(--fill, 0%), var(--line) var(--fill, 0%));
}
input[type="range"]::-moz-range-track {
    height: 6px; border-radius: 99px;
    background: linear-gradient(90deg, var(--accent) var(--fill, 0%), var(--line) var(--fill, 0%));
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 20px; height: 20px; margin-top: -7px;
    border-radius: 50%;
    background: #fff;
    border: 3px solid var(--accent);
    box-shadow: var(--shadow-1);
}
input[type="range"]::-moz-range-thumb {
    width: 20px; height: 20px;
    border-radius: 50%;
    background: #fff;
    border: 3px solid var(--accent);
    box-shadow: var(--shadow-1);
}

/* Цвета */
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
    width: 34px; height: 34px;
    border-radius: 9px;
    border: 2px solid var(--line);
    cursor: pointer;
    padding: 0;
    transition: transform .12s, border-color .15s;
    -webkit-tap-highlight-color: transparent;
}
.swatch:hover { transform: translateY(-1px); }
.swatch.is-active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.colorpick {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface-2);
    cursor: pointer;
    min-height: 46px;
}
.colorpick input[type="color"] {
    width: 30px; height: 30px;
    padding: 0; border: none; border-radius: 8px;
    background: none; cursor: pointer; flex: 0 0 auto;
}
.colorpick input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
.colorpick input[type="color"]::-webkit-color-swatch { border: 1px solid var(--line); border-radius: 8px; }
.colorpick__text { font-size: 14px; color: var(--text-dim); font-weight: 600; }
.colorpick__hex {
    margin-left: auto;
    font-size: 12.5px; font-weight: 700;
    color: var(--text-faint);
    font-variant-numeric: tabular-nums;
    letter-spacing: .03em;
}

/* ---------- Панель действий ---------- */
.actionbar {
    grid-area: actions;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 45;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-top: 1px solid var(--line-soft);
}

/* На узких экранах шапка оставляет только бренд и сброс:
   счётчик фото и кнопка «добавить» дублируются панелью «Фото» ниже. */
@media (max-width: 699px) {
    .topbar .chip,
    .topbar #addMoreBtn { display: none; }
    /* Пальцем по крестику — цель крупнее */
    .photo-item, .photo-add { width: 68px; height: 68px; }
    .photo-item__del { width: 24px; height: 24px; font-size: 16px; }
    .swatch { width: 40px; height: 40px; }
}
@media (max-width: 359px) {
    .brand__text { display: none; }
}

/* --------------------------------------------------------------------------
   ПЛАНШЕТ — 2 колонки (≥ 700px)
   -------------------------------------------------------------------------- */
@media (min-width: 700px) {
    .workspace {
        min-height: calc(100dvh - 62px);
        grid-template-columns: minmax(0, 1fr) minmax(280px, 330px);
        /* Превью забирает всю свободную высоту — иначе колонка настроек
           распирает строки и между блоками появляются пустоты */
        grid-template-rows: auto auto minmax(300px, 1fr) auto;
        grid-template-areas:
            "photos     settings"
            "templates  settings"
            "preview    settings"
            "actions    settings";
        gap: 12px;
        padding: 14px;
        padding-bottom: 14px;
    }
    .actionbar {
        position: static;
        padding: 0;
        background: none;
        border: none;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
    .panel--settings { align-self: start; position: sticky; top: 74px; }
    .preview-stage { height: 100%; padding: 0; }
    .photo-item, .photo-add { width: 66px; height: 66px; }
}

/* --------------------------------------------------------------------------
   ДЕСКТОП — 3 колонки: шаблоны | превью | настройки (≥ 1024px)
   -------------------------------------------------------------------------- */
@media (min-width: 1024px) {
    body { overflow: hidden; }
    .app { height: 100dvh; }

    .workspace {
        flex: 1 1 auto;
        min-height: 0;
        grid-template-columns: clamp(220px, 20vw, 300px) minmax(0, 1fr) clamp(280px, 24vw, 340px);
        grid-template-rows: auto minmax(0, 1fr) auto;
        grid-template-areas:
            "templates preview photos"
            "templates preview settings"
            "templates preview actions";
        gap: 14px;
        padding: 14px clamp(14px, 2vw, 24px);
        overflow: hidden;
    }

    /* Левая колонка — шаблоны, во всю высоту со своим скроллом */
    .panel--templates {
        grid-row: 1 / -1;
        min-height: 0;
        overflow: hidden;
        container-type: inline-size;
        container-name: tplpanel;
    }
    .templates-scroll {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        overflow-x: hidden;
        margin-right: -6px;
        padding-right: 6px;
    }
    .templates {
        grid-auto-flow: row;
        grid-auto-columns: auto;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        overflow: visible;
        padding-bottom: 2px;
    }
    /* Контейнерный запрос: в широкой панели — три колонки шаблонов */
    @container tplpanel (min-width: 290px) {
        .templates { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    }

    /* Центр — превью, занимает всю высоту */
    .panel--preview { grid-row: 1 / -1; min-height: 0; }
    .preview-stage { height: 100%; padding: 0; }

    /* Правая колонка */
    .panel--settings { position: static; min-height: 0; overflow-y: auto; }
    .photo-item, .photo-add { width: 58px; height: 58px; }
    /* В узкой колонке фото переносятся, а не прячутся в горизонтальный скролл */
    .photostrip { flex-wrap: wrap; overflow-x: visible; padding-bottom: 0; }
}

@media (min-width: 1500px) {
    .workspace { padding-inline: clamp(24px, 4vw, 64px); }
}

/* --------------------------------------------------------------------------
   Оверлей перетаскивания
   -------------------------------------------------------------------------- */
.dropveil {
    position: fixed; inset: 0;
    z-index: 90;
    display: none;
    place-items: center;
    background: rgba(14,17,23,.86);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    pointer-events: none;
}
.dropveil.is-visible { display: grid; }
.dropveil span {
    padding: 22px 34px;
    border: 2px dashed var(--accent);
    border-radius: var(--r-lg);
    background: rgba(124,108,255,.12);
    font-size: clamp(16px, 3vw, 22px);
    font-weight: 650;
    text-align: center;
}

/* --------------------------------------------------------------------------
   Уведомления
   -------------------------------------------------------------------------- */
.toasts {
    position: fixed;
    left: 50%; transform: translateX(-50%);
    bottom: max(78px, calc(env(safe-area-inset-bottom) + 78px));
    z-index: 100;
    display: flex; flex-direction: column; gap: 8px;
    width: min(420px, calc(100vw - 24px));
    pointer-events: none;
}
@media (min-width: 700px) { .toasts { bottom: 24px; } }

.toast {
    padding: 12px 16px;
    border-radius: var(--r-md);
    background: var(--surface-2);
    border: 1px solid var(--line);
    box-shadow: var(--shadow-2);
    font-size: 14.5px;
    animation: toast-in .22s ease-out;
}
.toast--err { border-color: rgba(255,107,107,.5); background: #2a1b1f; color: #ffc9c9; }
.toast--ok  { border-color: rgba(124,108,255,.5); }

@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* --------------------------------------------------------------------------
   Модальное меню ячейки (тач: тап по ячейке превью)
   -------------------------------------------------------------------------- */
.cell-modal {
    position: fixed; inset: 0;
    z-index: 120;
    display: none;
    align-items: flex-end;
    justify-content: center;
}
.cell-modal.is-open { display: flex; }

.cell-modal__backdrop {
    position: absolute; inset: 0;
    background: rgba(8,10,15,.64);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    animation: modal-fade .18s ease-out;
}

.cell-modal__sheet {
    position: relative;
    width: min(480px, 100%);
    max-height: 82dvh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    background: var(--surface-2);
    border: 1px solid var(--line-soft);
    border-bottom: none;
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    padding: 10px 16px calc(16px + env(safe-area-inset-bottom));
    box-shadow: var(--shadow-2);
    animation: sheet-up .22s cubic-bezier(.2,.9,.3,1);
}

@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes sheet-up { from { transform: translateY(22px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
    .cell-modal__backdrop, .cell-modal__sheet { animation: none; }
}

.cell-modal__handle {
    width: 40px; height: 4px; border-radius: 99px;
    background: var(--line);
    margin: 4px auto 12px;
    flex: 0 0 auto;
}

.cell-modal__prev {
    display: flex; align-items: center; gap: 12px;
    margin-bottom: 14px;
    flex: 0 0 auto;
}
.cell-modal__thumb {
    width: 52px; height: 52px;
    border-radius: var(--r-md);
    background-size: cover; background-position: center;
    background-color: var(--bg-2);
    border: 1px solid var(--line);
    flex: 0 0 auto;
}
.cell-modal__label { font-size: 15px; font-weight: 650; }

.cell-modal__resize {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    margin-bottom: 14px;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
    flex: 0 0 auto;
}
.cell-modal__resize-caption {
    font-size: 13px; font-weight: 600;
    color: var(--text-dim);
    flex: 1 1 auto;
    min-width: 0;
}
.cell-modal__resize-row {
    display: flex; align-items: center; gap: 10px;
    flex: 0 0 auto;
}
.cell-modal__resize-value {
    min-width: 74px;
    text-align: center;
    font-size: 13px; font-weight: 650;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}
.cell-modal__resize-btn {
    display: grid; place-items: center;
    width: 40px; height: 40px;
    flex: 0 0 auto;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--surface-2);
    color: var(--text);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background .12s, border-color .12s, opacity .12s;
}
.cell-modal__resize-btn svg { width: 18px; height: 18px; }
.cell-modal__resize-btn:active { background: var(--accent-soft); border-color: var(--accent); }
.cell-modal__resize-btn:disabled {
    opacity: .35;
    cursor: default;
    pointer-events: none;
}

.cell-modal__actions {
    display: flex; flex-direction: column; gap: 8px;
    margin-bottom: 4px;
    flex: 0 0 auto;
}
.cell-modal__btn {
    display: flex; align-items: center; gap: 12px;
    padding: 13px 14px;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--text);
    font-size: 15px; font-weight: 600;
    text-align: left;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    min-height: 50px;
    transition: background .12s, border-color .12s;
}
.cell-modal__btn:active { background: var(--accent-soft); border-color: var(--accent); }
.cell-modal__btn svg { width: 20px; height: 20px; flex: 0 0 auto; color: #b9b0ff; }
.cell-modal__btn--danger { color: #ffb3b3; }
.cell-modal__btn--danger svg { color: var(--danger); }
.cell-modal__btn--danger:active { background: rgba(255,107,107,.14); border-color: rgba(255,107,107,.5); }

.cell-modal__swaplist {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 8px;
    margin-bottom: 4px;
    flex: 0 0 auto;
}
.cell-modal__swap-item {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--r-sm);
    overflow: hidden;
    border: 2px solid var(--line);
    background: var(--bg-2);
    padding: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.cell-modal__swap-item canvas { width: 100%; height: 100%; display: block; }
.cell-modal__swap-item span {
    position: absolute; left: 4px; top: 4px;
    min-width: 16px; height: 16px; padding: 0 4px;
    display: grid; place-items: center;
    border-radius: 5px;
    background: rgba(8,10,15,.74);
    font-size: 10px; font-weight: 700; color: #fff;
}
.cell-modal__swap-item:active { border-color: var(--accent); }

.cell-modal__cancel {
    flex: 0 0 auto;
    width: 100%;
    margin-top: 8px;
    padding: 13px;
    border: none; border-radius: var(--r-md);
    background: var(--surface);
    color: var(--text-dim);
    font-size: 15px; font-weight: 650;
    cursor: pointer;
    min-height: 48px;
    -webkit-tap-highlight-color: transparent;
}
.cell-modal__cancel:active { background: var(--bg-2); }

body.cell-modal-open { overflow: hidden; }

@media (min-width: 700px) {
    .cell-modal { align-items: center; }
    .cell-modal__sheet {
        border-radius: var(--r-xl);
        border-bottom: 1px solid var(--line-soft);
        max-height: 74dvh;
    }
}

/* --------------------------------------------------------------------------
   Доступность
   -------------------------------------------------------------------------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
