:root {
    --bg: #ffffff;
    --card: #fff;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --success: #16a34a;
    --danger: #ef4444;
    --chip: #eef2ff;
    --shadow: 0 6px 20px rgba(0, 0, 0, .06);
    --radius: 16px;
    --radius-sm: 12px;
    --skeleton: #e5e7eb;
    --skeleton-shine: linear-gradient(90deg, rgba(229, 231, 235, 0) 0%, rgba(229, 231, 235, .9) 50%, rgba(229, 231, 235, 0) 100%);
}

* {
    box-sizing: border-box
}

html,
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Noto Sans Thai", system-ui, -apple-system, Segoe UI, Roboto;
    line-height: 1.5
}

.container {
    max-width: 1200px;
    margin: 32px auto;
    padding: 0 20px
}

.header {
    margin-bottom: 18px
}

.header h1 {
    margin: 0 0 6px;
    font-size: 28px
}

.header p {
    margin: 0;
    color: var(--muted)
}

.controls {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
    margin: 16px 0 22px
}

.search-box {
    flex: 1;
    min-width: 220px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card);
    padding: 10px 14px;
    border-radius: 12px;
    box-shadow: var(--shadow)
}

.search-box input {
    border: 0;
    outline: none;
    width: 100%;
    font-size: 14px;
    background: transparent
}

.filter-group {
    display: flex;
    gap: 10px
}

.filter-group select {
    appearance: none;
    background: var(--card);
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 10px 14px;
    font-size: 14px;
    box-shadow: var(--shadow)
}

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 18px
}

@media (max-width:1100px) {
    .grid {
        grid-template-columns: repeat(3, 1fr)
    }
}

@media (max-width:820px) {
    .grid {
        grid-template-columns: repeat(2, 1fr)
    }
}

@media (max-width:520px) {
    .grid {
        grid-template-columns: 1fr
    }
}

.product {
    background: var(--card);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease
}

.product:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 28px rgba(0, 0, 0, .08)
}

.badge {
    position: absolute;
    margin: 12px 0 0 12px;
    z-index: 2;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    color: #fff
}

.badge.new {
    background: #2563eb
}

.badge.available {
    background: #059669
}

.badge.sold {
    background: #ef4444
}

.img-wrapper {
    position: relative;
    aspect-ratio: 4/3;
    background: linear-gradient(180deg, #f8fafc, #eef2f7)
}

.product-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    opacity: 0;
    transition: opacity .25s ease
}

.product-image.is-loaded {
    opacity: 1
}

.product-info {
    padding: 14px
}

.product-info h3 {
    margin: 0 0 6px;
    font-size: 16px
}

.meta {
    display: flex;
    gap: 10px;
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 8px
}

.price {
    font-size: 18px;
    font-weight: 800;
    margin-bottom: 4px
}

.code {
    font-size: 12px;
    color: var(--muted)
}

/* Skeletons */
.skeleton {
    position: relative;
    overflow: hidden;
    background: var(--skeleton)
}

.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--skeleton-shine);
    transform: translateX(-100%);
    animation: shine 1.2s infinite
}

@keyframes shine {
    to {
        transform: translateX(100%)
    }
}

.skeleton-img {
    aspect-ratio: 4/3;
    border-bottom: 1px solid #e5e7eb
}

.skeleton-line {
    height: 12px;
    border-radius: 6px;
    margin: 10px 14px
}

.skeleton-price {
    height: 18px;
    width: 40%;
    margin: 10px 14px;
    border-radius: 8px
}

/* No results */
.no-products {
    background: var(--card);
    box-shadow: var(--shadow);
    border-radius: var(--radius);
    padding: 32px;
    text-align: center
}

.no-products i {
    font-size: 40px;
    color: var(--primary)
}

.no-products h3 {
    margin: 10px 0 6px
}

/* Modal - ปรับปรุงให้สวยงามยิ่งขึ้น */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .7);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.3s ease
}

.modal.open {
    display: flex;
    opacity: 1;
}

.modal-content {
    background: #fff;
    border-radius: 20px;
    max-width: 980px;
    width: 100%;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    overflow: hidden;
    transform: scale(0.9);
    transition: transform 0.3s ease
}

