:root {
    /* Color Palette - Modern & Professional */
    --primary: #4f46e5; /* Indigo/Purple */
    --primary-hover: #4338ca;
    --bg-main: #f8fafc;
    --bg-card: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --input-bg: #ffffff;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --transition: all 0.3s ease;
}

[data-theme="dark"] {
    --bg-main: #0f172a;
    --bg-card: #1e293b;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --border: #334155;
    --input-bg: #0f172a;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.3), 0 2px 4px -2px rgb(0 0 0 / 0.3);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Poppins', 'Inter', sans-serif;
    background-color: var(--bg-main);
    color: var(--text-main);
    line-height: 1.5;
    transition: var(--transition);
    overflow-x: hidden;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}

/* Header & Controls */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.logo-brand {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.controls {
    display: flex;
    gap: 1rem;
    align-items: center;
}

/* Common Components */
.btn {
    padding: 0.6rem 1.2rem;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
    border: none;
}

.btn-primary {
    background-color: var(--primary);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-main);
}

.btn-outline:hover {
    background-color: var(--border);
}

.btn-danger {
    background-color: #ef4444;
    color: white;
}

.btn-danger:hover {
    background-color: #dc2626;
}

.theme-toggle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-main);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.theme-toggle:hover {
    background: var(--border);
}

/* Layout Grid */
.main-grid {
    display: grid;
    grid-template-columns: 1fr 1.2fr;
    gap: 2rem;
    align-items: start;
}

@media (max-width: 1024px) {
    .main-grid {
        grid-template-columns: 1fr;
    }
}

/* Editor Section */
.editor-card {
    background: var(--bg-card);
    padding: 2rem;
    border-radius: 16px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}

.section-title {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--primary);
}

/* Form Styling */
.form-group {
    margin-bottom: 1.2rem;
    min-width: 0;
    width: 100%;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 0.4rem;
    color: var(--text-muted);
}

input, select, textarea {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--input-bg);
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.9rem;
    transition: var(--transition);
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

/* Ensure cross-browser date input doesn't stretch */
input[type="date"] {
    display: block;
}

/* Items Table Editor */
.items-editor {
    margin-top: 2rem;
}

.item-row {
    display: grid;
    grid-template-columns: 2.5fr 0.8fr 1.2fr 0.5fr;
    gap: 0.8rem;
    margin-bottom: 0.8rem;
    align-items: start;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.item-row .btn-remove {
    padding: 0.75rem;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Preview Section */
.preview-card {
    position: sticky;
    top: 2rem;
    background: var(--bg-card);
    padding: 3rem;
    border-radius: 8px; /* Classic invoice feel */
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    min-height: 800px;
    color: #1e293b; /* Preview always slightly dark for realism */
}

[data-theme="dark"] .preview-card {
    background: #ffffff; /* Keep preview paper white for realism? No, user might want dark mode preview. Let's make it paper-like always for PDF consistency, but UI-friendly. Actually, user asked for professional SaaS UI, usually invoice preview is 1:1 with printed version. */
    color: #1e293b;
}

/* Invoice Header in Preview */
.invoice-preview-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 3rem;
}

.company-placeholder {
    width: 120px;
    height: 60px;
    background: #f1f5f9;
    border: 2px dashed #cbd5e1;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    color: #94a3b8;
    cursor: pointer;
    overflow: hidden;
}

.company-logo-img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.invoice-info h1 {
    font-size: 2rem;
    font-weight: 800;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 0.5rem;
    word-break: break-word; /* ensure title can wrap if too long */
}

/* Invoice Details */
.invoice-details-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 3rem;
}

.detail-block h4 {
    font-size: 0.75rem;
    text-transform: uppercase;
    color: #94a3b8;
    margin-bottom: 0.5rem;
    letter-spacing: 0.5px;
}

.detail-block p {
    font-weight: 500;
    font-size: 0.95rem;
}

/* Table in Preview */
.preview-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
}

.preview-table th {
    text-align: left;
    padding: 1rem;
    background: #f8fafc;
    border-bottom: 2px solid #e2e8f0;
    font-size: 0.8rem;
    text-transform: uppercase;
    color: #64748b;
}

.preview-table td {
    padding: 1rem;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.9rem;
}

