/**
 * pages/manual.css — Manual Invoice Mode
 * Item cards, quantity controls, preview section, table view,
 * sticky action bar, required-fields indicator, unit toggle.
 */

/* ============================================
   MANUAL VIEW SPECIFIC OVERRIDES
   ============================================ */
#manual-view .input-group {
    margin-bottom: 4px;
}

#manual-view .view-toggle-container {
    margin-bottom: 4px;
}

/* ============================================
   ITEM CARDS CONTAINER
   ============================================ */
#manual-items-container {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 12px;
}

/* ============================================
   ITEM CARD
   ============================================ */
.item-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: var(--spacing-md);
    position: relative;
    transition: transform 0.2s ease-out;
    background: var(--bg-card);
    z-index: 2;
    margin-bottom: 0;
    outline: 2px solid transparent;
    box-shadow: var(--app-shadow-sm);
}

.item-card.is-selected {
    outline: 2px solid var(--primary) !important;
    box-shadow: var(--app-shadow-md);
}

#manual-items-container.is-multi-select .item-card:not(.is-selected) {
    opacity: 0.6;
}

/* Animations */
@keyframes card-in {
    from {
        opacity: 0;
        transform: translateY(15px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes card-out {
    from {
        opacity: 1;
        transform: scale(1);
    }

    to {
        opacity: 0;
        transform: scale(0.95);
    }
}

.item-card.new-item {
    animation: card-in 400ms cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}

.item-card.removing {
    animation: card-out 400ms cubic-bezier(0.5, 0, 0.7, 0.4) forwards;
    pointer-events: none;
}

.item-card.swiped-left {
    transform: translateX(-120px);
}

.item-row {
    display: flex;
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-xs);
}

.item-row.full {
    flex-direction: column;
}

.remove-item-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    color: #ff999a;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
}

/* ============================================
   QUANTITY CONTROL
   ============================================ */
.qty-control {
    display: flex;
    height: 27px;
    align-items: center;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-input);
    overflow: hidden;
}

.qty-btn {
    padding: 14px 12px;
    background: var(--bg-body);
    border: none;
    cursor: pointer;
    font-weight: bold;
    color: var(--text-main);
    transition: background 0.15s;
}

.qty-btn:hover {
    background: var(--border-color);
}

.qty-input.item-qty {
    font-weight: 500;
}

.qty-input {
    width: 35px;
    text-align: center;
    border: none;
    background: var(--bg-input);
    color: var(--text-main);
    -moz-appearance: textfield;
    appearance: textfield;
    text-decoration: none;
    text-decoration-style: none;
    box-decoration-break: none;
}

/* ============================================
   UNIT SWITCH (Pcs / Lot compact toggle)
   ============================================ */
.unit-switch {
    display: inline-flex;
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    font-size: 0.65rem;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    line-height: 1;
    align-items: center;
}

.unit-switch .unit-opt {
    padding: 2px 6px;
    color: var(--text-muted);
    transition: all 0.2s ease;
}

.unit-switch .unit-opt.active {
    background: var(--primary);
    color: #fff;
}

.unit-switch .unit-opt.active[data-unit="lot"] {
    background: var(--tertiary);
}

/* ============================================
   PREVIEW SECTION
   ============================================ */
.preview-section {
    margin-top: var(--spacing-lg);
    border-top: 2px dashed var(--border-color);
    padding-top: var(--spacing-md);
}

.preview-cards {
    display: flex;
    gap: var(--spacing-md);
    margin-bottom: var(--spacing-md);
}

.preview-card {
    flex: 1;
    aspect-ratio: 1/1.414;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

.invoice-preview {
    background-color: var(--invoice-bg);
}

.letter-preview {
    background-color: var(--letter-bg);
}

.preview-content {
    background: white;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 0;
    box-shadow: var(--app-shadow-sm);
    overflow: hidden;
    position: relative;
    touch-action: none;
    -webkit-overflow-scrolling: auto;
    pointer-events: none !important; /* Allow clicks to pass through to parent .preview-card */
}

/* A4 preview wrapper & iframe */
.a4-preview-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

.a4-preview-frame {
    width: 794px;
    height: 1123px;
    transform-origin: top center;
    border: 0;
    display: block;
    flex: 0 0 auto;
}

/* HTML render container (A4 at 96dpi) */
.html-preview-container {
    width: 794px;
    height: 1123px;
    transform-origin: top left;
    background: white;
    padding: 40px;
    font-family: 'Helvetica', sans-serif;
    color: #000;
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

.html-preview-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    border-bottom: 2px solid #eee;
    padding-bottom: 10px;
}

.html-preview-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    font-size: 12px;
}

