/* CSS Reset and Variables */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-card: rgba(30, 41, 59, 0.5);
    --bg-card-hover: rgba(51, 65, 85, 0.5);
    --border-color: rgba(148, 163, 184, 0.1);
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent-cyan: #38bdf8;
    --accent-purple: #818cf8;
    --accent-gradient: linear-gradient(135deg, #38bdf8 0%, #818cf8 100%);
    --glow-cyan: 0 0 16px rgba(56, 189, 248, 0.25);
    --glow-purple: 0 0 16px rgba(129, 140, 248, 0.25);
    --transition-fast: 0.15s ease;
    --transition-normal: 0.3s ease;
    --transition-slow: 0.5s ease;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

html, body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* Dashboard Layout */
.dashboard {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background:
        radial-gradient(ellipse at top left, rgba(56, 189, 248, 0.04) 0%, transparent 50%),
        radial-gradient(ellipse at bottom right, rgba(129, 140, 248, 0.04) 0%, transparent 50%),
        var(--bg-primary);
}

/* Header */
.header {
    padding: 1rem 2rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-card);
    backdrop-filter: blur(10px);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1800px;
    margin: 0 auto;
}

.title {
    font-size: 1.5rem;
    font-weight: 600;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.slot-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.slot-label {
    color: var(--text-secondary);
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.slot-number {
    font-size: 1.5rem;
    font-weight: 700;
    font-family: 'SF Mono', 'Fira Code', monospace;
    color: var(--accent-cyan);
    text-shadow: var(--glow-cyan);
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    gap: 1.5rem;
    padding: 1.5rem 2rem;
    overflow: hidden;
    max-width: 1800px;
    margin: 0 auto;
    width: 100%;
}

/* Chart Container */
.chart-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    overflow: hidden;
    backdrop-filter: blur(10px);
}

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

.timer-section {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.timer-ring {
    position: relative;
    width: 48px;
    height: 48px;
}

.timer-ring svg {
    transform: rotate(-90deg);
    width: 100%;
    height: 100%;
}

.timer-bg {
    fill: none;
    stroke: var(--border-color);
    stroke-width: 4;
}

.timer-progress {
    fill: none;
    stroke: url(#timerGradient);
    stroke-width: 4;
    stroke-linecap: round;
    stroke-dasharray: 283;
    stroke-dashoffset: 0;
    transition: stroke-dashoffset 0.1s linear;
}

.timer-ring svg defs {
    display: block;
}

.timer-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 0.875rem;
    font-weight: 700;
    font-family: 'SF Mono', 'Fira Code', monospace;
    color: var(--text-primary);
}

.timer-label {
    color: var(--text-secondary);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Winning Builder Tile */
.winning-builder {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.875rem;
    margin-left: 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.winning-builder.has-winner {
    border-color: rgba(56, 189, 248, 0.3);
    background: rgba(56, 189, 248, 0.08);
}

.winning-builder.is-delivered {
    border-color: rgba(251, 191, 36, 0.5);
    background: rgba(251, 191, 36, 0.12);
    box-shadow: 0 0 12px rgba(251, 191, 36, 0.2);
}

.winning-builder.is-delivered .winning-value {
    color: #fbbf24;
}

.winning-label {
    font-size: 0.625rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.winning-color {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
    transition: all var(--transition-fast);
}

.winning-color.active {
    box-shadow: 0 0 8px currentColor;
}

.winning-name {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text-primary);
    max-width: 120px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.winning-value {
    font-size: 0.75rem;
    font-family: 'SF Mono', 'Fira Code', monospace;
    color: var(--accent-cyan);
    margin-left: 0.25rem;
}

/* Controls */
.controls {
    display: flex;
    gap: 0.5rem;
}

.control-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.control-btn:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
    border-color: var(--accent-cyan);
}

.control-btn:active {
    transform: scale(0.95);
}

.control-btn svg {
    width: 20px;
    height: 20px;
}

.play-btn {
    width: 48px;
    background: var(--accent-gradient);
    border: none;
    color: white;
}

.play-btn:hover {
    box-shadow: var(--glow-cyan);
    border-color: transparent;
}

.play-btn .pause-icon {
    display: none;
}

.play-btn.playing .play-icon {
    display: none;
}

.play-btn.playing .pause-icon {
    display: block;
}

/* Chart Wrapper */
.chart-wrapper {
    flex: 1;
    padding: 1rem;
    min-height: 0;
}

#chart {
    width: 100%;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: 280px;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    flex-shrink: 0;
}

/* Cards */
.stats-card, .legend-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.25rem;
    backdrop-filter: blur(10px);
}

.stats-card h3, .legend-card h3 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-secondary);
    margin-bottom: 1rem;
}

/* Relays (in chart header) */
.relays-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1;
    justify-content: center;
    align-items: center;
    padding: 0 1rem;
}

.relay-tile {
    display: inline-block;
    padding: 0.1875rem 0.5rem;
    font-size: 0.625rem;
    font-weight: 500;
    color: var(--accent-cyan);
    background: rgba(56, 189, 248, 0.1);
    border: 1px solid rgba(56, 189, 248, 0.2);
    border-radius: 4px;
    white-space: nowrap;
    transition: all var(--transition-fast);
}

