/* ==========================================================================
   Inventory & Billing — admin theme on top of Bootstrap 5.3
   Light and dark themes are driven by <html data-bs-theme="light|dark">.
   Custom colours are defined once as tokens below; components use tokens only.
   ========================================================================== */

/* ---------- Tokens ---------- */
:root,
[data-bs-theme="light"] {
    --app-font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

    --app-primary: #4f46e5;
    --app-primary-hover: #4338ca;
    --app-primary-rgb: 79, 70, 229;
    --app-primary-soft: #eef2ff;
    --app-primary-text: #4338ca;

    --app-page: #f5f7fb;
    --app-surface: #ffffff;
    --app-surface-2: #f8fafc;
    --app-border: #e5e7eb;
    --app-text: #1e293b;
    --app-muted: #64748b;
    --app-shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 1px 3px rgba(15, 23, 42, .06);
    --app-shadow-lg: 0 10px 30px rgba(15, 23, 42, .10);

    --app-sidebar-bg: #0f172a;
    --app-sidebar-text: #cbd5e1;
    --app-sidebar-muted: #64748b;
    --app-sidebar-hover: rgba(255, 255, 255, .06);
    --app-sidebar-active: rgba(99, 102, 241, .18);
    --app-sidebar-active-text: #ffffff;
    --app-sidebar-border: rgba(255, 255, 255, .06);

    /* Chart roles (validated with the dataviz palette validator on #ffffff) */
    --chart-series-1: #2a78d6;
    --chart-series-2: #eb6834;
    --chart-grid: #eef0f3;
    --chart-axis: #94a3b8;
    --chart-text: #64748b;

    /* Bootstrap variables */
    --bs-body-font-family: var(--app-font);
    --bs-body-font-size: .875rem;
    --bs-body-bg: var(--app-page);
    --bs-body-color: var(--app-text);
    --bs-secondary-color: var(--app-muted);
    --bs-border-color: var(--app-border);
    --bs-tertiary-bg: var(--app-surface-2);
    --bs-primary: var(--app-primary);
    --bs-primary-rgb: var(--app-primary-rgb);
    --bs-primary-bg-subtle: var(--app-primary-soft);
    --bs-primary-text-emphasis: var(--app-primary-text);
    --bs-primary-border-subtle: #c7d2fe;
    --bs-link-color: var(--app-primary);
    --bs-link-color-rgb: var(--app-primary-rgb);
    --bs-link-hover-color: var(--app-primary-hover);
    --bs-border-radius: .5rem;
    --bs-border-radius-sm: .375rem;
    --bs-border-radius-lg: .75rem;
    --bs-focus-ring-color: rgba(var(--app-primary-rgb), .25);
}

[data-bs-theme="dark"] {
    --app-primary: #6366f1;
    --app-primary-hover: #818cf8;
    --app-primary-rgb: 99, 102, 241;
    --app-primary-soft: rgba(99, 102, 241, .16);
    --app-primary-text: #a5b4fc;

    --app-page: #0b1120;
    --app-surface: #111827;
    --app-surface-2: #172033;
    --app-border: #1f2a3d;
    --app-text: #e2e8f0;
    --app-muted: #94a3b8;
    --app-shadow: 0 1px 2px rgba(0, 0, 0, .3);
    --app-shadow-lg: 0 10px 30px rgba(0, 0, 0, .45);

    --app-sidebar-bg: #0a0f1c;
    --app-sidebar-border: rgba(255, 255, 255, .05);

    /* Dark steps of the same chart hues (validated on #111827) */
    --chart-series-1: #3987e5;
    --chart-series-2: #d95926;
    --chart-grid: #1f2a3d;
    --chart-axis: #475569;
    --chart-text: #94a3b8;

    --bs-body-bg: var(--app-page);
    --bs-body-color: var(--app-text);
    --bs-secondary-color: var(--app-muted);
    --bs-border-color: var(--app-border);
    --bs-tertiary-bg: var(--app-surface-2);
    --bs-secondary-bg: var(--app-surface-2);
    --bs-emphasis-color: #f8fafc;
    --bs-primary-bg-subtle: var(--app-primary-soft);
    --bs-primary-text-emphasis: var(--app-primary-text);
    --bs-primary-border-subtle: rgba(99, 102, 241, .35);
    --bs-link-color: #818cf8;
    --bs-link-color-rgb: 129, 140, 248;
    --bs-link-hover-color: #a5b4fc;
    color-scheme: dark;
}

/* ---------- Base ---------- */
body {
    font-family: var(--app-font);
    background: var(--app-page);
    color: var(--app-text);
    -webkit-font-smoothing: antialiased;
    letter-spacing: -.005em;
}

