/* Invoice & Estimate Styles - Matching App Design */

/* Date inputs - iOS fix */
#invoiceModal input[type="date"],
#estimateModal input[type="date"] {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 160px !important;
    max-width: 160px !important;
    height: 44px !important;
    max-height: 44px !important;
    min-height: 44px !important;
    padding: 0.5rem 0.75rem !important;
    box-sizing: border-box !important;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
}

/* Collapsible Sections */
.collapsible-section {
    background: var(--bg-secondary);
    border-radius: 10px;
    margin-bottom: 0.75rem;
    overflow: hidden;
}

.collapsible-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    cursor: pointer;
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
}

.collapsible-header .collapse-icon {
    color: var(--text-muted);
    font-size: 12px;
    transition: transform 0.2s ease;
}

.collapsible-section.collapsed .collapse-icon {
    transform: rotate(180deg);
}

.collapsible-content {
    padding: 0 1rem 1rem 1rem;
    opacity: 1;
}

.collapsible-section.collapsed .collapsible-content {
    height: 0 !important;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    overflow: hidden !important;
}

.collapsible-content .form-group {
    margin-bottom: 0.75rem;
}

.collapsible-content .form-group:last-child {
    margin-bottom: 0;
}

.collapsible-content .form-row {
    margin-bottom: 0.75rem;
}

.collapsible-content .form-row:last-child {
    margin-bottom: 0;
}

/* Footer Totals Section - Collapsible */
.footer-totals-section {
    width: 100%;
    margin-bottom: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: var(--bg-secondary);
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.footer-total-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: var(--font-size-secondary);
}

.footer-total-main > span:first-child {
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
}

.footer-total-main > span:nth-child(2) {
    font-weight: var(--font-weight-bold);
    color: var(--accent-green);
    flex: 1;
    text-align: right;
    margin-right: 0.5rem;
}

.collapse-indicator {
    color: var(--text-muted);
    font-size: 10px;
    transition: transform 0.2s ease;
}

.footer-totals-section.collapsed .collapse-indicator {
    transform: rotate(180deg);
}

.footer-totals-details {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border-color);
    overflow: hidden;
    opacity: 1;
}

.footer-totals-section.collapsed .footer-totals-details {
    height: 0;
    margin-top: 0;
    padding-top: 0;
    border-top: none;
    overflow: hidden;
    opacity: 0;
}

.footer-total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: var(--font-size-tertiary);
}

.footer-total-row > span:first-child {
    color: var(--text-secondary);
}

.footer-total-row > span:last-child {
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
}

.footer-total-row.discount-row > span:last-child {
    color: var(--accent-red, #e53e3e);
}

.footer-buttons {
    display: flex;
    gap: 0.5rem;
    width: 100%;
}

.footer-buttons .btn {
    flex: 1;
}

#invoiceModal .modal-footer,
#estimateModal .modal-footer {
    flex-direction: column;
}

/* Prevent modal dragging on mobile */
#invoiceModal, #estimateModal {
    touch-action: none;
    -webkit-user-drag: none;
    -webkit-touch-callout: none;
    overflow-x: hidden;
}

#invoiceModal .modal-content,
#estimateModal .modal-content {
    touch-action: auto;
    -webkit-user-drag: none;
    max-width: 100vw;
    overflow-x: hidden;
    overflow-y: hidden;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    max-height: 100vh;
    max-height: 100dvh;
}

#invoiceModal .modal-body,
#estimateModal .modal-body {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    flex: 1;
    min-height: 0;
    max-width: 100% !important;
    width: 100% !important;
    box-sizing: border-box !important;
    padding: 1rem 0.75rem;
}

/* Prevent any element from causing horizontal overflow - but exclude date inputs and only target invoice modal children */
#invoiceModal .modal-content *:not(input[type="date"]):not(#invoiceDueDate):not(#estimateValidUntil),
#estimateModal .modal-content *:not(input[type="date"]):not(#invoiceDueDate):not(#estimateValidUntil) {
    max-width: 100%;
    box-sizing: border-box;
}

/* Ensure all *form* inputs fit within screen - EXCEPT date inputs.
   IMPORTANT: Do NOT force 100% width on structured calc inputs inside line items. */
#invoiceModal .form-group input:not([type="date"]),
#invoiceModal .form-group select,
#invoiceModal .form-group textarea,
#estimateModal .form-group input:not([type="date"]),
#estimateModal .form-group select,
#estimateModal .form-group textarea {
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
}

/* Structured calc inputs must be allowed to shrink (override modal-wide 100% rule above). */
#invoiceModal .line-item.structured-item .calc-field input:not(.structured-total-input),
#estimateModal .line-item.structured-item .calc-field input:not(.structured-total-input) {
    /* Baseline size for numeric fields; JS expands as needed. */
    box-sizing: border-box;
    width: 55px;
    min-width: 55px;
    max-width: none !important;
}

#invoiceModal .line-item.structured-item .calc-field input.structured-total-input,
#estimateModal .line-item.structured-item .calc-field input.structured-total-input {
    width: 100% !important;
    max-width: 100% !important;
}




/* Fix form groups to prevent overflow */
#invoiceModal .form-group,
#estimateModal .form-group {
    max-width: 100%;
    overflow: hidden;
    margin-bottom: 0.75rem;
}

#invoiceModal .form-row,
#estimateModal .form-row {
    max-width: 100%;
    overflow: hidden;
    margin-bottom: 0.75rem;
}

/* Only apply to form-rows inside invoice/estimate modals */
#invoiceModal .form-row,
#estimateModal .form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    width: 100%;
    box-sizing: border-box;
}

/* Only apply to form-groups inside invoice/estimate modals */
#invoiceModal .form-group,
#estimateModal .form-group {
    width: 100%;
    box-sizing: border-box;
    min-width: 0;
}

#invoiceModal .form-group label,
#estimateModal .form-group label {
    display: block;
    margin-bottom: 0.375rem;
    color: var(--text-secondary);
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-semibold);
    letter-spacing: 0.2px;
    line-height: 1.3;
}

/* Only apply to form-groups inside invoice/estimate modals */
#invoiceModal .form-group input:not([type="date"]),
#invoiceModal .form-group select,
#estimateModal .form-group input:not([type="date"]),
#estimateModal .form-group select {
    width: 100%;
    padding: 0.75rem 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    box-sizing: border-box;
    transition: all 0.3s ease;
    min-height: 44px;
    -webkit-appearance: none;
    appearance: none;
}

/* Textarea specific styling */
#invoiceModal .form-group textarea,
#estimateModal .form-group textarea {
    width: 100%;
    padding: 0.75rem 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    box-sizing: border-box;
    transition: all 0.3s ease;
    font-family: inherit;
    min-height: 80px;
    resize: vertical;
    line-height: 1.4;
}

