:root {
    --primary-color: #4361ee;
    --primary-hover: #3a0ca3;
    --secondary-color: #f8f9fa;
    --success-color: #2ecc71;
    --text-main: #2b2d42;
    --text-muted: #8d99ae;
    --bg-color: #edf2f4;
    --glass-bg: rgba(255, 255, 255, 0.85);
    --glass-border: rgba(255, 255, 255, 0.5);
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.1);
    --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

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

body {
    font-family: 'Sarabun', sans-serif;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    min-height: 100vh;
    color: var(--text-main);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

/* App Container */
.app-container {
    width: 100%;
    max-width: 900px;
    background: var(--glass-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    box-shadow: var(--glass-shadow);
    padding: 40px;
    overflow: hidden;
    position: relative;
}

/* Header */
.app-header {
    text-align: center;
    margin-bottom: 30px;
}

.logo {
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, #8dc63f, #000000); /* Matched BAAN colors roughly */
    border-radius: 16px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto 15px;
    color: white;
    font-size: 28px;
    box-shadow: 0 10px 20px rgba(141, 198, 63, 0.3);
}

.app-header h1 {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 5px;
}

.app-header p {
    color: var(--text-muted);
    font-size: 14px;
}

/* Progress Bar */
.progress-container {
    display: flex;
    justify-content: space-between;
    position: relative;
    margin-bottom: 40px;
    padding: 0 20px;
}

.progress-container::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 20px;
    right: 20px;
    height: 4px;
    background: #e0e0e0;
    transform: translateY(-50%);
    z-index: 1;
    border-radius: 2px;
}

.progress-bar {
    position: absolute;
    top: 50%;
    left: 20px;
    height: 4px;
    background: var(--primary-color);
    transform: translateY(-50%);
    z-index: 2;
    transition: width 0.4s ease;
    border-radius: 2px;
    width: 0%;
}

.step-indicator {
    width: 36px;
    height: 36px;
    background: #e0e0e0;
    color: #fff;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 600;
    z-index: 3;
    transition: var(--transition);
    border: 4px solid var(--glass-bg);
}

.step-indicator.active {
    background: var(--primary-color);
    box-shadow: 0 0 0 4px rgba(67, 97, 238, 0.2);
}

.step-indicator.completed {
    background: var(--success-color);
}

/* Wizard Content */
.wizard-container {
    position: relative;
}

.wizard-step {
    display: none;
    animation: fadeIn 0.5s ease forwards;
}

.wizard-step.active {
    display: block;
}

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

h2 {
    font-size: 20px;
    margin-bottom: 20px;
    color: var(--primary-hover);
    border-bottom: 2px solid #eee;
    padding-bottom: 10px;
}

/* Forms */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
}

.form-group {
    margin-bottom: 15px;
}

.full-width {
    grid-column: 1 / -1;
}

label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 6px;
    color: #4a4a4a;
}

.required {
    color: #e74c3c;
}

input[type="text"],
input[type="date"],
input[type="email"],
input[type="tel"],
input[type="number"],
textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ced4da;
    border-radius: 6px;
    font-family: 'Prompt', sans-serif;
    font-size: 14px;
    transition: var(--transition);
    background: rgba(255, 255, 255, 0.9);
}

input:focus, textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.15);
}

.input-with-action {
    display: flex;
    gap: 10px;
}

.input-with-action input {
    flex: 1;
}

.help-text {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
    display: block;
}

/* Search Box */
.search-box {
    position: relative;
    margin-bottom: 20px;
}

.search-box i {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
}

.search-box input {
    padding-left: 40px;
    border-color: var(--primary-color);
    background: #f8f9fa;
}

.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    max-height: 200px;
    overflow-y: auto;
    z-index: 10;
    display: none;
}

.search-result-item {
    padding: 12px 15px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    transition: background 0.2s;
}

.search-result-item:hover {
    background: #f1f3f5;
}

.search-result-item strong {
    display: block;
    color: var(--primary-color);
}

