/**
 * SmartImageCrop UI. Replaces the Cropper.js stylesheet.
 *
 * Note there is deliberately no `image-orientation` rule anywhere here. The old
 * views set `image-orientation: 0deg` on the cropper image to fight a rotation
 * bug; it only changed how the image was painted, never the pixel data the crop
 * was computed from, so it made the preview disagree with the output. The crop
 * surface is now a <canvas> holding already-upright pixels, so there is no
 * orientation left for CSS to have an opinion about.
 *
 * Everything below is namespaced `sic-` and the modal styles itself rather than
 * leaning on Bootstrap's .btn / .modal-header / .modal-footer. That is not
 * stylistic fussiness: css/styles.css redefines .btn-secondary globally as
 * `width:100%; padding:15px!important; font-size:18px!important`, which is what
 * stretched the Cancel and Zoom buttons across the dialog. Styling the crop
 * modal in its own namespace keeps it identical on every page that embeds it,
 * whatever each page's CSS does to the shared Bootstrap classes.
 */

.sic-modal {
    --sic-surface: #ffffff;
    --sic-panel: #f7f8fa;
    --sic-line: #e4e7ec;
    --sic-ink: #1f2430;
    --sic-muted: #6b7280;
    --sic-accent: #800080;
    --sic-accent-ink: #ffffff;
    --sic-radius: 12px;
}

/* ---------------------------------------------------------------- dialog -- */

/* 9999 carried over from the per-page .crop-modal rule this replaces: the
   organizer header sits above Bootstrap's own modal z-index on some pages. */
.sic-modal {
    z-index: 9999;
}

.sic-modal .modal-dialog {
    max-width: 920px;
}

.sic-modal .modal-content {
    overflow: hidden;
    border: 0;
    border-radius: var(--sic-radius);
    box-shadow: 0 24px 60px rgba(16, 20, 30, 0.28);
    background: var(--sic-surface);
}

.sic-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 18px 22px;
    border-bottom: 1px solid var(--sic-line);
}

.sic-head-text {
    min-width: 0;
    flex: 1 1 auto;
}

.sic-title {
    margin: 0;
    font-size: 1.0625rem;
    font-weight: 600;
    line-height: 1.3;
    color: var(--sic-ink);
}

.sic-subtitle {
    margin: 2px 0 0;
    font-size: 0.8125rem;
    color: var(--sic-muted);
}

.sic-badge {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
    padding: 5px 11px;
    border: 1px solid var(--sic-line);
    border-radius: 999px;
    background: var(--sic-panel);
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--sic-muted);
    white-space: nowrap;
}

/* Own close button: .btn-close renders as a 0-size box wherever the page also
   loads Bootstrap 4, whose equivalent is .close. */
.sic-close {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--sic-muted);
    font-size: 1.125rem;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.sic-close:hover {
    background: var(--sic-panel);
    color: var(--sic-ink);
}

.sic-body {
    display: flex;
    align-items: stretch;
    gap: 0;
    padding: 0;
}

.sic-main {
    flex: 1 1 auto;
    min-width: 0;
    padding: 20px 22px;
}

.sic-side {
    display: flex;
    flex-direction: column;
    gap: 18px;
    flex: 0 0 258px;
    padding: 20px 22px;
    border-left: 1px solid var(--sic-line);
    background: var(--sic-panel);
}

.sic-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 22px;
    border-top: 1px solid var(--sic-line);
    background: var(--sic-panel);
}

.sic-foot-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-left: auto;
}

/* --------------------------------------------------------------- buttons -- */

.sic-modal .sic-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    width: auto;
    min-height: 40px;
    padding: 9px 18px;
    border: 1px solid var(--sic-line);
    border-radius: 9px;
    background: var(--sic-surface);
    box-shadow: none;
    color: var(--sic-ink);
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.2;
    text-align: center;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.sic-modal .sic-btn:hover {
    border-color: #cdd3dd;
    background: #eef0f4;
    color: var(--sic-ink);
}

