:root {
    --bg-main: #0a0e1a;
    --bg-card: rgba(18, 24, 38, 0.85);
    --bg-card-hover: rgba(28, 36, 56, 0.95);
    --border-color: rgba(255, 255, 255, 0.08);
    --border-glow: rgba(239, 68, 68, 0.3);
    --red-square: #ef4444;
    --red-glow: rgba(239, 68, 68, 0.5);
    --blue-enemy: #3b82f6;
    --blue-glow: rgba(59, 130, 246, 0.4);
    --arena-border: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --gold: #f59e0b;
    --silver: #94a3b8;
    --bronze: #d97706;
    --online-green: #10b981;
    --font-main: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-main);
    background-image: 
        radial-gradient(at 0% 0%, rgba(239, 68, 68, 0.08) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(59, 130, 246, 0.08) 0px, transparent 50%),
        radial-gradient(at 50% 50%, rgba(15, 23, 42, 0.6) 0px, transparent 100%);
    color: var(--text-primary);
    font-family: var(--font-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* Header */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 28px;
    background: rgba(10, 14, 26, 0.8);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-square {
    width: 26px;
    height: 26px;
    background: linear-gradient(135deg, #ff5757, #c70000);
    border-radius: 6px;
    box-shadow: 0 0 15px var(--red-glow);
    animation: pulse-slow 3s infinite ease-in-out;
}

@keyframes pulse-slow {
    0%, 100% { transform: scale(1); box-shadow: 0 0 12px var(--red-glow); }
    50% { transform: scale(1.06); box-shadow: 0 0 20px rgba(239, 68, 68, 0.8); }
}

.app-title {
    font-size: 20px;
    font-weight: 800;
    letter-spacing: -0.5px;
    background: linear-gradient(to right, #ffffff, #cbd5e1);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.badge-tag {
    font-size: 11px;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
    padding: 3px 8px;
    border-radius: 6px;
    background: rgba(239, 68, 68, 0.15);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.3);
}

.header-status {
    display: flex;
    align-items: center;
    gap: 16px;
}

.live-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.25);
    padding: 6px 12px;
    border-radius: 9999px;
    font-size: 13px;
    font-weight: 600;
    color: #6ee7b7;
}

.live-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--online-green);
    box-shadow: 0 0 10px var(--online-green);
    animation: live-blink 1.8s infinite;
}

@keyframes live-blink {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.85); }
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-icon {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 38px;
    height: 38px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.2s ease;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    border-color: rgba(255, 255, 255, 0.2);
}

/* User nickname pill */
.user-pill {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    padding: 6px 12px;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s;
}

.user-pill:hover {
    border-color: rgba(239, 68, 68, 0.4);
    background: rgba(239, 68, 68, 0.05);
}

.user-pill-name {
    font-size: 13px;
    font-weight: 600;
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Main Container */
.main-content {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 420px;
    gap: 30px;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
    padding: 24px;
}

/* Game Area */
.game-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 16px;
}

.instructions-card {
    text-align: center;
    max-width: 500px;
}

.instructions-card h2 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 6px;
    color: #ffffff;
}

.instructions-card p {
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.5;
}

.instructions-card strong {
    color: #f87171;
}

/* Timer HUD */
.timer-container {
    background: rgba(15, 23, 42, 0.7);
    border: 1px solid var(--border-color);
    padding: 10px 28px;
    border-radius: 16px;
    display: flex;
    align-items: baseline;
    gap: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.timer-label {
    font-size: 13px;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 1px;
    color: var(--text-muted);
}

.timer-display {
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 38px;
    font-weight: 800;
    color: #fca5a5;
    text-shadow: 0 0 15px rgba(239, 68, 68, 0.4);
    min-width: 140px;
    text-align: right;
}

.timer-unit {
    font-size: 18px;
    color: var(--text-muted);
    font-weight: 600;
}

/* Canvas Arena Wrapper */
.arena-wrapper {
    position: relative;
    border-radius: 16px;
    padding: 4px;
    background: linear-gradient(145deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.02));
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.08);
}