.search-result-item span {
    font-size: 12px;
    color: var(--text-muted);
}

.mobile-label {
    display: none;
}

/* Items Table */
.items-container {
    overflow-x: auto;
    margin-bottom: 15px;
}

.table-items {
    width: 100%;
    border-collapse: collapse;
}

.table-items th {
    background: #f1f3f5;
    padding: 10px;
    text-align: left;
    font-size: 14px;
    font-weight: 500;
    border-bottom: 2px solid #dee2e6;
}

.table-items td {
    padding: 8px 5px;
    vertical-align: top;
}

.table-items .item-desc {
    width: 100%;
}

.table-items .item-qty {
    width: 80px;
    text-align: center;
}

.table-items .item-price {
    width: 100px;
    text-align: right;
}

.table-items .item-group {
    background: #fdfbf7;
    font-weight: 500;
}

.btn-remove {
    background: none;
    border: none;
    color: #e74c3c;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    transition: background 0.2s;
}
.btn-remove:hover {
    background: #fee;
}

.add-item-container {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.totals-preview {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    width: 300px;
    margin-left: auto;
    margin-bottom: 20px;
}

.total-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
    font-size: 14px;
}

.total-row.small {
    color: #666;
}

.total-row span:last-child {
    font-weight: 600;
}

/* Buttons */
.step-actions {
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
}

