/* ===== INVOICE PAGE CSS ===== */
:root {
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --bg: #0a0e1a;
    --bg-card: #111827;
    --bg-hover: #1a2234;
    --bg-input: #0d1424;
    --border: #1e293b;
    --border-focus: #6366f1;
    --accent: #6366f1;
    --accent-light: #818cf8;
    --accent-glow: rgba(99, 102, 241, 0.15);
    --green: #10b981;
    --green-bg: rgba(16, 185, 129, 0.12);
    --yellow: #f59e0b;
    --red: #ef4444;
    --red-bg: rgba(239, 68, 68, 0.12);
    --text: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
    --transition: 0.2s ease;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

/* ===== NAV (hidden when embedded in iframe) ===== */
nav { display: none !important; }

/* ===== MAIN LAYOUT ===== */
.invoice-page {
    display: grid;
    grid-template-columns: 560px 1fr;
    gap: 1.5rem;
    max-width: 1600px;
    margin: 0 auto;
    padding: 1.5rem;
    min-height: 100vh;
}

/* ===== INPUT PANEL ===== */
.input-panel {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    overflow-y: auto;
    max-height: calc(100vh - 3rem);
    position: sticky;
    top: 1.5rem;
}

.input-panel h2 {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.section-title {
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--accent-light);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 1.25rem 0 0.6rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border);
}

.section-title:first-of-type {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem;
    margin-bottom: 0.6rem;
}

.form-row.full { grid-template-columns: 1fr; }
.form-row.triple { grid-template-columns: 1fr 1fr 1fr; }

