:root {
    --primary: #00a86b;
    --primary-dark: #008c59;
    --bg-main: #1f1f1f;
    --bg-sidebar: #2b2b2b;
    --bg-chat: #252525;
    --bg-card: #2e2e2e;
    --bg-input: #3a3a3a;
    --text-white: #ffffff;
    --text-gray: #b0b0b0;
    --border-color: #444;

    /* Role colors */
    --admin-color: #ff6b6b;
    --mod-color: #4dabf7;
    --user-color: #00a86b;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-white);
    overflow-x: hidden;
    max-width: 100%;
}

/* Ensure 100% height so fixed elements sized with 100% work correctly */
html, body { height: 100%; }

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* ============================================================
   SIDEBAR
   ============================================================ */

.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 220px;
    height: 100%;
    background: var(--bg-sidebar);
    border-right: 3px solid var(--primary);
    padding: 20px;
    overflow-y: auto;
    z-index: 200;
}

.logo {
    color: var(--primary);
    font-size: 1.5em;
    font-weight: bold;
    margin-bottom: 30px;
    text-align: center;
}

.sidebar a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 10px 0;
    padding: 10px;
    color: var(--primary);
    text-decoration: none;
    border-radius: 5px;
    transition: all 0.3s ease;
    font-size: 0.92em;
}

.sidebar a:hover {
    background: var(--bg-input);
    padding-left: 15px;
}

/* DM unread badge */
.dm-badge {
    background: #ff4444;
    color: #fff;
    font-size: 0.7em;
    font-weight: bold;
    border-radius: 10px;
    padding: 2px 7px;
    min-width: 18px;
    text-align: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    animation: pulse-badge 2s infinite;
}

@keyframes pulse-badge {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255,68,68,0.5); }
    50%       { box-shadow: 0 0 0 5px rgba(255,68,68,0); }
}

/* Badge shelf in sidebar */
.badge-shelf {
    margin-top: 14px;
    padding: 10px;
    background: linear-gradient(135deg, #1a1a0d, #2a2a10);
    border: 1px solid #555522;
    border-radius: 8px;
}
.badge-icons {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}
.badge-icon {
    font-size: 1.3em;
    cursor: help;
}

/* ============================================================
   541 BUCKS DISPLAY
   ============================================================ */

.bucks-display {
    margin-top: 16px;
    padding: 12px;
    background: linear-gradient(135deg, #1a2e22, #0d1f16);
    border: 1px solid var(--primary);
    border-radius: 8px;
    text-align: center;
}
.bucks-label {
    font-size: 0.7em;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 4px;
}
.bucks-amount {
    font-size: 1.2em;
    font-weight: 900;
    color: var(--primary);
    font-family: monospace;
}

/* ============================================================
   CHAT PANEL
   ============================================================ */

.chat {
    position: fixed;
    left: 220px;
    top: 0;
    width: 320px;
    height: 100%;
    background: var(--bg-chat);
    border-right: 2px solid var(--primary);
    display: flex;
    flex-direction: column;
    z-index: 200;
}

.chat-header {
    padding: 15px;
    background: var(--bg-sidebar);
    border-bottom: 2px solid var(--primary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h3 { margin: 0; color: var(--primary); }

#messages {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#messages::-webkit-scrollbar { width: 8px; }
#messages::-webkit-scrollbar-track { background: var(--bg-sidebar); }
#messages::-webkit-scrollbar-thumb { background: var(--primary); border-radius: 4px; }

/* Scrollbar styling for other containers (webkit) */
.sidebar::-webkit-scrollbar { width: 10px; }
.sidebar::-webkit-scrollbar-track { background: var(--bg-main); }
.sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.06); border-radius: 6px; }
.content::-webkit-scrollbar { height: 10px; width: 10px; }
.content::-webkit-scrollbar-track { background: transparent; }
.content::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.06); border-radius: 6px; }
.leaderboard-wrap::-webkit-scrollbar { height: 10px; }
.leaderboard-wrap::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.06); border-radius: 6px; }

/* Hover state for thumbs */
#messages::-webkit-scrollbar-thumb:hover,
.sidebar::-webkit-scrollbar-thumb:hover,
.content::-webkit-scrollbar-thumb:hover {
    background: rgba(0,168,107,0.9);
}

