/* ============================================================
   PDF Editor — Images tool (menu, overlays, existing hits, stamps)
   ============================================================ */

.pedit-images-layer {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 26; /* above shapes; float toolbar stays above for its buttons */
    pointer-events: none;
    overflow: visible;
}

.pedit-images-layer.is-placing,
.pedit-images-layer.is-drawing,
.pedit-images-layer.has-selection {
    pointer-events: auto;
}

.pedit-existing-images-layer {
    position: absolute;
    top: 0;
    left: 0;
    /* Below text/preview (z22) in default edit so watermarks cannot cover
       selectable text. Exclusive Images modes raise this layer (below). */
    z-index: 21;
    /* CRITICAL: keep none on the full-page layer. `.is-active { pointer-events:auto }`
       was stealing hover/click from every text hit underneath. Only individual
       `.pedit-existing-image-hit` nodes capture pointers. */
    pointer-events: none;
    overflow: visible;
}

.pedit-existing-images-layer.is-active {
    pointer-events: none;
}

/* Images → select/move/place: existing hits must sit above text again. */
#pedit-canvas-wrap.images-existing-mode .pedit-existing-images-layer,
#pedit-canvas-wrap.images-place-mode .pedit-existing-images-layer,
#pedit-canvas-wrap.images-draw-mode .pedit-existing-images-layer,
.pedit-existing-images-layer.is-armed {
    z-index: 29;
}

.pedit-image {
    position: absolute;
    box-sizing: border-box;
    pointer-events: auto;
    cursor: move;
    touch-action: none;
    overflow: visible;
    transform-origin: center center;
}

.pedit-image-inner {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    object-position: center;
    pointer-events: none;
    user-select: none;
}

.pedit-image.is-selected {
    outline: 1px dashed rgba(247, 145, 18, 0.95);
    outline-offset: 2px;
    border: none;
}

.pedit-image-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    margin-left: -5px;
    margin-top: -5px;
    background: #fff;
    border: 2px solid #f79112;
    border-radius: 2px;
    box-sizing: border-box;
    z-index: 2;
    pointer-events: auto;
    touch-action: none;
}

.pedit-image-handle[data-handle="nw"],
.pedit-image-handle[data-handle="se"] { cursor: nwse-resize; }
.pedit-image-handle[data-handle="ne"],
.pedit-image-handle[data-handle="sw"] { cursor: nesw-resize; }

/* Free-rotate handle above top edge (rotates with selection box) */
.pedit-image-rotate-handle {
    position: absolute;
    right: -11px;
    top: -11px;
    left: auto;
    width: 22px;
    height: 22px;
    margin: 0;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    border: 2px solid #f79112;
    border-radius: 50%;
    cursor: grab;
    touch-action: none;
    z-index: 4;
    pointer-events: auto;
    color: #f79112;
    font-size: 11px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
}

.pedit-image-rotate-handle:active {
    cursor: grabbing;
}

.pedit-image-rotate-handle i {
    pointer-events: none;
    line-height: 1;
}

