/* Shared Professional Image Editor */
.image-editor-modal {
    position: fixed;
    inset: 0;
    z-index: 1400;
    display: none;
    padding: clamp(.5rem, 1.5vw, 1.25rem);
    background: var(--overlay-bg, rgba(0, 0, 0, .72));
    color: var(--modal-text, var(--text));
}

.image-editor-modal.is-open {
    display: grid;
    place-items: center;
}

body.image-editor-open {
    overflow: hidden;
}

.image-editor-shell {
    position: relative;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    width: min(1500px, 100%);
    height: min(940px, 100%);
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg, 18px);
    background: var(--modal-bg, var(--surface-card));
    box-shadow: var(--shadow-lg);
}

.image-editor-header,
.image-editor-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: .85rem 1rem;
    background: var(--surface-card);
    border-color: var(--border);
}

.image-editor-header { border-bottom: 1px solid var(--border); }
.image-editor-footer { border-top: 1px solid var(--border); }
.image-editor-header h2 { margin: 0; font-size: clamp(1.05rem, 2vw, 1.35rem); }
.image-editor-header p { margin: .2rem 0 0; color: var(--text-muted); font-size: .9rem; }
.image-editor-header-actions,
.image-editor-footer-actions { display: flex; align-items: center; gap: .5rem; }

.image-editor-icon-btn,
.image-editor-toolbar button,
.image-editor-ratio-row button,
.image-editor-presets button,
.image-editor-range-row button {
    min-width: 44px;
    min-height: 44px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 10px);
    background: var(--button-secondary-bg, var(--surface-muted));
    color: var(--button-secondary-text, var(--text));
    cursor: pointer;
}

.image-editor-icon-btn:hover,
.image-editor-toolbar button:hover,
.image-editor-ratio-row button:hover,
.image-editor-presets button:hover,
.image-editor-ratio-row button.is-active {
    border-color: var(--primary);
    color: var(--primary);
}

.image-editor-icon-btn:focus-visible,
.image-editor-toolbar button:focus-visible,
.image-editor-ratio-row button:focus-visible,
.image-editor-presets button:focus-visible,
.image-editor-range-row button:focus-visible,
.image-editor-control input:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
}

.image-editor-layout {
    display: grid;
    grid-template-columns: 112px minmax(0, 1fr) minmax(290px, 360px);
    min-height: 0;
    background: var(--surface-subtle, var(--bg));
}

.image-editor-toolbar {
    display: flex;
    flex-direction: column;
    gap: .45rem;
    padding: .75rem;
    overflow-y: auto;
    border-right: 1px solid var(--border);
    background: var(--surface-card);
}

.image-editor-toolbar button {
    display: grid;
    justify-items: center;
    gap: .25rem;
    padding: .55rem .35rem;
    font: inherit;
    font-size: .75rem;
}

.image-editor-toolbar i { font-size: 1rem; }

.image-editor-workspace {
    display: grid;
    grid-template-rows: minmax(280px, 1fr) auto auto;
    min-width: 0;
    min-height: 0;
    padding: .75rem;
    gap: .65rem;
    overflow: hidden auto;
}

.image-editor-stage {
    position: relative;
    min-height: 320px;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 12px);
    background-color: #25282d;
    background-image:
        linear-gradient(45deg, #33373d 25%, transparent 25%),
        linear-gradient(-45deg, #33373d 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #33373d 75%),
        linear-gradient(-45deg, transparent 75%, #33373d 75%);
    background-size: 24px 24px;
    background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}

.image-editor-stage > img {
    display: block;
    max-width: 100%;
}

.image-editor-ratio-row,
.image-editor-presets {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: .4rem;
}

.image-editor-ratio-row > span { color: var(--text-muted); font-weight: 600; }
.image-editor-ratio-row button,
.image-editor-presets button { min-height: 38px; padding: .35rem .6rem; }

.image-editor-compare {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .65rem;
}

.image-editor-compare figure {
    display: grid;
    justify-items: center;
    align-content: start;
    gap: .35rem;
    min-width: 0;
    margin: 0;
    padding: .55rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 12px);
    background: var(--surface-card);
}

.image-editor-compare figcaption { color: var(--text-muted); font-size: .82rem; }
.image-editor-compare canvas {
    display: block;
    width: auto;
    max-width: 100%;
    max-height: 240px;
    border-radius: var(--radius-sm, 8px);
    background: #fff;
}

.image-editor-panel {
    min-width: 0;
    overflow-y: auto;
    padding: .75rem;
    border-left: 1px solid var(--border);
    background: var(--surface-card);
}

.image-editor-presets { margin-bottom: .8rem; }
.image-editor-presets button { flex: 1 1 calc(50% - .4rem); font: inherit; }

.image-editor-controls { display: grid; gap: .75rem; }
.image-editor-control {
    padding: .6rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md, 10px);
    background: var(--surface-muted);
}

