/* Calculation Editor Styles (compact, aligned with calculation-list layout) */

.calculation-editor .calc-form,
.calc-form {
    display: flex;
    flex-direction: column;
    gap: 18px;
    /* match filters-container visual style for stammdaten */
    background: var(--surface);
    padding: 20px;
    border-radius: 8px 8px 0px 0px;
    /* match the main screen inner width so the editor lines up with the header/list */
    width: 100%;
    max-width: 1200px;
    box-sizing: border-box;
}

/* Sticky toolbar bar (position buttons + attachments + save/cancel) */
.calculation-editor .editor-sticky-bar {
    position: sticky;
    top: var(--sticky-offset, 0px);
    z-index: 1500; /* above panels, below overlays/popups */
    background: var(--surface);
    border-bottom: 1px solid var(--input-border);
    /* match editor width and alignment */
    width: 100%;
    max-width: 1200px;
    margin: 0 auto 16px auto;
    border-radius: 0px 0px 8px 8px;
    padding: 12px 20px;
    box-sizing: border-box;
    box-shadow: 0 8px 24px -16px rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(20px);
}

/* Loading Overlay (Calculation Editor) */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 5000;
    backdrop-filter: blur(2px);
}
.loading-overlay__inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 32px 46px;
    background: var(--surface, #222);
    border: 1px solid var(--input-border);
    border-radius: 14px;
    box-shadow: 0 8px 32px -4px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.05) inset;
}
.loading-overlay__spinner {
    width: 54px;
    height: 54px;
    border: 5px solid rgba(255, 255, 255, 0.25);
    border-top-color: var(--text, #fff);
    border-radius: 50%;
    animation: loading-overlay-spin 0.9s linear infinite;
}
.loading-overlay__text {
    color: var(--text, #fff);
    font-size: 16px;
    font-weight: 500;
    letter-spacing: 0.5px;
}
@keyframes loading-overlay-spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* Ensure the calculation editor root can anchor absolutely positioned internal popups */
.calculation-editor {
    position: relative;
}

.calculation-editor input,
.calculation-editor input[type="text"],
.calculation-editor input[type="date"],
.calculation-editor select,
input,
select {
    padding: 10px 12px;
    border-radius: 6px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--text);
    font-size: 15px;
}

/* Make the date-picker icon visible on dark backgrounds */
.calculation-editor input[type="date"] {
    /* reserve space for the native icon so it doesn't overlap text */
    padding-right: 10px;
}

/* For WebKit browsers, invert the native calendar indicator so it becomes light on dark */
.calculation-editor input[type="date"]::-webkit-calendar-picker-indicator {
    filter: invert(1) brightness(2) contrast(1.2);
    opacity: 0.95;
}

.calculation-editor .form-controls {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* Attachments (button + count) */
.calculation-editor .attachments-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 38px;
}

.calculation-editor .btn-attach {
    width: 38px;
    height: 38px;
    flex: 0 0 38px; /* enforce fixed box like other toolbar buttons */
    padding: 0;
    border: 0;
    border-radius: 4px;
    background-color: transparent;
    background-size: contain; /* scale SVG to fill like other icons */
    background-position: center;
    background-repeat: no-repeat;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.15s;
}
.calculation-editor .btn-attach:hover {
    background-color: var(--overlay-bg);
}
.calculation-editor .btn-attach:active {
    transform: translate(1px, 1px);
}

.calculation-editor .attachment-count {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    cursor: pointer;
    line-height: 1.2;
    display: flex;
    align-items: center;
    text-decoration: underline;
    width: 60px;
    margin-right: 18px;
}
.calculation-editor .attachment-count:hover {
    text-decoration: underline;
}

/* Attachment popup */
.calculation-editor .attachment-popup {
    background: linear-gradient(145deg, var(--surface) 0%, var(--surface-alt, #202020) 100%);
    border: 1px solid var(--input-border);
    border-radius: 10px;
    padding: 10px 0 6px 0;
    min-width: 300px;
    max-width: 420px;
    max-height: 340px;
    overflow-y: auto;
    box-shadow: 0 10px 28px -4px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.03) inset;
    z-index: 2000;
    position: absolute;
    backdrop-filter: blur(4px) saturate(130%);
    color: var(--text); /* ensure white text */
}
.calculation-editor .attachment-popup::before {
    content: "";
    position: absolute;
    top: -7px;
    left: 32px;
    width: 14px;
    height: 14px;
    background: inherit;
    transform: rotate(45deg);
    border-left: 1px solid var(--input-border);
    border-top: 1px solid var(--input-border);
    box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.2);
}
.calculation-editor .attachment-popup::-webkit-scrollbar {
    width: 10px;
}
.calculation-editor .attachment-popup::-webkit-scrollbar-track {
    background: transparent;
}
.calculation-editor .attachment-popup::-webkit-scrollbar-thumb {
    background: var(--overlay-bg);
    border-radius: 6px;
    border: 2px solid transparent;
    background-clip: content-box;
}
.calculation-editor .attachment-popup::-webkit-scrollbar-thumb:hover {
    background: var(--input-border);
    border: 2px solid transparent;
    background-clip: content-box;
}

.calculation-editor .attachment-list {
    list-style: none;
    margin: 0;
    padding: 4px 6px 2px 6px;
    display: flex;
    flex-direction: column;
    gap: 0;
}

.calculation-editor .attachment-list li {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px 8px 38px; /* extra left space for icon */
    position: relative;
    font-size: 14px;
    line-height: 1.2;
    transition: background-color 0.18s;
    background: transparent;
}
.calculation-editor .attachment-list li:not(:last-child)::after {
    content: "";
    position: absolute;
    left: 14px;
    right: 10px;
    bottom: 0;
    height: 1px;
    background: linear-gradient(to right, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0));
}
.calculation-editor .attachment-list li:hover {
    background: rgba(255, 255, 255, 0.04);
}