.modal.open .modal-content {
    transform: scale(1);
}

.modal-body {
    display: grid;
    grid-template-columns: 1fr 1fr;
    max-height: 90vh;
}

@media (max-width:860px) {
    .modal-body {
        grid-template-columns: 1fr
    }
}

.modal-image {
    position: relative;
    background: #f8fafc;
    min-height: 320px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden
}

.modal-image img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    transition: opacity 0.3s ease
}

.slider-controls {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    pointer-events: none
}

.slider-btn {
    width: 44px;
    height: 44px;
    border-radius: 999px;
    border: 0;
    background: rgba(255, 255, 255, .95);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    pointer-events: auto;
    transition: all 0.2s ease
}

.slider-btn:hover {
    background: rgba(255, 255, 255, 1);
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2)
}

.slider-btn i {
    font-size: 18px;
    color: #374151
}

.close-btn {
    position: absolute;
    top: 16px;
    right: 20px;
    cursor: pointer;
    font-size: 28px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.9);
    color: #374151;
    transition: all 0.2s ease;
    z-index: 10
}

.close-btn:hover {
    background: rgba(255, 255, 255, 1);
    transform: scale(1.1)
}

.modal-details {
    padding: 24px;
    overflow-y: auto;
    max-height: 100%
}

.modal-details h2 {
    margin: 0 0 12px;
    font-size: 24px;
    color: #1f2937
}

.detail-item {
    display: flex;
    gap: 10px;
    margin: 14px 0;
    padding-bottom: 14px;
    border-bottom: 1px solid #f3f4f6
}

.detail-label {
    width: 120px;
    color: var(--muted);
    font-weight: 500;
    flex-shrink: 0
}

.price-detail {
    font-size: 28px;
    font-weight: 800;
    margin: 12px 0 18px;
    color: #dc2626
}

.status-tag {
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
    display: inline-block
}

.status-available {
    background: #dcfce7;
    color: #166534
}

.status-sold {
    background: #fee2e2;
    color: #991b1b
}

.status-new {
    background: #e0e7ff;
    color: #3730a3
}

/* Image counter */
.image-counter {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500
}

/* Loading animation for modal image */
.modal-image-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8fafc
}

.modal-image-loading .spinner {
    width: 40px;
    height: 40px;
    border: 3px solid #e5e7eb;
    border-top: 3px solid #3b82f6;
    border-radius: 50%;
    animation: spin 1s linear infinite
}

@keyframes spin {
    0% {
        transform: rotate(0deg)
    }

    100% {
        transform: rotate(360deg)
    }
}

:root {
    --bg: #f4f6f8;
    --card: #fff;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --success: #16a34a;
    --danger: #ef4444;
    --chip: #eef2ff;
    --shadow: 0 6px 20px rgba(0, 0, 0, .06);
    --radius: 16px;
    --radius-sm: 12px;
    --skeleton: #e5e7eb;
    --skeleton-shine: linear-gradient(90deg, rgba(229, 231, 235, 0) 0%, rgba(229, 231, 235, .9) 50%, rgba(229, 231, 235, 0) 100%);
}

* {
    box-sizing: border-box
}

html,
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Noto Sans Thai", system-ui, -apple-system, Segoe UI, Roboto;
    line-height: 1.5
}

/* ให้เอกสารกินเต็มความสูง */
html,
body {
    height: 100%;
    margin: 0;
}

body {
    background: none;
    /* ปิด background เดิมที่เป็นสีพื้น */
}

body::before {
    content: "";
    position: fixed;
    /* ล็อกติด viewport */
    top: 0;
    left: 0;
    width: 100vw;
    /* ครอบเต็มกว้าง */
    height: 100vh;
    /* ครอบเต็มสูง */
    background-image: url("https://t4.ftcdn.net/jpg/02/77/27/91/360_F_277279156_3kiEemL1mqy1tRsGtI5W1HHN6VPhI91B.jpg");
    background-size: cover;
    /* ให้ภาพครอบเต็ม */
    background-position: center;
    /* จัดกึ่งกลาง */
    background-repeat: no-repeat;
    z-index: -1;
    /* อยู่หลังสุด */
    opacity: 0.2;
    /* ให้การ์ดด้านหน้าดูชัด */
}