/* Only apply to form-groups inside invoice/estimate modals */
#invoiceModal .form-group input:focus,
#invoiceModal .form-group select:focus,
#invoiceModal .form-group textarea:focus,
#estimateModal .form-group input:focus,
#estimateModal .form-group select:focus,
#estimateModal .form-group textarea:focus {
    outline: none;
    border-color: var(--accent-green);
    background: var(--bg-tertiary);
}

/* Only apply to form-groups inside invoice/estimate modals */
#invoiceModal .form-group input[readonly],
#estimateModal .form-group input[readonly] {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    cursor: not-allowed;
    opacity: 0.8;
}

/* Placeholder styling */
#invoiceModal .form-group input::placeholder,
#invoiceModal .form-group textarea::placeholder,
#estimateModal .form-group input::placeholder,
#estimateModal .form-group textarea::placeholder {
    color: var(--text-muted);
    opacity: 0.7;
}

/* Select styling - app color palette */
#invoiceModal .form-group select,
#estimateModal .form-group select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2368d391' d='M10.293 3.293L6 7.586 1.707 3.293A1 1 0 00.293 4.707l5 5a1 1 0 001.414 0l5-5a1 1 0 10-1.414-1.414z'/%3E%3C/svg%3E");
    background-position: right 1rem center;
    background-repeat: no-repeat;
    background-size: 12px;
    padding-right: 3rem;
    cursor: pointer;
}

/* Buttons - App Color Palette */
#invoiceModal .btn,
#estimateModal .btn {
    padding: 0.75rem 1.25rem;
    border: none;
    border-radius: 10px;
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-semibold);
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 44px;
    touch-action: manipulation;
}

#invoiceModal .btn-primary,
#estimateModal .btn-primary {
    background: var(--accent-green);
    color: #1a202c;
    font-weight: var(--font-weight-bold);
}

@media (hover: hover) and (pointer: fine) {
#invoiceModal .btn-primary:hover,
#estimateModal .btn-primary:hover {
    background: var(--accent-green);
    transform: translateY(-2px);
}
}


#invoiceModal .btn-primary:active,
#estimateModal .btn-primary:active {
    transform: translateY(0);
}

#invoiceModal .btn-secondary,
#estimateModal .btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

@media (hover: hover) and (pointer: fine) {
#invoiceModal .btn-secondary:hover,
#estimateModal .btn-secondary:hover {
    background: var(--bg-card);
    border-color: var(--accent-green);
    transform: translateY(-2px);
}
}


#invoiceModal .btn-secondary:active,
#estimateModal .btn-secondary:active {
    transform: translateY(0);
}

/* Modal Footer Buttons */
#invoiceModal .modal-footer,
#estimateModal .modal-footer {
    display: flex;
    gap: 0.625rem;
    padding: 0.875rem 0.75rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    border-radius: 0 0 20px 20px;
}

#invoiceModal .modal-footer .btn,
#estimateModal .modal-footer .btn {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    padding: 0.75rem 1rem;
}

/* Line Items Styling */
.line-items-container {
    margin-bottom: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.line-item {
    position: relative;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    transition: border-color 0.2s ease;
    overflow: hidden;
}

/* Swipe container */
.line-item-swipe-container {
    position: relative;
    display: flex;
}

/* Main content area */
.line-item-main {
    flex: 1;
    display: flex;
    background: var(--bg-secondary);
    position: relative;
    z-index: 2;
    will-change: transform;
    min-width: 0;
}

/* Drag handle */
.line-item-drag-handle {
    width: 36px;
    min-width: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    cursor: grab;
    background: var(--bg-tertiary);
    border-right: 1px solid var(--border-color);
    touch-action: none;
}

.line-item-drag-handle:active {
    cursor: grabbing;
    background: var(--accent-primary-alpha);
    color: var(--accent-primary);
}

.line-item-drag-handle i {
    font-size: 14px;
}

/* Content area */
.line-item-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.75rem;
    min-width: 0;
}

.line-item-label {
    display: block;
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
    margin-bottom: 0.25rem;
    letter-spacing: 0.3px;
}

.line-item-row-desc {
    width: 100%;
}

.line-item-simple-header {
    justify-content: space-between;
    width: 100%;
    margin-bottom: 0.15rem;
}

.line-item-simple-header textarea.line-item-desc {
    flex: 0 1 auto;
    width: auto;
    min-width: 220px;
    max-width: 520px;
    margin: 0;
}

.line-item-row-desc-simple .line-item-desc {
    width: 100%;
}

.line-item-row-desc-simple textarea.line-item-desc {
    padding: 0.5rem 0.625rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    box-sizing: border-box;
    display: block;
    resize: none;
    overflow: hidden;
    line-height: 1.25;
    min-height: 34px;
}

/* Simple header description should look like other inputs too */
.line-item-simple-header textarea.line-item-desc {
    padding: 0.5rem 0.625rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    box-sizing: border-box;
    resize: none;
    overflow: hidden;
    line-height: 1.25;
    min-height: 34px;
}

.li-expand-btn {
    margin-left: 0.25rem;
    border: none;
    background: transparent;
    color: var(--text-muted);
    width: 30px;
    height: 30px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}

.li-expand-btn:active {
    opacity: 0.8;
}

.line-item-total-compact {
    margin-left: auto;
    font-weight: 800;
    color: var(--accent-green);
    font-size: 0.82rem;
    white-space: nowrap;
    padding-left: 0.35rem;
    flex-shrink: 0;
}

.line-item-details {
    width: 100%;
}

.line-item.li-collapsed .line-item-details {
    height: 0 !important;
    overflow: hidden !important;
    opacity: 0;
}

/* Invoice / estimate: dimension input unit (aligned with template creator ft·in / m·mm·cm) */
.invoice-line-unit-row {
    margin-bottom: 0.85rem;
    padding-bottom: 0.65rem;
    border-bottom: 1px solid var(--border-color, rgba(255, 255, 255, 0.08));
}

.invoice-line-unit-row .line-item-label {
    margin-bottom: 0.2rem;
}

.invoice-line-unit-hint {
    font-size: 0.68rem;
    color: var(--text-muted);
    line-height: 1.35;
    margin: 0 0 0.5rem 0;
}

.invoice-input-unit-toggle {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    background: var(--bg-tertiary);
    border-radius: 10px;
    padding: 4px;
    border: 1px solid var(--border-color);
}