.calculation-editor .attachment-list li a {
    flex: 1;
    color: var(--text); /* inherit white text */
    font-size: 13px;
    text-decoration: none;
    word-break: break-word;
    font-weight: 500;
}
.calculation-editor .attachment-list li a:hover {
    text-decoration: underline;
}

.calculation-editor .attachment-delete {
    background: transparent;
    color: var(--text-label);
    border: 0;
    border-radius: 4px;
    width: 22px;
    height: 22px;
    font-size: 13px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background-color 0.18s, color 0.18s;
}
.calculation-editor .attachment-delete:hover {
    background: #c0392b;
    color: #fff;
}

/* Bullet dot for each attachment item */
.calculation-editor .attachment-list li::before {
    content: "";
    position: absolute;
    left: 12px;
    top: 50%;
    width: 18px;
    height: 18px;
    transform: translateY(-50%);
    background: url("/assets/images/toolbar/attachment_small.svg") center center / contain no-repeat;
    opacity: 0.9;
}

.calculation-editor .btn-save,
.calculation-editor .btn-cancel {
    padding: 10px 16px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-size: 14px;
}

.calculation-editor .btn-save {
    background: var(--btn-gradient);
    color: black;
}

.calculation-editor .btn-cancel {
    background: var(--overlay-bg);
    color: var(--text);
    border: 1px solid var(--input-border);
}

.calculation-editor .title-row h2 {
    margin: 0;
    color: var(--text);
    font-size: 20px;
}

/* Admin catalog edit button placed beside Kalkulationen headline */
.btn-catalog-admin {
    width: 20px;
    height: 20px;
    margin-left: 8px;
    background: transparent;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
    background-image: url("/assets/images/catalog/edit.svg");
    background-size: 20px 20px;
    background-repeat: no-repeat;
    background-position: center;
    transition: background-color 0.2s, transform 0.15s;
}
.btn-catalog-admin:hover {
    background-color: var(--overlay-bg);
}
.btn-catalog-admin:active {
    transform: translate(1px, 1px);
}

