/* Basic Reset & Defaults */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary-color: #0056b3; /* Professional Blue */
    --secondary-color: #f8f9fa; /* Light Gray */
    --text-color: #333;
    --border-color: #ced4da;
    --error-color: #dc3545;
    --success-color: #28a745;
    --font-en: 'Roboto', sans-serif; /* Roboto supports Latin & Cyrillic */
    --font-he: 'Assistant', sans-serif;
    --font-ar: 'Noto Sans Arabic', sans-serif; /* Added Arabic font */
    --header-height: 70px;
    --cut-line-color: #aaa; /* Color for print cut lines */
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-en); /* Default font supports EN and RU */
    line-height: 1.6;
    color: var(--text-color);
    background-color: #eef2f5; /* Slightly off-white background */
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Language Specific Fonts */
html[lang="he"] body {
    font-family: var(--font-he);
}
html[lang="ar"] body {
    font-family: var(--font-ar); /* Apply Arabic font */
}


/* Header */
header {
    background-color: var(--primary-color);
    color: white;
    padding: 0 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: var(--header-height);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

header h1 {
    font-size: 1.5rem;
    font-weight: 500;
}

.lang-switcher button {
    background: none;
    border: 1px solid white;
    color: white;
    padding: 0.3rem 0.8rem;
    margin-left: 0.5rem; /* Default LTR */
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.2s ease, color 0.2s ease;
    font-size: 0.9rem;
    font-family: inherit; /* Ensure buttons use the right font */
}

.lang-switcher button:hover,
.lang-switcher button.active {
    background-color: white;
    color: var(--primary-color);
}

/* RTL Language Button Spacing */
html[dir="rtl"] .lang-switcher button {
    margin-left: 0;
    margin-right: 0.5rem;
}

/* Main Content Area */
main {
    flex: 1; /* Allow main to grow and push footer down */
    max-width: 1200px;
    width: 95%;
    margin: 2rem auto;
    display: grid;
    grid-template-columns: 300px 1fr; /* Sidebar for config, rest for barcodes */
    gap: 2rem;
}

/* Configuration Section */
.configuration {
    background-color: white;
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    align-self: start; /* Stick to the top */
}

.configuration h2 {
    margin-bottom: 1.5rem;
    color: var(--primary-color);
    font-size: 1.3rem;
    border-bottom: 1px solid #eee;
    padding-bottom: 0.5rem;
}

.form-group {
    margin-bottom: 1.2rem;
}

/* Form Label/Legend Weights per language */
.form-group label, .layout-options legend {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500; /* Default (EN, RU) */
    color: #555;
}
html[lang="he"] .form-group label,
html[lang="he"] .layout-options legend {
    font-weight: 600; /* Assistant (HE) */
}
html[lang="ar"] .form-group label,
html[lang="ar"] .layout-options legend {
    font-weight: 500; /* Noto Sans Arabic (Medium weight) */
}


.form-group textarea,
.form-group input[type="number"] {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    font-family: inherit; /* Inherit font from body */
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.form-group textarea:focus,
.form-group input[type="number"]:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(0, 86, 179, 0.2);
    outline: none;
}

.form-group small {
    display: block;
    margin-top: 0.3rem;
    font-size: 0.85rem;
    color: #6c757d;
}

.layout-options {
    border: 1px solid var(--border-color);
    padding: 1rem;
    border-radius: 4px;
    margin-top: 1.5rem;
}

.layout-options legend {
    padding: 0 0.5em;
    font-size: 1.1rem;
    /* Weights set above based on lang */
    color: var(--primary-color);
    margin-left: 10px; /* Default LTR */
}
/* Legend Margin for RTL languages */
html[dir="rtl"] .layout-options legend {
    margin-left: 0;
    margin-right: 10px;
}


.layout-options .form-group {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    margin-bottom: 0.8rem;
}

.layout-options label {
    flex-basis: 100px; /* Fixed width for labels */
    margin-bottom: 0;
    text-align: right; /* Default LTR */
}
/* Label Alignment for RTL languages */
html[dir="rtl"] .layout-options label {
    text-align: left;
}

.layout-options input[type="number"] {
    flex: 1;
}

.form-actions {
    margin-top: 2rem;
    display: flex;
    flex-direction: column; /* Stack buttons vertically */
    gap: 0.75rem; /* Space between buttons */
}

.form-actions button {
    padding: 0.8rem 1rem;
    font-size: 1rem;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease, opacity 0.2s ease;
    font-weight: 500; /* Default (EN, RU, AR) */
}
html[lang="he"] .form-actions button {
    font-weight: 600; /* Assistant (HE) */
}

#generate-btn {
    background-color: var(--primary-color);
    color: white;
}
#generate-btn:hover {
    background-color: #004085;
}

#print-btn {
    background-color: var(--success-color);
    color: white;
}
#print-btn:hover {
    background-color: #218838;
}
#print-btn:disabled {
    background-color: #a0d1a9;
    cursor: not-allowed;
    opacity: 0.7;
}

