/* --- Variables Globales --- */
:root {
    --primary-color: #1b9bff;
    --primary-hover: #0056b3;
    --primary-light: #4A98F7;
    --bg-color: #ADEBB3;
    --panel-bg: #ffffff;
    --text-color: #5a6168;
    --heading-color: #333;
    --danger-color: #ff4d4d;
    --success-color: #28a745;
    --modal-bg: #ffffff;
}

/* --- Variables MODO OSCURO --- */
body.dark-theme {
    --bg-color: #1a1a2e;
    --panel-bg: #16213e;
    --text-color: #e0e0e0;
    --heading-color: #ffffff;
    --primary-color: #0f3460;
    --primary-hover: #e94560;
    --modal-bg: #16213e;
}

/* --- Reset y Estilos Base --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Nunito', sans-serif;
}

html, body {
    height: auto;
    min-height: 100%;
    margin: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-color);
    color: var(--text-color);
    scroll-behavior: smooth;
    transition: background 0.3s ease, color 0.3s ease;
    overscroll-behavior: none;
}

.grid {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-height: 100vh;
    padding-bottom: 50px;
    position: relative;
    z-index: 10;
}

/* --- Header --- */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 15px 20px;
    flex-wrap: wrap; 
}

.header .logo img { height: 80px; width: auto; object-fit: contain; }
.header nav { display: flex; gap: 15px; align-items: center; flex-wrap: wrap; }

.header nav a {
    color: #D7942D;
    font-weight: 700;
    text-shadow: 1px 1px 0px rgba(255, 255, 255, 0.5);
    background: rgba(255, 255, 255, 0.2);
    padding: 4px 8px;
    border-radius: 4px;
    text-decoration: none;
    transition: 0.3s;
}
body.dark-theme .header nav a {
    text-shadow: none;
    color: #f1c40f;
}

.header nav a:hover { color: var(--primary-color); }
.header nav a.active { background: var(--primary-color); color: white; padding: 5px 10px; border-radius: 5px; }

.theme-toggle {
    background: rgba(0,0,0,0.1); border: none; cursor: pointer;
    font-size: 1.2rem; padding: 5px 10px; border-radius: 20px; transition: 0.3s;
}
body.dark-theme .theme-toggle { background: rgba(255,255,255,0.1); color: white; }

.header nav a.btn-paypal {
    background: #ffc439; color: #003087; text-shadow: none;
    display: flex; align-items: center; gap: 6px; padding: 5px 12px;
    border-radius: 20px; transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.header nav a.btn-paypal:hover {
    background: #ffb300; color: #002266; transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

/* --- Área de Resultados (IA) --- */
#resultado-container { padding: 0 20px; width: 100%; position: relative; }

#resultado {
    background: var(--panel-bg); color: var(--heading-color);
    min-height: 80px; max-height: 200px; overflow-y: auto; padding: 20px;
    border-left: 8px solid var(--primary-color); border-radius: 8px;
    font-size: 1.1rem; line-height: 1.4; text-align: center; 
    box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}

.overlay-espera {
    display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.7); color: white; z-index: 1000;
    flex-direction: column; justify-content: center; align-items: center; 
    backdrop-filter: blur(5px); border-radius: 8px;
}
.loader-icon { font-size: 2rem; margin-bottom: 10px; }

/* --- Layout Principal --- */
.container-principal {
    flex: 1; display: flex; flex-direction: row; gap: 20px; align-items: stretch;
    width: 100%; padding: 0 20px; box-sizing: border-box; justify-content: center;
    flex-wrap: wrap; height: 100%; min-height: 85vh; overflow: visible;
}

#toggle-tools {
    display: block; position: fixed; top: 15px; left: 15px; z-index: 10000; 
    padding: 10px 15px; background: var(--primary-color); color: white;
    border: none; border-radius: 5px; cursor: pointer; font-size: 1.2rem;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
#toggle-tools.active { background: var(--danger-color); }