h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 { font-weight: 600; letter-spacing: -.01em; }
a { text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 2px; }
.btn:hover, .dropdown-item:hover, .app-menu-link:hover, .list-group-item:hover, .app-brand:hover, a:hover > .card { text-decoration: none; }
code { color: var(--app-primary-text); background: var(--app-primary-soft); padding: .1rem .35rem; border-radius: .25rem; font-size: .8em; }
.text-muted { color: var(--app-muted) !important; }
::selection { background: rgba(var(--app-primary-rgb), .2); }

/* ---------- Layout ---------- */
.app-wrapper { display: flex; min-height: 100vh; }

.app-sidebar {
    width: 256px;
    background: var(--app-sidebar-bg);
    color: var(--app-sidebar-text);
    position: fixed;
    inset: 0 auto 0 0;
    display: flex;
    flex-direction: column;
    z-index: 1040;
    transition: width .2s ease, transform .2s ease;
    border-right: 1px solid var(--app-sidebar-border);
}

.app-brand {
    display: flex;
    align-items: center;
    gap: .7rem;
    height: 64px;
    padding: 0 1.25rem;
    color: #fff;
    font-weight: 700;
    font-size: 1.05rem;
    white-space: nowrap;
    flex-shrink: 0;
}
.app-brand:hover { color: #fff; }
.app-brand-logo {
    width: 34px; height: 34px; border-radius: .6rem; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; font-size: .95rem;
    box-shadow: 0 4px 12px rgba(99, 102, 241, .35);
}
.app-brand small { display: block; font-size: .7rem; font-weight: 500; color: var(--app-sidebar-muted); letter-spacing: .02em; }

.app-menu { padding: .5rem .75rem 1rem; overflow-y: auto; flex: 1; scrollbar-width: thin; scrollbar-color: #334155 transparent; }
.app-menu-heading {
    font-size: .68rem; font-weight: 600; text-transform: uppercase; letter-spacing: .08em;
    color: var(--app-sidebar-muted); padding: 1.1rem .75rem .4rem; white-space: nowrap;
}
.app-menu-link {
    display: flex; align-items: center; gap: .75rem;
    padding: .55rem .75rem; margin-bottom: 2px; border-radius: .5rem;
    color: var(--app-sidebar-text); font-weight: 500; white-space: nowrap;
    transition: background .15s ease, color .15s ease;
}
.app-menu-link i { width: 1.25rem; text-align: center; opacity: .8; font-size: .95rem; }
.app-menu-link:hover { background: var(--app-sidebar-hover); color: #fff; }
.app-menu-link.active { background: var(--app-sidebar-active); color: var(--app-sidebar-active-text); box-shadow: inset 3px 0 0 #818cf8; }
.app-menu-link.active i { opacity: 1; color: #a5b4fc; }
.app-menu-link.disabled { opacity: .4; cursor: not-allowed; }
.app-menu-link.disabled:hover { background: none; color: var(--app-sidebar-text); }
.app-menu-link .badge { background: rgba(255, 255, 255, .1); color: var(--app-sidebar-text); font-weight: 500; font-size: .62rem; }

.app-sidebar-footer { padding: .75rem; border-top: 1px solid var(--app-sidebar-border); flex-shrink: 0; }
.app-sidebar-user { display: flex; align-items: center; gap: .65rem; padding: .4rem .5rem; border-radius: .5rem; color: var(--app-sidebar-text); white-space: nowrap; overflow: hidden; }
.app-sidebar-user .name { color: #fff; font-weight: 600; font-size: .82rem; line-height: 1.2; }
.app-sidebar-user .role { color: var(--app-sidebar-muted); font-size: .72rem; }

.app-main { flex: 1; margin-left: 256px; display: flex; flex-direction: column; min-width: 0; transition: margin-left .2s ease; }

.app-topbar {
    height: 64px; position: sticky; top: 0; z-index: 1030;
    display: flex; align-items: center; gap: .75rem; padding: 0 1.5rem;
    background: color-mix(in srgb, var(--app-surface) 85%, transparent);
    backdrop-filter: saturate(180%) blur(8px);
    border-bottom: 1px solid var(--app-border);
}
.app-topbar .icon-btn {
    width: 38px; height: 38px; border-radius: .5rem; border: 1px solid transparent;
    display: inline-flex; align-items: center; justify-content: center;
    color: var(--app-muted); background: transparent; transition: background .15s ease, color .15s ease;
}
.app-topbar .icon-btn:hover { background: var(--app-surface-2); color: var(--app-text); border-color: var(--app-border); }

.avatar {
    width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff; font-weight: 600; font-size: .85rem;
}
.avatar-sm { width: 30px; height: 30px; font-size: .78rem; }

.app-content { flex: 1; padding: 1.5rem; }
.app-footer { padding: 1rem 1.5rem; color: var(--app-muted); font-size: .78rem; border-top: 1px solid var(--app-border); }

/* Page header */
.page-header { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: .75rem; margin-bottom: 1.25rem; }
.page-title { font-size: 1.35rem; font-weight: 700; margin: 0; letter-spacing: -.02em; }
.breadcrumb { --bs-breadcrumb-divider-color: var(--app-muted); --bs-breadcrumb-item-active-color: var(--app-muted); font-size: .78rem; margin-bottom: .2rem; }
.breadcrumb a { color: var(--app-muted); }
.breadcrumb a:hover { color: var(--app-primary); }

/* Collapsed sidebar (desktop) */
@media (min-width: 992px) {
    .sidebar-collapsed .app-sidebar { width: 76px; }
    .sidebar-collapsed .app-main { margin-left: 76px; }
    .sidebar-collapsed .app-brand-text,
    .sidebar-collapsed .app-menu-heading,
    .sidebar-collapsed .app-menu-link span,
    .sidebar-collapsed .app-menu-link .badge,
    .sidebar-collapsed .app-sidebar-user > div { display: none; }
    .sidebar-collapsed .app-menu-link { justify-content: center; padding: .65rem; }
    .sidebar-collapsed .app-brand { justify-content: center; padding: 0; }
    .sidebar-collapsed .app-sidebar-user { justify-content: center; }
}

/* Mobile: off-canvas sidebar */
.app-sidebar-backdrop { display: none; }
@media (max-width: 991.98px) {
    .app-sidebar { transform: translateX(-100%); }
    .app-main { margin-left: 0; }
    .app-content { padding: 1rem; }
    .app-topbar { padding: 0 1rem; }
    .sidebar-open .app-sidebar { transform: none; box-shadow: var(--app-shadow-lg); }
    .sidebar-open .app-sidebar-backdrop { display: block; position: fixed; inset: 0; background: rgba(2, 6, 23, .5); z-index: 1035; }
}

/* ---------- Cards ---------- */
.card {
    --bs-card-bg: var(--app-surface);
    --bs-card-border-color: var(--app-border);
    --bs-card-border-radius: .75rem;
    --bs-card-inner-border-radius: calc(.75rem - 1px);
    --bs-card-cap-bg: transparent;
    --bs-card-cap-padding-y: .9rem;
    --bs-card-cap-padding-x: 1.25rem;
    --bs-card-spacer-x: 1.25rem;
    box-shadow: var(--app-shadow);
}
.card-header { font-weight: 600; border-bottom-color: var(--app-border); }
.card-footer { background: transparent; border-top-color: var(--app-border); }
.card-header .small, .card-header .fw-normal { font-weight: 400; }

.stat-card .card-body { padding: 1.1rem 1.25rem; }
.stat-icon {
    width: 46px; height: 46px; border-radius: .65rem; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; font-size: 1.15rem;
}
.stat-value { font-size: 1.5rem; font-weight: 700; line-height: 1.15; letter-spacing: -.02em; color: var(--app-text); }
.stat-card .text-muted.small { font-weight: 500; }
.stat-delta { font-size: .75rem; font-weight: 600; }

a > .card, .report-card { transition: transform .12s ease, box-shadow .12s ease, border-color .12s ease; }
a:hover > .card, .report-card:hover { transform: translateY(-2px); box-shadow: var(--app-shadow-lg); border-color: rgba(var(--app-primary-rgb), .35); }

/* ---------- Buttons ---------- */
.btn { --bs-btn-font-weight: 500; --bs-btn-font-size: .875rem; --bs-btn-padding-x: .9rem; --bs-btn-border-radius: .5rem; }
.btn-sm { --bs-btn-padding-x: .6rem; --bs-btn-font-size: .8rem; --bs-btn-border-radius: .4rem; }
.btn-lg { --bs-btn-font-size: 1rem; --bs-btn-border-radius: .6rem; }
.btn-primary {
    --bs-btn-bg: var(--app-primary); --bs-btn-border-color: var(--app-primary);
    --bs-btn-hover-bg: var(--app-primary-hover); --bs-btn-hover-border-color: var(--app-primary-hover);
    --bs-btn-active-bg: var(--app-primary-hover); --bs-btn-active-border-color: var(--app-primary-hover);
    --bs-btn-disabled-bg: var(--app-primary); --bs-btn-disabled-border-color: var(--app-primary);
    box-shadow: 0 1px 2px rgba(var(--app-primary-rgb), .25);
}
.btn-outline-primary {
    --bs-btn-color: var(--app-primary); --bs-btn-border-color: rgba(var(--app-primary-rgb), .45);
    --bs-btn-hover-bg: var(--app-primary); --bs-btn-hover-border-color: var(--app-primary);
    --bs-btn-active-bg: var(--app-primary); --bs-btn-active-border-color: var(--app-primary);
}
[data-bs-theme="dark"] .btn-outline-primary { --bs-btn-color: #a5b4fc; }
.btn-light {
    --bs-btn-bg: var(--app-surface); --bs-btn-color: var(--app-text); --bs-btn-border-color: var(--app-border);
    --bs-btn-hover-bg: var(--app-surface-2); --bs-btn-hover-color: var(--app-text); --bs-btn-hover-border-color: var(--app-border);
    --bs-btn-active-bg: var(--app-surface-2); --bs-btn-active-color: var(--app-text); --bs-btn-active-border-color: var(--app-border);
}
.btn-outline-secondary {
    --bs-btn-color: var(--app-muted); --bs-btn-border-color: var(--app-border);
    --bs-btn-hover-bg: var(--app-surface-2); --bs-btn-hover-color: var(--app-text); --bs-btn-hover-border-color: var(--app-border);
    --bs-btn-active-bg: var(--app-surface-2); --bs-btn-active-color: var(--app-text); --bs-btn-active-border-color: var(--app-border);
}
.btn-check:checked + .btn-outline-secondary { background: var(--app-primary); border-color: var(--app-primary); color: #fff; }

/* ---------- Forms ---------- */
.form-label { font-weight: 500; font-size: .82rem; margin-bottom: .35rem; }
.form-control, .form-select { border-color: var(--app-border); background-color: var(--app-surface); font-size: .875rem; }
.form-control:focus, .form-select:focus {
    border-color: rgba(var(--app-primary-rgb), .6);
    box-shadow: 0 0 0 .2rem rgba(var(--app-primary-rgb), .15);
}
.form-check-input:checked { background-color: var(--app-primary); border-color: var(--app-primary); }
.form-check-input:focus { border-color: rgba(var(--app-primary-rgb), .6); box-shadow: 0 0 0 .2rem rgba(var(--app-primary-rgb), .15); }
.input-group-text { background: var(--app-surface-2); border-color: var(--app-border); color: var(--app-muted); }
.form-text { color: var(--app-muted); }

/* ---------- Tables ---------- */
.table {
    --bs-table-bg: transparent;
    --bs-table-color: var(--app-text);
    --bs-table-border-color: var(--app-border);
    --bs-table-hover-bg: rgba(var(--app-primary-rgb), .04);
    --bs-table-hover-color: var(--app-text);
    --bs-table-striped-bg: var(--app-surface-2);
    font-size: .85rem;
}
.table > :not(caption) > * > * { padding: .7rem .75rem; vertical-align: middle; }
.table-sm > :not(caption) > * > * { padding: .5rem .6rem; }
.table > thead th {
    font-size: .7rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em;
    color: var(--app-muted); background: var(--app-surface-2); border-bottom-width: 1px; white-space: nowrap;
}
.table-light { --bs-table-bg: var(--app-surface-2); --bs-table-color: var(--app-text); --bs-table-border-color: var(--app-border); }
.table tfoot td { font-weight: 600; background: var(--app-surface-2); }
.table a { font-weight: 500; }

/* DataTables 2 */
div.dt-container .dt-search input, div.dt-container .dt-length select {
    border: 1px solid var(--app-border); border-radius: .5rem; background: var(--app-surface); color: var(--app-text); padding: .3rem .6rem;
}
div.dt-container .dt-search input { min-width: 220px; margin-left: .4rem; }
div.dt-container .dt-info { color: var(--app-muted); font-size: .8rem; }
div.dt-container .pagination {
    --bs-pagination-active-bg: var(--app-primary); --bs-pagination-active-border-color: var(--app-primary);
    --bs-pagination-color: var(--app-text); --bs-pagination-bg: var(--app-surface); --bs-pagination-border-color: var(--app-border);
    --bs-pagination-hover-bg: var(--app-surface-2); --bs-pagination-hover-color: var(--app-text); --bs-pagination-font-size: .8rem;
    --bs-pagination-disabled-bg: var(--app-surface); --bs-pagination-focus-box-shadow: 0 0 0 .2rem rgba(var(--app-primary-rgb), .15);
}
div.dt-container > .row { --bs-gutter-y: .5rem; }

/* ---------- Badges, alerts, misc ---------- */
.badge { font-weight: 600; letter-spacing: .01em; padding: .35em .65em; border-radius: 999px; }
.alert { border-radius: .65rem; font-size: .875rem; }
.dropdown-menu {
    --bs-dropdown-bg: var(--app-surface); --bs-dropdown-border-color: var(--app-border);
    --bs-dropdown-link-active-bg: var(--app-primary); --bs-dropdown-font-size: .85rem;
    box-shadow: var(--app-shadow-lg); border-radius: .65rem; padding: .4rem;
}
.dropdown-item { border-radius: .4rem; padding: .45rem .7rem; }
.list-group { --bs-list-group-active-bg: var(--app-primary); --bs-list-group-active-border-color: var(--app-primary); --bs-list-group-bg: var(--app-surface); }
.progress { --bs-progress-bar-bg: var(--app-primary); --bs-progress-bg: var(--app-surface-2); }
.modal { --bs-modal-bg: var(--app-surface); --bs-modal-border-radius: .85rem; }

.product-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: .5rem; background: var(--app-surface-2); border: 1px solid var(--app-border); }
.product-image-lg { max-width: 100%; max-height: 260px; object-fit: contain; border-radius: .65rem; }
.detail-list dt { color: var(--app-muted); font-weight: 500; font-size: .8rem; }
.detail-list dd { margin-bottom: .7rem; }

/* Light-only Bootstrap utilities, made theme-aware */
[data-bs-theme="dark"] .bg-white { background-color: var(--app-surface) !important; }
[data-bs-theme="dark"] .bg-light { background-color: var(--app-surface-2) !important; }
[data-bs-theme="dark"] .text-dark { color: var(--app-text) !important; }
[data-bs-theme="dark"] .text-bg-light { background-color: var(--app-surface-2) !important; color: var(--app-text) !important; }

/* Invoices are paper documents: always light */
.invoice-paper { --bs-card-bg: #ffffff; background: #ffffff !important; color: #1f2937; }

/* ---------- Permission grid ---------- */
.perm-module + .perm-module { border-top: 1px solid var(--app-border); }

/* ---------- POS ---------- */
.pos-results { z-index: 1050; max-height: 360px; overflow-y: auto; top: calc(100% + 4px); border-radius: .65rem; }
.pos-results .list-group-item.active small { color: rgba(255, 255, 255, .8); }

/* ---------- Charts ---------- */
.chart-box { position: relative; height: 300px; }
.chart-box-sm { position: relative; height: 240px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 1rem; font-size: .8rem; color: var(--app-muted); }
.chart-legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: .35rem; vertical-align: -1px; }
.chart-table td, .chart-table th { font-variant-numeric: tabular-nums; }
details.chart-data > summary { cursor: pointer; font-size: .78rem; color: var(--app-muted); }
details.chart-data > summary:hover { color: var(--app-primary); }

/* ---------- Login ---------- */
.auth-page { min-height: 100vh; display: grid; grid-template-columns: 1.05fr 1fr; background: var(--app-surface); }
.auth-brand {
    position: relative; overflow: hidden; color: #fff; padding: 3rem;
    display: flex; flex-direction: column; justify-content: space-between;
    background: radial-gradient(circle at 20% 20%, rgba(139, 92, 246, .55), transparent 55%),
                radial-gradient(circle at 80% 80%, rgba(56, 189, 248, .35), transparent 50%),
                linear-gradient(135deg, #1e1b4b 0%, #312e81 45%, #0f172a 100%);
}
.auth-brand h1 { font-size: 2.1rem; font-weight: 700; line-height: 1.2; letter-spacing: -.02em; max-width: 26rem; }
.auth-brand p { color: rgba(255, 255, 255, .75); max-width: 26rem; }
.auth-feature { display: flex; gap: .75rem; align-items: flex-start; margin-bottom: 1rem; color: rgba(255, 255, 255, .85); }
.auth-feature i { width: 34px; height: 34px; border-radius: .5rem; background: rgba(255, 255, 255, .12); display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
.auth-form-wrap { display: flex; align-items: center; justify-content: center; padding: 2rem; }
.auth-form { width: 100%; max-width: 380px; }
@media (max-width: 991.98px) {
    .auth-page { grid-template-columns: 1fr; }
    .auth-brand { display: none; }
}

/* ---------- Print ---------- */
@media print {
    .app-sidebar, .app-sidebar-backdrop, .app-topbar, .app-footer, .alert, .d-print-none, .page-header .btn { display: none !important; }
    .app-main { margin-left: 0 !important; }
    .app-content { padding: 0 !important; }
    body { background: #fff; }
    .card { box-shadow: none !important; }
}
