/* ---------- Cordial photo editor (photoedit.js) ---------- */
dialog.photo-editor:not([open]) { display: none; }

dialog.photo-editor {
    --pe-stage: #000;
    --pe-ease: var(--ease-out, cubic-bezier(0.22, 1, 0.36, 1));
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: var(--surface);
    color: var(--text);
    box-shadow: none;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    overscroll-behavior: contain;
    -webkit-tap-highlight-color: transparent;
}
dialog.photo-editor[open] {
    animation: pe-in 0.22s var(--pe-ease);
}
dialog.photo-editor::backdrop {
    background: rgba(8, 10, 16, 0.62);
    backdrop-filter: none;
    animation: pe-fade 0.22s var(--pe-ease);
}
@keyframes pe-in { from { opacity: 0; transform: scale(0.97); } }
@keyframes pe-fade { from { opacity: 0; } }

.photo-editor :where(button) {
    font: inherit;
    color: inherit;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}
.photo-editor :focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ---------- Top bar ---------- */
.pe-top {
    flex: none;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 8px;
    min-height: 56px;
    padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(8px, env(safe-area-inset-left, 0px));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.pe-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 650;
    letter-spacing: -0.01em;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.pe-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    min-width: 44px;
    padding: 0 14px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    font-size: 0.95rem;
    font-weight: 550;
    cursor: pointer;
    transition: background-color 0.18s var(--pe-ease), color 0.18s var(--pe-ease), transform 0.15s var(--pe-ease), opacity 0.18s var(--pe-ease);
}
.pe-btn:active:not(:disabled) { transform: scale(0.96); }
.pe-btn:disabled { cursor: default; opacity: 0.55; }
.pe-cancel { justify-self: start; color: var(--text-muted); }
.pe-done {
    justify-self: end;
    min-width: 76px;
    height: 36px;
    min-height: 36px;
    padding: 0 18px;
    background: var(--accent);
    color: var(--on-accent);
    font-weight: 650;
    /* 44px hit area around a 36px pill */
    position: relative;
}
.pe-done::after { content: ""; position: absolute; inset: -4px; }
.pe-done[aria-busy="true"] { opacity: 0.8; }

/* ---------- Body / stage ---------- */
.pe-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}
.pe-stage {
    position: relative;
    flex: 1;
    min-height: 0;
    display: grid;
    place-items: center;
    overflow: hidden;
    background: var(--pe-stage);
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
.pe-frame {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    transition: opacity 0.2s var(--pe-ease);
}
.pe-canvas-slot, .pe-layers, .pe-layer { position: absolute; inset: 0; }
.pe-canvas { display: block; width: 100%; height: 100%; }
.pe-layers { pointer-events: none; }
.pe-comparing .pe-canvas { filter: none !important; }
.pe-comparing .pe-layers { visibility: hidden; }

.pe-status {
    position: absolute;
    left: 50%;
    bottom: 16px;
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: 0.85rem;
    pointer-events: none;
}
.pe-status:empty { display: none; }
.pe-loading .pe-status { top: 50%; bottom: auto; transform: translate(-50%, -50%); background: transparent; color: rgba(255, 255, 255, 0.75); }
.pe-loading .pe-frame { opacity: 0; }

/* ---------- Controls ---------- */
.pe-controls {
    flex: none;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}
.pe-panels { position: relative; }
.pe-panel[hidden] { display: none; }

.pe-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
}
.pe-tabs {
    flex: 1;
    display: flex;
    gap: 2px;
    padding: 3px;
    border-radius: 999px;
    background: var(--panel);
    border: 1px solid var(--border);
}
.pe-tab {
    flex: 1;
    min-height: 40px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s var(--pe-ease), color 0.2s var(--pe-ease), box-shadow 0.2s var(--pe-ease);
}
.pe-tab[aria-selected="true"] {
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 1px var(--border);
}
.pe-reset { flex: none; color: var(--text-muted); font-size: 0.9rem; padding: 0 12px; }

