/* === Allgemeine Styles === */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
    color: #333;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    padding: 40px;
}

/* === Header === */
header {
    text-align: center;
    margin-bottom: 40px;
}

h1 {
    font-size: 2.5rem;
    color: #667eea;
    margin-bottom: 10px;
}

.subtitle {
    color: #666;
    font-size: 1.1rem;
}

h2 {
    font-size: 1.5rem;
    color: #667eea;
    margin-bottom: 20px;
}

/* === Upload-Bereich === */
.upload-section {
    margin-bottom: 40px;
}

/* Eigenes Bild Upload */
.upload-custom {
    margin-top: 40px;
    text-align: center;
    padding: 30px 20px;
    background: #f8f9ff;
    border-radius: 15px;
}

.upload-custom p {
    color: #666;
    margin-bottom: 15px;
    font-size: 1rem;
}

/* === Beispielbilder-Galerie === */
.example-gallery {
    text-align: center;
}

.example-gallery h3 {
    font-size: 1.3rem;
    color: #667eea;
    margin-bottom: 25px;
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 20px;
    max-width: 100%;
}

.gallery-item {
    position: relative;
    cursor: pointer;
    border-radius: 12px;
    overflow: hidden;
    background: white;
    box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
    border: 3px solid transparent;
}

.gallery-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
    border-color: #667eea;
}

.gallery-item img {
    width: 100%;
    height: 120px;
    object-fit: cover;
    display: block;
}

.gallery-label {
    display: block;
    padding: 10px 8px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-size: 0.85rem;
    font-weight: 600;
    text-align: center;
}

/* === Buttons === */
.btn-primary, .btn-secondary, .btn-toggle {
    padding: 12px 30px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #e0e0e0;
    color: #333;
}

.btn-secondary:hover {
    background: #d0d0d0;
}

.btn-toggle {
    background: #667eea;
    color: white;
    min-width: 120px;
}

/* === Steuerung === */
.controls-section {
    margin-bottom: 30px;
    padding: 20px;
    background: #f8f9ff;
    border-radius: 10px;
}

.controls-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 15px;
}

.control-group label {
    font-weight: 600;
    color: #666;
}

.palette-selector {
    padding: 8px 15px;
    border: 2px solid #667eea;
    border-radius: 8px;
    background: white;
    color: #333;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.palette-selector:hover {
    border-color: #764ba2;
    background: #f8f9ff;
}

.grid-size-input {
    width: 70px;
    padding: 8px 12px;
    border: 2px solid #667eea;
    border-radius: 8px;
    background: white;
    color: #333;
    font-size: 1rem;
    font-weight: 600;
    text-align: center;
    transition: all 0.3s ease;
}

.grid-size-input:hover {
    border-color: #764ba2;
    background: #f8f9ff;
}

.grid-size-input:focus {
    outline: none;
    border-color: #764ba2;
    box-shadow: 0 0 0 3px rgba(118, 75, 162, 0.1);
}

.control-group span {
    font-weight: 700;
    color: #667eea;
    font-size: 1.2rem;
}

.color-number-input {
    width: 60px;
    padding: 8px 12px;
    border: 2px solid #667eea;
    border-radius: 8px;
    background: white;
    color: #333;
    font-size: 1rem;
    font-weight: 600;
    text-align: center;
    transition: all 0.3s ease;
}

.color-number-input:hover {
    border-color: #764ba2;
    background: #f8f9ff;
}

.color-number-input:focus {
    outline: none;
    border-color: #764ba2;
    box-shadow: 0 0 0 3px rgba(118, 75, 162, 0.1);
}