.step-actions.split {
    justify-content: space-between;
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-family: 'Prompt', sans-serif;
    font-weight: 500;
    font-size: 15px;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-sm {
    padding: 8px 16px;
    font-size: 13px;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 4px 10px rgba(67, 97, 238, 0.3);
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(67, 97, 238, 0.4);
}

.btn-secondary {
    background: white;
    color: var(--text-main);
    border: 1px solid #ced4da;
}

.btn-secondary:hover {
    background: #f8f9fa;
}

.btn-success {
    background: var(--success-color);
    color: white;
    box-shadow: 0 4px 10px rgba(46, 204, 113, 0.3);
}

.btn-success:hover {
    background: #27ae60;
    transform: translateY(-2px);
    box-shadow: 0 6px 15px rgba(46, 204, 113, 0.4);
}

.btn-icon {
    width: 44px;
    height: 44px;
    border-radius: 6px;
    border: 1px solid #ced4da;
    background: white;
    color: var(--primary-color);
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    justify-content: center;
    align-items: center;
}

.btn-icon:hover {
    background: #f8f9fa;
    border-color: var(--primary-color);
}

/* Summary Card */
.summary-card {
    background: rgba(255,255,255,0.6);
    border: 1px solid rgba(0,0,0,0.05);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
}

.summary-item {
    display: flex;
    margin-bottom: 10px;
}

.summary-item .label {
    width: 120px;
    font-weight: 500;
    color: var(--text-muted);
}

.summary-item .value {
    flex: 1;
    font-weight: 500;
}

.summary-item .highlight {
    color: var(--primary-color);
    font-weight: 600;
    font-size: 16px;
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.9);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.loading-overlay.active {
    opacity: 1;
    pointer-events: all;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 5px solid #f3f3f3;
    border-top: 5px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* =========================================================
   PDF Template Styling (Matched with user's PDF)
   ========================================================= */
.pdf-page {
    width: 210mm;
    height: 295mm;
    max-height: 295mm;
    padding: 10mm 15mm;
    background: white;
    font-family: 'Sarabun', sans-serif;
    color: #000;
    position: relative;
    box-sizing: border-box;
    font-size: 11px;
    line-height: 1.35;
    overflow: hidden;
}

/* Header Section */
.pdf-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 12px;
}

.pdf-logo-area {
    width: 95px; /* Increased logo size */
    margin-right: 15px;
}

.pdf-logo {
    width: 100%;
    height: auto;
    display: block;
}

.pdf-company-info {
    flex: 1;
}

.pdf-company-info h2 {
    font-size: 13.5px;
    font-weight: 700;
    margin: 0 0 1px 0;
    color: #000;
    border: none;
    padding: 0;
}

.pdf-company-info p {
    font-size: 10.5px;
    margin: 0;
}

.pdf-doc-title {
    text-align: right;
    width: 200px;
}

.tpl-copy-type {
    font-size: 13.5px;
    font-weight: 700;
    margin: 0 0 1px 0;
}

.pdf-doc-title .title-text {
    font-size: 14.5px;
    color: #3b82f6; /* Accent color matching blue/cyan title */
    margin: 0;
    border: none;
    padding: 0;
    font-weight: 700;
}

.pdf-doc-title .subtitle-text {
    font-size: 12px;
    color: #444;
    margin: 0;
}

/* Table-based Metadata Section */
.pdf-meta-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 12px;
}

.pdf-meta-table td {
    border: none !important;
    padding: 0 !important;
}

.pdf-meta-subtable {
    width: 100%;
    border-collapse: collapse;
}

.pdf-meta-subtable td {
    border: none !important;
    padding: 2.5px 0 !important;
    font-size: 11px;
}

.pdf-meta-subtable .meta-label {
    width: 135px;
    font-weight: 700;
    color: #000;
    text-align: left;
    white-space: nowrap;
}

.customer-info-table .meta-label {
    width: 135px; /* Reduced from 220px to remove gap and give values more room */
    padding-right: 15px !important;
}

.pdf-meta-subtable .meta-label.right {
    text-align: right;
    padding-right: 15px !important;
}

.doc-info-table .meta-label.right {
    width: 150px; /* Increased space on right doc column */
}

.pdf-meta-subtable .meta-value {
    text-align: left;
    word-break: break-word;
}

.pdf-meta-subtable .meta-value.right {
    text-align: right;
}

/* Table Section */
.pdf-body {
    margin-bottom: 15px;
}

.pdf-table {
    width: 100%;
    border-collapse: collapse;
}

.pdf-table th, .pdf-table td {
    border: 0.5px solid #000;
    padding: 3px 5px;
    font-size: 10.5px;
}

.pdf-table th {
    background-color: #d9d9d9;
    text-align: center;
    font-weight: 700;
    padding: 5px;
}

.pdf-table tbody td {
    border-bottom: none;
    border-top: none;
    height: 18px; /* Slightly compact empty rows height to fit within A4 */
}

/* Last row in tbody should have bottom border */
.pdf-table tbody tr:last-child td {
    border-bottom: 0.5px solid #000;
}

/* Number columns alignment */
.pdf-table td:nth-child(1),
.pdf-table td:nth-child(3) {
    text-align: center;
}

.pdf-table td:nth-child(4),
.pdf-table td:nth-child(5) {
    text-align: right;
}

.text-right {
    text-align: right;
}

.pdf-table tfoot td {
    border: none;
    padding: 3px 5px;
}

.text-amount-words {
    vertical-align: top;
    padding-top: 5px !important;
}

.amount-words-wrapper {
    background: #f2f2f2;
    text-align: center;
    border: none !important; /* No borders for the amount words card */
    border-radius: 4px;
    overflow: hidden;
}

.amount-title {
    font-weight: 700;
    padding: 3.5px;
    background: #d9d9d9;
    border: none !important; /* No border for the title */
}

.amount-text {
    padding: 5px;
    font-weight: 500;
}

/* Borderless styling for totals to align elegantly */
.footer-label-cell {
    border: none !important;
    text-align: right !important; /* Forced important to override nth-child centering */
    padding: 4px 10px 4px 0 !important;
}

.footer-value-cell {
    border: none !important;
    text-align: right !important;
    padding: 4px 0 !important;
}

.border-bottom {
    border-bottom: 0.5px solid #000 !important;
}

.no-border {
    border: none !important;
}

.grand-total-label {
    font-weight: 700;
    font-size: 12px;
    border: none !important;
    text-align: right !important; /* Forced important to override nth-child centering */
    padding: 6px 10px 6px 0 !important;
}

.grand-total-value {
    font-weight: 700;
    font-size: 12px;
    border: none !important;
    text-align: right !important;
    padding: 6px 0 !important;
}

/* Document Footer */
.pdf-footer {
    font-size: 10px;
    line-height: 1.3;
}

.footer-notes {
    margin-bottom: 12px;
}

.footer-signature {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
}

/* Responsive */
@media (max-width: 600px) {
    .form-grid {
        grid-template-columns: 1fr;
    }
    .step-actions.split {
        flex-direction: column-reverse;
        gap: 10px;
    }
    .step-actions.split .btn {
        width: 100%;
        justify-content: center;
    }
    .totals-preview {
        width: 100%;
    }

    /* Responsive Table to Card layout for mobile */
    .table-items thead {
        display: none;
    }
    
    .table-items, 
    .table-items tbody, 
    .table-items tr {
        display: block;
        width: 100%;
    }
    
    .table-items tr.item-row {
        background: rgba(255, 255, 255, 0.95);
        border: 1px solid #ced4da;
        border-radius: 12px;
        padding: 15px;
        margin-bottom: 15px;
        position: relative;
        box-shadow: 0 4px 10px rgba(0,0,0,0.03);
    }
    
    .table-items td {
        display: block;
        padding: 0;
        margin-bottom: 10px;
        width: 100% !important;
    }
    
    .table-items tr.standard-item {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }
    
    .table-items tr.standard-item td:nth-child(1) {
        grid-column: 1 / -1;
    }
    
    .table-items tr.standard-item td:nth-child(2),
    .table-items tr.standard-item td:nth-child(3) {
        grid-column: span 1;
        margin-bottom: 0;
    }
    
    /* Position the remove button at the top-right of the card */
    .table-items tr.standard-item td:nth-child(4) {
        position: absolute;
        top: 10px;
        right: 10px;
        width: auto !important;
        margin-bottom: 0;
    }
    
    .table-items tr.group-item td {
        margin-bottom: 0;
    }
    
    .mobile-label {
        display: block;
        font-size: 11px;
        color: var(--text-muted);
        margin-bottom: 4px;
        font-weight: 600;
        text-align: left;
    }

    /* Responsive History Table to Cards */
    .history-table-container {
        border: none !important;
        background: transparent !important;
        overflow: visible !important;
    }

    .history-table thead {
        display: none;
    }
    
    .history-table, 
    .history-table tbody, 
    .history-table tr {
        display: block;
        width: 100%;
    }
    
    .history-table tr {
        background: rgba(255, 255, 255, 0.95);
        border: 1px solid rgba(67, 97, 238, 0.08);
        border-radius: 16px;
        padding: 15px;
        margin-bottom: 12px;
        display: grid;
        grid-template-columns: 1fr auto;
        align-items: center;
        gap: 6px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.02);
    }
    
    .history-table td {
        display: block;
        padding: 0;
        border: none !important;
        background: transparent !important;
    }
    
    .history-table td:nth-child(1) {
        grid-row: 1;
        grid-column: 1;
        font-weight: 700;
        font-size: 15px;
        color: var(--primary-color);
    }
    
    .history-table td:nth-child(2) {
        grid-row: 1;
        grid-column: 2;
        text-align: right;
        font-size: 12px;
        color: var(--text-muted);
    }
    
    .history-table td:nth-child(4) {
        grid-row: 2;
        grid-column: 1 / -1;
        font-weight: 600;
        font-size: 14px;
        color: var(--text-main);
        padding: 4px 0;
    }
    
    .history-table td:nth-child(3) {
        grid-row: 3;
        grid-column: 1;
        font-size: 12px;
        color: var(--text-muted);
    }
    
    .history-table td:nth-child(3)::before {
        content: "Ref No: ";
        font-weight: 600;
    }
    
    .history-table td:nth-child(5) {
        grid-row: 3;
        grid-column: 2;
        text-align: right !important;
    }
    
    .history-table td[colspan="5"] {
        grid-row: 1;
        grid-column: 1 / -1;
        text-align: center !important;
        padding: 20px 0;
        color: var(--text-muted);
    }
}