/* ถ้าเคยใส่ background-image ไว้ที่ .container ให้ลบทิ้ง/ไม่ต้องมี */


.container {
    max-width: 1200px;
    margin: 32px auto;
    padding: 0 20px
}

.header {
    margin-bottom: 18px
}

.header h1 {
    margin: 0 0 6px;
    font-size: 28px
}

.header p {
    margin: 0;
    color: var(--muted)
}

.controls {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
    margin: 16px 0 22px
}

.search-box {
    flex: 1;
    min-width: 220px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card);
    padding: 10px 14px;
    border-radius: 12px;
    box-shadow: var(--shadow)
}

.search-box input {
    border: 0;
    outline: none;
    width: 100%;
    font-size: 14px;
    background: transparent
}

.filter-group {
    display: flex;
    gap: 10px
}

.filter-group select {
    appearance: none;
    background: var(--card);
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 10px 14px;
    font-size: 14px;
    box-shadow: var(--shadow)
}

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 18px
}

@media (max-width:1100px) {
    .grid {
        grid-template-columns: repeat(3, 1fr)
    }
}

@media (max-width:820px) {
    .grid {
        grid-template-columns: repeat(2, 1fr)
    }
}

@media (max-width:520px) {
    .grid {
        grid-template-columns: 1fr
    }
}

/* ==== การ์ดสินค้า + เอฟเฟกต์พลิก (เพิ่มใหม่) ==== */
.product {
    background: transparent;
    /* เราจะมี front/back แยกเอง */
    border-radius: var(--radius);
    perspective: 1200px;
    cursor: pointer;
}

.flip-card {
    position: relative;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    transition: transform .6s cubic-bezier(.2, .6, .2, 1);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.product:hover .flip-card {
    transform: rotateY(180deg);
}

.flip-side {
    position: absolute;
    inset: 0;
    background: var(--card);
    border-radius: var(--radius);
    backface-visibility: hidden;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.flip-front {
    transform: rotateY(0deg);
}

.flip-back {
    transform: rotateY(180deg);
    padding: 14px;
    gap: 10px;
}

.badge {
    position: absolute;
    margin: 12px 0 0 12px;
    z-index: 2;
    padding: 6px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    color: #fff
}

.badge.new {
    background: #2563eb
}

.badge.available {
    background: #059669
}

.badge.sold {
    background: #ef4444
}

.img-wrapper {
    position: relative;
    aspect-ratio: 4/3;
    background: linear-gradient(180deg, #f8fafc, #eef2f7)
}

.product-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    opacity: 0;
    transition: opacity .25s ease
}

.product-image.is-loaded {
    opacity: 1
}

.product-info {
    padding: 14px
}

.product-info h3 {
    margin: 0 0 6px;
    font-size: 16px
}

.meta {
    display: flex;
    gap: 10px;
    color: var(--muted);
    font-size: 13px;
    margin-bottom: 8px
}

.price {
    font-size: 18px;
    font-weight: 800;
    margin-bottom: 4px
}

.code {
    font-size: 12px;
    color: var(--muted)
}

/* ด้านหลังการ์ด */
.flip-back h3 {
    margin: 0 0 6px;
    font-size: 16px
}

.flip-back .kv {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: var(--muted)
}

.flip-back .kv span:last-child {
    color: var(--text)
}

.flip-back .price {
    font-size: 18px;
    font-weight: 800;
    margin-top: 4px
}

.status-tag {
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700
}

.status-available {
    background: #dcfce7;
    color: #166534
}

.status-sold {
    background: #fee2e2;
    color: #991b1b
}

.status-new {
    background: #e0e7ff;
    color: #3730a3
}

.flip-back .more {
    margin-top: auto;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.35;
    max-height: 3.7em;
    overflow: hidden
}

/* ==== Skeletons ==== */
.skeleton {
    position: relative;
    overflow: hidden;
    background: var(--skeleton)
}

.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--skeleton-shine);
    transform: translateX(-100%);
    animation: shine 1.2s infinite
}

