:root {
    --bg-color: #F5F0E8;
    --text-color: #1a1a1a;
    --border-color: #1a1a1a;
    --primary-color: #3b82f6;
    --secondary-color: #64748b;
    --accent-color: #10b981;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'IBM Plex Mono', monospace;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    padding: 2rem;
    display: flex;
    justify-content: center;
}

h1, h2, h3, .title, .mockup-title {
    font-family: 'Space Grotesk', sans-serif;
    font-weight: 700;
    text-transform: uppercase;
}

.mono-text {
    font-family: 'IBM Plex Mono', monospace;
}

.grid-layout {
    width: 100%;
    max-width: 1200px;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 2rem;
    border: 2px solid var(--border-color);
    padding: 3rem;
    background: var(--bg-color);
    position: relative;
    /* Risograph offset overprint effect */
    box-shadow: 4px 4px 0 var(--accent-color);
    /* Compensate shadow for visual centering */
    transform: translateX(-2px);
}

.header-section {
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: 1rem;
}

.title {
    font-size: clamp(3rem, 5vw, 5rem);
    line-height: 0.9;
    letter-spacing: -0.05em;
    margin-bottom: 1rem;
}

.subtitle {
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.85rem;
    font-weight: 600;
    letter-spacing: 0.05em;
}

.settings-bar {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.85rem;
    font-weight: 600;
    padding: 0.5rem 1rem;
    border: 2px solid var(--border-color);
    box-shadow: 2px 2px 0 var(--accent-color);
    background: var(--bg-color);
}

.settings-bar input[type="range"] {
    cursor: pointer;
    accent-color: var(--border-color);
}

.main-section {
    display: grid;
    grid-template-columns: minmax(0, 0.5fr) minmax(0, 1fr);
    gap: 3rem;
    align-items: start;
}

.left-col {
    position: sticky;
    top: 2rem;
}

/* Crop marks for drop zone */
.drop-zone {
    position: relative;
    width: 100%;
    aspect-ratio: 4/5;
    max-height: 80vh;
    border: 2px dotted var(--border-color);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    background: rgba(0,0,0,0.02);
    overflow: hidden;
    transition: background 0.2s;
}

.drop-zone:hover, .drop-zone.dragover {
    background: rgba(0,0,0,0.05);
}

/* Only block page scrolling once there is an image to drag-select on */
.drop-zone.has-image { touch-action: none; }

.selection-box {
    position: absolute;
    border: 2px dashed var(--text-color);
    background: rgba(0, 0, 0, 0.1);
    display: none;
    z-index: 5;
    pointer-events: none;
    mix-blend-mode: difference;
}

.crop-mark {
    position: absolute;
    width: 20px;
    height: 20px;
    border: 3px solid var(--border-color);
    z-index: 2;
}
.crop-mark.top-left { top: -3px; left: -3px; border-right: none; border-bottom: none; }
.crop-mark.top-right { top: -3px; right: -3px; border-left: none; border-bottom: none; }
.crop-mark.bottom-left { bottom: -3px; left: -3px; border-right: none; border-top: none; }
.crop-mark.bottom-right { bottom: -3px; right: -3px; border-left: none; border-top: none; }

.drop-content {
    text-align: center;
    z-index: 2;
    padding: 0 1rem;
}
.drop-content[hidden] { display: none !important; }

.upload-graphic {
    width: 64px;
    height: 64px;
    border: 2px solid var(--border-color);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto 1.5rem auto;
    background: transparent;
    color: var(--border-color);
    transition: all 0.2s ease;
}
.drop-zone:hover .upload-graphic {
    transform: translateY(-2px);
    background: var(--border-color);
    color: var(--bg-color);
}

.drop-text { 
    font-weight: 700; 
    font-size: 1.15rem; 
    font-family: 'Space Grotesk', sans-serif;
    display: block;
    margin-bottom: 0.5rem;
}
.drop-sub { 
    font-size: 0.75rem; 
    font-weight: 600; 
    opacity: 0.5; 
    font-family: 'IBM Plex Mono', monospace;
    line-height: 1.4;
    display: block;
    white-space: nowrap;
}

#preview-canvas {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    object-fit: contain;
    opacity: 0;
    transition: opacity 0.5s step-end;
    image-rendering: pixelated;
    mix-blend-mode: multiply; /* Key for print aesthetic */
    z-index: 1;
}
#preview-canvas.visible { opacity: 1; }

