/* ============================================================
   PUZZLE CUTLINE MAKER — Light cream sidebar · Slate blue accent
   Adapted for generous touch targets & older users.
   Min touch target: 48px. Base font: 15px. Generous spacing.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;500;600;700;800&display=swap');

:root {
    --bg-canvas:  oklch(14% 0.008 250);

    --bg-sidebar: oklch(97% 0.008 80);
    --bg-panel:   oklch(93% 0.010 80);
    --bg-input:   oklch(88% 0.010 80);
    --bg-hover:   oklch(83% 0.012 80);
    --bg-lib:     oklch(90% 0.010 80);

    --text:       oklch(22% 0.015 250);
    --text-dim:   oklch(42% 0.012 240);
    --text-faint: oklch(60% 0.010 240);

    --accent:     oklch(52% 0.13 250);
    --accent-hi:  oklch(46% 0.14 250);
    --accent-dim: oklch(52% 0.13 250 / 0.12);
    --accent-text: oklch(98% 0.005 250);

    --green: oklch(50% 0.14 150);

    --r-sm:   10px;
    --r-md:   14px;
    --r-lg:   18px;
    --r-pill: 100px;

    --font: 'Nunito', sans-serif;

    /* Touch/spacing scale — everything derives from this */
    --touch-min:  48px;   /* minimum tap target height */
    --gap-xs:      6px;
    --gap-sm:     10px;
    --gap-md:     16px;
    --gap-lg:     22px;
    --panel-pad:  18px;
}

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

body {
    font-family: var(--font);
    background: var(--bg-canvas);
    color: var(--text);
    overflow: hidden;
    font-size: 15px;   /* up from 14px — easier to read */
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

#app {
    display: flex;
    height: 100vh;
}

/* ── SIDEBAR ─────────────────────────────────────────────── */
#sidebar {
    width: clamp(340px, 26vw, 400px);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    overflow: hidden;   /* scroll handled by #sidebar-scroll child */
    z-index: 10;
    box-shadow: 2px 0 16px oklch(0% 0 0 / 0.15);
    container-type: inline-size;
}

/* Inner scroll wrapper — lets the generate bar sit outside the scroll */
#sidebar-scroll {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: var(--bg-input) transparent;
    padding-bottom: 10px;
}

#sidebar-scroll::-webkit-scrollbar { width: 6px; }
#sidebar-scroll::-webkit-scrollbar-thumb { background: var(--bg-input); border-radius: 4px; }
#sidebar-scroll::-webkit-scrollbar-track { background: transparent; }

/* Mobile-only generate bar — hidden on desktop */
#mobile-generate-bar { display: none; }

#sidebar-header {
    padding: 22px var(--panel-pad) 18px;
    border-bottom: 1px solid var(--bg-panel);
    margin-bottom: 10px;
}

#sidebar-logo {
    width: 100%;
    max-height: 64px;
    object-fit: contain;
    object-position: center;
    display: block;
}

/* ── PANELS ─────────────────────────────────────────────── */
.panel {
    padding: var(--panel-pad);
    background: var(--bg-panel);
    border-radius: var(--r-md);
    margin: 0 10px 10px;
}

.panel-label {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: var(--gap-md);
}

.panel-hint {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-faint);
    margin-bottom: 0;
    line-height: 1.5;
}

/* ── BUTTONS ─────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    /* min-height 48px satisfies large-finger touch target */
    min-height: var(--touch-min);
    padding: 0 20px;
    border: none;
    border-radius: var(--r-pill);
    background: var(--bg-input);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
    text-align: center;
    white-space: nowrap;
    line-height: 1;
    margin-bottom: 0;
    outline: none;
}

.btn:hover  { background: var(--bg-hover); }
.btn:active { transform: scale(0.97); }
.btn:focus-visible {
    box-shadow: 0 0 0 3px var(--accent-dim), 0 0 0 5px var(--accent);
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-text);
}
.btn-primary:hover {
    background: var(--accent-hi);
    color: var(--accent-text);
}

