:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --secondary: #64748b;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #d97706;
    --bg: #f1f5f9;
    --card: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --sidebar-w: 240px;
    --navbar-h: 60px;
}

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

body { font-family: 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }

/* ── Login ── */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%); }
.login-card { background: var(--card); border-radius: 16px; padding: 40px; width: 100%; max-width: 400px; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.login-logo { text-align: center; margin-bottom: 28px; }
.login-logo .logo-icon { font-size: 48px; }
.login-logo h1 { font-size: 24px; font-weight: 700; color: var(--primary); margin: 8px 0 4px; }
.login-logo p { color: var(--text-muted); font-size: 13px; }
.login-hint { text-align: center; margin-top: 16px; color: var(--text-muted); font-size: 12px; }

/* ── Navbar ── */
.navbar { position: fixed; top: 0; left: 0; right: 0; height: var(--navbar-h); background: var(--primary); color: #fff; display: flex; align-items: center; justify-content: space-between; padding: 0 20px; z-index: 100; box-shadow: 0 2px 8px rgba(0,0,0,.15); }
.nav-brand { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.nav-toggle { background: none; border: none; color: #fff; font-size: 22px; cursor: pointer; display: none; }

/* ── Layout ── */
.layout { display: flex; margin-top: var(--navbar-h); min-height: calc(100vh - var(--navbar-h)); }

/* ── Sidebar ── */
.sidebar { width: var(--sidebar-w); background: #1e293b; flex-shrink: 0; position: fixed; top: var(--navbar-h); bottom: 0; overflow-y: auto; transition: transform .3s; }
.nav-menu { list-style: none; padding: 16px 0; }
.nav-menu li a { display: flex; align-items: center; gap: 10px; padding: 12px 20px; color: #94a3b8; text-decoration: none; transition: all .2s; font-size: 14px; }
.nav-menu li a:hover, .nav-menu li a.active { background: rgba(255,255,255,.08); color: #fff; border-left: 3px solid var(--primary); padding-left: 17px; }
.nav-divider { height: 1px; background: #334155; margin: 8px 16px; }

/* ── Main Content ── */
.main-content { margin-left: var(--sidebar-w); flex: 1; padding: 24px; }

/* ── Cards ── */
.card { background: var(--card); border-radius: 12px; padding: 24px; box-shadow: 0 1px 4px rgba(0,0,0,.06); border: 1px solid var(--border); margin-bottom: 20px; }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.card-title { font-size: 16px; font-weight: 600; color: var(--text); }

/* ── Stats Grid ── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--card); border-radius: 12px; padding: 20px; border: 1px solid var(--border); display: flex; align-items: center; gap: 16px; }
.stat-icon { width: 52px; height: 52px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 24px; flex-shrink: 0; }
.stat-icon.blue { background: #dbeafe; }
.stat-icon.green { background: #dcfce7; }
.stat-icon.orange { background: #fef3c7; }
.stat-icon.purple { background: #ede9fe; }
.stat-value { font-size: 22px; font-weight: 700; color: var(--text); }
.stat-label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ── Forms ── */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-weight: 500; margin-bottom: 6px; color: var(--text); font-size: 13px; }
.form-control { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; color: var(--text); background: #fff; transition: border-color .2s; }
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
select.form-control { cursor: pointer; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }

/* ── Buttons ── */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 18px; border-radius: 8px; border: none; font-size: 14px; font-weight: 500; cursor: pointer; transition: all .2s; text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-secondary { background: var(--secondary); color: #fff; }
.btn-secondary:hover { background: #475569; }
.btn-outline { background: transparent; color: var(--primary); border: 1px solid var(--primary); }
.btn-outline:hover { background: var(--primary); color: #fff; }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; padding: 12px; }
.btn-icon { padding: 6px 10px; }

/* ── Table ── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead th { background: #f8fafc; padding: 11px 14px; text-align: left; font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .5px; border-bottom: 2px solid var(--border); }
tbody td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: none; }

/* ── Alerts ── */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-danger { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ── Badge ── */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-success { background: #dcfce7; color: #166534; }
.badge-primary { background: #dbeafe; color: #1e40af; }

/* ── Invoice Builder ── */
.invoice-items-table { width: 100%; border-collapse: collapse; }
.invoice-items-table th { background: #f8fafc; padding: 10px 8px; font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; border-bottom: 2px solid var(--border); }
.invoice-items-table td { padding: 8px; border-bottom: 1px solid var(--border); }
.invoice-items-table td input, .invoice-items-table td select { padding: 7px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; width: 100%; }
.invoice-items-table td input:focus, .invoice-items-table td select:focus { outline: none; border-color: var(--primary); }

/* ── Totals Box ── */
.totals-box { background: #f8fafc; border-radius: 10px; padding: 20px; min-width: 280px; }
.totals-row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; color: var(--text-muted); }
.totals-row.total { font-size: 18px; font-weight: 700; color: var(--text); border-top: 2px solid var(--border); margin-top: 8px; padding-top: 12px; }

/* ── Search Bar ── */
.search-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.search-bar .form-control { max-width: 300px; }

/* ── Page Header ── */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; flex-wrap: wrap; gap: 12px; }
.page-header h2 { font-size: 20px; font-weight: 700; }

/* ── Modal ── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 200; align-items: center; justify-content: center; }
.modal-overlay.open { display: flex; }
.modal { background: var(--card); border-radius: 16px; padding: 28px; width: 100%; max-width: 500px; max-height: 90vh; overflow-y: auto; }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-title { font-size: 16px; font-weight: 600; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); }

/* ── Responsive ── */
@media (max-width: 768px) {
    .nav-toggle { display: block; }
    .sidebar { transform: translateX(-100%); z-index: 99; }
    .sidebar.open { transform: translateX(0); }
    .main-content { margin-left: 0; padding: 16px; }
    .stats-grid { grid-template-columns: 1fr 1fr; }
    .form-row { grid-template-columns: 1fr; }
    .page-header { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 480px) {
    .stats-grid { grid-template-columns: 1fr; }
    .login-card { margin: 16px; padding: 28px 20px; }
}

/* ── Print / PDF ── */
/* ── Invoice Print A5 Portrait ── */

@media print {

    @page {
        size: A5 landscape;
        margin: 6mm;
    }

    html,
    body {
        background: #fff;
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
        font-size: 15.5px;
    }

    .navbar,
    .sidebar,
    .page-header,
    .btn,
    .no-print {
        display: none !important;
    }

    /* Force full-bleed regardless of whatever wrapper/container markup
       lives in header.php / footer.php - eliminates the large white
       border seen when a centered container or default page margin
       from the browser's print dialog eats into the page. */
    .layout,
    .main-content,
    .container,
    .content-wrapper,
    .wrapper {
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
        max-width: none !important;
        min-height: 0;
        display: block;
    }

    /* Stretch to fill the page (fixes white space), with a small safety buffer
       so it doesn't tip into a 2nd page. Works correctly now that print dialog
       Margins = None and Headers/Footers = Off (those were consuming extra space before).
       If content is short, footer (margin-top:auto below) fills the rest.
       If content grows past ~8 GST / 10 non-GST rows, box grows naturally - no clipping. */
    /* Box hugs its natural content - no forced full-page stretch
       (that created an odd large empty gap in the middle for invoices with few items).
       8 GST / 10 non-GST rows still fit comfortably on one A5 landscape page. */
    .card.invoice-print {
        display: flex;
        flex-direction: column;
        width: 100%;
        min-height: 134mm;
        border: 1.5pt solid #000;
        box-shadow: none;
        padding: 0;
        margin: 0;
        box-sizing: border-box;
        line-height: 1.05;
    }


    .table-wrapper {
        width: 100%;
    }

    /* ================= ORIGINAL GST / NON-GST header (unchanged) ================= */

    .invoice-header {
        text-align: center;
        border-bottom: 0.75pt solid #000;
        padding: 2px;
    }

    .invoice-header .invoice-type-title {
        font-size: 14px;
        font-weight: bold;
        margin: 0;
        line-height: 1.15;
    }

    .invoice-header .shop-name {
        font-size: 22px;
        font-weight: bold;
        margin: 1px 0;
        line-height: 1.15;
    }

    .invoice-header p {
        margin: 0;
        font-size: 13px;
        line-height: 1.15;
    }

    .bank-details,
    .gstin-row {
        text-align: center;
        border-bottom: 0.75pt solid #000;
        padding: 2px;
        font-size: 13px;
        font-weight: bold;
        line-height: 1.15;
    }

    .invoice-info-box {
        display: flex;
        border-bottom: 0.75pt solid #000;
    }

    .buyer-box {
        flex: 1;
        padding: 2px 6px;
        border-right: 0.75pt solid #000;
    }

    .buyer-box p {
        margin: 0;
        font-size: 12px;
        line-height: 1.15;
    }

    .meta-box {
        width: 180px;
        display: flex;
        flex-direction: column;
        border-left: 0.75pt solid #000;
    }

    .meta-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1px 6px;
        border-bottom: 0.75pt solid #000;
        font-size: 12px;
        line-height: 1.15;
    }

    .meta-row:last-child {
        border-bottom: none;
    }

    /* ================= EWAY-ONLY: Full width title ================= */

    .eway-title {
        text-align: center;
        font-size: 13px;
        font-weight: bold;
        margin: 2px 0;
        line-height: 1.15;
    }

    /* ================= EWAY-ONLY: Left col (Seller/Consignee/Buyer) + Right col (Meta) ================= */
    /* display:flex with default align-items:stretch makes both columns
       automatically match height - the right meta box always stretches
       to equal the left column's natural content height, exactly like
       the Hajiyar Traders sample. */

    .invoice-top-flex {
        display: flex;
        border-top: 0.75pt solid #000;
        border-bottom: 0.75pt solid #000;
    }

    .left-col {
        flex: 1;
        display: flex;
        flex-direction: column;
        border-right: 0.75pt solid #000;
    }

    .seller-box {
        padding: 3px 6px;
        border-bottom: 0.75pt solid #000;
    }

    .seller-box .shop-name {
        font-size: 16px;
        font-weight: bold;
        margin: 0 0 1px;
        line-height: 1.15;
    }

    .seller-box p {
        margin: 0;
        font-size: 11px;
        line-height: 1.25;
    }

    .party-section {
        padding: 3px 6px;
        border-bottom: 0.75pt solid #000;
    }

    .party-section.last {
        border-bottom: none;
    }

    .party-section p {
        margin: 0;
        font-size: 11.5px;
        line-height: 1.2;
    }

    .meta-col {
        width: 230px;
        display: flex;
        flex-direction: column;
    }

    .meta-row {
        display: flex;
        border-bottom: 0.75pt solid #000;
        font-size: 10.5px;
        line-height: 1.25;
    }

    .meta-row:last-child {
        border-bottom: none;
    }

    .meta-row > div {
        flex: 1;
        padding: 2px 5px;
        border-right: 0.75pt solid #000;
    }

    .meta-row > div:last-child {
        border-right: none;
    }

    .meta-row b {
        font-size: 11px;
    }

    .meta-row.grow {
        flex-grow: 1;
    }

    /* Product table */

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

    .invoice-items-table th {
        border: 0.75pt solid #000;
        padding: 2px 4px;
        text-align: center;
        font-size: 12px;
        line-height: 1.1;
        white-space: nowrap;
    }

    .invoice-items-table td {
        border-left: 0.75pt solid #000;
        border-right: 0.75pt solid #000;
        border-top: none;
        border-bottom: none;
        padding: 2px 4px;
        text-align: center;
        font-size: 12px;
        line-height: 1.1;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    /* These columns must always stay on a single line (original GST/NON-GST table) */
    .invoice-items-table td:nth-child(4),  /* Qty (e.g. "1.00 Nos") */
    .invoice-items-table td:nth-child(5),  /* Rate */
    .invoice-items-table td:last-child,    /* Amount */
    .invoice-items-table td:nth-last-child(2) { /* Net Rate */
        white-space: nowrap;
    }

    /* Same, but for the EWAY table's different column order
       (Sl No, Description, HSN/SAC, Quantity, Rate, Rate Incl.Tax, per, Disc%, Amount) */
    .eway-items-table td:nth-child(4),  /* Quantity */
    .eway-items-table td:nth-child(5),  /* Rate */
    .eway-items-table td:nth-child(6),  /* Rate (Incl. of Tax) */
    .eway-items-table td:last-child {   /* Amount */
        white-space: nowrap;
    }

    .invoice-items-table tbody tr:last-child td {
        border-bottom: 0.75pt solid #000;
    }

    /* Footer totals rows (Sub Total / CGST / SGST / Rounded Off / TOTAL) */
    .invoice-items-table tfoot td {
        border-left: 0.75pt solid #000;
        border-right: 0.75pt solid #000;
        border-top: none;
        border-bottom: none;
        padding: 1px 8px;
        font-size: 12px;
        line-height: 1.1;
    }

    .invoice-items-table tfoot tr:last-child td{
    border-bottom:none;
    }

    .table-total-row td {
    font-size:14px;
    font-weight:bold;
    border-top:none !important;
    border-bottom:none !important;
    }

    .spacer-row td {
        border: none !important;
        height: 10px;
    }

    /* Footer - pinned to the bottom of the card via margin-top:auto above. */

    .totals-spacer {
        flex-grow: 1;
        width: 9%;
        margin-left: auto;
        border-left: 0.75pt solid #000;
    }

    .totals-spacer.no-line {
        border-left: none;
    }

    /* EWAY layout: pushes Amount Chargeable / HSN summary / signature block
       down to the bottom of the A4 page when there are few items, exactly
       like the .totals-spacer above does for the GST/NON-GST layout. */
    .eway-footer-spacer {
        flex-grow: 1;
        width: 100%;
    }

    .invoice-total{
       width: 100%;
       margin-top: 0;
       margin-bottom: 0;
       border-top: 0.75pt solid #000 !important;
    }

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

    .invoice-total td{
       padding:4px 14px;
       font-size:14px;
       font-weight:bold;
       text-align:right;
    }

    .invoice-total td:first-child{
        width: 91%;
        border-right: 0.75pt solid #000 !important;
    }

    .invoice-total td:last-child{
        width: 9%;
    }
    .eway-total-row td {
        border-top: 0.75pt solid #000 !important;
        font-size: 13px;
        font-weight: bold;
        padding: 3px 8px;
    }

    .amount-chargeable-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-top: 0.75pt solid #000;
        padding: 3px 6px;
        font-size: 11.5px;
    }

    .amount-chargeable-row .eoe {
        font-style: italic;
    }

    .amount-chargeable-value {
        padding: 0 6px 3px;
        font-size: 11.5px;
        font-weight: bold;
        border-bottom: 0.75pt solid #000;
    }

    .hsn-summary-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 2px;
    }

    .hsn-summary-table th,
    .hsn-summary-table td {
        border: 0.75pt solid #000;
        padding: 2px 5px;
        font-size: 10.5px;
        text-align: center;
    }

    .hsn-total-row td {
        font-weight: bold;
    }

    .tax-amount-words {
        padding: 3px 6px;
        font-size: 11px;
        border-bottom: 0.75pt solid #000;
    }

    .invoice-footer {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        border-top: 0.75pt solid #000;
        padding: 4px 8px;
        margin-top: 0;
    }



    .declaration {
        flex: 1;
        font-size: 11px;
        line-height: 1.15;
        padding-right: 10px;
    }

    .declaration p {
        margin: 0;
    }

    .generated-invoice{
    margin-top:3px;
    font-size:11px;
    font-weight:600;
    color:#333;
    }

    .signature-section {
        display: flex;
        justify-content: flex-end;
        align-items: flex-end;
        gap: 35px;
        flex: 1;
    }

    .customer-sign,
    .signature {
        width: 45%;
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        font-size: 12px;
        line-height: 1.15;
    }

    .customer-sign {
        text-align: left;
    }

    .signature {
        text-align: right;
    }

    /* Multi-page invoices: force each item-page onto its own physical page,
       except the last one (which naturally ends the document). */
    .card.invoice-print:not(:last-of-type) {
        page-break-after: always;
        break-after: page;
        margin-bottom: 0;
    }

    .continued-row td {
        text-align: right;
        font-style: italic;
        padding: 4px 8px;
        border-top: 0.75pt solid #000 !important;
    }

}