.table-total-section {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.5rem;
}

.total-row {
    display: flex;
    justify-content: space-between;
    width: 100%;
    max-width: 250px;
    padding: 0.5rem 0;
}

.total-row.grand-total {
    border-top: 2px solid #e2e8f0;
    margin-top: 0.5rem;
    padding-top: 1rem;
    color: var(--primary);
    font-weight: 800;
    font-size: 1.2rem;
}

.preview-footer {
    margin-top: 4rem;
    border-top: 1px solid #f1f5f9;
    padding-top: 1.5rem;
}

.preview-footer h4 {
    font-size: 0.75rem;
    text-transform: uppercase;
    color: #94a3b8;
    margin-bottom: 0.5rem;
}

.preview-footer p {
    font-size: 0.85rem;
    color: #64748b;
    font-style: italic;
}

/* Responsive Utilities */
@media (max-width: 640px) {
    .container {
        padding: 1rem;
    }
    
    header {
        flex-direction: column;
        align-items: stretch;
        gap: 1.5rem;
    }

    .logo-brand {
        justify-content: center;
    }

    .controls {
        justify-content: center;
        flex-wrap: wrap;
    }

    .form-row {
        grid-template-columns: 1fr;
    }
    
    .item-row {
        grid-template-columns: 1fr 1fr auto;
    }
    
    .item-row input:first-child {
        grid-column: span 3;
    }

    .preview-card {
        padding: 1.5rem;
        min-height: auto;
        font-size: 0.8rem;
    }

    .invoice-preview-header {
        flex-direction: column;
        gap: 1.5rem;
    }

    .invoice-info {
        text-align: left !important;
    }

    .detail-block {
        text-align: left !important;
    }

    .invoice-details-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .preview-table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }

    .preview-table th, .preview-table td {
        padding: 0.75rem 0.5rem;
    }
}

@media (max-width: 480px) {
    html {
        font-size: 14px;
    }

    .editor-card {
        padding: 1.25rem;
    }
    
    .preview-card {
        padding: 1.25rem;
    }

    .item-row {
        grid-template-columns: 1fr;
    }
    
    .item-row input:first-child {
        grid-column: 1 / -1;
    }
    
    .item-row .btn-remove {
        height: auto;
    }
    
    .invoice-info h1 {
        font-size: 1.75rem;
    }

    .controls {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group {
        width: 100%;
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .btn-group .btn {
        width: 100%;
        justify-content: center;
    }
}

@media (max-width: 360px) {
    html {
        font-size: 12px; /* Scale everything down using rem */
    }

    .container {
        padding: 0.25rem;
    }
    
    .editor-card, .preview-card {
        padding: 0.75rem;
        border-radius: 8px;
    }
    
    .invoice-info h1 {
        font-size: 1.25rem;
    }

    .section-title {
        font-size: 0.9rem;
    }
    
    .btn {
        font-size: 0.75rem;
        padding: 0.4rem;
    }
    
    .detail-block h4 {
        font-size: 0.65rem;
    }

    .detail-block p {
        font-size: 0.8rem;
    }

    input, select, textarea {
        font-size: 0.75rem;
        padding: 0.5rem;
    }

    label {
        font-size: 0.7rem;
    }

    /* Override inline font sizes forcefully for extremely small screens */
    #p-sender-name, #p-client-name {
        font-size: 0.9rem !important;
    }
    #p-sender-contact, #p-client-contact {
        font-size: 0.75rem !important;
    }
}

/* Animations */
.fade-up {
    animation: fadeUp 0.6s ease forwards;
}

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.spin {
    animation: spin 1s linear infinite;
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-main);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Print Styles */
@media print {
    body {
        background-color: white !important;
        color: black !important;
    }
    
    header, 
    .editor-section, 
    .controls, 
    #theme-toggle, 
    #add-item-btn, 
    #download-btn, 
    #print-btn,
    .btn-group {
        display: none !important;
    }

    .main-grid {
        display: block !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .preview-section {
        width: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .preview-card {
        position: static !important;
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
        width: 100% !important;
        min-height: auto !important;
    }

    .container {
        max-width: 100% !important;
        width: 100% !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    /* Keep colors on print if supported */
    .invoice-info h1 {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        color: #4f46e5 !important;
    }
}
