/**
 * MV Studio — Structured Prompt Builder (v2.72)
 * [iimate_mv_prompt_studio] / mv-prompt-builder.js
 *
 * สี category 10 ตัวแรกใช้ hue เดียวกับ .iimate-mv-cat-* ใน
 * mv-studio.css เป๊ะ (แก้สีต้องแก้ทั้งสองไฟล์ให้ตรงกัน — ตั้งใจ
 * ไม่ import ข้ามไฟล์เพื่อให้หน้านี้ standalone) — 3 ตัวใหม่:
 * subject (near-white — เป็นตัวหลักของภาพ อ่านเด่นสุด),
 * face (violet), framing (cyan-teal)
 */

.iimate-ps {
    max-width: 1440px;
    margin: 0 auto;
    padding: 24px 16px 64px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI',
        Roboto, 'Helvetica Neue', sans-serif;
    color: #e6e8ee;
    box-sizing: border-box;
}

.iimate-ps *,
.iimate-ps *::before,
.iimate-ps *::after {
    box-sizing: border-box;
}

.iimate-ps-title {
    margin: 0 0 4px;
    color: #ffffff;
    font-size: 18px;
    font-weight: 700;
}

.iimate-ps-subtitle {
    margin: 0 0 12px;
    color: #98a0b3;
    font-size: 13px;
}

/* ---------- top box ---------- */

.iimate-ps-topbox {
    background: #141a26;
    border: 1px solid #232c3d;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 24px;
}

.iimate-ps-source {
    width: 100%;
    background: #0e1420;
    color: #e6e8ee;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 12px;
    font-size: 14px;
    resize: vertical;
    margin-bottom: 12px;
}

.iimate-ps-toprow {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.iimate-ps-toprow .iimate-ps-btn {
    flex: 1 1 240px;
}

/* ---------- v2.74.x: panels (Text Analysis / Image Analysis) ---------- */

.iimate-ps-panels-row {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    margin-top: 4px;
}

.iimate-ps-panel {
    flex: 1 1 280px;
    min-width: 260px;
    background: #171f31;
    border: 1px solid #2a3550;
    border-radius: 10px;
    padding: 14px;
}

.iimate-ps-panel-title {
    font-size: 13px;
    font-weight: 700;
    color: #98a0b3;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 10px;
}

.iimate-ps-panel .iimate-ps-model-select {
    width: 100%;
    max-width: none;
}

.iimate-ps-panel .iimate-ps-model-row {
    flex-wrap: nowrap;
}

/* ---------- v2.74.x: progress bar ---------- */

.iimate-ps-progress-wrap {
    margin-top: 14px;
}

.iimate-ps-progress-bar {
    height: 6px;
    border-radius: 999px;
    background: #1a2338;
    overflow: hidden;
}

.iimate-ps-progress-fill {
    height: 100%;
    width: 40%;
    border-radius: 999px;
    background: linear-gradient(90deg, #14857a, #7c3aed);
    animation: iimate-ps-progress-slide 1.2s ease-in-out infinite;
}

@keyframes iimate-ps-progress-slide {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(350%); }
}

.iimate-ps-progress-label {
    margin-top: 6px;
    font-size: 12px;
    color: #98a0b3;
    text-align: center;
}

/* ---------- buttons ---------- */

.iimate-ps-btn {
    background: #2b3650;
    color: #e6e8ee;
    border: none;
    border-radius: 8px;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.15s ease;
}

.iimate-ps-btn:hover:not(:disabled) {
    filter: brightness(1.15);
}

.iimate-ps-btn:disabled {
    opacity: 0.55;
    cursor: default;
}

.iimate-ps-btn-small {
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 500;
}

.iimate-ps-btn-full {
    width: 100%;
    display: block;
}

.iimate-ps-btn-teal {
    background: #14857a;
    color: #ffffff;
}

.iimate-ps-btn-purple {
    background: #7c3aed;
    color: #ffffff;
}

.iimate-ps-btn-orange {
    background: #f5a623;
    color: #1a1a1a;
}

.iimate-ps-plus-notice {
    margin-top: 10px;
    padding: 8px 12px;
    border-radius: 6px;
    background: rgba(245, 166, 35, 0.12);
    border: 1px solid rgba(245, 166, 35, 0.35);
    color: #f5a623;
    font-size: 13px;
}

.iimate-ps-notice-link {
    color: #f5a623;
    font-weight: 600;
    text-decoration: underline;
    white-space: nowrap;
}

.iimate-ps-notice-link:hover {
    color: #ffc266;
}

/* ---------- status ---------- */

.iimate-ps-status {
    display: none;
    margin-top: 10px;
    font-size: 13px;
    color: #98a0b3;
}

.iimate-ps-status.is-visible {
    display: block;
}

.iimate-ps-status.is-error {
    color: #ff7b7b;
}

/* ---------- 2-column grid ---------- */

.iimate-ps-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 24px;
    align-items: start;
}