/* Firefox scrollbar styling */
.sidebar, #messages, .content, .leaderboard-wrap {
    scrollbar-width: thin;
    scrollbar-color: var(--primary) transparent;
}

.chat-message {
    background: var(--bg-card);
    padding: 10px;
    border-radius: 8px;
    border-left: 3px solid var(--primary);
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 5px;
}

.badge { font-size: 1.1em; }

.username { font-weight: bold; font-size: 0.95em; }
.username.role-admin     { color: var(--admin-color); }
.username.role-moderator { color: var(--mod-color); }
.username.role-user      { color: var(--user-color); }

.timestamp {
    color: var(--text-gray);
    font-size: 0.72em;
    margin-left: auto;
    white-space: nowrap;
}

.message-content {
    margin: 5px 0 0 0;
    color: var(--text-white);
    font-size: 0.9em;
    line-height: 1.4;
    word-wrap: break-word;
}

.chat-error {
    background: rgba(255, 107, 107, 0.2);
    border-left: 3px solid #ff6b6b;
    padding: 10px;
    border-radius: 8px;
    animation: slideIn 0.3s ease;
}
.chat-error p { margin: 0; color: #ff6b6b; font-size: 0.9em; }

#chat-form {
    padding: 10px;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border-color);
}

#message-input {
    width: 100%;
    padding: 10px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    color: var(--text-white);
    font-size: 0.9em;
}
#message-input:focus { outline: none; border-color: var(--primary); }

#online-users {
    padding: 10px;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border-color);
    font-size: 0.85em;
    color: var(--text-gray);
}

.online-user { display: inline-block; margin: 2px 0; }

/* Collapsible chat */
.chat.collapsed #messages,
.chat.collapsed #chat-form,
.chat.collapsed #online-users { display: none; }

.chat-toggle-btn {
    background: none;
    border: none;
    color: var(--primary);
    font-size: 1.4em;
    cursor: pointer;
}

/* Backdrop */
.chat-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1050;
}
.chat-backdrop.show { display: block; }

/* ============================================================
   MAIN CONTENT
   ============================================================ */

.content {
    margin-left: 540px;
    padding: 30px;
    min-height: 100vh;
    box-sizing: border-box;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto; /* allow main content to scroll independently */
}

h1 { color: var(--primary); margin-bottom: 30px; }

/* ============================================================
   CARDS
   ============================================================ */

.card {
    background: var(--bg-card);
    padding: 20px;
    margin-bottom: 20px;
    border-left: 4px solid var(--primary);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform 0.2s ease;
    max-width: 100%;
    box-sizing: border-box;
}
.card:hover { transform: translateY(-2px); }
.card h3 { margin-top: 0; color: var(--primary); }
.card input {
    width: 100%;
    padding: 10px;
    margin-bottom: 10px;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    color: var(--text-white);
    font-size: 0.95em;
    box-sizing: border-box;
}
.card input:focus { outline: none; border-color: var(--primary); }
.card button {
    background: var(--primary);
    border: none;
    padding: 12px 20px;
    cursor: pointer;
    color: var(--text-white);
    border-radius: 5px;
    font-weight: bold;
    transition: background 0.3s ease;
}
.card button:hover { background: var(--primary-dark); }
.card a { color: var(--primary); text-decoration: none; }
.card a:hover { text-decoration: underline; }

/* ============================================================
   GRID
   ============================================================ */

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
}

/* ============================================================
   LEADERBOARD
   ============================================================ */

.leaderboard-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overflow: visible !important;
}

.leaderboard {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg-card);
    border-radius: 8px;
    overflow: hidden;
    overflow: visible !important;
}
.leaderboard th {
    background: var(--bg-sidebar);
    color: var(--primary);
    padding: 15px;
    text-align: left;
    font-weight: bold;
}
.leaderboard td {
    padding: 12px 15px;
    border-bottom: 1px solid var(--border-color);
}
.leaderboard tr:last-child td { border-bottom: none; }
.leaderboard tr:hover { background: var(--bg-input); }

/* Leaderboard badge popover fix */
.leaderboard-badge-pop {
    position: fixed !important;
    z-index: 9999 !important;
    min-width: 180px;
    max-width: 320px;
    background: #222;
    border: 1px solid #444;
    border-radius: 8px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.35);
    padding: 12px 16px;
    color: #fff;
    font-size: 0.95em;
    pointer-events: auto;
    top: var(--popover-top, 0);
    left: var(--popover-left, 0);
}