.html-preview-table th {
    background: #4A90E2;
    color: white;
    padding: 5px;
    text-align: left;
}

.html-preview-table td {
    border-bottom: 1px solid #eee;
    padding: 5px;
}

.html-preview-total {
    margin-top: 20px;
    text-align: right;
    font-weight: bold;
    font-size: 14px;
}

/* ============================================
   STICKY ACTION BAR
   ============================================ */
@media (max-width: 1023px) {
    .action-bar-sticky {
        position: fixed;
        bottom: 76px;
        left: 0;
        right: 0;
        background: var(--bg-card);
        padding: var(--spacing-md);
        box-shadow:
            0 -1px 3px color-mix(in srgb, var(--active-tab-glow) 6%, transparent),
            0 -6px 16px color-mix(in srgb, var(--active-tab-glow) 10%, transparent);
        display: flex;
        align-items: center;
        justify-content: space-between;
        z-index: 990;
        transition: transform 0.3s ease;
        border-top: 1px solid var(--border-color);
    }

    .action-bar-sticky.hide {
        transform: translateY(220%);
    }

    /* Target #manual-action-bar specifically on mobile for dual-pill layout */
    #manual-action-bar {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        padding: 0 16px !important;
        pointer-events: none;
    }

    #manual-action-bar .mobile-action-content {
        display: flex !important;
        flex-direction: row !important;
        /* Total on left, buttons on right */
        flex-wrap: wrap !important;
        /* Wrap gracefully on very narrow screens */
        align-items: center;
        justify-content: space-between !important;
        /* Centered layout for symmetry */
        width: 100%;
        gap: 24px !important;
        pointer-events: none;
        /* Clicks pass through the gap between pills */
    }

    /* Right pill: Save and Download buttons */
    #manual-action-bar .mobile-action-content>div:not(.total-display) {
        background: var(--bg-card) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 20px !important;
        padding: 6px 8px !important;
        display: flex !important;
        gap: 4px !important;
        align-items: center;
        justify-content: center !important;
        /* Centrally align buttons inside the pill */
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12) !important;
        height: 46px !important;
        width: 160px !important;
        max-width: 160px !important;
        flex: 0 0 160px !important;
        box-sizing: border-box;
        pointer-events: auto;
        /* Enable clicks only on this pill */
    }

    /* Left pill: Total amount display */
    #manual-action-bar .mobile-action-content .total-display {
        background: var(--bg-card) !important;
        border: 1px solid var(--border-color) !important;
        border-radius: 20px !important;
        padding: 4px 8px !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        align-items: center !important;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12) !important;
        height: 46px !important;
        width: 150px !important;
        max-width: 150px !important;
        flex: 0 0 150px !important;
        box-sizing: border-box;
        pointer-events: auto;
        /* Enable clicks only on this pill */
    }

    #manual-action-bar .total-display small {
        font-size: 0.6rem !important;
        line-height: 1 !important;
        margin-bottom: 2px !important;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: var(--text-muted);
    }

    #manual-action-bar .total-display span {
        font-size: 0.85rem !important;
        line-height: 1.1 !important;
        font-weight: 700 !important;
        color: var(--active-tab-glow) !important;
    }

    /* Compact button styling on mobile inside the left pill */
    #manual-action-bar #btn-save-only-mobile {
        height: 32px !important;
        padding: 0 8px !important;
        font-size: 0.75rem !important;
        border-radius: 20px !important;
        border: 1.5px solid var(--border-color) !important;
        background: transparent !important;
        color: var(--text-main) !important;
    }

    #manual-action-bar .download-split-btn {
        height: 32px !important;
        display: flex !important;
        align-items: center !important;
    }

    #manual-action-bar #btn-download-mobile {
        height: 32px !important;
        padding: 0 8px !important;
        font-size: 0.75rem !important;
        border-radius: 20px 0 0 20px !important;
        border: none !important;
        background: var(--primary) !important;
        color: white !important;
    }

    #manual-action-bar #btn-download-split-arrow-mobile {
        height: 32px !important;
        padding: 0 6px !important;
        border-radius: 0 20px 20px 0 !important;
        border: none !important;
        background: var(--primary) !important;
        color: white !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
}

