.compression-widget {
    margin-top: 1.2rem;
    padding: 1.4rem;
    border: 1px solid #dbeafe;
    border-radius: 24px;
    background: linear-gradient(135deg, #f8fbff, #eef6ff);
    box-shadow: 0 14px 35px rgba(15, 23, 42, 0.08);
}

.compression-controls,
.speed-control-row {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: 0.9rem;
    margin-bottom: 1.2rem;
}

.compression-field {
    display: flex;
    flex-direction: column;
    min-width: 220px;
    flex: 1;
}

.compression-field label,
.speed-control-row label {
    display: block;
    margin-bottom: 0.35rem;
    font-weight: 800;
    color: #1e293b;
}

.compression-field input,
.compression-field select {
    padding: 0.7rem 0.85rem;
    border: 1px solid #cbd5e1;
    border-radius: 14px;
    background: #ffffff;
    font-size: 1rem;
}

.compression-controls button {
    border: 0;
    border-radius: 999px;
    padding: 0.75rem 1rem;
    font-weight: 800;
    cursor: pointer;
    background: #2563eb;
    color: #ffffff;
    box-shadow: 0 8px 18px rgba(37, 99, 235, 0.22);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.compression-controls button:hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 22px rgba(37, 99, 235, 0.28);
}

.speed-control-row {
    align-items: center;
}

.speed-control-row input[type="range"] {
    width: 240px;
}

#rleSpeedLabel,
#bitmapRleSpeedLabel {
    font-weight: 900;
    color: #1d4ed8;
}

.rle-output-layout,
.bitmap-rle-layout,
.huffman-layout,
.lossy-layout {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 1rem;
    align-items: start;
}

.rle-strip,
.rle-compressed-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
    min-height: 74px;
    padding: 1rem;
    border-radius: 18px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
}

.rle-symbol {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 13px;
    background: #e0f2fe;
    border: 1px solid #bae6fd;
    font-weight: 900;
    color: #0f172a;
    transition: transform 0.25s ease, background 0.25s ease, border-color 0.25s ease;
}

.rle-symbol.active {
    transform: translateY(-5px) scale(1.06);
    background: #bfdbfe;
    border-color: #2563eb;
}

.rle-run {
    min-width: 72px;
    padding: 0.65rem 0.85rem;
    border-radius: 16px;
    background: #dcfce7;
    border: 1px solid #86efac;
    color: #14532d;
    font-weight: 900;
    text-align: center;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.rle-run.visible {
    opacity: 1;
    transform: translateY(0);
}

.compression-summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
    gap: 0.9rem;
    margin: 1rem 0;
}

.compression-card {
    padding: 1rem;
    border-radius: 18px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
}

.compression-label {
    display: block;
    margin-bottom: 0.35rem;
    font-size: 0.78rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
}

.compression-card strong {
    font-size: 1.18rem;
    color: #0f172a;
    word-break: break-word;
}

.compression-note {
    margin-top: 1rem;
    padding: 1rem;
    border-radius: 18px;
    background: #fff7ed;
    border: 1px solid #fed7aa;
    color: #7c2d12;
    font-weight: 700;
}

.bitmap-rle-grid {
    display: grid;
    grid-template-columns: repeat(8, 34px);
    grid-template-rows: repeat(8, 34px);
    gap: 4px;
    padding: 0.8rem;
    border-radius: 20px;
    background: #e2e8f0;
    width: max-content;
}

.bitmap-rle-cell {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.14);
    transition: transform 0.18s ease, box-shadow 0.18s ease;
}

.bitmap-rle-cell.scanning {
    transform: scale(1.12);
    box-shadow: inset 0 0 0 3px rgba(37, 99, 235, 0.7);
}

.bitmap-rle-panel {
    padding: 1rem;
    border-radius: 20px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
}

.bitmap-rle-panel h3 {
    margin-top: 0;
}

.bitmap-rle-rows {
    display: grid;
    gap: 0.5rem;
}

.bitmap-rle-row {
    padding: 0.55rem 0.7rem;
    border-radius: 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-weight: 700;
    color: #334155;
}

.bitmap-rle-row.active {
    background: #dbeafe;
    border-color: #60a5fa;
    color: #1d4ed8;
}

.huffman-table td,
.huffman-table th {
    text-align: center;
}

.huffman-panel {
    padding: 1rem;
    border-radius: 20px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
}

.huffman-panel h3 {
    margin-top: 0;
}

.huffman-bit-stream {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    min-height: 64px;
    padding: 0.8rem;
    border-radius: 14px;
    background: #0f172a;
    color: #e2e8f0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.huffman-bit {
    padding: 0.25rem 0.35rem;
    border-radius: 7px;
    background: rgba(255, 255, 255, 0.08);
    font-weight: 900;
}

.huffman-bit.active {
    background: #fbbf24;
    color: #0f172a;
}

.huffman-decoded-output {
    min-height: 42px;
    padding: 0.8rem;
    border-radius: 14px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    font-weight: 900;
    letter-spacing: 0.04em;
}

