/* Reset y configuración base PWA */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html {
    overscroll-behavior-y: none; /* Previene el pull-to-refresh en todos los navegadores móviles */
    overscroll-behavior-x: none;
}

body {
    width: 100vw;
    min-height: 100dvh; /* Dynamic Viewport Height para móviles (soluciona barra de navegación) */
    overflow-x: hidden;
    overflow-y: auto;
    background-color: #e0f7fa; 
    font-family: 'Comic Sans MS', 'Chalkboard SE', sans-serif;
    overscroll-behavior: none; /* BLOQUEO DE PULL-TO-REFRESH SIN BLOQUEAR SCROLL LOCAL */
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
}

#app-container {
    width: 100%;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    padding: 10px;
    gap: 15px;
}

/* Lienzo compuesto en capas */
#workspace {
    flex-grow: 1;
    min-height: 45vh; /* Asegurar que la pizarra tenga un minimo de alto utilizable en móviles apaisados */
    position: relative;
    border: 8px solid #FF9500;
    border-radius: 20px;
    box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
    background-color: #FFFFFF;
    overflow: hidden; /* Asegurar que ningún sticker desborde la pizarra visualmente */
}

#drawingBoard {
    width: 100%;
    height: 100%;
    display: block; /* remueve el pixel extra de alineamiento inline */
    cursor: crosshair;
    touch-action: none; /* Canvas mantiene su propio touch-action: none */
}

#stickers-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none !important; /* La pizarra captura eventos normales */
}

/* Modos del espacio de trabajo guiando cursores y eventos */
.hand-mode-active #stickers-container {
    pointer-events: auto !important; /* Activamos la interacción frontal */
    z-index: 50; /* Forzamos a que esté por encima de la pizarra de dibujo */
}
.hand-mode-active #drawingBoard {
    cursor: default; /* desactivamos cursor de dibujo manual */
    pointer-events: none; /* Prevenimos interferencias táctiles con la pizarra en modo mano */
}

/* Estilos individuales del DOM Sticker */
.sticker {
    position: absolute;
    user-select: none;
    touch-action: none;
    transform-origin: center;
    /* el arrastre sin 'transition' lo hace más directo a 60fps */
    pointer-events: none; /* explicitamente inactivo a menos que sea modo mano */
    padding: 20px; /* agrandar el hitbox táctil */
    margin: -20px; /* compensar visualmente el padding extra */
    border-radius: 20px;
    border: 3px solid transparent;
    transition: background-color 0.2s, border-color 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
}
.hand-mode-active .sticker {
    cursor: grab;
    filter: drop-shadow(0 4px 5px rgba(0,0,0,0.3)); /* resaltar agarrable */
    pointer-events: auto !important; /* explicitamente activo */
    background-color: rgba(255, 255, 255, 0.6); /* Hacer visible la caja táctil */
    border-color: #FF9500; /* Borde naranja punteado para ver donde tocar */
    border-style: dashed;
}
.hand-mode-active .sticker:active {
    cursor: grabbing;
    filter: drop-shadow(0 8px 12px rgba(0,0,0,0.4)); /* efecto elevar */
}

/* Contenedor principal de pegatinas flotante */
#sticker-picker-container,
#template-picker-container {
    position: absolute;
    bottom: 10px;
    right: 10px;
    background: rgba(255,255,255,0.9);
    padding: 5px;
    border-radius: 25px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    gap: 5px;
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: auto;
    max-width: calc(100% - 20px);
}
#sticker-picker-container.hidden,
#template-picker-container.hidden {
    opacity: 0;
    transform: translateY(20px);
    pointer-events: none;
}

#sticker-picker,
#template-picker {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 5px;
    scroll-behavior: smooth;
    max-width: 60vw;
}
#sticker-picker::-webkit-scrollbar,
#template-picker::-webkit-scrollbar {
    display: none;
}

.sticker-scroll-btn {
    background: none;
    border: none;
    font-size: 28px;
    color: #FF9500;
    cursor: pointer;
    padding: 0 5px;
    user-select: none;
    touch-action: manipulation;
}
.sticker-scroll-btn:active {
    transform: scale(0.8);
}
.sticker-option,
.template-option {
    font-size: 35px;
    cursor: pointer;
    transition: transform 0.1s;
    filter: grayscale(0.5);
    flex-shrink: 0; /* Evita que salgan de golpe estrujadas, permitiendo el scroll horizontal real */
}
.template-option {
    border-radius: 10px;
    background: white;
    width: 60px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 2px solid #ccc;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.template-option img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}
.sticker-option.selected,
.template-option.selected {
    transform: scale(1.3);
    filter: grayscale(0);
}
.template-option.selected {
    border-color: #FF9500;
}

/* Barras de herramientas */
#controls-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex-shrink: 0;
}
.toolbar {
    min-height: 85px;
    height: auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    background-color: #FFFFFF;
    border-radius: 25px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    padding: 10px;
    gap: 12px;
}

button {
    border: none;
    outline: none;
    cursor: pointer;
    transition: transform 0.1s ease, box-shadow 0.1s ease, border 0.1s ease;
    flex-shrink: 0;
}

.thickness-tools {
    display: flex;
    gap: 5px;
    background: #f0f0f0;
    padding: 5px 10px;
    border-radius: 35px;
    align-items: center;
    height: 70px;
}
.thickness-btn {
    width: 55px;
    height: 55px;
    border-radius: 50%;
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 3px 0px rgba(0,0,0,0.1);
}
.thickness-btn.active {
    background-color: #e0e0e0;
    box-shadow: inset 0 3px 5px rgba(0,0,0,0.1);
    transform: translateY(2px);
}
.thickness-btn .dot {
    background-color: #333;
    border-radius: 50%;
}

