:root {
    /* Colors */
    --primary: #4F46E5;
    --primary-hover: #4338CA;
    --secondary: #10B981;
    --secondary-hover: #059669;
    --bg-color: #F3F4F6;
    --surface: #FFFFFF;
    --text-main: #111827;
    --text-muted: #6B7280;
    --border: #E5E7EB;
    --danger: #EF4444;
    --danger-hover: #DC2626;
    
    /* Typography */
    --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-doc: 'Noto Sans Thai', 'Inter', sans-serif;
    
    /* Effects */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --transition: all 0.2s ease-in-out;
}

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

body {
    font-family: var(--font-ui);
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Mobile Tabs */
.mobile-tabs {
    display: none;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.5rem;
    gap: 0.5rem;
    z-index: 20;
}

.tab-btn {
    flex: 1;
    padding: 0.75rem;
    background: #F3F4F6;
    color: var(--text-muted);
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 0.875rem;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
}

.tab-btn.active {
    background: var(--primary);
    color: white;
}

/* Sidebar Controls */
.controls-panel {
    width: 450px;
    background-color: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
    z-index: 10;
}

.panel-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(135deg, var(--primary) 0%, #7C3AED 100%);
    color: white;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

.logo i {
    font-size: 1.5rem;
}

.logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
}

.panel-header p {
    font-size: 0.875rem;
    opacity: 0.9;
}

.form-sections {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.form-group {
    background: #F9FAFB;
    padding: 1.25rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
}

.form-group h3 {
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Custom Form Section Colors */
.form-group.bg-seller {
    background-color: #EFF6FF;
    border-color: #BFDBFE;
}
.form-group.bg-seller h3 {
    color: #1D4ED8;
}

.form-group.bg-document {
    background-color: #ECFDF5;
    border-color: #A7F3D0;
}
.form-group.bg-document h3 {
    color: #047857;
}

.form-group.bg-buyer {
    background-color: #FFF7ED;
    border-color: #FED7AA;
}
.form-group.bg-buyer h3 {
    color: #C2410C;
}

.form-group.bg-items {
    background-color: #F5F3FF;
    border-color: #DDD6FE;
}
.form-group.bg-items h3 {
    color: #6D28D9;
}

.form-group.bg-summary {
    background-color: #FFF1F2;
    border-color: #FECDD3;
}
.form-group.bg-summary h3 {
    color: #BE123C;
}

.form-group.bg-notes {
    background-color: #F8FAFC;
    border-color: #E2E8F0;
}
.form-group.bg-notes h3 {
    color: #334155;
}

.input-row {
    display: flex;
    gap: 1rem;
    margin-bottom: 0.75rem;
}

.input-col {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.input-col label, .form-group > label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="tel"],
textarea {
    width: 100%;
    padding: 0.625rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font-family: var(--font-ui);
    font-size: 0.875rem;
    transition: var(--transition);
    background: var(--surface);
    margin-bottom: 0.75rem;
}

input:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

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

.section-header-flex h3 {
    margin-bottom: 0;
}

/* Items List */
.items-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.item-row {
    display: flex;
    gap: 0.5rem;
    align-items: flex-start;
    background: var(--surface);
    padding: 0.75rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
}

.item-desc { flex: 3; }
.item-qty { flex: 1; }
.item-price { flex: 1.5; }

.item-row input {
    margin-bottom: 0;
}

/* Buttons */
button {
    cursor: pointer;
    font-family: var(--font-ui);
    transition: var(--transition);
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn-text {
    background: none;
    color: var(--primary);
    font-size: 0.875rem;
    padding: 0.25rem 0.5rem;
}

.btn-text:hover {
    color: var(--primary-hover);
    background: rgba(79, 70, 229, 0.1);
}

.btn-icon {
    background: none;
    color: var(--danger);
    padding: 0.625rem;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
}

.btn-icon:hover {
    background: #FEE2E2;
    border-color: #FCA5A5;
}

.action-buttons {
    padding: 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    background: var(--surface);
}

.btn {
    padding: 0.875rem;
    font-size: 1rem;
    width: 100%;
}

.btn-primary {
    background-color: var(--primary);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background-color: var(--secondary);
    color: white;
}

.btn-secondary:hover {
    background-color: var(--secondary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

/* Toggle Switch */
.toggle-col {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
    padding: 0 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

.toggle-col label:first-child {
    margin-bottom: 0;
}

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input { 
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #ccc;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
}

input:checked + .slider {
    background-color: var(--primary);
}

input:checked + .slider:before {
    transform: translateX(20px);
}

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

/* Preview Area */
.preview-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #E5E7EB;
    background-image: radial-gradient(#D1D5DB 1px, transparent 1px);
    background-size: 20px 20px;
}

.preview-toolbar {
    padding: 1rem 2rem;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: center;
}

.preview-toolbar h2 {
    font-size: 1rem;
    color: var(--text-muted);
}

.preview-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    display: flex;
    justify-content: center;
    align-items: flex-start;
}

/* The Document */
.receipt-document {
    width: 210mm; /* A4 width */
    min-height: 297mm; /* A4 height */
    background: white;
    box-shadow: var(--shadow-lg);
    padding: 40px 50px;
    font-family: var(--font-doc);
    color: #000;
    position: relative;
    margin: 0 auto;
}

.receipt-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 30px;
    border-bottom: 2px solid #000;
    padding-bottom: 20px;
}

.receipt-title {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.2;
}

.receipt-title span {
    font-size: 16px;
    font-weight: 400;
    color: #555;
}

.receipt-meta table {
    border-collapse: collapse;
}

.receipt-meta td {
    padding: 4px 8px;
    font-size: 14px;
}

.receipt-meta td:first-child {
    text-align: right;
    color: #555;
}

.strong-text {
    font-weight: 700;
    font-size: 16px;
}

.receipt-parties {
    display: flex;
    gap: 40px;
    margin-bottom: 30px;
}

.party {
    flex: 1;
}

.party h3 {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 8px;
}

.party h4 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 8px;
}

.party p {
    font-size: 14px;
    margin-bottom: 4px;
    line-height: 1.4;
}

.party-label {
    font-size: 12px;
    color: #555;
    margin-bottom: 4px;
    text-transform: uppercase;
}

.receipt-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 30px;
}

.receipt-table th {
    background-color: #F3F4F6;
    padding: 12px;
    font-size: 14px;
    font-weight: 600;
    text-align: center;
    border: 1px solid #D1D5DB;
}

.receipt-table td {
    padding: 10px 12px;
    font-size: 14px;
    border: 1px solid #D1D5DB;
}

.receipt-table tbody td:nth-child(1),
.receipt-table tbody td:nth-child(3) {
    text-align: center;
}

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

.notes-cell {
    vertical-align: top;
    padding: 16px !important;
}

.notes-title {
    font-weight: 600;
    font-size: 12px;
    margin-bottom: 8px;
    color: #555;
}

.summary-label {
    text-align: right;
    font-weight: 600;
    font-size: 14px;
    background-color: #F9FAFB;
}

.summary-value {
    text-align: right;
    font-weight: 600;
}

.total-label, .total-value {
    font-size: 16px;
    background-color: #EEF2FF;
    color: var(--primary);
}

.receipt-footer {
    display: flex;
    justify-content: space-around;
    margin-top: 60px;
    page-break-inside: avoid;
}

.signature-box {
    text-align: center;
    width: 200px;
}

.signature-line {
    border-bottom: 1px dashed #000;
    height: 40px;
    margin-bottom: 10px;
}

.signature-label {
    font-weight: 600;
    font-size: 14px;
    margin-bottom: 4px;
}

.signature-date {
    font-size: 12px;
    color: #555;
}

/* Print optimizations inside preview */
@media print {
    body {
        background: white;
    }
    .mobile-tabs, .controls-panel, .preview-toolbar {
        display: none !important;
    }
    .app-container {
        display: block;
        height: auto;
        overflow: visible;
    }
    .preview-area {
        background: white;
        display: block !important;
    }
    .preview-container {
        padding: 0;
        display: block;
    }
    .receipt-wrapper {
        transform: none !important;
        height: auto !important;
    }
    .receipt-document {
        box-shadow: none;
        padding: 0;
        width: 100%;
        min-height: auto;
        margin: 0;
    }
}

/* Mobile Responsive */
@media (max-width: 800px) {
    body {
        overflow: auto; /* allow body scroll if needed, though we manage it in app-container */
    }

    .app-container {
        flex-direction: column;
    }

    .mobile-tabs {
        display: flex;
    }

    .controls-panel {
        width: 100%;
        height: calc(100vh - 60px); /* minus tabs height */
        border-right: none;
        box-shadow: none;
    }

    .preview-area {
        display: none;
        height: calc(100vh - 60px);
        width: 100%;
    }

    .preview-toolbar {
        display: none; /* Hide toolbar on mobile to save space */
    }

    /* Tab States */
    .app-container.show-preview .controls-panel {
        display: none;
    }
    
    .app-container.show-preview .preview-area {
        display: flex;
    }

    .preview-container {
        padding: 1rem;
        align-items: flex-start;
        justify-content: center;
        overflow-x: hidden;
        padding-bottom: 20px;
    }

    .receipt-wrapper {
        transform-origin: top center;
    }
    
    .receipt-document {
        margin-bottom: 0;
    }


}
