:root {
    --bg: #f3f6fc;
    --surface: #ffffff;
    --surface-soft: #f8faff;
    --text: #111827;
    --muted: #667085;
    --primary: #5b5cf0;
    --primary-dark: #4a4bd6;
    --secondary: #1f2937;
    --border: #e4e7ec;
    --success: #0f9b6f;
    --error: #d92d20;
    --info: #1570ef;
    --warning: #d97706;
    --shadow: 0 10px 24px rgba(16, 24, 40, 0.08);
    --shadow-soft: 0 2px 6px rgba(16, 24, 40, 0.08);
    --sidebar-w: 250px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Inter", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

/* ───── Login / standalone shell ───── */
.app-shell {
    width: 100%;
    max-width: 1150px;
    margin: 0 auto;
    padding: 1rem 0.85rem 1.3rem;
}

/* ───── Sidebar + Main layout ───── */
.app-layout {
    display: flex;
    min-height: 100vh;
}

.app-sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.25rem 0.85rem;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    overflow-y: auto;
    z-index: 50;
}

.app-main {
    flex: 1;
    min-width: 0;
    margin-left: var(--sidebar-w);
    padding: 1.5rem 2rem 2rem;
    max-width: 960px;
}

/* ───── Sidebar internals ───── */
.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding-bottom: 1.25rem;
    margin-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.sidebar-logo {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 1rem;
    flex-shrink: 0;
}

.sidebar-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1.2;
}

.sidebar-subtitle {
    margin: 0;
    font-size: 0.75rem;
    color: var(--muted);
    font-weight: 500;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    margin-top: 0.5rem;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    text-decoration: none;
    color: var(--muted);
    padding: 0.55rem 0.7rem;
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.9rem;
    border: none;
    background: transparent;
    cursor: pointer;
    width: 100%;
    text-align: left;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-link:hover {
    background: var(--bg);
    color: var(--text);
}

.sidebar-link.active {
    background: #eef0ff;
    color: var(--primary);
    font-weight: 600;
}

.sidebar-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 20px;
    height: 20px;
}

.sidebar-logout {
    margin-top: auto;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.sidebar-logout-btn {
    color: var(--error);
    font-family: inherit;
}

.sidebar-logout-btn:hover {
    background: #fef2f2;
    color: var(--error);
}

/* ───── Common ───── */
h1, h2, h3, p, form {
    margin-top: 0;
}

.page-header {
    margin-bottom: 1.25rem;
}

.page-header h1 {
    margin-bottom: 0.15rem;
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.page-header p {
    margin-bottom: 0;
    font-size: 0.85rem;
}

.page-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
}

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1rem 1.15rem;
    margin-bottom: 0.85rem;
    box-shadow: none;
}

.card h2 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 0.6rem;
}

.card h3 {
    font-size: 0.88rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.auth-card {
    margin: 0;
    width: 100%;
}

.clerk-mount {
    display: flex;
    align-items: center;
    justify-content: center;
}

.stack-form {
    display: grid;
    gap: 0.7rem;
}

input[type="email"],
input[type="password"],
input[type="text"],
input[type="file"],
input[type="number"],
select,
textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.875rem;
    padding: 0.6rem 0.75rem;
    background: #fff;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: #a8b1ff;
    box-shadow: 0 0 0 4px rgba(91, 92, 240, 0.15);
}

label {
    font-weight: 500;
    font-size: 0.82rem;
    color: var(--secondary);
}

.btn {
    border: 0;
    border-radius: 8px;
    padding: 0.55rem 0.9rem;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
    text-align: center;
    transition: transform 0.15s ease, filter 0.15s ease, background 0.15s ease;
}

.btn:hover {
    transform: translateY(-1px);
    filter: brightness(0.98);
}

.btn-sm {
    padding: 0.35rem 0.6rem;
    font-size: 0.78rem;
    border-radius: 6px;
}

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

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

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

.btn-ghost {
    background: #fff;
    color: var(--secondary);
    border: 1px solid var(--border);
}

