/* ============================================================
   JPEG Quantization Tool - style.css (v2.1)
   Mobile-first · responsive · dark-themed · collapsible steps
   ============================================================ */

/* ---------- Custom Properties ---------- */
:root {
    --clr-bg: #1a1a2e;
    --clr-surface: #16213e;
    --clr-surface-alt: #0f3460;
    --clr-accent: #c49a6c;
    --clr-accent-alt: #7ebdc2;
    --clr-text: #e0e0e0;
    --clr-text-muted: #8a8a9a;
    --clr-danger: #e74c3c;
    --clr-success: #2ecc71;
    --clr-white: #ffffff;
    --clr-decode: #a78bfa; /* purple for decode steps */

    --ff-body: "Segoe UI", system-ui, -apple-system, sans-serif;
    --ff-mono: "Fira Code", "Cascadia Code", "Consolas", monospace;

    --radius: 8px;
    --radius-lg: 16px;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
    --transition: 0.25s ease;
}

/* ---------- Reset ---------- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
html {
    scroll-behavior: smooth;
}
body {
    font-family: var(--ff-body);
    font-size: 1rem;
    line-height: 1.6;
    color: var(--clr-text);
    background: var(--clr-bg);
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}
img,
canvas {
    display: block;
    max-width: 100%;
}
a {
    color: var(--clr-accent-alt);
}

/* ---------- Hero ---------- */
.hero {
    text-align: center;
    padding: 1.5rem 1.25rem 1rem;
    background: linear-gradient(
        135deg,
        var(--clr-surface),
        var(--clr-surface-alt)
    );
    border-bottom: 3px solid var(--clr-accent);
}
.hero h1 {
    font-size: clamp(1.4rem, 4vw, 2.4rem);
    color: var(--clr-accent);
    margin-bottom: 0.3rem;
}
.hero__subtitle {
    max-width: 52rem;
    margin: 0 auto 0.6rem;
    color: var(--clr-text-muted);
    font-size: 0.9rem;
}
.hero__attribution {
    max-width: 48rem;
    margin: 0 auto 0.75rem;
    color: var(--clr-text-muted);
    font-size: 0.78rem;
    opacity: 0.85;
    line-height: 1.5;
}
.hero__attribution a {
    color: var(--clr-accent-alt);
}
.hero__nav {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    flex-wrap: wrap;
}

/* ---------- Lenna History ---------- */
.lenna-history {
    max-width: 64rem;
    margin: 0 auto;
    padding: 0.5rem 1.25rem;
}
.lenna-history details {
    background: var(--clr-surface);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
}
.lenna-history summary {
    cursor: pointer;
    color: var(--clr-accent-alt);
    font-size: 0.9rem;
}
.lenna-history p {
    font-size: 0.85rem;
    color: var(--clr-text-muted);
    margin-top: 0.5rem;
    line-height: 1.6;
}
.lenna-history__cite {
    font-style: italic;
    border-left: 3px solid var(--clr-accent);
    padding-left: 0.75rem;
}

/* ---------- Upload Bar ---------- */
.upload-bar {
    padding: 0.6rem 1.25rem;
}
.upload-bar__inner {
    max-width: 64rem;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: center;
    justify-content: center;
}
.upload-bar__dropzone {
    flex: 1 1 260px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 0.85rem 1.25rem;
    border: 2px dashed var(--clr-accent-alt);
    border-radius: var(--radius);
    cursor: pointer;
    transition:
        background var(--transition),
        border-color var(--transition);
    text-align: center;
    color: var(--clr-text-muted);
    font-size: 0.9rem;
}
.upload-bar__dropzone:hover,
.upload-bar__dropzone:focus-visible {
    background: rgba(126, 189, 194, 0.08);
    border-color: var(--clr-accent);
    outline: none;
}
.upload-bar__dropzone.drag-over {
    background: rgba(126, 189, 194, 0.15);
    border-color: var(--clr-accent);
}
.upload-bar__icon {
    font-size: 1.3rem;
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.9rem;
    font-size: 0.85rem;
    font-family: var(--ff-body);
    border: 1px solid var(--clr-accent-alt);
    border-radius: var(--radius);
    background: transparent;
    color: var(--clr-text);
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
    text-decoration: none;
}
.btn:hover,
.btn:focus-visible {
    background: rgba(126, 189, 194, 0.12);
    border-color: var(--clr-accent);
    outline: none;
}
.btn--primary {
    background: var(--clr-accent);
    color: var(--clr-bg);
    border-color: var(--clr-accent);
    font-weight: 600;
    font-size: 1rem;
    padding: 0.65rem 2rem;
    width: 100%;
    justify-content: center;
}
.btn--primary:hover {
    background: #d4aa7c;
}
.btn--small {
    padding: 0.3rem 0.65rem;
    font-size: 0.8rem;
}
.btn--toggle[aria-pressed="true"] {
    background: var(--clr-accent-alt);
    color: var(--clr-bg);
}

