:root {
    /* Dark Mode Palette (UI) */
    --bg-app: #09090b;
    --bg-panel: #18181b;
    --border: rgba(255, 255, 255, 0.1);
    --primary: #3b82f6;
    /* Blue */
    --primary-hover: #2563eb;
    --text-main: #f8fafc;
    --text-muted: #a1a1aa;

    /* Print/Document Palette (Light) */
    --doc-bg: #ffffff;
    --doc-text: #1e293b;
    --doc-muted: #64748b;
    --doc-border: #e2e8f0;
    --doc-primary: #0f172a;

    --radius: 8px;
    --transition: 0.2s ease;
}

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

body {
    font-family: 'Inter', system-ui, sans-serif;
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

/* -- App Layout -- */
.app-layout {
    display: flex;
    height: 100%;
}

/* -- Sidebar (UI Controls) -- */
.sidebar {
    width: 320px;
    background: var(--bg-panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 10;
}

.sidebar-header {
    padding: 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.brand-icon {
    color: var(--primary);
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.control-group h3 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

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

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

.input-col.full {
    flex: 100%;
}

.mt-2 {
    margin-top: 0.5rem;
}

.mt-3 {
    margin-top: 1rem;
}

label {
    font-size: 0.85rem;
    font-weight: 500;
}

.form-input {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 0.6rem 0.8rem;
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.95rem;
    outline: none;
    transition: var(--transition);
    width: 100%;
}

.form-input:focus {
    border-color: var(--primary);
}

.file-upload-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem 1rem;
    background: rgba(255, 255, 255, 0.05);
    border: 1px dashed var(--border);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: var(--transition);
    width: 100%;
    text-align: center;
}

.file-upload-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-radius: 6px;
    font-family: inherit;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    border: none;
    transition: var(--transition);
}

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

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

.text-btn {
    background: transparent;
    color: #ef4444;
    padding: 0.4rem;
    font-size: 0.85rem;
}

.text-btn:hover {
    background: rgba(239, 68, 68, 0.1);
}

.full-width {
    width: 100%;
}

.sidebar-footer {
    padding: 1.5rem;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.1);
}

.footer-note {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    margin-top: 0.75rem;
}

.hidden {
    display: none !important;
}

/* -- Live Preview Area -- */
.preview-area {
    flex: 1;
    overflow-y: auto;
    padding: 3rem;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* subtle pattern */
    background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1px);
    background-size: 20px 20px;
}

/* -- The Document Itself -- */
.invoice-wrapper {
    background: var(--doc-bg);
    width: 100%;
    max-width: 800px;
    /* Standard letter/A4-ish proportions */
    min-height: 1056px;
    padding: 3rem 4rem;
    color: var(--doc-text);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
}

/* Transparent, auto-resizing inputs for the document */
.doc-input {
    background: transparent;
    border: 1px solid transparent;
    color: inherit;
    font-family: inherit;
    font-size: inherit;
    width: 100%;
    padding: 0.25rem;
    outline: none;
    transition: border-color 0.2s;
    border-radius: 4px;
}

.doc-input:hover,
.doc-input:focus {
    border-color: rgba(0, 0, 0, 0.1);
    background: rgba(0, 0, 0, 0.02);
}

.doc-textarea {
    resize: none;
    overflow: hidden;
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 3rem;
    gap: 2rem;
}

.brand-section {
    flex: 1;
}

#invoice-logo {
    max-width: 200px;
    max-height: 80px;
    object-fit: contain;
    margin-bottom: 1rem;
}

.company-details {
    font-size: 0.95rem;
    line-height: 1.5;
    color: var(--doc-muted);
    min-height: 100px;
}

.meta-section {
    text-align: right;
    min-width: 200px;
}

.document-title {
    font-size: 2.5rem;
    font-weight: 300;
    letter-spacing: 0.1em;
    color: var(--doc-primary);
    margin-bottom: 1.5rem;
}

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

.meta-table th {
    text-align: right;
    padding: 0.4rem 1rem 0.4rem 0;
    font-weight: 600;
    color: var(--doc-muted);
    font-size: 0.9rem;
}

.meta-table td {
    text-align: right;
    padding: 0.4rem 0;
    font-weight: 500;
    font-size: 0.95rem;
}

.bill-to-section {
    margin-bottom: 3rem;
}

.bill-to-section h3 {
    font-size: 0.85rem;
    text-transform: uppercase;
    color: var(--doc-muted);
    margin-bottom: 0.5rem;
    margin-left: 0.25rem;
    /* align with input padding */
}

.client-details {
    font-size: 1rem;
    line-height: 1.5;
    min-height: 80px;
    max-width: 300px;
}

/* Items Table */
.items-table-container {
    margin-bottom: 3rem;
}

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

.items-table th {
    text-align: left;
    padding: 0.75rem 0.5rem;
    font-size: 0.85rem;
    text-transform: uppercase;
    color: var(--doc-muted);
    border-bottom: 2px solid var(--doc-border);
}

.items-table td {
    padding: 0.5rem;
    border-bottom: 1px solid var(--doc-border);
    vertical-align: top;
}

.col-desc {
    width: 45%;
}

.col-qty {
    width: 15%;
    text-align: right;
}

.col-rate {
    width: 20%;
    text-align: right;
}

.col-amount {
    width: 20%;
    text-align: right;
    font-weight: 500;
}

.col-action {
    width: 40px;
    text-align: right;
}

/* Right align inputs within specific columns */
.col-qty input,
.col-rate input {
    text-align: right;
}

.item-amount-display {
    padding-top: 0.75rem !important;
    /* align with inputs visually */
}

.remove-btn {
    background: none;
    border: none;
    color: #ef4444;
    cursor: pointer;
    padding: 0.5rem;
    opacity: 0.3;
    transition: opacity 0.2s;
}

.item-row:hover .remove-btn {
    opacity: 1;
}

.add-item-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: transparent;
    border: none;
    color: var(--primary);
    font-weight: 500;
    cursor: pointer;
    padding: 1rem 0.5rem;
    transition: opacity 0.2s;
}

.add-item-btn:hover {
    opacity: 0.8;
}

/* Footer / Totals */
.invoice-footer {
    display: flex;
    justify-content: space-between;
    gap: 2rem;
    align-items: flex-start;
}

.notes-section {
    flex: 1;
    max-width: 400px;
}

.notes-section h3 {
    font-size: 0.85rem;
    text-transform: uppercase;
    color: var(--doc-muted);
    margin-bottom: 0.5rem;
    margin-left: 0.25rem;
}

.notes {
    font-size: 0.9rem;
    color: var(--doc-muted);
    min-height: 60px;
}

.totals-section {
    width: 300px;
}

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

.totals-table th {
    text-align: right;
    padding: 0.75rem 1rem 0.75rem 0;
    font-weight: 500;
    color: var(--doc-muted);
    font-size: 0.95rem;
}

.totals-table td {
    text-align: right;
    padding: 0.75rem 0;
    font-weight: 500;
    font-size: 1rem;
}

.total-row th,
.total-row td {
    border-top: 2px solid var(--doc-border);
    font-weight: 700;
    font-size: 1.25rem;
    color: var(--doc-primary);
    padding-top: 1rem;
}

/* Print Specific Rules */
@media print {
    body {
        background: white;
    }

    .no-print {
        display: none !important;
    }

    .preview-area {
        padding: 0;
        justify-content: flex-start;
        background: none;
    }

    .invoice-wrapper {
        box-shadow: none;
        max-width: none;
        min-height: auto;
        padding: 0;
    }

    .doc-input {
        border: none !important;
        background: transparent !important;
    }
}