    :root {
        --primary: #6366F1;
        --primary-dark: #4F52E0;
        --secondary: #8B5CF6;
        --background: #F8FAFC;
        --surface: #FFFFFF;
        --text-primary: #0F172A;
        --text-secondary: #475569;
        --text-muted: #64748B;
        --border: #E2E8F0;
        --danger: #EF4444;
        --success: #10B981;

        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);

        --radius-md: 0.5rem;
        --radius-lg: 1rem;
        --radius-full: 9999px;

        /* Dynamic Theme Color */
        --theme-color: #2563EB;
    }

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

    body {
        font-family: 'Inter', 'Inter Fallback', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
        background-color: var(--background);
        color: var(--text-primary);
        line-height: 1.5;
        -webkit-font-smoothing: antialiased;
    }

    /* ----- Layout & Container ----- */
    .container {
        max-width: 1400px;
        margin: 0 auto;
        padding: 0 1.5rem;
    }

    header {
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        padding: 1.25rem 0;
        position: sticky;
        top: 0;
        z-index: 50;
        box-shadow: var(--shadow-sm);
    }

    .header-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .brand {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        font-size: 1.5rem;
        font-weight: 800;
        color: var(--text-primary);
        text-decoration: none;
    }

    .brand i {
        color: var(--primary);
        font-size: 1.75rem;
    }

    .page-header {
        text-align: center;
        padding: 3rem 0;
    }

    .page-header h1 {
        font-size: 2.5rem;
        font-weight: 800;
        color: var(--text-primary);
        margin-bottom: 0.75rem;
    }

    .page-header p {
        font-size: 1.125rem;
        color: var(--text-secondary);
        max-width: 600px;
        margin: 0 auto;
    }

    /* ----- Main Grid ----- */
    .app-grid {
        display: grid;
        grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
        gap: 2rem;
        padding-bottom: 4rem;
        align-items: start;
    }

    /* ----- Form Cards ----- */
    .card {
        background: var(--surface);
        border-radius: var(--radius-lg);
        border: 1px solid var(--border);
        box-shadow: var(--shadow-md);
        margin-bottom: 1.5rem;
        overflow: hidden;
        transition: box-shadow 0.2s;
    }

    .card:hover {
        box-shadow: var(--shadow-lg);
    }

    .card-header {
        background: #F8FAFC;
        padding: 1.25rem 1.5rem;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .card-header h2 {
        font-size: 1.125rem;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: var(--text-primary);
    }

    .card-header h2 i {
        color: var(--primary);
    }

    .card-body {
        padding: 1.5rem;
    }

    /* ----- Form Elements ----- */
    .form-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1.25rem;
    }

    .form-grid.col-3 {
        grid-template-columns: repeat(3, 1fr);
    }

    .form-grid.col-1 {
        grid-template-columns: 1fr;
    }

    @media (max-width: 640px) {

        .form-grid,
        .form-grid.col-3 {
            grid-template-columns: 1fr;
        }
    }

    .form-group {
        display: flex;
        flex-direction: column;
        gap: 0.375rem;
        position: relative;
    }

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

    .required-star {
        color: var(--danger);
    }

    .form-control {
        width: 100%;
        padding: 0.625rem 0.875rem;
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        font-family: inherit;
        font-size: 0.95rem;
        color: var(--text-primary);
        transition: all 0.2s;
        background: #fff;
    }

    .form-control:focus {
        outline: none;
        border-color: var(--primary);
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
    }

    .form-control.error {
        border-color: var(--danger);
        background-color: #FEF2F2;
    }

    .error-msg {
        color: var(--danger);
        font-size: 0.75rem;
        font-weight: 500;
        margin-top: 0.25rem;
        display: none;
    }

    /* ----- Items Table ----- */
    .table-responsive {
        overflow-x: auto;
        margin: -1.5rem;
        padding: 1.5rem;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        min-width: 700px;
    }

    th {
        text-align: left;
        font-size: 0.875rem;
        font-weight: 600;
        color: var(--text-secondary);
        padding-bottom: 0.75rem;
        border-bottom: 2px solid var(--border);
    }

    td {
        padding: 0.75rem 0;
        border-bottom: 1px solid var(--border);
        vertical-align: top;
    }

    td .form-control {
        margin-bottom: 0.25rem;
    }

    /* Specific Table Column Widths */
    .col-desc {
        width: 35%;
    }

    .col-qty {
        width: 12%;
    }

    .col-rate {
        width: 18%;
    }

    .col-gst {
        width: 15%;
    }

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

    .col-action {
        width: 5%;
        text-align: center;
    }

    .item-amount {
        font-weight: 600;
        color: var(--text-primary);
        display: block;
        padding-top: 0.625rem;
        text-align: right;
    }

    /* ----- Buttons ----- */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
        padding: 0.625rem 1.25rem;
        font-weight: 600;
        font-size: 0.95rem;
        border-radius: var(--radius-md);
        border: none;
        cursor: pointer;
        transition: all 0.2s;
        font-family: inherit;
    }

    .btn i {
        font-size: 1rem;
    }

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

    .btn-primary:hover {
        background-color: var(--primary-dark);
        transform: translateY(-1px);
    }

    .btn-secondary {
        background-color: white;
        color: var(--text-primary);
        border: 1px solid var(--border);
    }

    .btn-secondary:hover {
        background-color: #F1F5F9;
    }

    .btn-danger-outline {
        background: transparent;
        color: var(--text-muted);
        padding: 0.625rem;
    }

    .btn-danger-outline:hover {
        color: var(--danger);
        background: #FEF2F2;
        border-radius: var(--radius-md);
    }

    .btn-full {
        width: 100%;
    }

    .btn-lg {
        padding: 0.875rem 1.5rem;
        font-size: 1.05rem;
    }

    /* ----- Summary Section ----- */
    .summary-box {
        background: #F8FAFC;
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        padding: 1.25rem;
        margin-top: 1.5rem;
        width: 100%;
        max-width: 400px;
        margin-left: auto;
    }

    .summary-row {
        display: flex;
        justify-content: space-between;
        margin-bottom: 0.75rem;
        font-size: 0.95rem;
        color: var(--text-secondary);
    }

    .summary-row.total {
        margin-top: 1rem;
        padding-top: 1rem;
        border-top: 2px solid var(--border);
        font-size: 1.25rem;
        font-weight: 800;
        color: var(--theme-color);
    }

    /* ----- Theme Selector ----- */
    .theme-selector {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    .theme-btn {
        width: 36px;
        height: 36px;
        border-radius: 50%;
        border: 3px solid white;
        box-shadow: 0 0 0 1px var(--border);
        cursor: pointer;
        transition: transform 0.2s;
    }

    .theme-btn:hover {
        transform: scale(1.1);
    }

    .theme-btn.active {
        box-shadow: 0 0 0 2px var(--text-primary);
        transform: scale(1.1);
    }

    /* ----- Currency Selector ----- */
    .currency-selector {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    .currency-btn {
        padding: 0.35rem 0.65rem;
        border-radius: var(--radius-md);
        border: 1.5px solid var(--border);
        background: white;
        font-size: 0.82rem;
        font-weight: 600;
        color: var(--text-secondary);
        cursor: pointer;
        transition: all 0.2s;
        font-family: inherit;
    }

    .currency-btn:hover {
        border-color: var(--primary);
        color: var(--primary);
    }

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

    /* ----- Logo Upload ----- */
    .logo-upload-area {
        border: 2px dashed var(--border);
        border-radius: var(--radius-md);
        padding: 1.25rem;
        text-align: center;
        cursor: pointer;
        transition: all 0.2s;
        background: #fafbfc;
        position: relative;
    }

    .logo-upload-area:hover {
        border-color: var(--primary);
        background: #eff6ff;
    }

    .logo-upload-area input[type="file"] {
        position: absolute;
        inset: 0;
        opacity: 0;
        cursor: pointer;
        width: 100%;
        height: 100%;
    }

    .logo-upload-area .upload-icon {
        font-size: 1.5rem;
        color: var(--text-muted);
        margin-bottom: 0.4rem;
    }

    .logo-upload-area p {
        font-size: 0.8rem;
        color: var(--text-muted);
    }

    .logo-preview-wrap {
        display: none;
        align-items: center;
        gap: 0.75rem;
        margin-top: 0.75rem;
    }

    .logo-preview-wrap.visible {
        display: flex;
    }

    .logo-preview-wrap img {
        max-height: 60px;
        max-width: 140px;
        object-fit: contain;
        border-radius: 4px;
        border: 1px solid var(--border);
    }

    .logo-preview-wrap button {
        background: none;
        border: none;
        color: var(--danger);
        cursor: pointer;
        font-size: 0.85rem;
        padding: 0.25rem 0.5rem;
        border-radius: var(--radius-md);
        transition: background 0.2s;
    }

    .logo-preview-wrap button:hover {
        background: #FEF2F2;
    }

    /* ----- Live Preview Modal ----- */
    .preview-modal-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.65);
        z-index: 1000;
        align-items: center;
        justify-content: center;
        padding: 1.5rem;
    }

    .preview-modal-overlay.open {
        display: flex;
    }

    .preview-modal {
        background: white;
        border-radius: var(--radius-lg);
        max-width: 900px;
        width: 100%;
        max-height: 90vh;
        display: flex;
        flex-direction: column;
        box-shadow: 0 25px 50px rgba(0, 0, 0, 0.3);
        overflow: hidden;
    }

    .preview-modal-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 1rem 1.5rem;
        border-bottom: 1px solid var(--border);
        background: #F8FAFC;
        flex-shrink: 0;
    }

    .preview-modal-header h3 {
        font-size: 1rem;
        font-weight: 700;
        color: var(--text-primary);
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .preview-modal-header h3 i {
        color: var(--primary);
    }

    .preview-modal-close {
        background: none;
        border: none;
        font-size: 1.25rem;
        color: var(--text-muted);
        cursor: pointer;
        padding: 0.25rem 0.5rem;
        border-radius: var(--radius-md);
        transition: all 0.2s;
    }

    .preview-modal-close:hover {
        background: #e2e8f0;
        color: var(--text-primary);
    }

    .preview-modal-body {
        overflow-y: auto;
        padding: 1.5rem;
        flex: 1;
        background: #e5e7eb;
    }

    .preview-inner {
        background: white;
        border-radius: 6px;
        overflow: hidden;
        box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
        /* Scale to fit - will be overridden inline */
        transform-origin: top left;
        width: 794px;
    }
    .preview-inner-box { position: relative; overflow: hidden; margin: 0 auto; }

    /* ========================================= */
    /* HIDDEN PDF TEMPLATE STYLES                */
    /* ========================================= */
    #pdf-container {
        position: fixed;
        width: 794px;
        left: -10000px;
        top: 0;
        visibility: hidden;
        pointer-events: none;
    }

    /* The container passed to html2pdf */
    .pdf-wrapper {
        background: white;
        color: #111827;
        font-family: 'Inter', 'Inter Fallback', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
        width: 794px;
        max-width: none;
        padding: 30px;
        box-sizing: border-box;
        position: relative;
        overflow: hidden;
    }

    .pdf-wrapper {
        page-break-inside: avoid;
    }

    .pdf-table tr {
        page-break-inside: avoid;
    }

    .pdf-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 30px;
        border-bottom: 3px solid var(--theme-color);
        padding-bottom: 20px;
    }

    .pdf-header-left h1 {
        font-family: 'Playfair Display', serif;
        font-size: 32px;
        color: var(--theme-color);
        margin-bottom: 5px;
        line-height: 1.2;
    }

    .pdf-header-left p {
        font-size: 11px;
        color: #4b5563;
        margin-bottom: 3px;
        line-height: 1.4;
        max-width: 250px;
    }

    .pdf-header-right {
        text-align: right;
    }

    .pdf-header-right h2 {
        font-size: 42px;
        font-weight: 800;
        color: var(--theme-color);
        letter-spacing: 2px;
        text-transform: uppercase;
        line-height: 1;
        margin-bottom: 10px;
    }

    .pdf-meta {
        display: grid;
        grid-template-columns: auto auto;
        gap: 5px 15px;
        text-align: left;
        background: #f9fafb;
        padding: 10px 15px;
        border-radius: 6px;
        border: 1px solid #e5e7eb;
        display: inline-grid;
    }

    .pdf-meta strong {
        font-size: 11px;
        color: #374151;
    }

    .pdf-meta span {
        font-size: 11px;
        color: #111827;
        text-align: right;
        font-weight: 600;
    }

    .pdf-bill-to {
        margin-bottom: 30px;
    }

    .pdf-section-title {
        font-size: 12px;
        font-weight: 700;
        color: var(--theme-color);
        text-transform: uppercase;
        letter-spacing: 1px;
        margin-bottom: 10px;
    }

    .pdf-client-box {
        background: #f8fafc;
        border-left: 4px solid var(--theme-color);
        padding: 15px;
        border-radius: 0 6px 6px 0;
        max-width: 350px;
    }

    .pdf-client-name {
        font-size: 16px;
        font-weight: 700;
        color: #111827;
        margin-bottom: 5px;
        line-height: 1.3;
    }

    .pdf-client-details p {
        font-size: 11px;
        color: #4b5563;
        margin-bottom: 3px;
        line-height: 1.4;
    }

    .pdf-table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 30px;
    }

    .pdf-table th {
        background-color: var(--theme-color);
        color: white;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        padding: 12px 15px;
        text-align: left;
        border: none;
    }

    .pdf-table th:last-child {
        text-align: right;
    }

    .pdf-table td {
        padding: 12px 15px;
        font-size: 11px;
        color: #1f2937;
        border-bottom: 1px solid #e5e7eb;
        vertical-align: top;
    }

    .pdf-table tr:last-child td {
        border-bottom: 2px solid var(--theme-color);
    }

    .pdf-item-name {
        font-weight: 600;
        color: #111827;
        margin-bottom: 3px;
        font-size: 12px;
    }

    .pdf-item-desc {
        color: #6b7280;
        font-size: 10px;
    }

    .pdf-text-right {
        text-align: right;
    }

    .pdf-text-center {
        text-align: center;
    }

    .pdf-summary-wrapper {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 40px;
    }

    .pdf-bank-details {
        max-width: 350px;
        background: #f9fafb;
        padding: 15px;
        border-radius: 6px;
        border: 1px solid #e5e7eb;
    }

    .pdf-bank-grid {
        display: grid;
        grid-template-columns: 100px auto;
        gap: 5px;
        font-size: 10px;
    }

    .pdf-bank-grid strong {
        color: #4b5563;
    }

    .pdf-bank-grid span {
        color: #111827;
        font-weight: 600;
    }

    .pdf-totals {
        width: 250px;
    }

    .pdf-total-row {
        display: flex;
        justify-content: space-between;
        padding: 6px 0;
        font-size: 12px;
        color: #4b5563;
    }

    .pdf-total-row span:last-child {
        font-weight: 600;
        color: #111827;
    }

    .pdf-grand-total {
        display: flex;
        justify-content: space-between;
        background-color: var(--theme-color);
        color: white;
        padding: 10px 15px;
        border-radius: 6px;
        margin-top: 10px;
        font-size: 16px;
        font-weight: 700;
    }

    .pdf-grand-total span {
        color: white !important;
        font-weight: 800 !important;
    }

    .pdf-footer {
        margin-top: auto;
        padding-top: 20px;
        border-top: 1px solid #e5e7eb;
        font-size: 10px;
        color: #6b7280;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 20px;
    }

    .pdf-footer strong {
        color: #374151;
        display: block;
        margin-bottom: 3px;
    }

    .currency-symbol {
        font-family: sans-serif;
    }


    .app-grid {
        display: grid;
        grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
        gap: 2rem;
        padding-bottom: 4rem;
        align-items: start;
    }

    /* Tablet */
    @media (max-width: 1024px) {
        .app-grid {
            grid-template-columns: 1fr;
        }
    }


    aside {
        width: 100%;
    }

    @media (max-width: 1024px) {
        aside .card {
            position: static !important;
            top: auto !important;
        }
    }

    @media (max-width: 768px) {
        .form-grid {
            grid-template-columns: 1fr;
            gap: 1rem;
        }

        .form-grid.col-3 {
            grid-template-columns: 1fr;
        }
    }

    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    @media (max-width: 640px) {
        table {
            min-width: 600px;
        }
    }

    @media (max-width: 640px) {
        .card-body {
            padding: 1rem;
        }

        .card-header {
            padding: 1rem;
        }

        .page-header h1 {
            font-size: 1.8rem;
        }
    }

    @media (max-width: 640px) {
        .summary-box {
            max-width: 100%;
            margin-left: 0;
        }
    }

    @media (max-width: 640px) {
        .btn {
            width: 100%;
        }

        .btn-lg {
            font-size: 1rem;
        }
    }

    @media (max-width: 992px) {
        .form-wrapper {
            max-width: 100%;
            overflow: hidden !important;
        }
    }