/* Filter strip */
.pe-strip {
    display: flex;
    gap: 10px;
    padding: 14px 16px 10px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    scroll-padding: 0 16px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}
.pe-strip::-webkit-scrollbar { display: none; }
.pe-preset {
    flex: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    width: 68px;
    padding: 0;
    border: 0;
    background: none;
    cursor: pointer;
    scroll-snap-align: start;
}
.pe-thumb-wrap {
    display: block;
    width: 64px;
    height: 64px;
    border-radius: 14px;
    overflow: hidden;
    background: var(--panel);
    box-shadow: 0 0 0 1px var(--border);
    transition: box-shadow 0.2s var(--pe-ease), transform 0.2s var(--pe-ease);
}
.pe-thumb { display: block; width: 100%; height: 100%; }
.pe-preset-label {
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--text-muted);
    transition: color 0.2s var(--pe-ease);
}
.pe-preset:active .pe-thumb-wrap { transform: scale(0.95); }
.pe-preset[aria-pressed="true"] .pe-thumb-wrap { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
.pe-preset[aria-pressed="true"] .pe-preset-label { color: var(--accent); font-weight: 650; }
.pe-preset:focus-visible { outline: none; }
.pe-preset:focus-visible .pe-thumb-wrap { outline: 2px solid var(--accent); outline-offset: 5px; }

/* Sliders */
.pe-sliders {
    display: grid;
    gap: 2px;
    padding: 8px 16px 6px;
    max-height: min(34vh, 300px);
    overflow-y: auto;
    overscroll-behavior: contain;
}
.pe-row-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.pe-label {
    font-size: 0.9rem;
    font-weight: 550;
    color: var(--text);
    cursor: pointer;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    padding: 4px 0;
}
.pe-val {
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-faint);
    min-width: 3ch;
    text-align: right;
    transition: color 0.18s var(--pe-ease);
}
.pe-changed .pe-val { color: var(--accent); font-weight: 600; }

.pe-range {
    --pe-a: 0%;
    --pe-b: 0%;
    --pe-track: var(--border-strong, var(--border));
    display: block;
    width: 100%;
    height: 32px;
    margin: 0;
    background: transparent;
    font-size: 16px;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    touch-action: pan-y;
}
.pe-range::-webkit-slider-runnable-track {
    height: 4px;
    border-radius: 4px;
    background: linear-gradient(to right, var(--pe-track) var(--pe-a), var(--accent) var(--pe-a), var(--accent) var(--pe-b), var(--pe-track) var(--pe-b));
}
.pe-range::-moz-range-track {
    height: 4px;
    border-radius: 4px;
    background: linear-gradient(to right, var(--pe-track) var(--pe-a), var(--accent) var(--pe-a), var(--accent) var(--pe-b), var(--pe-track) var(--pe-b));
}
.pe-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 22px;
    height: 22px;
    margin-top: -9px;
    border-radius: 50%;
    background: var(--surface);
    border: 0;
    box-shadow: 0 0 0 1.5px var(--accent), 0 2px 6px rgba(0, 0, 0, 0.2);
    transition: transform 0.15s var(--pe-ease);
}
.pe-range::-moz-range-thumb {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--surface);
    border: 0;
    box-shadow: 0 0 0 1.5px var(--accent), 0 2px 6px rgba(0, 0, 0, 0.2);
}
.pe-range:active::-webkit-slider-thumb { transform: scale(1.12); }
.pe-range:focus-visible { outline: none; }
.pe-range:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 1.5px var(--accent), 0 0 0 5px var(--accent-soft); }
.pe-range:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 1.5px var(--accent), 0 0 0 5px var(--accent-soft); }

