﻿@import url('https://fonts.googleapis.com/css2?family=Prompt:wght@300;400;500;600;700&display=swap');

:root {
    --bg: #111;
    --panel: rgba(0, 0, 0, .45);
    --shadow: 0 18px 60px rgba(0, 0, 0, .55);
    --btn: #ffffffee;
    --btnText: #111;
    --navH: 54px;
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    /* บังคับซ่อนส่วนที่ล้นแนวนอนทั้งหมดเด็ดขาด */
}

body {
    margin: 0;
    font-family: 'Prompt', system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--bg);
    color: #fff;
}

img {
    user-select: none;
}

.topnav {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--navH);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    z-index: 20;
    background: rgba(0, 0, 0, .55);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, .08);
}

.topnav .brand {
    color: #fff;
    text-decoration: none;
    font-weight: 700;
}

.topnav .links {
    display: flex;
    gap: 8px;
}

.topnav .links a {
    color: #fff;
    text-decoration: none;
    font-size: 13px;
    padding: 8px 10px;
    border-radius: 999px;
    background: rgba(255, 255, 255, .08);
    border: 1px solid rgba(255, 255, 255, .10);
}

.topnav .links a.active {
    background: rgba(255, 255, 255, .92);
    color: #111;
    border-color: transparent;
}

button {
    border: 0;
    border-radius: 10px;
    padding: 10px 12px;
    background: var(--btn);
    color: var(--btnText);
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 8px 22px rgba(0, 0, 0, .18);
    transition: transform .08s ease, opacity .15s ease;
}

button:active {
    transform: scale(.98);
}

button.soft {
    background: rgba(255, 255, 255, .14);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, .18);
    box-shadow: none;
}

.productWrap {
    padding-top: calc(var(--navH) + 12px);
    padding-bottom: 24px;
    padding-left: 12px;
    padding-right: 12px;
    /* เปลี่ยนจาก min-height เป็นกำหนดตายตัวไม่ให้หน้าเว็บเลื่อนได้ */
    height: 100vh;
    width: 100%;
    /* บังคับกว้าง 100vw */
    max-width: 100%;
    /* กัน scrollbar ยืด */
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: hidden;
    /* ไม่ให้ scroll นอกกรอบ ทำให้ตัว board ไม่หายไปตอนเลือกของ */
}

.productTop {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 12px;
    padding: 0 0 4px 0;
    /* productWrap มี padding แล้ว ไม่ต้องใส่ตรงนี้อีกบน desktop */
}

.productTitle h1 {
    margin: 0;
    font-size: 18px;
    line-height: 1.2;
}

.productTitle p {
    margin: 6px 0 0;
    color: rgba(255, 255, 255, .75);
    font-size: 12.5px;
}

.productActions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.pill {
    color: #fff;
    background: var(--panel);
    padding: 8px 10px;
    border-radius: 999px;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.board {
    position: relative;
    /* ไม่ต้องมี margin แนวนอนเพราะ productWrap มี padding แล้ว */
    margin: 0;
    min-width: 0;
    height: min(52vh, 420px);
    background: rgba(255, 255, 255, .06);
    border: 1px solid rgba(255, 255, 255, .10);
    border-radius: 18px;
    box-shadow: 0 18px 60px rgba(0, 0, 0, .35);
    overflow: hidden;
    flex-shrink: 0;
}

.board.dragOver {
    outline: 2px dashed rgba(255, 255, 255, .55);
    outline-offset: -10px;
}

.boardHint {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    text-align: center;
    color: rgba(255, 255, 255, .85);
    padding: 18px;
    font-size: 14px;
}

.boardHint small {
    display: block;
    margin-top: 8px;
    color: rgba(255, 255, 255, .65);
    font-size: 12px;
}

.neckline {
    position: absolute;
    left: 6%;
    right: 6%;
    top: 52%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .30), transparent);
    opacity: .85;
}

.piece {
    position: absolute;
    width: 56px;
    height: 56px;
    display: grid;
    place-items: center;
    cursor: grab;
    user-select: none;
    touch-action: none;
}

.piece.active {
    cursor: grabbing;
    transform: scale(1.02);
}

.piece img {
    width: 46px;
    height: 46px;
    object-fit: cover;
    border-radius: 12px;
    pointer-events: auto;
}