/* History Section Styling */
.history-divider {
    margin: 30px 0 20px;
    border-top: 1px dashed rgba(0,0,0,0.15);
}

.history-section {
    text-align: left;
    animation: fadeIn 0.8s ease forwards;
}

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

.history-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--primary-hover);
    display: flex;
    align-items: center;
    gap: 8px;
}

.history-table-container {
    overflow-x: auto;
    border-radius: 12px;
    border: 1px solid rgba(0,0,0,0.06);
    background: rgba(255, 255, 255, 0.4);
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    text-align: left;
}

.history-table th {
    background: rgba(0,0,0,0.04);
    padding: 10px 15px;
    font-weight: 600;
    color: #4a4a4a;
    border-bottom: 1px solid rgba(0,0,0,0.08);
}

.history-table td {
    padding: 10px 15px;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    color: #2b2d42;
}

.history-table tr:last-child td {
    border-bottom: none;
}

.history-table tr:hover td {
    background: rgba(67, 97, 238, 0.03);
}

/* =========================================================
   Export Options UI (Mobile & Desktop Premium)
   ========================================================= */
.export-options-card {
    background: rgba(255, 255, 255, 0.85);
    border: 1px solid rgba(67, 97, 238, 0.15);
    border-radius: 16px;
    padding: 24px;
    margin-bottom: 25px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
}