/* Editor grid to resemble list-item layout */
.calculation-editor .editor-row {
    display: flex;
    gap: 22px;
    align-items: center;
}

.calculation-editor .editor-col {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.calculation-editor .editor-col.small {
    flex: 0 0 180px;
}

.calculation-editor .title-row {
    margin-bottom: 12px;
}

/* responsive */
@media (max-width: 768px) {
    .calculation-editor .calc-form {
        padding: 12px;
    }
    .calculation-editor .form-controls {
        justify-content: center;
    }
}

/* Position Toolbar */
.calculation-editor .position-toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-left: 5px;
}

.calculation-editor .position-btn {
    width: 38px;
    height: 38px;
    border: 0px solid var(--input-border);
    border-radius: 4px;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;
    background-color: transparent;
    cursor: pointer;
    transition: background-color 0.2s;
    padding: 0;
    margin-right: 4px;
}

.calculation-editor .position-btn:hover {
    background-color: var(--overlay-bg);
}

.calculation-editor .position-btn:active,
.calculation-editor .position-btn.pressed {
    transform: translate(1px, 1px);
}

/* Position Panel List */
.calculation-editor .position-panel-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 20px;
}

/* Group sum row (meta) */
.calculation-editor .group-sum-row {
    display: flex;
    align-items: center;
    justify-content: end;
    gap: 10px;
    padding: 10px 12px;
    margin: 2px 0 8px 0;
    border: 4px solid var(--primary-text);
    margin-left: 20px;
    margin-right: 20px;
}
.calculation-editor .group-sum-row--top {
    border-bottom: none;
    border-radius: 16px 16px 0 0;
    margin-bottom: 0;
}
.calculation-editor .group-sum-row--bottom {
    border-top: none;
    border-radius: 0 0 16px 16px;
    margin-top: 0;
    padding-top: 4px;
}
.calculation-editor .group-sum-row__label {
    color: var(--text-label);
    font-size: 13px;
    font-weight: 500;
}
.calculation-editor .group-sum-row__total {
    color: var(--text);
    font-size: 32px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}
.calculation-editor .group-sum-row__remove {
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 4px;
}
.calculation-editor .group-sum-row__remove:hover {
    background: var(--overlay-bg);
}

/* Group selection hint in sticky bar */
.calculation-editor .position-toolbar .position-btn--group {
    /* visually separate group button from the 4 add buttons */
    margin-left: 16px;
}

.calculation-editor .group-select-hint {
    margin-left: 8px; /* place right next to group button */
    color: var(--text-label);
    font-size: 13px;
}

/* Grand total section at bottom of position list */
.calculation-editor .position-panel-grand-total-wrap {
    margin-top: 28px;
    padding: 24px 20px 120px 20px; /* extra bottom padding for scroll affordance */
    box-sizing: border-box;
    position: relative;
    background: var(--surface); /* match panel background */
    border-radius: 8px;
}
.calculation-editor .position-panel-grand-total-separator {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--input-border);
}
.calculation-editor .position-panel-grand-total-label {
    font-size: 18px;
    font-weight: 400;
    color: var(--text-label);
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}
.calculation-editor .position-panel-grand-total-value {
    font-size: 40px;
    font-weight: 600;
    line-height: 1.1;
    background: linear-gradient(90deg, var(--text) 0%, var(--text) 60%, var(--text-label) 100%);
    background-clip: text;
    -webkit-background-clip: text;
    color: var(--text);
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
    font-variant-numeric: tabular-nums;
}

/* Position Panel */
.calculation-editor .position-panel {
    background: var(--surface);
    border: 0px solid var(--input-border);
    border-radius: 8px;
    padding: 16px;
    box-sizing: border-box;
    transition: background-color 0.2s ease-in-out;
}

/* Selected panel highlight */
.calculation-editor .position-panel.position-panel--selected {
    outline: 2px solid var(--input-border);
    outline-offset: 0;
    background: var(--surface-flash);
}

.calculation-editor .position-panel.position-panel--new {
    background-color: var(--surface-flash);
}