/* ---------- Comparison Layout ---------- */
.comparison {
    max-width: 80rem;
    margin: 0 auto;
    padding: 0.75rem 1.25rem;
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}
@media (min-width: 900px) {
    .comparison {
        grid-template-columns: 1fr auto 1fr;
        align-items: start;
    }
}
.comparison__panel {
    text-align: center;
}
.comparison__panel--controls {
    max-width: 26rem;
    margin: 0 auto;
}
.comparison__heading {
    font-size: 1rem;
    color: var(--clr-accent);
    margin-bottom: 0.6rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}
.comparison__hint {
    font-size: 0.75rem;
    color: var(--clr-text-muted);
    margin: 0.3rem 0;
}

/* ---------- Canvas Wrappers (aspect-ratio safe) ---------- */
.canvas-wrap {
    position: relative;
    display: inline-block;
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    touch-action: pan-y pinch-zoom;
}
.canvas-wrap canvas {
    width: 100%;
    max-width: 400px;
    /* height: auto keeps native aspect ratio, no squashing */
    height: auto;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    cursor: crosshair;
    touch-action: pan-y pinch-zoom;
}
.canvas-wrap__selection {
    display: none;
    position: absolute;
    border: 2px solid var(--clr-danger);
    pointer-events: none;
    z-index: 2;
    transition:
        left 0.1s,
        top 0.1s,
        width 0.1s,
        height 0.1s;
}
.snippet-canvas {
    width: 100%;
    max-width: 200px;
    height: auto;
    margin: 0.4rem auto;
    border-radius: var(--radius);
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    box-shadow: var(--shadow);
}

/* ---------- Controls Layout ---------- */
.controls-layout {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center;
    align-items: center;
    margin-bottom: 0.75rem;
}
.controls-layout__table {
    flex: 1 1 160px;
    max-width: 220px;
}
.controls-layout__table canvas {
    width: 100%;
    height: auto;
    border-radius: var(--radius);
    cursor: pointer;
}
.controls-layout__sliders {
    flex: 1 1 130px;
    max-width: 180px;
}
.slider-group {
    margin-bottom: 0.4rem;
}
.slider-group label {
    display: block;
    font-size: 0.75rem;
    color: var(--clr-text-muted);
    margin-bottom: 0.1rem;
}

/* ---------- Range Inputs ---------- */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(
        to right,
        var(--clr-accent-alt),
        var(--clr-accent)
    );
    border: 1px solid rgba(224, 224, 224, 0.25);
    outline: none;
}
input[type="range"]::-webkit-slider-runnable-track {
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(
        to right,
        var(--clr-accent-alt),
        var(--clr-accent)
    );
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--clr-accent);
    cursor: pointer;
    border: 2px solid var(--clr-white);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
    margin-top: -6px;
}
input[type="range"]::-moz-range-track {
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(
        to right,
        var(--clr-accent-alt),
        var(--clr-accent)
    );
    border: 1px solid rgba(224, 224, 224, 0.25);
}
input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--clr-accent);
    cursor: pointer;
    border: 2px solid var(--clr-white);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

