:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --bg-main: #f8fafc;
    --bg-card: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --divider: #f1f5f9;
    --shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --shadow-paper: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Inter', sans-serif;
    --paper-width: 210mm;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-main);
    color: var(--text-main);
    font-family: var(--font-body);
    line-height: 1.5;
    overflow-x: hidden;
}

.app-container {
    width: 100%;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header Styles */
.main-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 30px;
    border-bottom: 1px solid var(--border);
    background: white;
    z-index: 100;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 16px;
}

.template-selector {
    display: flex;
    align-items: center;
    background: #f1f5f9;
    padding: 4px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    transition: all 0.2s;
}

.template-selector:focus-within {
    border-color: var(--primary);
    background: white;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.template-selector label {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-right: 8px;
    white-space: nowrap;
}

.template-selector select {
    background: transparent;
    border: none;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-main);
    cursor: pointer;
    outline: none;
    padding: 6px 0;
    padding-right: 20px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    font-size: 2rem;
}

.logo h1 {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-main);
}

.logo h1 span {
    color: var(--primary);
}

/* Button Styles */
.btn {
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn-primary {
    background-color: var(--primary);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background-color: #f1f5f9;
    color: var(--text-main);
}

.btn-secondary:hover {
    background-color: #e2e8f0;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.85rem;
}

.btn-icon {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    background: white;
    cursor: pointer;
    color: #ef4444;
    font-size: 1.2rem;
    transition: all 0.2s;
}

.btn-icon:hover {
    background-color: #fef2f2;
    border-color: #fecaca;
}

/* Layout Wrapper */
.content-wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr;
    /* Balanced 50-50 split */
    gap: 0;
    flex-grow: 1;
    /* Take remaining height in app-container flex */
    overflow: hidden;
}

/* Form Styles */
.form-section {
    height: 100%;
    overflow-y: auto;
    padding: 20px;
    border-right: 1px solid var(--border);
    background: #f8fafc;
}

.card {
    background: white;
    border-radius: 12px;
    border: 1px solid var(--border);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    padding: 16px;
    /* Reduced further from 20px */
}

.card-header {
    margin-bottom: 15px;
    /* Reduced from 20px */
}

.card-header h2 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    /* Smaller heading */
    margin-bottom: 2px;
}

.card-header p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.form-group-wrapper h3 {
    font-size: 0.75rem;
    /* Smaller section headers */
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 10px;
    /* Reduced space */
    font-weight: 700;
}

.divider {
    border: 0;
    border-top: 1px solid var(--divider);
    margin: 15px 0;
    /* Reduced from 24px */
}

.form-row {
    display: flex;
    gap: 10px;
    margin-bottom: 8px;
    /* Significantly reduced margin */
}

.form-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    /* Reduced gap between label and input */
    min-width: 0;
    /* Prevent overflow in flex */
}

.form-group.full {
    width: 100%;
}

.form-group.flex-3 {
    flex: 3;
}

.form-group.flex-2 {
    flex: 2;
}

.form-group.flex-1 {
    flex: 1;
}

.form-group.loading input {
    border-color: #3b82f6;
    background-image: linear-gradient(90deg, transparent, rgba(59, 130, 246, 0.1), transparent);
    background-size: 200% 100%;
    animation: loading-shimmer 1.5s infinite linear;
}

@keyframes loading-shimmer {
    0% {
        background-position: 200% 0;
    }

    100% {
        background-position: -200% 0;
    }
}

.form-group label {
    font-size: 0.75rem;
    /* Slightly smaller labels */
    font-weight: 600;
    color: var(--text-main);
}

