/* ============================================
   LABELS MODAL — Thumbnail Sidebar Layout
   ============================================ */

:root {
    --thumb-label-width: clamp(110px, calc((90dvh - 205px) * 148 / 210 / 3), 165px);
    --label-preview-width: min(480px, calc((90dvh - 220px) * 148 / 210), 100%);
}

/* ===== Modal Panel ===== */
.labels-modal-panel {
    width: min(1200px, 95vw);
    height: min(900px, 90dvh);
    min-height: 620px;
    max-height: 90dvh;
    overflow: hidden;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    display: grid;
    grid-template-rows: auto 1fr;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

/* ===== Toolbar ===== */
.labels-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
    z-index: 2;
}

.toolbar-title {
    display: grid;
    gap: 0.2rem;
}

.toolbar-title h2 {
    font-family: 'Space Grotesk', sans-serif;
    margin: 0;
    font-size: 1rem;
    line-height: 1.2;
}

.labels-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ===== Warning Banner ===== */
.label-warning {
    margin: 0;
    padding: 0.65rem 1rem;
    border-bottom: 1px solid rgba(220, 38, 38, 0.35);
    background: rgba(220, 38, 38, 0.08);
    color: #fca5a5;
    font-size: 0.72rem;
    line-height: 1.4;
}

/* ===== Workspace (viewer + sidebar) ===== */
.labels-workspace {
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 250px;
}

/* ===== Left: Label Viewer ===== */
.label-viewer {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-rows: auto 1fr auto;
    background: var(--bg-primary);
}

.viewer-status {
    min-height: 48px;
    padding: 10px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-muted);
    font-size: 12px;
}

.viewer-status strong {
    color: var(--text-primary);
    font-size: 13px;
}

.viewer-stage {
    min-height: 0;
    padding: 18px;
    overflow: hidden;
    display: grid;
    place-items: center;
}

/* ===== Selected Label (large preview) ===== */
.selected-label {
    width: var(--label-preview-width);
    aspect-ratio: 148 / 210;
    background: #ffffff;
    color: #0d0f12;
    border: 1px solid #4b5563;
    box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
    display: grid;
    grid-template-rows: 1fr auto;
    overflow: hidden;
    transition: opacity 120ms ease, transform 120ms ease;
    container-type: inline-size;
}

.selected-label.is-changing {
    opacity: 0.35;
    transform: translateY(4px);
}

.label-content {
    width: 86%;
    margin: 4cqi auto 0;
    display: grid;
    grid-template-rows: auto auto 1fr;
    row-gap: 3.5cqi;
}

.label-logo-area {
    height: 11cqi;
    border-bottom: 1px solid #9ca3af;
    display: grid;
    place-items: center;
    padding-bottom: 1.8cqi;
}

.label-logo-fallback {
    width: 68%;
    height: 7cqi;
    border: 1px dashed #8f98a6;
    color: #5d6674;
    display: grid;
    place-items: center;
    font-size: 2.6cqi;
    font-weight: 700;
    letter-spacing: 0.5cqi;
}

.label-reference {
    text-align: center;
    font-size: 12cqi;
    line-height: 0.95;
    font-weight: 800;
    overflow-wrap: anywhere;
}

.label-details-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3cqi 5cqi;
    align-content: start;
}

.label-line-full {
    grid-column: 1 / -1;
}

.label-key {
    font-size: 2.7cqi;
    line-height: 1;
    margin-bottom: 1.2cqi;
    color: #111827;
}

.label-value {
    font-size: 5cqi;
    line-height: 1.1;
    font-weight: 800;
    text-transform: uppercase;
    overflow-wrap: anywhere;
}

/* ===== Barcode Area ===== */
.barcode-wrap {
    width: 94%;
    margin: 0 auto 3.5cqi;
    position: relative;
}

.label-divider {
    border-top: 2px solid #9ca3af;
    margin-bottom: 2cqi;
}

.barcode-box {
    text-align: center;
}

.barcode-svg {
    width: 100%;
    height: 8cqi;
}

.barcode-fallback {
    border: 1px dashed #111827;
    padding: 1cqi;
    font-size: 2.8cqi;
    text-align: center;
    margin: 0 auto;
}