@media (max-width: 960px) {
    .iimate-ps-grid {
        grid-template-columns: minmax(0, 1fr);
    }
}

.iimate-ps-col-left {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 0;
}

.iimate-ps-col-right {
    position: sticky;
    top: 24px;
    min-width: 0;
}

/* ---------- section cards ---------- */

.iimate-ps-card {
    background: #141a26;
    border: 1px solid #232c3d;
    border-left-width: 4px;
    border-radius: 10px;
    padding: 16px;
    transition: background 0.2s ease, border-color 0.2s ease;
}

.iimate-ps-card.is-locked {
    background: #1a1a14;
    border-color: #4a4324;
}

.iimate-ps-lock-btn {
    background: none;
    border: none;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    padding: 2px 4px;
    flex-shrink: 0;
    opacity: 0.6;
    transition: opacity 0.15s ease;
}

.iimate-ps-lock-btn:hover {
    opacity: 1;
}

.iimate-ps-card.is-locked .iimate-ps-lock-btn {
    opacity: 1;
}

.iimate-ps-card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 4px;
}

.iimate-ps-card-label {
    font-size: 15px;
    font-weight: 700;
    /* v2.74.x — กินพื้นที่ที่เหลือ ให้ปุ่ม 📎 กับ ↻ ไปเกาะกลุ่มกัน
       ทางขวา แทนที่จะโดน space-between ดันแยกออกจากกัน */
    flex: 1;
    min-width: 0;
}

.iimate-ps-card-desc {
    margin: 0 0 10px;
    color: #7d8598;
    font-size: 12px;
}

.iimate-ps-hint-label {
    display: block;
    color: #7d8598;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 4px;
}

.iimate-ps-hint {
    width: 100%;
    background: #1a2233;
    color: #cdd3e0;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 13px;
    margin-bottom: 8px;
}

.iimate-ps-card-text {
    width: 100%;
    background: #0e1420;
    color: #e6e8ee;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 10px;
    font-size: 14px;
    resize: vertical;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.iimate-ps-card-text.is-flash {
    border-color: #4caf7d;
}

.iimate-ps-card-text.is-error {
    border-color: #ff7b7b;
}

.iimate-ps-card-count {
    margin-top: 6px;
    text-align: right;
    font-size: 11px;
    color: #5d6578;
}

.iimate-ps-card-count.is-near {
    color: #f5a623;
}

.iimate-ps-card-count.is-over {
    color: #ff7b7b;
    font-weight: 600;
}

.iimate-ps-ref-thumb {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    padding: 6px 8px;
    background: #0e1420;
    border: 1px solid #2a3550;
    border-radius: 8px;
}

.iimate-ps-ref-thumb img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
}

.iimate-ps-ref-thumb-label {
    color: #7d8598;
    font-size: 11px;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.iimate-ps-ref-thumb-remove {
    background: none;
    border: none;
    color: #7d8598;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    padding: 2px 6px;
    flex-shrink: 0;
}

.iimate-ps-ref-thumb-remove:hover {
    color: #ff7b7b;
}

/* v2.75 — [Item extra] แถบ thumbnail หลายรูปของการ์ด Accessories —
   flex-wrap เพราะจำนวนชิ้นไม่จำกัด ต่างจาก refThumb ที่มีรูปเดียวเสมอ */
.iimate-ps-accessory-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.iimate-ps-accessory-item {
    position: relative;
    width: 48px;
    height: 48px;
    flex-shrink: 0;
}

.iimate-ps-accessory-item img {
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: 6px;
    border: 1px solid #2a3550;
    display: block;
}

.iimate-ps-accessory-item-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 18px;
    height: 18px;
    line-height: 16px;
    text-align: center;
    padding: 0;
    background: #1a2233;
    border: 1px solid #2a3550;
    border-radius: 50%;
    color: #7d8598;
    font-size: 13px;
    cursor: pointer;
}