/* ---------- Q-Table Info & Entropy Option ---------- */
/* ---------- Compression Info Dropdown ---------- */
.compression-info {
    background: var(--clr-surface);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.5rem;
    font-size: 0.83rem;
    border-left: 3px solid var(--clr-accent-alt);
}
.compression-info summary {
    cursor: pointer;
    color: var(--clr-accent-alt);
    font-size: 0.85rem;
}
.compression-info p {
    color: var(--clr-text-muted);
    margin-top: 0.5rem;
    line-height: 1.55;
}
.compression-info a {
    color: var(--clr-accent-alt);
}

.qtable-info,
.entropy-option {
    background: var(--clr-surface);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.5rem;
    font-size: 0.83rem;
}
.qtable-info summary,
.entropy-option summary {
    cursor: pointer;
    color: var(--clr-accent-alt);
    font-size: 0.85rem;
}
.qtable-info p,
.entropy-option p {
    color: var(--clr-text-muted);
    margin-top: 0.5rem;
    line-height: 1.55;
}
.qtable-info ul,
.entropy-option ul {
    color: var(--clr-text-muted);
    margin: 0.5rem 0 0.5rem 1.2rem;
    font-size: 0.85rem;
}
.qtable-info__cite {
    font-style: italic;
    border-left: 3px solid var(--clr-accent);
    padding-left: 0.75rem;
}
.entropy-controls {
    margin-top: 0.6rem;
}
.toggle-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.85rem;
}
.entropy-output {
    margin-top: 0.75rem;
}
.entropy-output h4 {
    font-size: 0.85rem;
    color: var(--clr-accent);
    margin-bottom: 0.4rem;
}

/* ---------- Auto-Requantise Toggle ---------- */
.auto-requant {
    margin-bottom: 0.6rem;
    padding: 0.5rem 0.75rem;
    background: var(--clr-surface);
    border-radius: var(--radius);
}
.auto-requant__warn {
    font-size: 0.75rem;
    color: var(--clr-accent);
    margin-top: 0.25rem;
    opacity: 0.8;
}

/* ---------- Cell Edit Overlay ---------- */
.cell-edit-overlay {
    margin-top: 0.4rem;
    padding: 0.5rem 0.75rem;
    background: var(--clr-surface-alt);
    border: 1px solid var(--clr-accent);
    border-radius: var(--radius);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.82rem;
    animation: fadeSlideIn 0.25s ease;
}
.cell-edit-overlay label {
    color: var(--clr-text-muted);
    white-space: nowrap;
}
.cell-edit-overlay input[type="number"] {
    width: 64px;
    padding: 0.25rem 0.4rem;
    font-family: var(--ff-mono);
    font-size: 0.82rem;
    background: var(--clr-surface);
    border: 1px solid var(--clr-accent-alt);
    border-radius: var(--radius);
    color: var(--clr-text);
}
.cell-edit-overlay input[type="number"]:focus {
    outline: 2px solid var(--clr-accent);
    outline-offset: 1px;
}
.cell-edit-overlay__buttons {
    display: flex;
    gap: 0.3rem;
}

/* ---------- Frequency Band Info ---------- */
.freq-band-info {
    margin-bottom: 0.5rem;
    font-size: 0.8rem;
}
.freq-band-info summary {
    cursor: pointer;
    color: var(--clr-accent-alt);
    font-size: 0.8rem;
}
.freq-band-info p {
    color: var(--clr-text-muted);
    margin-top: 0.35rem;
    line-height: 1.55;
    font-size: 0.78rem;
}

/* ---------- Table I/O ---------- */
.table-io {
    margin-bottom: 0.6rem;
}
.table-io input[type="text"] {
    width: 100%;
    padding: 0.35rem 0.5rem;
    font-family: var(--ff-mono);
    font-size: 0.7rem;
    background: var(--clr-surface);
    border: 1px solid var(--clr-surface-alt);
    border-radius: var(--radius);
    color: var(--clr-text);
    margin-bottom: 0.4rem;
}
.table-io input:invalid {
    border-color: var(--clr-danger);
}
.table-io__buttons {
    display: flex;
    gap: 0.4rem;
    justify-content: center;
    flex-wrap: wrap;
}

