:root {
    --bg-dark: #0f172a;
    --bg-panel: #1e293b;
    --primary: #3b82f6;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border: #334155;

    --p1-color: #ef4444;
    /* Red */
    --p2-color: #3b82f6;
    /* Blue */
    --p3-color: #a855f7;
    /* Purple */
    --p4-color: #22c55e;
    /* Green */
    --p5-color: #eab308;
    /* Yellow */
    --p6-color: #ec4899;
    /* Pink */
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-main);
    height: 100vh;
    height: 100dvh;
    /* Dynamic Viewport Height for mobile */
    overflow: hidden;
    /* Prevent pull-to-refresh on mobile */
    overscroll-behavior: none;
}

.app-container {
    display: flex;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: 320px;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.logo h1 {
    font-weight: 600;
    font-size: 2rem;
    letter-spacing: -1px;
}

.logo span {
    color: var(--primary);
}

.logo p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.lang-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    cursor: pointer;
    margin-top: 5px;
}

.lang-btn:hover {
    color: var(--text-main);
    border-color: var(--text-muted);
}

.control-group h3 {
    text-transform: uppercase;
    font-size: 0.75rem;
    color: var(--text-muted);
    letter-spacing: 1px;
    margin-bottom: 1rem;
}

/* Upload Button */
.upload-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    padding: 1rem;
    background: linear-gradient(135deg, var(--primary), #2563eb);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s ease;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.upload-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
}

/* Updated Grid Player List */
.group-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    margin-bottom: 0.25rem;
    /* Reduced from 0.5rem */
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 0.8rem;
    cursor: pointer;
    transition: transform 0.2s;
}

.merged-list.grid-layout {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    /* Reduced from 8px */
}

.player-card {
    background: rgba(255, 255, 255, 0.05);
    border: 2px solid transparent;
    border-radius: 6px;
    /* slightly smaller radius */
    padding: 0.2rem;
    /* Reduced from 0.4rem */
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    /* Reduced from 4px */
    cursor: pointer;
    transition: all 0.2s;
}

.player-card:hover {
    background: rgba(255, 255, 255, 0.1);
}

.player-card.active {
    background: rgba(255, 255, 255, 0.15);
}

.card-color {
    width: 16px;
    /* Reduced from 20px */
    height: 16px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.card-score {
    font-size: 1rem;
    /* Reduced from 1.2rem */
    font-weight: 700;
}

/* Summary Bar */
.summary-bar {
    display: none;
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid var(--border);
    padding: 0.8rem;
    border-radius: 8px;
    align-items: center;
    justify-content: space-around;
    cursor: pointer;
}

.control-group.collapsed .collapsible-content {
    display: none;
}

.control-group.collapsed .summary-bar {
    display: flex;
}

.summary-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.summary-item span {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.summary-item b {
    font-size: 1.2rem;
}

.winner-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 700;
    font-size: 1.2rem;
}

.summary-vl {
    width: 1px;
    height: 30px;
    background: var(--border);
}

.color-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.list-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    font-size: 0.8rem;
}

.sm-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 2px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.7rem;
}

.sm-btn:hover {
    color: var(--text-main);
    border-color: var(--text-muted);
}

.spacer {
    flex: 1;
}

/* Status Bar */
.status-bar {
    margin-top: auto;
    font-size: 0.85rem;
    color: var(--text-muted);
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

/* Canvas Area */
.canvas-area {
    flex: 1;
    background-color: #000;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.canvas-wrapper {
    flex: 1;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    background-color: #050505;
}

canvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    transform-origin: 0 0;
    /* Changed from center center for pan/zoom logic */
    cursor: grab;
    touch-action: none;
}

canvas:active {
    cursor: grabbing;
}

.canvas-controls {
    position: absolute;
    bottom: 20px;
    right: 20px;
    display: flex;
    gap: 10px;
    z-index: 100;
}

.canvas-controls button {
    background: rgba(30, 41, 59, 0.8);
    color: var(--text-main);
    border: 1px solid var(--border);
    padding: 8px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    backdrop-filter: blur(4px);
    transition: all 0.2s;
}

.canvas-controls button:hover {
    background: var(--primary);
    border-color: var(--primary);
}

/* Loading Overlay */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 500;
    color: white;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-top: 4px solid var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Annotation Editor */
.annotation-editor {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 280px;
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    z-index: 200;
    backdrop-filter: blur(8px);
    transition: opacity 0.2s, transform 0.2s;
}

.annotation-editor.hidden {
    opacity: 0;
    transform: translateY(-10px);
    pointer-events: none;
    display: block !important;
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.5rem;
}

.editor-header h4 {
    font-size: 0.95rem;
    color: var(--text-main);
}

.editor-header button {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
}

.editor-row {
    margin-bottom: 1rem;
}

.editor-row label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.color-picker {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.color-swatch {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.2s;
}

.color-swatch:hover {
    transform: scale(1.1);
}

.color-swatch.selected {
    border-color: #fff;
    box-shadow: 0 0 0 2px var(--primary);
}

.score-buttons {
    display: flex;
    gap: 8px;
}

.score-btn {
    flex: 1;
    padding: 8px;
    background: var(--bg-dark);
    border: 1px solid var(--border);
    color: var(--text-muted);
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s;
}

.score-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-main);
}

.score-btn.selected {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.editor-actions {
    display: flex;
    gap: 10px;
    margin-top: 1.5rem;
}

.editor-actions button {
    flex: 1;
    padding: 8px;
    border-radius: 4px;
    border: none;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
}

.editor-actions .danger {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
    border: 1px solid #ef4444;
}

.editor-actions .danger:hover {
    background: #ef4444;
    color: white;
}

.editor-actions .primary {
    background: var(--primary);
    color: white;
}

.empty-state {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: var(--text-muted);
    pointer-events: none;
}

.empty-state .icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

.hidden {
    display: none !important;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        padding: 1rem;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 1rem;
        border-right: none;
        border-bottom: 1px solid var(--border);
        overflow-y: auto;
        max-height: 40vh;
        /* Allowed more space */
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    }

    .sidebar>* {
        flex: 1 1 auto;
        min-width: 140px;
    }

    .logo {
        display: none;
    }

    /* Hide logo on mobile to save space */
    .status-bar {
        display: none;
    }

    .canvas-area {
        height: 60vh;
    }

    /* Give canvas remaining space */

    .annotation-editor {
        width: 90%;
        left: 5%;
        right: auto;
        top: 10px;
    }

    .canvas-controls {
        bottom: 80px;
        padding-bottom: env(safe-area-inset-bottom);
    }
}