.iimate-ps-accessory-item-remove:hover {
    color: #ff7b7b;
    border-color: #ff7b7b;
}

.iimate-ps-ref-thumb-model {
    display: block;
    width: 100%;
    margin-top: 4px;
    font-size: 10px;
    color: #4caf7d;
}

.iimate-ps-compose-model {
    margin-top: 8px;
    font-size: 12px;
    color: #4caf7d;
    text-align: center;
}

.iimate-ps-model-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.iimate-ps-model-label {
    font-size: 12px;
    color: #98a0b3;
}

.iimate-ps-model-select {
    max-width: 320px;
}

.iimate-ps-model-used {
    font-size: 12px;
    color: #4caf7d;
}

/* ---------- final prompt panel ---------- */

.iimate-ps-final-panel {
    background: #141a26;
    border: 1px solid #232c3d;
    border-radius: 12px;
    padding: 20px;
}

.iimate-ps-final-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.iimate-ps-final-box {
    background: #0e1420;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 14px;
    margin-top: 12px;
    min-height: 120px;
    font-size: 14px;
    line-height: 1.7;
}

.iimate-ps-final-placeholder {
    color: #5d6578;
    font-size: 13px;
}

.iimate-ps-final-placeholder.is-error {
    color: #ff7b7b;
}

.iimate-ps-word-badge {
    display: none;
    margin-top: 8px;
    font-size: 12px;
    color: #7d8598;
}

.iimate-ps-word-badge.is-visible {
    display: block;
}

.iimate-ps-word-badge.is-warn {
    color: #f5a623;
}

/* ---------- generate controls ---------- */

.iimate-ps-gen-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
    margin: 16px 0 12px;
}

.iimate-ps-select {
    background: #1a2233;
    color: #e6e8ee;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 13px;
}

.iimate-ps-count-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
}

.iimate-ps-count-label {
    color: #7d8598;
    font-size: 13px;
}

.iimate-ps-count-btn {
    background: #1a2233;
    color: #cdd3e0;
    border: 1px solid #2a3550;
    border-radius: 6px;
    width: 34px;
    height: 34px;
    font-size: 13px;
    cursor: pointer;
}

.iimate-ps-count-btn.is-active {
    background: #f5a623;
    color: #1a1a1a;
    border-color: #f5a623;
    font-weight: 700;
}

/* ---------- results ---------- */

.iimate-ps-results {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
    margin-top: 14px;
}

.iimate-ps-result-item img {
    width: 100%;
    border-radius: 8px;
    display: block;
    margin-bottom: 6px;
    cursor: pointer;
    transition: opacity 0.15s ease;
}

.iimate-ps-result-item img:hover {
    opacity: 0.85;
}

.iimate-ps-result-item .iimate-ps-btn {
    width: 100%;
    display: block;
    text-align: center;
    text-decoration: none;
    box-sizing: border-box;
    margin-top: 6px;
}

/* ---------- v2.74.x: image lightbox ---------- */

.iimate-ps-lightbox-overlay {
    position: fixed;
    inset: 0;
    background: rgba(5, 8, 14, 0.9);
    z-index: 100000;
    align-items: center;
    justify-content: center;
    padding: 40px;
}