.invoice-input-unit-toggle .invoice-unit-opt {
    flex: 1;
    min-width: 2.5rem;
    padding: 0.45rem 0.35rem;
    font-size: 0.72rem;
    font-weight: 700;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.invoice-input-unit-toggle .invoice-unit-opt.active {
    background: var(--accent-primary);
    color: #fff;
}

.line-item-row-numbers {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.5rem;
}

.line-item-field {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Action buttons (swipe reveal on mobile, visible on desktop) */
.line-item-actions {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100px;
    display: flex;
    z-index: 1;
    background: var(--bg-tertiary);
}

.btn-action-copy,
.btn-action-delete {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    font-size: 18px;
    cursor: pointer;
    transition: opacity 0.2s;
}

.btn-action-copy {
    color: var(--accent-green);
}

.btn-action-delete {
    color: #e53e3e;
}

.btn-action-copy:active,
.btn-action-delete:active {
    opacity: 0.5;
}

/* Input styling */
.line-item input {
    padding: 0.5rem 0.625rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    box-sizing: border-box;
    width: 100%;
}

/* Structured calc inputs can be content-sized (JS sets width in ch). */
.line-item.structured-item .calc-field input {
    width: auto;
}

.line-item.structured-item .calc-field input.structured-total-input {
    width: 100%;
}

.line-item input:focus {
    outline: none;
    border-color: var(--accent-green);
}

.line-item input[readonly] {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: var(--font-weight-semibold);
}

/* Make simple line-item totals match structured totals */
.line-item input.line-item-total[readonly] {
    color: var(--accent-green);
    font-family: var(--mono);
    font-weight: var(--font-weight-semibold);
}

/* Drag states */
.line-item.dragging {
    opacity: 0.7;
    border: 1px dashed var(--accent-primary);
    z-index: 100;
}

.line-item.drag-over {
    border-color: var(--accent-green);
}

/* All devices: swipe to reveal */
.line-item-swipe-container {
    overflow: hidden;
}

@media (max-width: 768px) {
    .line-item input {
        min-height: unset;
    }
}

/* Extra-small phones (iPhone SE / <480px): tighten structured rows */
@media (max-width: 480px) {
    .line-item-content {
        padding: 0.65rem;
        gap: 0.4rem;
    }

    .line-item.structured-item .line-item-content {
        padding: 0.65rem;
        gap: 0.4rem;
    }

    .structured-header {
        gap: 0.4rem;
    }

    .structured-name {
        min-width: 96px;
        max-width: 200px;
        padding: 0.45rem 0.55rem;
        font-size: 0.86rem;
    }

    .mode-badge {
        font-size: 0.58rem;
        padding: 0.14rem 0.36rem;
        border-radius: 6px;
    }

    .inv-row-unit-opt {
        padding: 3px 5px;
        font-size: 0.62rem;
    }

    .calc-fields {
        gap: 4px;
        padding-top: 0.2rem;
    }

    .calc-field {
        min-width: 56px;
        gap: 2px;
    }

    /* Shrink fixed-width calc groups */
    .calc-field.calc-field-total {
        flex: 0 0 96px;
        max-width: 96px;
        min-width: 96px;
    }

    .calc-field:has(.field-areaX),
    .calc-field:has(.field-areaY),
    .calc-field:has(.field-volumeX),
    .calc-field:has(.field-volumeY),
    .calc-field:has(.field-volumeZ),
    .calc-field:has(.field-multiplier),
    .calc-field:has(.field-hours),
    .calc-field:has(.field-days),
    .calc-field:has(.field-workers) {
        flex: 0 0 70px;
        max-width: 70px;
    }

    .calc-field:has(.field-quantity),
    .calc-field:has(.field-laborUnitQty) {
        flex: 0 0 78px;
        max-width: 78px;
    }

    .calc-field:has(.field-unitPrice),
    .calc-field:has(.field-rate),
    .calc-field:has(.field-ratePerDay),
    .calc-field:has(.field-laborRatePerUnit),
    .calc-field:has(.field-laborAmount) {
        flex: 0 0 86px;
        max-width: 86px;
    }

    .calc-field label {
        font-size: 9px;
        letter-spacing: 0.2px;
    }

    .calc-field input {
        padding: 5px 6px;
        font-size: var(--font-size-tertiary);
        border-radius: 7px;
    }

    .calc-sep {
        font-size: var(--font-size-tertiary);
        padding-bottom: 5px;
    }

    .inv-inline-labor-bar {
        gap: 3px;
        margin-bottom: 0.3rem;
    }

    .inv-inline-labor-btn {
        min-width: 3.7rem;
        padding: 5px 4px;
        font-size: 0.66rem;
    }

    .inv-perim-btn {
        height: 32px;
        padding: 0 9px;
        border-radius: 9px;
        font-size: 0.7rem;
    }

    .inv-perim-rem {
        width: 32px;
    }

    .invoice-input-unit-toggle {
        gap: 3px;
        padding: 3px;
        border-radius: 9px;
    }

    .invoice-input-unit-toggle .invoice-unit-opt {
        padding: 0.4rem 0.3rem;
        font-size: 0.68rem;
        border-radius: 7px;
    }
}

.btn-icon-copy {
    padding: 0.5rem;
    background: var(--accent-blue);
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: var(--font-size-tertiary);
    min-width: 40px;
    min-height: 40px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
.btn-icon-copy:hover {
    background: var(--accent-blue);
    transform: scale(1.1);
}
}


.btn-icon-copy:active {
    transform: scale(0.95);
}

.btn-icon-small {
    padding: 0.5rem;
    background: var(--accent-red);
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: var(--font-size-tertiary);
    min-width: 40px;
    min-height: 40px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
.btn-icon-small:hover {
    background: var(--accent-red);
    transform: scale(1.1);
}
}


.btn-icon-small:active {
    transform: scale(0.95);
}

/* Add Line Item Button */
#invoiceModal .line-items-container + .btn,
#estimateModal .line-items-container + .btn {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px dashed var(--border-color);
    padding: 0.5rem 1rem;
    width: auto;
    margin-top: 0.5rem;
    min-height: 40px;
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-tertiary);
}

@media (hover: hover) and (pointer: fine) {
#invoiceModal .line-items-container + .btn:hover,
#estimateModal .line-items-container + .btn:hover {
    background: var(--bg-card);
    border-color: var(--accent-green);
    border-style: solid;
    color: var(--accent-green);
}
}


/* Totals Section */
#invoiceModal .form-row:has(#invoiceSubtotal),
#invoiceModal .form-row:has(#invoiceTotal),
#estimateModal .form-row:has(#estimateSubtotal),
#estimateModal .form-row:has(#estimateTotal) {
    background: var(--bg-secondary);
    padding: 1.25rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    margin-top: 1rem;
}

#invoiceModal .form-row:has(#invoiceTotal) .form-group:last-child input,
#estimateModal .form-row:has(#estimateTotal) .form-group:last-child input {
    background: var(--accent-green);
    border-color: var(--accent-green);
    color: var(--accent-green);
    font-weight: var(--font-weight-bold);
    font-size: var(--font-size-default);
}

