* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden;
    background: #000;
    position: relative;
    user-select: none;
    -webkit-user-select: none;
}

#backgroundContainer { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; z-index: -1; }
.background-layer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity 1s; }
.background-layer.active { opacity: 1; }

#gameCanvas {
    display: block; width: 100vw; height: 100vh;
    cursor: crosshair; background: transparent;
    touch-action: none; 
}

/* === UI PRINCIPAL === */
#ui { position: absolute; top: 20px; left: 20px; z-index: 100; display: flex; flex-direction: column; gap: 15px; max-width: 320px; }

/* Botones Circulares */
#paletteToggle, #maskToggle {
    width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
    cursor: pointer; box-shadow: 0 4px 15px rgba(0,0,0,0.3); font-size: 28px; 
    border: 3px solid white; transition: transform 0.2s, box-shadow 0.2s;
    color: #333; 
}

#paletteToggle { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }
#paletteToggle.open { background: linear-gradient(135deg, #764ba2 0%, #667eea 100%); }

#maskToggle { background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%); color: #555; }
#maskToggle.open { background: linear-gradient(135deg, #fecfef 0%, #ff9a9e 100%); }

#paletteToggle:hover, #maskToggle:hover { transform: scale(1.1); box-shadow: 0 6px 20px rgba(0,0,0,0.4); }

/* Paneles */
#colorPalette, #maskSelector {
    background: rgba(255, 255, 255, 0.95); padding: 25px; border-radius: 24px; 
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    display: none; backdrop-filter: blur(10px); min-width: 220px; 
    animation: slideIn 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}