.tools-board {
    position: fixed; top: 80px; left: -100%; width: 280px; display: block; 
    height: calc(100vh - 100px); overflow-y: auto; z-index: 9999;
    transition: left 0.3s ease; background: var(--panel-bg);
    padding: 15px; box-shadow: 2px 0 10px rgba(0,0,0,0.2);
    border-radius: 8px;
}
.tools-board.active { left: 0; }
.tools-board .option:focus-visible { background: rgba(27, 155, 255, 0.2); transform: translateX(8px); }
.tools-board .option.active i { animation: pulse-blue 2s infinite; }
.tools-board::-webkit-scrollbar { width: 6px; }
.tools-board::-webkit-scrollbar-thumb { background-color: var(--primary-light); border-radius: 10px; }

@keyframes pulse-blue { 0% { transform: scale(1); color: var(--primary-color); } 50% { transform: scale(1.2); color: var(--primary-hover); } 100% { transform: scale(1); color: var(--primary-color); } }

input[type=range] { height: 25px; margin: 10px 0; width: 100%; background: transparent; }
input[type=range]:focus { outline: none; }
input[type=range]::-webkit-slider-runnable-track { width: 100%; height: 8px; cursor: pointer; background: #ddd; border-radius: 5px; }
input[type=range]::-webkit-slider-thumb { height: 20px; width: 20px; border-radius: 50%; background: var(--primary-color); cursor: pointer; -webkit-appearance: none; margin-top: -6px; }
body.dark-theme input[type=range]::-webkit-slider-runnable-track { background: #333; }

.title { font-weight: bold; border-bottom: 1px solid #ddd; margin-bottom: 10px; display: block; padding-bottom: 5px; color: var(--heading-color); }
body.dark-theme .title { border-bottom: 1px solid #444; }
.row { width: 100%; margin-bottom: 20px; }
.options { list-style: none; width: 100%; padding: 0; }
.row .option { display: flex; align-items: center; gap: 10px; width: 100%; flex-wrap: wrap; }

.tools-board .option { display: flex; cursor: pointer; align-items: center; margin-bottom: 12px; padding: 10px; border-radius: 8px; transition: background 0.3s, transform 0.2s; color: var(--text-color); width: 100%; }
.tools-board .option:hover { background: rgba(0,0,0,0.05); transform: translateX(5px); }
body.dark-theme .tools-board .option:hover { background: rgba(255,255,255,0.05); }

.tools-board .option.active { color: var(--primary-color); background: rgba(27, 155, 255, 0.1); font-weight: bold; }
.tools-board .option i { margin-right: 12px; width: 25px; text-align: center; font-size: 1.2rem; }
.tools-board .option span { font-size: 1.05rem; }
.input-select { width: 100%; padding: 8px; background: var(--panel-bg); color: var(--text-color); border: 1px solid #ddd; border-radius: 5px; margin-top: 5px; }
body.dark-theme .input-select { border-color: #444; }

#color-picker { cursor: pointer; opacity: 1; width: 100%; height: 100%; border: none; background: none; }
.colors .options { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.colors .option { height: 25px; width: 25px; border-radius: 50%; margin-top: 0; cursor: pointer; }
.colors .option.selected { outline: 3px solid var(--primary-color); outline-offset: 2px; transform: scale(1.2); }
.colors .option:last-child { padding: 0; overflow: hidden; display: flex; align-items: center; justify-content: center; }

/* --- Área de Dibujo --- */
.drawing-board { flex: 2; display: flex; background: var(--panel-bg); border-radius: 7px; overflow: hidden; height: 100%; width: 100%; position: relative; z-index: 1; }

#canvas-wrapper {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    transform-origin: 0 0;
    will-change: transform;
    cursor: crosshair;
    background: #ffffff;
    border: 2px solid #333;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
#canvas-wrapper.hide-mouse-cursor { cursor: none !important; }
body.dark-theme #canvas-wrapper { border: 2px solid #555; }

/* Cursor visual propio */
#cursor-visual { position: fixed; width: 20px; height: 20px; top: 0; left: 0; pointer-events: none; border: 2px solid rgba(255, 255, 255, 0.8); border-radius: 50%; transform: translate(-50%, -50%); user-select: none; z-index: 9999; display: none; mix-blend-mode: difference; box-shadow: 0 0 0 1px #000; transition: transform 0.05s ease-out; will-change: left, top; }

/* --- Cursores Remotos Multijugador --- */
.remote-cursor {
    position: fixed; pointer-events: none; z-index: 9998;
    display: flex; align-items: center; gap: 5px;
    transition: transform 0.05s linear;
    will-change: transform;
}
.remote-cursor-dot {
    width: 12px; height: 12px; border-radius: 50%;
    border: 2px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.remote-cursor-name {
    background: rgba(0, 0, 0, 0.6); color: white; padding: 2px 6px;
    border-radius: 4px; font-size: 11px; font-weight: bold;
    white-space: nowrap; box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* --- Botones --- */
.button-style, .buttons button { width: 100%; margin-top: 10px; padding: 10px; border: none; border-radius: 5px; cursor: pointer; font-weight: bold; color: white; transition: filter 0.2s, transform 0.1s; display: block; text-align: center; box-sizing: border-box; }
.import-btn { background: #1abc9c; }
.buttons button:hover:not(:disabled), .import-btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
button:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; }
.buttons button:focus-visible { filter: brightness(1.2); box-shadow: 0 0 0 4px rgba(27, 155, 255, 0.4); }

#btn-share { background: linear-gradient(135deg, #6e8efb, #a777e3); display: flex; align-items: center; justify-content: center; gap: 8px; box-shadow: 0 4px 15px rgba(110, 142, 251, 0.3); }
#btn-gemini { background: linear-gradient(135deg, #6e8efb, #a777e3); padding: 12px 20px; border-radius: 8px; box-shadow: 0 4px 15px rgba(110, 142, 251, 0.4); display: flex; align-items: center; justify-content: center; gap: 10px; }
.undo-btn { background: #6c757d; } .redo-btn { background: #8e44ad; } .btn-zoom { background: #6c757d; } .clear-canvas { background: var(--danger-color); } .save-img { background: var(--success-color); }

/* --- Historial --- */
.history-board { flex: 0 0 250px; max-height: 85vh; background: var(--panel-bg); border-radius: 15px; padding: 20px; display: flex; flex-direction: column; box-shadow: 0 4px 15px rgba(0,0,0,0.05); overflow: hidden; }
.history-board h2 { color: var(--heading-color); font-size: 1.1rem; margin-bottom: 10px; }
#historial-galeria { display: flex; flex-direction: column; height: 100%; padding: 10px; background: rgba(0, 0, 0, 0.05); border-radius: 10px; }
body.dark-theme #historial-galeria { background: rgba(255, 255, 255, 0.05); }

#lista-miniaturas { flex: 1; display: flex; flex-direction: column; gap: 15px; overflow-y: auto; height: 100%; padding-right: 5px; }
#lista-miniaturas::-webkit-scrollbar { width: 8px; }
#lista-miniaturas::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 10px; }
body.dark-theme #lista-miniaturas::-webkit-scrollbar-track { background: #2a2a4a; }
#lista-miniaturas::-webkit-scrollbar-thumb { background: var(--primary-light); border-radius: 10px; }

.miniatura-contenedor { width: 100%; height: 140px; flex: 0 0 140px; padding: 0; border-radius: 10px; overflow: visible; border: 2px solid var(--panel-bg); transition: transform 0.2s; position: relative; }
.miniatura-contenedor:hover { transform: translateY(-5px); border-color: var(--primary-color); }
.miniatura-contenedor img { width: 100%; height: 100%; display: block; object-fit: contain; aspect-ratio: 16 / 9; background: #fff; border-radius: 8px;}
.btn-borrar-mini { position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; background: var(--danger-color); color: white; border: 2px solid white; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 12px; z-index: 50; }

/* --- Chat Multijugador Flotante --- */
#btn-toggle-chat {
    position: fixed; bottom: 20px; right: 20px; z-index: 10001;
    background: var(--primary-color); color: white; border: none;
    width: 60px; height: 60px; border-radius: 50%; font-size: 1.5rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.3); cursor: pointer;
    transition: transform 0.2s; display: none;
}
#btn-toggle-chat:hover { transform: scale(1.1); }
#btn-toggle-chat .badge {
    position: absolute; top: 0; right: 0; background: var(--danger-color);
    color: white; font-size: 12px; font-weight: bold; width: 20px; height: 20px;
    border-radius: 50%; display: flex; align-items: center; justify-content: center;
    border: 2px solid white; display: none;
}

.chat-container {
    position: fixed; bottom: 90px; right: 20px; width: 320px; height: 400px;
    background: var(--panel-bg); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    z-index: 10000; display: none; flex-direction: column; overflow: hidden;
    border: 1px solid rgba(0,0,0,0.1);
}
body.dark-theme .chat-container { border: 1px solid rgba(255,255,255,0.1); }

.chat-header {
    background: var(--primary-color); color: white; padding: 12px 15px;
    font-weight: bold; display: flex; justify-content: space-between; align-items: center;
}
.chat-header button {
    background: none; border: none; color: white; cursor: pointer; font-size: 1.2rem;
}
.chat-messages {
    flex: 1; padding: 15px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--primary-light); border-radius: 10px; }

.chat-msg { background: rgba(0,0,0,0.05); padding: 8px 12px; border-radius: 8px; font-size: 0.9rem; align-self: flex-start; max-width: 90%; word-break: break-word; }
body.dark-theme .chat-msg { background: rgba(255,255,255,0.05); }
.chat-msg.self { background: rgba(27, 155, 255, 0.15); align-self: flex-end; border-bottom-right-radius: 2px; }
.chat-msg strong { display: block; font-size: 0.8rem; color: var(--primary-color); margin-bottom: 2px; }
.chat-msg.self strong { color: var(--text-color); }

.chat-input-wrapper {
    display: flex; padding: 10px; border-top: 1px solid rgba(0,0,0,0.1); gap: 8px;
}
body.dark-theme .chat-input-wrapper { border-top: 1px solid rgba(255,255,255,0.1); }
.chat-input-wrapper input {
    flex: 1; padding: 10px; border-radius: 20px; border: 1px solid #ccc; background: var(--bg-color); color: var(--text-color); outline: none;
}
body.dark-theme .chat-input-wrapper input { border-color: #555; }
.chat-input-wrapper button {
    background: var(--primary-color); color: white; border: none; width: 40px; height: 40px; border-radius: 50%; cursor: pointer;
}

/* Modales y Toasts */
.toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; display: flex; flex-direction: column; gap: 10px; pointer-events: none; }
.toast { padding: 12px 20px; border-radius: 6px; color: white; font-weight: bold; box-shadow: 0 4px 10px rgba(0,0,0,0.2); animation: slideIn 0.3s ease-out; pointer-events: auto; transition: opacity 0.3s; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.modal-estilo { padding: 20px; border-radius: 10px; border: 1px solid #ccc; background: var(--modal-bg); color: var(--text-color); margin: auto; }
body.dark-theme .modal-estilo { border: 1px solid #444; }
.modal-content { display: flex; flex-direction: column; gap: 10px; }
.modal-content input { padding: 8px; border-radius: 5px; border: 1px solid #ccc; background: var(--panel-bg); color: var(--text-color);}
body.dark-theme .modal-content input { border: 1px solid #555; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.btn-cancelar { background: var(--danger-color); color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;}
.btn-aceptar { background: var(--primary-color); color: white; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;}

/* --- Footer --- */
#contacto-footer { width: 100%; background: #2c3e50; color: white; padding: 40px 20px; text-align: center; margin-top: 40px; }
body.dark-theme #contacto-footer { background: #0f172a; }
#footer-title { font-size: 1.5rem; margin-bottom: 20px; }
.contenedorRedes { display: flex; justify-content: center; gap: 25px; }
.contenedorRedes a { font-size: 20px; color: #bdc3c7; text-decoration: none; transition: 0.3s; background: rgba(255,255,255,0.1); width: 45px; height: 45px; display: flex; align-items: center; justify-content: center; border-radius: 50%; }
.contenedorRedes a:hover { background: var(--primary-color); color: white; transform: translateY(-3px); }
.contenedorRedes a.fa-paypal:hover { background: #003087; }

:focus-visible { outline: 3px solid var(--primary-color); outline-offset: 2px; border-radius: 4px; }
:focus:not(:focus-visible) { outline: none; }

.canvas-scanning::after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 4px; background: linear-gradient(to bottom, transparent, var(--primary-color)); box-shadow: 0 0 15px var(--primary-color); animation: scanning 2s linear infinite; }
@keyframes scanning { 0% { top: 0%; } 100% { top: 100%; } }
@keyframes flashEffect { 0% { opacity: 1; } 50% { opacity: 0.3; background-color: #fff; } 100% { opacity: 1; } }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
@keyframes dots { 0%, 20% { content: " ."; } 40% { content: " . ."; } 60% { content: " . . ."; } 80%, 100% { content: ""; } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.shake { animation: shake 0.4s ease-in-out; }
.canvas-flash { animation: flashEffect 0.2s ease-out; }

/* MEDIA QUERIES */
@media (max-width: 768px) {
    .container-principal { display: flex; flex-direction: column; align-items: center; gap: 15px; padding: 10px; width: 100%; height: auto; }
    .drawing-board { order: 1; width: 100%; min-height: 60vh; position: relative; margin-bottom: 20px; }
    .history-board { order: 3; display: flex; flex: 1 1 auto; width: 100%; max-height: none; padding: 20px; border-top: 2px solid var(--primary-light); }
    #lista-miniaturas { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; padding: 5px; max-height: 300px; }
    .colors .options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: 100%; }
    .buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
    .clear-canvas { order: 10; }
    #toggle-tools { display: block !important; top: 20px !important; left: 20px !important; width: 50px; height: 50px; font-size: 20px; border-radius: 50% !important; box-shadow: 0 4px 10px rgba(0,0,0,0.3) !important; }
    .chat-container { width: calc(100% - 40px); bottom: 90px; right: 20px; }
}
@media (min-width: 769px) {
    .grid { display: flex; flex-direction: column; align-items: flex-start; gap: 20px; min-height: 80vh; padding: 20px; }
    #toggle-tools { display: none !important; }
    .tools-board { position: static !important; left: 0 !important; height: auto !important; display: block !important; box-shadow: none !important; flex: 0 0 280px; transition: none !important; }
    .history-board { position: static; left: 0; height: auto; display: block !important; box-shadow: none; }
    .container-principal { display: flex; flex-direction: row; justify-content: center; align-items: stretch; }
    .drawing-board { min-height: 75vh; }
}
@media (pointer: coarse) { #cursor-visual { display: none !important; } #canvas-wrapper { cursor: default; } }

/* --- AÑADIDOS PARA FUNCIONES AVANZADAS --- */
#board-container {
    overflow: hidden; 
    position: relative;
}

.draw-layer {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    touch-action: none; 
    pointer-events: none; 
}

#interaction-layer {
    pointer-events: auto; 
    z-index: 100;
}

body.panning-mode #interaction-layer {
    cursor: grab !important;
}
body.panning-mode:active #interaction-layer {
    cursor: grabbing !important;
}

/* --- Añadidos para Grabación y Selección --- */
@keyframes march {
    to { stroke-dashoffset: 20; }
}

.pulsing-record {
    animation: pulse-red 1.5s infinite;
}

@keyframes pulse-red {
    0% { background-color: #e74c3c; box-shadow: 0 0 0 0 rgba(231, 76, 60, 0.7); }
    70% { background-color: #c0392b; box-shadow: 0 0 0 10px rgba(231, 76, 60, 0); }
    100% { background-color: #e74c3c; box-shadow: 0 0 0 0 rgba(231, 76, 60, 0); }
}