.color-btn {
    width: 65px;
    height: 65px;
    border-radius: 50%;
    box-shadow: 0 5px 0px rgba(0,0,0,0.15), 0 8px 8px rgba(0,0,0,0.1);
    border: 4px solid white;
    position: relative;
}
.color-btn.active {
    transform: translateY(4px) scale(1.1);
    box-shadow: 0 2px 0px rgba(0,0,0,0.15), 0 4px 4px rgba(0,0,0,0.1);
    border: 4px solid #333; 
}
.color-btn:active {
    transform: translateY(5px);
    box-shadow: none;
}
.custom-color-btn {
    background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
    overflow: hidden;
}

.tool-btn {
    width: 65px;
    height: 65px;
    border-radius: 50%;
    background-color: white;
    font-size: 32px;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 5px 0px rgba(0,0,0,0.1), 0 8px 8px rgba(0,0,0,0.1);
    border: 4px solid white;
}
.tool-btn.active {
    border-color: #333;
    transform: translateY(4px) scale(1.1);
    box-shadow: 0 2px 0px rgba(0,0,0,0.15);
}
.tool-btn:active {
    transform: translateY(5px);
    box-shadow: none;
}

.action-btn {
    width: 65px;
    height: 65px;
    border-radius: 50%;
    background: radial-gradient(circle, #ff4e50, #f9d423); 
    font-size: 32px;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 5px 0px #b71c1c, 0 8px 8px rgba(0,0,0,0.15);
}
.action-btn:active {
    transform: translateY(5px);
    box-shadow: none;
}

@media (min-width: 768px) {
    #app-container {
        padding: 20px;
        gap: 20px;
        max-width: 1024px;
        margin: auto;
    }
    
    .toolbar {
        min-height: 100px;
        height: auto;
        border-radius: 50px;
        padding: 15px 25px;
        gap: 15px;
        justify-content: center;
    }

    .thickness-tools {
        padding: 5px 15px;
        height: 80px;
    }

    .thickness-btn {
        width: 70px;
        height: 70px;
    }
    
    .color-btn, .tool-btn, .action-btn {
        width: 80px;
        height: 80px;
        border-width: 5px;
        font-size: 40px;
    }
    
    .sticker-option {
        font-size: 45px;
    }
}

/* --- Banner de Actualización --- */
#update-notification {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #FF9500, #FFCC00);
    padding: 3px;
    border-radius: 24px;
    box-shadow: 0 10px 25px rgba(255, 149, 0, 0.4);
    z-index: 1000;
    transition: opacity 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275), transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    width: 90%;
    max-width: 400px;
}

#update-notification.hidden {
    opacity: 0;
    transform: translate(-50%, 150%);
    pointer-events: none;
}

.update-content {
    background: #FFFFFF;
    border-radius: 21px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 10px;
}

.update-icon {
    font-size: 40px;
    margin-bottom: -5px;
}

.update-text h3 {
    color: #FF9500;
    margin-bottom: 5px;
    font-size: 22px;
}

.update-text p {
    color: #666;
    font-size: 14px;
    line-height: 1.4;
}

.update-actions {
    display: flex;
    gap: 10px;
    width: 100%;
    margin-top: 5px;
}

.update-actions button {
    flex: 1;
    padding: 12px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: bold;
    font-family: inherit;
    box-shadow: 0 4px 0 rgba(0,0,0,0.1);
}

.update-btn-yes {
    background-color: #34C759;
    color: white;
}

.update-btn-yes:active {
    background-color: #2eb350;
    transform: translateY(4px);
    box-shadow: 0 0px 0 rgba(0,0,0,0.1);
}

.update-btn-no {
    background-color: #f0f0f0;
    color: #555;
}

.update-btn-no:active {
    background-color: #e0e0e0;
    transform: translateY(4px);
    box-shadow: 0 0px 0 rgba(0,0,0,0.1);
}

/* --- Pantalla de Inicio --- */
#start-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100dvh;
    background: radial-gradient(circle at center, #ffffff 0%, #e0f7fa 100%);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    transition: opacity 0.5s ease-out, transform 0.5s ease-out;
}

#start-screen.fade-out {
    opacity: 0;
    transform: scale(1.1);
    pointer-events: none;
}

.start-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 30px;
    animation: float 4s ease-in-out infinite;
}

.app-title {
    font-size: 55px;
    color: #FF9500;
    text-shadow: 3px 3px 0 #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff, 0 4px 15px rgba(255, 149, 0, 0.4);
    text-align: center;
    margin: 0;
    line-height: 1.1;
    padding: 0 20px;
}

.mascot-container {
    font-size: 100px;
    filter: drop-shadow(0 10px 15px rgba(0,0,0,0.2));
    animation: wiggle 3s ease-in-out infinite;
}

.start-btn {
    background: linear-gradient(135deg, #34C759, #2eb350);
    color: white;
    font-size: 28px;
    font-weight: bold;
    font-family: inherit;
    padding: 20px 40px;
    border-radius: 50px;
    border: 5px solid white;
    box-shadow: 0 10px 0 #1b8a37, 0 15px 25px rgba(0,0,0,0.2);
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.start-btn:active {
    transform: translateY(10px);
    box-shadow: 0 0px 0 #1b8a37, 0 5px 10px rgba(0,0,0,0.2);
}

.designer-credits {
    padding: 15px;
    font-size: 14px;
    color: #888;
    text-align: center;
    font-family: sans-serif;
    letter-spacing: 0.5px;
}

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

@keyframes wiggle {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-10deg) scale(1.1); }
    75% { transform: rotate(10deg) scale(1.1); }
}

@media (min-width: 768px) {
    .app-title { font-size: 80px; }
    .mascot-container { font-size: 150px; }
    .start-btn { font-size: 35px; padding: 25px 60px; }
}
