#inventory-root {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 55;
}

#inventory-root.open {
    pointer-events: auto;
}

/* เว้น HUD ด้านบน — ยังเห็นเลือดตอนโดนมอนตี */
#inventory-root {
    --inv-top-clearance: calc(var(--safe-top) + clamp(92px, 15vh, 118px));
    --inv-slot-size: 46px;
    --inv-slot-gap: 5px;
    --inv-grid-max-cols: 9;
}

[data-inv-backdrop] {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: var(--inv-top-clearance);
    background: rgba(0, 0, 0, 0.62);
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

#inventory-root.open [data-inv-backdrop] {
    opacity: 1;
    pointer-events: auto;
}

[data-inv-sheet] {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: var(--inv-top-clearance);
    max-height: none;
    transform: translateY(100%);
    transition: transform 0.28s ease-out;
    display: flex;
    flex-direction: column;
    padding: 10px max(10px, env(safe-area-inset-right, 0px)) calc(10px + var(--safe-bottom))
        max(10px, env(safe-area-inset-left, 0px));
    border-radius: 12px 12px 0 0;
    pointer-events: auto;
    overflow: hidden;
    background: linear-gradient(180deg, #32281e 0%, #1a1410 45%, #100c08 100%);
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
    box-sizing: border-box;
}

#inventory-root.open [data-inv-sheet] {
    transform: translateY(0);
}

.inv-header-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 8px;
    margin-bottom: 8px;
    flex-shrink: 0;
}

.inv-header-title {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 6px 10px;
    line-height: 1.35;
}

.inv-header-label {
    font-size: var(--text-lg);
    color: var(--px-gold);
    flex-shrink: 0;
}

.inv-header-gold,
.inv-header-row [data-inv-gold-text] {
    font-size: var(--text-md);
    font-weight: bold;
    color: #ffe082;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 0 8px rgba(255, 213, 79, 0.35);
    word-break: break-all;
}

.inv-header-row .pixel-btn {
    flex-shrink: 0;
    min-height: var(--touch-min);
    font-size: var(--text-sm);
    padding: 8px 12px;
}

/* --- 3 คอลัมน์: ช่องซ้าย | พรีวิว | ช่องขวา --- */
.inv-equip-layout {
    display: grid;
    grid-template-columns: minmax(48px, 1fr) minmax(96px, 1.3fr) minmax(48px, 1fr);
    gap: clamp(4px, 1.5vw, 10px);
    align-items: stretch;
    flex-shrink: 0;
    margin-bottom: 10px;
    padding: 6px 4px;
    background: rgba(0, 0, 0, 0.35);
    border-radius: 8px;
    min-height: clamp(228px, 36vh, 280px);
}

.inv-equip-col {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    gap: var(--inv-slot-gap);
    min-width: 0;
}

.inv-equip-center {
    display: flex;
    flex-direction: column;
    gap: var(--inv-slot-gap);
    min-width: 0;
    min-height: 0;
}

.inv-equip-potions {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: clamp(8px, 3vw, 14px);
    flex-shrink: 0;
}

.potion-slot-ui {
    position: relative;
    flex-shrink: 0;
}

.inv-equip-preview {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    padding: 6px 4px;
    background: #354f35;
    border: 2px solid #2e5a2e;
    overflow: hidden;
}