.sic-modal .sic-btn:focus-visible {
    outline: 2px solid var(--sic-accent);
    outline-offset: 2px;
}

.sic-modal .sic-btn-primary {
    border-color: var(--sic-accent);
    background: var(--sic-accent);
    color: var(--sic-accent-ink);
}

.sic-modal .sic-btn-primary:hover {
    border-color: #6b006b;
    background: #6b006b;
    color: var(--sic-accent-ink);
}

.sic-modal .sic-btn-ghost {
    border-color: transparent;
    background: transparent;
    color: var(--sic-muted);
}

.sic-modal .sic-btn-ghost:hover {
    background: #e9ecf1;
    color: var(--sic-ink);
}

.sic-modal .sic-btn[disabled] {
    opacity: 0.55;
    cursor: not-allowed;
}

/* Tool buttons sit in a two-up grid so the sidebar reads as a toolbar rather
   than a stack of mismatched pills. */
.sic-tools {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.sic-modal .sic-tools .sic-btn {
    min-height: 38px;
    padding: 8px 10px;
    font-size: 0.8125rem;
}

.sic-modal .sic-tools .sic-btn-wide {
    grid-column: 1 / -1;
}

/* ------------------------------------------------------------ crop stage -- */

.sic-root {
    width: 100%;
}

.sic-stage {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    overflow: hidden;
    border-radius: 10px;
    background: #15181d;
    /* A checkerboard so transparent PNGs read as transparent, not as white. */
    background-image:
        linear-gradient(45deg, #1d2127 25%, transparent 25%, transparent 75%, #1d2127 75%),
        linear-gradient(45deg, #1d2127 25%, transparent 25%, transparent 75%, #1d2127 75%);
    background-size: 20px 20px;
    background-position: 0 0, 10px 10px;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.sic-view {
    display: block;
    max-width: 100%;
    pointer-events: none;
}

/* The box is a window onto the image: everything outside it is dimmed by a
   large spread shadow, which avoids four separate overlay elements. */
.sic-box {
    position: absolute;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.95);
    box-shadow: 0 0 0 9999px rgba(10, 12, 16, 0.6);
    cursor: move;
    outline: none;
}

.sic-box:focus-visible {
    border-color: #4c9ffe;
    box-shadow: 0 0 0 9999px rgba(10, 12, 16, 0.6), 0 0 0 2px #4c9ffe;
}

/* Rule-of-thirds guides, to help line a subject up. */
.sic-box::before,
.sic-box::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.sic-box::before {
    background-image:
        linear-gradient(to right, transparent calc(33.333% - 0.5px), rgba(255, 255, 255, 0.28) calc(33.333% - 0.5px), rgba(255, 255, 255, 0.28) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px)),
        linear-gradient(to right, transparent calc(66.666% - 0.5px), rgba(255, 255, 255, 0.28) calc(66.666% - 0.5px), rgba(255, 255, 255, 0.28) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px));
}

.sic-box::after {
    background-image:
        linear-gradient(to bottom, transparent calc(33.333% - 0.5px), rgba(255, 255, 255, 0.28) calc(33.333% - 0.5px), rgba(255, 255, 255, 0.28) calc(33.333% + 0.5px), transparent calc(33.333% + 0.5px)),
        linear-gradient(to bottom, transparent calc(66.666% - 0.5px), rgba(255, 255, 255, 0.28) calc(66.666% - 0.5px), rgba(255, 255, 255, 0.28) calc(66.666% + 0.5px), transparent calc(66.666% + 0.5px));
}

/* Corner brackets rather than square dots: they read as a crop frame and stay
   legible against both bright and dark areas of a photo. */
.sic-handle {
    position: absolute;
    z-index: 2;
    width: 18px;
    height: 18px;
    border: 3px solid #fff;
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.55));
}

/* Touch targets larger than the visible handle, so corners are grabbable on a
   phone without making the handles look clumsy on a desktop. */