/* Desktop-only sections hidden on mobile only */
@media (max-width: 1023px) {

    .action-bar-total,
    .action-bar-buttons,
    .action-bar-divider {
        display: none;
    }
}

/* Mobile-only action content — shown on mobile only */
@media (max-width: 1023px) {
    .mobile-action-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        gap: 10px;
    }
}

.total-display {
    display: flex;
    flex-direction: column;
}

.total-display span {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--active-tab-glow);
}

.batch-delete-btn {
    background-color: #ff4d4f;
    color: white;
    border: none;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow:
        0 1px 3px rgba(255, 77, 79, 0.1),
        0 4px 15px rgba(255, 77, 79, 0.3);
}

/* ============================================
   FLOATING REQUIRED FIELDS INDICATOR
   ============================================ */
.floating-indicator {
    position: absolute;
    right: 20px;
    bottom: calc(100% + 15px);
    background: #ff4d4f;
    color: white;
    padding: 10px 16px;
    border-radius: 30px;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 15px rgba(255, 77, 79, 0.4);
    z-index: 85;
    cursor: default;
    font-weight: 400;
    font-size: 0.6rem;
    letter-spacing: 1px;
    animation: bounce-in 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    pointer-events: none;
    white-space: nowrap;
}

.floating-indicator.hidden {
    display: none;
    transform: translateY(20px);
    opacity: 0;
}

.indicator-badge {
    background: white;
    color: #ff4d4f;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6rem;
    font-weight: 800;
}

/* ============================================
   TABLE VIEW
   ============================================ */
.table-view-container {
    overflow-x: auto;
    width: 100%;
    margin-bottom: 20px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    box-shadow: var(--app-shadow-sm);
}

.table-note {
    width: 250px;
}

@media (min-width: 768px) {
    .table-view-container {
        width: 95vw;
        max-width: 1000px;
        position: relative;
        left: 50%;
        transform: translateX(-50%);
    }

    .table-note {
        width: none !important;
        max-width: auto;
    }
}

@media (max-width: 767px) {
    .table-note {
        width: 250px !important;
    }
}

.table-view-container::-webkit-scrollbar {
    height: 6px;
}

.table-view-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}

.item-table {
    width: 100%;
    min-width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    table-layout: auto;
    border: 1px solid var(--border-color);
}

.item-table tr:nth-child(even) {
    background-color: rgba(0, 0, 0, 0.02);
}

.item-table th,
.item-table td {
    white-space: nowrap;
    padding: 8px;
    text-align: left;
    border-right: 1px solid rgba(0, 0, 0, 0.08);
}

.item-table th:last-child,
.item-table td:last-child {
    border-right: none;
}

.item-table thead {
    background: var(--bg-body);
    border-bottom: 2px solid var(--border-color);
}

.item-table th {
    background: var(--bg-body);
    font-size: 0.85rem;
    color: var(--text-muted);
}