.inv-preview-canvas {
    display: block;
    width: 100%;
    height: 100%;
    max-height: clamp(128px, 22vh, 168px);
    object-fit: contain;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

.equip-slot-ui {
    width: var(--inv-slot-size);
    height: var(--inv-slot-size);
    flex-shrink: 0;
    box-sizing: border-box;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    padding: 0;
    background: linear-gradient(145deg, #342a20 0%, #1a1410 100%);
    box-shadow: inset 0 1px 0 rgba(141, 110, 99, 0.2);
    cursor: pointer;
}

.equip-slot-ui img,
.inv-slot img {
    width: 72%;
    height: 72%;
    image-rendering: pixelated;
    pointer-events: none;
}

[data-inv-grid] .inv-slot {
    position: relative;
}

.inv-slot-qty {
    position: absolute;
    right: 2px;
    bottom: 2px;
    padding: 0 3px;
    min-width: 14px;
    font-size: 9px;
    font-weight: 700;
    line-height: 1.2;
    text-align: center;
    color: #fff8e8;
    background: rgba(12, 8, 6, 0.82);
    border: 1px solid rgba(201, 162, 39, 0.55);
    border-radius: 3px;
    pointer-events: none;
    text-shadow: 0 1px 0 #000;
    box-sizing: border-box;
}

.potion-slot-ui .inv-slot-qty {
    z-index: 2;
    background: #000;
    background-color: #000;
    border-color: rgba(255, 255, 255, 0.35);
}

/* มีไอเทมแล้วไม่ต้องแสดงป้ายชื่อซ้ำ — กันทับ badge จำนวน */
.equip-slot-ui.potion-slot-ui.filled::after {
    content: none;
}

.equip-slot-ui.potion-slot-ui::after {
    z-index: 1;
}

.equip-slot-ui::after {
    content: attr(data-label);
    position: absolute;
    bottom: 2px;
    left: 0;
    right: 0;
    font-size: 9px;
    line-height: 1.1;
    color: #b0a090;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}

.equip-slot-ui.empty::before {
    content: '';
    width: 40%;
    height: 40%;
    border: 2px dashed rgba(255, 255, 255, 0.12);
    border-radius: 2px;
}

.equip-slot-ui.cosmetic {
    opacity: 0.72;
    cursor: default;
}

.equip-slot-ui.filled {
    border-color: #5d4037;
}

.equip-slot-ui:active:not(.cosmetic) {
    border-color: var(--px-gold);
}

.inv-bag-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin: 0 0 6px;
    flex-shrink: 0;
    min-width: 0;
}

.inv-bag-title-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.inv-bag-heading {
    font-size: var(--text-md);
    font-weight: bold;
    color: var(--px-gold);
    flex-shrink: 0;
}

.inv-sort-btn {
    min-height: 28px;
    padding: 4px 10px;
    font-size: var(--text-xs);
    line-height: 1.2;
}

.inv-bag-meta-inline {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: clamp(6px, 2vw, 10px);
    flex: 1;
    min-width: 0;
    flex-wrap: wrap;
}

.inv-bag-meta-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: var(--text-xs);
    color: #9a8f82;
    white-space: nowrap;
}

.inv-bag-meta-pill [data-inv-slots-text],
.inv-bag-meta-pill [data-inv-weight-text] {
    font-size: var(--text-sm);
    font-weight: bold;
    color: #fff;
}

.inv-bag-weight-pill {
    gap: 5px;
    max-width: 100%;
}

.inv-bag-weight-pill [data-inv-weight-text] {
    font-variant-numeric: tabular-nums;
}

.inv-weight-mini {
    display: inline-block;
    width: clamp(28px, 10vw, 44px);
    height: 4px;
    vertical-align: middle;
    background: #1a1410;
    border: 1px solid #3e2723;
    border-radius: 2px;
    overflow: hidden;
    flex-shrink: 0;
}

.inv-weight-fill {
    display: block;
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #558b2f, #7cb342);
    transition: width 0.2s ease, background 0.2s;
}

.inv-bag-weight-pill.overload [data-inv-weight-text] {
    color: #ef9a9a;
}

.inv-bag-weight-pill.overload .inv-weight-fill {
    background: linear-gradient(90deg, #c62828, #e53935);
}

.inv-bag-weight-pill.warn .inv-weight-fill {
    background: linear-gradient(90deg, #f9a825, #ffb300);
}

.inv-bag-weight-pill.warn:not(.overload) [data-inv-weight-text] {
    color: #ffe082;
}

.inv-bag-scroll {
    flex: 1;
    min-height: 0;
    min-width: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 2px 4px 6px;
    box-sizing: border-box;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.inv-bag-scroll::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

[data-inv-grid] {
    --inv-grid-width: calc(
        var(--inv-grid-max-cols) * var(--inv-slot-size) +
            (var(--inv-grid-max-cols) - 1) * var(--inv-slot-gap)
    );
    display: grid;
    width: min(100%, var(--inv-grid-width));
    margin-inline: auto;
    grid-template-columns: repeat(auto-fill, var(--inv-slot-size));
    grid-auto-rows: var(--inv-slot-size);
    gap: var(--inv-slot-gap);
    justify-content: center;
    align-content: start;
    box-sizing: border-box;
}

[data-inv-grid] .inv-slot {
    width: var(--inv-slot-size);
    height: var(--inv-slot-size);
    justify-self: center;
    padding: 0;
    position: relative;
    background: #2a221c;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
}

[data-inv-grid] .inv-slot:not(.inv-slot-locked):not(.inv-slot-empty) {
    cursor: grab;
    touch-action: manipulation;
}

.equip-slot-ui.filled {
    cursor: grab;
    touch-action: manipulation;
}

.inv-slot-empty {
    opacity: 0.55;
    cursor: default;
    background: #221c18;
}

.inv-slot-empty::before {
    content: '';
    width: 40%;
    height: 40%;
    border: 2px dashed rgba(255, 255, 255, 0.12);
    border-radius: 2px;
}

.inv-slot-locked {
    opacity: 0.38;
    cursor: not-allowed;
    background: #181410;
    border-color: #3a3028 !important;
    box-shadow: none;
}

.inv-slot-locked::after {
    content: '🔒';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(10px, 2.8vw, 14px);
    line-height: 1;
    opacity: 0.85;
    pointer-events: none;
}

.inv-slot.equipped {
    border-color: #2ecc71;
    box-shadow: 0 0 8px rgba(46, 204, 113, 0.4);
}

.inv-slot-label {
    display: none;
}

@media (max-width: 479px) {
    .inv-equip-layout {
        min-height: clamp(168px, 30vh, 220px);
    }
}

@media (max-height: 680px) {
    #inventory-root {
        --inv-top-clearance: calc(var(--safe-top) + 86px);
    }

    .inv-equip-layout {
        min-height: 176px;
    }
}

@media (min-width: 480px) {
    #inventory-root {
        --inv-slot-size: 52px;
    }

    [data-inv-sheet] {
        padding-left: max(14px, env(safe-area-inset-left, 0px));
        padding-right: max(14px, env(safe-area-inset-right, 0px));
    }

    .equip-slot-ui::after {
        font-size: var(--text-xs);
    }
}