.btn-danger {
    background: #fff;
    color: var(--error);
    border: 1px solid #fecaca;
}

.btn-danger:hover {
    background: #fef2f2;
}

.full-width {
    width: 100%;
}

.muted {
    color: var(--muted);
}

.hint-box {
    margin-top: 1rem;
    background: linear-gradient(180deg, #eff2ff 0%, #f7f8ff 100%);
    border-radius: 12px;
    border: 1px solid #d4d8ff;
    padding: 0.85rem;
    font-size: 0.9rem;
}

/* ───── Status badges ───── */
.badge {
    font-size: 0.75rem;
    border-radius: 999px;
    padding: 0.22rem 0.6rem;
    font-weight: 600;
    white-space: nowrap;
}

.badge-blue {
    background: #eff4ff;
    color: #3538cd;
}

.badge-green {
    background: #ecfdf3;
    color: #067647;
}

.badge-amber {
    background: #fffaeb;
    color: #b54708;
}

.badge-red {
    background: #fef3f2;
    color: #b42318;
}

.badge-gray {
    background: #f2f4f7;
    color: #344054;
}

/* ───── Invoice table ───── */
.invoice-table-wrap {
    overflow-x: auto;
    margin: 0 -1rem -1rem;
    padding: 0 1rem 1rem;
}

.invoice-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
}

.invoice-table th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    font-weight: 600;
    font-size: 0.78rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.invoice-table td {
    padding: 0.7rem 0.75rem;
    border-bottom: 1px solid #f0f2f5;
    vertical-align: middle;
}

.invoice-table tr:last-child td {
    border-bottom: 0;
}

.invoice-table tr:hover td {
    background: var(--surface-soft);
}

.table-actions {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

/* ───── Old card-based invoice list (kept for compat) ───── */
.invoice-list {
    display: grid;
    gap: 0.8rem;
}

.invoice-item {
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 0.82rem;
    display: grid;
    gap: 0.52rem;
    background: var(--surface-soft);
}

.invoice-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.94rem;
}

.text-link {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;
}

.text-link:hover {
    text-decoration: underline;
}

.invoice-actions {
    display: inline-flex;
    gap: 0.65rem;
    align-items: center;
}

.header-actions {
    display: inline-flex;
    gap: 0.55rem;
    flex-wrap: wrap;
}

/* ───── Flash messages ───── */
.flash-stack {
    display: grid;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.flash {
    border-radius: 8px;
    padding: 0.55rem 0.75rem;
    font-size: 0.84rem;
    border: 1px solid transparent;
}

.flash-success {
    border-color: #99f6e4;
    background: #f0fdfa;
    color: var(--success);
}

.flash-error {
    border-color: #fecaca;
    background: #fef2f2;
    color: var(--error);
}

.flash-info {
    border-color: #bae6fd;
    background: #f0f9ff;
    color: var(--info);
}

/* ───── Misc ───── */
.preview {
    border: 1px dashed var(--border);
    border-radius: 12px;
    padding: 0.7rem;
    min-height: 2.8rem;
    word-break: break-word;
    background: #fafbff;
}

.auth-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2rem;
    padding: 6vh 1rem 2rem;
    min-height: 100vh;
}