/* Invoice/Estimate Tabs */
.invoice-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
    padding: 0.5rem;
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px solid var(--border-color);
}

.invoice-tab {
    flex: 1;
    padding: 0.875rem 1.5rem;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-base);
    cursor: pointer;
    transition: all 0.3s ease;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

@media (hover: hover) and (pointer: fine) {
.invoice-tab:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}
}


.invoice-tab.active {
    background: var(--accent-green);
    color: #1a202c;
}

.invoice-tab-content {
    display: none;
    animation: fadeIn 0.3s ease;
}

.invoice-tab-content.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.invoice-actions {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
}

.invoices-list, .estimates-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 1.5rem;
}

.invoice-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.25rem;
    transition: all 0.2s ease;
    cursor: pointer;
    margin-bottom: 1rem;
}

@media (hover: hover) and (pointer: fine) {
.invoice-card:hover {
    transform: translateY(-2px);
    border-color: var(--accent-primary);
}
}


.invoice-card:active {
    transform: translateY(0);
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1rem;
    padding-bottom: 0.875rem;
    border-bottom: 1px solid var(--border-color);
    gap: 0.75rem;
}

.invoice-number {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    font-family: inherit;
    line-height: 1.3;
}

.invoice-status {
    padding: 0.375rem 0.75rem;
    border-radius: 20px;
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
    flex-shrink: 0;
}

.status-draft {
    background: rgba(160, 174, 192, 0.15);
    color: var(--text-muted);
    border: 1px solid rgba(160, 174, 192, 0.3);
}

.status-sent {
    background: rgba(99, 179, 237, 0.15);
    color: var(--accent-blue);
    border: 1px solid rgba(99, 179, 237, 0.3);
}

.status-open {
    background: rgba(99, 179, 237, 0.15);
    color: var(--accent-blue);
    border: 1px solid rgba(99, 179, 237, 0.3);
}

.status-paid, .status-accepted {
    background: rgba(104, 211, 145, 0.15);
    color: var(--accent-green);
    border: 1px solid rgba(104, 211, 145, 0.3);
}

.status-overdue {
    background: rgba(252, 129, 129, 0.15);
    color: var(--accent-red);
    border: 1px solid rgba(252, 129, 129, 0.3);
}

.status-rejected {
    background: rgba(252, 129, 129, 0.15);
    color: var(--accent-red);
    border: 1px solid rgba(252, 129, 129, 0.3);
}

.invoice-body {
    margin-bottom: 0.75rem;
}

.invoice-client {
    font-size: var(--font-size-secondary);
    color: var(--text-secondary);
    margin-bottom: 0.625rem;
    font-weight: var(--font-weight-normal);
    line-height: 1.5;
}

.invoice-amount {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--accent-primary);
    font-family: inherit;
    line-height: 1.3;
    margin-bottom: 0.25rem;
}

.invoice-balance {
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-normal);
    color: var(--text-muted);
    margin-top: 0.5rem;
}

.invoice-actions {
    display: flex;
    gap: 0.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.btn-icon {
    flex: 1;
    padding: 0.625rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: var(--font-size-secondary);
}

@media (hover: hover) and (pointer: fine) {
.btn-icon:hover {
    background: var(--accent-green);
    color: #1a202c;
    border-color: var(--accent-green);
    transform: translateY(-2px);
}
}


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

.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-state i {
    font-size: 4rem;
    margin-bottom: 1.5rem;
    opacity: 0.3;
    color: var(--text-tertiary);
}

.empty-state p {
    font-size: var(--font-size-default);
    font-weight: var(--font-weight-medium);
}

/* Light theme adjustments */
body.light-theme .invoice-card {
    background: var(--bg-card);
}

@media (hover: hover) and (pointer: fine) {
body.light-theme .invoice-card:hover {
}
}


body.light-theme .invoice-tab.active {
    background: var(--accent-green);
    color: #1a202c;
}

@media (hover: hover) and (pointer: fine) {
body.light-theme .btn-icon:hover {
    background: var(--accent-green);
    color: #1a202c;
}
}


/* Mobile responsive */
@media (max-width: 768px) {
    #invoiceModal .modal-body,
    #estimateModal .modal-body {
        padding: 1rem;
    }
    
    .invoices-list, .estimates-list {
        grid-template-columns: 1fr;
    }
    
    
    /* Force all inputs to stay within bounds - EXCEPT date inputs */
    input:not([type="date"]), select, textarea {
        max-width: 100% !important;
        width: 100% !important;
    }
    
    
    .invoice-tabs {
        gap: 0.25rem;
        padding: 0.25rem;
    }
    
    .invoice-tab {
        padding: 0.75rem 1rem;
        font-size: var(--font-size-secondary);
    }
    
    .invoice-card {
        padding: 1rem;
        margin-bottom: 0.875rem;
    }
    
    .invoice-header {
        margin-bottom: 0.875rem;
        padding-bottom: 0.75rem;
    }
    
    .invoice-number {
        font-size: var(--font-size-secondary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-amount {
        font-size: var(--font-size-secondary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-client {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-normal);
        margin-bottom: 0.5rem;
    }
    
    .invoice-item {
        padding: 0.75rem;
        margin-bottom: 0.5rem;
    }
    
    .invoice-num {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-amt {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-item .invoice-amt {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-view-section h4 {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-semibold);
    }
    
    .invoice-view-info {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-normal);
    }
    
    .invoice-view-info strong {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-semibold);
    }
    
    .invoice-view-table th {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-semibold);
    }
    
    .invoice-view-table td {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-normal);
    }
    
    .invoice-summary-row {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-normal);
    }
    
    .invoice-summary-row span:first-child {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-semibold);
    }
    
    .invoice-summary-row.invoice-total {
        font-size: var(--font-size-secondary);
        font-weight: var(--font-weight-bold);
    }
    
    .invoice-view-notes {
        font-size: var(--font-size-tertiary);
        font-weight: var(--font-weight-normal);
    }
    
    /* Only apply to form-rows inside invoice/estimate modals */
    #invoiceModal .form-row,
    #estimateModal .form-row {
        grid-template-columns: 1fr !important;
        gap: 0.75rem;
        width: 100%;
        max-width: 100%;
    }
    
    /* Only apply to form-groups inside invoice/estimate modals */
    #invoiceModal .form-group,
    #estimateModal .form-group {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0;
        margin-bottom: 1rem;
    }
    
    /* Only apply to form-groups inside invoice/estimate modals */
    #invoiceModal .form-group input:not([type="date"]),
    #invoiceModal .form-group select,
    #estimateModal .form-group input:not([type="date"]),
    #estimateModal .form-group select {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    
    /* Prevent horizontal scroll - AGGRESSIVE */
    #invoiceModal .modal-content,
    #estimateModal .modal-content {
        width: calc(100vw - 2rem) !important;
        max-width: calc(100vw - 2rem) !important;
        margin: 1rem !important;
        overflow-x: hidden !important;
    }
    
    #invoiceModal .modal-body,
    #estimateModal .modal-body {
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: hidden !important;
        padding: 0.875rem 0.625rem !important;
        box-sizing: border-box !important;
    }
    
    /* Force form-row to not overflow */
    #invoiceModal .form-row,
    #estimateModal .form-row {
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: hidden !important;
        box-sizing: border-box !important;
        grid-template-columns: 1fr !important;
        gap: 0.75rem !important;
        margin-bottom: 0.75rem !important;
    }
    
    #invoiceModal .form-group,
    #estimateModal .form-group {
        margin-bottom: 0.75rem;
    }
    
    .invoice-actions {
        flex-direction: column;
        gap: 0.75rem;
    }
    
    .invoice-actions .btn {
        width: 100%;
    }
    
    #invoiceModal .modal-footer,
    #estimateModal .modal-footer {
        flex-direction: column;
        gap: 0.625rem;
        padding: 0.875rem 0.625rem !important;
    }
    
    #invoiceModal .modal-footer .btn,
    #estimateModal .modal-footer .btn {
        width: 100%;
        min-height: 44px;
        padding: 0.75rem 1rem;
        font-size: var(--font-size-secondary);
    }
}