.form-field {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-field label {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.form-field input,
.form-field select,
.form-field textarea {
    padding: 0.5rem 0.65rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.82rem;
    font-family: var(--font);
    outline: none;
    transition: all var(--transition);
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-field textarea {
    resize: vertical;
    min-height: 60px;
}

/* ===== ITEMS TABLE IN FORM ===== */
.items-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.item-row {
    display: grid;
    grid-template-columns: 55px 70px 1fr 85px 85px 32px;
    gap: 0.35rem;
    margin-bottom: 0.4rem;
    align-items: center;
}

.item-row input {
    padding: 0.4rem 0.5rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-size: 0.78rem;
    font-family: var(--font);
    outline: none;
    transition: all var(--transition);
}

.item-row input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.item-row input.amount-field {
    background: rgba(99, 102, 241, 0.06);
    color: var(--accent-light);
    font-weight: 600;
    text-align: right;
}

.item-labels {
    display: grid;
    grid-template-columns: 55px 70px 1fr 85px 85px 32px;
    gap: 0.35rem;
    margin-bottom: 0.3rem;
}

.item-labels span {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
}

.btn-remove-item {
    width: 28px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-remove-item:hover {
    background: var(--red-bg);
    color: var(--red);
    border-color: var(--red);
}

.btn-add-item {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.4rem 0.8rem;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 600;
    font-family: var(--font);
    cursor: pointer;
    transition: all var(--transition);
    margin-top: 0.4rem;
}

.btn-add-item:hover {
    border-color: var(--green);
    color: var(--green);
    background: var(--green-bg);
}

/* ===== TOTALS IN FORM ===== */
.totals-grid {
    display: grid;
    grid-template-columns: 1fr 120px;
    gap: 0.4rem;
    margin-top: 0.75rem;
}

.totals-grid label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
}

.totals-grid input {
    padding: 0.4rem 0.5rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-size: 0.82rem;
    font-family: var(--font);
    text-align: right;
    outline: none;
    transition: all var(--transition);
}

.totals-grid input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.totals-grid input.readonly {
    background: rgba(99, 102, 241, 0.06);
    color: var(--accent-light);
    font-weight: 700;
    cursor: default;
}

.total-final {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 2px solid var(--accent);
}

.total-final label {
    font-weight: 800 !important;
    color: var(--text) !important;
    font-size: 0.9rem !important;
}

.total-final input {
    font-size: 1rem !important;
    font-weight: 800 !important;
    color: var(--green) !important;
    background: rgba(16, 185, 129, 0.08) !important;
    border-color: rgba(16, 185, 129, 0.3) !important;
}

/* ===== ACTION BUTTONS ===== */
.action-bar {
    display: flex;
    gap: 0.5rem;
    margin-top: 1.25rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.btn-print {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    background: linear-gradient(135deg, var(--accent), #7c3aed);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 700;
    font-family: var(--font);
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3);
}

.btn-print:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 24px rgba(99, 102, 241, 0.45);
}

.btn-clear {
    padding: 0.7rem 1rem;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    font-weight: 600;
    font-family: var(--font);
    cursor: pointer;
    transition: all var(--transition);
}

.btn-clear:hover {
    color: var(--red);
    border-color: var(--red);
    background: var(--red-bg);
}

/* ===== PREVIEW PANEL ===== */
.preview-panel {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 1rem;
}

/* ===== INVOICE PREVIEW (matches PDF format) ===== */
.invoice-sheet {
    width: 8.5in;
    min-height: 11in;
    background: #fff;
    color: #000;
    padding: 0.6in 0.65in;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    font-family: 'Arial', 'Helvetica Neue', Helvetica, sans-serif;
    font-size: 10pt;
    line-height: 1.4;
    position: relative;
}

/* Invoice header */
.inv-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 0.3in;
}

.inv-company {
    font-size: 14pt;
    font-weight: 700;
    font-style: italic;
}

.inv-company-addr {
    font-size: 9pt;
    color: #333;
    margin-top: 2pt;
    line-height: 1.5;
}

.inv-company-phone {
    margin-top: 8pt;
    font-size: 9pt;
    color: #333;
}

.inv-title-block {
    text-align: right;
}

.inv-title {
    font-size: 26pt;
    font-weight: 700;
    color: #333;
    letter-spacing: 3pt;
    font-style: italic;
}

.inv-meta {
    margin-top: 6pt;
    font-size: 9pt;
    text-align: left;
    display: inline-block;
}

.inv-meta-row {
    display: flex;
    gap: 8pt;
}

.inv-meta-label {
    min-width: 90pt;
    text-align: right;
    color: #555;
}

.inv-meta-value {
    font-weight: 600;
}

/* Bill To / Ship To */
.inv-addresses {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.2in;
    margin-bottom: 0.15in;
}

.inv-addr-box {
    border: 1px solid #999;
}

.inv-addr-label {
    background: #ddd;
    padding: 3pt 6pt;
    font-weight: 700;
    font-size: 9pt;
    border-bottom: 1px solid #999;
}

.inv-addr-content {
    padding: 6pt;
    font-size: 9pt;
    min-height: 50pt;
    white-space: pre-line;
    line-height: 1.5;
}

/* Order metadata table */
.inv-order-meta {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 0.1in;
    font-size: 9pt;
}

.inv-order-meta th {
    background: #ddd;
    border: 1px solid #999;
    padding: 3pt 6pt;
    font-weight: 700;
    text-align: center;
    font-size: 8pt;
}

.inv-order-meta td {
    border: 1px solid #999;
    padding: 3pt 6pt;
    text-align: center;
    font-size: 9pt;
}

/* Items table */
.inv-items {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 0;
    font-size: 9pt;
}

.inv-items thead th {
    background: #ddd;
    border: 1px solid #999;
    padding: 4pt 6pt;
    font-weight: 700;
    font-size: 8.5pt;
    text-align: center;
}

.inv-items thead th.col-qty { width: 65pt; }
.inv-items thead th.col-item { width: 80pt; }
.inv-items thead th.col-desc { width: auto; }
.inv-items thead th.col-price { width: 80pt; }
.inv-items thead th.col-amount { width: 80pt; }

.inv-items tbody td {
    border-left: 1px solid #999;
    border-right: 1px solid #999;
    padding: 3pt 6pt;
    vertical-align: top;
    font-size: 9pt;
}

.inv-items tbody td.num {
    text-align: right;
}

.inv-items tbody td.center {
    text-align: center;
}

.inv-items-spacer {
    border-left: 1px solid #999;
    border-right: 1px solid #999;
    border-bottom: 1px solid #999;
    min-height: 200pt;
}

/* Totals section */
.inv-footer {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-top: 0;
}

.inv-check-memo {
    font-size: 9pt;
    color: #333;
    padding-top: 12pt;
}

.inv-totals {
    width: 320pt;
}

.inv-totals table {
    width: 100%;
    border-collapse: collapse;
    font-size: 9pt;
}

.inv-totals td {
    padding: 2pt 6pt;
    border: 1px solid #999;
}

.inv-totals .label-cell {
    font-weight: 600;
    background: #f5f5f5;
    width: 180pt;
}

.inv-totals .value-cell {
    text-align: right;
    width: 140pt;
}

.inv-totals .total-row td {
    font-weight: 800;
    font-size: 10pt;
    background: #e8e8e8;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 1100px) {
    .invoice-page {
        grid-template-columns: 1fr;
    }
    .input-panel {
        position: static;
        max-height: none;
    }
    .invoice-sheet {
        width: 100%;
        min-height: auto;
        padding: 0.4in;
    }
}

/* ===== PRINT STYLES ===== */
@media print {
    * { margin: 0; padding: 0; }
    
    body {
        background: #fff !important;
        color: #000 !important;
    }

    .input-panel,
    .action-bar,
    nav,
    .preview-panel {
        display: none !important;
    }

    .invoice-page {
        display: block !important;
        padding: 0 !important;
        margin: 0 !important;
        max-width: none !important;
    }

    .invoice-sheet {
        box-shadow: none !important;
        border-radius: 0 !important;
        width: 100% !important;
        padding: 0.4in 0.5in !important;
        min-height: auto !important;
        page-break-after: always;
    }

    .inv-items-spacer {
        min-height: 100pt !important;
    }
}

/* ===== SCROLLBAR ===== */
.input-panel::-webkit-scrollbar { width: 5px; }
.input-panel::-webkit-scrollbar-track { background: var(--bg); }
.input-panel::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }

/* ===== CUSTOMER SEARCH ===== */
.customer-search-wrap {
    position: relative;
    margin-bottom: 0.6rem;
}

.customer-search-wrap input {
    width: 100%;
    padding: 0.55rem 0.75rem 0.55rem 2rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.85rem;
    font-family: var(--font);
    outline: none;
    transition: all var(--transition);
}

.customer-search-wrap input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.customer-search-wrap::before {
    content: '🔍';
    position: absolute;
    left: 0.6rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.8rem;
    pointer-events: none;
    z-index: 1;
}

.customer-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--accent);
    border-radius: var(--radius-sm);
    max-height: 200px;
    overflow-y: auto;
    z-index: 50;
    display: none;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.customer-dropdown.show { display: block; }

