:root {
  --bg-primary: #0f0c29;
  --bg-secondary: #1a1740;
  --bg-card: rgba(255,255,255,0.06);
  --bg-card-hover: rgba(255,255,255,0.1);
  --text-primary: #f0f0f0;
  --text-secondary: #8888aa;
  --text-muted: #555577;
  --accent: #667eea;
  --accent-gradient: linear-gradient(135deg, #667eea, #764ba2);
  --green: #2ecc71;
  --red: #e74c3c;
  --orange: #f39c12;
  --admin-color: #e74c3c;
  --mod-color: #f39c12;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --header-height: 52px;
  --nav-height: 64px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; overflow: hidden; }
body { font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif; background: var(--bg-primary); color: var(--text-primary); -webkit-font-smoothing: antialiased; user-select: none; }
#app { height: 100%; display: flex; flex-direction: column; }
input { font-family: inherit; font-size: 16px; }

/* ========== AUTH ========== */
#page-auth { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 24px; overflow-y: auto; }
.auth-screen { width: 100%; max-width: 400px; }
.auth-header { text-align: center; margin-bottom: 32px; }
.auth-header h1 { font-size: 2.4em; background: var(--accent-gradient); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; font-weight: 800; letter-spacing: -0.03em; }
.auth-header p { color: var(--text-secondary); margin-top: 6px; font-size: 0.95em; }
.auth-tabs { display: flex; background: var(--bg-card); border-radius: var(--radius-md); padding: 4px; margin-bottom: 24px; }
.tab { flex: 1; padding: 12px; border: none; background: transparent; color: var(--text-secondary); font-size: 0.95em; font-weight: 600; border-radius: var(--radius-sm); cursor: pointer; transition: all 0.2s; }
.tab.active { background: var(--accent-gradient); color: #fff; }
#authForm { display: flex; flex-direction: column; gap: 8px; }
.field-group { display: flex; flex-direction: column; gap: 4px; }
.field-spacer { margin-bottom: 8px; }
.field-group input { width: 100%; padding: 16px 14px; background: var(--bg-card); border: 1.5px solid transparent; border-radius: var(--radius-sm); color: var(--text-primary); font-size: 16px; transition: all 0.2s; outline: none; }
.field-group input:focus { border-color: var(--accent); background: var(--bg-card-hover); }
.field-group input::placeholder { color: var(--text-muted); }
.field-hint { font-size: 0.82em; padding: 4px 4px 0; }
.field-hint.available { color: var(--green); }
.field-hint.taken { color: var(--red); }
.field-hint.checking { color: var(--text-secondary); }

.password-wrap { position: relative; display: flex; align-items: center; }
.password-wrap input { width: 100%; padding-right: 48px; }
.eye-toggle { position: absolute; right: 8px; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border: none; background: transparent; color: var(--text-muted); cursor: pointer; border-radius: 50%; }
.eye-toggle:active { background: var(--bg-card); }

.password-strength { padding: 4px 0; }
.strength-bar { height: 4px; background: var(--bg-card-hover); border-radius: 2px; overflow: hidden; margin-bottom: 4px; }
.strength-fill { height: 100%; width: 0%; border-radius: 2px; transition: all 0.3s ease; }
.strength-text { font-size: 0.82em; }
.strength-text.weak { color: var(--red); }
.strength-text.medium { color: var(--orange); }
.strength-text.strong { color: var(--green); }
.forgot-password { text-align: center; padding-top: 8px; }
.forgot-password a { color: var(--text-muted); text-decoration: none; font-size: 0.88em; }

.error-snackbar { position: fixed; bottom: 24px; left: 16px; right: 16px; padding: 14px 20px; background: rgba(231,76,60,0.95); color: #fff; border-radius: var(--radius-sm); text-align: center; font-size: 0.92em; z-index: 200; animation: slideUp 0.3s ease; }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ========== APP SHELL ========== */
#app-shell { display: flex; flex-direction: column; height: 100%; }

#appHeader { display: flex; align-items: center; justify-content: space-between; height: var(--header-height); padding: 0 16px; padding-top: var(--safe-top); background: rgba(15,12,41,0.95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid rgba(255,255,255,0.06); z-index: 10; flex-shrink: 0; }
.header-brand { font-size: 1.15em; font-weight: 700; background: var(--accent-gradient); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
.header-right { display: flex; align-items: center; gap: 8px; }
.header-balance { font-weight: 700; font-size: 0.95em; color: var(--green); }

#mainContent { flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
.page-content { display: none; min-height: 100%; padding: 12px 16px; padding-bottom: calc(var(--nav-height) + 16px); }
.page-content.active { display: block; }

/* ========== BOTTOM NAV ========== */
#bottomNav { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: rgba(15,12,41,0.96); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-top: 1px solid rgba(255,255,255,0.06); padding-bottom: var(--safe-bottom); z-index: 50; }
.nav-item { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; border: none; background: transparent; color: var(--text-muted); font-size: 0.7em; font-weight: 500; cursor: pointer; transition: color 0.2s; }
.nav-item svg { transition: all 0.2s; }
.nav-item.active { color: var(--accent); }
.nav-item.active svg { stroke: var(--accent); filter: drop-shadow(0 0 6px rgba(102,126,234,0.4)); }
.badge { background: var(--red); color: #fff; font-size: 0.7em; padding: 1px 6px; border-radius: 10px; margin-left: 2px; vertical-align: top; }

/* ========== BUTTONS ========== */
.btn-primary { background: var(--accent-gradient); color: #fff; border: none; border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; font-size: 1em; transition: all 0.15s; }
.btn-primary:active { transform: scale(0.97); opacity: 0.9; }
.btn-primary:disabled { opacity: 0.4; transform: none; }
.btn-large { padding: 16px; font-size: 1.05em; border-radius: var(--radius-md); }
.btn-icon-sm { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border: none; background: var(--bg-card); border-radius: 50%; color: var(--text-secondary); cursor: pointer; font-size: 1.2em; }

/* ========== ROLE BADGES ========== */
.role-badge { font-size: 0.78em; font-weight: 700; padding: 1px 6px; border-radius: 4px; margin-right: 2px; }
.role-badge.admin { color: var(--admin-color); background: rgba(231,76,60,0.15); }
.role-badge.moderator { color: var(--mod-color); background: rgba(243,156,18,0.15); }

/* ========== GAMES ========== */
.games-category { margin-bottom: 20px; }
.games-category-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.games-category-header h2 { font-size: 1em; color: var(--text-secondary); font-weight: 600; }
.category-icon { font-size: 1.2em; }

.games-list { display: flex; flex-direction: column; gap: 12px; }
.game-card { background: var(--bg-card); border-radius: var(--radius-lg); overflow: hidden; backdrop-filter: blur(8px); }
.game-card-header { display: flex; align-items: center; gap: 14px; padding: 16px 18px 0; }
.game-icon { font-size: 2em; width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; background: var(--bg-card-hover); border-radius: var(--radius-md); }
.game-card-header h2 { font-size: 1.15em; font-weight: 700; }
.game-card-header p { color: var(--text-secondary); font-size: 0.85em; margin-top: 2px; }
.game-card-body { padding: 14px 18px 18px; }
.bet-input-group { display: flex; gap: 10px; }
.bet-input-group input { flex: 1; padding: 14px 12px; background: var(--bg-primary); border: 1.5px solid rgba(255,255,255,0.08); border-radius: var(--radius-sm); color: var(--text-primary); font-size: 16px; outline: none; }
.bet-input-group input:focus { border-color: var(--accent); }
.bet-input-group input::placeholder { color: var(--text-muted); }
.bet-input-group .btn-primary { padding: 14px 24px; white-space: nowrap; }
.game-status { margin-top: 10px; padding: 10px 12px; border-radius: var(--radius-sm); text-align: center; font-size: 0.9em; color: var(--text-secondary); background: var(--bg-card-hover); }

/* Plinko */
.plinko-game { margin-top: 12px; }
.plinko-risk { display: flex; gap: 6px; margin-bottom: 10px; }
.risk-btn { flex: 1; padding: 10px; border: 1.5px solid rgba(255,255,255,0.1); background: transparent; color: var(--text-secondary); border-radius: var(--radius-sm); font-size: 0.85em; font-weight: 600; cursor: pointer; transition: all 0.2s; }
.risk-btn.active { border-color: var(--accent); background: rgba(102,126,234,0.15); color: var(--accent); }
.risk-btn:active { transform: scale(0.97); }

.plinko-board-container {
  background: var(--bg-primary); border-radius: var(--radius-md);
  overflow: hidden; margin-bottom: 10px;
}
#plinkoCanvas { display: block; width: 100%; height: auto; max-width: 340px; margin: 0 auto; background: var(--bg-primary); }

#activeGame { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,0.7); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; padding: 24px; }
.active-game-card { background: var(--bg-secondary); border-radius: var(--radius-xl); padding: 28px 24px; width: 100%; max-width: 360px; text-align: center; }
.active-game-card h2 { font-size: 1.2em; margin-bottom: 16px; color: var(--text-secondary); }
.active-game-card p { margin: 8px 0; font-size: 1em; }
.active-game-card .btn-primary { margin: 6px; padding: 14px 28px; }

/* ========== CHAT ========== */
.chat-messages { padding: 8px 0 12px; }
.chat-message { padding: 10px 14px; margin: 4px 0; background: var(--bg-card); border-radius: var(--radius-md); max-width: 85%; }
.chat-message.own { margin-left: auto; background: rgba(102,126,234,0.15); border-bottom-right-radius: 4px; }
.chat-message.other { margin-right: auto; border-bottom-left-radius: 4px; }
.chat-message .username { color: var(--accent); font-weight: 600; font-size: 0.82em; cursor: pointer; }
.chat-message .username:active { opacity: 0.6; }
.chat-message .time { color: var(--text-muted); font-size: 0.72em; margin-left: 8px; }
.chat-message .msg { display: block; margin-top: 3px; font-size: 0.95em; line-height: 1.4; word-break: break-word; }

.dm-banner { display: flex; align-items: center; gap: 8px; padding: 10px 4px; margin-bottom: 4px; background: var(--bg-card); border-radius: var(--radius-sm); font-weight: 600; font-size: 0.9em; }

.chat-bar { position: fixed; bottom: var(--nav-height); left: 0; right: 0; display: flex; gap: 8px; padding: 8px 8px; padding-bottom: calc(8px + var(--safe-bottom)); background: rgba(15,12,41,0.97); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid rgba(255,255,255,0.06); z-index: 40; align-items: center; }
.chat-bar input { flex: 1; padding: 12px 14px; background: var(--bg-card); border: 1.5px solid rgba(255,255,255,0.08); border-radius: 22px; color: var(--text-primary); font-size: 16px; outline: none; }
.chat-bar input:focus { border-color: var(--accent); }
.chat-bar input::placeholder { color: var(--text-muted); }
.btn-send { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; border: none; background: var(--accent-gradient); border-radius: 50%; color: #fff; cursor: pointer; flex-shrink: 0; }
.btn-send:active { transform: scale(0.9); }
.btn-dm { width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border: none; background: var(--bg-card); border-radius: 50%; color: var(--text-secondary); cursor: pointer; flex-shrink: 0; }
.btn-dm:active { background: var(--bg-card-hover); }

/* ========== DM PANEL ========== */
.dm-panel { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 70; background: var(--bg-primary); display: flex; flex-direction: column; padding-top: var(--safe-top); }
.dm-panel-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; font-weight: 600; }
.dm-panel-search { padding: 0 16px 8px; position: relative; }
.dm-panel-search input { width: 100%; padding: 12px 14px; background: var(--bg-card); border: 1.5px solid rgba(255,255,255,0.08); border-radius: var(--radius-sm); color: var(--text-primary); font-size: 16px; outline: none; }
.dm-panel-search input:focus { border-color: var(--accent); }
.dm-panel-list { flex: 1; overflow-y: auto; padding: 8px 16px; }
.dm-panel-label { font-size: 0.78em; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }

.dm-results { position: absolute; top: 100%; left: 16px; right: 16px; background: var(--bg-secondary); border-radius: var(--radius-sm); z-index: 10; max-height: 200px; overflow-y: auto; margin-top: 4px; }
.dm-user-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid rgba(255,255,255,0.04); }
.dm-user-item:active { background: var(--bg-card); }
.dm-user-avatar { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; background: var(--accent-gradient); border-radius: 50%; font-weight: 700; font-size: 0.9em; flex-shrink: 0; }
.dm-user-avatar.sm { width: 28px; height: 28px; font-size: 0.75em; }
.dm-user-name { font-size: 0.9em; font-weight: 500; }

.dm-conv-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; cursor: pointer; border-bottom: 1px solid rgba(255,255,255,0.04); }
.dm-conv-item:active { opacity: 0.7; }
.dm-conv-name { font-size: 0.9em; font-weight: 500; }

/* ========== USER POPUP ========== */
.user-popup { position: fixed; inset: 0; z-index: 75; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; padding: 24px; }
.user-popup-card { background: var(--bg-secondary); border-radius: var(--radius-xl); padding: 28px 24px; width: 100%; max-width: 320px; text-align: center; }
.user-popup-header { display: flex; align-items: center; gap: 14px; margin-bottom: 20px; text-align: left; }
.user-popup-avatar { width: 52px; height: 52px; display: flex; align-items: center; justify-content: center; background: var(--accent-gradient); border-radius: 50%; font-size: 1.4em; font-weight: 700; flex-shrink: 0; }
.user-popup-name { font-size: 1.1em; font-weight: 700; }
.user-popup-role { font-size: 0.82em; color: var(--text-secondary); margin-top: 2px; }
.user-popup-actions { margin-bottom: 12px; }
.btn-popup-close { width: 100%; padding: 12px; border: 1px solid rgba(255,255,255,0.1); background: transparent; color: var(--text-secondary); border-radius: var(--radius-sm); font-size: 0.95em; cursor: pointer; }

/* ========== PROFILE ========== */
#page-profile { padding-bottom: calc(80px + var(--nav-height)); }
.profile-card { display: flex; align-items: center; gap: 16px; background: var(--bg-card); border-radius: var(--radius-lg); padding: 20px; margin-bottom: 12px; }
#profileAvatar { width: 72px; height: 72px; display: flex; align-items: center; justify-content: center; background: var(--accent-gradient); border-radius: 50%; font-size: 2em; font-weight: 700; flex-shrink: 0; position: relative; overflow: hidden; }
#profileAvatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-upload-label { position: absolute; bottom: 0; right: 0; width: 26px; height: 26px; background: var(--accent); border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; border: 2px solid var(--bg-primary); }
.profile-info-text { flex: 1; }
.profile-username { font-size: 1.15em; font-weight: 700; }
.profile-role { font-size: 0.82em; color: var(--text-secondary); margin-top: 2px; }
.profile-email { font-size: 0.82em; color: var(--text-muted); margin-top: 2px; }

.profile-stats-row { display: flex; justify-content: space-around; background: var(--bg-card); border-radius: var(--radius-lg); padding: 16px; margin-bottom: 12px; }
.stat-item { display: flex; flex-direction: column; align-items: center; }
.stat-value { font-size: 1.3em; font-weight: 700; }
.stat-label { font-size: 0.75em; color: var(--text-secondary); margin-top: 2px; }

.profile-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.btn-action { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; padding: 14px 8px; border: none; border-radius: var(--radius-md); font-size: 0.85em; font-weight: 600; color: #fff; cursor: pointer; }
.btn-action:active { transform: scale(0.97); }
.btn-deposit { background: linear-gradient(135deg, #2ecc71, #27ae60); }
.btn-withdraw { background: linear-gradient(135deg, #e74c3c, #c0392b); }
.btn-test { background: linear-gradient(135deg, #667eea, #764ba2); font-size: 0.9em; }
.action-icon { font-size: 1.3em; font-weight: 300; }

.section-card { background: var(--bg-card); border-radius: var(--radius-lg); overflow: hidden; margin-bottom: 16px; }
.section-card-header { padding: 16px 18px 0; }
.section-card-header h3 { font-size: 0.95em; color: var(--text-secondary); font-weight: 600; }
.transaction-list { padding: 8px 0; }
.transaction-item { display: flex; justify-content: space-between; align-items: center; padding: 12px 18px; border-bottom: 1px solid rgba(255,255,255,0.04); }
.transaction-item:last-child { border-bottom: none; }
.transaction-desc { font-size: 0.9em; }
.transaction-date { font-size: 0.75em; color: var(--text-muted); margin-top: 2px; }
.transaction-amount { font-weight: 700; font-size: 1em; }
.transaction-amount.positive { color: var(--green); }
.transaction-amount.negative { color: var(--red); }

.btn-logout { width: 100%; padding: 16px; border: 1.5px solid rgba(231,76,60,0.3); background: transparent; color: var(--red); border-radius: var(--radius-md); font-size: 1em; font-weight: 600; cursor: pointer; margin-bottom: 24px; }
.btn-logout:active { background: rgba(231,76,60,0.1); }

/* ========== BALANCE MODAL ========== */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 80; display: flex; align-items: flex-end; justify-content: center; }
.modal-sheet { width: 100%; max-width: 480px; background: var(--bg-secondary); border-radius: var(--radius-xl) var(--radius-xl) 0 0; padding: 12px 20px 32px; padding-bottom: calc(32px + var(--safe-bottom)); animation: sheetUp 0.3s ease; }
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal-handle { width: 36px; height: 4px; background: var(--text-muted); border-radius: 2px; margin: 0 auto 16px; }
.modal-sheet h3 { font-size: 1.15em; margin-bottom: 20px; text-align: center; }
.amount-presets { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 12px; }
.preset-btn { padding: 12px 4px; background: var(--bg-card); border: 1.5px solid transparent; border-radius: var(--radius-sm); color: var(--text-primary); font-size: 0.95em; font-weight: 600; cursor: pointer; }
.preset-btn:active { background: var(--accent); }
.preset-btn.selected { border-color: var(--accent); background: rgba(102,126,234,0.2); }
.modal-body .field-group { margin-bottom: 12px; }
.modal-body input { width: 100%; padding: 16px 14px; background: var(--bg-card); border: 1.5px solid rgba(255,255,255,0.08); border-radius: var(--radius-sm); color: var(--text-primary); font-size: 20px; font-weight: 600; text-align: center; outline: none; }
.modal-body input:focus { border-color: var(--accent); }

/* ========== UTILITIES ========== */
.hidden { display: none !important; }

@media (min-width: 768px) {
  #page-auth { padding: 40px; }
  .games-list { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
}