/* Tablet responsive */
@media (max-width: 1024px) and (min-width: 769px) {
    /* Only apply to form-rows inside invoice/estimate modals */
    #invoiceModal .form-row,
    #estimateModal .form-row {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Job Site Detail Invoice/Estimate List */
.job-site-invoices-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.invoice-group h4 {
    color: var(--text-secondary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-semibold);
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.invoice-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.875rem 1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-bottom: 0.625rem;
}

@media (hover: hover) and (pointer: fine) {
.invoice-item:hover {
    background: var(--bg-tertiary);
    border-color: var(--accent-primary);
    transform: translateX(2px);
}
}


.invoice-item:active {
    transform: translateX(0);
}

.invoice-item > div:first-child {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    flex: 1;
    min-width: 0;
}

.invoice-item > div:last-child {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    text-align: right;
    flex-shrink: 0;
}

.invoice-num {
    font-family: inherit;
    font-weight: var(--font-weight-bold);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    white-space: nowrap;
}

.invoice-stat {
    padding: 0.25rem 0.625rem;
    border-radius: 12px;
    font-size: var(--font-size-secondaryallest);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    white-space: nowrap;
    flex-shrink: 0;
}

.invoice-amt {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--accent-primary);
    line-height: 1.2;
}

.invoice-item .invoice-amt {
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-bold);
    color: var(--accent-primary);
    margin-bottom: 0.125rem;
}

.invoice-amt {
    margin-left: auto;
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--accent-green);
}

.empty-invoice-state {
    text-align: center;
    padding: 1rem 0.75rem;
    color: var(--text-muted);
    font-size: var(--font-size-tertiary);
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.invoice-card {
    animation: slideIn 0.3s ease;
}


/* Invoice/Estimate View Modal Styles */
.invoice-view {
    padding: 1rem 0;
}

.invoice-view-status-row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.invoice-view-status-label {
    font-size: var(--font-size-tertiary);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.invoice-view-status-row .invoice-status-select {
    padding: 0.5rem 0.875rem;
    border-radius: 8px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    font-size: var(--font-size-tertiary);
    min-width: 8rem;
    cursor: pointer;
}

.invoice-view-section {
    margin-bottom: 1.5rem;
}

.invoice-view-section h4 {
    color: var(--text-secondary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-semibold);
    margin-bottom: 0.875rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.invoice-view-info {
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-normal);
}

.invoice-view-info div {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: flex-start;
    line-height: 1.5;
}

.invoice-view-info strong {
    color: var(--text-secondary);
    min-width: 100px;
    flex-shrink: 0;
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-secondary);
}

.invoice-view-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    background: var(--bg-secondary);
    border-radius: 8px;
    overflow: hidden;
    table-layout: fixed;
}

.invoice-view-table thead {
    background: var(--bg-tertiary);
}

.invoice-view-table th {
    padding: 0.75rem;
    text-align: left;
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    border-bottom: 1px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Column widths for line items table */
.invoice-view-table th:nth-child(1),
.invoice-view-table td:nth-child(1) {
    width: 45%;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.invoice-view-table th:nth-child(2),
.invoice-view-table td:nth-child(2) {
    width: 12%;
    text-align: right;
}

.invoice-view-table th:nth-child(3),
.invoice-view-table td:nth-child(3) {
    width: 20%;
    text-align: right;
}

.invoice-view-table th:nth-child(4),
.invoice-view-table td:nth-child(4) {
    width: 23%;
    text-align: right;
    font-weight: var(--font-weight-semibold);
}

.invoice-view-table td {
    padding: 0.75rem;
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-normal);
    border-bottom: 1px solid var(--border-color);
    vertical-align: top;
}

.invoice-view-table tbody tr:last-child td {
    border-bottom: none;
}

@media (hover: hover) and (pointer: fine) {
.invoice-view-table tbody tr:hover {
    background: var(--bg-tertiary);
}
}


.invoice-summary {
    background: var(--bg-secondary);
    padding: 1rem;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    margin-top: 1rem;
}

.invoice-summary-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-normal);
}

.invoice-summary-row span:first-child {
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-secondary);
}

.invoice-summary-row.invoice-total {
    border-top: 1px solid var(--border-color);
    margin-top: 0.5rem;
    padding-top: 1rem;
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
    color: var(--accent-primary);
}

.invoice-summary-row.invoice-total span:first-child {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-bold);
}

.invoice-view-notes {
    background: var(--bg-secondary);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    font-weight: var(--font-weight-normal);
    line-height: 1.5;
    white-space: pre-wrap;
}

/* Line items in view — single sheet, aligned rows (invoice + estimate) */
.invoice-view-line-sheet {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
    margin-top: 0.25rem;
}

.invoice-view-no-items {
    color: var(--text-muted);
    padding: 1.25rem 1rem;
    margin: 0;
    text-align: center;
    font-size: var(--font-size-secondary);
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px dashed var(--border-color);
}

.invoice-view-line-section {
    margin: 0;
    padding: 0.55rem 1rem 0.55rem 0.85rem;
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-secondary);
    background: var(--bg-tertiary);
    border-bottom: 1px solid var(--border-color);
    border-left: 4px solid var(--accent-green);
}