.tray {
    margin: 12px 12px 0;
    background: rgba(0, 0, 0, .28);
    border: 1px solid rgba(255, 255, 255, .10);
    border-radius: 18px;
    overflow: hidden;
}

.trayContainer {
    flex: 1;
    min-height: 0;
    min-width: 0;
    /* หัวใจสำคัญไม่ให้ flex child ถ่างจอ */
    width: 100%;
    /* บังคับ width */
    overflow-y: auto;
    overflow-x: hidden;
    /* ไม่ให้กว้างออกไปมี scrollbar แบบ 100% แน่นอน */
    overscroll-behavior-y: contain;
    padding-bottom: 20px;
}

.trayContainer::-webkit-scrollbar {
    width: 6px;
}

.trayContainer::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, .15);
    border-radius: 10px;
}

/* ── charm section (กล่องแต่ละหมวด) ── */
.charmSection {
    /* ใช้ margin จัดระยะระหว่างกล่องแทน padding เพื่อให้พื้นที่ scroll สะอาด */
    margin: 8px 0 0;
    min-width: 0;
    box-sizing: border-box;
    background: rgba(0, 0, 0, .28);
    border: 1px solid rgba(255, 255, 255, .10);
    border-radius: 18px;
    overflow: hidden;
    flex-shrink: 0;
}

.charmSectionHead {
    padding: 10px 12px 6px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.charmSectionLabel {
    font-size: 12.5px;
    color: rgba(255, 255, 255, .70);
    font-weight: 500;
    letter-spacing: .02em;
}

/* ── Letter Filter bar ── */
.letterFilter {
    display: flex;
    flex-wrap: wrap;
    /* บังคับให้ปุ่มที่เกินขอบปัดขึ้นบรรทัดใหม่ */
    gap: 5px;
}

.letterBtn {
    flex: 0 0 auto;
    min-width: 34px;
    height: 30px;
    padding: 0 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    background: rgba(255, 255, 255, .08);
    color: rgba(255, 255, 255, .75);
    border: 1px solid rgba(255, 255, 255, .10);
    box-shadow: none;
    cursor: pointer;
    transition: background .12s, color .12s;
}

.letterBtn.active {
    background: rgba(255, 255, 255, .9);
    color: #111;
    border-color: transparent;
}


.trayList {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 8px;
    padding: 10px;
    width: 100%;
    /* บังคับไม่ให้ grid ถ่างเกินกรอบพ่อ */
    box-sizing: border-box;
}

img.charm {
    width: 100%;
    /* เต็ม cell */
    aspect-ratio: 1 / 1;
    /* คงอัตราส่วน square */
    object-fit: contain;
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    cursor: grab;
}

img.charm:active {
    cursor: grabbing;
}

/* baseList อยู่ใน charmSection แล้ว ลบ baseBar ออกได้ */
.baseList {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding: 6px 10px 10px;
    flex: 1;
}

.baseList::-webkit-scrollbar {
    height: 8px;
}

.baseList::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, .18);
    border-radius: 999px;
}

.baseItem {
    flex: 0 0 auto;
    width: 84px;
    height: 54px;
    border-radius: 14px;
    background: rgba(255, 255, 255, .06);
    border: 1px solid rgba(255, 255, 255, .12);
    display: grid;
    place-items: center;
    cursor: pointer;
}

.baseItem img {
    width: 74px;
    height: 44px;
    object-fit: contain;
    pointer-events: auto;
}

.baseItem.active {
    background: rgba(255, 255, 255, .92);
    border-color: transparent;
}

.baseItem.active img {
    filter: none;
}

.baseLayer {
    position: absolute;
    inset: 0;
    pointer-events: auto;
    z-index: 1;
}

.piece {
    z-index: 3;
}

.basePiece {
    position: absolute;
    cursor: grab;
    touch-action: none;
    user-select: none;
}

.basePiece.active {
    cursor: grabbing;
}

.basePiece img {
    width: min(300px, 70%);
    height: auto;
    object-fit: contain;
    pointer-events: auto;
}