/* ========================================================== */
/* MOBILE / UX IMPROVEMENTS                                    */
/* ========================================================== */
#generator { scroll-margin-top: 80px; }

.hero-cta {
    display: inline-flex; align-items: center; gap: .6rem;
    margin: 1.25rem 0 .5rem; padding: .85rem 1.75rem;
    background: var(--primary); color: #fff; font-weight: 700; font-size: 1.05rem;
    border-radius: 999px; text-decoration: none;
    box-shadow: 0 8px 20px -6px rgba(99,102,241,.6);
}
.hero-cta:hover { background: var(--primary-dark); }

/* iOS zooms into inputs below 16px - prevent that */
@media (max-width: 768px) {
    .form-control, input, select, textarea { font-size: 16px !important; }
    .form-control { min-height: 46px; }
    .btn { min-height: 46px; }
    .theme-btn { width: 40px !important; height: 40px !important; }
    .currency-btn { min-height: 42px; }
}

/* Line items: turn the wide table into one card per item on phones */
@media (max-width: 640px) {
    .table-responsive { margin: 0 !important; padding: 0 !important; overflow: visible; }
    #itemsTable, #itemsTable tbody, #itemsTable tr, #itemsTable td { display: block; width: 100% !important; min-width: 0 !important; max-width: none !important; }
    #itemsTable { min-width: 0 !important; }
    #itemsTable thead { display: none; }
    #itemsTable tr {
        position: relative; border: 1px solid var(--border); border-radius: var(--radius-md);
        padding: .85rem; margin-bottom: .85rem; background: #fcfcfd;
    }
    #itemsTable td { border: 0; padding: .25rem 0; }
    #itemsTable td[data-label]::before {
        content: attr(data-label); display: block; font-size: .75rem; font-weight: 600;
        color: var(--text-secondary); margin-bottom: .2rem;
    }
    #itemsTable td.col-qty, #itemsTable td.col-rate, #itemsTable td.col-gst { display: inline-block; width: 32% !important; vertical-align: top; padding-right: 2%; }
    #itemsTable td.col-gst { padding-right: 0; }
    #itemsTable td.col-amt { font-weight: 700; padding-top: .6rem; }
    #itemsTable td.col-action { position: absolute; top: .4rem; right: .4rem; width: auto !important; padding: 0; }
    #itemsTable td.col-desc { padding-right: 2.5rem; }
}