/* Crisp HTML stamp (legacy — stamps now use raster in editor) */
.pedit-stamp-face {
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5% 6%;
    color: var(--stamp-color, #c45c26);
    border: 2px solid var(--stamp-color, #c45c26);
    border-radius: 5px;
    background: transparent;
    box-shadow: inset 0 0 0 1px var(--stamp-color, #c45c26);
    pointer-events: none;
    user-select: none;
    overflow: hidden;
}

.pedit-stamp-subject {
    font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    font-weight: 700;
    font-size: calc(var(--stamp-h, 100px) * 0.2);
    letter-spacing: 0.1em;
    line-height: 1;
    text-align: center;
    text-transform: uppercase;
    max-width: 100%;
}

.pedit-stamp-rule {
    width: 55%;
    max-width: 180px;
    height: 1px;
    margin: 0.25em 0;
    background: var(--stamp-color, #c45c26);
    opacity: 0.45;
    flex-shrink: 0;
}

.pedit-stamp-meta {
    font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    font-weight: 500;
    font-size: calc(var(--stamp-h, 100px) * 0.085);
    letter-spacing: 0.02em;
    line-height: 1.15;
    text-align: center;
    opacity: 0.9;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Dropdown — theme-matched */
.pedit-images-menu {
    position: fixed;
    z-index: 950;
    width: 300px;
    max-height: min(70vh, 560px);
    overflow-y: auto;
    padding: 12px;
    background: rgba(20, 28, 40, 0.98);
    border: 2px solid var(--primary-light);
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    backdrop-filter: blur(10px);
    display: none;
}

.pedit-images-menu.is-open {
    display: block;
}

.pedit-images-lib-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 4px;
}

.pedit-images-lib-list:empty,
.pedit-images-lib-list[hidden] {
    display: none;
}

.pedit-images-lib-item {
    position: relative;
    display: block;
}

.pedit-images-lib-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin: 0;
    padding: 12px 14px;
    border: 2px solid #f79112;
    border-radius: 10px;
    background: rgba(247, 145, 18, 0.06);
    cursor: pointer;
    min-height: 64px;
}

.pedit-images-lib-preview:hover {
    background: rgba(247, 145, 18, 0.16);
}

.pedit-images-lib-preview img {
    max-width: 100%;
    max-height: 80px;
    object-fit: contain;
    pointer-events: none;
    user-select: none;
}

.pedit-images-lib-remove {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: rgba(12, 16, 24, 0.78);
    color: #fff;
    font-size: 14px;
    line-height: 22px;
    text-align: center;
    cursor: pointer;
    z-index: 2;
}

.pedit-images-lib-remove:hover {
    background: #f79112;
    color: #111;
}

.pedit-existing-image-hit {
    position: absolute;
    box-sizing: border-box;
    pointer-events: auto;
    cursor: pointer;
    touch-action: none;
    background: transparent;
    outline: 1px dashed transparent;
}

/* Watermark / underlay images: body text sits on top. Keep hits inert in the
   default edit tool so text hover + edit win; Images → delete/move re-enables. */
.pedit-existing-image-hit.is-text-underlay {
    pointer-events: none !important;
    cursor: default;
}

.pedit-existing-images-layer.is-armed .pedit-existing-image-hit.is-text-underlay,
#pedit-canvas-wrap.images-existing-mode .pedit-existing-image-hit.is-text-underlay,
#pedit-canvas-wrap.images-place-mode .pedit-existing-image-hit.is-text-underlay,
#pedit-canvas-wrap.images-draw-mode .pedit-existing-image-hit.is-text-underlay {
    pointer-events: auto !important;
}

.pedit-existing-images-layer.is-armed .pedit-existing-image-hit,
.pedit-existing-image-hit:not(.is-text-underlay):hover,
.pedit-existing-images-layer.is-armed .pedit-existing-image-hit.is-text-underlay:hover,
#pedit-canvas-wrap.images-existing-mode .pedit-existing-image-hit:hover {
    background: rgba(247, 145, 18, 0.12);
    outline: 1px dashed rgba(247, 145, 18, 0.75);
}

.pedit-existing-image-hit.is-selected {
    background: rgba(247, 145, 18, 0.16);
    outline: 2px dashed #f79112;
}

.pedit-existing-image-cover {
    position: absolute;
    background: #ffffff;
    pointer-events: none;
    /* Kill any subpixel fringe from underlying stamp borders */
    box-shadow: 0 0 0 1px #fff;
}

#pedit-canvas-wrap.images-place-mode,
#pedit-canvas-wrap.images-draw-mode {
    cursor: crosshair;
}

#pedit-canvas-wrap.images-place-mode .pedit-text-hit,
#pedit-canvas-wrap.images-draw-mode .pedit-text-hit {
    pointer-events: none !important;
}

#pedit-canvas-wrap.images-existing-mode .pedit-existing-image-hit {
    outline-color: rgba(247, 145, 18, 0.55);
    background: rgba(247, 145, 18, 0.08);
}

.pedit-images-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    margin: 0 0 6px;
    padding: 9px 12px;
    border: 1px solid var(--primary-light);
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    text-align: left;
}

.pedit-images-menu-item:last-child {
    margin-bottom: 0;
}

.pedit-images-menu-item i {
    width: 1.1em;
    color: #f79112;
    text-align: center;
}

.pedit-images-menu-item:hover:not(:disabled) {
    background: rgba(247, 145, 18, 0.14);
    color: var(--text-primary);
    border-color: #f79112;
}

.pedit-images-menu-primary {
    border-color: #f79112;
    color: var(--text-primary);
}

