:root {
    --sidebar-bg: #1f2937;
    --sidebar-bg-dark: #111827;
    --sidebar-fg: #cbd5e1;
    --sidebar-fg-muted: #94a3b8;
    --sidebar-active-bg: #374151;
    --sidebar-active-fg: #ffffff;
    --sidebar-accent: #6366f1;
    --content-bg: #f3f4f6;
    --topbar-bg: #ffffff;
    --topbar-border: #e5e7eb;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.04);
    --sidebar-width: 260px;
}

html, body {
    height: 100%;
}

body {
    background-color: var(--content-bg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 0;
}

/* ===== App shell ===== */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.app-sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: linear-gradient(180deg, var(--sidebar-bg-dark) 0%, var(--sidebar-bg) 80px);
    color: var(--sidebar-fg);
    padding: 0 0 1rem 0;
    overflow-y: auto;
    transition: margin-left 0.25s ease;
}

.app-shell.sidebar-collapsed .app-sidebar {
    margin-left: calc(-1 * var(--sidebar-width));
}

.app-main {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.app-topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1.25rem;
    background: var(--topbar-bg);
    border-bottom: 1px solid var(--topbar-border);
    box-shadow: var(--card-shadow);
}

.topbar-title {
    font-weight: 600;
    color: #374151;
}

.topbar-spacer { flex-grow: 1; }

.app-content {
    flex-grow: 1;
    padding: 1.5rem;
}

.app-footer {
    padding: 0.75rem 1.5rem;
    border-top: 1px solid var(--topbar-border);
    background: #fff;
    text-align: center;
}

/* ===== Sidebar branding & user box ===== */
.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1.25rem 1.25rem;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.sidebar-brand i {
    background: var(--sidebar-accent);
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
}

.brand-title { font-weight: 700; line-height: 1.1; }
.brand-subtitle { font-size: 0.7rem; color: var(--sidebar-fg-muted); }

.sidebar-userbox {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.25rem;
    margin: 1rem;
    background: rgba(255, 255, 255, 0.04);
    border-radius: 8px;
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--sidebar-accent);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
}

.user-name { color: #fff; font-weight: 600; line-height: 1.1; }
.user-email { color: var(--sidebar-fg-muted); }

.sidebar-section-label {
    font-size: 0.7rem;
    letter-spacing: 0.05em;
    color: var(--sidebar-fg-muted);
    padding: 0.75rem 1.5rem 0.25rem;
    text-transform: uppercase;
}

/* ===== Sidebar nav ===== */
.sidebar-nav {
    list-style: none;
    margin: 0;
    padding: 0;
}

.sidebar-nav li a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 1.5rem;
    color: var(--sidebar-fg);
    text-decoration: none;
    font-size: 0.9rem;
    transition: background-color 0.15s ease;
}

.sidebar-nav li a:hover {
    background: rgba(255, 255, 255, 0.05);
    color: #fff;
}

.sidebar-nav li a.active {
    background: var(--sidebar-active-bg);
    color: var(--sidebar-active-fg);
    border-left: 3px solid var(--sidebar-accent);
    padding-left: calc(1.5rem - 3px);
}

.sidebar-nav li a i {
    width: 18px;
    text-align: center;
    color: var(--sidebar-fg-muted);
}

.sidebar-disabled a {
    cursor: not-allowed !important;
    opacity: 0.55;
}

.sidebar-disabled a:hover {
    background: transparent !important;
    color: var(--sidebar-fg) !important;
}