.export-options-card h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--primary-hover);
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.radio-label {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    background: white;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    cursor: pointer;
    transition: var(--transition);
}

.radio-label:hover {
    border-color: #cbd5e1;
    background: #f8fafc;
}

.radio-label input[type="radio"] {
    margin-right: 12px;
    width: 20px;
    height: 20px;
    accent-color: var(--primary-color);
    cursor: pointer;
    flex-shrink: 0;
}

.radio-label.selected {
    border-color: var(--primary-color);
    background: rgba(67, 97, 238, 0.04);
    box-shadow: 0 4px 12px rgba(67, 97, 238, 0.08);
}

.radio-text {
    font-size: 15px;
    font-weight: 500;
    color: var(--text-main);
}

.radio-label.selected .radio-text {
    color: var(--primary-hover);
    font-weight: 600;
}

/* Signature Positioning inside PDF */
.footer-signature {
    position: relative !important;
}

.signature-image-wrapper {
    position: absolute;
    right: 65px;
    bottom: 12px; /* Shifted up from -15px to sit above/on top of the dots */
    width: 140px;
    height: 65px;
    pointer-events: none;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
}

.tpl-signature-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    mix-blend-mode: multiply; /* Make signature white background transparent */
}

/* Prevent Safari auto-detected links from showing underlines or custom colors in PDF */
.pdf-page a, 
.pdf-page a:hover,
.pdf-page [href] {
    text-decoration: none !important;
    color: inherit !important;
    border-bottom: none !important;
}

/* Regenerate Banner styling */
.regenerate-banner {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    border: 1px solid #f59e0b;
    border-radius: 12px;
    padding: 12px 20px;
    margin-bottom: 20px;
    color: #b45309;
    font-size: 14px;
    font-weight: 500;
    gap: 15px;
    box-shadow: 0 4px 10px rgba(245, 158, 11, 0.15);
    animation: fadeIn 0.4s ease forwards;
}

.banner-content {
    display: flex;
    align-items: center;
    gap: 10px;
}

.banner-content i {
    font-size: 18px;
    color: #d97706;
}

.regenerate-banner button {
    background: white;
    border: 1px solid #f59e0b;
    color: #b45309;
}

.regenerate-banner button:hover {
    background: #fffbeb;
    border-color: #d97706;
    color: #d97706;
}