.brand-panel {
    background: linear-gradient(135deg, #eef2ff 0%, #f9f5ff 100%);
    border: 1px solid #e2e8ff;
    border-radius: 16px;
    padding: 1.5rem 1.75rem;
    text-align: center;
    max-width: 480px;
    width: 100%;
}

.brand-panel h1 {
    margin-bottom: 0.55rem;
    font-size: 1.5rem;
    line-height: 1.2;
}

.brand-panel .muted {
    margin-bottom: 0;
}

.eyebrow {
    display: inline-block;
    margin-bottom: 0.45rem;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    background: #ffffff;
    border: 1px solid #d9ddff;
    color: #3d45bf;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.modern-top-bar {
    background: linear-gradient(145deg, #ffffff 0%, #f8f9ff 100%);
}

.modern-top-bar h1 {
    margin-bottom: 0.25rem;
}

.stats-grid {
    display: grid;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.stat-card {
    margin-bottom: 0;
    padding: 0.9rem 1rem;
}

.stat-card p {
    margin-bottom: 0.25rem;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.stat-card h3 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
}

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.6rem;
}

.section-head h2 {
    font-size: 0.92rem;
    margin-bottom: 0;
}

.upload-card {
    max-width: 680px;
}

/* ───── Dropzone ───── */
.dropzone {
    position: relative;
    border: 2px dashed var(--border);
    border-radius: 14px;
    padding: 2rem 1rem;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.dropzone:hover,
.dropzone.drag-over {
    border-color: var(--primary);
    background: #f5f5ff;
}

.dropzone-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
}

.dropzone-icon {
    color: var(--muted);
    margin-bottom: 0.5rem;
}

.dropzone-content p {
    margin: 0.2rem 0 0;
    font-size: 0.9rem;
}

.dropzone-selected {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--primary);
}

.dropzone-clear {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--muted);
    padding: 0 0.3rem;
}

.dropzone-clear:hover {
    color: var(--error);
}

.camera-btn {
    display: none;
}

@media (pointer: coarse) {
    .camera-btn {
        display: block;
    }
}

/* ───── Fixed total bar ───── */
.sticky-total {
    position: fixed;
    bottom: 0;
    left: var(--sidebar-w);
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 0.6rem 2rem;
    font-size: 0.9rem;
    text-align: right;
    z-index: 40;
}

/* ───── Receipt attach button ───── */
.attach-receipt-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.4rem;
    padding: 0.5rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--secondary);
    transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
    min-height: 42px;
}

.attach-receipt-btn:hover {
    background: #eef0ff;
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: 0 2px 6px rgba(91, 92, 240, 0.1);
}

.attach-input {
    display: none;
}

.receipt-attached {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.3rem;
    padding: 0.3rem 0.6rem;
    background: #ecfdf3;
    border: 1px solid #a7f3d0;
    border-radius: 6px;
    font-size: 0.78rem;
    color: var(--success);
    font-weight: 500;
}

.receipt-remove-btn {
    background: none;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: var(--muted);
    padding: 0 0.2rem;
    line-height: 1;
}

.receipt-remove-btn:hover {
    color: var(--error);
}

.receipt-pending {
    margin-top: 0.3rem;
    padding: 0.3rem 0.6rem;
    background: #eff4ff;
    border: 1px solid #c7d2fe;
    border-radius: 6px;
    font-size: 0.78rem;
    color: var(--primary);
    font-weight: 500;
}

/* ───── Broker dropdown ───── */
.broker-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    max-height: 220px;
    overflow-y: auto;
    z-index: 60;
    margin-top: 4px;
}

.broker-option {
    padding: 0.6rem 0.75rem;
    cursor: pointer;
    font-size: 0.88rem;
    border-bottom: 1px solid #f0f2f5;
}

.broker-option:last-child {
    border-bottom: 0;
}

.broker-option:hover {
    background: var(--bg);
}

.broker-selected {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    padding: 0.6rem 0;
}

.broker-pick-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    padding: 0.65rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 0.4rem;
    cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease;
}

.broker-pick-row:hover {
    border-color: var(--primary);
    background: #f8f8ff;
}

.form-grid {
    display: grid;
    gap: 0.7rem;
}

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.8rem;
    padding: 0.3rem 0;
    border-bottom: 1px solid #f0f2f5;
    font-size: 0.85rem;
}

.info-row:last-child {
    border-bottom: 0;
}

.review-grid {
    display: grid;
    gap: 0.9rem;
}

.extracted-grid {
    display: grid;
    gap: 0.9rem;
}

.extracted-grid .card {
    margin-bottom: 0;
}