.lossy-grid {
    display: grid;
    grid-template-columns: repeat(16, 18px);
    grid-template-rows: repeat(10, 18px);
    gap: 2px;
    padding: 0.8rem;
    border-radius: 20px;
    background: #e2e8f0;
    width: max-content;
    max-width: 100%;
    overflow-x: auto;
}

.lossy-cell {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    transition: background 0.3s ease, transform 0.2s ease;
}

.lossy-cell.changed {
    transform: scale(1.18);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.5);
}

.formula-line {
    padding: 0.8rem 1rem;
    border-radius: 16px;
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
}

.exam-tip-box,
.warning-box {
    margin: 1rem 0;
    padding: 1rem;
    border-radius: 18px;
}

.exam-tip-box {
    background: #ecfdf5;
    border: 1px solid #bbf7d0;
    color: #14532d;
}

.warning-box {
    background: #fff7ed;
    border: 1px solid #fed7aa;
    color: #7c2d12;
}

@media (max-width: 760px) {
    .compression-controls,
    .speed-control-row {
        flex-direction: column;
        align-items: stretch;
    }

    .speed-control-row input[type="range"] {
        width: 100%;
    }

    .bitmap-rle-grid {
        grid-template-columns: repeat(8, 28px);
        grid-template-rows: repeat(8, 28px);
    }

    .bitmap-rle-cell {
        width: 28px;
        height: 28px;
    }

    .lossy-grid {
        grid-template-columns: repeat(16, 14px);
        grid-template-rows: repeat(10, 14px);
    }

    .lossy-cell {
        width: 14px;
        height: 14px;
    }
}

/*jpeg compression*/
/* ===== JPEG compression widget ===== */

.jpeg-control-grid {
    display: grid;
    grid-template-columns: minmax(260px, 520px);
    gap: 0.9rem;
    margin-bottom: 1.2rem;
}

.jpeg-quality-only-grid {
    max-width: 640px;
}

.jpeg-control-grid input[type="range"] {
    width: 100%;
}

.jpeg-quality-hint,
.jpeg-click-hint {
    margin: 0.45rem 0 0;
    color: #64748b;
    font-size: 0.9rem;
    line-height: 1.5;
}

.jpeg-preview-layout {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1rem;
    align-items: start;
}

.jpeg-preview-card {
    padding: 1rem;
    border-radius: 22px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
}

.jpeg-preview-card h3 {
    margin-top: 0;
}

.jpeg-preview-card canvas {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 16px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.jpeg-preview-canvas {
    cursor: zoom-in;
}

.jpeg-info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 0.7rem;
    margin-top: 0.9rem;
}

.jpeg-info-grid > div {
    padding: 0.75rem;
    border-radius: 14px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.jpeg-info-grid strong {
    color: #0f172a;
    font-size: 1rem;
}

.jpeg-difference-panel {
    margin-top: 1rem;
    padding: 1rem;
    border-radius: 18px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.jpeg-difference-bar {
    height: 18px;
    overflow: hidden;
    border-radius: 999px;
    background: #e2e8f0;
    border: 1px solid #cbd5e1;
}

#jpegDifferenceFill {
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: linear-gradient(90deg, #22c55e, #f59e0b, #ef4444);
    transition: width 0.35s ease;
}

#jpegCompressionExplanation {
    margin-bottom: 0;
    font-weight: 700;
    color: #334155;
}

/* ===== JPEG preview modal ===== */

.jpeg-preview-modal {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: none;
}

.jpeg-preview-modal.active {
    display: block;
}

.jpeg-preview-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.78);
    backdrop-filter: blur(6px);
}

.jpeg-preview-modal-content {
    position: relative;
    z-index: 1;

    width: min(1100px, calc(100vw - 2rem));
    max-height: calc(100vh - 2rem);

    margin: 1rem auto;
    padding: 1rem;

    border-radius: 24px;
    background: #ffffff;
    box-shadow: 0 30px 90px rgba(0, 0, 0, 0.35);

    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.jpeg-preview-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.jpeg-preview-modal-header h3 {
    margin: 0;
    color: #0f172a;
}

.jpeg-preview-modal-close {
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 999px;
    background: #f1f5f9;
    color: #0f172a;
    font-size: 1.6rem;
    line-height: 1;
    cursor: pointer;
}

.jpeg-preview-modal-close:hover {
    background: #0f172a;
    color: white;
}

#jpegPreviewModalImage {
    max-width: 100%;
    max-height: calc(100vh - 190px);
    object-fit: contain;
    border-radius: 18px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.jpeg-preview-modal-note {
    margin: 0;
    color: #475569;
    font-weight: 700;
}

body.modal-open {
    overflow: hidden;
}

@media (max-width: 760px) {
    .jpeg-control-grid {
        grid-template-columns: 1fr;
    }

    .jpeg-preview-modal-content {
        width: calc(100vw - 1rem);
        margin: 0.5rem auto;
        border-radius: 18px;
    }

    #jpegPreviewModalImage {
        max-height: calc(100vh - 180px);
    }
}