/* Ensure leaderboard rows allow overflow for popovers */
.leaderboard tr,
.leaderboard td {
    position: relative;
    overflow: visible !important;
}

/* ============================================================
   ROLE BADGES & ADMIN
   ============================================================ */

.admin { color: var(--admin-color); font-weight: bold; }
.mod   { color: var(--mod-color); font-weight: bold; }

.admin-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.admin-actions button {
    padding: 8px 15px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.85em;
    transition: all 0.3s ease;
}
.btn-promote { background: var(--mod-color); color: white; }
.btn-ban     { background: var(--admin-color); color: white; }
.btn-mute    { background: #ffa94d; color: white; }
.btn-delete  { background: #868e96; color: white; }
.admin-actions button:hover { transform: scale(1.05); opacity: 0.9; }

/* ============================================================
   FLASH MESSAGES
   ============================================================ */

.flash-message {
    padding: 12px 20px;
    margin-bottom: 20px;
    border-radius: 5px;
    background: rgba(255, 107, 107, 0.2);
    border-left: 4px solid #ff6b6b;
    color: #ff6b6b;
}

/* ============================================================
   SIDEBAR & CHAT CLOSE BUTTONS (hidden on desktop)
   ============================================================ */

.sidebar-close-btn,
.chat-close-btn {
    display: none;
    background: none;
    border: none;
    color: #888;
    font-size: 1.4em;
    cursor: pointer;
    padding: 4px 8px;
    margin-left: auto;
    align-items: center;
    justify-content: center;
    line-height: 1;
}
.sidebar-close-btn:hover,
.chat-close-btn:hover { color: #fff; }

/* ============================================================
   MOBILE NAV BAR (hidden on desktop)
   ============================================================ */

.mobile-nav { display: none; }

/* ============================================================
   TABLET (≤ 1200px) — hide chat behind toggle
   ============================================================ */

@media (max-width: 1200px) {
    .content {
        margin-left: 220px !important;
    }
    .sidebar {
        width: 220px !important;
    }
    .chat {
        left: auto !important;
        right: 0 !important;
        transform: translateX(100%);
        transition: transform 0.3s ease;
        border-left: 2px solid var(--primary);
        border-right: none;
    }
    .chat.open {
        transform: translateX(0);
    }
}

/* ============================================================
   MOBILE (≤ 768px) — full drawer + bottom nav layout
   ============================================================ */

@media (max-width: 768px) {

    /* Lock page to screen — no horizontal scroll */
    html, body {
        overflow-x: hidden;
        max-width: 100vw;
    }

    /* Sidebar becomes a left slide-in drawer */
    .sidebar {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 80% !important;
        max-width: 300px !important;
        height: 100% !important;
        z-index: 1100;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        overflow-y: auto;
        border-right: 3px solid var(--primary);
    }
    .sidebar.mobile-open {
        transform: translateX(0);
    }

    /* Show close buttons inside drawers */
    .sidebar-close-btn,
    .chat-close-btn {
        display: flex !important;
    }

    /* Chat becomes a right slide-in drawer */
    .chat {
        position: fixed !important;
        top: 0 !important;
        right: 0 !important;
        left: auto !important;
        width: 100% !important;
        max-width: 420px !important;
        height: 100% !important;
        z-index: 1100;
        transform: translateX(100%) !important;
        transition: transform 0.3s ease !important;
        border-left: 2px solid var(--primary) !important;
        border-right: none !important;
    }
    .chat.open {
        transform: translateX(0) !important;
    }

    /* Content: full width, padded bottom for nav bar */
    .content {
        margin-left: 0 !important;
        padding: 5px 5px 5px !important;
        width: 100% !important;
        max-width: 100vw !important;
        box-sizing: border-box !important;
        overflow-x: hidden !important;
    }

    /* Backdrop sits above content, below drawers */
    .chat-backdrop {
        z-index: 1050;
    }

    /* All containers stay in bounds */
    .card,
    .grid,
    [class$="-wrapper"],
    [class$="-panel"],
    [class$="-hero"],
    [class$="-header"],
    [class$="-bar"],
    [class$="-grid"] {
        max-width: 100% !important;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    /* Tighter card padding on mobile */
    .card { padding: 14px; }

    /* Single column grid */
    .grid { grid-template-columns: 1fr !important; }

    /* Tables scroll horizontally instead of overflowing */
    .leaderboard-wrap,
    .market-table,
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
        min-width: unset !important;
    }

    /* Images stay contained */
    img {
        max-width: 100%;
        height: auto;
    }

    /* Inputs, textareas, selects, buttons respect container */
    input, textarea, select, button {
        max-width: 100%;
        box-sizing: border-box;
    }

    /* Flex rows wrap on small screens */
    .risk-bar,
    .market-tabs,
    .admin-actions,
    .dice-bucks-bar,
    .keno-header,
    .market-header {
        flex-wrap: wrap;
    }

    /* Game panels tighter padding */
    .dice-panel,
    .keno-grid,
    .slots-machine {
        padding: 16px !important;
        box-sizing: border-box;
    }

    h1 { font-size: 1.4em; }

    /* ── Bottom navigation bar ── */
    .mobile-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 60px;
        background: #141414;
        border-top: 2px solid var(--primary);
        z-index: 1000;
        align-items: stretch;
        justify-content: space-around;
    }
    .mobile-nav a,
    .mobile-nav button {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        background: none;
        border: none;
        color: #666;
        font-size: 0.58em;
        letter-spacing: 0.03em;
        text-transform: uppercase;
        text-decoration: none;
        cursor: pointer;
        padding: 6px 0;
        transition: color 0.2s;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-nav a .nav-icon,
    .mobile-nav button .nav-icon {
        font-size: 1.55em;
        line-height: 1;
    }
    .mobile-nav a:hover,
    .mobile-nav button:hover,
    .mobile-nav a.active,
    .mobile-nav button.active {
        color: var(--primary);
    }
    .mobile-nav .nav-chat-badge {
        position: relative;
    }
    .mobile-nav .chat-dot {
        position: absolute;
        top: 6px;
        right: calc(50% - 18px);
        width: 8px;
        height: 8px;
        background: #ff6b6b;
        border-radius: 50%;
        display: none;
    }
    .mobile-nav .chat-dot.show { display: block; }

    /* Badge equip grid and emoji equip grid */
    .equip-grid {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
        margin-bottom: 16px;
    }

    .equip-badge-card {
        background: #222;
        border: 1px solid #2a2a2a;
        border-radius: 12px;
        padding: 12px 14px;
        display: flex;
        align-items: center;
        gap: 10px;
        transition: border-color 0.18s;
        min-width: 170px;
        max-width: 240px;
        flex: 1 1 170px;
    }
    .equip-badge-card:hover { border-color: #444; }
    .equip-badge-card.active-left  { border-color: #00a86b; background: #1a2e22; }
    .equip-badge-card.active-right { border-color: #4dabf7; background: #1a2030; }
    .equip-badge-card.active-both  { border-color: #fbbf24; background: #2a2510; }

    .ebc-emoji { font-size: 1.8em; flex-shrink: 0; }
    .ebc-info  { flex: 1; min-width: 0; }
    .ebc-name  { font-size: 0.82em; color: #ccc; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .ebc-btns  { display: flex; gap: 4px; margin-top: 6px; flex-wrap: wrap; justify-content: flex-start; }
    .ebc-btn {
        font-size: 0.68em;
        font-weight: bold;
        border: 1px solid #333;
        border-radius: 6px;
        padding: 3px 8px;
        cursor: pointer;
        background: #1a1a1a;
        color: #777;
        transition: all 0.15s;
        white-space: nowrap;
    }
    .ebc-btn:hover { border-color: #555; color: #ddd; }
    .ebc-btn.active-l { background: #1a2e22; border-color: #00a86b; color: #00a86b; }
    .ebc-btn.active-r { background: #1a2030; border-color: #4dabf7; color: #4dabf7; }
    .ebc-btn.active-b { background: #2a2510; border-color: #fbbf24; color: #fbbf24; }

    .unequip-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 32px; }
    .unequip-btn {
        font-size: 0.8em;
        font-weight: bold;
        border: 1px solid #333;
        border-radius: 7px;
        padding: 5px 14px;
        cursor: pointer;
        background: #1a1a1a;
        color: #666;
        transition: all 0.15s;
    }
    .unequip-btn:hover { border-color: #666; color: #ccc; }
}