.btn-stepper {
    width: 36px;
    height: 36px;
    border: 2px solid #667eea;
    border-radius: 8px;
    background: white;
    color: #667eea;
    font-size: 1.5rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-stepper:hover {
    background: #667eea;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 3px 10px rgba(102, 126, 234, 0.3);
}

.btn-stepper:active {
    transform: translateY(0);
}

.palette-selector:focus {
    outline: none;
    border-color: #764ba2;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

/* === Vorschau-Bereich === */
.preview-section {
    margin-bottom: 30px;
}

/* Comparison Container für Original + Raster */
/* Standard: Nebeneinander (für Hochformat-Bilder) */
.comparison-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    align-items: start;
}

/* Vertikal: Übereinander (für Querformat-Bilder) */
.comparison-container.vertical-layout {
    grid-template-columns: 1fr;
    gap: 20px;
    max-width: 800px;
    margin: 0 auto;
}

.image-column,
.grid-column {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.image-column h3,
.grid-column h3 {
    font-size: 1.2rem;
    color: #667eea;
    margin-bottom: 15px;
    text-align: center;
}

/* Original-Bild Container */
.original-image-container {
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    width: fit-content;
}

.original-image-container img {
    /* Raster ist 40×12px + gaps = ~519px breit, 56×12px + gaps = ~727px hoch */
    width: 519px;
    height: 727px;
    object-fit: contain;
    border-radius: 5px;
    display: block;
    background: #f5f5f5;
}

/* Querformat-Bilder: Breitere, flachere Darstellung */
.comparison-container.vertical-layout .original-image-container img {
    width: 727px;   /* Gleich wie Raster-Höhe im Hochformat */
    height: 519px;  /* Gleich wie Raster-Breite im Hochformat */
}

/* Raster Container */
.preview-container {
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* === Raster-Grid (Web-Ansicht) === */
/* Hochformat: 40 Spalten × 56 Zeilen */
.grid-container {
    display: grid;
    grid-template-columns: repeat(40, 12px);
    grid-template-rows: repeat(56, 12px);
    gap: 1px;
    background: #ddd;
    border: 2px solid #333;
}

/* Querformat: 56 Spalten × 40 Zeilen */
.vertical-layout .grid-container {
    grid-template-columns: repeat(56, 12px);
    grid-template-rows: repeat(40, 12px);
}

.grid-cell {
    width: 12px;
    height: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 8px;
    font-weight: bold;
    border: 1px solid #ccc;
    background: white;
}

.grid-cell.color-mode {
    font-size: 0;
}

/* Raster ohne Zwischenraum - nur visuell ausblenden, Größe beibehalten */
.grid-container.no-grid {
    background: transparent;
}

.grid-container.no-grid .grid-cell {
    border-color: transparent;
}

/* === Farblegende === */
.legend-section {
    margin-bottom: 30px;
}

.legend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
}

.legend-header h2 {
    margin: 0;
}

.color-legend {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 15px;
    background: #f8f9ff;
    border-radius: 8px;
    border: 2px solid #e0e0e0;
}

.legend-number {
    font-weight: bold;
    font-size: 1.3rem;
    color: #667eea;
    min-width: 30px;
}

.legend-color {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 2px solid #333;
}

.legend-name {
    font-weight: 600;
    color: #333;
}

/* === Lade-Anzeige === */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading-overlay p {
    color: white;
    font-size: 1.2rem;
    margin-top: 20px;
}

.spinner {
    width: 60px;
    height: 60px;
    border: 6px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* === Footer === */
footer {
    text-align: center;
    padding-top: 30px;
    margin-top: 30px;
    border-top: 2px solid #e0e0e0;
    color: #666;
    font-size: 0.9rem;
}

/* === Responsive Design === */
@media (max-width: 768px) {
    .container {
        padding: 20px;
    }

    h1 {
        font-size: 2rem;
    }

    /* Side-by-Side wird zu Stacked auf Mobil */
    .comparison-container {
        grid-template-columns: 1fr;
        gap: 20px;
    }

    .controls-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .control-group {
        justify-content: center;
    }

    /* Galerie auf Mobil */
    .gallery-grid {
        grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
        gap: 15px;
    }

    .gallery-item img {
        height: 100px;
    }

    .gallery-label {
        font-size: 0.75rem;
        padding: 8px 5px;
    }

    /* Hochformat */
    .grid-container {
        grid-template-columns: repeat(40, 10px);
        grid-template-rows: repeat(56, 10px);
    }

    /* Querformat auf Mobil */
    .vertical-layout .grid-container {
        grid-template-columns: repeat(56, 10px);
        grid-template-rows: repeat(40, 10px);
    }

    .grid-cell {
        width: 10px;
        height: 10px;
        font-size: 6px;
    }

    /* Original-Bild auf Mobil kleiner */
    .original-image-container img {
        width: 429px;  /* 40×10px + gaps */
        height: 599px; /* 56×10px + gaps */
    }

    /* Querformat-Bilder auf Mobil */
    .comparison-container.vertical-layout .original-image-container img {
        width: 599px;
        height: 429px;
    }
}

/* ========================================= */
/* === DRUCK-OPTIMIERUNG (@media print) === */
/* ========================================= */

@media print {
    /* Verstecke alles außer dem Raster und der Legende */
    body {
        background: white;
        padding: 0;
        margin: 0;
    }

    .container {
        box-shadow: none;
        border-radius: 0;
        padding: 0;
    }

    header,
    .upload-section,
    .controls-section,
    .loading-overlay,
    .image-column,
    footer {
        display: none !important;
    }

    /* Nur Raster anzeigen, nicht das Original-Bild */
    .comparison-container {
        display: block;
    }

    .grid-column h3 {
        display: none;
    }

    /* A4-Seite konfigurieren */
    @page {
        size: A4 portrait;
        margin: 10mm;
    }

    /* Raster für Druck optimieren: 5mm × 5mm Kästchen */
    .preview-section h2 {
        font-size: 14pt;
        margin-bottom: 5mm;
        color: black;
    }

    .preview-container {
        padding: 0;
        box-shadow: none;
        background: white;
    }

    /* Hochformat: 40 Spalten × 56 Zeilen */
    .grid-container {
        display: grid;
        grid-template-columns: repeat(40, 5mm);
        grid-template-rows: repeat(56, 5mm);
        gap: 0;
        background: white;
        border: 1pt solid black;
        page-break-inside: avoid;
    }

    /* Querformat: 56 Spalten × 40 Zeilen */
    .vertical-layout .grid-container {
        grid-template-columns: repeat(56, 5mm);
        grid-template-rows: repeat(40, 5mm);
    }

    .grid-cell {
        width: 5mm;
        height: 5mm;
        border: 0.5pt solid black;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 7pt;
        font-weight: bold;
        font-family: Arial, sans-serif;
        background: white !important; /* Keine Hintergrundfarben beim Drucken */
        color: black !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    /* Beim Drucken werden IMMER Nummern angezeigt, nie Farben */
    .grid-cell.color-mode {
        font-size: 7pt !important;
    }

    /* Farblegende auf neuer Seite */
    .legend-section {
        page-break-before: always;
        margin-top: 10mm;
    }

    .legend-section h2 {
        font-size: 14pt;
        margin-bottom: 5mm;
    }

    .color-legend {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 5mm;
    }

    .legend-item {
        display: flex;
        align-items: center;
        gap: 5mm;
        padding: 3mm;
        border: 1pt solid black;
        background: white;
        page-break-inside: avoid;
    }

    .legend-number {
        font-size: 12pt;
        font-weight: bold;
        min-width: 8mm;
    }

    .legend-color {
        width: 10mm;
        height: 10mm;
        border: 1pt solid black;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .legend-name {
        font-size: 10pt;
    }
}