/* ---------- Meta / Warning / Sample ---------- */
.meta-box {
    margin: 0.6rem 0;
    padding: 0.55rem 0.85rem;
    border: 1px solid var(--clr-surface-alt);
    border-radius: var(--radius);
    background: var(--clr-surface);
    font-size: 0.82rem;
    min-height: 2.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
.warning {
    color: var(--clr-accent);
    font-size: 0.82rem;
    animation: fadeSlideIn 0.4s ease;
    text-align: center;
    margin: 0.4rem 0;
}
@keyframes fadeSlideIn {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}
.sample-size {
    margin-top: 0.6rem;
}
.sample-size label {
    display: block;
    font-size: 0.82rem;
    color: var(--clr-text-muted);
    margin-bottom: 0.2rem;
}

/* ---------- Floating Mobile Sample Control ---------- */
.floating-sample {
    position: absolute;
    z-index: 100;
    background: var(--clr-surface-alt);
    border: 1px solid var(--clr-accent);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: var(--clr-text);
    /* JS positions this near the tap location */
}
.floating-sample label {
    white-space: nowrap;
}
.floating-sample input[type="range"] {
    width: 100px;
}

/* ---------- Loader ---------- */
.loader {
    width: 24px;
    height: 24px;
    border: 3px solid var(--clr-surface-alt);
    border-top-color: var(--clr-accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ============================================================
   EDUCATIONAL SECTION
   ============================================================ */
.edu {
    max-width: 72rem;
    margin: 1.25rem auto 0;
    padding: 1rem 1.25rem;
}
.edu__title {
    text-align: center;
    font-size: clamp(1.2rem, 3vw, 1.8rem);
    color: var(--clr-accent);
    margin-bottom: 0.4rem;
}
.edu--decode .edu__title {
    color: var(--clr-decode);
}
.edu__intro {
    text-align: center;
    max-width: 48rem;
    margin: 0 auto 1rem;
    color: var(--clr-text-muted);
    font-size: 0.88rem;
}

/* ---------- Collapsible Steps ---------- */
.edu__step {
    background: var(--clr-surface);
    border-radius: var(--radius-lg);
    margin-bottom: 0.6rem;
    box-shadow: var(--shadow);
    overflow: hidden;
}
.edu__step-header {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.75rem 1.1rem;
    cursor: pointer;
    list-style: none;
    user-select: none;
}
.edu__step-header::-webkit-details-marker {
    display: none;
}
.edu__step-header::marker {
    content: "";
}

.edu__step-number {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    background: var(--clr-accent);
    color: var(--clr-bg);
    font-weight: 700;
    font-size: 0.95rem;
    flex-shrink: 0;
}
.edu__step-number--decode {
    background: var(--clr-decode);
}
.edu__step h3 {
    font-size: 1rem;
    color: var(--clr-white);
    flex: 1;
}
.edu__chevron {
    width: 1.2rem;
    height: 1.2rem;
    border-right: 2px solid var(--clr-text-muted);
    border-bottom: 2px solid var(--clr-text-muted);
    transform: rotate(45deg);
    transition: transform 0.25s ease;
    flex-shrink: 0;
}
.edu__step[open] > .edu__step-header .edu__chevron {
    transform: rotate(-135deg);
}

.edu__step-body {
    padding: 0 1.1rem 1rem;
    animation: slideOpen 0.3s ease;
}
@keyframes slideOpen {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.edu__step p {
    color: var(--clr-text-muted);
    margin-bottom: 0.75rem;
    line-height: 1.6;
    font-size: 0.9rem;
}
.edu__note {
    background: rgba(196, 154, 108, 0.1);
    border-left: 3px solid var(--clr-accent);
    padding: 0.6rem 0.85rem;
    border-radius: 0 var(--radius) var(--radius) 0;
    font-size: 0.85rem;
    margin-top: 0.75rem;
}
.edu__sub-detail {
    margin-top: 0.75rem;
    background: var(--clr-bg);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
}
.edu__sub-detail summary {
    cursor: pointer;
    color: var(--clr-accent-alt);
    font-size: 0.85rem;
}

/* Arrow indicators between canvases */
.edu__arrow {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6rem;
    color: var(--clr-accent);
    font-weight: 700;
    min-width: 2rem;
    flex-shrink: 0;
}
@media (max-width: 600px) {
    .edu__arrow {
        transform: rotate(90deg);
        margin: 0.25rem auto;
    }
}

/* Canvas cards */
.edu__canvas-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center;
    align-items: center;
}
.edu__canvas-card {
    flex: 1 1 160px;
    max-width: 240px;
    text-align: center;
}
.edu__canvas-card--wide {
    flex: 1 1 280px;
    max-width: 380px;
}
.edu__canvas-card h4 {
    font-size: 0.78rem;
    color: var(--clr-accent-alt);
    margin-bottom: 0.35rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.edu__canvas-card canvas {
    width: 100%;
    height: auto;
    border-radius: var(--radius);
    background: var(--clr-bg);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
.edu__animate-btn {
    margin-top: 0.5rem;
}

/* Stream display */
.stream-display {
    font-family: var(--ff-mono);
    font-size: 0.72rem;
    background: var(--clr-bg);
    padding: 0.75rem;
    border-radius: var(--radius);
    max-height: 200px;
    overflow-y: auto;
    word-break: break-all;
    line-height: 1.7;
    color: var(--clr-text-muted);
}
.stream-display--small {
    max-height: 140px;
}
.stream-display .zero {
    color: var(--clr-danger);
    opacity: 0.5;
}
.stream-display .nonzero {
    color: var(--clr-success);
    font-weight: 600;
}
.stream-display .highlight {
    background: var(--clr-accent);
    color: var(--clr-bg);
    border-radius: 2px;
    padding: 0 2px;
}

/* ---------- Footer ---------- */
.footer {
    text-align: center;
    padding: 1.25rem 1.25rem;
    color: var(--clr-text-muted);
    font-size: 0.76rem;
    line-height: 1.55;
    border-top: 1px solid var(--clr-surface-alt);
    margin-top: 1rem;
}
.footer p + p {
    margin-top: 0.3rem;
}
.footer a {
    color: var(--clr-accent-alt);
}

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    padding: 0.65rem 1.1rem;
    background: var(--clr-surface-alt);
    color: var(--clr-text);
    border-left: 4px solid var(--clr-accent);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    font-size: 0.85rem;
    z-index: 1000;
    animation:
        toastIn 0.3s ease,
        toastOut 0.3s ease 2.7s forwards;
}
@keyframes toastIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
}
@keyframes toastOut {
    to {
        opacity: 0;
        transform: translateY(20px);
    }
}

/* ---------- Responsive ---------- */
@media (max-width: 600px) {
    .hero {
        padding: 1rem 0.85rem 0.75rem;
    }
    .hero__subtitle {
        font-size: 0.82rem;
    }
    .hero__attribution {
        font-size: 0.72rem;
    }
    .edu__step-body {
        padding: 0 0.85rem 0.85rem;
    }
    .edu__step-header {
        padding: 0.6rem 0.85rem;
    }
    .controls-layout__table {
        max-width: 100%;
    }
    .controls-layout__sliders {
        max-width: 100%;
    }
    .btn--primary {
        font-size: 0.9rem;
        padding: 0.55rem 1.25rem;
    }
    .snippet-canvas {
        max-width: 160px;
    }
    .edu__canvas-card {
        max-width: 100%;
        flex: 1 1 100%;
    }
    .edu__canvas-card--wide {
        max-width: 100%;
    }
    /* Hide static sample control on mobile - floating one appears instead */
    .sample-size {
        display: none;
    }
}