.invoice-view-line-row {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

.invoice-view-line-row:last-child {
    border-bottom: none;
}

.invoice-view-line-row-main {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.invoice-view-line-row-left {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.45rem;
    min-width: 0;
    flex: 1;
}

.invoice-view-line-title {
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    font-size: var(--font-size-tertiary);
    line-height: 1.35;
    min-width: 0;
}

.invoice-view-line-amount {
    font-family: var(--mono, ui-monospace, monospace);
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-bold);
    color: var(--accent-green);
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
    text-align: right;
    min-width: 4.5rem;
    padding-top: 0.05rem;
}

.invoice-view-line-formula {
    margin-top: 0.45rem;
    padding-top: 0.45rem;
    border-top: 1px solid var(--border-color);
    font-size: 0.78rem;
    line-height: 1.45;
    color: var(--text-tertiary);
    word-break: break-word;
}

.invoice-view-line-simple .invoice-view-line-row-main {
    align-items: center;
}

.invoice-view-line-simple .invoice-view-line-formula {
    border-top: none;
    margin-top: 0.35rem;
    padding-top: 0;
    font-variant-numeric: tabular-nums;
}

.invoice-view-mode-pill {
    flex-shrink: 0;
    font-size: 0.55rem;
    font-weight: 700;
    padding: 0.18rem 0.4rem;
    border-radius: 4px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    line-height: 1.2;
}

.invoice-view-mode-pill.area { background: rgba(104,211,145,0.15); color: #68d391; }
.invoice-view-mode-pill.materials { background: rgba(252,129,129,0.15); color: #fc8181; }
.invoice-view-mode-pill.labor { background: rgba(183,148,246,0.15); color: #b794f4; }
.invoice-view-mode-pill.volume { background: rgba(246,173,85,0.15); color: #f6ad55; }
.invoice-view-mode-pill.perimeter { background: rgba(99,179,237,0.15); color: #63b3ed; }
.invoice-view-mode-pill.custom { background: rgba(160,174,192,0.15); color: #a0aec0; }

/* Invoice View Actions - Buttons at the top (single row) */
.invoice-view-actions {
    padding: 0.75rem 0.5rem;
    border-bottom: 1px solid var(--border-color);
    background: transparent;
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
    justify-content: space-evenly;
    gap: 0.35rem;
    overflow-x: auto;
}

/* Ghost actions: transparent bg, accent green icon + label (Export / Convert / Delete / Edit) */
.invoice-view-actions .invoice-view-action-ghost {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    color: var(--accent-green) !important;
}

.invoice-view-actions .invoice-view-action-ghost i,
.invoice-view-actions .invoice-view-action-ghost span {
    color: var(--accent-green);
}

@media (hover: hover) and (pointer: fine) {
    .invoice-view-actions .invoice-view-action-ghost:hover {
        background: rgba(104, 211, 145, 0.1) !important;
        transform: none;
        border: none !important;
    }
}

.invoice-view-actions .invoice-view-action-ghost:active {
    opacity: 0.85;
}

.invoice-view-actions .btn {
    flex: 1 1 0;
    min-width: 0;
    max-width: none;
    padding: 0.5rem 0.125rem;
    font-size: var(--font-size-tertiary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    white-space: normal;
    text-align: center;
    min-height: 44px; /* Apple HIG minimum touch target */
    border-radius: 8px;
}

.invoice-view-actions .btn i {
    font-size: var(--font-size-base);
    flex-shrink: 0;
}

.invoice-view-actions .btn span {
    font-size: var(--font-size-tertiary);
    line-height: 1.1;
    display: block;
}

/* Handle buttons without spans (fallback) */
.invoice-view-actions .btn:not(:has(span)) {
    font-size: var(--font-size-tertiary);
}

#invoiceViewModal .modal-body {
    padding-bottom: 1rem;
    margin-bottom: 0;
    padding-top: 0;
}

/* Mobile responsive for view modal */
@media (max-width: 768px) {
    .invoice-view-table {
        font-size: var(--font-size-secondary);
    }
    
    .invoice-view-table th,
    .invoice-view-table td {
        padding: 0.5rem;
    }
    
    .invoice-view-section {
        margin-bottom: 1.25rem;
    }
    
    .invoice-view-section h4 {
        font-size: var(--font-size-secondary);
        margin-bottom: 0.75rem;
    }
    
    .invoice-view-info {
        font-size: var(--font-size-tertiary);
        gap: 0.5rem;
    }
    
    .invoice-view-info strong {
        min-width: 90px;
        font-size: var(--font-size-tertiary);
    }
    
    .invoice-view-table th,
    .invoice-view-table td {
        padding: 0.5rem 0.375rem;
        font-size: var(--font-size-tertiary);
    }
    
    .invoice-summary {
        padding: 0.875rem;
        margin-top: 0.875rem;
    }

    .invoice-view-line-sheet {
        border-radius: 10px;
    }

    .invoice-view-line-row {
        padding: 0.75rem 0.85rem;
    }

    .invoice-view-line-section {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    .invoice-view-line-amount {
        font-size: var(--font-size-tertiary);
        min-width: 4rem;
    }
    
    .invoice-view-actions {
        padding: 0.625rem 0.375rem;
        flex-wrap: nowrap;
        gap: 0.25rem;
    }
    
    .invoice-view-actions .btn {
        flex: 1 1 0;
        min-width: 0;
        padding: 0.5rem 0.125rem;
        font-size: var(--font-size-tertiary);
        min-height: 44px; /* Apple HIG minimum touch target */
        gap: 0.25rem;
        white-space: normal;
        overflow: visible;
        text-overflow: unset;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-align: center;
        border-radius: 8px;
    }
    
    .invoice-view-actions .btn i {
        font-size: var(--font-size-base);
        flex-shrink: 0;
    }
    
    .invoice-view-actions .btn span {
        font-size: var(--font-size-tertiary);
        line-height: 1.1;
        word-break: break-word;
    }
}

/* ═══════════════════════════════════════════════════════════════
   STRUCTURED LINE ITEMS (FROM TEMPLATES)
   ═══════════════════════════════════════════════════════════════ */

/* Structured line item container */
.line-item.structured-item {
    background: var(--bg-secondary);
}

.line-item.structured-item .line-item-content {
    padding: 0.75rem;
    gap: 0.5rem;
}

.structured-content {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    width: 100%;
}

/* Header with name + mode badge + per-row dimension unit (area / perimeter / volume) */
.structured-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}

.structured-header-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-left: auto;
    flex-shrink: 0;
}

/* Compact row toggle — same idea as workshop template creator .tc-row-unit-toggle */
.inv-row-unit-toggle {
    display: inline-flex;
    background: var(--bg-tertiary);
    border-radius: 6px;
    padding: 2px;
    gap: 1px;
    border: 1px solid var(--border-color);
    flex-shrink: 0;
    margin: 0.1rem 0 0.25rem auto;
    width: fit-content;
}

.inv-row-unit-opt {
    padding: 4px 6px;
    font-size: 0.65rem;
    font-weight: 600;
    border-radius: 4px;
    color: var(--text-secondary);
    background: transparent;
    border: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.12s ease, color 0.12s ease;
}

.inv-row-unit-opt.active {
    background: var(--accent-primary);
    color: #fff;
}

.inv-row-unit-opt:active {
    opacity: 0.85;
}

/* Labor method bar (template creator–style: Hourly / Daily / Fixed / Per unit) */
.inv-inline-labor-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin: 0.15rem 0 0.35rem 0;
    width: 100%;
}

.inv-inline-labor-btn {
    flex: 1;
    min-width: 4.25rem;
    padding: 6px 4px;
    border-radius: 8px;
    font-size: 0.68rem;
    font-weight: 600;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1.5px solid var(--border-color);
    cursor: pointer;
    transition: background 0.14s ease, color 0.14s ease, border-color 0.14s ease;
    -webkit-tap-highlight-color: transparent;
}

.inv-inline-labor-btn.active {
    background: var(--accent-primary);
    color: #fff;
    border-color: var(--accent-primary);
}

.inv-inline-labor-btn:active {
    opacity: 0.88;
}

.inv-perim-lengths {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 6px;
    width: 100%;
    justify-content: flex-start;
}

.inv-perim-leg {
    flex: 0 0 auto;
    max-width: none;
    min-width: 0;
    width: fit-content;
    min-width: unset;
}

.inv-perim-leg input.field-perim-leg {
    width: auto;
}

/* Perimeter leg fields shouldn't inherit calc-field min widths */
.calc-field.inv-perim-leg {
    min-width: unset;
}

.inv-perim-btn {
    height: 34px;
    padding: 0 10px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 700;
    font-size: 0.72rem;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.inv-perim-btn:active {
    opacity: 0.85;
}

.inv-perim-rem {
    width: 34px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.inv-perim-price-row {
    display: flex;
    flex-wrap: nowrap;
    align-items: flex-end;
    gap: 6px;
    width: 100%;
    margin-top: 2px;
}

.structured-name {
    /* Autosized via JS: start compact, grow with text */
    flex: 0 1 auto;
    width: 220px;
    min-width: 220px;
    max-width: 100%;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    outline: none;
    font-size: var(--font-size-tertiary);
    font-weight: var(--font-weight-normal);
    color: var(--text-primary);
    caret-color: var(--accent-green);
    padding: 0.5rem 0.625rem;
    border-radius: 6px;
    resize: none;
    overflow: hidden;
    line-height: 1.25;
}

.structured-name::placeholder {
    color: var(--text-muted);
    font-weight: var(--font-weight-normal);
}

/* Mode badge */
.mode-badge {
    font-size: 0.62rem;
    font-weight: var(--font-weight-bold);
    padding: 0.16rem 0.42rem;
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    flex-shrink: 0;
}

.mode-badge.area      { background: rgba(104,211,145,0.12); color: #68d391; border-color: rgba(104,211,145,0.3); }
.mode-badge.perimeter { background: rgba(99,179,237,0.12);  color: #63b3ed; border-color: rgba(99,179,237,0.3); }
.mode-badge.volume    { background: rgba(246,173,85,0.12);  color: #f6ad55; border-color: rgba(246,173,85,0.3); }
.mode-badge.materials { background: rgba(252,129,129,0.12); color: #fc8181; border-color: rgba(252,129,129,0.3); }
.mode-badge.labor     { background: rgba(183,148,246,0.12); color: #b794f4; border-color: rgba(183,148,246,0.3); }
.mode-badge.custom    { background: rgba(160,174,192,0.12); color: #a0aec0; border-color: rgba(160,174,192,0.3); }

/* Calculation fields container */
.calc-fields {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 6px;
    padding: 0.3rem 0 0;
}

/* Individual calculation field */
.calc-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 68px;
    flex-shrink: 1;
}

/* Content-sized calc fields (mobile): let inputs drive width */
.line-item.structured-item .calc-field:not(.calc-field-total) {
    flex: 0 0 auto;
    max-width: none;
    width: fit-content;
}

/* Total field should not expand to fill leftover space */
.calc-field.calc-field-total {
    flex: 0 0 110px;
    max-width: 110px;
    min-width: 110px;
}

/* Specific field width constraints */
.calc-field:has(.field-areaX),
.calc-field:has(.field-areaY),
.calc-field:has(.field-volumeX),
.calc-field:has(.field-volumeY),
.calc-field:has(.field-volumeZ),
.calc-field:has(.field-multiplier),
.calc-field:has(.field-hours),
.calc-field:has(.field-days),
.calc-field:has(.field-workers) {
    flex: 0 0 80px;
    max-width: 80px;
}

.calc-field:has(.field-quantity),
.calc-field:has(.field-laborUnitQty) {
    flex: 0 0 90px;
    max-width: 90px;
}

.calc-field:has(.field-unitPrice),
.calc-field:has(.field-rate),
.calc-field:has(.field-ratePerDay),
.calc-field:has(.field-laborRatePerUnit),
.calc-field:has(.field-laborAmount) {
    flex: 0 0 100px;
    max-width: 100px;
}

/* If autosizing is enabled (we set input widths in ch), prevent calc-field fixed flex-basis rules
   from forcing huge boxes in browser layouts. Total field remains fixed. */
.line-item.structured-item .calc-field:not(.calc-field-total) {
    flex: 0 0 auto !important;
    max-width: none !important;
    min-width: unset !important;
    width: fit-content;
}

.line-item.structured-item .inv-perim-lengths .calc-field.inv-perim-leg {
    flex: 0 0 auto !important;
    max-width: none !important;
    min-width: unset !important;
    width: fit-content;
}

.calc-field label {
    font-size: 10px;
    font-weight: var(--font-weight-normal);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    text-align: center;
    white-space: nowrap;
}

.calc-field input {
    padding: 6px 7px;
    font-size: var(--font-size-tertiary);
    text-align: center;
    background: var(--bg-tertiary);
    border: 1.5px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    width: 100%;
    transition: border-color 0.15s ease, background 0.15s ease;
    -moz-appearance: textfield;
}

/* Structured calc inputs should be content-sized (JS sets width in ch).
   Keep Total full width. This block is intentionally placed AFTER the base `.calc-field input` rule. */
.line-item.structured-item .calc-field input:not(.structured-total-input) {
    /* Baseline: ~3 digits wide, then JS grows via inline width (ch). */
    box-sizing: border-box;
    width: 55px;
    min-width: 55px;
}

.line-item.structured-item .calc-field input.structured-total-input {
    width: 100% !important;
}

/* Perimeter legs: ensure container and input can actually shrink */
.calc-field.inv-perim-leg {
    min-width: unset;
    flex: 0 0 auto;
}

.calc-field.inv-perim-leg input.field-perim-leg {
    width: auto !important;
    min-width: 0;
}

/* Remove spinner arrows for webkit browsers */
.calc-field input::-webkit-outer-spin-button,
.calc-field input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Remove number spinners globally (iOS/Android WKWebView, Safari, Chrome) */
input[type="number"] {
    -moz-appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Extra: hide UA shadow parts if exposed in DevTools */
[useragentpart="-webkit-inner-spin-button"],
[useragentpart="-webkit-outer-spin-button"] {
    display: none !important;
    width: 0 !important;
    height: 0 !important;
}

.calc-field input:focus {
    outline: none;
    border-color: var(--accent-green);
    background: var(--bg-secondary);
}

.calc-field input::placeholder {
    color: var(--text-tertiary);
    opacity: 0.5;
}

/* Separator (× symbol) */
.calc-sep {
    font-size: var(--font-size-tertiary);
    font-weight: 700;
    color: var(--text-muted);
    padding-bottom: 7px;
    flex-shrink: 0;
    align-self: flex-end;
}

/* Structured total as an inline field (to match simple items) */
.calc-field-total input.structured-total-input[readonly] {
    color: var(--accent-green);
    font-family: var(--mono);
    font-weight: var(--font-weight-semibold);
    background: var(--bg-tertiary);
    border: 1.5px solid var(--border-color);
}

/* Mobile responsive for structured items */
@media (max-width: 768px) {
    .calc-field:has(.field-areaX),
    .calc-field:has(.field-areaY),
    .calc-field:has(.field-volumeX),
    .calc-field:has(.field-volumeY),
    .calc-field:has(.field-volumeZ),
    .calc-field:has(.field-multiplier),
    .calc-field:has(.field-hours),
    .calc-field:has(.field-days),
    .calc-field:has(.field-workers) {
        flex: 0 0 70px;
        max-width: 70px;
    }
    
    .calc-field:has(.field-quantity),
    .calc-field:has(.field-laborUnitQty) {
        flex: 0 0 75px;
        max-width: 75px;
    }
    
    .calc-field:has(.field-unitPrice),
    .calc-field:has(.field-rate),
    .calc-field:has(.field-ratePerDay),
    .calc-field:has(.field-laborRatePerUnit),
    .calc-field:has(.field-laborAmount) {
        flex: 0 0 85px;
        max-width: 85px;
    }
    
    .calc-field input {
        font-size: var(--font-size-tertiary);
        padding: 10px 8px;
    }
    
    .calc-field label {
        font-size: 0.58rem;
    }
    
    .structured-name {
        font-size: var(--font-size-tertiary);
        max-width: 200px;
    }
    
    .structured-total-label {
        font-size: 0.78rem;
    }
    
    .structured-total {
        font-size: 0.9rem;
    }
}

/* Template selector dropdown */
#invoiceTemplateSelect,
#estimateTemplateSelect {
    margin-bottom: 1rem;
    padding: 0.75rem 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: var(--font-size-secondary);
    width: 100%;
    box-sizing: border-box;
    cursor: pointer;
}

#invoiceTemplateSelect:focus,
#estimateTemplateSelect:focus {
    outline: none;
    border-color: var(--accent-green);
    background: var(--bg-tertiary);
}

/* Section divider for template sections */
.line-item-section-divider {
    display: flex;
    align-items: center;
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 0;
    margin: 0.5rem 0;
    background: var(--bg-secondary);
    border-radius: 10px;
    border: 1px solid var(--border-color);
    overflow: hidden;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: auto;
}

.line-item-section-divider .section-divider-input {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    color: inherit;
    font: inherit;
    letter-spacing: inherit;
    text-transform: inherit;
    padding: 0.75rem;
    margin: 0;
    user-select: text;
    -webkit-user-select: text;
}

.line-item-section-divider .section-divider-input::placeholder {
    color: var(--text-muted);
}

.line-items-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.line-item-section-divider.dragging {
    opacity: 0.85;
    transform: scale(0.995);
    outline: 2px solid rgba(104, 211, 145, 0.35);
}

.line-item-section-divider.drag-over {
    outline: 2px dashed rgba(104, 211, 145, 0.55);
    outline-offset: 3px;
}

.line-item.drag-over {
    outline: 2px dashed rgba(104, 211, 145, 0.45);
    outline-offset: 3px;
}

/* Insert position indicator (drop between items) */
.line-item,
.line-item-section-divider {
    position: relative;
}

.line-item-main {
    position: relative;
}

.line-item.drag-over .line-item-main {
    outline: 2px dashed rgba(104, 211, 145, 0.45);
    outline-offset: 3px;
}

.line-item.dragging .line-item-main,
.line-item.structured-item.dragging .line-item-main {
    outline: 2px solid rgba(104, 211, 145, 0.35);
    outline-offset: 3px;
}

.line-item.drag-insert-before .line-item-main::before,
.line-item.drag-insert-after .line-item-main::after {
    content: '';
    position: absolute;
    left: 8px;
    right: 8px;
    height: 2px;
    background: rgba(104, 211, 145, 0.9);
    border-radius: 2px;
    pointer-events: none;
}

.line-item.drag-insert-before .line-item-main::before {
    top: -1px;
}

.line-item.drag-insert-after .line-item-main::after {
    bottom: -1px;
}

.drag-insert-before::before,
.drag-insert-after::after {
    content: '';
    position: absolute;
    left: 8px;
    right: 8px;
    height: 2px;
    background: rgba(104, 211, 145, 0.9);
    border-radius: 2px;
}

.drag-insert-before::before {
    top: -1px;
}

.drag-insert-after::after {
    bottom: -1px;
}

.line-item.dragging {
    opacity: 0.85;
    transform: scale(0.995);
    outline: 2px solid rgba(104, 211, 145, 0.35);
}

/* Make separator handle match regular line items */
.line-item-section-divider .line-item-drag-handle {
    width: 36px;
    min-width: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    cursor: grab;
    background: var(--bg-tertiary);
    border-right: 1px solid var(--border-color);
    touch-action: none;
    align-self: stretch;
}

.line-item-section-divider .line-item-drag-handle:active {
    cursor: grabbing;
    background: var(--accent-primary-alpha);
    color: var(--accent-primary);
}

.line-item-section-divider .line-item-drag-handle i {
    font-size: 14px;
}

.line-item-section-divider .section-divider-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