.btn-ghost {
    background: var(--bg-input);
    color: var(--text-dim);
}
.btn-ghost:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.upload-btn {
    width: 100%;
    margin-bottom: var(--gap-sm);
}

.file-input-hidden { display: none; }

.btn-row {
    display: flex;
    gap: var(--gap-sm);
}
.btn-row .btn { flex: 1; }

/* ── SLIDERS ─────────────────────────────────────────────── */
.slider-field {
    margin-bottom: var(--gap-md);
}

.slider-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.field-label {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-dim);
}

.val-badge {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-dim);
    background: var(--bg-input);
    border-radius: var(--r-pill);
    padding: 4px 12px;
    min-width: 48px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    outline: none;
    border: none;
    cursor: pointer;
    margin: 6px 0;
    display: block;
}

input[type="range"]:focus-visible {
    box-shadow: 0 0 0 3px var(--accent-dim);
    border-radius: 3px;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 28px;        /* big enough for a confident grab */
    height: 28px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid var(--bg-sidebar);
    box-shadow: 0 1px 4px oklch(0% 0 0 / 0.2);
    transition: transform 0.1s, background 0.15s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    background: var(--accent-hi);
    transform: scale(1.1);
}

input[type="range"]::-moz-range-thumb {
    width: 28px;
    height: 28px;
    background: var(--accent);
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid var(--bg-sidebar);
}

/* ── SELECT & NUMBER ────────────────────────────────────── */
.field-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--gap-sm);
    gap: var(--gap-sm);
}

.select-field,
.num-input {
    background: var(--bg-input);
    border: none;
    color: var(--text);
    border-radius: var(--r-sm);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 600;
    /* min-height matches touch target */
    min-height: var(--touch-min);
    padding: 0 12px;
    outline: none;
    transition: background 0.15s, box-shadow 0.15s;
}

.select-field:hover,
.num-input:hover {
    background: var(--bg-hover);
}

.select-field:focus,
.select-field:focus-visible,
.num-input:focus,
.num-input:focus-visible {
    background: var(--bg-hover);
    box-shadow: 0 0 0 2px var(--accent);
}

.select-field { min-width: 130px; cursor: pointer; }
.select-sm    { min-width: 90px; }
.num-input    { width: 80px; text-align: right; }

input[type="checkbox"] {
    width: 20px;
    height: 20px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* ── LIBRARY GRIDS ──────────────────────────────────────── */
.lib-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-auto-rows: auto;
    align-content: start;
    gap: 7px;
    max-height: 250px;
    overflow-y: auto;
    padding: 5px;
    background: var(--bg-lib);
    border-radius: var(--r-sm);
    margin-top: var(--gap-sm);
    scrollbar-width: thin;
    scrollbar-color: var(--bg-hover) transparent;
}

.lib-grid::-webkit-scrollbar { width: 4px; }
.lib-grid::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 4px; }

.library-item {
    aspect-ratio: 1;
    width: 100%;
    height: auto;
    box-sizing: border-box;
    overflow: hidden;
    background: var(--bg-panel);
    border: 2px solid transparent;
    border-radius: var(--r-sm);
    padding: 8px;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s, border-color 0.15s, box-shadow 0.15s;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    outline: none;
}

.library-item:hover {
    background: var(--accent-dim);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px oklch(0% 0 0 / 0.12);
    z-index: 1;
}

.library-item.selected,
.library-item[aria-selected="true"] {
    background: var(--accent-dim);
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent);
}

.library-item:focus-visible {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent);
}

.library-item img {
    max-width: 100%;
    max-height: 60%;
    object-fit: contain;
    flex-shrink: 1;
    min-height: 0;
}

.library-item span {
    margin-top: 5px;
    font-size: 0.75rem;   /* up from 0.7rem — more legible */
    font-weight: 600;
    text-align: center;
    color: var(--text-faint);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    display: block;
    flex-shrink: 0;
}

