/* Сторінка «Маска кізоньки».
   Спільні стилі (шапка, перша секція, фон секції зі сніжинками, кнопки, футер) — у layout.css.
   Логіка редактора — public/js/maska.js */

/* ---------- Перша секція: мобільний фон ---------- */

/* На телефоні десктопний фон обрізається по центру, і хатинка з маскою випадає з кадру.
   Тут окремий квадратний кадр public/images/maska-bg-mobile.jpg (зібраний з основного фону maska-bg):
   угорі зоряне небо під текстом, нижче — хатинка з маскою. Важить ~10% від десктопного. */
@media (max-width: 600px) {
    .page-hero {
        /* квадратна секція, щоб кадр показувався цілим; на ширших екранах обрізається небо зверху */
        min-height: min(100vw, 480px);
        background:
            linear-gradient(180deg, rgba(0, 28, 91, 0) 72%, var(--navy-800) 100%),
            url("../images/maska-bg-mobile.jpg") center bottom / cover no-repeat,
            linear-gradient(180deg, var(--navy-700) 0%, var(--navy-800) 100%);
    }

    /* Примусовий перенос на вузьких екранах лишає одне слово в рядку — хай текст переноситься сам */
    .page-hero__text br {
        display: none;
    }
}

.mask-studio {
    padding-bottom: 48px;
}

.mask-studio [hidden] {
    display: none !important;
}

.mask-studio .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* ---------- Сітка: редактор зліва, кроки справа ---------- */

.mask-studio__layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 400px);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
        "stage upload"
        "stage masks"
        "stage controls"
        "stage save";
    gap: 16px 32px;
    align-items: start;
}

.mask-studio__stage {
    grid-area: stage;
    min-width: 0;
}

.mask-step--upload {
    grid-area: upload;
}

.mask-step--masks {
    grid-area: masks;
}

.mask-step--controls {
    grid-area: controls;
}

.mask-step--save {
    grid-area: save;
}

/* ---------- Картки (як картки відео) ---------- */

.mask-stage,
.mask-step {
    border: 1.5px solid #218bd2;
    border-radius: 22px;
    background: linear-gradient(180deg, #0a3683, #06235d 85%);
    box-shadow: 0 0 15px #0b94fe80;
}

.mask-step {
    min-width: 0;
    margin: 0;
    padding: 20px 22px 22px;
}

.mask-step__title {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 0 0 16px;
    padding: 0;
    font-size: 19px;
    font-weight: 600;
    line-height: 1.25;
    letter-spacing: 0.02em;
}

.mask-step__num {
    display: grid;
    flex: none;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--gold);
    box-shadow: 0 0 12px rgba(255, 212, 0, 0.45);
    color: var(--navy-900);
    font-size: 15px;
    font-weight: 700;
}

.mask-step__action {
    width: 100%;
}

.mask-step__action:disabled {
    cursor: not-allowed;
    opacity: 0.5;
    box-shadow: none;
    transform: none;
}

.mask-step__note {
    margin: 12px 0 0;
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.5;
}

.mask-alert {
    margin: 12px 0 0;
    padding: 10px 14px;
    border: 1px solid rgba(255, 128, 128, 0.7);
    border-radius: 12px;
    background: rgba(255, 72, 72, 0.16);
    color: #ffe1e1;
    font-size: 14px;
    line-height: 1.45;
}

/* ---------- Другорядні кнопки ---------- */

.mask-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    min-height: 48px;
    padding: 0 22px;
    border: 1.5px solid var(--card-border);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--white);
    cursor: pointer;
    font-family: inherit;
    font-size: 15px;
    font-weight: 500;
    letter-spacing: 0.01em;
    transition:
        border-color 0.2s ease,
        background 0.2s ease,
        color 0.2s ease;
}

/* без затримки й зуму від подвійного тапу на телефоні */
.mask-btn,
.mask-icon-btn,
.mask-option,
.mask-step__action {
    touch-action: manipulation;
}

.mask-btn svg {
    width: 20px;
    height: 20px;
    flex: none;
}

.mask-btn:hover:not(:disabled) {
    border-color: var(--gold);
    color: var(--gold);
}

.mask-btn:disabled {
    cursor: not-allowed;
}

.mask-icon-btn {
    display: grid;
    flex: none;
    place-items: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1.5px solid var(--card-border);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.06);
    color: var(--white);
    cursor: pointer;
    transition:
        border-color 0.2s ease,
        color 0.2s ease;
}

