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

:root {
    /* === PROFESSIONAL DEEP NAVY PALETTE === */
    --navy:      #0f1f3d;
    --navy-mid:  #162847;
    --navy-light:#1e3a5f;
    --accent:    #2d8ef0;
    --accent-d:  #1a6cc7;
    --teal:      #0ea5a0;
    --green:     #10b981;
    --red:       #ef4444;
    --amber:     #f59e0b;

    --bg:        #f0f4f9;
    --bg-subtle: #e8edf5;
    --card:      #ffffff;
    --border:    #d1dae8;
    --border-mid:#b8c5d9;
    --text:      #0f1f3d;
    --text-mid:  #2e4265;
    --muted:     #5a7192;
    --muted-lt:  #8aa0bb;

    --font-ui:   'DM Sans', system-ui, sans-serif;
    --font-serif:'Crimson Text', Georgia, serif;

    --header-h:  60px;
    --radius:    10px;
    --shadow-sm: 0 1px 4px rgba(15,31,61,0.08);
    --shadow-md: 0 4px 20px rgba(15,31,61,0.11);
    --shadow-lg: 0 8px 40px rgba(15,31,61,0.16);
}

html, body {
    height: 100%;
}

body {
    font-family: var(--font-ui);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
}

/* =========================================
   APP HEADER
   ========================================= */
.app-header {
    background: linear-gradient(135deg, var(--navy) 0%, var(--navy-mid) 60%, var(--navy-light) 100%);
    color: #fff;
    padding: 0 24px;
    box-shadow: 0 2px 16px rgba(15,31,61,0.4);
    position: sticky;
    top: 0;
    z-index: 200;
    height: var(--header-h);
    border-bottom: 1px solid rgba(255,255,255,0.08);
}
.app-header-inner {
    max-width: 1400px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    gap: 16px;
}
.app-logo {
    display: flex;
    align-items: center;
    gap: 14px;
}
.app-header-logo {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    object-fit: cover;
    border: 2px solid rgba(45,142,240,0.5);
    box-shadow: 0 0 0 3px rgba(45,142,240,0.15);
}
.app-title { display: flex; flex-direction: column; }
.app-title-main {
    font-size: 0.97em;
    font-weight: 700;
    letter-spacing: 0.01em;
    line-height: 1.2;
    color: #fff;
}
.app-title-sub {
    font-size: 0.69em;
    opacity: 0.6;
    font-weight: 400;
    letter-spacing: 0.03em;
    margin-top: 1px;
}
.header-tabs { display: flex; gap: 6px; }
.header-tab {
    background: rgba(255,255,255,0.07);
    color: rgba(255,255,255,0.75);
    border: 1px solid rgba(255,255,255,0.14);
    border-radius: 7px;
    padding: 7px 18px;
    cursor: pointer;
    font-size: 0.84em;
    font-weight: 500;
    transition: all 0.2s;
    font-family: var(--font-ui);
    letter-spacing: 0.01em;
}
.header-tab:hover { background: rgba(255,255,255,0.14); color: #fff; }
.header-tab.active {
    background: var(--accent);
    color: #fff;
    font-weight: 700;
    border-color: transparent;
    box-shadow: 0 2px 12px rgba(45,142,240,0.4);
}

/* =========================================
   MAIN LAYOUT
   ========================================= */
.main-content { max-width: 820px; margin: 0 auto; padding: 28px 16px 60px; }

.panel { display: none; }
.panel.active { display: block; }

/* Single column layout */
.single-col-layout {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* =========================================
   FORM CARDS
   ========================================= */
.form-card {
    background: var(--card);
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border);
    overflow: hidden;
}
.card-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 18px;
    background: linear-gradient(135deg, #f8fafc 0%, #eef3fb 100%);
    border-bottom: 1px solid var(--border);
}
.card-icon { font-size: 1.1em; }
.card-header h3 {
    font-size: 0.95em;
    font-weight: 700;
    color: var(--navy);
    flex: 1;
}
.test-count-badge {
    background: var(--accent);
    color: #fff;
    font-size: 0.72em;
    font-weight: 700;
    padding: 3px 9px;
    border-radius: 20px;
    letter-spacing: 0.02em;
}
.card-body {
    padding: 16px 18px;
}
.tests-body { padding: 12px 14px; }

/* =========================================
   INPUT FORM CARD
   ========================================= */
.input-form {
    background: var(--card);
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(30,45,64,0.09);
    overflow: hidden;
}
.form-title {
    background: linear-gradient(135deg, #f8fafc 0%, #eef3fb 100%);
    border-bottom: 1px solid var(--border);
    padding: 22px 28px;
}
.form-title h2 {
    font-size: 1.25em;
    font-weight: 700;
    color: var(--primary);
    text-align: left;
}

.form-section {
    padding: 24px 28px;
    border-bottom: 1px solid var(--border);
}
.form-section:last-of-type { border-bottom: none; }

.section-header { margin-bottom: 18px; }
.section-header h3 {
    font-size: 1em;
    font-weight: 600;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 6px;
}

/* =========================================
   FORM FIELDS
   ========================================= */
.input-group-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 11px;
}
.input-group { display: flex; flex-direction: column; gap: 4px; }

label {
    font-size: 0.73em;
    font-weight: 700;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="tel"],
select,
textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1.5px solid var(--border);
    border-radius: 7px;
    font-size: 0.9em;
    font-family: var(--font-ui);
    color: var(--text);
    background: #fafdff;
    transition: border-color 0.18s, box-shadow 0.18s, background 0.18s;
    outline: none;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(45,142,240,0.14);
    background: #fff;
}
input::placeholder { color: var(--muted-lt); }
textarea { resize: vertical; }
select { cursor: pointer; }

