/* ==========================================================================
   Web Image Editor (crop / rotate / filters / layers / resize / format)
   Opened from the Gallery Browser or any image picker via window.openWebImageEditor()
   ========================================================================== */

#pie-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000010;
    background: rgba(15, 23, 42, 0.72);
    backdrop-filter: blur(3px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    box-sizing: border-box;
}

#pie-modal {
    width: min(1440px, 98vw);
    height: min(920px, 96vh);
    background: #ffffff;
    color: #1f2937;
    border-radius: 10px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-sizing: border-box;
}

#pie-modal * { box-sizing: border-box; }

.pie-header {
    background: #1e293b;
    color: #f8fafc;
    padding: 10px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    border-bottom: 1px solid #334155;
}

.pie-header h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.pie-header-controls {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.pie-btn {
    border: 1px solid #475569;
    border-radius: 6px;
    padding: 6px 11px;
    font-size: 12px;
    font-weight: 600;
    background: #334155;
    color: #f8fafc;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.pie-btn:hover { background: #475569; }
.pie-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.pie-btn-primary { background: #2563eb; border-color: #2563eb; }
.pie-btn-primary:hover { background: #3b82f6; }

.pie-btn-danger { background: #7f1d1d; border-color: #7f1d1d; }
.pie-btn-danger:hover { background: #991b1b; }

.pie-close {
    border: none;
    background: transparent;
    color: #f8fafc;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 4px 8px;
}
.pie-close:hover { color: #f87171; }

.pie-body {
    flex: 1;
    display: flex;
    min-height: 0;
}

.pie-canvas-area {
    flex: 1;
    min-width: 0;
    background: repeating-conic-gradient(#f1f5f9 0% 25%, #e2e8f0 0% 50%) 50% / 18px 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 18px;
    position: relative;
    overflow: auto;
}

.pie-canvas-area img#pie-preview-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    box-shadow: 0 4px 18px rgba(0,0,0,0.25);
    background: #fff;
}

.pie-canvas-loading {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,0.55);
    font-size: 13px;
    font-weight: 600;
    color: #334155;
}
.pie-canvas-loading.active { display: flex; }

.pie-panel {
    width: 340px;
    flex-shrink: 0;
    border-left: 1px solid #e2e8f0;
    background: #f8fafc;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.pie-section {
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 10px 12px;
}

.pie-section > summary {
    font-size: 12.5px;
    font-weight: 700;
    color: #0f172a;
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.pie-section > summary::-webkit-details-marker { display: none; }
.pie-section > summary::after {
    content: "\25BE";
    font-size: 11px;
    color: #64748b;
}
.pie-section[open] > summary::after { content: "\25B4"; }

.pie-section-body {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.pie-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.pie-row label {
    font-size: 11.5px;
    font-weight: 600;
    color: #475569;
    flex: 1 1 auto;
    min-width: 70px;
}

.pie-row input[type="number"],
.pie-row input[type="text"],
.pie-row select {
    width: 84px;
    font-size: 12px;
    padding: 4px 6px;
    border: 1px solid #cbd5e1;
    border-radius: 5px;
    background: #fff;
    color: #1f2937;
}

.pie-row select.pie-wide { width: 100%; }

.pie-row input[type="range"] { flex: 1 1 auto; }

.pie-row .pie-val { font-size: 11px; color: #64748b; width: 34px; text-align: right; }

.pie-checkbox-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    font-weight: 600;
    color: #334155;
}

.pie-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.pie-list-item {
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 6px 8px;
    background: #f8fafc;
}

.pie-list-item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.pie-list-item-title {
    font-size: 12px;
    font-weight: 700;
    color: #0f172a;
}

.pie-list-item-actions {
    display: flex;
    gap: 4px;
}

.pie-mini-btn {
    border: 1px solid #cbd5e1;
    background: #fff;
    border-radius: 4px;
    font-size: 11px;
    padding: 2px 6px;
    cursor: pointer;
    color: #334155;
}
.pie-mini-btn:hover { background: #e2e8f0; }
.pie-mini-btn.danger { border-color: #fca5a5; color: #b91c1c; }
.pie-mini-btn.danger:hover { background: #fee2e2; }

.pie-color-swatch {
    width: 26px;
    height: 22px;
    border-radius: 4px;
    border: 1px solid #cbd5e1;
    cursor: pointer;
    padding: 0;
}

.pie-footer {
    border-top: 1px solid #e2e8f0;
    background: #f1f5f9;
    padding: 8px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    color: #475569;
    gap: 10px;
    flex-wrap: wrap;
}

.pie-status.error { color: #b91c1c; font-weight: 700; }
.pie-status.success { color: #15803d; font-weight: 700; }

/* Crop overlay tool */
#pie-crop-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000011;
    background: rgba(15, 23, 42, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

#pie-crop-box {
    background: #0f172a;
    border-radius: 10px;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 96vw;
    max-height: 96vh;
}

.pie-crop-stage {
    position: relative;
    display: inline-block;
    line-height: 0;
    max-width: 82vw;
    max-height: 76vh;
    overflow: auto;
}

.pie-crop-stage img {
    max-width: 82vw;
    max-height: 76vh;
    display: block;
    user-select: none;
    -webkit-user-drag: none;
}

.pie-crop-rect {
    position: absolute;
    border: 2px solid #3b82f6;
    background: rgba(59, 130, 246, 0.15);
    cursor: move;
    box-sizing: border-box;
}

.pie-crop-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    background: #3b82f6;
    border: 1px solid #fff;
    border-radius: 2px;
}
.pie-crop-handle.nw { top: -6px; left: -6px; cursor: nwse-resize; }
.pie-crop-handle.ne { top: -6px; right: -6px; cursor: nesw-resize; }
.pie-crop-handle.sw { bottom: -6px; left: -6px; cursor: nesw-resize; }
.pie-crop-handle.se { bottom: -6px; right: -6px; cursor: nwse-resize; }

.pie-crop-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

@media (max-width: 900px) {
    .pie-panel { width: 280px; }
}