#trayList,
.board,
.piece,
.basePiece,
img.charm {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.dragGhost {
    position: fixed;
    width: 52px;
    height: 52px;
    object-fit: contain;
    pointer-events: auto;
    z-index: 9999;
    opacity: .95;
    transform: translateZ(0);
}

.transformUI {
    position: absolute;
    display: flex;
    gap: 6px;
    padding: 6px;
    background: rgba(0, 0, 0, .6);
    border-radius: 12px;
    z-index: 50;
}

.transformUI.hidden {
    display: none;
}

.transformUI button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    padding: 0;
    font-size: 16px;
}

/* @media (max-width: 520px) {
    .productTop {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .productActions {
        width: 100%;
        justify-content: flex-start;
    }

    button {
        padding: 9px 10px;
        font-size: 13px;
    }

    .board {
        height: 260px;
    }

    img.charm {
        width: 56px;
        height: 56px;
    }

    .baseItem {
        width: 72px;
        height: 48px;
    }

    .baseItem img {
        width: 62px;
        height: 38px;
    }

        .productWrap {
            padding-bottom: 10px;
        }
    
        .trayList {
            padding: 8px;
            gap: 8px;
        }
    
        .baseList {
            padding: 4px 2px;
            gap: 8px;
        }
} */

.board,
.baseLayer,
.piece,
.basePiece {
    touch-action: none;
    /* สำคัญมากสำหรับ pinch/rotate บนมือถือ */
}

html.lockScroll,
body.lockScroll {
    height: 100%;
    overflow: hidden !important;
}

body {
    overscroll-behavior-y: none;
}

@media (hover: none) and (pointer: coarse) {

    html,
    body {
        height: 100%;
        /* ไม่ lock overflow เพื่อให้ scroll ระหว่าง section ได้ */
        overscroll-behavior: none;
    }

    .productWrap {
        height: 100dvh;
        /* บังคับให้มือถือเป็น 100dvh พอดีจอ ไม่มี scroll เลื่อนทั้งหน้า */
        min-height: 0;
        padding-top: calc(var(--navH) + 8px);
        padding-bottom: 16px;
        padding-left: 12px;
        padding-right: 12px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        overflow: hidden;
        /* ป้องกันหน้าจอเด้ง */
    }

    .productTop {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        padding: 10px 0 0;
        /* ไม่ต้อง padding แนวนอน เพราะ productWrap ทำแล้ว */
    }

    .productActions {
        width: 100%;
        justify-content: flex-start;
        gap: 8px;
    }

    button {
        padding: 9px 10px;
        font-size: 13px;
    }

    .board {
        /* ใช้ padding ของ productWrap แทน margin ตัวเอง */
        margin: 0;
        height: 160px;
        flex: 0 0 auto;
    }

    .charmSection {
        flex: 0 0 auto;
        margin: 0;
        /* ไม่ต้อง width: 100% เพราะ flex column stretch จัดให้เอง */
        min-width: 0;
        border-radius: 14px;
    }

    /* trayList ใช้ grid แต่ละหมวดแล้ว ไม่ต้อง scroll แนวนอน */
    .trayList {
        touch-action: auto;
        padding: 8px;
        gap: 8px;
        /* เรียง 6 คอลัมน์ต่อแถว A B C D E F แล้ว G ขึ้นบรรทัดใหม่ตามที่สั่ง */
        grid-template-columns: repeat(6, 1fr);
        width: 100%;
        box-sizing: border-box;
    }

    /* img.charm ใช้ width: 100% จาก global แล้ว ไม่ต้อง override */
    img.charm {
        width: 100%;
        height: auto;
        touch-action: auto;
        /* ให้ grid scroll แนวตั้งได้ก่อน longpress */
    }

    /* piece/basePiece ยังต้องการ touch-action: none เพื่อ drag บน board */
    .piece,
    .basePiece {
        touch-action: none;
    }

    .baseList {
        padding: 6px 10px 8px;
        gap: 8px;
        flex-wrap: wrap;
        overflow-x: hidden;
    }

    .baseItem {
        width: 72px;
        height: 48px;
    }

    .baseItem img {
        width: 62px;
        height: 38px;
    }

    .basePiece img {
        width: min(260px, 70%);
    }
}

@media print {
    body {
        background: #fff !important;
        color: #111 !important;
    }

    .topnav,
    .productTop,
    .tray {
        display: none !important;
    }

    .board {
        height: 80vh !important;
        margin: 0 !important;
        border: 0 !important;
        box-shadow: none !important;
        background: #fff !important;
    }

    .neckline {
        opacity: .25 !important;
    }
}