.sic-handle::after {
    content: '';
    position: absolute;
    inset: -12px;
}

.sic-handle-nw {
    top: -2px;
    left: -2px;
    border-right: 0;
    border-bottom: 0;
    border-top-left-radius: 3px;
    cursor: nwse-resize;
}

.sic-handle-ne {
    top: -2px;
    right: -2px;
    border-left: 0;
    border-bottom: 0;
    border-top-right-radius: 3px;
    cursor: nesw-resize;
}

.sic-handle-se {
    right: -2px;
    bottom: -2px;
    border-top: 0;
    border-left: 0;
    border-bottom-right-radius: 3px;
    cursor: nwse-resize;
}

.sic-handle-sw {
    bottom: -2px;
    left: -2px;
    border-top: 0;
    border-right: 0;
    border-bottom-left-radius: 3px;
    cursor: nesw-resize;
}

/* ------------------------------------------------------------- side rail -- */

.sic-panel-label {
    display: block;
    margin: 0 0 8px;
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--sic-muted);
}

.sic-preview-wrap {
    overflow: hidden;
    border: 1px solid var(--sic-line);
    border-radius: 10px;
    background: #eceef2;
}

.sic-preview-wrap canvas {
    display: block;
    width: 100%;
    height: auto;
}

.sic-meta {
    display: block;
    margin-top: 8px;
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
    color: var(--sic-muted);
}

.sic-hint {
    margin: 0;
    font-size: 0.8125rem;
    line-height: 1.45;
    color: var(--sic-muted);
}

.sic-main .sic-hint {
    margin-top: 12px;
}

.sic-foot .sic-hint {
    flex: 1 1 auto;
    min-width: 0;
}

/* ----------------------------------------------------------- zoom slider -- */

.sic-zoom {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sic-zoom i {
    flex: 0 0 auto;
    font-size: 0.9375rem;
    color: var(--sic-muted);
}

.sic-modal input[type="range"].sic-range {
    flex: 1 1 auto;
    width: 100%;
    height: 4px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 999px;
    background: #d7dbe3;
    box-shadow: none;
    appearance: none;
    -webkit-appearance: none;
    cursor: pointer;
}

.sic-modal input[type="range"].sic-range::-webkit-slider-thumb {
    width: 16px;
    height: 16px;
    border: 2px solid #fff;
    border-radius: 50%;
    background: var(--sic-accent);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    appearance: none;
    -webkit-appearance: none;
}

.sic-modal input[type="range"].sic-range::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border: 2px solid #fff;
    border-radius: 50%;
    background: var(--sic-accent);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.sic-modal input[type="range"].sic-range:focus-visible {
    outline: 2px solid var(--sic-accent);
    outline-offset: 4px;
}

/* -------------------------------------------------------------- responsive */

/* The side rail becomes a row under the image: a 258px column beside a stage
   leaves neither enough room on a tablet. */
@media (max-width: 991.98px) {
    .sic-body {
        flex-direction: column;
    }

    .sic-side {
        flex: 1 1 auto;
        border-left: 0;
        border-top: 1px solid var(--sic-line);
    }

    .sic-preview-wrap {
        max-width: 280px;
    }
}

@media (max-width: 575.98px) {
    .sic-head,
    .sic-main,
    .sic-side,
    .sic-foot {
        padding-left: 16px;
        padding-right: 16px;
    }

    .sic-badge {
        display: none;
    }

    .sic-foot {
        flex-wrap: wrap;
    }

    /* On a phone the hint below the stage already says what to do, so the
       footer gives its whole width to the two actions. */
    .sic-foot .sic-hint {
        display: none;
    }

    .sic-foot-actions {
        width: 100%;
    }

    .sic-modal .sic-foot-actions .sic-btn {
        flex: 1 1 0;
    }

    .sic-handle {
        width: 22px;
        height: 22px;
    }
}