.item-table tbody tr {
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.item-table tbody tr:hover {
    background: rgba(0, 0, 0, 0.02);
}

.item-table td input,
.item-table td textarea,
.item-table td select {
    width: 100%;
    border: none;
    background: transparent;
    font-size: 0.9rem;
    color: var(--text-main);
    font-family: inherit;
    resize: none;
    outline: none;
}

.item-table .item-name {
    width: auto;
    white-space: nowrap;
}

.item-table .item-note {
    overflow: hidden;
    word-break: break-word;
    white-space: pre-wrap;
}

.item-table td input:focus,
.item-table td textarea:focus {
    background: rgba(74, 144, 226, 0.05);
    border-radius: 3px;
}

/* Fix icon overlapping in Barang cell */
/* Core fix: Hidden Span via CSS Grid for self-sizing inputs */
.input-sizer {
    display: inline-grid;
    vertical-align: middle;
    align-items: center;
    position: relative;
    min-width: 1ch;
    width: auto;
}

.input-sizer::after {
    content: attr(data-value) "  ";
    /* extra space for comfort */
    visibility: hidden;
    white-space: pre;
    grid-area: 1 / 1;
    font: inherit;
    padding: 4px;
    border: none;
}

.input-sizer input,
.input-sizer select {
    grid-area: 1 / 1;
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
    border: none;
    background: transparent;
    font: inherit;
    padding: 4px;
    margin: 0;
    outline: none;
}

.item-table td .input-with-icon {
    display: flex;
    align-items: center;
    gap: 4px;
}

.item-table td .item-price-format {
    width: 90%;
    min-width: 60px;
}

/* Column width constraints & Sizing (Excel-like Auto Width) */
.item-table th,
.item-table td {
    width: 1%;
    white-space: nowrap;
}

.item-table th {
    text-align: center;
}

/* Table Container */
.table-with-checkboxes {
    width: 100%;
}

/* Table columns constraints (9 columns) */
.item-table th:nth-child(1),
.item-table td:nth-child(1) {
    /* Checkbox */
    width: 36px;
    text-align: center;
}

.item-table th:nth-child(2),
.item-table td:nth-child(2) {
    /* No */
    width: 36px;
    text-align: center;
}

.item-table th:nth-child(4) {
    text-align: center;
}

.item-table td:nth-child(4) {
    /* Harga */
    text-align: right;
    width: auto;
}

.item-table th:nth-child(5),
.item-table td:nth-child(5) {
    /* Tipe */
    min-width: 110px;
}

.item-table th:nth-child(6),
.item-table td:nth-child(6) {
    /* Qty */
    text-align: center;
}

.item-table th:nth-child(7),
.item-table td:nth-child(7) {
    /* Note */
    text-align: left;
    width: 100%;
    white-space: normal;
}

.item-table th:nth-child(8),
.item-table td:nth-child(8) {
    /* Total */
    text-align: right;
    width: 100px;
}

.item-table th:nth-child(9),
.item-table td:nth-child(9) {
    /* Actions */
    width: 40px;
    margin: 0 2px;
    text-align: center;
}

/* Row hover & selection states */
.item-table tbody tr.item-table-row {
    transition: background-color 0.15s ease;
}

.item-table tbody tr.item-table-row:hover,
.item-table tbody tr.item-table-row.row-hover {
    background-color: rgba(255, 255, 255, 0.04) !important;
}

.item-table tbody tr.item-table-row.is-selected {
    background-color: rgba(39, 174, 96, 0.09) !important;
}

.item-table tbody tr.item-table-row.is-selected:hover,
.item-table tbody tr.item-table-row.is-selected.row-hover {
    background-color: rgba(39, 174, 96, 0.14) !important;
}

/* Custom checkable elements styling */
.table-with-checkboxes input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 15px;
    height: 15px;
    border: 1.5px solid rgba(255, 255, 255, 0.25);
    border-radius: 4px;
    background: transparent;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    vertical-align: middle;
    transition: border-color 0.15s, background-color 0.15s;
    flex-shrink: 0;
    position: relative;
}

.table-with-checkboxes input[type="checkbox"]:hover {
    border-color: rgba(255, 255, 255, 0.5);
}

.table-with-checkboxes input[type="checkbox"]:checked {
    background-color: var(--primary, #27ae60);
    border-color: var(--primary, #27ae60);
}

.table-with-checkboxes input[type="checkbox"]:checked::after {
    content: '';
    position: absolute;
    left: 4.5px;
    top: 1px;
    width: 3px;
    height: 6px;
    border: 2px solid #fff;
    border-top: none;
    border-left: none;
    transform: rotate(45deg);
}


/* Quantity in table view */
.item-table td .item-qty,
.item-table td .table-qty-input {
    width: auto;
    text-align: center;
}

.remove-item-btn {
    color: #ff4d4f;
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.7;
    transition: opacity 0.2s;
}

/* Ensure delete btn in table stays visible even when labels are hidden */
.labels-hidden .item-table .remove-item-btn {
    opacity: 0.7 !important;
    pointer-events: auto !important;
}

.item-card .remove-item-btn {
    position: absolute;
    top: 10px;
    right: 10px;
}

.item-table .remove-item-btn {
    position: static;
    margin: 0 auto;
}

.remove-item-btn:hover {
    opacity: 1;
}

.template-picker-btn {
    position: absolute;
    right: 10px;
    top: 48%;
    transform: translateY(-50%);
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    color: var(--text-muted);
}

/* Hide spinners on table qty input */
.table-qty-input::-webkit-outer-spin-button,
.table-qty-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.table-qty-input[type="text"] {
    -moz-appearance: textfield;
    appearance: textfield;
    text-align: center;
    font-weight: 600;
}