/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #f8f9fa;
    --surface: #ffffff;
    --border: #dee2e6;
    --border-light: #e9ecef;
    --text: #212529;
    --text-muted: #6c757d;
    --text-light: #adb5bd;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #dbeafe;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --warning: #d97706;
    --radius: 8px;
    --radius-sm: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* --- Layout --- */
.container { max-width: 1200px; margin: 0 auto; padding: 24px; }

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
}
.nav-brand { font-weight: 700; font-size: 1.2rem; color: var(--text); text-decoration: none; }
.nav-links { display: flex; gap: 16px; }
.nav-links a { color: var(--text-muted); text-decoration: none; font-size: 0.9rem; }
.nav-links a:hover { color: var(--primary); }

/* --- Typography --- */
h2 { margin-bottom: 16px; }
h3 { margin-bottom: 8px; font-size: 1rem; }
pre { background: var(--bg); padding: 12px; border-radius: var(--radius); overflow-x: auto; font-size: 0.85rem; }
.text-muted { color: var(--text-muted); font-size: 0.9rem; }
.empty-state { color: var(--text-muted); padding: 32px; text-align: center; }

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    padding: 8px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    cursor: pointer;
    text-decoration: none;
    gap: 6px;
    transition: background 0.15s, border-color 0.15s;
    white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }

/* --- Badges --- */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--bg);
    color: var(--text-muted);
}
.badge-ready { background: #dcfce7; color: var(--success); }
.badge-generating, .badge-segmenting { background: #fef3c7; color: var(--warning); }
.badge-failed { background: #fee2e2; color: var(--danger); }
.badge-pending { background: var(--bg); color: var(--text-muted); }

/* --- Cards --- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    text-decoration: none;
    color: var(--text);
    transition: box-shadow 0.15s;
}
.card:hover { box-shadow: var(--shadow-md); }
.card img { width: 100%; height: 200px; object-fit: contain; background: var(--bg); }
.card-body { padding: 12px; }
.card-placeholder {
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    color: var(--text-muted);
}
.card-link { text-decoration: none; color: inherit; display: block; }
.garment-card { position: relative; }
.card-delete {
    position: absolute;
    top: 6px;
    right: 6px;
    opacity: 0;
    transition: opacity 0.15s;
}
.garment-card:hover .card-delete { opacity: 1; }
.card-delete .btn { min-width: 28px; padding: 2px 7px; font-size: 1rem; line-height: 1; }

.card-swatches { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.card-swatch {
    display: inline-block;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1.5px solid var(--border);
    flex-shrink: 0;
}

.card-horizontal { display: flex; align-items: center; justify-content: space-between; padding: 12px; }
.card-horizontal .card-body { flex: 1; }
.card-actions { display: flex; gap: 8px; align-items: center; }

/* --- Grids --- */
.garment-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.variant-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; margin-top: 16px; }

.variant-card {
    position: relative;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}
.variant-img { width: 100%; height: 160px; object-fit: contain; }
.variant-info {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    font-size: 0.85rem;
}
.variant-info code { color: var(--text-muted); font-size: 0.75rem; }
.variant-actions { padding: 0 8px 8px; }
.variant-save { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.variant-palette-select { padding: 3px 6px; font-size: 0.8rem; border: 1px solid var(--border); border-radius: 4px; }
.variant-new-name { padding: 3px 6px; font-size: 0.8rem; border: 1px solid var(--border); border-radius: 4px; width: 100%; }
.variant-save-status { font-size: 0.8rem; }
.variant-delete { position: absolute; top: 6px; left: 6px; opacity: 0; transition: opacity 0.15s; z-index: 1; }
.variant-card:hover .variant-delete { opacity: 1; }
.variant-delete-btn {
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: white;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.variant-delete-btn:hover { background: var(--danger); }
.hidden { display: none !important; }

/* --- HTMX Indicators --- */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline-flex; align-items: center; }

/* --- Color Swatches --- */
.color-swatch {
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid var(--border);
    flex-shrink: 0;
}
.swatch-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }

/* --- Create page (two-column) --- */
.create-page { max-width: 960px; margin: 0 auto; }
.create-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: start;
}
.create-sidebar { min-width: 0; }
.create-preview { min-width: 0; }
.form-actions { display: flex; gap: 8px; margin-top: 8px; }
@media (max-width: 720px) {
    .create-form { grid-template-columns: 1fr; }
}

/* --- Preset preview --- */
.preset-preview {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}
.preset-preview-empty { display: flex; align-items: center; justify-content: center; min-height: 200px; }
.preset-preview-header { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.preset-preview-section { margin-bottom: 12px; }
.preset-preview-section:last-child { margin-bottom: 0; }
.preset-preview-section label { display: block; font-weight: 600; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.preset-preview-section pre { margin: 0; font-size: 0.85rem; white-space: pre-wrap; word-break: break-word; }
.pre-muted { color: var(--text-muted); }
.preset-preview-actions { display: flex; align-items: center; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.prompt-textarea-sm { font-size: 0.85rem; color: var(--text-muted); }
.save-confirm { font-size: 0.8rem; color: var(--success); font-weight: 600; animation: fade-out 2s ease-in forwards; }
@keyframes fade-out { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }

/* --- Forms --- */
.form-page { max-width: 600px; margin: 0 auto; }
.form-page-narrow { max-width: 400px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
.form-group input, .form-group select, .form-group textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
}
.form-group input[type="color"] { width: 60px; height: 36px; padding: 2px; cursor: pointer; }
.help-text { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.error { font-size: 0.8rem; color: var(--danger); margin-top: 4px; }

.inline-form { display: flex; gap: 12px; align-items: end; flex-wrap: wrap; margin-bottom: 16px; }
.inline-form .form-group { margin-bottom: 0; }

/* --- Sections --- */
.section { margin-bottom: 32px; }
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }

/* --- Detail Page --- */
.detail-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; gap: 16px; }
.detail-header-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }
.detail-prompt { margin-bottom: 24px; }
.prompt-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.prompt-textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 0.9rem;
    line-height: 1.5;
    resize: vertical;
    transition: border-color 0.15s;
}
.prompt-textarea:focus { outline: none; border-color: var(--primary); }
.prompt-actions { display: flex; gap: 8px; margin-top: 8px; }
.detail-images { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; margin-bottom: 32px; }
.image-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.preview-img { max-width: 100%; border-radius: var(--radius); }

/* --- Checkerboard bg for transparent images --- */
.checkerboard-bg {
    background-image:
        linear-gradient(45deg, #e0e0e0 25%, transparent 25%),
        linear-gradient(-45deg, #e0e0e0 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #e0e0e0 75%),
        linear-gradient(-45deg, transparent 75%, #e0e0e0 75%);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

/* --- Color entry rows (palette editor) --- */
.color-entry {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border-light);
}
.color-entry:last-child { border-bottom: none; }

/* --- Spinner --- */
.spinner-container { display: flex; align-items: center; gap: 12px; padding: 16px 0; }
.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Generating placeholder --- */
.generating-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 48px 16px;
    background: var(--bg);
    border-radius: var(--radius);
    color: var(--text-muted);
}

/* --- Gallery hero carousel --- */
.gallery-hero {
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 560px;
    margin: 0 auto 8px;
}
.gallery-hero-img { flex: 1; min-width: 0; }
.gallery-hero-img .img-zoomable { border-radius: var(--radius); overflow: hidden; }
.gallery-hero-label {
    text-align: center;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 6px;
}
.gallery-arrow {
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, border-color 0.15s;
}
.gallery-arrow:hover { background: var(--bg); border-color: var(--primary); color: var(--primary); }
.gallery-hero-actions {
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    margin-bottom: 24px;
}
.gallery-counter { font-size: 0.8rem; color: var(--text-muted); }

/* --- Gallery cards --- */
.gallery-card { cursor: pointer; transition: box-shadow 0.15s, border-color 0.15s; }
.gallery-card-active { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.3); }

/* --- Recolor section --- */
.recolor-section-inner {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
}
.recolor-section-inner h3 {
    margin-bottom: 16px;
    font-size: 1.05rem;
}
.recolor-row {
    display: flex;
    gap: 16px;
    align-items: end;
    flex-wrap: wrap;
}
.recolor-single {
    display: flex;
    gap: 10px;
    align-items: end;
    flex-wrap: wrap;
    flex: 1;
    min-width: 280px;
}
.recolor-single .form-group { margin-bottom: 0; }
.recolor-divider {
    width: 1px;
    height: 36px;
    background: var(--border);
    align-self: end;
    margin-bottom: 4px;
}
.recolor-palette {
    display: flex;
    gap: 10px;
    align-items: end;
    flex-wrap: wrap;
    flex: 1;
    min-width: 280px;
}
.recolor-palette .form-group { margin-bottom: 0; }
.recolor-palette select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    min-width: 160px;
}
.recolor-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border-light);
}

/* --- Enhance panel --- */
.enhance-panel { margin-bottom: 24px; }
.enhance-form {
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.enhance-fields { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.enhance-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
}
.enhance-input:focus { outline: none; border-color: var(--primary); }
.enhance-ref { display: flex; align-items: center; gap: 8px; }
.enhance-file-input { display: none; }
.enhance-file-name { font-size: 0.8rem; color: var(--text-muted); }
.enhance-actions { display: flex; gap: 8px; align-items: center; }
.enhance-form .help-text { margin-top: 8px; }

/* --- Enhancement review page --- */
.enhance-review { max-width: 960px; margin: 0 auto; }
.enhance-compare {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: start;
    margin-bottom: 24px;
}
@media (max-width: 720px) {
    .enhance-compare { grid-template-columns: 1fr; }
}
.enhance-compare-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}
.enhance-compare-panel h3 { margin-bottom: 12px; }
.enhance-decision {
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-top: 16px;
}
.enhance-accept-form {
    display: flex;
    gap: 8px;
    align-items: center;
    flex: 1;
}
.enhance-name-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
}
.enhance-name-input:focus { outline: none; border-color: var(--primary); }

