/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: #1a1a1a;
    color: #fff;
    overflow: hidden;
    touch-action: manipulation;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Header */
:root {
    --header-height: 50px;
    --mobile-controls-height: 60px;
    --z-sidebar-overlay: 998;
    --z-sidebar: 999;
    --z-mobile-controls: 100;
    --z-loading: 2000;
}

.header {
    background-color: #2a2a2a;
    padding: 0.5rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #3a3a3a;
    flex-wrap: wrap;
    min-height: var(--header-height);
}

.header h1 {
    font-size: clamp(1.2rem, 3vw, 1.5rem);
    color: #fff;
}

.stats {
    display: flex;
    gap: clamp(0.5rem, 2vw, 2rem);
    font-size: clamp(0.7rem, 1.5vw, 0.9rem);
    flex-wrap: wrap;
}

.stats span {
    color: #888;
    white-space: nowrap;
}

.stats span span {
    color: #fff;
    font-weight: bold;
}

/* Main Content */
.main-content {
    display: flex;
    height: calc(100vh - var(--header-height, 50px));
    height: calc(100dvh - var(--header-height, 50px));
}

/* Canvas Container */
.canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background-color: #0a0a0a;
    cursor: crosshair;
}

.canvas-container.cooldown-active {
    cursor: not-allowed;
}

#pixelCanvas {
    position: absolute;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
}

/* Zoom Controls */
.zoom-controls {
    position: absolute;
    bottom: 20px;
    left: 20px;
    display: flex;
    gap: 10px;
    background-color: rgba(42, 42, 42, 0.9);
    padding: 10px;
    border-radius: 8px;
}

.zoom-controls button {
    width: 40px;
    height: 40px;
    border: none;
    background-color: #3a3a3a;
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1.2rem;
    transition: background-color 0.2s;
    padding: 0;
    overflow: hidden;
}

.zoom-controls button#zoomReset {
    width: auto;
    padding: 0 10px;
    font-size: 0.9rem;
}

.zoom-controls button:hover {
    background-color: #4a4a4a;
}

/* Sidebar */
.sidebar {
    width: min(300px, 30vw);
    background-color: #2a2a2a;
    padding: 1rem;
    overflow-y: auto;
    overflow-x: hidden;
    border-left: 2px solid #3a3a3a;
    flex-shrink: 0;
    scrollbar-width: thin;
    scrollbar-color: #4a4a4a #2a2a2a;
}

.sidebar::-webkit-scrollbar {
    width: 8px;
}

.sidebar::-webkit-scrollbar-track {
    background: #2a2a2a;
}

.sidebar::-webkit-scrollbar-thumb {
    background: #4a4a4a;
    border-radius: 4px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: #5a5a5a;
}

.tool-section {
    margin-bottom: 2rem;
    padding: 1rem;
    background-color: #1a1a1a;
    border-radius: 8px;
}

.tool-section h3 {
    margin-bottom: 1rem;
    font-size: 1.1rem;
    color: #fff;
}

/* Color Picker */
#colorPicker {
    width: 100%;
    height: 50px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.color-palette {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(28px, 1fr));
    gap: 4px;
    margin-top: 10px;
    max-width: 100%;
}

.color-option {
    aspect-ratio: 1;
    min-width: 28px;
    border: 2px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    transition: border-color 0.2s;
}

.color-option:hover {
    border-color: #fff;
}

.color-option.selected {
    border-color: #4CAF50;
}

/* Cooldown */
.cooldown-bar {
    width: 100%;
    height: 20px;
    background-color: #3a3a3a;
    border-radius: 10px;
    overflow: hidden;
    margin-top: 10px;
}

.cooldown-progress {
    height: 100%;
    background-color: #4CAF50;
    transition: width 0.1s linear;
}

/* Ad Container */
.ad-container {
    margin-top: 2rem;
}

.ad-placeholder {
    width: 100%;
    height: 250px;
    background-color: #3a3a3a;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    color: #666;
}

/* Mobile Controls */
.mobile-controls {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #2a2a2a;
    padding: 1rem;
    border-top: 2px solid #3a3a3a;
}

.mobile-controls button {
    flex: 1;
    padding: 0.8rem;
    border: none;
    background-color: #3a3a3a;
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
    margin: 0 0.5rem;
}

/* Mobile overlay for sidebar */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 998;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .sidebar {
        width: 250px;
    }
    
    .color-palette {
        grid-template-columns: repeat(auto-fill, minmax(25px, 1fr));
    }
}