/* ── EXPORT ─────────────────────────────────────────────── */
.export-block {
    background: var(--bg-input);
    border-radius: var(--r-md);
    padding: var(--gap-md);
    margin-bottom: var(--gap-sm);
}

.export-title {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-faint);
    margin-bottom: var(--gap-sm);
}

.export-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--gap-sm);
    gap: var(--gap-sm);
}

.export-block .btn {
    width: 100%;
    margin-top: var(--gap-sm);
}

.color-input {
    width: 48px;
    height: 48px;   /* matches touch target */
    border: none;
    border-radius: var(--r-sm);
    padding: 3px;
    background: var(--bg-hover);
    cursor: pointer;
    outline: none;
    transition: box-shadow 0.15s, transform 0.1s;
}

.color-input:hover {
    transform: scale(1.08);
    box-shadow: 0 2px 8px oklch(0% 0 0 / 0.15);
}

.color-input:focus-visible {
    box-shadow: 0 0 0 2px var(--accent);
}

/* ── CANVAS AREA ─────────────────────────────────────────── */
#canvas-container {
    flex: 1;
    position: relative;
    background: var(--bg-canvas);
    overflow: hidden;
}

#preview {
    width: 100%;
    height: 100%;
    cursor: crosshair;
    display: block;
}

#image-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0.35;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;
    z-index: 1;
    mix-blend-mode: screen;
}

/* ── CANVAS BOTTOM CONTROLS ──────────────────────────────── */
/* Desktop: floats over canvas, centred within canvas-container */
#canvas-bottom-controls {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 24px;
    background: oklch(97% 0.008 80 / 0.95);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: none;
    border-radius: var(--r-pill);
    padding: 12px 24px;
    z-index: 20;
    box-shadow: 0 4px 24px oklch(0% 0 0 / 0.22);
}

.control-group {
    display: flex;
    align-items: center;
    gap: var(--gap-md);
}

.control-group .field-label {
    white-space: nowrap;
    font-size: 14px;
    color: var(--text-dim);
}

#canvas-bottom-controls input[type="range"] {
    width: 150px;
}

.btn-generate {
    min-height: 52px;
    width: 200px;       /* fixed — won't resize as label changes */
    flex-shrink: 0;
    border-radius: var(--r-pill);
    font-size: 15px;
    white-space: nowrap;
}

/* ── ZOOM CONTROLS ───────────────────────────────────────── */
#zoom-controls {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 4px;
    background: oklch(97% 0.008 80 / 0.93);
    backdrop-filter: blur(10px);
    border: none;
    border-radius: var(--r-pill);
    padding: 6px 10px;
    box-shadow: 0 2px 12px oklch(0% 0 0 / 0.18);
}

#zoom-controls button {
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-dim);
    font-size: 20px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s, background 0.15s, box-shadow 0.15s;
    line-height: 1;
    margin: 0;
    font-family: var(--font);
    text-transform: none;
    letter-spacing: 0;
    outline: none;
}

#zoom-controls button:hover {
    background: var(--bg-input);
    color: var(--accent);
}
#zoom-controls button:active { transform: scale(0.9); }
#zoom-controls button:focus-visible {
    background: var(--bg-input);
    box-shadow: 0 0 0 2px var(--accent);
}

#zoom-label {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-dim);
    min-width: 50px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    background: none;
    border: none;
    padding: 0 4px;
}

/* ── CONTROLS HINT ───────────────────────────────────────── */
#controls-hint {
    position: absolute;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    background: oklch(97% 0.008 80 / 0.93);
    backdrop-filter: blur(10px);
    padding: 10px 24px;
    border-radius: var(--r-pill);
    border: none;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-faint);
    pointer-events: none;
    white-space: nowrap;
    z-index: 10;
    box-shadow: 0 2px 12px oklch(0% 0 0 / 0.12);
}

#controls-hint strong {
    color: var(--text-dim);
    margin-right: 4px;
}