/* Historical/deprecated catalog reference (panel refers to removed/deprecated item) */
.calculation-editor .position-panel.position-panel--deprecated-ref {
    position: relative;
    background: linear-gradient(135deg, var(--surface) 0%, var(--deprecated-bg, #311a1a) 100%);
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.04) inset;
}

/* Inline deprecated badge inside header (calculation editor) */
.calculation-editor .position-panel__deprecated-badge {
    background: var(--deprecated-bg, #3b1f1f);
    color: var(--deprecated-text, #ffb4b4);
    border: 1px solid var(--deprecated-border, #5a2d2d);
    font-size: 11px;
    padding: 2px 8px;
    line-height: 1.2;
    border-radius: 12px;
    letter-spacing: 0.2px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
}

.calculation-editor .position-panel__header {
    font-size: 16px;
    font-weight: bold;
    color: var(--text);
    margin-bottom: 8px;
    border-bottom: 1px solid var(--input-border);
    padding-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.calculation-editor .position-panel__header-fields {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.calculation-editor .position-panel__title-input {
    width: 180px;
    padding: 6px 8px !important;
    border: 1px solid var(--input-border) !important;
    border-radius: 4px !important;
    background: var(--input-bg) !important;
    color: var(--text) !important;
    font-size: 14px !important;
}

.calculation-editor .position-panel__type-select {
    width: 140px;
    padding: 6px 8px !important;
    border: 1px solid var(--input-border) !important;
    border-radius: 4px !important;
    background: var(--input-bg) !important;
    color: var(--text) !important;
    font-size: 14px !important;
}

.calculation-editor .position-panel__unit-price-input {
    width: 60px;
    padding: 6px 8px !important;
    border: 1px solid var(--input-border) !important;
    border-radius: 4px !important;
    background: var(--input-bg) !important;
    color: var(--text) !important;
    font-size: 14px !important;
    text-align: right !important;
}

.calculation-editor .position-panel__unit-price-label {
    font-size: 14px;
    font-weight: 300;
    color: var(--text-label);
    margin-left: 0px;
    align-self: center;
    white-space: nowrap;
}

.calculation-editor .position-panel__tier-icon {
    width: 24px;
    height: 24px;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;
    margin-left: 4px;
    align-self: center;
    flex-shrink: 0;
    cursor: help;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.calculation-editor .position-panel__tier-icon:hover {
    opacity: 1;
}

/* Applied Tier indicator next to the staffel icon */
.calculation-editor .position-panel__tier-applied {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: 2px;
    font-size: 13px;
    font-weight: 600;
    color: var(--accent-color);
    white-space: nowrap;
}

.calculation-editor .position-panel__type-icon {
    width: 20px;
    height: 20px;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;
    flex-shrink: 0;
}

.calculation-editor .position-panel__remove-btn {
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    font-size: 18px;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.calculation-editor .position-panel__remove-btn:hover {
    background-color: var(--overlay-bg);
}

/* Controls wrap on header right side */
.calculation-editor .position-panel__controls {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* Move buttons */
.calculation-editor .position-panel__move-btn {
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.2s;
}
.calculation-editor .position-panel__move-btn:hover {
    background-color: var(--overlay-bg);
}

/* Position Panel Base */
.calculation-editor .position-panel__base {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.calculation-editor .position-panel__base-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 16px;
    align-items: center;
}

/* Inline variant for compact math-style layout */
.calculation-editor .position-panel__base-row--inline {
    gap: 8px;
}

.calculation-editor .position-panel__field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 140px;
}

.calculation-editor .position-panel__label {
    font-size: 12px;
    color: var(--text-label);
}

.calculation-editor .position-panel__number-input {
    padding: 6px 8px;
    border-radius: 4px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--text);
    font-size: 14px;
    text-align: right;
}

/* Utility: narrow numeric input for Stück */
.calculation-editor .position-panel__number-input--narrow {
    width: 60px;
}

/* Extra-compact for width/height controls */
.calculation-editor .position-panel__number-input--compact {
    width: 70px;
}

/* Inline aesthetics */
.calculation-editor .position-panel__separator {
    color: var(--text-label);
    padding: 0 0px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
}
.calculation-editor .position-panel__inline-unit,
.calculation-editor .position-panel__inline-text {
    color: var(--text-label);
    font-size: 13px;
}
.calculation-editor .position-panel__result {
    padding: 2px 4px;
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    font-weight: 300;
}
.calculation-editor .position-panel__result--emphasis {
    font-weight: 500;
}
.calculation-editor .position-panel__note-input {
    padding: 6px 8px;
    border-radius: 4px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--text);
    font-size: 14px;
    text-align: left;
    width: 440px; /* twice the compact inline baseline (120px) */
}
.calculation-editor .position-panel__summary {
    margin-top: 6px;
    display: flex;
    gap: 8px;
    align-items: baseline;
}
.calculation-editor .position-panel__summary--align-right {
    justify-content: flex-end;
}
.calculation-editor .position-panel__field--final {
    margin-left: auto;
}
.calculation-editor .position-panel__field--compact-inline {
    min-width: 120px;
}

/* Ensure total price (inline) stands out similarly across types */
.calculation-editor .position-panel__total-price {
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    text-align: right;
    min-width: 90px;
    font-size: 28px;
    line-height: 1.05;
}
.calculation-editor .position-panel__piece-final-price {
    font-weight: 400;
    font-variant-numeric: tabular-nums;
    text-align: right;
    min-width: 80px;
    font-size: 14px;
    line-height: 1.2;
}
.calculation-editor .position-panel__field--final .position-panel__label,
.calculation-editor .position-panel__field--compact-inline .position-panel__label {
    text-align: right;
    display: block;
}

/* Align grand total value to right side */
.calculation-editor .position-panel-grand-total-label,
.calculation-editor .position-panel-grand-total-value {
    text-align: right;
}
.calculation-editor .position-panel-grand-total-wrap {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

/* Utility: hide spinners for number inputs (webkit + firefox) */
.calculation-editor .position-panel__number-input--no-spin::-webkit-outer-spin-button,
.calculation-editor .position-panel__number-input--no-spin::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.calculation-editor .position-panel__number-input--no-spin {
    appearance: textfield;
    -moz-appearance: textfield;
}

.calculation-editor .position-panel__value {
    padding: 6px 8px;
    border-radius: 4px;
    border: 1px dashed var(--input-border);
    background: transparent;
    color: var(--text);
    font-size: 14px;
    min-width: 100px;
}

/* Tier Popup Styles */
.tier-popup {
    position: fixed;
    background: var(--tier-bg);
    border: 1px solid var(--input-border);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 2000;
    min-width: 280px;
    max-width: 400px;
    font-size: 14px;
    overflow: hidden;
}

.tier-popup-header {
    background: var(--overlay-bg);
    padding: 12px 16px;
    font-weight: bold;
    color: var(--text);
    border-bottom: 1px solid var(--input-border);
    font-size: 16px;
}

.tier-popup-content {
    padding: 16px;
    color: var(--text);
}

.tier-popup-base-price {
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--input-border);
    font-size: 14px;
}

.tier-popup-tiers-header {
    font-weight: bold;
    margin-bottom: 8px;
    color: var(--text);
    font-size: 14px;
}

.tier-popup-tier {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid var(--input-border-light, rgba(255, 255, 255, 0.1));
}

.tier-popup-tier:last-child {
    border-bottom: none;
}

.tier-quantity {
    color: var(--text-label);
    font-size: 13px;
}

.tier-price {
    font-weight: 500;
    color: var(--text);
    font-size: 14px;
}

/* Responsive adjustments for tier popup */
@media (max-width: 768px) {
    .tier-popup {
        min-width: 250px;
        max-width: 90vw;
    }

    .tier-popup-header {
        padding: 10px 12px;
        font-size: 14px;
    }

    .tier-popup-content {
        padding: 12px;
    }
}