#colorPalette.open, #maskSelector.open { display: block; }
@keyframes slideIn { from { opacity: 0; transform: translateY(-15px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }

/* Selector de Color */
.color-picker-wrapper { display: flex; justify-content: center; margin-bottom: 20px; }
#colorPicker { 
    width: 65px; height: 65px; border: none; border-radius: 50%; 
    cursor: pointer; padding: 0; overflow: hidden; background: none;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
#colorPicker::-webkit-color-swatch-wrapper { padding: 0; }
#colorPicker::-webkit-color-swatch { border: 4px solid #fff; border-radius: 50%; }

#brushSizeControl { background: #f7fafc; padding: 15px; border-radius: 15px; border: 1px solid #edf2f7; }
#brushSizeSlider { width: 100%; cursor: pointer; accent-color: #667eea; }

/* Lista de Máscaras */
.mask-list { display: flex; flex-direction: column; gap: 12px; max-height: 50vh; overflow-y: auto; padding-right: 5px; }
.mask-list::-webkit-scrollbar { width: 6px; }
.mask-list::-webkit-scrollbar-thumb { background: #cbd5e0; border-radius: 3px; }
.mask-item { 
    border-radius: 12px; overflow: hidden; cursor: pointer; 
    border: 3px solid transparent; transition: all 0.2s; position: relative; flex-shrink: 0; 
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.mask-item img { width: 100%; display: block; transition: transform 0.3s; }
.mask-item:hover img { transform: scale(1.05); }
.mask-item.selected { border-color: #667eea; box-shadow: 0 0 0 3px rgba(102,126,234,0.3); }
.mask-item.locked { filter: grayscale(100%); opacity: 0.7; cursor: not-allowed; }
.mask-item.locked::after { content: '🔒'; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px; text-shadow: 0 2px 5px rgba(0,0,0,0.5); }

/* Botones Superiores */
#topRightActions { position: absolute; top: 20px; right: 20px; display: flex; gap: 12px; z-index: 100; }
#galleryButton, #saveButton { 
    background: white; padding: 12px 24px; border-radius: 30px; 
    font-weight: bold; color: #4a5568; cursor: pointer; 
    box-shadow: 0 4px 15px rgba(0,0,0,0.15); transition: all 0.2s;
    display: flex; align-items: center; gap: 8px; font-size: 15px;
}
#galleryButton:hover, #saveButton:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.2); }
#saveButton { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; }

/* === BARRA INFERIOR (LAYOUT BASE - HORIZONTAL) === */
#bottomBar {
    position: absolute; bottom: 20px; left: 0; width: 100%; padding: 0 20px;
    display: flex; 
    /* IMPORTANTE: Manda los grupos a los extremos absolutos */
    justify-content: space-between; 
    align-items: flex-end;
    pointer-events: none; z-index: 100;
}

.controls-group { display: flex; gap: 15px; pointer-events: auto; }

.control-btn {
    width: 60px; height: 60px; background: rgba(255,255,255,0.95); border-radius: 16px;
    display: flex; align-items: center; justify-content: center; font-size: 26px;
    cursor: pointer; box-shadow: 0 4px 15px rgba(0,0,0,0.15); transition: transform 0.2s, background 0.2s; pointer-events: auto;
    backdrop-filter: blur(5px);
}
.control-btn:hover { transform: translateY(-3px); background: white; }
.control-btn:active { transform: scale(0.95); }
.control-btn.disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* Instrucciones (CENTRADAS ABSOLUTAS EN HORIZONTAL) */
/* Al usar position absolute, no estorban a los botones que se van a los extremos */
#instructions-container {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0; /* Alineadas con los botones */
    
    background: rgba(255, 255, 255, 0.9); 
    padding: 10px 20px; border-radius: 20px;
    text-align: center; font-size: 14px; color: #333; 
    pointer-events: none; white-space: nowrap; backdrop-filter: blur(4px);
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

/* Visibilidad */
.mobile-only, .mobile-text { display: none !important; }
.desktop-text { display: inline; }
body.is-touch .mobile-only { display: flex !important; }
body.is-touch .mobile-text { display: inline !important; } 
body.is-touch .desktop-text { display: none; }

/* === MODALES === */
.modal { 
    display: none; position: fixed; z-index: 200; left: 0; top: 0; width: 100%; height: 100%; 
    background-color: rgba(0,0,0,0.8); backdrop-filter: blur(5px);
    justify-content: center; align-items: center; 
}
.modal.open { display: flex; animation: fadeIn 0.3s; }

.modal-content { 
    background: white; padding: 40px; border-radius: 24px; 
    width: 90%; max-width: 480px; text-align: center; 
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    position: relative;
}

.modal-content h2 { margin-bottom: 10px; color: #2d3748; font-size: 24px; }
.modal-content p { color: #718096; margin-bottom: 25px; line-height: 1.5; }

.modal-content input, .modal-content textarea { 
    width: 100%; padding: 15px; margin: 10px 0; 
    border: 2px solid #e2e8f0; border-radius: 12px; 
    font-size: 16px; transition: border-color 0.2s; font-family: inherit; background: #f7fafc;
}
.modal-content input:focus, .modal-content textarea:focus { 
    border-color: #667eea; outline: none; background: white;
}

.modal-buttons { display: flex; justify-content: space-between; gap: 15px; margin-top: 25px; }

button { 
    padding: 12px 24px; border: none; border-radius: 12px; cursor: pointer; 
    font-weight: 600; font-size: 16px; font-family: inherit;
    background-color: #edf2f7; color: #4a5568; transition: all 0.2s; flex: 1;
}
button:hover { background-color: #e2e8f0; transform: translateY(-1px); }

.primary-btn { 
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
    color: white; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.primary-btn:hover { 
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%); 
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.6); transform: translateY(-2px);
}

/* === GALERÍA === */
.gallery-content { max-width: 800px; max-height: 85vh; padding: 30px; }

.gallery-grid { 
    display: grid; 
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); 
    gap: 20px; margin-top: 20px; 
    overflow-y: auto; padding: 10px; min-height: 100px; 
    max-height: 60vh; 
}

.gallery-grid::-webkit-scrollbar { width: 8px; }
.gallery-grid::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
.gallery-grid::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }

.gallery-item { 
    border: 1px solid #e2e8f0; padding: 10px; border-radius: 16px; 
    background: #fff; position: relative; 
    box-shadow: 0 4px 6px rgba(0,0,0,0.05); transition: transform 0.3s, box-shadow 0.3s;
}
.gallery-item:hover { transform: translateY(-5px); box-shadow: 0 15px 30px rgba(0,0,0,0.1); }

.gallery-img-container { 
    width: 100%; 
    height: 160px; 
    overflow: hidden; border-radius: 10px; 
    /* Gradiente radial: Claro centro (#666), Oscuro orillas (#111) */
    background: radial-gradient(circle, #666 0%, #111 100%);
    display: flex; align-items: center; justify-content: center; margin-bottom: 10px;
    border: 1px solid #444; 
}

.gallery-item img { 
    width: 100%; height: 100%; object-fit: contain; 
    filter: drop-shadow(0 5px 15px rgba(0,0,0,0.5)); 
}

.gallery-item h4 { font-size: 16px; margin: 0; color: #2d3748; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gallery-item small { display: block; color: #a0aec0; font-size: 12px; margin-bottom: 10px; }

.delete-card-btn { 
    position: absolute; top: -10px; right: -10px; width: 32px; height: 32px; 
    background: #fc8181; color: white; border-radius: 50%; 
    display: flex; align-items: center; justify-content: center; 
    cursor: pointer; border: 3px solid white; box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    font-weight: bold; transition: transform 0.2s, background 0.2s;
}
.delete-card-btn:hover { transform: scale(1.1); background: #f56565; }

.share-insta-btn { 
    width: 100%; padding: 10px; margin-top: 5px;
    background: linear-gradient(45deg, #f09433 0%, #e6683c 25%, #dc2743 50%, #cc2366 75%, #bc1888 100%); 
    color: white; font-size: 13px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center; gap: 5px;
}
.share-insta-btn:hover { opacity: 0.95; transform: translateY(-1px); }

.close-modal { position: absolute; top: 15px; right: 20px; font-size: 32px; cursor: pointer; color: #cbd5e0; transition: color 0.2s; z-index: 5; }
.close-modal:hover { color: #4a5568; }

#toast { 
    visibility: hidden; position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%); 
    background: rgba(30, 30, 30, 0.9); color: white; padding: 16px 30px; 
    border-radius: 50px; z-index: 1000; font-weight: 500;
    box-shadow: 0 10px 20px rgba(0,0,0,0.2); backdrop-filter: blur(5px);
    opacity: 0; transition: opacity 0.5s, bottom 0.5s;
}
#toast.show { visibility: visible; opacity: 1; bottom: 50px; }

/* === MÓVIL / PORTRAIT (CAMBIO TOTAL DE LAYOUT) === */
@media (orientation: portrait) {
    #bottomBar { 
        justify-content: space-evenly; /* Equidistantes en toda la barra */
        padding: 0 5px; 
        flex-wrap: wrap; 
    }
    
    .controls-group { display: contents; /* Desaparecen visualmente para distribuir hijos */ }
    
    #instructions-container {
        /* Regresamos al flujo normal para que se vaya arriba */
        position: static; 
        transform: none;
        order: -1; 
        
        width: 90%;
        margin-bottom: 15px;
        white-space: normal;
    }

    .control-btn { width: 14vw; height: 14vw; max-width: 60px; max-height: 60px; font-size: 22px; }
    #ui { transform: scale(0.9); transform-origin: top left; }
    
    /* MODALES MÓVIL */
    .modal-content { padding: 25px; width: 95%; max-height: 90vh; overflow-y: auto; }
    .modal-buttons { flex-direction: column-reverse; } 
    button { width: 100%; padding: 15px; }

    /* GALERÍA MÓVIL */
    .gallery-grid { grid-template-columns: 1fr; gap: 15px; }
    .gallery-item { margin: 0 auto; width: 100%; max-width: 300px; }
    .gallery-img-container { height: 200px; }
    .gallery-item h4 { font-size: 14px; }
    .share-insta-btn { font-size: 11px; padding: 8px; }
}