.form-group input,
.form-group textarea,
.form-group select {
    padding: 6px 10px;
    /* More compact inputs */
    border: 1.5px solid var(--border);
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.85rem;
    /* Smaller input text */
    transition: border-color 0.2s, box-shadow 0.2s;
    outline: none;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

/* Specific fix for branding inputs overlap */
.branding-row {
    flex-direction: column;
}

/* Items List Compact - Grid Based */
.item-row {
    background: #f8fafc;
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 12px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Ensure form rows inside items use full width without weird overlaps */
.item-row .form-row {
    display: flex;
    gap: 10px;
    margin-bottom: 0;
}

/* First row: Item, HSN */
.item-row .row-1 .form-group:nth-child(1) {
    flex: 3;
}

/* Item Name */
.item-row .row-1 .form-group:nth-child(2) {
    flex: 1;
    min-width: 80px;
}

/* HSN */

/* Second row: Description (Full) */
.item-row .row-2 .form-group {
    flex: 1;
}

/* Third row: Qty, Unit, Rate, GST, Action */
.item-row .row-3 {
    align-items: flex-end;
}

.item-row .row-3 .form-group:nth-child(1) {
    flex: 0 0 60px;
}

/* Qty */
.item-row .row-3 .form-group:nth-child(2) {
    flex: 0 0 70px;
}

/* Unit */
.item-row .row-3 .form-group:nth-child(3) {
    flex: 1;
    min-width: 90px;
}

/* Rate */
.item-row .row-3 .form-group:nth-child(4) {
    flex: 0 0 85px;
}

/* GST */
.item-row .row-3 .form-group.actions {
    flex: 0 0 32px;
    margin-bottom: 4px;
}

/* Preview Section Styles */
.preview-section {
    background: #cbd5e1;
    padding: 20px;
    height: 100%;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.preview-sticky {
    width: 100%;
    max-width: 800px;
    display: flex;
    justify-content: center;
}

.invoice-paper {
    font-family: 'Ubuntu', sans-serif;
    background: white;
    width: 800px;
    min-height: 1120px;
    padding: 50px;
    /* Slightly more padding for professional look */
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    transform-origin: top center;
    transform: scale(var(--preview-scale, 1));
    flex-shrink: 0;
    transition: transform 0.2s ease-out;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

.preview-main-content {
    flex: 1;
    /* Pushes footer to bottom */
}

/* Header Layout: Logo & Seller Info */
.preview-header-layout {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    align-items: flex-start;
}

.preview-divider {
    border: none;
    border-top: 1px solid #e2e8f0;
    margin: 10px 0 25px 0;
}

.logo-preview-container {
    width: 200px;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    overflow: hidden;
}

.logo-preview-container img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.seller-info-preview {
    text-align: right;
    max-width: 400px;
}

.seller-info-preview h1 {
    font-size: 1.4rem;
    color: #1e293b;
    font-weight: 800;
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.seller-info-preview p {
    font-size: 0.85rem;
    color: #475569;
    line-height: 1.5;
    margin-bottom: 2px;
}

/* Title Bar */
.invoice-title-bar {
    text-align: center;
    margin-bottom: 30px;
    border-top: 2px solid #000;
    border-bottom: 2px solid #000;
    padding: 8px 0;
}

.type-badge {
    background: #000;
    color: white;
    padding: 4px 20px;
    display: inline-block;
    font-size: 1.1rem;
    letter-spacing: 3px;
    font-weight: 700;
}

/* Billing Layout: Bill To & Invoice Info */
.preview-billing-layout {
    display: flex;
    justify-content: space-between;
    gap: 40px;
    margin-bottom: 10px;
}

.billing-col {
    flex: 1;
}

.label-heading {
    font-size: 0.9rem;
    color: #334155;
    margin-bottom: 8px;
    font-weight: 500;
}

.client-name {
    font-weight: 800;
    font-size: 1.1rem;
    color: #0f172a;
    margin-bottom: 6px;
    text-transform: uppercase;
}

.billing-col p {
    font-size: 0.85rem;
    color: #475569;
    line-height: 1.5;
}

.invoice-meta-preview {
    min-width: 250px;
}

.meta-item {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 10px;
    margin-bottom: 4px;
    font-size: 0.95rem;
}

.meta-label {
    font-weight: 800;
    color: #0f172a;
    text-align: left;
}

.meta-value {
    color: #475569;
    text-align: left;
}

.place-of-supply-row {
    margin-top: 5px;
    /* Reduced space */
    margin-bottom: 5px;
    /* Reduced space */
    font-size: 0.95rem;
    font-weight: 700;
    color: #1e293b;
}

/* Items Table Enhancement: Grid Look */
.preview-items table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
}

.preview-items th {
    background: #f8fafc;
    /* Light gray header */
    text-align: left;
    padding: 5px 10px;
    /* Reduced vertical padding */
    font-size: 0.82rem;
    font-weight: 700;
    color: #1e293b;
    border: 1px solid #e2e8f0;
}

.preview-items td {
    padding: 4px 10px;
    /* Reduced vertical padding */
    border: 1px solid #e2e8f0;
    /* All sides border for grid look */
    font-size: 0.9rem;
    color: #334155;
    vertical-align: top;
}

.preview-items td div.gst-rate {
    font-size: 0.75rem;
    color: #64748b;
    margin-top: 2px;
}

.preview-items td:not(:nth-child(2)):not(:nth-child(3)) {
    text-align: right;
}

.preview-footer {
    margin-top: 20px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 60px;
    border-top: 3px solid #000;
    /* Added thick top border like image */
    padding-top: 25px;
}

.terms-section h3 {
    font-size: 0.85rem;
    font-weight: 800;
    text-transform: uppercase;
    margin-bottom: 12px;
    color: #1e293b;
}

.terms-section p {
    font-size: 0.8rem;
    color: #64748b;
    margin-bottom: 6px;
    line-height: 1.4;
}

/* Totals Area */
.totals-calculation-area {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 300px;
    /* Reduced slightly to ensure gap */
    margin-left: auto;
    background: #fdfdfd;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    padding: 15px 25px;
    border-right: 5px solid #1e293b;
    /* Right accent for right-side totals */
    box-sizing: border-box;
}

.totals-calculation-area .total-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.95rem;
    color: #475569;
    gap: 20px;
    padding: 2px 0;
}

.grand-total-minimal {
    font-weight: 800;
    font-size: 1.15rem;
    color: #0f172a !important;
    border-top: 2px solid #1e293b;
    border-bottom: 1px solid #e2e8f0;
    padding: 10px 0 !important;
    margin-top: 5px;
}

.bank-details-preview {
    background: #fdfdfd;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    padding: 15px 25px;
    border-left: 5px solid #1e293b;
    width: 300px;
    /* Matching width of totals */
    box-sizing: border-box;
    overflow-wrap: break-word;
}

.bank-details-preview h3 {
    color: #1e293b;
    font-size: 0.8rem !important;
    letter-spacing: 1px;
    margin-bottom: 8px !important;
    border-bottom: 1px solid #f1f5f9 !important;
    padding-bottom: 4px;
}

.bank-line {
    display: grid;
    grid-template-columns: 80px 1fr;
    font-size: 0.8rem;
    margin-bottom: 3px;
    line-height: 1.4;
}

.bank-line .label {
    color: #64748b;
    font-weight: 500;
}

.bank-line .value {
    color: #1e293b;
    font-weight: 700;
    word-break: break-word;
    white-space: normal;
}

#p-amountWords {
    display: none;
    /* Removed as requested */
}

.preview-bottom-layout {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid #f1f5f9;
}

.signature-section {
    margin-top: 80px;
    display: flex;
    justify-content: flex-end;
}

.sign-block {
    text-align: center;
    min-width: 250px;
}

.sign-block p {
    font-size: 0.85rem;
    color: #64748b;
    font-weight: 500;
}

.sign-block p span {
    color: #1e293b;
    font-weight: 800;
}

.sign-space {
    width: 180px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 5px 0;
    overflow: hidden;
}

.sign-space img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Responsive Table */
@media (max-width: 1024px) {
    .content-wrapper {
        grid-template-columns: 1fr;
    }

    .preview-section {
        padding: 20px;
        background: transparent;
    }

    .invoice-paper {
        padding: 30px;
        min-height: auto;
    }
}

/* Template container is handled dynamically by script.js */

/* Print Styles */
@media print {
    @page {
        margin: 0;
        size: auto;
    }

    html,
    body {
        background: white;
        height: 100%;
        margin: 0 !important;
        padding: 0 !important;
        overflow: visible !important;
    }

    .app-container,
    .main-header,
    .form-section,
    .preview-section {
        margin: 0 !important;
        padding: 0 !important;
        background: none !important;
    }

    body> :not(.app-container),
    .main-header,
    .form-section,
    .header-actions {
        display: none !important;
    }

    .content-wrapper {
        display: block !important;
        padding: 0 !important;
        margin: 0 !important;
    }

    .preview-section {
        display: block !important;
        width: 100% !important;
        padding: 0 !important;
        margin: 0 !important;
        border-radius: 0 !important;
    }

    .preview-sticky {
        position: static !important;
        width: 100% !important;
        max-width: none !important;
    }

    .invoice-paper {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        box-shadow: none !important;
        padding: 40px !important;
        margin: 0 !important;
        visibility: visible !important;
        transform: none !important;
        /* Critical: Disable scale for print */
    }

    .invoice-paper * {
        visibility: visible !important;
    }
}

/* --- Landing Page Specific Styles --- */
.landing-body {
    background: #ffffff;
    overflow-y: auto;
    height: auto;
    background-image: radial-gradient(#e2e8f0 1px, transparent 1px);
    background-size: 40px 40px;
}

.hero-section {
    padding: 120px 5% 80px;
    background: radial-gradient(circle at top right, rgba(238, 242, 255, 0.8) 0%, rgba(255, 255, 255, 0.5) 100%);
    text-align: center;
    position: relative;
    overflow: hidden;
}

.hero-section::after {
    content: '';
    position: absolute;
    top: -10%;
    right: -10%;
    width: 40%;
    height: 60%;
    background: radial-gradient(circle, rgba(79, 70, 229, 0.1) 0%, transparent 70%);
    z-index: 1;
    filter: blur(80px);
}

.hero-content {
    max-width: 900px;
    margin: 0 auto;
    position: relative;
    z-index: 2;
}

.hero-tag {
    display: inline-block;
    padding: 6px 16px;
    background: #eef2ff;
    color: var(--primary);
    border-radius: 100px;
    font-size: 0.85rem;
    font-weight: 700;
    margin-bottom: 24px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.hero-h1 {
    font-size: 4.5rem;
    font-weight: 800;
    line-height: 1.05;
    color: #0f172a;
    margin-bottom: 28px;
    letter-spacing: -3px;
    font-family: 'Outfit', sans-serif;
}

.hero-h1 span {
    background: linear-gradient(135deg, #4f46e5 0%, #a855f7 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.hero-p {
    font-size: 1.25rem;
    color: #64748b;
    margin-bottom: 40px;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}

.hero-cta {
    display: flex;
    gap: 16px;
    justify-content: center;
}

.btn-hero {
    padding: 16px 32px;
    font-size: 1.1rem;
    border-radius: 12px;
}

.hero-mockup {
    margin-top: 100px;
    perspective: 2000px;
}

.mockup-container {
    background: #ffffff;
    padding: 12px;
    border-radius: 24px;
    box-shadow: 0 50px 100px -20px rgba(15, 23, 42, 0.2), 0 30px 60px -30px rgba(0, 0, 0, 0.3);
    transform: rotateX(8deg) rotateY(-2deg) scale(1.05);
    max-width: 1050px;
    margin: 0 auto;
    border: 1px solid rgba(226, 232, 240, 0.8);
    animation: floatMockup 6s ease-in-out infinite;
}

@keyframes floatMockup {

    0%,
    100% {
        transform: rotateX(8deg) rotateY(-2deg) scale(1.05) translateY(0);
    }

    50% {
        transform: rotateX(6deg) rotateY(-1deg) scale(1.06) translateY(-20px);
    }
}

.mockup-container img {
    width: 100%;
    border-radius: 12px;
    display: block;
}

.features-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
    padding: 100px 5%;
    max-width: 1200px;
    margin: 0 auto;
}

.feature-card {
    padding: 40px;
    border-radius: 28px;
    background: rgba(255, 255, 255, 0.6);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(241, 245, 249, 0.8);
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
    overflow: hidden;
}

.feature-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.03) 0%, transparent 100%);
    pointer-events: none;
}

.feature-card:hover {
    transform: translateY(-10px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.05);
    border-color: #e2e8f0;
}

.feature-icon {
    width: 50px;
    height: 50px;
    background: #f1f5f9;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    margin-bottom: 24px;
}

.feature-h3 {
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: 16px;
    color: #0f172a;
}

.feature-p {
    color: #64748b;
    line-height: 1.6;
}

.cta-banner {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    padding: 100px 5%;
    text-align: center;
    color: white;
    margin: 120px 5%;
    border-radius: 48px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 40px 80px -20px rgba(15, 23, 42, 0.4);
}

.cta-banner::before {
    content: '';
    position: absolute;
    bottom: -50px;
    left: -50px;
    width: 200px;
    height: 200px;
    background: radial-gradient(circle, rgba(79, 70, 229, 0.2) 0%, transparent 70%);
}

.cta-banner h2 {
    font-size: 2.5rem;
    margin-bottom: 24px;
}

.landing-footer {
    padding: 60px 5%;
    text-align: center;
    border-top: 1px solid #f1f5f9;
    color: #94a3b8;
    font-size: 0.9rem;
}

@media (max-width: 768px) {
    .hero-h1 {
        font-size: 2.5rem;
    }
}

.company-fields-toggle.hidden {
    display: none;
}

.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(15, 23, 42, 0.7);
    backdrop-filter: blur(4px);
}

.modal-content {
    background-color: #fff;
    margin: 5% auto;
    width: 600px;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    overflow: hidden;
    animation: modalSlide 0.3s ease-out;
}

@keyframes modalSlide {
    from {
        transform: translateY(-30px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header {
    padding: 20px 25px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f8fafc;
}

.modal-header h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-main);
}

.close-modal {
    font-size: 28px;
    font-weight: bold;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.2s;
}

.close-modal:hover {
    color: #ef4444;
}

.modal-body {
    padding: 25px;
    max-height: 70vh;
    overflow-y: auto;
}

#modalFormContainer .company-fields-toggle.hidden {
    display: block !important;
}

.btn-success {
    background-color: #10b981 !important;
    color: white !important;
}