:root {
    --primary-color: #2c3e50;
    --secondary-color: #3498db;
    --bg-color: #f8f9fa;
    --panel-bg: #ffffff;
    --border-color: #e2e8f0;
    --text-main: #334155;
    --text-light: #64748b;
    --danger-color: #e53e3e;
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --radius: 8px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
}

/* Header */
.app-header {
    background-color: var(--primary-color);
    color: white;
    padding: 1.5rem 2rem;
    box-shadow: var(--shadow);
}
.header-content {
    max-width: 1400px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}
.header-content h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}
.header-content p {
    color: #cbd5e1;
    font-size: 0.9rem;
}

/* Main Layout */
.app-main {
    max-width: 1400px;
    margin: 2rem auto;
    padding: 0 2rem;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    align-items: start;
}

/* Buttons */
button {
    cursor: pointer;
    font-family: inherit;
    font-weight: 500;
    border-radius: var(--radius);
    padding: 0.5rem 1rem;
    border: none;
    transition: all 0.2s;
}
.primary-btn {
    background-color: var(--secondary-color);
    color: white;
}
.primary-btn:hover {
    background-color: #2980b9;
}
.secondary-btn {
    background-color: #e2e8f0;
    color: var(--text-main);
}
.secondary-btn:hover {
    background-color: #cbd5e1;
}
.danger-action {
    background-color: #fee2e2;
    color: #991b1b;
}
.danger-action:hover {
    background-color: #fecaca;
}
.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}
.status-text {
    min-height: 1.2rem;
    margin-top: 0.7rem;
    color: var(--text-light);
    font-size: 0.8rem;
}

/* Builder Panel */
.builder-panel {
    background: var(--panel-bg);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
}
.disclaimer {
    background-color: #fffbeb;
    border-left: 4px solid #f59e0b;
    padding: 1rem;
    font-size: 0.85rem;
    color: #92400e;
    margin-bottom: 1.5rem;
    border-radius: 0 var(--radius) var(--radius) 0;
}
fieldset {
    border: none;
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}
fieldset:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}
legend {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 1rem;
    width: 100%;
}
.form-row {
    display: flex;
    gap: 1rem;
}
.form-group {
    margin-bottom: 1rem;
    flex: 1;
}
label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 0.4rem;
    color: var(--text-main);
}
input[type="text"],
input[type="number"],
select,
textarea {
    width: 100%;
    padding: 0.6rem;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    font-family: inherit;
    font-size: 0.95rem;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--secondary-color);
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}
.invalid-input {
    border-color: var(--danger-color) !important;
    box-shadow: 0 0 0 2px rgba(229, 62, 62, 0.16) !important;
}
.invalid-status {
    color: var(--danger-color);
}
.help-text {
    font-size: 0.8rem;
    color: var(--text-light);
    margin-bottom: 0.8rem;
}
.explainer {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: #cbd5e1;
    color: white;
    font-size: 10px;
    cursor: help;
    margin-left: 4px;
}

/* Allergens dynamic UI */
.allergen-row {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    align-items: center;
}
.allergen-row input {
    margin-bottom: 0;
}
.remove-allergen {
    background: none;
    border: none;
    color: var(--danger-color);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0 0.5rem;
}

/* Pictogram Selector */
.pictogram-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    padding: 0.5rem 0;
}
.pictogram-selector label {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-weight: normal;
    cursor: pointer;
}

/* Preview Panel */
.preview-panel {
    position: sticky;
    top: 2rem;
}
.preview-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    gap: 1rem;
    flex-wrap: wrap;
}
.preview-toolbar h2 {
    font-size: 1.25rem;
    font-weight: 600;
}
.preview-actions {
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 0.5rem;
}

/* The Label */
.label-container {
    background: white;
    border: 1px solid #000; /* Real labels have clear boundaries */
    padding: 1.5rem;
    width: min(100%, var(--label-preview-width, 500px));
    min-height: var(--label-preview-min-height, auto);
    max-width: 500px;
    margin: 0 auto;
    font-family: Arial, sans-serif;
    color: #000;
    box-shadow: var(--shadow);
    overflow-wrap: anywhere;
}
.label-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    border-bottom: 2px solid #000;
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}
.label-header h3 {
    margin: 0;
    font-size: 1.2rem;
    text-transform: uppercase;
    line-height: 1.2;
}
.label-header span {
    font-weight: bold;
    white-space: nowrap;
    margin-left: 1rem;
}
.label-pictograms {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.label-pictograms svg {
    width: 40px;
    height: 40px;
}
.label-signal-word {
    font-weight: bold;
    font-size: 1.1rem;
    text-transform: uppercase;
    margin-bottom: 0.5rem;
}
.label-statements {
    font-size: 0.75rem;
    line-height: 1.3;
    margin-bottom: 1rem;
}
.label-statements p {
    margin-bottom: 0.4rem;
}
.label-statements .allergen-statement {
    font-style: italic;
    margin-top: 0.8rem;
}
.label-supplier {
    border-top: 1px solid #ccc;
    padding-top: 0.5rem;
    font-size: 0.75rem;
}
.label-barcode {
    border-top: 1px solid #ccc;
    margin-top: 0.7rem;
    padding-top: 0.55rem;
    text-align: center;
}
.barcode-svg {
    display: block;
    width: 100%;
    max-width: 280px;
    height: auto;
    margin: 0 auto;
}

/* Modal */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}
.modal.hidden {
    display: none;
}
.modal-content {
    background: white;
    padding: 2rem;
    border-radius: var(--radius);
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
}
.close-btn {
    position: absolute;
    top: 1rem;
    right: 1.5rem;
    font-size: 1.5rem;
    cursor: pointer;
}
.test-pass { color: green; font-weight: bold; }
.test-fail { color: red; font-weight: bold; }

/* Print Styles */
@media print {
    body {
        background: none;
        margin: 0;
        padding: 0;
    }
    .no-print {
        display: none !important;
    }
    .app-main {
        display: block;
        margin: 0;
        padding: 0;
    }
    .label-container {
        box-shadow: none;
        max-width: none;
        width: var(--label-print-width, 90mm);
        min-height: var(--label-print-min-height, 60mm);
        border: 1px solid #000;
        page-break-inside: avoid;
    }
}

/* Responsive */
@media (max-width: 900px) {
    .app-main {
        grid-template-columns: 1fr;
    }
    .preview-panel {
        position: static;
    }
    .form-row {
        flex-direction: column;
        gap: 0;
    }
    .preview-actions {
        justify-content: flex-start;
    }
}