.image-editor-control > label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: .5rem;
    margin-bottom: .35rem;
    color: var(--text);
    font-size: .88rem;
}

.image-editor-control output {
    min-width: 3.25rem;
    text-align: right;
    color: var(--primary);
    font-variant-numeric: tabular-nums;
}

.image-editor-range-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 38px;
    align-items: center;
    gap: .5rem;
}

.image-editor-range-row input { width: 100%; accent-color: var(--primary); }
.image-editor-range-row button { min-width: 38px; min-height: 38px; }

.image-editor-status {
    color: var(--text-muted);
    font-size: .9rem;
}

.image-editor-discard {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: var(--overlay-bg, rgba(0, 0, 0, .72));
}

.image-editor-discard[hidden] { display: none; }
.image-editor-discard-box {
    width: min(440px, 100%);
    padding: 1.2rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg, 16px);
    background: var(--modal-bg, var(--surface-card));
    color: var(--modal-text, var(--text));
    box-shadow: var(--shadow-lg);
}
.image-editor-discard-box h3 { margin-top: 0; }
.image-editor-discard-box > div { display: flex; justify-content: flex-end; gap: .6rem; }

.image-editor-field {
    display: grid;
    grid-template-columns: minmax(0, 140px) minmax(0, 1fr);
    align-items: start;
    gap: .8rem;
}

.image-editor-field-preview {
    position: relative;
    display: grid;
    place-items: center;
    min-height: 160px;
    overflow: hidden;
    border: 1px dashed var(--border);
    border-radius: var(--radius-md, 12px);
    background: var(--surface-muted);
}

.image-editor-field-preview img {
    display: block;
    width: 100%;
    max-height: 220px;
    object-fit: contain;
}

.image-editor-field-actions { display: grid; gap: .55rem; }
.image-editor-field-actions .image-editor-help { margin: 0; color: var(--text-muted); font-size: .85rem; }
.image-editor-bulk-cell { display: grid; gap: .4rem; min-width: 220px; }
.image-editor-edit-btn { justify-content: center; }

@media (max-width: 1050px) {
    .image-editor-layout { grid-template-columns: 78px minmax(0, 1fr) minmax(250px, 320px); }
    .image-editor-toolbar button span { display: none; }
    .image-editor-toolbar button { min-height: 48px; }
}

@media (max-width: 760px) {
    .image-editor-modal { padding: 0; }
    .image-editor-shell { width: 100%; height: 100%; border: 0; border-radius: 0; }
    .image-editor-header { padding: .65rem .75rem; }
    .image-editor-header p { display: none; }
    .image-editor-layout {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(330px, 52vh) auto minmax(220px, 1fr);
        overflow-y: auto;
    }
    .image-editor-toolbar {
        grid-row: 2;
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        border: 0;
        border-top: 1px solid var(--border);
        border-bottom: 1px solid var(--border);
    }
    .image-editor-toolbar button { flex: 0 0 52px; }
    .image-editor-workspace { grid-row: 1; overflow: visible; padding: .5rem; }
    .image-editor-stage { min-height: 300px; }
    .image-editor-compare { display: none; }
    .image-editor-panel { grid-row: 3; border-left: 0; overflow: visible; }
    .image-editor-footer { position: sticky; bottom: 0; z-index: 5; padding: .55rem; }
    .image-editor-status { display: none; }
    .image-editor-footer-actions { width: 100%; }
    .image-editor-footer-actions .btn { flex: 1; min-height: 48px; }
    .image-editor-field { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
    .image-editor-modal *,
    .image-editor-modal *::before,
    .image-editor-modal *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
    }
}

@media print {
    .image-editor-modal { display: none !important; }
}

/* =========================================================
   Compact full-screen editor + four-corner perspective crop
   ========================================================= */