.barcode-value-text {
    margin-top: 1cqi;
    text-align: center;
    font-size: 3cqi;
    line-height: 1;
    font-weight: 600;
    letter-spacing: 0.3cqi;
    color: #374151;
}

.barcode-pallet-nr {
    margin-top: 1cqi;
    text-align: center;
    font-size: 8cqi;
    line-height: 1;
    font-weight: 800;
}

.external-no {
    position: absolute;
    right: 0;
    bottom: 0.8cqi;
    font-size: 2.5cqi;
    font-weight: 700;
}

/* ===== Viewer Footer / Pager ===== */
.viewer-footer {
    min-height: 44px;
    padding: 8px 18px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    color: var(--text-muted);
    font-size: 12px;
}

.pager-buttons {
    display: flex;
    gap: 8px;
}

.pager-btn {
    width: 34px;
    height: 30px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
    font: inherit;
}

.pager-btn:hover:not(:disabled) {
    background: var(--bg-hover);
    border-color: var(--border-highlight);
}

.pager-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ===== Right: Thumbnail Rail ===== */
.thumbnail-rail {
    min-height: 0;
    width: 250px;
    background: var(--bg-tertiary);
    border-left: 1px solid var(--border-color);
    display: grid;
    grid-template-rows: auto 1fr;
}

.thumbnail-header {
    padding: 0.7rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.thumbnail-header h2 {
    margin: 0;
    font-size: 13px;
    line-height: 1.2;
}

.thumbnail-count {
    color: var(--text-muted);
    font-size: 12px;
}

.thumbnail-list {
    min-height: 0;
    overflow-y: auto;
    padding: 8px 0 10px;
    display: grid;
    justify-items: center;
    gap: 6px;
}

/* ===== Thumbnail Items ===== */
.thumb-item {
    width: calc(var(--thumb-label-width) + 28px);
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    padding: 5px;
    background: transparent;
    color: inherit;
    cursor: pointer;
    display: grid;
    justify-items: center;
    gap: 4px;
    font: inherit;
}

.thumb-item:hover {
    background: var(--bg-hover);
    border-color: var(--border-highlight);
}

.thumb-item.active {
    background: rgba(249, 115, 22, 0.14);
    border-color: var(--accent-orange);
}

.thumb-label {
    width: var(--thumb-label-width);
    aspect-ratio: 148 / 210;
    background: #ffffff;
    color: #111827;
    border: 1px solid #6b7280;
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.25);
    display: grid;
    grid-template-rows: 1fr auto;
    overflow: hidden;
}

.thumb-content {
    width: 86%;
    margin: 7% auto 0;
    display: grid;
    grid-template-rows: auto auto 1fr;
    row-gap: clamp(4px, 0.9dvh, 8px);
}

.thumb-logo {
    height: clamp(16px, 2.8dvh, 24px);
    border-bottom: 1px solid #d1d5db;
    display: grid;
    place-items: center;
    font-size: 5px;
    letter-spacing: 1px;
    font-weight: 700;
    color: #6b7280;
}

.thumb-reference {
    text-align: center;
    font-size: clamp(18px, 3dvh, 24px);
    line-height: 1;
    font-weight: 800;
}

.thumb-lines {
    display: grid;
    gap: clamp(4px, 0.8dvh, 7px);
    align-content: start;
}

.thumb-line {
    height: clamp(5px, 0.9dvh, 8px);
    background: #111827;
}

.thumb-line.short { width: 70%; }
.thumb-line.medium { width: 86%; }

.thumb-barcode-wrap {
    width: 88%;
    margin: 0 auto clamp(5px, 1dvh, 8px);
}

.thumb-divider {
    border-top: 1px solid #d1d5db;
    margin-bottom: 5px;
}

.thumb-barcode {
    height: clamp(17px, 2.7dvh, 24px);
    display: flex;
    justify-content: center;
    gap: 1px;
    overflow: hidden;
}

.thumb-barcode span {
    width: var(--w);
    background: #111827;
}

.thumb-number {
    margin-top: 3px;
    text-align: center;
    font-size: 11px;
    line-height: 1;
    font-weight: 800;
}