.pedit-images-menu-item:disabled,
.pedit-images-menu-item.is-disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.pedit-images-menu-divider {
    height: 1px;
    margin: 6px 0 10px;
    background: rgba(255, 255, 255, 0.12);
}

/* Existing selection uses full toolbar; rotate/dup promote via JS flag .ift-existing */

/* Create stamp modal */
.pedit-stamp-modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 1200;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(8, 12, 20, 0.72);
    backdrop-filter: blur(4px);
}

.pedit-stamp-modal {
    width: min(520px, 100%);
    background: rgba(20, 28, 40, 0.98);
    border: 2px solid var(--primary-light);
    border-radius: 14px;
    box-shadow: var(--shadow-md);
    color: var(--text-primary);
    overflow: hidden;
}

.pedit-stamp-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.pedit-stamp-modal-header h3 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-primary);
}

.pedit-stamp-modal-close {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
}

.pedit-stamp-modal-close:hover {
    background: rgba(247, 145, 18, 0.14);
    color: #f79112;
}

.pedit-stamp-modal-body {
    padding: 16px;
}

.pedit-stamp-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.pedit-stamp-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.pedit-stamp-field > span {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.pedit-stamp-field input,
.pedit-stamp-field select,
.pedit-stamp-date-trigger,
.pedit-stamp-datetime {
    width: 100%;
    box-sizing: border-box;
    padding: 9px 11px;
    border: 1px solid var(--primary-light);
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.28);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
}

.pedit-stamp-field input:focus,
.pedit-stamp-field select:focus,
.pedit-stamp-date-trigger:focus,
.pedit-stamp-datetime:focus {
    outline: none;
    border-color: #f79112;
    box-shadow: 0 0 0 2px rgba(247, 145, 18, 0.2);
}

.pedit-stamp-date-dd {
    position: relative;
}

.pedit-stamp-date-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    text-align: left;
    cursor: pointer;
}

.pedit-stamp-date-trigger i {
    font-size: 0.72rem;
    color: var(--text-secondary);
    transition: transform 0.15s ease;
}

.pedit-stamp-date-trigger[aria-expanded="true"] i {
    transform: rotate(180deg);
    color: #f79112;
}

.pedit-stamp-date-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pedit-stamp-date-menu {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 4px);
    z-index: 5;
    padding: 6px;
    border: 1px solid rgba(247, 145, 18, 0.45);
    border-radius: 10px;
    background: rgba(16, 22, 32, 0.98);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}

.pedit-stamp-date-menu.is-open {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.pedit-stamp-date-menu button {
    display: block;
    width: 100%;
    margin: 0;
    padding: 9px 11px;
    border: none;
    border-radius: 7px;
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
}

.pedit-stamp-date-menu button:hover {
    background: rgba(247, 145, 18, 0.14);
    color: var(--text-primary);
}

.pedit-stamp-date-menu button.is-active {
    background: rgba(247, 145, 18, 0.2);
    color: #f79112;
}

.pedit-stamp-datetime {
    margin-top: 8px;
    color-scheme: dark;
}

.pedit-stamp-datetime.is-visible,
.pedit-stamp-datetime:not([hidden]) {
    display: block;
}

.pedit-stamp-datetime[hidden] {
    display: none !important;
}

.pedit-stamp-colors {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    min-height: 38px;
}

.pedit-stamp-swatch {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    padding: 0;
}

.pedit-stamp-swatch.is-active {
    border-color: #fff;
    box-shadow: 0 0 0 2px #f79112;
}

.pedit-stamp-preview-wrap {
    margin-top: 16px;
    padding: 18px 12px;
    border: 1px dashed rgba(247, 145, 18, 0.45);
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.22);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 110px;
}

.pedit-stamp-preview {
    max-width: 100%;
    max-height: 140px;
    object-fit: contain;
}

.pedit-stamp-modal-footer {
    padding: 0 16px 16px;
    display: flex;
    justify-content: center;
}

.pedit-stamp-save {
    min-width: 140px;
    padding: 11px 22px;
    border: none;
    border-radius: 10px;
    background: #f79112;
    color: #111;
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 800;
    cursor: pointer;
}

.pedit-stamp-save:hover {
    filter: brightness(1.06);
}

@media (max-width: 520px) {
    .pedit-stamp-grid {
        grid-template-columns: 1fr;
    }
}
