/* ═══════════════════════════════════════════════════════════════
   Image Edit Modal — Vertical layout (image top, prompt bottom)
   Used by: RoomFlow, SnapRender, etc.
   ═══════════════════════════════════════════════════════════════ */

/* ── Overlay ── */
.iem-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10002;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
}
.iem-overlay.active {
    display: flex;
}

/* ── Modal container ── */
.iem-modal {
    position: relative;
    background: #f5f3f0;
    border-radius: 16px;
    width: min(94vw, 1260px);
    height: min(90vh, 860px);
    display: flex;
    flex-direction: column;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.3);
    overflow: visible;
    animation: iemSlideIn 0.25s ease;
}
/* ── Body: horizontal split (main + history) ── */
.iem-body {
    flex: 1;
    overflow: hidden;
    border-radius: 16px;
    min-height: 0;
    display: flex;
}
.iem-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}
@keyframes iemSlideIn {
    from { opacity: 0; transform: translateY(16px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ── Close button (absolute top-right) ── */
.iem-close-btn {
    position: absolute;
    top: -16px;
    right: -16px;
    z-index: 20;
    width: 32px;
    height: 32px;
    border: none;
    background: rgba(0,0,0,0.55);
    border-radius: 50%;
    font-size: 20px;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    line-height: 1;
}
.iem-close-btn:hover {
    background: rgba(0,0,0,0.75);
}

/* ── Image canvas area (top, fills available space) ── */
.iem-canvas-area {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
    background: #eae7e2;
}
.iem-canvas-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 20px 20px 8px;
    position: relative;
}
.iem-canvas-wrap canvas {
    max-width: 100%;
    max-height: 100%;
    border-radius: 8px;
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.1);
}

/* ── Canvas overlay tools for the reusable modal ── */
#iemOverlay .canvas-toolbar {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}
#iemOverlay .canvas-toolbar > * {
    pointer-events: auto;
}
#iemOverlay .canvas-tool-group {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: #fff;
    border-radius: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}
#iemOverlay .canvas-color-dot {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.15s;
    flex-shrink: 0;
}
#iemOverlay .canvas-color-dot:hover {
    transform: scale(1.15);
}
#iemOverlay .canvas-color-dot.active {
    border-color: #333;
    box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.2);
}
#iemOverlay .canvas-color-dot[data-color="#ffffff"] {
    border-color: #ccc;
}
#iemOverlay .canvas-color-dot[data-color="#ffffff"].active {
    border-color: #333;
}
#iemOverlay .canvas-tool-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    transition: all 0.15s;
    color: #555;
}
#iemOverlay .canvas-tool-btn svg {
    width: 20px;
    height: 20px;
}
#iemOverlay .canvas-tool-btn:hover {
    background: #f0f0f0;
}
#iemOverlay .canvas-tool-btn.active {
    background: #e8eaf6;
}
#iemOverlay .canvas-tool-btn.active svg {
    stroke: #1a73e8;
}
#iemOverlay .canvas-actions {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: #fff;
    border-radius: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}
#iemOverlay .canvas-action-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 50%;
    background: transparent;
    cursor: pointer;
    color: #555;
}
#iemOverlay .canvas-action-btn svg {
    width: 18px;
    height: 18px;
}
#iemOverlay .canvas-action-btn:hover {
    background: #f0f0f0;
}

/* ── Generating overlay (dark mask over canvas only) ── */
.iem-canvas-wrap.generating .canvas-container {
    position: relative;
}
.iem-canvas-wrap.generating .canvas-container::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(30, 30, 30, 0.55);
    border-radius: 8px;
    z-index: 5;
    pointer-events: none;
}

/* ── Disabled state for undo/redo in edit modal ── */
.iem-canvas-area .canvas-action-btn:disabled {
    opacity: 0.3;
    cursor: default;
}

/* ── Bottom bar ── */
.iem-bottom-bar {
    flex-shrink: 0;
    background: #eae7e2;
    padding: 0 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ── Right history panel ── */
.iem-history-panel {
    width: 160px;
    flex-shrink: 0;
    background: #edeae6;
    border-left: 1px solid #ddd;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.iem-hp-title {
    font-size: 12px;
    font-weight: 600;
    color: #666;
    padding: 14px 12px 8px;
    flex-shrink: 0;
}
.iem-hp-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 10px 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.iem-hp-list::-webkit-scrollbar {
    width: 4px;
}
.iem-hp-list::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 2px;
}
.iem-hp-item {
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: border-color 0.2s, box-shadow 0.2s;
    flex-shrink: 0;
}
.iem-hp-item:hover {
    border-color: #4A90D9;
    box-shadow: 0 2px 8px rgba(74,144,217,0.25);
}
.iem-hp-item img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    display: block;
}
.iem-hp-item .iem-hp-step {
    position: absolute;
    top: 4px;
    left: 4px;
    background: rgba(0,0,0,0.5);
    color: #fff;
    font-size: 10px;
    padding: 1px 5px;
    border-radius: 4px;
    line-height: 1.4;
}
.iem-hp-empty {
    padding: 20px 12px;
    font-size: 11px;
    color: #aaa;
    text-align: center;
    line-height: 1.5;
}