#clear-btn {
    background-color: #6c757d;
    color: white;
}
#clear-btn:hover {
    background-color: #5a6268;
}

.error {
    color: var(--error-color);
    margin-top: 1rem;
    font-weight: bold;
    text-align: center;
}
.info {
    color: #555;
    margin-top: 1rem;
    font-style: italic;
    text-align: center;
}


/* Barcode Display Area */
.barcode-display {
    background-color: white;
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    min-height: 400px; /* Ensure it has some height initially */
}

#barcode-container {
    display: grid; /* Grid layout for barcodes */
    /* grid-template-columns will be set by JS */
    /* gap will be set by JS for screen */
    width: 100%; /* Make container take full width */
    margin-top: 1rem; /* Add some space above the grid */
}

/* == Define barcode items as query containers (applies screen & print) == */
.barcode-item {
    container-type: inline-size; /* Track width */
    container-name: barcode-cell; /* Give it a name (optional but good) */

    /* Screen-specific styles */
    background-color: white;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    overflow: hidden;
    border: 1px dashed var(--border-color);
    min-height: 100px;
}

/* Style for placeholder items on screen */
.barcode-item-placeholder {
    background-color: var(--secondary-color); /* Light grey background for placeholders on screen */
    border-style: dotted; /* Dotted border for placeholders on screen */
}

.barcode-item svg {
    max-width: 100%; /* Ensure barcode scales down on screen */
    height: 60px; /* Fixed height for screen consistency */
    display: block;
    margin: 0 auto; /* Center barcode horizontally */
}

.barcode-text {
    margin-top: 8px; /* Space between barcode and text */
    font-size: 0.9rem; /* Screen font size */
    color: #333;
    word-break: break-all; /* Break long numbers if needed */
}

.barcode-text.error-text {
    color: var(--error-color);
    font-weight: bold;
}

/* Footer */
footer {
    text-align: center;
    padding: 1.5rem 1rem; /* Increased padding */
    margin-top: 2rem;
    background-color: #e9ecef;
    color: #6c757d;
    font-size: 0.9rem;
    display: flex; /* Use flexbox */
    flex-direction: column; /* Stack items vertically */
    align-items: center; /* Center items horizontally */
    gap: 0.75rem; /* Space between copyright and button */
}