/* --- Collapsible edit section --- */
.edit-section {
    margin-top: 32px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}
.edit-section-toggle {
    padding: 12px 16px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-muted);
    list-style: none;
    display: flex;
    align-items: center;
    gap: 6px;
}
.edit-section-toggle::-webkit-details-marker { display: none; }
.edit-section-toggle::before { content: "\25B6"; font-size: 0.7rem; transition: transform 0.15s; }
.edit-section[open] .edit-section-toggle::before { transform: rotate(90deg); }
.edit-section-body { padding: 0 16px 16px; }

/* --- Generated review actions --- */
.generated-actions {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}

/* --- Segmenting scan-line animation --- */
.segmenting-preview {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius);
}
.segmenting-preview .preview-img {
    display: block;
    width: 100%;
}
.scan-line {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(
        90deg,
        transparent 0%,
        var(--primary) 20%,
        rgba(37, 99, 235, 0.6) 50%,
        var(--primary) 80%,
        transparent 100%
    );
    box-shadow: 0 0 12px rgba(37, 99, 235, 0.5), 0 0 24px rgba(37, 99, 235, 0.2);
    animation: scan 2s ease-in-out infinite;
}
@keyframes scan {
    0%   { top: 0; }
    50%  { top: calc(100% - 3px); }
    100% { top: 0; }
}

/* --- Messages --- */
.messages { margin-bottom: 16px; }
.message { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 8px; font-size: 0.9rem; }
.message-success { background: #dcfce7; color: var(--success); }
.message-error { background: #fee2e2; color: var(--danger); }
.message-warning { background: #fef3c7; color: var(--warning); }

/* --- Preset filters --- */
.preset-filters { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.preset-search-input {
    flex: 1;
    min-width: 180px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
}
.preset-search-input:focus { outline: none; border-color: var(--primary); }
.preset-search-input-sm { padding: 6px 10px; font-size: 0.85rem; }
.category-tabs { flex-shrink: 0; }
.category-select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--surface);
    cursor: pointer;
}
.category-select:focus { outline: none; border-color: var(--primary); }
.badge-category { background: #e0e7ff; color: #3730a3; }
.preset-badges { display: flex; gap: 6px; margin-bottom: 4px; }

/* --- Preset listbox (create page) --- */
.preset-listbox {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    padding: 4px;
}
.preset-listbox option { padding: 4px 8px; border-radius: 4px; }
.preset-listbox option:checked { background: var(--primary); color: white; }

/* --- Preset list --- */
.preset-list { display: flex; flex-direction: column; gap: 8px; }

/* --- Editable name --- */
.editable-name { cursor: pointer; border-bottom: 1px dashed var(--border); }
.editable-name:hover { border-bottom-color: var(--primary); color: var(--primary); }

/* --- Palette link in recolor section --- */
.palette-link {
    font-size: 0.85rem;
    color: var(--primary);
    text-decoration: none;
}
.palette-link:hover { text-decoration: underline; }

/* --- Palette list page --- */
.palette-list { display: flex; flex-direction: column; gap: 8px; }
.palette-colors {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 6px;
}
.palette-color-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px 3px 4px;
    border: 1px solid var(--border-light);
    border-radius: 20px;
    font-size: 0.78rem;
    background: var(--surface);
}
.palette-color-chip .color-swatch {
    width: 16px;
    height: 16px;
}

/* --- Image actions (zoom + download) --- */
.img-zoomable { position: relative; display: inline-block; width: 100%; }
.img-zoomable .preview-img,
.img-zoomable .variant-img { display: block; width: 100%; }
.img-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.15s;
}
.img-zoomable:hover .img-actions { opacity: 1; }
.img-action-btn {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.55);
    color: white;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}
.img-action-btn:hover { background: rgba(0, 0, 0, 0.8); }

/* --- Lightbox --- */
.lightbox {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}
.lightbox-open { opacity: 1; pointer-events: auto; }
.lightbox-toolbar {
    position: absolute;
    top: 16px;
    right: 20px;
    display: flex;
    gap: 8px;
    z-index: 1;
}
.lightbox-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.15);
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    line-height: 1;
}
.lightbox-btn:hover { background: rgba(255, 255, 255, 0.3); }
.lightbox-img {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    cursor: zoom-in;
    transition: transform 0.1s ease-out;
    user-select: none;
}

/* --- Responsive --- */
@media (max-width: 720px) {
    .recolor-row { flex-direction: column; }
    .recolor-divider { width: 100%; height: 1px; margin: 0; }
    .recolor-single, .recolor-palette { min-width: 0; }
    .detail-header { flex-wrap: wrap; }
    .gallery-hero-actions { flex-wrap: wrap; }
}