.mask-icon-btn svg {
    width: 20px;
    height: 20px;
}

.mask-icon-btn:hover:not(:disabled) {
    border-color: var(--gold);
    color: var(--gold);
}

.mask-icon-btn:disabled {
    cursor: not-allowed;
}

/* ---------- Редактор ---------- */

.mask-stage {
    position: relative;
    display: grid;
    place-items: center;
    padding: 14px;
    transition:
        border-color 0.2s ease,
        box-shadow 0.2s ease;
}

/* Поки фото немає — порожня рамка з підказкою */
.mask-stage:not(.has-photo) {
    aspect-ratio: 4 / 3;
}

.mask-stage.is-dragover {
    border-color: var(--gold);
    box-shadow: 0 0 26px rgba(255, 212, 0, 0.55);
}

.mask-stage__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 24px;
    border: 2px dashed rgba(130, 185, 255, 0.55);
    border-radius: 14px;
    cursor: pointer;
    text-align: center;
}

.mask-stage__empty-icon {
    width: 64px;
    height: 64px;
    margin-bottom: 14px;
    color: var(--gold);
    filter: drop-shadow(0 0 10px rgba(255, 212, 0, 0.45));
}

.mask-stage__empty-title {
    margin: 0 0 8px;
    font-size: clamp(18px, 1.7vw, 24px);
    font-weight: 600;
    letter-spacing: 0.02em;
}

.mask-stage__empty-text {
    max-width: 320px;
    margin: 0 0 20px;
    color: var(--text-muted);
    font-size: 15px;
    line-height: 1.5;
}

.mask-stage__empty-narrow {
    display: none;
}

.mask-stage__canvas {
    display: block;
    max-width: 100%;
    border-radius: 12px;
    background: #031646;
    cursor: grab;
    /* жести на полотні керують маскою, а не прокручують сторінку */
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

/* Рамку фокусу показуємо лише при керуванні з клавіатури */
.mask-stage__canvas:focus:not(:focus-visible) {
    outline: none;
}

.mask-stage__canvas.is-empty {
    cursor: default;
}

.mask-stage__canvas.is-dragging {
    cursor: grabbing;
}

.mask-stage__loader {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    border-radius: inherit;
    background: rgba(3, 19, 60, 0.72);
    font-size: 15px;
    font-weight: 500;
}

.mask-stage__spinner {
    width: 44px;
    height: 44px;
    border: 4px solid rgba(255, 255, 255, 0.25);
    border-top-color: var(--gold);
    border-radius: 50%;
    animation: mask-spin 0.8s linear infinite;
}

.mask-stage__hint {
    margin: 12px 4px 0;
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.5;
    text-align: center;
}

.mask-stage__hint-mouse {
    display: none;
}

@media (hover: hover) and (pointer: fine) {
    .mask-stage__hint-touch {
        display: none;
    }

    .mask-stage__hint-mouse {
        display: inline;
    }
}

@media (hover: none) {
    .mask-stage__drop-text {
        display: none;
    }
}

@keyframes mask-spin {
    to {
        transform: rotate(360deg);
    }
}

/* ---------- Вибір маски ---------- */

.mask-picker {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
}

.mask-option {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    min-width: 0;
    padding: 10px 6px 8px;
    border: 2px solid rgba(130, 185, 255, 0.45);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--white);
    cursor: pointer;
    font-family: inherit;
    transition:
        border-color 0.2s ease,
        box-shadow 0.2s ease,
        transform 0.2s ease;
}

.mask-option:hover {
    transform: translateY(-2px);
    border-color: var(--card-border);
    box-shadow: var(--card-glow);
}

.mask-option[aria-pressed="true"] {
    border-color: var(--gold);
    background: rgba(255, 212, 0, 0.1);
    box-shadow: 0 0 18px rgba(255, 212, 0, 0.45);
}

/* Галочка на вибраній масці */
.mask-option[aria-pressed="true"]::after {
    content: "";
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m6 12.5 4 4 8-9' fill='none' stroke='%23061a5c' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")
            center / 16px no-repeat,
        var(--gold);
}

.mask-option__thumb {
    display: grid;
    place-items: center;
    width: 100%;
    aspect-ratio: 1;
}

.mask-option__thumb img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
}

.mask-option__label {
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.02em;
}