.image-editor-modal { padding: 4px; }
.image-editor-shell {
    width: calc(100vw - 8px);
    height: calc(100dvh - 8px);
    max-width: none;
    max-height: none;
    border-radius: 14px;
}
.image-editor-header,
.image-editor-footer { padding: .45rem .65rem; gap: .55rem; }
.image-editor-header h2 { font-size: clamp(.98rem, 1.7vw, 1.18rem); }
.image-editor-header p { margin-top: .08rem; font-size: .78rem; }
.image-editor-layout { grid-template-columns: 76px minmax(0, 1fr) minmax(238px, 292px); }
.image-editor-toolbar { gap: .28rem; padding: .35rem; }
.image-editor-toolbar button {
    min-height: 42px;
    padding: .34rem .2rem;
    font-size: .66rem;
}
.image-editor-toolbar i { font-size: .92rem; }
.image-editor-tool-primary {
    background: var(--button-bg) !important;
    color: var(--button-text) !important;
    border-color: var(--button-bg) !important;
}
.image-editor-workspace {
    grid-template-rows: minmax(0, 1fr) auto auto;
    padding: .35rem;
    gap: .35rem;
    overflow: hidden;
}
.image-editor-stage { min-height: 0; height: 100%; }
.image-editor-panel { padding: .4rem; }
.image-editor-presets { margin-bottom: .4rem; gap: .28rem; }
.image-editor-presets button { min-height: 32px; padding: .22rem .4rem; font-size: .78rem; }
.image-editor-controls { gap: .35rem; }
.image-editor-control { padding: .35rem .45rem; }
.image-editor-control > label { margin-bottom: .15rem; font-size: .76rem; }
.image-editor-range-row { grid-template-columns: minmax(0, 1fr) 32px; gap: .35rem; }
.image-editor-range-row button { min-width: 32px; min-height: 32px; }
.image-editor-range-row input { min-height: 24px; }
.image-editor-footer-actions .btn { min-height: 40px; padding-block: .35rem; }
.image-editor-status { font-size: .78rem; }

.image-editor-shell.is-panel-collapsed .image-editor-layout {
    grid-template-columns: 76px minmax(0, 1fr) 0;
}
.image-editor-shell.is-panel-collapsed .image-editor-panel {
    display: none;
}
.image-editor-compare.is-collapsed { display: none; }
.image-editor-compare:not(.is-collapsed) {
    max-height: 130px;
    overflow: hidden;
}
.image-editor-compare:not(.is-collapsed) canvas { max-height: 90px; }

.image-editor-crop-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(220px, .7fr);
    gap: .35rem;
    align-items: center;
}
.image-editor-ratio-row,
.image-editor-zoom-row {
    min-height: 36px;
    padding: .2rem .35rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 9px);
    background: var(--surface-card);
}
.image-editor-ratio-row { gap: .25rem; }
.image-editor-ratio-row button { min-width: 40px; min-height: 30px; padding: .15rem .35rem; font-size: .72rem; }
.image-editor-zoom-row {
    display: grid;
    grid-template-columns: auto minmax(80px, 1fr) 46px 32px;
    gap: .35rem;
    align-items: center;
}
.image-editor-zoom-row label { display: inline-flex; gap: .3rem; align-items: center; font-size: .75rem; color: var(--text-muted); }
.image-editor-zoom-row input { width: 100%; accent-color: var(--primary); }
.image-editor-zoom-row output { font-size: .72rem; color: var(--primary); text-align: right; font-variant-numeric: tabular-nums; }
.image-editor-zoom-row button { min-width: 32px; min-height: 30px; padding: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-muted); color: var(--text); }

.image-editor-perspective {
    position: absolute;
    inset: 0;
    z-index: 15;
    overflow: hidden;
    background: #171b20;
    touch-action: none;
}
.image-editor-perspective[hidden] { display: none; }
.image-editor-perspective canvas {
    display: block;
    width: 100%;
    height: 100%;
}
.perspective-handle {
    position: absolute;
    z-index: 4;
    width: 30px;
    height: 30px;
    min-width: 30px;
    min-height: 30px;
    padding: 0;
    border: 3px solid #fff;
    border-radius: 50%;
    background: #00a9d6;
    color: #fff;
    box-shadow: 0 2px 8px rgba(0,0,0,.45);
    transform: translate(-50%, -50%);
    cursor: grab;
    touch-action: none;
}
.perspective-handle:active { cursor: grabbing; }
.perspective-handle span { display: grid; place-items: center; font-size: .7rem; font-weight: 800; }
.perspective-actions {
    position: absolute;
    left: 50%;
    bottom: 10px;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: .45rem;
    max-width: calc(100% - 20px);
    padding: .35rem .45rem;
    border: 1px solid rgba(255,255,255,.2);
    border-radius: 12px;
    background: rgba(5,17,24,.88);
    color: #fff;
    box-shadow: 0 8px 28px rgba(0,0,0,.35);
    transform: translateX(-50%);
    backdrop-filter: blur(8px);
}
.perspective-actions span { font-size: .75rem; white-space: nowrap; }
.perspective-actions .btn { min-height: 34px; padding: .25rem .55rem; font-size: .75rem; }