/* ── Prompt card (imageforge style) ── */
.iem-prompt-card {
    background: #fff;
    border: 1.5px solid #e0e0e0;
    border-radius: 12px;
    padding: 12px 14px 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    transition: border-color 0.2s;
}
/* Prompt row — ref image + textarea side by side */
.iem-prompt-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
}
.iem-prompt-row .iem-prompt {
    flex: 1;
    min-width: 0;
}
/* Reference image upload */
.iem-ref-upload {
    width: 48px;
    height: 48px;
    flex-shrink: 0;
    border-radius: 8px;
    border: 1.5px dashed #d0d0d0;
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
    transition: border-color 0.15s, background 0.15s;
}
.iem-ref-preview {
    position: absolute;
    top: 0;
    left: 0;
}
.iem-ref-upload:hover {
    border-color: #aaa;
    background: #f8f8f8;
}
.iem-ref-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
}
.iem-ref-preview {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 6px;
}
.iem-ref-remove {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    border: none;
    background: rgba(0,0,0,0.5);
    color: #fff;
    border-radius: 50%;
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
}
.iem-ref-remove:hover {
    background: rgba(0,0,0,0.7);
}
.iem-prompt-card:focus-within {
    border-color: #c0c0c0;
}
.iem-prompt {
    width: 100%;
    border: none;
    padding: 0;
    font-size: 15.6px;
    line-height: 1.5;
    resize: none;
    outline: none;
    font-family: inherit;
    color: #333;
    min-height: 46px;
    max-height: 92px;
    overflow-y: auto;
    background: transparent;
}
.iem-prompt::placeholder {
    color: #aaa;
}
.iem-prompt-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    border-top: 1px dashed #e8e8e8;
    padding-top: 10px;
}
.iem-controls-left {
    display: flex;
    align-items: center;
    gap: 6px;
}
.iem-controls-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* ── Custom upward dropdown ── */
.iem-dropdown-up {
    position: relative;
    flex-shrink: 0;
}
.iem-drop-trigger {
    height: 30px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    padding: 0 24px 0 8px;
    font-size: 12px;
    background: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    color: #555;
    white-space: nowrap;
    transition: border-color 0.15s, background-color 0.15s;
    position: relative;
}
.iem-drop-trigger:hover {
    background-color: #fafafa;
    border-color: #ccc;
}
.iem-drop-trigger.open {
    border-color: #4A90D9;
}
.iem-drop-arrow {
    position: absolute;
    right: 7px;
    top: 50%;
    transform: translateY(-50%);
    transition: transform 0.2s;
}
.iem-drop-trigger.open .iem-drop-arrow {
    transform: translateY(-50%) rotate(180deg);
}
.iem-drop-menu {
    display: none;
    position: absolute;
    bottom: calc(100% + 4px);
    left: 0;
    min-width: 100%;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.12);
    padding: 4px 0;
    z-index: 20;
}
.iem-drop-menu.show {
    display: block;
    animation: iemDropUp 0.15s ease;
}
@keyframes iemDropUp {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}
.iem-drop-option {
    padding: 7px 12px;
    font-size: 12px;
    color: #555;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.1s;
}
.iem-drop-option:hover {
    background: #f0f4ff;
    color: #333;
}
.iem-drop-option.active {
    color: #1a73e8;
    font-weight: 600;
}
/* ── Generate button ── */
.iem-generate-btn {
    height: 36px;
    border: none;
    border-radius: 8px;
    background: linear-gradient(to right, #0491e8, #1c1fd8);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 0 20px;
    white-space: nowrap;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.1s;
}
.iem-generate-btn:hover {
    opacity: 0.9;
}
.iem-generate-btn:active {
    transform: scale(0.97);
}
.iem-generate-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}
.iem-gen-cost {
    font-size: 11px;
    font-weight: 400;
    opacity: 0.85;
}

/* ── Circular progress spinner ── */
.iem-progress {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 15;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}
.iem-progress.hide { display: none !important; }
.iem-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255,255,255,0.3);
    border-top-color: #1a73e8;
    border-radius: 50%;
    animation: iemSpin 0.8s linear infinite;
}
@keyframes iemSpin {
    to { transform: rotate(360deg); }
}
.iem-spinner-text {
    font-size: 12px;
    color: #fff;
    background: rgba(0,0,0,0.5);
    padding: 3px 10px;
    border-radius: 10px;
    white-space: nowrap;
}

/* ── (Result thumbnails now in history panel) ── */

/* ── Utility ── */
.hide { display: none !important; }

/* ── Responsive (narrow screens) ── */
@media (max-width: 700px) {
    .iem-modal {
        width: 98vw;
        height: 95vh;
        border-radius: 12px;
    }
    .iem-body {
        flex-direction: column;
    }
    .iem-history-panel {
        width: auto;
        max-height: 120px;
        border-left: none;
        border-top: 1px solid #ddd;
        flex-direction: row;
    }
    .iem-hp-title {
        writing-mode: vertical-lr;
        padding: 8px 4px;
    }
    .iem-hp-list {
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 8px;
    }
    .iem-hp-item {
        width: 80px;
    }
    .iem-prompt-controls {
        flex-wrap: wrap;
    }
    .iem-controls-left {
        width: 100%;
    }
    .iem-select-mini {
        flex: 1;
    }
    .iem-generate-btn {
        width: 100%;
    }
}