/* Поки завантажується повнорозмірна маска */
.mask-option.is-loading .mask-option__thumb {
    opacity: 0.45;
}

.mask-option.is-loading::before {
    content: "";
    position: absolute;
    top: calc(50% - 26px);
    left: calc(50% - 16px);
    width: 32px;
    height: 32px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-top-color: var(--gold);
    border-radius: 50%;
    animation: mask-spin 0.8s linear infinite;
}

/* ---------- Розмір і поворот ---------- */

.mask-controls {
    border-color: #218bd2;
}

/* legend у fieldset — float, щоб стилі заголовка працювали однаково */
.mask-controls > legend {
    float: left;
    width: 100%;
}

.mask-controls > legend + * {
    clear: both;
}

.mask-controls:disabled .mask-control,
.mask-controls:disabled .mask-controls__reset {
    opacity: 0.45;
}

.mask-control + .mask-control {
    margin-top: 14px;
}

.mask-control__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 4px;
    font-size: 15px;
    font-weight: 500;
}

.mask-control__head output {
    color: var(--gold);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.mask-control__row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.mask-controls__reset {
    width: 100%;
    margin-top: 18px;
}

/* Повзунок: заповнена частина від --from до --to (виставляє JS) */
.mask-range {
    --from: 0%;
    --to: 50%;
    --track: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0.22) var(--from),
        var(--gold) var(--from),
        var(--gold) var(--to),
        rgba(255, 255, 255, 0.22) var(--to)
    );
    flex: 1;
    min-width: 0;
    height: 44px;
    margin: 0;
    background: transparent;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
}

.mask-range:disabled {
    cursor: not-allowed;
}

.mask-range:focus-visible {
    outline-offset: 0;
}

.mask-range::-webkit-slider-runnable-track {
    height: 6px;
    border-radius: 999px;
    background: var(--track);
}

.mask-range::-moz-range-track {
    height: 6px;
    border-radius: 999px;
    background: var(--track);
}

.mask-range::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
    margin-top: -9px;
    border: 3px solid var(--navy-900);
    border-radius: 50%;
    background: var(--gold);
    box-shadow:
        0 0 0 2px var(--gold),
        0 0 12px rgba(255, 212, 0, 0.6);
    -webkit-appearance: none;
}

.mask-range::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border: 3px solid var(--navy-900);
    border-radius: 50%;
    background: var(--gold);
    box-shadow:
        0 0 0 2px var(--gold),
        0 0 12px rgba(255, 212, 0, 0.6);
}

/* =========================================================
   Планшет: редактор на всю ширину, маски й керування під ним
   ========================================================= */

@media (max-width: 900px) {
    .mask-studio__layout {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-rows: none;
        grid-template-areas:
            "upload upload"
            "stage stage"
            "masks controls"
            "save save";
        gap: 16px 20px;
        align-items: stretch;
    }

    /* Кнопка в порожній рамці дублює крок 1, що стоїть одразу над нею */
    .mask-stage__empty .mask-btn,
    .mask-stage__empty-wide {
        display: none;
    }

    .mask-stage__empty-narrow {
        display: inline;
    }

    .mask-stage__empty-text {
        margin-bottom: 0;
    }
}

/* =========================================================
   Мобайл: усе в одну колонку, маски в ряд під фото
   ========================================================= */

@media (max-width: 600px) {
    .mask-studio {
        padding-bottom: 32px;
    }

    .mask-studio__layout {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas:
            "upload"
            "stage"
            "masks"
            "controls"
            "save";
        gap: 14px;
    }

    .mask-stage {
        padding: 8px;
        border-radius: 18px;
    }

    .mask-stage:not(.has-photo) {
        aspect-ratio: 4 / 5;
        max-height: 62vh;
    }

    .mask-stage__canvas {
        border-radius: 10px;
    }

    .mask-step {
        padding: 16px 16px 18px;
        border-radius: 18px;
    }

    .mask-step__title {
        margin-bottom: 14px;
        font-size: 17px;
    }

    .mask-step__action {
        min-height: 52px;
        font-size: 15px;
    }

    .mask-picker {
        gap: 10px;
    }

    .mask-option {
        padding: 8px 4px 6px;
        border-radius: 14px;
    }

    .mask-option__label {
        font-size: 12px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .mask-option,
    .mask-stage {
        transition: none;
    }

    .mask-option:hover {
        transform: none;
    }
}
