/* ==========================================================================
   1. FONTS & THEME VARIABLES
   ========================================================================== */

@font-face {
    font-family: 'Inter';
    src: local('Inter Regular'), local('Inter-Regular'), url('/fonts/Inter-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Inter';
    src: local('Inter SemiBold'), local('Inter-SemiBold'), url('/fonts/Inter-SemiBold.woff2') format('woff2');
    font-weight: 600;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Inter';
    src: local('Inter Bold'), local('Inter-Bold'), url('/fonts/Inter-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

:root {
    /* Light Theme (Default) */
    --bg-body: #f8fafc;
    --bg-surface: #ffffff;
    --bg-input: #f1f5f9;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --accent-color: #2563eb;
    --accent-hover: #1d4ed8;
    --border-color: #e2e8f0;
    --danger: #ef4444;
    --success: #10b981;
    --ad-bg: #e2e8f0;
    --card-bg: #f1f5f9;
    --radius: 8px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] {
    /* Dark Theme */
    --bg-body: #0f172a;
    --bg-surface: #1e293b;
    --bg-input: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent-color: #3b82f6;
    --accent-hover: #60a5fa;
    --border-color: #334155;
    --danger: #f87171;
    --success: #34d399;
    --ad-bg: #0b1120;
    --card-bg: #0f172a;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
    --shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.7);
}

/* ==========================================================================
   2. RESET & ACCESSIBILITY
   ========================================================================== */

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

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-primary);
    line-height: 1.6;
    transition: background-color 0.3s ease, color 0.3s ease;
    -webkit-font-smoothing: antialiased;
}

.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

.hidden { display: none !important; }

/* ==========================================================================
   3. TYPOGRAPHY & BUTTONS
   ========================================================================== */

h1, h2, h3, h4 {
    color: var(--text-primary);
    line-height: 1.2;
    margin-bottom: 0.5rem;
}

button {
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    transition: all 0.2s;
}

.btn-primary {
    background: var(--accent-color);
    color: #ffffff;
    padding: 0.75rem 1.5rem;
    text-decoration: none;
    display: inline-block;
}

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

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-primary);
    padding: 0.75rem 1.5rem;
    border: 1px solid var(--border-color);
}

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

.btn-outline {
    background: transparent;
    color: var(--accent-color);
    padding: 0.5rem 1rem;
    border: 1px solid var(--accent-color);
}

