/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Light Theme Colors */
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-accent: #f1f5f9;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --border-color: #e2e8f0;
    --border-light: #f1f5f9;
    --primary-color: #3b82f6;
    --primary-hover: #2563eb;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    --radius: 8px;
    --radius-sm: 4px;
    --radius-lg: 12px;
}

[data-theme="dark"] {
    /* Dark Theme Colors */
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-accent: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --border-light: #475569;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.2);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.3);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.4);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.6;
    transition: all 0.3s ease;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 1rem;
}

/* Header */
.header {
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    padding: 1rem 0;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 2rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.logo-icon {
    width: 2.5rem;
    height: 2.5rem;
    background: var(--primary-color);
    color: white;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
}

.logo h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
}

.logo p {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.currency-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.currency-selector select {
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.875rem;
}

.theme-toggle {
    width: 2.5rem;
    height: 2.5rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.theme-toggle:hover {
    background: var(--bg-accent);
}

[data-theme="dark"] .sun-icon {
    display: none;
}

[data-theme="light"] .moon-icon {
    display: none;
}

.saved-invoices button {
    padding: 0.5rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.875rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: all 0.2s ease;
}

.saved-invoices button:hover {
    background: var(--bg-accent);
}

/* Main Content */
.main {
    padding: 2rem 0 6rem;
}

.invoice-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    align-items: start;
}

/* Form Panel */
.form-panel {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.form-section {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow-sm);
    animation: fadeIn 0.3s ease-out;
}

.form-section h2 {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 1rem;
}

.form-section h2 svg {
    color: var(--primary-color);
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.section-header h2 {
    margin-bottom: 0;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.form-grid.three-col {
    grid-template-columns: 1fr 1fr 1fr;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-group.full-width {
    grid-column: 1 / -1;
}

.form-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-group input,
.form-group textarea,
.form-group select {
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.875rem;
    transition: all 0.2s ease;
}

.input-with-select {
    display: flex;
    gap: 0.5rem;
}

.input-with-select input {
    flex: 1;
}

.input-with-select select {
    min-width: 80px;
    flex-shrink: 0;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgb(59 130 246 / 0.1);
}

.form-group input[readonly] {
    background: var(--bg-accent);
    color: var(--text-muted);
}

.form-group textarea {
    resize: vertical;
    min-height: 4rem;
}

/* Buttons */
.btn-add,
.btn-primary,
.btn-secondary,
.btn-export,
.btn-danger {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-accent);
}

.btn-add {
    background: var(--success-color);
    color: white;
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
}

.btn-add:hover {
    background: #059669;
}

.btn-export {
    background: #dc2626;
    color: white;
}

.btn-export:hover {
    background: #b91c1c;
}

.btn-danger {
    background: var(--danger-color);
    color: white;
    padding: 0.5rem;
    min-width: auto;
}

.btn-danger:hover {
    background: #dc2626;
}

/* Invoice Items */
#items-container {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.item-row {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1fr auto;
    gap: 0.75rem;
    align-items: end;
    padding: 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-accent);
}

.item-total {
    padding: 0.75rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    font-weight: 500;
    color: var(--text-primary);
    text-align: right;
}

.empty-items {
    text-align: center;
    padding: 2rem;
    color: var(--text-muted);
    border: 2px dashed var(--border-color);
    border-radius: var(--radius);
}

/* Totals */
.totals-summary {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.total-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    color: var(--text-secondary);
}

.total-final {
    border-top: 1px solid var(--border-color);
    margin-top: 0.5rem;
    padding-top: 1rem;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* Payment Methods */
.payment-grid {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1.5rem;
}

.payment-options {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.payment-option {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: var(--radius);
    transition: background-color 0.2s ease;
}

.payment-option:hover {
    background: var(--bg-accent);
}

.payment-option input[type="radio"] {
    margin: 0;
}

.payment-details {
    display: none;
}

.payment-details.active {
    display: block;
}

.payment-details label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.payment-details textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.875rem;
    resize: vertical;
}

/* Preview Panel */
.preview-panel {
    position: sticky;
    top: 6rem;
}

.preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.preview-header h3 {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
}

.preview-header h3 svg {
    color: var(--primary-color);
}

.invoice-preview {
    background: white;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow-lg);
    color: #1f2937;
    min-height: 800px;
    animation: fadeIn 0.3s ease-out;
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: start;
    margin-bottom: 2rem;
}

.company-info h1 {
    font-size: 1.5rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.company-info p {
    color: #6b7280;
    font-size: 0.875rem;
    margin-bottom: 0.25rem;
}

.company-address {
    white-space: pre-line;
    color: #6b7280;
    font-size: 0.875rem;
}

.invoice-details h2 {
    font-size: 2rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 1rem;
}

.invoice-meta p {
    color: #6b7280;
    font-size: 0.875rem;
    margin-bottom: 0.25rem;
}

.bill-to {
    margin-bottom: 2rem;
}

.bill-to h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.client-info p {
    color: #1f2937;
    margin-bottom: 0.25rem;
}

.client-info p:first-child {
    font-weight: 600;
}

.client-address {
    white-space: pre-line;
    color: #6b7280;
    font-size: 0.875rem;
}

.items-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 2rem;
}

.items-table th {
    text-align: left;
    padding: 0.75rem 0;
    border-bottom: 2px solid #e5e7eb;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1f2937;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.items-table th:nth-child(2) {
    text-align: center;
    width: 4rem;
}

.items-table th:nth-child(3),
.items-table th:nth-child(4) {
    text-align: right;
    width: 6rem;
}

.items-table td {
    padding: 0.75rem 0;
    border-bottom: 1px solid #f3f4f6;
    font-size: 0.875rem;
}

.items-table td:nth-child(2) {
    text-align: center;
}

.items-table td:nth-child(3),
.items-table td:nth-child(4) {
    text-align: right;
}

.items-table td:nth-child(4) {
    font-weight: 500;
}

.totals-section {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 2rem;
}

.totals-content {
    width: 16rem;
}

.totals-row {
    display: flex;
    justify-content: space-between;
    padding: 0.25rem 0;
    font-size: 0.875rem;
}

.totals-row:not(.totals-final) {
    color: #6b7280;
}

.totals-final {
    border-top: 1px solid #e5e7eb;
    padding-top: 0.75rem;
    margin-top: 0.5rem;
    font-weight: 600;
    font-size: 1.125rem;
    color: #1f2937;
}

.payment-info,
.notes-terms {
    margin-bottom: 1.5rem;
}

.payment-info h4,
.notes-terms h4 {
    font-size: 0.875rem;
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.payment-method {
    font-weight: 500;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.payment-details-preview {
    white-space: pre-line;
    color: #6b7280;
    font-size: 0.875rem;
    font-family: 'Courier New', monospace;
    background: #f9fafb;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
}

.notes-terms {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

.notes-content,
.terms-content {
    white-space: pre-line;
    color: #6b7280;
    font-size: 0.875rem;
    line-height: 1.5;
}

.invoice-footer {
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid #e5e7eb;
    text-align: center;
}

.invoice-footer p {
    color: #9ca3af;
    font-size: 0.75rem;
}

.empty-preview {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 400px;
    color: #9ca3af;
    text-align: center;
}

.empty-preview svg {
    margin-bottom: 1rem;
    opacity: 0.5;
}

/* Bottom Toolbar */
.bottom-toolbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    padding: 1rem 0;
    box-shadow: var(--shadow-lg);
    z-index: 50;
}

.toolbar-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.toolbar-left,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.auto-save-status {
    display: none;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--success-color);
}

.auto-save-status.show {
    display: flex;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    animation: fadeIn 0.2s ease-out;
}

.modal.show {
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-close {
    width: 2rem;
    height: 2rem;
    border: none;
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.modal-close:hover {
    background: var(--bg-accent);
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
    max-height: 60vh;
    overflow-y: auto;
}

.saved-invoice-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    margin-bottom: 0.75rem;
    background: var(--bg-secondary);
    transition: all 0.2s ease;
}

.saved-invoice-item:hover {
    background: var(--bg-accent);
}

.saved-invoice-info h4 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.saved-invoice-info p {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.saved-invoice-actions {
    display: flex;
    gap: 0.5rem;
}

.saved-invoice-actions button {
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.saved-invoice-actions button:hover {
    background: var(--bg-accent);
}

.saved-invoice-actions .btn-danger {
    background: var(--danger-color);
    color: white;
    border-color: var(--danger-color);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(1rem);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Responsive Design */
@media (max-width: 1200px) {
    .invoice-grid {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
    
    .preview-panel {
        position: static;
        order: -1;
    }
}

@media (max-width: 768px) {
    .container {
        padding: 0 0.75rem;
    }
    
    .header-content {
        flex-direction: column;
        gap: 1rem;
    }
    
    .header-actions {
        width: 100%;
        justify-content: space-between;
    }
    
    .form-grid {
        grid-template-columns: 1fr;
    }
    
    .form-grid.three-col {
        grid-template-columns: 1fr;
    }
    
    .item-row {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }
    
    .payment-grid {
        grid-template-columns: 1fr;
    }
    
    .notes-terms {
        grid-template-columns: 1fr;
    }
    
    .toolbar-content {
        flex-direction: column;
        gap: 1rem;
    }
    
    .toolbar-left,
    .toolbar-right {
        width: 100%;
        justify-content: center;
    }
    
    .invoice-preview {
        padding: 1rem;
    }
    
    .invoice-header {
        flex-direction: column;
        gap: 1rem;
    }
    
    .totals-section {
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .main {
        padding: 1rem 0 8rem;
    }
    
    .form-section {
        padding: 1rem;
    }
    
    .invoice-preview {
        padding: 0.75rem;
        font-size: 0.875rem;
    }
    
    .modal-content {
        width: 95%;
        margin: 1rem;
    }
}

/* Print Styles */
@media print {
    .header,
    .bottom-toolbar,
    .form-panel,
    .preview-header {
        display: none !important;
    }
    
    .main {
        padding: 0;
    }
    
    .invoice-grid {
        grid-template-columns: 1fr;
    }
    
    .preview-panel {
        position: static;
    }
    
    .invoice-preview {
        box-shadow: none;
        border: none;
        background: white;
        color: black;
    }
}