/* ───── BOL thumbnail + lightbox ───── */
.bol-thumb-wrap {
    display: inline-flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.3rem;
    cursor: pointer;
}

.bol-thumb {
    max-width: 180px;
    max-height: 140px;
    border-radius: 8px;
    border: 1px solid var(--border);
    object-fit: cover;
    transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.bol-thumb:hover {
    box-shadow: 0 4px 14px rgba(0,0,0,0.12);
    transform: scale(1.03);
}

.bol-thumb-hint {
    font-size: 0.75rem;
}

.lightbox {
    position: fixed;
    inset: 0;
    z-index: 10000;
}

.lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
}

.lightbox-body {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 2rem;
}

.lightbox-body img {
    max-width: 90vw;
    max-height: 90vh;
    border-radius: 10px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    object-fit: contain;
}

.lightbox-close {
    position: absolute;
    top: 1rem;
    right: 1.5rem;
    background: rgba(255,255,255,0.9);
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    font-size: 1.3rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.lightbox-close:hover {
    background: #fff;
}

.total-row {
    margin-top: 0.4rem;
    padding-top: 0.5rem;
    border-top: 2px solid #dfe3f8;
    font-size: 0.92rem;
    font-weight: 600;
}

.json-output {
    margin: 0;
    padding: 0.9rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: #101828;
    color: #d1faff;
    font-size: 0.82rem;
    line-height: 1.5;
    overflow: auto;
    max-height: 70vh;
}

/* ───── Step indicator ───── */
.stepper {
    display: flex;
    align-items: center;
    gap: 0;
    margin-bottom: 1.5rem;
    padding: 0.9rem 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow-soft);
    overflow-x: auto;
}

.step {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--muted);
    text-decoration: none;
}

a.step {
    cursor: pointer;
}

a.step:hover {
    color: var(--primary);
}

a.step:hover .step-num {
    border-color: var(--primary);
}

.step-num {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    font-weight: 700;
    border: 2px solid var(--border);
    background: #fff;
    flex-shrink: 0;
}

.step.active .step-num {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.step.active {
    color: var(--text);
    font-weight: 600;
}

.step.done .step-num {
    background: #ecfdf3;
    border-color: var(--success);
    color: var(--success);
}

.step.done {
    color: var(--success);
}

.step-line {
    flex: 1;
    height: 2px;
    background: var(--border);
    min-width: 1.5rem;
    margin: 0 0.4rem;
}

.step.done + .step-line,
.step-line.done {
    background: var(--success);
}

/* ───── Loading overlay ───── */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(4px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    z-index: 9999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

.loading-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-text {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--secondary);
}

/* ───── Responsive ───── */
@media (min-width: 768px) {
    .app-shell {
        padding: 1.35rem;
    }

    .auth-wrap {
        flex-direction: row;
        justify-content: center;
        align-items: center;
        gap: 3rem;
        padding: 0 2rem;
        min-height: 100vh;
    }

    .brand-panel {
        text-align: left;
        max-width: 400px;
        padding: 2rem;
    }

    .stats-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .two-col {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .review-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .extracted-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 959px) {
    .app-sidebar {
        position: fixed;
        left: -260px;
        transition: left 0.25s ease;
    }

    .app-sidebar.open {
        left: 0;
    }

    .app-main {
        margin-left: 0;
        padding: 1rem 1rem;
        max-width: 100%;
    }
}

@media (max-width: 767px) {
    .page-header-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .header-actions {
        width: 100%;
    }

    .sticky-total {
        left: 0;
    }

    .stepper {
        padding: 0.5rem 0.6rem;
        gap: 0;
    }

    .step span:not(.step-num) {
        display: none;
    }

    .invoice-table {
        font-size: 0.78rem;
    }

    .invoice-table th,
    .invoice-table td {
        padding: 0.45rem 0.5rem;
    }

    .card {
        border-radius: 10px;
        padding: 0.85rem;
    }

    .btn {
        padding: 0.5rem 0.75rem;
    }
}