.loading-overlay {
    position: absolute; top:0; left:0; width:100%; height:100%;
    background: var(--bg-color);
    display: flex; justify-content: center; align-items: center;
    z-index: 10;
}
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0,0,0,0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
    opacity: 0;
    pointer-events: none;
}
.loading-text {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 2rem;
    animation: blink 0.5s steps(2, start) infinite;
}
@keyframes blink { to { opacity: 0; } }

.section-header {
    display: flex; justify-content: space-between; align-items: flex-end;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

h2 { font-size: 2rem; letter-spacing: -0.02em; }

.export-actions { display: flex; gap: 0.5rem; }
.btn-export {
    font-family: 'IBM Plex Mono', monospace;
    background: transparent;
    border: 2px solid var(--border-color);
    color: var(--border-color);
    padding: 0.25rem 0.75rem;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.1s;
}
.btn-export:hover {
    background: var(--border-color);
    color: var(--bg-color);
    box-shadow: 3px 3px 0 var(--accent-color);
    transform: translate(-3px, -3px);
}

.palette {
    display: flex;
    height: 160px;
    border: 2px solid var(--border-color);
    margin-bottom: 2rem;
    box-shadow: 4px 4px 0 rgba(0,0,0,0.1);
}

.swatch {
    flex: 1;
    position: relative;
    border-right: 2px solid var(--border-color);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: clamp(0.25rem, 5cqw, 0.75rem);
    font-size: 0.75rem;
    cursor: pointer;
    overflow: hidden;
    container-type: inline-size;
    /* Ink bleed animation */
    animation: inkBleed 0.6s cubic-bezier(0, 0, 0, 1) backwards;
    mix-blend-mode: multiply; /* Risograph overlapping color effect */
}
.swatch:last-child { border-right: none; }

@keyframes inkBleed {
    0% { filter: blur(6px); opacity: 0; }
    100% { filter: blur(0); opacity: 1; }
}

.swatch-content {
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: space-between;
}

.swatch-header {
    font-weight: 600;
    text-transform: uppercase;
    border-bottom: 2px solid currentColor;
    padding-bottom: 2px;
    font-family: 'Space Grotesk', sans-serif;
    letter-spacing: -0.02em;
    font-size: clamp(0.4rem, 16cqw, 1rem);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.swatch-footer {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.swatch-hex {
    font-size: clamp(0.45rem, 15cqw, 0.9rem);
    font-weight: 600;
}

.swatch-rating {
    font-size: clamp(0.4rem, 12cqw, 0.7rem);
    border-top: 1px solid currentColor;
    padding-top: 2px;
    height: auto;
    min-height: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.color-input-wrapper {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    width: 20px; height: 20px;
    border: 2px solid currentColor;
    display: flex; justify-content: center; align-items: center;
    opacity: 0; transition: opacity 0.1s;
    background: var(--bg-color);
}
.swatch:hover .color-input-wrapper { opacity: 1; }
.color-input-wrapper input[type="color"] {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    opacity: 0;
    cursor: pointer;
    -webkit-appearance: none;
    border: none;
    padding: 0;
    box-sizing: border-box;
}
.color-input-wrapper svg { width: 12px; height: 12px; color: var(--border-color); }

.ui-mockup {
    border: 2px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-color);
    padding: 2rem;
    margin-top: 1rem;
    position: relative;
    /* Offset shadow for Riso feel */
    box-shadow: 4px 4px 0 var(--border-color);
}

.proof-strip {
    display: flex;
    height: 10px;
    margin: -2rem -2rem 1.5rem -2rem;
    border-bottom: 2px solid var(--border-color);
    overflow: hidden;
}
.proof-strip-color {
    flex: 1;
    height: 100%;
    mix-blend-mode: multiply;
}
.proof-strip-color + .proof-strip-color {
    border-left: 1px solid var(--border-color);
}

.mockup-header { 
    display: flex; justify-content: space-between; align-items: baseline; 
    border-bottom: 4px solid var(--border-color); 
    padding-bottom: 0.5rem; margin-bottom: 1rem; 
}
.mockup-title { font-size: 2.5rem; color: var(--border-color); line-height: 1;}
.badge {
    background: var(--accent-color);
    color: var(--bg-color);
    padding: 0.25rem 0.5rem;
    font-weight: 600;
    font-size: 0.85rem;
    border: 2px solid var(--border-color);
}

.proof-meta {
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    line-height: 1.8;
}
.proof-meta-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.15rem;
    align-items: baseline;
}
.proof-label {
    opacity: 0.4;
    letter-spacing: 0.05em;
}
.proof-value {
    letter-spacing: 0.02em;
}
.proof-sep {
    opacity: 0.25;
    margin: 0 0.25rem;
}

.progress-track {
    height: 32px;
    border: 2px solid var(--border-color);
    margin-bottom: 2rem;
    background: transparent;
    position: relative;
}
.progress-fill {
    height: 100%;
    background: var(--primary-color);
    border-right: 2px solid var(--border-color);
    transition: width 0.5s steps(10, end); /* Halftone mechanical feel */
}

.mockup-buttons { display: flex; gap: 1rem; }
.mockup-button, .mockup-button-secondary {
    font-family: 'IBM Plex Mono', monospace;
    padding: 0.75rem 1.5rem;
    font-size: 0.85rem;
    font-weight: 600;
    border: 2px solid var(--border-color);
    cursor: pointer;
    text-transform: uppercase;
}
.mockup-button {
    background: var(--primary-color);
    color: var(--bg-color);
}
.mockup-button-secondary {
    background: transparent;
    color: var(--border-color);
    box-shadow: 4px 4px 0 var(--accent-color);
}
.mockup-button:active {
    transform: translate(2px, 2px);
}
.mockup-button-secondary:active {
    transform: translate(2px, 2px);
    box-shadow: 2px 2px 0 currentColor;
}

.history-container { display: flex; gap: 1rem; overflow-x: auto; padding-bottom: 1rem; }
.favorites-wrap { flex-wrap: wrap; overflow-x: visible; }
.history-item {
    width: 100px;
    min-width: 100px;
    height: 60px;
    flex-shrink: 0;
    border: 2px solid var(--border-color);
    display: flex; cursor: pointer;
    box-shadow: 3px 3px 0 var(--border-color);
    transition: all 0.1s;
}
.history-item:hover { transform: translate(-2px, -2px); box-shadow: 5px 5px 0 var(--accent-color); }
.history-color { flex: 1; height: 100%; border-right: 2px solid var(--border-color); mix-blend-mode: multiply; }
.history-color:last-child { border-right: none; }

/* A11Y Contrast Matrix */
.a11y-matrix {
    border: 2px solid var(--border-color);
    margin-top: 1rem;
    overflow-x: auto;
}

.a11y-matrix table {
    width: 100%;
    border-collapse: collapse;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.75rem;
    font-weight: 600;
    table-layout: fixed;
}

.a11y-matrix th,
.a11y-matrix td {
    padding: 0.5rem 0.25rem;
    text-align: center;
    border: 1px solid var(--border-color);
    white-space: nowrap;
    overflow: hidden;
}

.a11y-matrix th {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 0.7rem;
    letter-spacing: 0.02em;
    background: var(--bg-color);
}

.a11y-matrix td.a11y-cell {
    position: relative;
    cursor: default;
    transition: transform 0.1s;
}

.a11y-matrix td.a11y-cell:hover {
    transform: scale(1.05);
    z-index: 2;
    box-shadow: 2px 2px 0 var(--accent-color);
}

.a11y-cell-ratio {
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1;
}

.a11y-cell-badge {
    font-size: 0.6rem;
    margin-top: 2px;
    letter-spacing: 0.05em;
    opacity: 0.8;
}

.a11y-cell--aaa {
    background: #1a1a1a;
    color: #F5F0E8;
}

.a11y-cell--aa {
    background: #3b6;
    color: #1a1a1a;
    mix-blend-mode: multiply;
}

.a11y-cell--aa-large {
    background: #e8c840;
    color: #1a1a1a;
    mix-blend-mode: multiply;
}

.a11y-cell--fail {
    background: transparent;
    color: var(--text-color);
    opacity: 0.3;
}

.a11y-cell--self {
    background: repeating-linear-gradient(
        45deg,
        transparent,
        transparent 3px,
        rgba(0,0,0,0.06) 3px,
        rgba(0,0,0,0.06) 6px
    );
    color: transparent;
}

.a11y-legend {
    display: flex;
    gap: 1rem;
    padding: 0.5rem 0.75rem;
    font-size: 0.7rem;
    font-weight: 600;
    font-family: 'IBM Plex Mono', monospace;
    border-top: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.a11y-legend-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.a11y-legend-swatch {
    width: 12px;
    height: 12px;
    border: 1px solid var(--border-color);
}

/* Paste feedback */
.paste-flash {
    animation: pasteFlash 0.4s ease-out;
}
@keyframes pasteFlash {
    0% { box-shadow: inset 0 0 0 4px var(--accent-color); }
    100% { box-shadow: inset 0 0 0 0 transparent; }
}

@media (max-width: 900px) {
    .main-section { grid-template-columns: minmax(0, 1fr); }
    .header-section { flex-direction: column; align-items: flex-start; gap: 1rem; }
    .title { font-size: 3rem; }
    .left-col { position: relative; } /* Disable sticky in single-column layout */
    .drop-zone { min-height: 300px; }
}

@media (max-width: 600px) {
    body { padding: 0.5rem; }
    .grid-layout { padding: 1.5rem; gap: 1.5rem; }
    
    .title { font-size: 2.2rem; }
    
    .header-section { align-items: stretch; }
    .settings-bar { width: 100%; justify-content: space-between; }
    
    .section-header { flex-direction: column; align-items: stretch; gap: 0.5rem; }
    
    /* Export buttons: row 1 = STAR + SAVE IMG, row 2 = FIGMA + TAILWIND + CSS */
    .export-actions { flex-wrap: wrap; gap: 0.5rem; }
    .btn-export:nth-child(1),
    .btn-export:nth-child(2) { flex: 1 1 45%; }
    .btn-export:nth-child(n+3) { flex: 1 1 30%; }
    
    .palette {
        flex-direction: column;
        height: auto;
    }
    .swatch {
        flex-direction: row;
        align-items: center;
        border-right: none;
        border-bottom: 2px solid var(--border-color);
        padding: 1rem;
    }
    .swatch:last-child {
        border-bottom: none;
    }
    .swatch-content {
        flex: 1;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        margin-right: 1rem;
    }
    .swatch-header {
        border-bottom: none;
        padding-bottom: 0;
        font-size: 1rem;
    }
    .swatch-footer {
        align-items: flex-end;
    }
    .swatch-hex {
        font-size: 1rem;
    }
    .swatch-rating {
        height: auto;
        font-size: 0.8rem;
    }
    .color-input-wrapper {
        position: relative;
        top: 0; right: 0;
        opacity: 1; /* Make visible on mobile */
        flex-shrink: 0;
        width: 24px;
        height: 24px;
        overflow: hidden;
    }
    
    /* A11Y Matrix mobile: horizontal scroll, remove blend modes (Safari issue) */
    .a11y-matrix {
        -webkit-overflow-scrolling: touch;
    }
    .a11y-matrix table {
        min-width: 360px;
    }
    .a11y-matrix th,
    .a11y-matrix td {
        padding: 0.35rem 0.15rem;
        font-size: 0.65rem;
    }
    .a11y-cell-ratio {
        font-size: 0.75rem;
    }
    .a11y-cell-badge {
        font-size: 0.5rem;
    }
    .a11y-cell--aa,
    .a11y-cell--aa-large {
        mix-blend-mode: normal; /* Safari mobile blend mode fix */
    }
    .a11y-legend {
        font-size: 0.6rem;
        gap: 0.5rem;
    }
    
    .mockup-buttons { flex-direction: column; }
    .mockup-button, .mockup-button-secondary { width: 100%; text-align: center; }
}

/* Role was lightness-shifted for contrast; the extracted colour is in the title */
.swatch-adj {
    display: inline-block;
    margin-left: 0.3em;
    padding: 0 0.25em;
    border: 1px solid currentColor;
    font-family: 'IBM Plex Mono', monospace;
    font-size: 0.6em;
    vertical-align: middle;
    cursor: help;
}

.palette-notes {
    margin: -1.25rem 0 2rem;
    font-size: 0.7rem;
    font-weight: 600;
    opacity: 0.7;
    line-height: 1.6;
}

/* Role swap: small trigger on each swatch; whole bar becomes a target while picking */
.swatch-swap {
    position: absolute;
    top: calc(0.5rem + 26px);
    right: 0.5rem;
    width: 20px;
    height: 20px;
    padding: 0;
    border: 2px solid var(--border-color);
    background: var(--bg-color);
    color: var(--border-color);
    font-size: 0.7rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
}
.swatch:hover .swatch-swap, .swatch:focus-within .swatch-swap, .palette.swapping .swatch-swap { opacity: 1; }
@media (hover: none) { .swatch-swap { opacity: 0.8; } }
.palette.swapping .swatch { cursor: crosshair; }
.swatch.swap-source { outline: 4px dashed currentColor; outline-offset: -8px; }

/* Colour-vision deficiency preview under the contrast matrix */
.cvd-panel { margin-top: 1rem; font-size: 0.7rem; font-weight: 600; }
.cvd-row { display: grid; grid-template-columns: 4.5rem minmax(0, 1fr); align-items: center; gap: 0.5rem; margin-bottom: 4px; }
.cvd-strip { display: flex; height: 18px; border: 2px solid var(--border-color); }
.cvd-strip > div { flex: 1; }
.cvd-warning { margin-top: 0.5rem; color: #c0392b; }
.cvd-ok { margin-top: 0.5rem; opacity: 0.6; }