.thumb-caption {
    width: var(--thumb-label-width);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    color: var(--text-muted);
    font-size: 10px;
    line-height: 1;
}

.thumb-item.active .thumb-caption {
    color: var(--text-primary);
}

.page-chip {
    padding: 2px 7px;
    border-radius: var(--radius-sm);
    background: var(--accent-orange);
    color: #ffffff;
    font-size: 10px;
    font-weight: 700;
}

/* ===== Empty State ===== */
.label-empty {
    padding: 2rem;
    color: var(--text-muted);
    text-align: center;
    border: 1px dashed var(--border-color);
    border-radius: 8px;
    background: var(--bg-tertiary);
    margin: 1rem 1.5rem 1.5rem;
}

/* ===== Print Styles ===== */
body.printing-labels .top-nav,
body.printing-labels .page-header,
body.printing-labels .stats-summary,
body.printing-labels .filters-bar,
body.printing-labels .positions-table-container,
body.printing-labels #positionModal,
body.printing-labels .theme-toggle,
body.printing-labels #toast {
    display: none !important;
}

@media print {
    @page {
        size: 148mm 210mm;
        margin: 0;
    }

    body.printing-labels {
        background: #fff !important;
    }

    body.printing-labels #labelsModal {
        position: static !important;
        display: block !important;
        background: #fff !important;
        inset: auto !important;
        padding: 0 !important;
    }

    body.printing-labels .labels-modal-panel {
        border: none !important;
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: visible !important;
        box-shadow: none !important;
        border-radius: 0 !important;
        display: block !important;
    }

    body.printing-labels .labels-toolbar,
    body.printing-labels .label-warning,
    body.printing-labels .thumbnail-rail,
    body.printing-labels .viewer-status,
    body.printing-labels .viewer-footer {
        display: none !important;
    }

    body.printing-labels .labels-workspace {
        display: block !important;
    }

    body.printing-labels .label-viewer {
        display: block !important;
    }

    body.printing-labels .viewer-stage {
        display: block !important;
        padding: 0 !important;
        overflow: visible !important;
    }

    body.printing-labels .selected-label {
        width: 148mm !important;
        height: 210mm !important;
        aspect-ratio: unset !important;
        margin: 0 auto;
        border: none;
        box-shadow: none;
        page-break-after: always;
        break-after: page;
    }

    /* Hide all labels except the one being printed */
    body.printing-labels .print-label-page {
        width: 148mm;
        height: 210mm;
        background: #fff;
        color: #0b0f14;
        margin: 0 auto;
        border: none;
        page-break-after: always;
        break-after: page;
        display: grid;
        grid-template-rows: 1fr auto;
        overflow: hidden;
        container-type: inline-size;
    }

    body.printing-labels .print-label-page:last-child {
        page-break-after: auto;
        break-after: auto;
    }
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
    .labels-modal-panel {
        width: calc(100vw - 24px);
        height: calc(100dvh - 24px);
        min-height: 0;
        max-height: calc(100dvh - 24px);
    }

    .labels-workspace {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(0, 1fr) 210px;
    }

    .thumbnail-rail {
        width: auto;
        border-left: 0;
        border-top: 1px solid var(--border-color);
        grid-template-rows: auto 1fr;
    }

    .thumbnail-list {
        padding: 12px;
        display: flex;
        overflow-x: auto;
        overflow-y: hidden;
        justify-content: flex-start;
    }

    .thumb-item {
        flex: 0 0 auto;
    }

    .thumbnail-header {
        padding: 10px 12px;
    }

    .viewer-stage {
        padding: 16px;
    }

    .toolbar-actions .btn {
        padding: 0 10px;
    }
}

@media (max-width: 640px) {
    .labels-toolbar,
    .viewer-status,
    .viewer-footer {
        align-items: flex-start;
        flex-direction: column;
    }

    .toolbar-actions,
    .pager-buttons {
        width: 100%;
    }

    .toolbar-actions .btn,
    .pager-buttons .pager-btn {
        flex: 1;
    }

    .selected-label {
        width: min(390px, calc((100dvh - 270px) * 148 / 210), 100%);
    }
}