@media (max-width: 1050px) {
    .image-editor-layout { grid-template-columns: 62px minmax(0, 1fr) minmax(220px, 270px); }
    .image-editor-shell.is-panel-collapsed .image-editor-layout { grid-template-columns: 62px minmax(0, 1fr) 0; }
    .image-editor-crop-controls { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
    .image-editor-modal { padding: 0; }
    .image-editor-shell { width: 100vw; height: 100dvh; border-radius: 0; }
    .image-editor-layout { grid-template-rows: minmax(0, 1fr) auto minmax(150px, 34vh); }
    .image-editor-shell.is-panel-collapsed .image-editor-layout { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto 0; }
    .image-editor-workspace { min-height: 0; overflow: hidden; }
    .image-editor-stage { min-height: 0; height: 100%; }
    .image-editor-panel { max-height: 34vh; overflow-y: auto; }
    .image-editor-toolbar { padding: .25rem; }
    .image-editor-toolbar button { flex-basis: 46px; min-height: 42px; }
    .image-editor-crop-controls { grid-template-columns: 1fr; }
    .image-editor-ratio-row { overflow-x: auto; flex-wrap: nowrap; }
    .image-editor-ratio-row > span { flex: 0 0 auto; }
    .image-editor-zoom-row { grid-template-columns: auto minmax(80px, 1fr) 42px 32px; }
    .perspective-actions { left: 8px; right: 8px; bottom: 8px; transform: none; flex-wrap: wrap; justify-content: center; }
    .perspective-actions span { width: 100%; text-align: center; white-space: normal; }
    .perspective-handle { width: 34px; height: 34px; min-width: 34px; min-height: 34px; }
}

/* =========================================================
   Perspective workspace UX fix
   - Commands stay outside the image
   - Four-corner mode uses the largest possible workspace
   ========================================================= */
.perspective-command-bar {
    position: static;
    left: auto;
    right: auto;
    bottom: auto;
    z-index: auto;
    display: none;
    width: 100%;
    max-width: none;
    min-height: 44px;
    padding: .35rem .5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 9px);
    background: var(--surface-card);
    color: var(--text);
    box-shadow: none;
    transform: none;
    backdrop-filter: none;
}
.image-editor-modal.is-perspective-mode .perspective-command-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
}
.perspective-command-bar > span {
    min-width: 0;
    color: var(--text-muted);
    font-size: .74rem;
    white-space: normal;
}
.perspective-action-buttons {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: .35rem;
    flex: 0 0 auto;
}
.perspective-command-bar .btn {
    min-height: 34px;
    padding: .25rem .5rem;
    font-size: .72rem;
    white-space: nowrap;
}

.image-editor-modal.is-perspective-mode .image-editor-shell .image-editor-layout {
    grid-template-columns: 62px minmax(0, 1fr) 0;
}
.image-editor-modal.is-perspective-mode .image-editor-panel {
    display: none;
}
.image-editor-modal.is-perspective-mode .image-editor-workspace {
    grid-template-rows: minmax(0, 1fr) auto;
    overflow: hidden;
}
.image-editor-modal.is-perspective-mode .image-editor-crop-controls,
.image-editor-modal.is-perspective-mode .image-editor-compare {
    display: none !important;
}
.image-editor-modal.is-perspective-mode .image-editor-stage {
    min-height: 0;
    height: 100%;
}
.image-editor-modal.is-perspective-mode .image-editor-toolbar button:disabled {
    display: none;
}
.image-editor-modal.is-perspective-mode .image-editor-toolbar {
    justify-content: flex-start;
}

/* Opening state: prioritize the image. The adjustment panel can be reopened
   from the sliders button in the header. */
.image-editor-shell.is-panel-collapsed .image-editor-stage {
    min-height: 0;
    height: 100%;
}

@media (max-width: 760px) {
    .image-editor-modal.is-perspective-mode .image-editor-layout,
    .image-editor-modal.is-perspective-mode .image-editor-shell.is-panel-collapsed .image-editor-layout {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(0, 1fr) auto;
    }
    .image-editor-modal.is-perspective-mode .image-editor-toolbar {
        display: none;
    }
    .image-editor-modal.is-perspective-mode .image-editor-workspace {
        padding: .2rem;
        gap: .25rem;
    }
    .image-editor-modal.is-perspective-mode .perspective-command-bar {
        align-items: stretch;
        flex-direction: column;
        gap: .3rem;
        padding: .3rem;
    }
    .perspective-command-bar > span {
        text-align: center;
        font-size: .7rem;
    }
    .perspective-action-buttons {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
    }
    .perspective-command-bar .btn {
        width: 100%;
        min-height: 40px;
    }
}