/* Tablet */
@media (max-width: 900px) {
    .stats {
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .tool-section {
        padding: 0.9rem;
    }
}

@media (max-width: 768px) {
    .header {
        padding: 0.5rem;
        gap: 0.5rem;
    }
    
    .stats {
        width: 100%;
        justify-content: space-around;
    }
    
    .main-content {
        flex-direction: column;
        height: calc(100vh - var(--header-height) - var(--mobile-controls-height));
        height: calc(100dvh - var(--header-height) - var(--mobile-controls-height));
    }
    
    .canvas-container {
        flex: 1;
        min-height: 50vh;
    }
    
    .sidebar {
        display: none;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        z-index: var(--z-sidebar);
        width: 85%;
        max-width: 350px;
        box-shadow: -2px 0 10px rgba(0, 0, 0, 0.5);
        overflow-y: auto;
        overflow-x: hidden;
    }
    
    .sidebar.show {
        display: block;
    }
    
    .sidebar-overlay.show {
        display: block;
    }
    
    .mobile-controls {
        display: flex;
        gap: 0.5rem;
        z-index: var(--z-mobile-controls);
        height: var(--mobile-controls-height);
    }
    
    .zoom-controls {
        bottom: 70px;
        left: 10px;
        padding: 5px;
    }
    
    .zoom-controls button {
        width: 35px;
        height: 35px;
        font-size: 1rem;
    }
    
    .tool-section {
        padding: 0.8rem;
        margin-bottom: 1rem;
    }
    
    #pixelInfo {
        font-size: 0.9rem;
    }
    
    #pixelInfo p {
        margin: 0.3rem 0;
    }
    
    .ad-placeholder {
        height: 150px;
    }
}

@media (max-width: 480px) {
    .header h1 {
        font-size: 1.1rem;
    }
    
    .stats span {
        font-size: 0.65rem;
    }
    
    .mobile-controls button {
        font-size: 0.9rem;
        padding: 0.8rem;
        min-width: 44px; /* Better touch target */
        min-height: 44px;
    }
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-loading);
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 3px solid #3a3a3a;
    border-top-color: #4CAF50;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Nickname Edit */
.nickname-container {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.edit-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
    padding: 0 0.3rem;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.edit-btn:hover {
    opacity: 1;
}

#nicknameEditForm {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

/* Mobile improvements for nickname edit */
@media (max-width: 768px) {
    #nicknameEditForm {
        flex-wrap: wrap;
    }
    
    #nicknameInput {
        width: 100%;
        font-size: 16px; /* Prevent zoom on iOS */
    }
    
    #saveNicknameBtn, #cancelNicknameBtn {
        flex: 1;
        padding: 0.6rem 1rem;
        font-size: 0.95rem;
    }
}

#nicknameInput {
    flex: 1;
    padding: 0.4rem;
    background: #3a3a3a;
    border: 1px solid #4a4a4a;
    color: #fff;
    border-radius: 4px;
    font-size: 0.9rem;
}

#nicknameInput:focus {
    outline: none;
    border-color: #4CAF50;
}

#saveNicknameBtn, #cancelNicknameBtn {
    padding: 0.4rem 0.8rem;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: background-color 0.2s;
}

#saveNicknameBtn {
    background: #4CAF50;
    color: #fff;
}

#saveNicknameBtn:hover {
    background: #45a049;
}

#cancelNicknameBtn {
    background: #666;
    color: #fff;
}

#cancelNicknameBtn:hover {
    background: #555;
}

/* Pixel Tooltip */
.pixel-tooltip {
    position: fixed;
    background: rgba(42, 42, 42, 0.95);
    color: #fff;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
    pointer-events: none;
    z-index: 1000;
    display: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    border: 1px solid #3a3a3a;
    backdrop-filter: blur(4px);
}

.tooltip-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.tooltip-content div {
    white-space: nowrap;
}

/* Mobile tooltip - show as fixed panel */
@media (max-width: 768px) {
    .pixel-tooltip {
        position: fixed;
        bottom: calc(var(--mobile-controls-height) + 10px);
        left: 10px;
        right: 10px;
        max-width: none;
        font-size: 1rem;
        padding: 16px;
        background: rgba(42, 42, 42, 0.98);
        animation: slideUp 0.2s ease-out;
    }
    
    @keyframes slideUp {
        from {
            transform: translateY(20px);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }
}

/* Connection Status Animations */
@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

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

/* Connection Status Notification */
.connection-status {
    transition: all 0.3s ease;
}

/* Connection Error Banner */
#connectionError {
    font-weight: 500;
    font-size: 0.95rem;
    box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}

/* Connection Status Indicator */
.connection-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    background: rgba(255,255,255,0.1);
    border-radius: 20px;
    font-size: 0.85rem;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ffc107;
    transition: background-color 0.3s ease;
}

.connection-indicator.connected .status-dot {
    background: #4CAF50;
    animation: pulse 2s infinite;
}

.connection-indicator.disconnected .status-dot {
    background: #f44336;
}

.connection-indicator.connecting .status-dot {
    background: #ffc107;
    animation: blink 1s infinite;
}

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

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0.3; }
}