.customer-dropdown-item {
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 0.82rem;
    border-bottom: 1px solid var(--border);
    transition: all var(--transition);
}

.customer-dropdown-item:last-child { border-bottom: none; }

.customer-dropdown-item:hover {
    background: var(--accent-glow);
    color: var(--accent-light);
}

.customer-dropdown-item .cust-id {
    font-weight: 700;
    color: var(--accent-light);
}

.customer-dropdown-item .cust-name {
    color: var(--text-secondary);
    font-size: 0.75rem;
}

/* ===== HISTORY PANEL ===== */
.history-section {
    margin-top: 1rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border);
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.6rem;
}

.history-header h3 {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--accent-light);
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.history-list {
    max-height: 250px;
    overflow-y: auto;
}

.history-card {
    padding: 0.55rem 0.65rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 0.4rem;
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-card:hover {
    border-color: var(--accent);
    background: var(--accent-glow);
}

.history-card .hist-inv-num {
    font-weight: 700;
    font-size: 0.82rem;
    color: var(--accent-light);
}

.history-card .hist-customer {
    font-size: 0.72rem;
    color: var(--text-secondary);
    margin-top: 1px;
}

.history-card .hist-right {
    text-align: right;
}

.history-card .hist-amount {
    font-weight: 700;
    font-size: 0.82rem;
    color: var(--green);
}

.history-card .hist-date {
    font-size: 0.68rem;
    color: var(--text-muted);
}

.history-empty {
    color: var(--text-muted);
    font-size: 0.78rem;
    text-align: center;
    padding: 1rem;
}

/* ===== SAVE BUTTON ===== */
.btn-save {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    background: linear-gradient(135deg, var(--green), #059669);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 700;
    font-family: var(--font);
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 4px 16px rgba(16, 185, 129, 0.3);
}

.btn-save:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 24px rgba(16, 185, 129, 0.45);
}

/* ===== TOAST ===== */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    padding: 0.7rem 1.5rem;
    background: var(--bg-card);
    border: 1px solid var(--green);
    color: var(--green);
    border-radius: 100px;
    font-size: 0.85rem;
    font-weight: 600;
    font-family: var(--font);
    opacity: 0;
    transition: all 0.3s ease;
    z-index: 999;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    border-color: var(--red);
    color: var(--red);
}

/* ===== ANIMATIONS ===== */
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.input-panel, .preview-panel { animation: fadeIn 0.4s ease-out; }
