* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
}

.container {
    background: white;
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 1200px;
    width: 100%;
}

h1 {
    text-align: center;
    color: #333;
    margin-bottom: 25px;
    font-size: 28px;
}

.editor-area {
    display: flex;
    gap: 30px;
    justify-content: center;
    align-items: flex-start;
}

.canvas-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

#pixelCanvas {
    border: 3px solid #333;
    cursor: crosshair;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    /* Evita che un dito che disegna faccia anche scorrere/zoomare la pagina */
    touch-action: none;
}

.right-panel {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 450px;
    flex-shrink: 0;
}

.horizontal-panel {
    display: flex;
    flex-direction: row;
    gap: 15px;
}

.control-group {
    background: #f5f5f5;
    padding: 20px;
    border-radius: 10px;
}

.control-group h3,
.control-group label {
    display: block;
    margin-bottom: 10px;
    color: #555;
    font-size: 16px;
}

select {
    width: 100%;
    padding: 10px;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    background: white;
    transition: border-color 0.3s;
}

select:hover {
    border-color: #667eea;
}

select:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.35);
}

.current-color {
    width: 60px;
    height: 60px;
    border: 3px solid #333;
    border-radius: 8px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    margin-bottom: 10px;
}

#colorPalette {
    display: grid;
    grid-template-columns: repeat(16, 1fr);
    gap: 3px;
    width: 100%;
}

/* Gli swatch sono <button>: azzero lo stile generico dei button qui sotto
   (padding/min-width/flex/box-shadow) per farli tornare dei quadratini */
.color-swatch {
    width: 100%;
    aspect-ratio: 1;
    min-width: 0;
    flex: none;
    padding: 0;
    margin: 0;
    cursor: pointer;
    border: 1px solid #ddd;
    border-radius: 0;
    box-shadow: none;
    transition: transform 0.1s;
}

.color-swatch:hover {
    transform: scale(1.2);
    border: 2px solid #333;
    z-index: 10;
}

.color-swatch:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 1px;
    z-index: 10;
}

/* Colore attualmente selezionato, per chi naviga da tastiera */
.color-swatch[aria-pressed="true"] {
    border: 2px solid #333;
    box-shadow: 0 0 0 2px #667eea;
}

/* Selettore strumento: tre pulsanti a icona invece di una <select> */
.tool-group {
    display: flex;
    gap: 8px;
}

.tool-btn {
    flex: 1;
    min-width: 0;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    color: #555;
    border: 2px solid #ddd;
    border-radius: 8px;
    box-shadow: none;
    cursor: pointer;
    transition: border-color 0.3s, background 0.3s, color 0.3s;
}

.tool-btn:hover {
    border-color: #667eea;
    transform: none;
    box-shadow: none;
}

.tool-btn:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 1px;
}

/* Strumento attivo */
.tool-btn[aria-pressed="true"] {
    background: #667eea;
    border-color: #667eea;
    color: white;
}

.buttons {
    display: flex;
    flex-direction: row;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

button {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: bold;
    transition: all 0.3s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    flex: 1;
    min-width: 140px;
}

.btn-undo {
    background: #34495e;
    color: white;
}

.btn-undo:hover {
    background: #2c3e50;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.btn-redo {
    background: #16a085;
    color: white;
}

.btn-redo:hover {
    background: #138d75;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

button:disabled,
button:disabled:hover {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.btn-clear {
    background: #e74c3c;
    color: white;
}

.btn-clear:hover {
    background: #c0392b;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.btn-download {
    background: #27ae60;
    color: white;
}

.btn-download:hover {
    background: #229954;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.btn-qr {
    background: #3498db;
    color: white;
}

.btn-qr:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.btn-export {
    background: #9b59b6;
    color: white;
}

.btn-export:hover {
    background: #8e44ad;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.btn-import {
    background: #f39c12;
    color: white;
}

.btn-import:hover {
    background: #e67e22;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

/* Modale QR Code */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    animation: fadeIn 0.3s;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.modal-content {
    background-color: white;
    margin: 10% auto;
    padding: 30px;
    border-radius: 15px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    text-align: center;
    position: relative;
}

.modal-content h2 {
    color: #333;
    margin-bottom: 20px;
    font-size: 22px;
}

.modal-content p {
    color: #666;
    margin-top: 15px;
    font-size: 14px;
}

/* Anche il pulsante di chiusura è un <button>: stesso azzeramento dello
   stile generico dei button */
.close {
    position: absolute;
    right: 15px;
    top: 15px;
    min-width: 0;
    flex: none;
    padding: 0;
    margin: 0;
    background: none;
    box-shadow: none;
    border-radius: 4px;
    line-height: 1;
    color: #aaa;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    transition: color 0.3s;
}

.close:hover {
    color: #333;
}

.close:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

#qrcode {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px 0;
}

#qrcode img {
    border: 3px solid #333;
    border-radius: 10px;
}

@media (max-width: 1024px) {
    .container {
        padding: 20px;
    }

    h1 {
        font-size: 22px;
    }

    .editor-area {
        flex-direction: column;
        align-items: center;
    }

    .right-panel {
        width: 100%;
        max-width: 450px;
    }

    #pixelCanvas {
        max-width: 100%;
        height: auto;
    }
}