.btn-icon {
    background: transparent;
    color: var(--text-primary);
    padding: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-icon:hover { background: var(--bg-input); }

.btn-text {
    background: transparent;
    color: var(--accent-color);
    padding: 0.5rem 0;
}

[data-theme="light"] .sun-icon { display: none; }
[data-theme="dark"] .moon-icon { display: none; }

/* ==========================================================================
   4. LAYOUT & ADSENSE SAFEGUARDS (CLS PREVENTION)
   ========================================================================== */

.main-header {
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav-actions {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.main-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

/* AdSense Constraints - Explicit heights prevent CLS */
.ad-slot {
    background: var(--ad-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 1rem auto;
    width: 100%;
    max-width: 970px;
    overflow: hidden;
}

.ad-header { min-height: 90px; }
.ad-in-content { min-height: 250px; }
.ad-footer { min-height: 90px; }

/* ==========================================================================
   5. CALCULATOR GRID
   ========================================================================== */

.tool-section { margin-bottom: 2rem; }
.section-header { text-align: center; margin-bottom: 2rem; }

.panel {
    background: var(--bg-surface);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
}

.calculator-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
}

@media (min-width: 768px) {
    .calculator-grid { grid-template-columns: 1fr 1fr; }
}

.form-group {
    margin-bottom: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-group label {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

input[type="text"], input[type="number"], select {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-family: inherit;
}

.tax-type-toggle {
    display: flex;
    gap: 1rem;
    margin-top: 1rem;
}

.result-row {
    display: flex;
    justify-content: space-between;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
}

.result-row.sub-row {
    padding-left: 1rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
    border-bottom: none;
}

.total-row {
    font-weight: 700;
    font-size: 1.2rem;
    border-top: 2px solid var(--border-color);
    border-bottom: none;
    margin-top: 0.5rem;
}

.text-highlight { color: var(--accent-color); font-weight: 600; }

/* ==========================================================================
   6. INVOICE GENERATOR & STRICT PDF VIRTUAL CANVAS
   ========================================================================== */

.invoice-toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.inline-group {
    flex-direction: row;
    align-items: center;
}

/* Virtual Canvas Wrapper: Forces horizontal scroll on narrow mobile screens */
.invoice-canvas-wrapper {
    width: 100%;
    overflow-x: auto;
    background: var(--ad-bg);
    padding: 2rem 1rem;
    border-radius: var(--radius);
}

/* Explicit Dimensions for strict html2pdf accuracy & print fidelity */
.invoice-container {
    width: 794px !important; /* STRICT A4 width at 96 DPI */
    min-height: 1123px; /* STRICT A4 height */
    margin: 0 auto;
    background: #ffffff !important; /* Must be pure white for PDF */
    color: #000000 !important; /* Must be pure black for standard PDF text */
    padding: 40px;
    box-shadow: 0 0 15px rgba(0,0,0,0.2);
    position: relative;
    box-sizing: border-box;
}

/* Content Editable Styling */
[contenteditable="true"] {
    outline: none;
    border: 1px dashed transparent;
    transition: border 0.2s;
    min-width: 20px;
    display: inline-block;
}

[contenteditable="true"]:hover, [contenteditable="true"]:focus {
    border-color: var(--accent-color);
    background: rgba(37, 99, 235, 0.05);
}

/* Invoice Header Alignments (Flexbox) */
.inv-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2.5rem;
    gap: 2rem;
}

.inv-company {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    max-width: 60%;
}

.inv-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    text-align: right;
    max-width: 35%;
}

.logo-upload-btn {
    display: inline-block;
    padding: 0.4rem 0.8rem;
    background: #f1f5f9;
    border: 1px dashed #cbd5e1;
    cursor: pointer;
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
    color: #334155;
}

#logo-container { margin-bottom: 0.5rem; }
#inv-logo { max-height: 80px; margin-bottom: 0.5rem; display: block; }
#inv-company-name { font-size: 1.5rem; margin-bottom: 0.25rem; }
#inv-company-details { font-size: 0.95rem; color: #333; white-space: pre-wrap; line-height: 1.4; }

.inv-title { font-size: 2.5rem; letter-spacing: 2px; text-transform: uppercase; margin-bottom: 0.5rem; line-height: 1; }
.meta-grid { display: grid; grid-template-columns: auto 1fr; gap: 0.5rem; font-size: 0.95rem; align-items: center; }

.inv-bill-to { margin-bottom: 2rem; }
.client-name { font-weight: bold; font-size: 1.2rem; margin: 0.5rem 0;}
#inv-client-details { font-size: 0.95rem; white-space: pre-wrap; line-height: 1.4; }

/* Invoice Table & Page Break Fixes */
.inv-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1rem;
}

.inv-table th, .inv-table td {
    padding: 0.75rem;
    border-bottom: 1px solid #e2e8f0;
    text-align: left;
}

.col-num { text-align: right !important; width: 14%; }
.col-action { text-align: center; width: 5%; }

/* CRITICAL: Prevent rows from splitting across PDF pages */
.inv-table tr, .inv-footer-grid {
    page-break-inside: avoid;
    break-inside: avoid;
}

.inv-table input {
    width: 100%;
    border: none;
    background: transparent;
    padding: 0.25rem;
    font-family: inherit;
    color: inherit;
    font-size: 0.9rem;
}
.inv-table input.num-input { text-align: right; }
.inv-table input:focus { outline: 1px solid var(--accent-color); background: rgba(37,99,235,0.05); }

.inv-footer-grid {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 2rem;
    margin-top: 2rem;
}

.inv-notes h4 { font-size: 1rem; margin-bottom: 0.5rem; }
#inv-notes-text { font-size: 0.9rem; white-space: pre-wrap; color: #444; }

.tot-row {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #e2e8f0;
}

.grand-total {
    font-weight: 700;
    font-size: 1.2rem;
    border-bottom: none;
    border-top: 2px solid #000;
}

/* ==========================================================================
   7. INVOICE TEMPLATES
   ========================================================================== */

/* Template 1: Modern (Default) */
.template-modern .inv-title { color: var(--accent-color); }
.template-modern .inv-table th { background: #f8fafc; font-weight: 600; border-top: 2px solid var(--accent-color); }

/* Template 2: Corporate */
.template-corporate { font-family: 'Arial', sans-serif; }
.template-corporate .inv-header { border-bottom: 3px solid #334155; padding-bottom: 1rem; }
.template-corporate .inv-table th { background: #334155; color: #fff; }
.template-corporate .inv-table td { border-left: 1px solid #e2e8f0; border-right: 1px solid #e2e8f0; }

/* Template 3: Minimal */
.template-minimal .inv-title { font-weight: 300; color: #94a3b8; }
.template-minimal .inv-table th { border-bottom: 2px solid #000; background: transparent; }
.template-minimal .inv-table td { border-bottom: none; padding: 0.5rem 0; }
.template-minimal .tot-row { border-bottom: none; }

/* Template 4: Compact */
.template-compact { padding: 20px; font-size: 0.85rem; }
.template-compact .inv-table th, .template-compact .inv-table td { padding: 0.25rem 0.5rem; }
.template-compact .inv-header { margin-bottom: 1rem; }
.template-compact .inv-title { font-size: 2rem; }

/* Template 5: Creative */
.template-creative .inv-header { background: linear-gradient(90deg, #2563eb, #8b5cf6); color: white; padding: 30px; border-radius: 8px; margin-top: -40px; margin-left: -40px; width: calc(100% + 80px); }
.template-creative #inv-company-details, .template-creative .meta-grid { color: rgba(255,255,255,0.9) !important; }
.template-creative .inv-table th { border-radius: 8px; background: #f1f5f9; border: none; }
.template-creative [contenteditable="true"]:hover { background: rgba(255,255,255,0.2); }

/* Template 6: Classic */
.template-classic { font-family: 'Times New Roman', serif; }
.template-classic .inv-title { font-weight: bold; text-align: center; width: 100%; border-bottom: 1px double #000; margin-bottom: 1rem; padding-bottom: 0.5rem; }
.template-classic .inv-meta { align-items: center; width: 100%; margin-top: 1rem; }
.template-classic .inv-table { border: 2px solid #000; }
.template-classic .inv-table th, .template-classic .inv-table td { border: 1px solid #000; }

/* Template 7: Print Optimized */
.template-print { color: #000 !important; background: #fff !important; font-family: monospace; }
.template-print * { border-color: #000 !important; }
.template-print .inv-table th { background: #fff; border-bottom: 2px solid #000; border-top: 2px solid #000;}
.template-print .inv-table td { border-bottom: 1px dashed #000; }

/* ==========================================================================
   8. SEO CONTENT & FOOTER (INTERACTIVE UI CARDS)
   ========================================================================== */

.seo-section article { margin-top: 1rem; color: var(--text-secondary); }
.seo-section h3 { margin-top: 1.5rem; font-size: 1.1rem; color: var(--text-primary); }

.main-footer {
    background: var(--bg-surface);
    border-top: 1px solid var(--border-color);
    padding: 4rem 1rem 2rem;
    margin-top: 2rem;
}

.footer-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 3rem;
}

.footer-ecosystem h4, .footer-legal h4 {
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 1.25rem;
}

/* Ecosystem Interactive Cards */
.ecosystem-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1rem;
}

.ecosystem-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1.25rem;
    text-decoration: none;
    color: var(--text-primary);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.ecosystem-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-hover);
    border-color: var(--accent-color);
}

.card-content {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.card-title { font-weight: 600; color: var(--accent-color); }
.card-desc { font-size: 0.85rem; color: var(--text-secondary); }
.card-arrow { font-size: 1.2rem; color: var(--text-secondary); transition: color 0.2s; }
.ecosystem-card:hover .card-arrow { color: var(--accent-color); }

/* Legal Interactive Pills */
.legal-pills {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1rem;
}

.legal-pill {
    padding: 0.5rem 1.25rem;
    background: var(--bg-input);
    border-radius: 50px;
    text-decoration: none;
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
    transition: background 0.2s, color 0.2s;
}

.legal-pill:hover {
    background: var(--accent-color);
    color: #ffffff;
}

.footer-disclaimer {
    font-size: 0.8rem;
    color: var(--text-secondary);
    padding: 1.5rem 0;
    border-top: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
    text-align: center;
    line-height: 1.5;
}

.footer-bottom {
    text-align: center;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

/* ==========================================================================
   9. STRICT PRINT MEDIA QUERY (NO-PRINT ENFORCEMENT)
   ========================================================================== */

@media print {
    body { background: #fff !important; color: #000 !important; }
    
    /* Strictly hide all UI components outside the invoice */
    .main-header, .ad-slot, .tool-section > .section-header, 
    #calculator-section, .invoice-toolbar, .no-print, .btn-text, 
    .seo-section, .main-footer, .invoice-canvas-wrapper::-webkit-scrollbar {
        display: none !important;
    }
    
    .invoice-canvas-wrapper {
        overflow: visible;
        padding: 0;
        background: none;
    }

    .invoice-container {
        width: 100% !important; /* Allow browser to manage A4 scale natively */
        min-height: auto !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }
    
    [contenteditable="true"] { border: none !important; background: transparent !important; }
    
    /* Strict page break handling for browser print dialogs */
    tr, .inv-footer-grid { page-break-inside: avoid !important; break-inside: avoid !important; }
    @page { margin: 0.5cm; }
}