/* ── MOBILE ───────────────────────────────────────────────── */
@media (max-width: 1024px) {
    #app { flex-direction: column; }

    /* dvh = dynamic viewport height — accounts for Android browser chrome */
    #canvas-container {
        flex: 0 0 50dvh;
        width: 100%;
        order: 2;
        overflow: hidden;
    }

    #sidebar {
        width: 100%;
        flex: 0 0 50dvh;
        order: 1;
        border-top: none;
        box-shadow: none;
        overflow: hidden;
    }

    #sidebar-scroll {
        flex: 1;
        overflow-y: auto;
    }
	
	#sidebar-scroll {
        display: flex;
        flex: 1;
        overflow: hidden;
        position: relative;
    }

    #sidebar-scroll-inner {
		flex: 1;
		overflow-y: auto;
		overflow-x: hidden;
		-webkit-overflow-scrolling: touch;
		scrollbar-width: none;
		touch-action: pan-y;          /* ADD THIS */
	}

    #sidebar-scroll-inner::-webkit-scrollbar { display: none; }

    #sidebar-scrollbar {
        width: 24px;
        flex-shrink: 0;
        background: var(--bg-panel);
        border-radius: 999px;
        margin: 4px;
        position: relative;
    }

    #sidebar-scrollbar-thumb {
		position: absolute;
		top: 0;
		left: 3px;
		width: 20px;
		background: var(--accent);
		border-radius: 999px;
		min-height: 60px;
		transition: background 0.15s;
		pointer-events: auto;
		cursor: grab;
		touch-action: none;           /* ADD THIS */
	}

    #sidebar-scrollbar-thumb:active {
        cursor: grabbing;
        background: var(--accent-hi);
    }


    /* Generous side margins — sliders stay clear of screen edges during scroll */
    .panel {
        margin: 0 24px 10px;
        padding: var(--panel-pad) 14px;
    }

    /* Desktop generate bar hidden on mobile — replaced by #mobile-generate-bar */
    #canvas-bottom-controls {
        display: none;
    }

    /* Mobile generate bar: sits at the visual bottom of the sidebar, outside scroll */
    #mobile-generate-bar {
        display: flex;
        flex-shrink: 0;
        flex-direction: row;
        align-items: center;
        gap: 12px;
        padding: 8px 16px;
        background: var(--bg-sidebar);
        border-top: 1px solid var(--bg-panel);
        box-shadow: 0 -2px 8px oklch(0% 0 0 / 0.08);
        width: 100%;
    }

    #mobile-generate-bar .control-group {
        flex-direction: row;
        align-items: center;
        gap: var(--gap-sm);
        flex: 1;
        min-width: 0;
    }

    #mobile-generate-bar .field-label {
        white-space: nowrap;
        flex-shrink: 0;
        font-size: 13px;
    }

    #mobile-generate-bar input[type="range"] {
        flex: 1;
        min-width: 60px;
    }

    #mobile-generate-bar .btn-generate {
        min-height: 40px;
        width: 180px;       /* fixed — matches longest label "Done (999 pieces)" */
        flex-shrink: 0;
        font-size: 13px;
        white-space: nowrap;
        border-radius: var(--r-pill);
    }

    .control-group {
        flex-direction: row;
        align-items: center;
        gap: var(--gap-sm);
        flex: 1;
        min-width: 0;
    }

    .control-group .field-label {
        white-space: nowrap;
        flex-shrink: 0;
        font-size: 13px;
    }

    #canvas-bottom-controls input[type="range"] {
        flex: 1;
        min-width: 60px;
    }

    .btn-generate {
        min-height: 40px;
        padding: 0 16px;
        font-size: 13px;
        white-space: nowrap;
        border-radius: var(--r-pill);
        flex-shrink: 0;
    }

    .export-row { flex-wrap: wrap; }
    .num-input, .select-field { width: 100%; min-width: unset; }

    #controls-hint { display: none; }
    #zoom-controls  { display: none; }
}

/* ── REDUCED MOTION ──────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
}