/* Crop */
.pe-panel-crop {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px 16px 12px;
}
.pe-aspects {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}
.pe-aspect {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 76px;
    padding: 8px 4px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--panel);
    cursor: pointer;
    transition: border-color 0.2s var(--pe-ease), background-color 0.2s var(--pe-ease), color 0.2s var(--pe-ease), transform 0.15s var(--pe-ease);
}
.pe-aspect:active { transform: scale(0.96); }
.pe-aspect-box { display: grid; place-items: center; width: 24px; height: 24px; margin-bottom: 2px; }
.pe-aspect-icon { display: block; border: 1.75px solid currentColor; border-radius: 3px; color: var(--text-muted); }
.pe-aspect-icon.pe-dashed { border-style: dashed; }
.pe-aspect-label { font-size: 0.8rem; font-weight: 600; }
.pe-aspect-sub { font-size: 0.72rem; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.pe-aspect[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.pe-aspect[aria-pressed="true"] .pe-aspect-icon,
.pe-aspect[aria-pressed="true"] .pe-aspect-sub { color: var(--accent); }
.pe-rotate {
    align-self: stretch;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--panel);
    font-size: 0.9rem;
}
.pe-rotate svg { flex: none; }

/* ---------- Hover (fine pointers only) ---------- */
@media (hover: hover) and (pointer: fine) {
    .pe-cancel:hover:not(:disabled), .pe-reset:hover { background: var(--panel); color: var(--text); }
    .pe-done:hover:not(:disabled) { background: var(--accent-hover, var(--accent)); }
    .pe-tab:hover:not([aria-selected="true"]) { color: var(--text); }
    .pe-preset:hover .pe-thumb-wrap { transform: translateY(-2px); }
    .pe-preset:hover .pe-preset-label { color: var(--text); }
    .pe-preset[aria-pressed="true"]:hover .pe-preset-label { color: var(--accent); }
    .pe-aspect:hover:not([aria-pressed="true"]), .pe-rotate:hover { border-color: var(--border-strong, var(--border)); background: var(--input, var(--panel)); }
}

/* ---------- Desktop / tablet ---------- */
@media (min-width: 760px) {
    dialog.photo-editor {
        position: fixed;
        inset: 0;
        width: min(1000px, calc(100vw - 48px));
        height: min(760px, 90vh);
        margin: auto;
        border-radius: 20px;
        border: 1px solid var(--border);
        box-shadow: var(--shadow-lg);
    }
    .pe-top { padding: 0 12px; min-height: 60px; }
    .pe-title { font-size: 1.05rem; }
    .pe-body { flex-direction: row; }
    .pe-stage { --pe-stage: #0c0d10; }
    .pe-controls {
        width: 340px;
        border-top: 0;
        border-left: 1px solid var(--border);
        padding: 0;
        min-height: 0;
    }
    .pe-toolbar {
        order: -1;
        border-top: 0;
        border-bottom: 1px solid var(--border);
        padding: 12px 14px;
    }
    .pe-panels { flex: 1; min-height: 0; overflow-y: auto; }
    .pe-strip {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 16px 10px;
        padding: 18px 16px;
        overflow: visible;
    }
    .pe-preset { width: auto; }
    .pe-thumb-wrap { width: 84px; height: 84px; border-radius: 16px; }
    .pe-sliders { max-height: none; padding: 14px 18px; gap: 8px; overflow: visible; }
    .pe-panel-crop { padding: 18px 16px; }
    .pe-aspects { grid-template-columns: repeat(2, 1fr); }
    .pe-aspect { min-height: 84px; }
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
    dialog.photo-editor, dialog.photo-editor::backdrop { animation: none; }
    .photo-editor *, .photo-editor *::before, .photo-editor *::after { transition-duration: 0.01ms !important; }
    .pe-preset:active .pe-thumb-wrap, .pe-btn:active, .pe-aspect:active { transform: none; }
}
:root[data-motion="reduce"] dialog.photo-editor,
:root[data-motion="reduce"] dialog.photo-editor::backdrop { animation: none !important; }
:root[data-motion="reduce"] .photo-editor *,
:root[data-motion="reduce"] .photo-editor *::before,
:root[data-motion="reduce"] .photo-editor *::after { transition-duration: 0.01ms !important; }
:root[data-motion="reduce"] .pe-preset:active .pe-thumb-wrap,
:root[data-motion="reduce"] .pe-btn:active,
:root[data-motion="reduce"] .pe-aspect:active { transform: none; }