.relay-tile:hover {
    background: rgba(56, 189, 248, 0.15);
    border-color: rgba(56, 189, 248, 0.3);
}

.relay-empty {
    font-size: 0.6875rem;
    color: var(--text-muted);
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border-color);
}

.stat-item:last-child {
    border-bottom: none;
}

.stat-label {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.stat-value {
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-weight: 600;
    color: var(--text-primary);
}

/* Legend */
.legend-card {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.legend-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
}

.legend-item:hover {
    background: var(--bg-card-hover);
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: 0 0 8px currentColor;
}

.legend-name {
    font-size: 0.8125rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.legend-count {
    margin-left: auto;
    font-size: 0.75rem;
    font-family: 'SF Mono', 'Fira Code', monospace;
    color: var(--text-muted);
    padding: 0.125rem 0.375rem;
    background: var(--bg-secondary);
    border-radius: 4px;
}

.legend-item.is-delivered {
    background: rgba(251, 191, 36, 0.1);
    border: 1px solid rgba(251, 191, 36, 0.3);
}

.legend-item.is-delivered .legend-color {
    box-shadow: 0 0 10px #fbbf24;
}

.delivered-star {
    color: #fbbf24;
    margin-right: 0.25rem;
    font-size: 0.875rem;
}

/* Status Bar */
.status-bar {
    padding: 0.5rem 2rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-card);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.footer-credits {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.footer-sep {
    opacity: 0.4;
}

.built-by .heart {
    color: #ef4444;
}

.built-by a,
.powered-by a {
    color: var(--accent-cyan);
    text-decoration: none;
    font-weight: 500;
    transition: color var(--transition-fast);
}

.built-by a:hover,
.powered-by a:hover {
    color: var(--accent-purple);
    text-decoration: underline;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: pulse 2s infinite;
}

.status-indicator.connected .status-dot {
    background: #34d399;
}

.status-indicator.error .status-dot {
    background: #f87171;
    animation: none;
}

.status-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

/* Loading State */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-cyan);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Responsive */
@media (max-width: 1024px) {
    .main-content {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        flex-direction: row;
    }

    .stats-card, .legend-card {
        flex: 1;
    }

    .legend-card {
        max-height: 200px;
    }
}

@media (max-width: 768px) {
    .header {
        padding: 1rem;
    }

    .main-content {
        padding: 1rem;
    }

    .sidebar {
        flex-direction: column;
    }

    .title {
        font-size: 1.25rem;
    }

    .slot-number {
        font-size: 1.25rem;
    }

    .relays-list {
        display: none;
    }
}

/* Transitions for smooth updates */
.slot-number, .stat-value {
    transition: all var(--transition-normal);
}

.chart-wrapper {
    animation: fadeIn var(--transition-slow);
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background:
        radial-gradient(ellipse at top left, rgba(56, 189, 248, 0.06) 0%, transparent 50%),
        radial-gradient(ellipse at bottom right, rgba(129, 140, 248, 0.06) 0%, transparent 50%),
        var(--bg-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

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

.loading-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.loading-logo {
    position: relative;
    width: 120px;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.loading-rings {
    position: absolute;
    width: 100%;
    height: 100%;
}

.ring {
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 50%;
    border: 2px solid transparent;
    transform: translate(-50%, -50%);
}

.ring-1 {
    width: 100%;
    height: 100%;
    border-top-color: var(--accent-cyan);
    border-right-color: var(--accent-cyan);
    animation: spin 1.5s linear infinite;
}

.ring-2 {
    width: 75%;
    height: 75%;
    border-bottom-color: var(--accent-purple);
    border-left-color: var(--accent-purple);
    animation: spin 1.2s linear infinite reverse;
}

.ring-3 {
    width: 50%;
    height: 50%;
    border-top-color: var(--accent-cyan);
    animation: spin 0.9s linear infinite;
}

.loading-icon {
    position: relative;
    z-index: 1;
    width: 32px;
    height: 32px;
    color: var(--accent-cyan);
    animation: pulse-glow 2s ease-in-out infinite;
}

.loading-icon svg {
    width: 100%;
    height: 100%;
}

@keyframes pulse-glow {
    0%, 100% {
        filter: drop-shadow(0 0 8px var(--accent-cyan));
        transform: scale(1);
    }
    50% {
        filter: drop-shadow(0 0 20px var(--accent-cyan));
        transform: scale(1.1);
    }
}

.loading-title {
    font-size: 1.75rem;
    font-weight: 600;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.loading-status {
    color: var(--text-secondary);
    font-size: 0.9375rem;
    min-height: 1.5em;
    transition: opacity 0.3s ease;
}

.loading-progress {
    width: 200px;
    height: 3px;
    background: var(--border-color);
    border-radius: 2px;
    overflow: hidden;
}

.loading-progress-bar {
    height: 100%;
    width: 0%;
    background: var(--accent-gradient);
    border-radius: 2px;
    transition: width 0.3s ease;
}
