/* ============================================
   MARKET WINDOW — Clan-style draggable
   ============================================ */
.market-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9998;
    pointer-events: none;
}
.market-overlay.open { display: block; }

.market-container {
    position: fixed;
    z-index: 9999;
    width: 600px;
    height: 520px;
    background: var(--color-panel);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: 0 12px 40px rgba(0,0,0,0.6);
    display: flex;
    flex-direction: column;
    pointer-events: auto;
    overflow: hidden;
    resize: both;
    min-width: 400px;
    min-height: 360px;
}

/* Header / drag handle */
.market-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: rgba(0,0,0,0.2);
    border-bottom: 1px solid var(--color-border);
    cursor: grab;
    flex-shrink: 0;
    user-select: none;
    gap: 10px;
}
.market-header:active { cursor: grabbing; }
.market-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 0;
}
.market-header-icon {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2em;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--color-border);
    flex-shrink: 0;
}
.market-header-name {
    font-size: 0.9em;
    font-weight: 700;
    color: var(--color-accent-bright);
    white-space: nowrap;
}
.market-close-btn {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: rgba(239,68,68,0.15);
    border: 1px solid rgba(239,68,68,0.3);
    color: #f87171;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.market-close-btn:hover { background: rgba(239,68,68,0.3); transform: scale(1.1); }

/* Body: sidebar + content */
.market-body {
    flex: 1;
    display: flex;
    min-height: 0;
}

/* Sidebar tabs */
.market-sidebar {
    width: 110px;
    background: rgba(0,0,0,0.15);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    padding: 6px 0;
    gap: 2px;
    flex-shrink: 0;
}
.market-tab {
    padding: 8px 12px;
    font-size: 0.78em;
    font-weight: 600;
    color: var(--color-text-dim);
    cursor: pointer;
    border: none;
    background: transparent;
    text-align: left;
    font-family: inherit;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}
.market-tab:hover { color: var(--color-text); background: rgba(255,255,255,0.03); }
.market-tab.active {
    color: var(--color-accent-bright);
    background: rgba(155,44,44,0.1);
    border-left-color: var(--color-accent);
}

/* Content area */
.market-content {
    flex: 1;
    padding: 12px;
    overflow-y: auto;
    min-width: 0;
}
.market-content::-webkit-scrollbar { width: 5px; }
.market-content::-webkit-scrollbar-track { background: transparent; }
.market-content::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

/* === Search bar === */
.market-search-bar {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}
.market-search-input {
    flex: 1;
    padding: 6px 10px;
    font-size: 0.78em;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text);
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
}
.market-search-input:focus { border-color: var(--color-accent-dim); }
.market-search-input::placeholder { color: var(--color-text-muted); }
.market-btn {
    padding: 5px 10px;
    font-size: 0.72em;
    font-weight: 600;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    background: rgba(0,0,0,0.15);
    color: var(--color-text-dim);
    font-family: inherit;
    transition: all 0.15s;
}
.market-btn:hover { border-color: var(--color-accent-dim); color: var(--color-text); background: rgba(255,255,255,0.03); }
.market-btn.active { border-color: var(--color-accent); color: var(--color-accent-bright); background: rgba(155,44,44,0.1); }

/* === Category pills === */
.market-cats {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    margin-bottom: 10px;
}
.market-cat {
    padding: 3px 8px;
    font-size: 0.68em;
    font-weight: 600;
    border: 1px solid var(--color-border);
    border-radius: 10px;
    cursor: pointer;
    background: transparent;
    color: var(--color-text-dim);
    font-family: inherit;
    transition: all 0.15s;
}
.market-cat:hover { color: var(--color-text); border-color: var(--color-accent-dim); }
.market-cat.active { color: var(--color-accent-bright); border-color: var(--color-accent); background: rgba(155,44,44,0.08); }

/* === Listings table === */
.market-table-head {
    display: grid;
    grid-template-columns: 1fr 55px 75px 80px 60px;
    gap: 4px;
    padding: 0 6px 6px;
    font-size: 0.62em;
    font-weight: 700;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid var(--color-border);
}
.market-table-row {
    display: grid;
    grid-template-columns: 1fr 55px 75px 80px 60px;
    gap: 4px;
    padding: 5px 6px;
    font-size: 0.75em;
    align-items: center;
    border-bottom: 1px solid rgba(255,255,255,0.02);
    transition: background 0.1s;
}
.market-table-row:hover { background: rgba(255,255,255,0.02); }