/* Net payable display */
.net-payable-display {
    padding: 8px 12px;
    background: linear-gradient(135deg, #ecfdf5, #d1fae5);
    border: 1.5px solid #6ee7b7;
    border-radius: 7px;
    font-size: 1.05em;
    font-weight: 700;
    color: #047857;
    letter-spacing: 0.01em;
}

/* =========================================
   REF-BY AUTOCOMPLETE
   ========================================= */
.ref-by-wrap { position: relative; }

.autocomplete-container {
    position: relative;
}

.autocomplete-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: #fff;
    border: 1.5px solid var(--accent);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 500;
    max-height: 200px;
    overflow-y: auto;
}
.autocomplete-dropdown.open { display: block; }

.autocomplete-item {
    padding: 9px 14px;
    font-size: 0.88em;
    cursor: pointer;
    color: var(--text-mid);
    transition: background 0.12s, color 0.12s;
    border-bottom: 1px solid var(--bg-subtle);
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover,
.autocomplete-item.highlighted {
    background: linear-gradient(90deg, #e8f1fd, #f0f6ff);
    color: var(--navy);
    font-weight: 600;
}
.autocomplete-item mark {
    background: none;
    color: var(--accent);
    font-weight: 700;
}

/* =========================================
   TEST ITEMS TABLE (INPUT)
   ========================================= */
.test-table-header {
    display: grid;
    grid-template-columns: 36px 1fr 130px 42px;
    gap: 8px;
    padding: 6px 8px;
    margin-bottom: 6px;
    font-size: 0.75em;
    font-weight: 700;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.test-item-row {
    display: grid;
    grid-template-columns: 36px 1fr 130px 42px;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
    background: #f8fafc;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 8px;
    transition: border-color 0.2s;
}
.test-item-row:hover { border-color: var(--accent); }

.row-number {
    text-align: center;
    font-size: 0.8em;
    font-weight: 700;
    color: var(--muted);
    background: var(--border);
    border-radius: 4px;
    padding: 4px 0;
    user-select: none;
}
.test-item-row input {
    padding: 8px 10px;
    font-size: 0.9em;
    background: #fff;
    margin: 0;
}
.remove-btn {
    background: #fee2e2;
    color: var(--red);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.1em;
    font-weight: 700;
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.remove-btn:hover { background: #fca5a5; }

.add-btn {
    margin-top: 12px;
    width: 100%;
    padding: 11px;
    background: linear-gradient(135deg, #e0f2fe, #bae6fd);
    color: #0369a1;
    border: 1.5px dashed #7dd3fc;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9em;
    font-weight: 600;
    font-family: var(--font-ui);
    transition: all 0.2s;
    letter-spacing: 0.01em;
}
.add-btn:hover {
    background: linear-gradient(135deg, #bae6fd, #7dd3fc);
    border-style: solid;
    border-color: #0ea5e9;
}

/* =========================================
   ACTION BUTTONS
   ========================================= */
.button-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    flex-shrink: 0;
}
.primary-btn, .secondary-btn {
    padding: 12px;
    border: none;
    border-radius: var(--radius);
    font-size: 0.92em;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--font-ui);
    transition: all 0.2s;
    letter-spacing: 0.01em;
}
.primary-btn {
    background: linear-gradient(135deg, var(--accent), var(--accent-d));
    color: #fff;
    box-shadow: 0 3px 12px rgba(45,142,240,0.35);
}
.primary-btn:hover {
    background: linear-gradient(135deg, #3a9af5, var(--accent));
    box-shadow: 0 5px 18px rgba(45,142,240,0.45);
    transform: translateY(-1px);
}
.secondary-btn {
    background: #fff;
    color: var(--navy);
    border: 2px solid var(--border-mid);
}
.secondary-btn:hover {
    background: var(--bg-subtle);
    border-color: var(--accent);
    color: var(--accent-d);
    transform: translateY(-1px);
}

/* =========================================
   HISTORY PANEL
   ========================================= */
.loading-text { color: var(--muted); padding: 20px 20px; font-size: 0.95em; }

.history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 13px 20px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
    transition: background 0.15s;
}
.history-item:last-child { border-bottom: none; }
.history-item:hover { background: #f4f8ff; }
.history-item-details strong { font-size: 0.95em; color: var(--text); }
.history-item-details p { font-size: 0.8em; color: var(--muted); margin-top: 3px; }
.history-btns { display: flex; gap: 8px; flex-shrink: 0; }
.history-btns button {
    padding: 7px 14px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8em;
    font-weight: 600;
    font-family: var(--font-ui);
    transition: opacity 0.2s, transform 0.15s;
}
.history-btns button:hover { transform: translateY(-1px); }
.btn-view { background: #d1fae5; color: #065f46; }
.btn-view:hover { background: #a7f3d0; }
.btn-del  { background: #fee2e2; color: var(--red); }
.btn-del:hover  { background: #fecaca; }

/* =========================================
   INVOICE PRINT PAGE
   Screen preview (hidden by default)
   ========================================= */
.invoice-print-page {
    display: none;
    background: white;
}

/* =========================================
   PRINT STYLES — A5 Portrait, matches PDF
   ========================================= */
@media print {
    @page { size: A5 portrait; margin: 10mm 12mm; }

    body { background: white; margin: 0; padding: 0; }
    .app-header, .main-content { display: none !important; }

    .invoice-print-page {
        display: block !important;
        width: 100%;
        font-family: Arial, sans-serif;
        font-size: 9pt;
        color: #000;
    }

    /* Top bar */
    .inv-top-bar {
        display: flex;
        justify-content: space-between;
        font-size: 7pt;
        color: #555;
        margin-bottom: 4mm;
}

    /* Header */
.inv-header {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #555;
    padding-bottom: 4mm;
    margin-bottom: 3mm;
    min-height: 20mm;
}

.inv-header-text {
    text-align: center;
    margin: 0 auto;
    padding-left: 8mm;
}

.inv-logo-wrap {
    position: absolute; left: 0;
    top: 0;
}

.inv-logo {
    width: 19mm;
    height: 19mm;
    object-fit: contain;
}

.inv-name {
    font-size: 14pt;
    font-weight: bold;
    margin-bottom: 1mm;
}

.inv-header-text p {
    font-size: 8.5pt;
    margin: 0.5mm 0;
}

.inv-title {
    text-align: center;
    font-size: 12pt;
    font-weight: bold;
    text-decoration: none;
    margin-top: 2mm;
    margin-bottom: -2mm;
    position: relative;
    z-index: 1;
}

    /* Patient meta grid */
    .inv-meta-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        font-size: 9pt;
        margin-bottom: 3mm;
        row-gap: 1mm;
    }
    .inv-meta-left  { text-align: left; }
    .inv-meta-right { text-align: right; }

    /* Barcode — spans full width */
    .inv-barcode-cell {
        grid-column: 1 / span 2;
        text-align: center;
        margin: 2mm 0 1mm;
    }
    .inv-barcode-lines {
        display: flex;
        justify-content: center;
        gap: 2px;
        height: 12mm;
        margin-bottom: 1mm;
    }
    .inv-barcode-lines span {
        display: inline-block;
        background: #000;
        height: 100%;
    }
    .inv-barcode-text { font-size: 7pt; letter-spacing: 1px; }

    /* Ref by — spans full width */
    .inv-refby-cell {
        grid-column: 1 / span 2;
        font-size: 9pt;
        margin-top: 1mm;
    }

    /* Test table */
    .inv-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 9pt;
        margin-bottom: 3mm;
    }
    .inv-table th, .inv-table td {
        border: 1px solid #000;
        padding: 2px 4px;
        text-align: left;
    }
    .inv-table th { background: #f0f0f0; font-weight: bold; }
    .col-sl { width: 8mm; text-align: center !important; }
    .col-total { width: 22mm; text-align: right !important; }
    .inv-table td.col-sl { text-align: center; }
    .inv-table td.col-total { text-align: right; }

    /* Summary box  */
    .inv-summary-section { overflow: hidden; margin-bottom: 2mm; }
    .inv-summary-box {
        float: right;
        width: 55%;
        border: 1px solid #000;
        padding: 3px 6px;
        font-size: 9pt;
    }
    .inv-srow {
        display: flex;
        justify-content: space-between;
        margin: 1px 0;
    }
    .bold-srow { font-weight: bold; }

    /* In-words & Note */
    .inv-inwords { clear: both; font-size: 8.5pt; margin-bottom: 1mm; }
    .inv-note { font-size: 9pt; margin-bottom: 3mm; }

    /* Signature row */
    .inv-sign-row {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        border-top: 1px solid #000;
        padding-top: 3mm;
        margin-bottom: 3mm;
        font-size: 9pt;
    }
    .inv-prepared-by { display: flex; align-items: center; gap: 8mm; }
    .paid-stamp {
        font-size: 16pt;
        font-weight: bold;
        padding: 1mm 4mm;
        line-height: 1;
    }
    .inv-auth-sign { font-size: 8.5pt; }

    /* Dept line & Software */
    .inv-dept-line { font-size: 8.5pt; font-weight: bold; margin-bottom: 1mm; }
    .inv-software-line { font-size: 7.5pt; color: #555; }
}

/* Screen preview of invoice (when user clicks generate) */
@media screen {
    .invoice-print-page.visible {
        display: block;
        max-width: 148mm;
        margin: 32px auto;
        padding: 10mm 12mm;
        box-shadow: 0 8px 40px rgba(0,0,0,0.18);
        border-radius: 4px;
        font-family: Arial, sans-serif;
        font-size: 9pt;
        color: #000;
        background: #fff;
    }

    .inv-top-bar {
        display: flex;
        justify-content: space-between;
        font-size: 7pt;
        color: #555;
        margin-bottom: 4mm;
    }
    .inv-header {
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        border-bottom: 2px solid #000;
        padding-bottom: 4mm;
        margin-bottom: 3mm;
        min-height: 20mm;
    }
    .inv-logo-wrap {
        position: absolute;
        left: 0;
        top: 0;
    }
    .inv-logo {
        width: 19mm;
        height: 19mm;
        object-fit: contain;
    }
    .inv-header-text {
        text-align: center;
        margin: 0 auto;
        padding-left: 8mm;
    }

    .inv-name {
        font-size: 14pt;
        font-weight: bold;
        margin-bottom: 1mm;
    }

    .inv-header-text p {
        font-size: 8.5pt;
        margin: 0.5mm 0;
    }

    .inv-title {
        text-align: center;
        font-size: 12pt;
        font-weight: bold;
        text-decoration: none;
        margin-top: 2mm;
        margin-bottom: 0;
    }
    .inv-meta-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        font-size: 9pt;
        margin-bottom: 3mm;
        row-gap: 1mm;
    }
    .inv-meta-left  { text-align: left; }
    .inv-meta-right { text-align: right; }
    .inv-barcode-cell {
        grid-column: 1 / span 2;
        text-align: center;
        margin: 2mm 0 1mm;
    }
    .inv-barcode-lines {
        display: flex;
        justify-content: center;
        gap: 2px;
        height: 12mm;
        margin-bottom: 1mm;
    }
    .inv-barcode-lines span {
        display: inline-block;
        background: #000;
        height: 100%;
    }
    .inv-barcode-text { font-size: 7pt; letter-spacing: 1px; }
    .inv-refby-cell {
        grid-column: 1 / span 2;
        font-size: 9pt;
        margin-top: 1mm;
    }
    .inv-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 9pt;
        margin-bottom: 3mm;
    }
    .inv-table th, .inv-table td {
        border: 1px solid #000;
        padding: 2px 4px;
        text-align: left;
    }
    .inv-table th { background: #f0f0f0; font-weight: bold; }
    .col-sl { width: 8mm; text-align: center !important; }
    .col-total { width: 22mm; text-align: right !important; }
    .inv-table td.col-sl { text-align: center; }
    .inv-table td.col-total { text-align: right; }
    .inv-summary-section { overflow: hidden; margin-bottom: 2mm; }
    .inv-summary-box {
        float: right;
        width: 55%;
        border: 1px solid #000;
        padding: 3px 6px;
        font-size: 9pt;
    }
    .inv-srow { display: flex; justify-content: space-between; margin: 1px 0; }
    .bold-srow { font-weight: bold; }
    .inv-inwords { clear: both; font-size: 8.5pt; margin-bottom: 1mm; }
    .inv-note { font-size: 9pt; margin-bottom: 3mm; }
    .inv-sign-row {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
        border-top: 1px solid #000;
        padding-top: 3mm;
        margin-bottom: 3mm;
        font-size: 9pt;
    }
    .inv-prepared-by { display: flex; align-items: center; gap: 8mm; }
    .paid-stamp {
        font-size: 16pt;
        font-weight: bold;
        border: 2px solid #000;
        padding: 1mm 4mm;
        line-height: 1;
    }
    .inv-auth-sign { font-size: 8.5pt; }
    .inv-dept-line { font-size: 8.5pt; font-weight: bold; margin-bottom: 1mm; }
    .inv-software-line { font-size: 7.5pt; color: #555; }

    /* Print button in preview */
    .print-preview-actions {
        max-width: 148mm;
        margin: 0 auto 16px;
        display: flex;
        gap: 12px;
    }
    .btn-print-now {
        flex: 1;
        padding: 12px;
        background: linear-gradient(135deg, var(--accent), var(--accent-d));
        color: #fff;
        border: none;
        border-radius: 8px;
        cursor: pointer;
        font-size: 0.95em;
        font-weight: 700;
        font-family: var(--font-ui);
        box-shadow: 0 3px 12px rgba(45,142,240,0.35);
    }
    .btn-close-preview {
        flex: 1;
        padding: 12px;
        background: #fff;
        color: var(--red);
        border: 2px solid var(--red);
        border-radius: 8px;
        cursor: pointer;
        font-size: 0.95em;
        font-weight: 700;
        font-family: var(--font-ui);
    }
}

/* =========================================
   RESPONSIVE — TABLET & MOBILE
   ========================================= */
@media (max-width: 960px) {
    .main-content { padding: 14px 12px 30px; }
}

@media (max-width: 600px) {
    :root { --header-h: auto; }
    .app-header { height: auto; padding: 10px 16px; }
    .app-header-inner { height: auto; flex-direction: column; align-items: flex-start; gap: 10px; padding: 4px 0; }
    .header-tabs { width: 100%; }
    .header-tab { flex: 1; text-align: center; padding: 8px 10px; font-size: 0.8em; }
    .main-content { padding: 10px 10px 30px; }
    .button-group { grid-template-columns: 1fr; }
    .input-group-row { grid-template-columns: 1fr; }
    .test-table-header { grid-template-columns: 26px 1fr 90px 32px; }
    .test-item-row { grid-template-columns: 26px 1fr 90px 32px; }
    .history-item { flex-wrap: wrap; }
    .app-title-main { font-size: 0.84em; }
    .app-title-sub { display: none; }
}

@media print {
    .print-preview-actions { display: none !important; }
}