/* Classic Red Square dimensions: 380x380 canvas with 40px outer border */
#gameCanvas {
    display: block;
    background-color: #ffffff;
    border: 40px solid #718096;
    border-radius: 8px;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    transition: border-color 0.2s;
}

#gameCanvas:active {
    cursor: grabbing;
}

#gameCanvas.active-game {
    border-color: #4a5568;
    box-shadow: 0 0 25px rgba(239, 68, 68, 0.2);
}

/* Arena Overlay (Game Over / Start) */
.arena-overlay {
    position: absolute;
    top: 44px; /* Inside the 40px border */
    left: 44px;
    width: 380px;
    height: 380px;
    background: rgba(10, 14, 26, 0.92);
    backdrop-filter: blur(8px);
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px;
    text-align: center;
    z-index: 10;
    transition: opacity 0.2s ease, visibility 0.2s;
}

.arena-overlay.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.overlay-icon {
    font-size: 42px;
    margin-bottom: 8px;
    animation: bounce 0.6s ease;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

.overlay-title {
    font-size: 24px;
    font-weight: 800;
    margin-bottom: 4px;
}

.overlay-time {
    font-family: 'JetBrains Mono', monospace;
    font-size: 32px;
    font-weight: 800;
    color: #ef4444;
    margin: 8px 0;
    text-shadow: 0 0 12px var(--red-glow);
}

.overlay-rank-badge {
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.2), rgba(217, 119, 6, 0.3));
    border: 1px solid rgba(245, 158, 11, 0.4);
    color: #fde68a;
    font-weight: 700;
    font-size: 13px;
    padding: 4px 12px;
    border-radius: 999px;
    margin-bottom: 14px;
}

.name-input-group {
    width: 100%;
    margin-bottom: 14px;
    text-align: left;
}

.name-input-group label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.input-with-button {
    display: flex;
    gap: 6px;
}

.input-name {
    flex: 1;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px 12px;
    color: #ffffff;
    font-family: var(--font-main);
    font-size: 14px;
    font-weight: 600;
    outline: none;
    transition: all 0.2s;
}

.input-name:focus {
    border-color: var(--red-square);
    background: rgba(255, 255, 255, 0.12);
}

.btn-primary {
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: #ffffff;
    border: none;
    border-radius: 8px;
    padding: 10px 18px;
    font-family: var(--font-main);
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
    transition: all 0.2s;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(239, 68, 68, 0.6);
}

.btn-primary:active {
    transform: translateY(1px);
}

.btn-restart {
    width: 100%;
    padding: 12px;
    font-size: 15px;
    margin-top: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.quick-stats {
    display: flex;
    gap: 16px;
    margin-top: 8px;
}

.quick-stat-box {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 8px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.quick-stat-title {
    font-size: 11px;
    text-transform: uppercase;
    font-weight: 600;
    color: var(--text-muted);
}

.quick-stat-val {
    font-family: 'JetBrains Mono', monospace;
    font-size: 16px;
    font-weight: 700;
    color: #ffffff;
}

/* Leaderboard Sidebar */
.leaderboard-section {
    background: var(--bg-card);
    backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    height: calc(100vh - 120px);
    max-height: 720px;
    position: sticky;
    top: 90px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
    overflow: hidden;
}

.lb-header {
    padding: 18px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.lb-title-area {
    display: flex;
    align-items: center;
    gap: 10px;
}

.lb-icon {
    font-size: 20px;
}

.lb-title {
    font-size: 16px;
    font-weight: 700;
    color: #ffffff;
}

.realtime-pill {
    font-size: 10px;
    text-transform: uppercase;
    font-weight: 800;
    letter-spacing: 0.5px;
    padding: 3px 8px;
    border-radius: 999px;
    background: rgba(16, 185, 129, 0.15);
    color: #34d399;
    border: 1px solid rgba(16, 185, 129, 0.3);
    display: flex;
    align-items: center;
    gap: 5px;
}

.realtime-pulse {
    width: 6px;
    height: 6px;
    background: #10b981;
    border-radius: 50%;
    animation: live-blink 1.2s infinite;
}

/* Podium section for top 3 */
.podium-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 16px;
    background: rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--border-color);
}

.podium-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 10px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    position: relative;
    transition: transform 0.2s;
}

