/* --- THEME AND COLOR VARIABLES --- */
:root {
    --primary-color: #0d6efd;
    --success-color: #198754;
    --danger-color: #dc3545;
    --secondary-color: #6c757d;

    /* Light Theme - Kept the lighter version */
    --bg-gradient-light: linear-gradient(135deg, #f7e9ff 0%, #c0e2ff 100%);
    --glass-bg-light: rgba(255, 255, 255, 0.4); /* Increased opacity for readability */
    --glass-border-light: rgba(255, 255, 255, 0.5);
    --text-color-light: #1e293b;
    --text-muted-light: #475569;
    --card-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.1);

    /* Dark Theme - Reverted to the original darker version */
    --bg-gradient-dark: linear-gradient(135deg, #020024 0%, #090979 35%, #00d4ff 100%);
    --glass-bg-dark: rgba(22, 33, 62, 0.4);
    --glass-border-dark: rgba(255, 255, 255, 0.15);
    --text-color-dark: #e2e8f0;
    --text-muted-dark: #94a3b8;

    /* Button Gradients */
    --btn-gradient-primary: linear-gradient(45deg, #0d6efd, #63a2ff);
    --btn-gradient-success: linear-gradient(45deg, #198754, #28a745);
    --btn-gradient-danger: linear-gradient(45deg, #dc3545, #ff7854);
}

html[data-theme='dark'] {
    --bs-body-color: var(--text-color-dark);
}

/* --- GENERAL & LAYOUT STYLES --- */
body {
    font-family: 'Inter', sans-serif;
    background: var(--bg-gradient-light);
    background-attachment: fixed;
    color: var(--text-color-light);
    transition: background 0.5s ease;
}

html[data-theme='dark'] body {
    background: var(--bg-gradient-dark);
    background-attachment: fixed;
    color: var(--text-color-dark);
}

/* --- GLASSMORPHISM & PREMIUM UI --- */
.card,
.form-control,
.form-select,
.nav-tabs .nav-link.active,
.ad-banner-container {
    background: var(--glass-bg-light);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border-light);
    box-shadow: var(--card-shadow);
    border-radius: 0.75rem;
    transition: background 0.3s, border 0.3s;
}

html[data-theme='dark'] .card,
html[data-theme='dark'] .form-control,
html[data-theme='dark'] .form-select,
html[data-theme='dark'] .nav-tabs .nav-link.active,
html[data-theme='dark'] .ad-banner-container {
    background: var(--glass-bg-dark);
    border: 1px solid var(--glass-border-dark);
}

/* --- COMPONENT STYLING --- */

/* Forms */
.form-control, .form-select {
    color: var(--text-color-light);
}

html[data-theme='dark'] .form-control,
html[data-theme='dark'] .form-select {
    color: var(--text-color-dark);
}

.form-control::placeholder { color: var(--text-muted-light); opacity: 0.8; }
html[data-theme='dark'] .form-control::placeholder { color: var(--text-muted-dark); opacity: 0.8; }

.form-control:focus, .form-select:focus {
    background: var(--glass-bg-light);
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
    color: var(--text-color-light);
}
html[data-theme='dark'] .form-control:focus,
html[data-theme='dark'] .form-select:focus {
    background: var(--glass-bg-dark);
    color: var(--text-color-dark);
}

/* --- FIX: Button Text Color --- */
/* General button styles */
.btn {
    border: none;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

/* Gradient buttons get white text */
.btn-primary, .btn-success, .btn-danger {
    color: #fff !important;
}
.btn-primary { background-image: var(--btn-gradient-primary); }
.btn-success { background-image: var(--btn-gradient-success); }
.btn-danger { background-image: var(--btn-gradient-danger); }

/* Outline buttons get theme-aware text color */
.btn-outline-secondary {
    color: var(--text-muted-light) !important;
    border: 1px solid var(--glass-border-light);
}
html[data-theme='dark'] .btn-outline-secondary {
    color: var(--text-muted-dark) !important;
    border: 1px solid var(--glass-border-dark);
}
.btn-outline-secondary:hover {
    background: var(--glass-bg-light);
    color: var(--text-color-light) !important;
    border-color: var(--glass-border-light);
}
html[data-theme='dark'] .btn-outline-secondary:hover {
    background: var(--glass-bg-dark);
    color: var(--text-color-dark) !important;
    border-color: var(--glass-border-dark);
}
/* --- END FIX --- */


/* Tabs */
.nav-tabs {
    border-bottom: none;
}
.nav-tabs .nav-link {
    border: none;
    color: var(--text-muted-light);
    border-radius: 0.5rem 0.5rem 0 0;
}
html[data-theme='dark'] .nav-tabs .nav-link {
    color: var(--text-muted-dark);
}
.nav-tabs .nav-link.active {
    color: var(--text-color-light);
    font-weight: 600;
}
html[data-theme='dark'] .nav-tabs .nav-link.active {
    color: var(--text-color-dark);
}

/* --- CONTROL PANEL (LEFT SIDE) --- */
#control-panel {
    height: 100vh;
    overflow-y: auto;
}
#control-panel::-webkit-scrollbar { width: 6px; }
#control-panel::-webkit-scrollbar-thumb { background: var(--glass-border-light); border-radius: 3px; }
html[data-theme='dark'] #control-panel::-webkit-scrollbar-thumb { background: var(--glass-border-dark); }

/* --- AD BANNER --- */
.ad-banner-container {
    width: 300px;
    height: 250px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 1.5rem auto;
    color: var(--text-muted-light);
    text-align: center;
}
html[data-theme='dark'] .ad-banner-container {
    color: var(--text-muted-dark);
}

/* --- HISTORY TAB ACTIONS --- */
.history-actions {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    z-index: 10;
}

/* --- PREVIEW PANEL (RIGHT SIDE) --- */
#preview-panel {
    background-color: rgba(0,0,0,0.1);
    height: 100vh;
    overflow-y: auto;
    display: flex;
    justify-content: center;
    padding-top: 2rem;
    padding-bottom: 2rem;
}
html[data-theme='dark'] #preview-panel {
    background-color: rgba(0,0,0,0.3);
}
#preview-panel:focus {
    outline: none !important;
    box-shadow: none !important;
}
#invoice-preview-wrapper {
    width: 100%;
    display: flex;
    justify-content: center;
}