/* Styling for the Contact Us Button/Link */
.button-contact {
    display: inline-block; /* Allow padding */
    padding: 0.4rem 1rem;
    font-size: 0.9rem;
    color: var(--primary-color); /* Use primary color for text */
    background-color: transparent; /* Transparent background */
    border: 1px solid var(--primary-color); /* Primary color border */
    border-radius: 4px;
    text-decoration: none; /* Remove underline from link */
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.button-contact:hover {
    background-color: var(--primary-color); /* Fill on hover */
    color: white; /* White text on hover */
}

/* Ensure footer items look okay on smaller screens if needed */
@media (min-width: 768px) {
    footer {
        flex-direction: row; /* Side-by-side on larger screens */
        justify-content: center; /* Center horizontally */
        gap: 1.5rem; /* More space between items */
    }
}


/* RTL Specific Styles */
html[dir="rtl"] {
    text-align: right;
}

html[dir="rtl"] main {
    grid-template-columns: 1fr 300px; /* Reverse grid columns */
}

html[dir="rtl"] .configuration {
    order: 2; /* Move config to the right */
}

html[dir="rtl"] .barcode-display {
    order: 1; /* Move barcode area to the left */
}

/* Accessibility */
.visually-hidden {
  position: absolute !important;
  height: 1px; width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap; /* prevent line breaks */
}


/* ================== */
/* == Print Styles == */
/* ================== */
@media print {
    @page {
        size: A4 landscape;
        margin: 10mm;

        @top-center {
            content: "Page " counter(page);
            font-size: 9pt; color: #666; vertical-align: top; padding-top: 2mm;
        }
    }

    html, body {
        height: 100%; width: 100%; margin: 0; padding: 0;
    }

    body {
        background-color: white !important; font-size: 10pt; color: black !important;
        -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important;
        counter-reset: page; display: flex; flex-direction: column;
        /* Ensure correct font is potentially applied in print */
        font-family: var(--font-en); /* Default */
    }
    html[lang="he"] body { font-family: var(--font-he) !important; }
    html[lang="ar"] body { font-family: var(--font-ar) !important; }


    header, footer, .configuration, .form-actions button:not(#print-btn), .lang-switcher, #generation-info, #error-message, #config-heading, #print-header-info, #barcode-preview-heading {
        display: none !important;
    }

    main {
        flex-grow: 1; max-width: 100%; width: 100%;
        display: flex; flex-direction: column;
        box-shadow: none; border: none; padding: 0; margin: 0;
    }

    .barcode-display {
        flex-grow: 1; box-shadow: none; border: none; padding: 0; margin: 0;
        width: 100% !important; background-color: white !important;
        display: flex; flex-direction: column;
    }

    #barcode-container {
        display: grid !important;
        grid-template-rows: var(--print-grid-rows, auto); /* Use JS variable for rows */
        /* Columns set by JS */
        gap: 0 !important;
        width: 100%;
        flex-grow: 1; /* Fill available space */
        overflow: hidden;
        background-color: white !important;
    }

    /* .barcode-item inherits container-type/name */
    /* Override screen styles for print */
    .barcode-item {
        border: none !important;
        /* Reduce padding slightly more */
        padding: 1mm !important;
        min-height: auto !important;
        page-break-inside: avoid !important;
        background-color: white !important;
        position: relative;
        overflow: hidden; /* Keep overflow hidden */
        display: flex !important;
        flex-direction: column;
        align-items: center;
        /* Change vertical distribution */
        justify-content: space-around; /* Distribute space, push from edges */
        width: 100%; height: 100%;
        box-shadow: none !important;
    }

     /* Placeholders & Errors */
    .barcode-item-placeholder { background-color: white !important; }
    .barcode-item-placeholder * { display: none !important; }
    .barcode-item-error { /* Optional: style error items */ }

     /* Page break trigger */
    .page-break-trigger { break-after: page !important; page-break-after: page !important; }

    /* Cut Lines */
    .barcode-item::before { /* Right */ content: ""; position: absolute; top: 0; right: 0; width: 1px; height: 100%; border-right: 1px dashed var(--cut-line-color) !important; }
    .barcode-item::after { /* Bottom */ content: ""; position: absolute; bottom: 0; left: 0; width: 100%; height: 1px; border-bottom: 1px dashed var(--cut-line-color) !important; }

    /* SVG Scaling */
    .barcode-item svg {
        display: block;
        width: 96%; /* Drive scaling by width */
        height: auto; /* Maintain aspect ratio */
        /* Reduce max-height slightly to give more vertical buffer */
        max-height: 85%;
        background-color: white !important;
    }

    /* --- Responsive Barcode Text --- */
    .barcode-text {
        margin-top: 0.5mm; /* Minimal gap */
        line-height: 1.1;
        color: black !important;
        word-break: break-all;
        text-align: center;
        background-color: white !important;
        flex-shrink: 0;
        width: 95%;
        max-width: 95%;
        /* Adjust max-height based on SVG reduction */
        max-height: 13%;
        overflow: hidden;

        /* Default smallest font size */
        font-size: 5pt;
    }

    /* Apply larger font sizes based on container width */
    @container barcode-cell (min-width: 60mm) { .barcode-text { font-size: 6pt; } }
    @container barcode-cell (min-width: 90mm) { .barcode-text { font-size: 7pt; } }
    @container barcode-cell (min-width: 130mm) { .barcode-text { font-size: 8pt; } }
    @container barcode-cell (min-width: 180mm) { .barcode-text { font-size: 9pt; } }
    @container barcode-cell (min-width: 240mm) { .barcode-text { font-size: 10pt; } }


     .barcode-text.error-text {
         color: var(--error-color) !important; font-weight: bold;
         font-size: 7pt !important;
         max-height: none; overflow: visible;
     }

} /* End @media print */

/* Keep the rest of your general styles, responsive styles etc. */

/* Responsive Adjustments (For Screen) */
@media (max-width: 992px) {
    main {
        grid-template-columns: 1fr; /* Stack config and barcodes */
        gap: 1.5rem;
    }
    html[dir="rtl"] main {
        grid-template-columns: 1fr; /* Same stacking for RTL */
    }
    .configuration {
       order: 1; /* Config first on small screens */
       align-self: stretch; /* Take full width */
    }
    html[dir="rtl"] .configuration {
        order: 1; /* Keep config first */
    }
    .barcode-display {
        order: 2;
    }
     html[dir="rtl"] .barcode-display {
        order: 2; /* Keep display second */
    }
}

@media (max-width: 768px) {
    header {
        padding: 0 1rem;
        height: auto;
        flex-direction: column;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }
    header h1 {
        font-size: 1.3rem;
        margin-bottom: 0.5rem;
    }
    .lang-switcher {
        margin-top: 0.5rem;
        /* Allow wrapping for many buttons */
        flex-wrap: wrap;
        justify-content: center;
    }
    .lang-switcher button {
        margin: 0.2rem; /* Adjust spacing for wrapped buttons */
    }
    main {
         width: 98%;
         margin: 1rem auto;
    }
    .configuration {
        padding: 1rem;
    }
    .barcode-display {
        padding: 1rem;
    }
    .layout-options .form-group {
        flex-direction: column;
        align-items: stretch;
        gap: 0.3rem;
    }
     .layout-options label {
        flex-basis: auto;
        text-align: left; /* Reset for stacking */
    }
    html[dir="rtl"] .layout-options label {
        text-align: right; /* Maintain RTL alignment */
    }
}