/* Sticky action bar on phones/tablets: Total + Preview + Download always reachable */
.mobile-actionbar { display: none; }
@media (max-width: 1024px) {
    .mobile-actionbar {
        display: flex; align-items: center; gap: .6rem;
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 900;
        background: #fff; border-top: 1px solid var(--border);
        padding: .6rem .8rem calc(.6rem + env(safe-area-inset-bottom, 0px));
        box-shadow: 0 -6px 18px rgba(15,23,42,.08);
    }
    .mobile-actionbar .mab-total { flex: 1; line-height: 1.2; min-width: 0; }
    .mobile-actionbar .mab-total small { display: block; font-size: .7rem; color: var(--text-muted); }
    .mobile-actionbar .mab-total strong { font-size: 1.05rem; color: var(--text-primary); white-space: nowrap; }
    .mobile-actionbar .btn { width: auto !important; padding: .65rem 1rem; }
    body { padding-bottom: 76px; }
}

/* Inline toast (replaces blocking alert() popups) */
.flow-toast {
    position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px);
    background: #0f172a; color: #fff; padding: .75rem 1.1rem; border-radius: .6rem;
    font-size: .9rem; max-width: 90vw; z-index: 2000; opacity: 0; pointer-events: none;
    transition: opacity .25s, transform .25s;
}
.flow-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.flow-toast.err { background: #b91c1c; }

.preview-modal-overlay { padding: .5rem; }
@media (min-width: 641px) { .preview-modal-overlay { padding: 1.5rem; } }
.preview-modal { max-height: 94vh; max-height: 94dvh; }
@media (max-width: 640px) { .preview-modal-body { padding: .5rem; } }