/* --- INVOICE PREVIEW DARK MODE ISOLATION --- */
#invoice-preview, .invoice-page-container {
    background: white !important;
    color: #333 !important;
    box-shadow: 0 0 25px rgba(0, 0, 0, 0.15);
    padding: 2.5rem;
    box-sizing: border-box;
    border-radius: 4px;
}
#invoice-preview *, .invoice-page-container * {
    color: #333 !important;
    background-color: transparent !important;
}
#invoice-preview h1, .invoice-page-container h1 { color: var(--primary-color) !important; }
#invoice-preview .table th, .invoice-page-container .table th { background-color: #f8f9fa !important; }
#invoice-preview .warranty-section, .invoice-page-container .warranty-section { color: #555 !important; }
#invoice-preview .footer, .invoice-page-container .footer { color: #888 !important; }

/* --- PAPER SIZES --- */
#invoice-preview.a4, .invoice-page-container.a4 { width: 210mm; }
#invoice-preview.letter, .invoice-page-container.letter { width: 8.5in; }
#invoice-preview.legal, .invoice-page-container.legal { width: 8.5in; }
#invoice-preview.a5, .invoice-page-container.a5 { width: 148mm; }
#invoice-preview.a6, .invoice-page-container.a6 { width: 105mm; font-size: 0.7rem; padding: 1.5rem; }

#invoice-preview.a4 { min-height: 297mm; }
#invoice-preview.letter { min-height: 11in; }
#invoice-preview.legal { min-height: 14in; }
#invoice-preview.a5 { min-height: 210mm; }
#invoice-preview.a6 { min-height: 148mm; }

#invoice-preview.a6 h1, .invoice-page-container.a6 h1 { font-size: 1.5rem; }
#invoice-preview.a6 .totals-section table, .invoice-page-container.a6 .totals-section table { width: 60%; }

/* --- INVOICE STYLES --- */
#invoice-preview .invoice-header, .invoice-page-container .invoice-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 2rem; }
#invoice-preview .invoice-header img, .invoice-page-container .invoice-header img { max-width: 180px; max-height: 100px; }
#invoice-preview.a5 .invoice-header img, .invoice-page-container.a5 .invoice-header img { max-width: 120px; max-height: 70px; }
#invoice-preview.a6 .invoice-header img, .invoice-page-container.a6 .invoice-header img { max-width: 80px; max-height: 50px; }

#invoice-preview .invoice-details, .invoice-page-container .invoice-details,
#invoice-preview .client-details, .invoice-page-container .client-details,
#invoice-preview .business-details, .invoice-page-container .business-details { font-size: 0.9em; }
#invoice-preview .table, .invoice-page-container .table { font-size: 0.9em; }
#invoice-preview .table th, .invoice-page-container .table th { border-bottom: 2px solid var(--primary-color); }
#invoice-preview .totals-section, .invoice-page-container .totals-section { margin-top: 2rem; text-align: right; }
#invoice-preview .totals-section table, .invoice-page-container .totals-section table { width: 50%; margin-left: auto; }
#invoice-preview .warranty-section, .invoice-page-container .warranty-section { margin-top: 2rem; font-size: 0.8em; border-top: 1px solid #eee; padding-top: 1rem; }
#invoice-preview .footer, .invoice-page-container .footer { margin-top: 3rem; padding-top: 1rem; border-top: 1px solid #ddd; text-align: center; font-size: 0.8em; }

.invoice-header, .totals-section, .footer, .table tr {
    page-break-inside: avoid;
}

/* --- MULTI-PAGE PDF STYLES --- */
.invoice-page-container {
    page-break-after: always;
}
.invoice-page-container:last-child {
    page-break-after: avoid;
}

/* --- PRINT-SPECIFIC STYLES --- */
@media print {
    body {
        background: none;
    }
    #control-panel {
        display: none;
    }
    #preview-panel {
        width: 100% !important;
        height: auto !important;
        padding: 0;
        margin: 0;
        overflow: hidden;
    }
    #invoice-preview {
        box-shadow: none;
        margin: 0;
        border: none;
        height: auto !important;
        min-height: 0 !important;
        transform: scale(1) !important;
        border-radius: 0;
    }
}

/* --- RESPONSIVE ADJUSTMENTS --- */
@media (max-width: 992px) {
    #control-panel, #preview-panel {
        height: auto;
    }
    #preview-panel {
        padding-top: 1rem;
    }
    #invoice-preview {
        transform: scale(0.95);
        transform-origin: top center;
        margin-top: 0;
        min-height: 0 !important;
    }
}

@media (max-width: 768px) {
    #invoice-preview {
        transform: scale(0.9);
        transform-origin: top center;
    }
}

@media (max-width: 576px) {
    #invoice-preview {
        transform: scale(0.75);
        transform-origin: top center;
        margin-top: -50px;
    }
    body {
        font-size: 0.9rem;
    }
    .p-4 {
        padding: 1.5rem !important;
    }
}