/* ===== Login page ===== */
.login-body-bg {
    min-height: 100vh;
    background: linear-gradient(135deg, #4338ca 0%, #6366f1 50%, #8b5cf6 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-wrapper {
    width: 100%;
    max-width: 420px;
    padding: 1rem;
}

.login-card {
    background: #fff;
    border-radius: 14px;
    overflow: hidden;
}

.login-header {
    padding: 2rem 2rem 1rem;
}

.login-logo {
    width: 60px;
    height: 60px;
    margin: 0 auto 1rem;
    border-radius: 14px;
    background: rgba(99, 102, 241, 0.12);
    color: var(--sidebar-accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.login-body {
    padding: 0 2rem 1.5rem;
}

.login-footer {
    padding: 1rem 2rem 1.5rem;
    border-top: 1px solid #f1f5f9;
}

.login-footer a {
    color: var(--sidebar-accent);
    text-decoration: none;
}

/* ===== Page header ===== */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.page-header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    color: #111827;
}

.page-header .subtitle {
    font-size: 0.85rem;
    color: #6b7280;
}

/* ===== Generic data card ===== */
.data-card {
    background: #fff;
    border-radius: 10px;
    padding: 1.25rem;
    box-shadow: var(--card-shadow);
}

/* ===== DataTables tweaks ===== */
table.dataTable thead th {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #4b5563;
    background: #f9fafb;
    border-bottom: 2px solid #e5e7eb;
}

table.dataTable tbody td { font-size: 0.875rem; }

.action-btn {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
    line-height: 1;
}

.status-pill {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.status-pill.active   { background: #dcfce7; color: #166534; }
.status-pill.inactive { background: #fee2e2; color: #991b1b; }

/* ===== Forms ===== */
.form-section-title {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #6b7280;
    margin: 1.5rem 0 0.5rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid #e5e7eb;
}

/* ===== Generic content cards ===== */
.kpi-card {
    background: #fff;
    border-radius: 10px;
    padding: 1.25rem;
    box-shadow: var(--card-shadow);
}

.kpi-label { font-size: 0.75rem; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; }
.kpi-value { font-size: 1.75rem; font-weight: 700; }

/* ===== Orders list — Excel-style grouped headers (Order / Shipment / Commission) ===== */
.orders-grouped-table .orders-group-header th {
    font-size: 0.7rem;
    letter-spacing: 0.08em;
    font-weight: 700;
    padding: 0.4rem 0.5rem;
    border-bottom: 2px solid #d1d5db;
    text-transform: uppercase;
}

.orders-grouped-table .grp-order        { background: #e5e7eb; color: #1f2937; border-right: 2px solid #ffffff; }
.orders-grouped-table .grp-shipment     { background: #cfe2ff; color: #084298; border-right: 2px solid #ffffff; }
.orders-grouped-table .grp-commission   { background: #d1e7dd; color: #0a3622; }

/* Subtle tinting on the body cells so the grouping carries down into the data rows */
.orders-grouped-table tbody td.grp-order-cell,
.orders-grouped-table thead th.grp-order-cell        { box-shadow: inset 3px 0 0 transparent; }
.orders-grouped-table thead th.grp-shipment-cell     { background: #f0f6ff; }
.orders-grouped-table thead th.grp-commission-cell   { background: #effaf3; }

/* Vertical dividers between the three groups in the body — applied to header cells; body cells inherit alignment */
.orders-grouped-table thead th.grp-order-cell:last-of-type,
.orders-grouped-table thead th.grp-shipment-cell:last-of-type {
    border-right: 2px solid #d1d5db;
}

/* Body row group-start cells get a left border so the vertical lane is visible through every row */
.orders-grouped-table tbody td.grp-shipment-start,
.orders-grouped-table tbody td.grp-commission-start {
    border-left: 2px solid #d1d5db;
}

/* Archived rows — distinct medium-grey background + muted text + diagonal pattern stripe so
   they're impossible to confuse with live "pending shipment" (red) rows. */
.orders-grouped-table tbody tr.order-row-archived,
.orders-grouped-table tbody tr.order-row-archived > td {
    background-color: #d6d8db !important;
    color: #6c757d;
    background-image: repeating-linear-gradient(
        45deg,
        transparent,
        transparent 8px,
        rgba(0, 0, 0, 0.025) 8px,
        rgba(0, 0, 0, 0.025) 16px
    );
}

.orders-grouped-table tbody tr.order-row-archived td a,
.orders-grouped-table tbody tr.order-row-archived td a:visited {
    color: #495057;
    text-decoration: line-through;
}

.orders-grouped-table tbody tr.order-row-archived td a:hover {
    text-decoration: line-through underline;
}

/* Column-header row: force center+middle so labels that wrap (e.g. "Shipment Date", "Short / Excess")
   line up visually with single-line labels (e.g. "Vendor", "Amount") instead of stacking top/bottom. */
.orders-grouped-table thead tr:not(.orders-group-header) th {
    text-align: center !important;
    vertical-align: middle;
    white-space: normal;
    line-height: 1.2;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

/* Group-header row already has text-center via Bootstrap utility; just ensure vertical centering */
.orders-grouped-table thead tr.orders-group-header th {
    vertical-align: middle;
}

/* ===== Collapsible filter panel (Orders list) ===== */
.filter-panel {
    padding: 0;
    overflow: hidden;
}

.filter-panel-header {
    display: flex;
    align-items: center;
    padding: 0.65rem 1rem;
    color: #1f2937;
    text-decoration: none;
    cursor: pointer;
    user-select: none;
    background: linear-gradient(180deg, #f9fafb 0%, #f3f4f6 100%);
    transition: background 0.15s ease;
}

    .filter-panel-header:hover {
        background: linear-gradient(180deg, #eef2ff 0%, #e0e7ff 100%);
        color: #1e293b;
        text-decoration: none;
    }

    .filter-panel-header .chevron {
        transition: transform 0.2s ease;
        color: #6b7280;
    }

    .filter-panel-header.collapsed .chevron {
        transform: rotate(-90deg);
    }

.filter-panel .filter-form {
    padding: 0 1rem 1rem 1rem;
}

/* ===== QC order picker — clickable card grid ===== */
.qc-pick-card.card-link {
    color: inherit;
}

    .qc-pick-card.card-link .card {
        transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
        border: 1px solid #e5e7eb;
    }

    .qc-pick-card.card-link:hover .card,
    .qc-pick-card.card-link:focus-visible .card {
        transform: translateY(-2px);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08), 0 2px 4px rgba(0, 0, 0, 0.04);
        border-color: #6366f1;
    }

    .qc-pick-card.card-link:active .card {
        transform: translateY(0);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
    }

/* ===== Production Tracking — detail sub-row (cumulative stage qtys) ===== */
.production-detail-row > td {
    background: #f9fafb;
    border-top: 0;
    padding-top: 0.15rem;
    padding-bottom: 0.4rem;
    border-bottom: 2px solid #e5e7eb;
}

.production-detail-row .small {
    font-size: 0.78rem;
}

/* ===== Tree view (Articles page) ===== */
.article-tree {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.tree-category {
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #ffffff;
    overflow: hidden;
}

.tree-category-header {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.7rem 1rem;
    background: linear-gradient(180deg, #f9fafb 0%, #f3f4f6 100%);
    color: #1f2937;
    text-decoration: none;
    font-weight: 600;
    transition: background 0.15s ease;
    cursor: pointer;
    user-select: none;
}

    .tree-category-header:hover {
        background: linear-gradient(180deg, #eef2ff 0%, #e0e7ff 100%);
        color: #1e293b;
        text-decoration: none;
    }

    .tree-category-header .chevron {
        color: #6b7280;
        transition: transform 0.2s ease;
        width: 12px;
        text-align: center;
    }

    .tree-category-header.collapsed .chevron {
        transform: rotate(-90deg);
    }

    .tree-category-header .folder-icon {
        color: #f59e0b;
    }

    .tree-category-header .tree-category-name {
        flex-grow: 0;
    }

    .tree-category-header .count-badge {
        font-weight: 500;
        font-size: 0.72rem;
        padding: 0.25em 0.6em;
    }

.tree-children {
    padding: 0.25rem 0;
    background: #ffffff;
}

.tree-leaf {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.55rem 1rem 0.55rem 2.5rem;
    border-top: 1px solid #f3f4f6;
    transition: background 0.12s ease;
}

    .tree-leaf:first-child {
        border-top: 0;
    }

    .tree-leaf:hover {
        background: #f9fafb;
    }

    .tree-leaf.leaf-archived {
        opacity: 0.6;
    }

    .tree-leaf .leaf-main {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        flex-grow: 1;
        min-width: 0;
    }

    .tree-leaf .leaf-icon {
        color: #9ca3af;
        width: 14px;
        text-align: center;
    }

    .tree-leaf .leaf-name {
        color: #111827;
        font-weight: 500;
    }

    .tree-leaf .leaf-actions {
        display: flex;
        gap: 0.25rem;
        opacity: 0;
        transition: opacity 0.15s ease;
    }

    .tree-leaf:hover .leaf-actions,
    .tree-leaf:focus-within .leaf-actions {
        opacity: 1;
    }

/* Always show actions on touch devices where :hover is unreliable */
@@media (hover: none) {
    .tree-leaf .leaf-actions {
        opacity: 1;
    }
}

/* ===== Responsive ===== */
@@media (max-width: 768px) {
    .app-sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 1040;
    }

    .app-shell:not(.sidebar-open) .app-sidebar {
        margin-left: calc(-1 * var(--sidebar-width));
    }

    .tree-leaf {
        padding-left: 1.75rem;
        flex-wrap: wrap;
    }
}