.market-item-cell {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}
.market-item-img {
    width: 26px;
    height: 26px;
    border-radius: 3px;
    flex-shrink: 0;
    object-fit: cover;
    background: rgba(0,0,0,0.3);
    border: 1px solid var(--color-border);
}
.market-item-name {
    font-weight: 600;
    color: var(--color-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.market-item-type {
    font-size: 0.85em;
    color: var(--color-text-muted);
}
.market-price { font-weight: 700; color: var(--color-gold, #ffd700); }
.market-seller { color: var(--color-text-muted); font-size: 0.9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.market-buy-btn {
    padding: 3px 8px;
    font-size: 0.72em;
    font-weight: 700;
    background: rgba(81,207,102,0.12);
    color: #51cf66;
    border: 1px solid rgba(81,207,102,0.3);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s;
}
.market-buy-btn:hover { background: rgba(81,207,102,0.25); color: #7fff7f; }

/* === Pagination === */
.market-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    padding: 8px 0 2px;
}
.market-page-btn {
    padding: 3px 10px;
    font-size: 0.72em;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: rgba(0,0,0,0.15);
    color: var(--color-text-dim);
    cursor: pointer;
    font-family: inherit;
}
.market-page-btn:disabled { opacity: 0.25; cursor: not-allowed; }
.market-page-info { font-size: 0.72em; color: var(--color-text-muted); }

/* === Sell tab: inventory grid === */
.market-sell-layout {
    display: flex;
    flex-direction: column;
    gap: 8px;
    height: 100%;
}
.market-inv-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
    gap: 3px;
    flex: 1;
    overflow-y: auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    padding: 5px;
    background: rgba(0,0,0,0.1);
    align-content: start;
}
.market-inv-grid::-webkit-scrollbar { width: 4px; }
.market-inv-grid::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

.market-inv-slot {
    width: 100%;
    height: 54px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.15s;
    position: relative;
    overflow: hidden;
    padding: 2px 1px;
    box-sizing: border-box;
}
.market-inv-slot:hover { border-color: var(--color-accent-dim); background: rgba(255,255,255,0.03); }
.market-inv-slot.selected { border-color: var(--color-accent); background: rgba(155,44,44,0.12); box-shadow: 0 0 6px rgba(var(--color-accent-rgb, 155,44,44),0.2); }
.market-inv-slot img {
    width: 24px;
    height: 24px;
    object-fit: cover;
    border-radius: 2px;
    flex-shrink: 0;
}
.market-inv-slot img[src=""],
.market-inv-slot img:not([src]) {
    display: none;
}
.market-inv-slot-name {
    font-size: 0.48em;
    color: var(--color-text-dim);
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
    margin-top: 1px;
    line-height: 1.1;
}
.market-inv-slot-qty {
    position: absolute;
    bottom: 1px;
    right: 2px;
    font-size: 0.5em;
    font-weight: 700;
    color: var(--color-text-muted);
    background: rgba(0,0,0,0.6);
    padding: 0 2px;
    border-radius: 2px;
    line-height: 1.2;
}

/* Sell form */
.market-sell-form {
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    padding: 10px;
    background: rgba(0,0,0,0.1);
}
.market-sell-info {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.market-sell-info img {
    width: 32px;
    height: 32px;
    border-radius: 3px;
    border: 1px solid var(--color-border);
    object-fit: cover;
}
.market-sell-item-name {
    font-size: 0.82em;
    font-weight: 600;
    color: var(--color-text);
}
.market-sell-item-qty {
    font-size: 0.7em;
    color: var(--color-text-muted);
}
.market-sell-fields {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    flex-wrap: wrap;
}
.market-sell-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.market-sell-field label {
    font-size: 0.65em;
    color: var(--color-text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.market-sell-field input {
    padding: 5px 8px;
    font-size: 0.78em;
    width: 80px;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text);
    font-family: inherit;
    outline: none;
}
.market-sell-field input:focus { border-color: var(--color-accent-dim); }
.market-sell-submit {
    padding: 6px 16px;
    font-size: 0.75em;
    font-weight: 700;
    background: rgba(81,207,102,0.15);
    color: #51cf66;
    border: 1px solid rgba(81,207,102,0.3);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s;
}
.market-sell-submit:hover { background: rgba(81,207,102,0.3); color: #7fff7f; }
.market-fee-info {
    margin-top: 6px;
    font-size: 0.65em;
    color: var(--color-text-muted);
}
/* Duration selector */
.market-dur-row {
    display: flex;
    gap: 4px;
    margin-bottom: 8px;
}
.market-dur-btn {
    flex: 1;
    padding: 5px 6px;
    font-size: 0.7em;
    font-weight: 600;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    background: rgba(0,0,0,0.15);
    color: var(--color-text-dim);
    font-family: inherit;
    transition: all 0.15s;
    text-align: center;
}
.market-dur-btn:hover { border-color: var(--color-accent-dim); color: var(--color-text); }
.market-dur-btn.active { border-color: var(--color-accent); color: var(--color-accent-bright); background: rgba(155,44,44,0.12); }
.market-dur-fee {
    font-size: 0.85em;
    opacity: 0.7;
}

/* Time left */
.market-time-left {
    font-size: 0.72em;
    color: var(--color-text-muted);
    white-space: nowrap;
}

.market-sell-hint {
    font-size: 0.72em;
    color: var(--color-text-muted);
    text-align: center;
    padding: 16px;
}

/* === My listings === */
.market-my-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 4px;
    font-size: 0.75em;
    border-bottom: 1px solid rgba(255,255,255,0.02);
}
.market-my-row .market-item-cell { flex: 1; }
.market-status {
    font-size: 0.7em;
    padding: 2px 6px;
    border-radius: 3px;
    font-weight: 600;
}
.market-status.active { background: rgba(81,207,102,0.1); color: #51cf66; }
.market-status.sold { background: rgba(77,171,247,0.1); color: #4dabf7; }
.market-status.cancelled { color: var(--color-text-muted); }
.market-cancel-btn {
    padding: 2px 8px;
    font-size: 0.68em;
    border: 1px solid rgba(239,68,68,0.3);
    border-radius: var(--radius-sm);
    background: none;
    color: #f87171;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s;
}
.market-cancel-btn:hover { background: rgba(239,68,68,0.12); }

/* === No access === */
.market-no-access {
    text-align: center;
    padding: 40px 20px;
}
.market-no-access-icon { font-size: 2.5em; margin-bottom: 10px; }
.market-no-access-text { font-size: 0.82em; color: var(--color-text-muted); line-height: 1.6; }

/* === Empty state === */
.market-empty {
    text-align: center;
    padding: 30px 12px;
    color: var(--color-text-muted);
    font-size: 0.78em;
}
.market-empty-icon { font-size: 1.8em; margin-bottom: 6px; }

/* === Toast === */
.market-toast {
    position: absolute;
    top: 44px;
    left: 50%;
    transform: translateX(-50%);
    padding: 6px 14px;
    font-size: 0.72em;
    font-weight: 600;
    border-radius: var(--radius-sm);
    z-index: 10;
    pointer-events: none;
    animation: mktToast 2s forwards;
}
.market-toast.success { background: rgba(81,207,102,0.15); color: #51cf66; border: 1px solid rgba(81,207,102,0.3); }
.market-toast.error { background: rgba(239,68,68,0.15); color: #f87171; border: 1px solid rgba(239,68,68,0.3); }
@keyframes mktToast {
    0%, 70% { opacity: 1; }
    100% { opacity: 0; }
}

/* === Mobile === */
@media (max-width: 600px) {
    .market-container {
        width: 100vw !important;
        height: 100dvh !important;
        border-radius: 0;
        top: 0 !important;
        left: 0 !important;
        transform: none !important;
        resize: none;
    }
    .market-sidebar { width: 80px; }
    .market-tab { font-size: 0.68em; padding: 6px 8px; }
    .market-inv-grid { grid-template-columns: repeat(4, 1fr); }
    .market-table-head,
    .market-table-row { grid-template-columns: 1fr 50px 65px 55px; }
    .market-table-head > :nth-child(4),
    .market-table-row > :nth-child(4) { display: none; }
}