.podium-card:hover {
    transform: translateY(-2px);
}

.podium-card.rank-1 {
    order: 2;
    background: linear-gradient(180deg, rgba(245, 158, 11, 0.15), rgba(245, 158, 11, 0.03));
    border-color: rgba(245, 158, 11, 0.4);
    transform: translateY(-4px);
}

.podium-card.rank-2 {
    order: 1;
    border-color: rgba(148, 163, 184, 0.3);
}

.podium-card.rank-3 {
    order: 3;
    border-color: rgba(217, 119, 6, 0.3);
}

.podium-badge {
    font-size: 18px;
    margin-bottom: 2px;
}

.podium-name {
    font-size: 12px;
    font-weight: 700;
    color: #ffffff;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 2px;
}

.podium-score {
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    font-weight: 800;
    color: #fca5a5;
}

/* Leaderboard list */
.lb-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* Custom scrollbar */
.lb-list::-webkit-scrollbar {
    width: 5px;
}
.lb-list::-webkit-scrollbar-track {
    background: transparent;
}
.lb-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.lb-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid transparent;
    border-radius: 10px;
    transition: all 0.25s ease;
}

.lb-item:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: var(--border-color);
}

.lb-item.is-me {
    background: rgba(239, 68, 68, 0.1);
    border-color: rgba(239, 68, 68, 0.35);
}

.lb-item.newly-added {
    animation: flash-row 1.5s ease-out;
}

@keyframes flash-row {
    0% { background: rgba(16, 185, 129, 0.4); transform: scale(1.02); }
    100% { background: rgba(255, 255, 255, 0.02); transform: scale(1); }
}

.lb-item-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.lb-rank {
    font-size: 12px;
    font-weight: 700;
    color: var(--text-muted);
    width: 24px;
    text-align: center;
}

.lb-user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.lb-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lb-date {
    font-size: 10px;
    color: var(--text-muted);
}

.lb-score-val {
    font-family: 'JetBrains Mono', monospace;
    font-size: 14px;
    font-weight: 700;
    color: #f87171;
    white-space: nowrap;
}

.lb-empty {
    text-align: center;
    padding: 30px 10px;
    color: var(--text-muted);
    font-size: 13px;
}

/* Footer banner in sidebar */
.lb-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    color: var(--text-muted);
}

.hosting-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    color: #38bdf8;
    font-weight: 600;
}

/* Toast notifications */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 1000;
    pointer-events: none;
}

.toast {
    background: rgba(18, 24, 38, 0.95);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--red-square);
    padding: 12px 16px;
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
    color: #ffffff;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 280px;
    animation: slide-in 0.3s ease-out, fade-out 0.3s ease-in 3.7s forwards;
    pointer-events: auto;
}

.toast.record {
    border-left-color: var(--gold);
}

@keyframes slide-in {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

@keyframes fade-out {
    from { opacity: 1; transform: translateY(0); }
    to { opacity: 0; transform: translateY(10px); }
}

/* Responsive */
@media (max-width: 950px) {
    .main-content {
        grid-template-columns: 1fr;
    }
    .leaderboard-section {
        position: static;
        height: auto;
        max-height: 500px;
    }
}

@media (max-width: 500px) {
    .arena-wrapper {
        transform: scale(0.85);
        transform-origin: top center;
    }
    .timer-display {
        font-size: 28px;
    }
    .app-header {
        padding: 12px 16px;
    }
}