.iimate-ps-lightbox-img {
    max-width: 100%;
    max-height: 100%;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

.iimate-ps-lightbox-close {
    position: fixed;
    top: 16px;
    right: 24px;
    background: rgba(255, 255, 255, 0.08);
    border: none;
    color: #e6e8ee;
    font-size: 28px;
    line-height: 1;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
}

.iimate-ps-lightbox-close:hover {
    background: rgba(255, 255, 255, 0.18);
}

/* ---------- category colors ----------
 * text (ตัวอักษรใน final prompt) + แถบซ้ายการ์ด
 * hue เดิม 10 ตัว = ตรงกับ mv-studio.css .iimate-mv-cat-*
 */

.iimate-ps-text-subject { color: hsl(210, 20%, 92%); }
.iimate-ps-text-face { color: hsl(265, 75%, 78%); }
.iimate-ps-text-framing { color: hsl(180, 65%, 60%); }
.iimate-ps-text-lighting { color: hsl(50, 90%, 65%); }
.iimate-ps-text-camera { color: hsl(240, 75%, 78%); }
.iimate-ps-text-mood { color: hsl(300, 70%, 72%); }
.iimate-ps-text-pose { color: hsl(160, 55%, 62%); }
.iimate-ps-text-style { color: hsl(340, 75%, 72%); }
.iimate-ps-text-texture { color: hsl(90, 50%, 65%); }
.iimate-ps-text-background { color: hsl(0, 65%, 68%); }
.iimate-ps-text-outfit { color: hsl(30, 85%, 65%); }
.iimate-ps-text-composition { color: hsl(200, 75%, 70%); }
.iimate-ps-text-colorgrading { color: hsl(280, 60%, 74%); }

.iimate-ps-cat-subject { border-left-color: hsl(210, 20%, 92%); }
.iimate-ps-cat-face { border-left-color: hsl(265, 75%, 78%); }
.iimate-ps-cat-framing { border-left-color: hsl(180, 65%, 60%); }
.iimate-ps-cat-lighting { border-left-color: hsl(50, 90%, 65%); }
.iimate-ps-cat-camera { border-left-color: hsl(240, 75%, 78%); }
.iimate-ps-cat-mood { border-left-color: hsl(300, 70%, 72%); }
.iimate-ps-cat-pose { border-left-color: hsl(160, 55%, 62%); }
.iimate-ps-cat-style { border-left-color: hsl(340, 75%, 72%); }
.iimate-ps-cat-texture { border-left-color: hsl(90, 50%, 65%); }
.iimate-ps-cat-background { border-left-color: hsl(0, 65%, 68%); }
.iimate-ps-cat-outfit { border-left-color: hsl(30, 85%, 65%); }
.iimate-ps-cat-composition { border-left-color: hsl(200, 75%, 70%); }
.iimate-ps-cat-colorgrading { border-left-color: hsl(280, 60%, 74%); }

/* ---------- v2.72 additions ---------- */

/* preview รูปที่เลือกตอน Upload Image to Analyze (v2.72.6) */
.iimate-ps-preview {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 12px;
}

.iimate-ps-preview img {
    width: 96px;
    height: 96px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid #2a3550;
}

.iimate-ps-preview-label {
    color: #7d8598;
    font-size: 12px;
}

/* Save Prompt row (v2.72.7) */
.iimate-ps-saveprompt-row {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid #232c3d;
}

.iimate-ps-saveprompt-row .iimate-ps-hint {
    flex: 1 1 180px;
    margin-bottom: 0;
}

/* list sections (v2.72.8-9) */
.iimate-ps-list-section {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid #232c3d;
}

.iimate-ps-list-empty {
    color: #5d6578;
    font-size: 13px;
    padding: 8px 0;
}

.iimate-ps-saved-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 320px;
    overflow-y: auto;
}

.iimate-ps-saved-card {
    background: #0e1420;
    border: 1px solid #2a3550;
    border-radius: 8px;
    padding: 10px 12px;
    cursor: pointer;
    transition: border-color 0.15s ease;
}

.iimate-ps-saved-card:hover {
    border-color: #f5a623;
}

/* v2.72.11 — thumbnail + เนื้อหา เรียงข้างกัน */
.iimate-ps-saved-row {
    display: flex;
    gap: 10px;
    align-items: flex-start;
}

.iimate-ps-saved-thumb {
    width: 56px;
    height: 56px;
    object-fit: cover;
    border-radius: 6px;
    flex: 0 0 auto;
    background: #1a2233;
}

.iimate-ps-saved-thumb-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #3a4258;
    font-size: 16px;
}

.iimate-ps-saved-content {
    min-width: 0;
    flex: 1 1 auto;
}

.iimate-ps-saved-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.iimate-ps-saved-title {
    color: #e6e8ee;
    font-size: 13px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.iimate-ps-saved-status {
    color: #7d8598;
    font-size: 11px;
    white-space: nowrap;
}

.iimate-ps-saved-snippet {
    color: #7d8598;
    font-size: 12px;
    line-height: 1.5;
}

.iimate-ps-gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: 8px;
    margin-bottom: 10px;
}

.iimate-ps-gallery-thumb {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 6px;
    cursor: pointer;
    transition: transform 0.15s ease;
}

.iimate-ps-gallery-thumb:hover {
    transform: scale(1.04);
}