@keyframes shine {
    to {
        transform: translateX(100%)
    }
}

.skeleton-img {
    aspect-ratio: 4/3;
    border-bottom: 1px solid #e5e7eb;
    border-radius: var(--radius) var(--radius) 0 0
}

.skeleton-line {
    height: 12px;
    border-radius: 6px;
    margin: 10px 14px;
    background: var(--skeleton)
}

.skeleton-price {
    height: 18px;
    width: 40%;
    margin: 10px 14px;
    border-radius: 8px;
    background: var(--skeleton)
}

/* No results */
.no-products {
    background: var(--card);
    box-shadow: var(--shadow);
    border-radius: var(--radius);
    padding: 32px;
    text-align: center
}

.no-products i {
    font-size: 40px;
    color: var(--primary)
}

.no-products h3 {
    margin: 10px 0 6px
}

/* ==== Modal ==== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 40
}

.modal.open {
    display: flex
}

.modal-content {
    background: #fff;
    border-radius: 20px;
    max-width: 980px;
    width: 100%;
    box-shadow: var(--shadow);
    overflow: hidden
}

.modal-body {
    display: grid;
    grid-template-columns: 1fr 1fr
}

@media (max-width:860px) {
    .modal-body {
        grid-template-columns: 1fr
    }
}

.modal-image {
    position: relative;
    background: #111;
    min-height: 320px
}

.modal-image img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #111;
    transition: opacity .2s ease
}

.slider-controls {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 6px
}

.slider-btn {
    width: 40px;
    height: 40px;
    border-radius: 999px;
    border: 0;
    background: rgba(255, 255, 255, .85);
    cursor: pointer
}

.close-btn {
    position: absolute;
    top: 10px;
    right: 14px;
    cursor: pointer;
    font-size: 26px
}

.modal-details {
    padding: 16px 18px
}

.modal-details h2 {
    margin: 0 0 6px
}

.detail-item {
    display: flex;
    gap: 10px;
    margin: 6px 0
}

.detail-label {
    width: 110px;
    color: var(--muted)
}

.price-detail {
    font-size: 22px;
    font-weight: 800;
    margin: 6px 0 10px
}

/* Loading overlay ใน modal */
.modal-image-loading {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, .2);
    z-index: 2
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid rgba(255, 255, 255, .6);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 1s linear infinite
}

@keyframes spin {
    to {
        transform: rotate(360deg)
    }
}

.image-counter {
    position: absolute;
    right: 10px;
    bottom: 8px;
    z-index: 3;
    background: rgba(0, 0, 0, .6);
    color: #fff;
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 999px
}

/* กันไว้ก่อนระหว่างที่ยังไม่คำนวนความสูงด้วย JS */
.flip-card {
    position: relative;
    perspective: 1000px;
    min-height: 520px;
    /* << กันพื้นที่ขั้นต่ำ ไม่ให้ชื่อซ้อนรูป */
    will-change: transform, height;
}

.flip-side {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
}

.flip-front {
    transform: rotateY(0deg);
}

.flip-back {
    transform: rotateY(180deg);
    overflow: auto;
}

.product .img-wrapper {
    aspect-ratio: 4 / 5;
    /* สัดส่วนภาพคงที่ ป้องกันโดด */
    background: linear-gradient(180deg, #f8fafc, #eef2f7);
}

.product .product-info {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 12px 14px;
    min-height: 120px;
    /* กันพื้นที่ข้อความ + ราคา */
}

/* ชื่อ: อนุญาต 2-3 บรรทัด แล้วตัด … */
.product .product-info h3 {
    margin: 0;
    font-size: 16px;
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    /* << สูงสุด 3 บรรทัด */
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: calc(1.35em * 2);
    /* อย่างน้อย 2 บรรทัดเพื่อไม่โดนบีบ */
}

.product .product-info .meta {
    font-size: 13px;
    color: var(--muted, #6b7280);
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

.product .product-info .code {
    font-size: 12px;
    color: var(--muted, #6b7280);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ราคาอยู่ล่างเสมอ */
.product .product-info .price {
    margin-top: auto;
    font-size: 18px;
    font-weight: 800;
    white-space: nowrap;
}