/*
 * Design system: warm brass-on-neutral light theme, deep slate dark theme. Palette is applied
 * two ways -
 *   1) Bootstrap's own theme variables (--bs-primary, --bs-success, --bs-body-bg, etc.) are
 *      redefined here so every Bootstrap component (buttons, badges, alerts, links, tables)
 *      picks up the new palette automatically, without fighting Bootstrap's !important utility
 *      classes one by one.
 *   2) A smaller set of app-specific tokens (--surface, --ink, --muted, --font-*, ...) drive the
 *      custom shell/card/table styling below.
 *
 * Theme resolution follows the 3-state pattern: bare :root is the light default; the dark media
 * query is guarded with :not([data-theme="light"]) so an explicit light choice always beats the
 * OS preference; [data-theme="dark"] repeats the same values so the toggle wins in both
 * directions. See js/theme.js for the toggle itself.
 */
:root {
    /* Bootstrap theme variables */
    --bs-primary: #A6702A;
    --bs-primary-rgb: 166, 112, 42;
    --bs-secondary: #5B6672;
    --bs-secondary-rgb: 91, 102, 114;
    --bs-success: #2E7D6B;
    --bs-success-rgb: 46, 125, 107;
    --bs-warning: #B8791A;
    --bs-warning-rgb: 184, 121, 26;
    --bs-danger: #B93B32;
    --bs-danger-rgb: 185, 59, 50;
    --bs-info: #3C6E9C;
    --bs-info-rgb: 60, 110, 156;
    --bs-body-bg: #F4F5F6;
    --bs-body-bg-rgb: 244, 245, 246;
    --bs-body-color: #17212A;
    --bs-body-color-rgb: 23, 33, 42;
    --bs-border-color: #DEE1E4;
    --bs-border-color-translucent: rgba(23, 33, 42, 0.15);
    --bs-tertiary-bg: #EDEEF0;
    --bs-secondary-bg: #EDEEF0;
    --bs-secondary-color: #5B6672;
    --bs-secondary-color-rgb: 91, 102, 114;
    --bs-tertiary-color: #7C8792;
    --bs-tertiary-color-rgb: 124, 135, 146;
    --bs-emphasis-color: #17212A;
    --bs-emphasis-color-rgb: 23, 33, 42;
    --bs-light: #EDEEF0;
    --bs-light-rgb: 237, 238, 240;
    --bs-link-color: #A6702A;
    --bs-link-color-rgb: 166, 112, 42;
    --bs-link-hover-color: #8A5C22;

    /* App tokens */
    --surface: #FFFFFF;
    --surface-alt: #EDEEF0;
    --ink: #17212A;
    --muted: #5B6672;
    --border: #DEE1E4;
    --accent: #A6702A;
    --accent-ink: #FFFFFF;
    --shadow: 0 1px 2px rgba(23, 33, 42, 0.06), 0 2px 8px rgba(23, 33, 42, 0.07);

    --font-display: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
    --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: ui-monospace, "Cascadia Code", Consolas, monospace;

    --sidebar-width: 248px;
    --sidebar-width-collapsed: 72px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bs-primary: #D9A54B;
        --bs-primary-rgb: 217, 165, 75;
        --bs-secondary: #93A1AC;
        --bs-secondary-rgb: 147, 161, 172;
        --bs-success: #4FA98E;
        --bs-success-rgb: 79, 169, 142;
        --bs-warning: #E0A63D;
        --bs-warning-rgb: 224, 166, 61;
        --bs-danger: #E0685D;
        --bs-danger-rgb: 224, 104, 93;
        --bs-info: #6FA0C9;
        --bs-info-rgb: 111, 160, 201;
        --bs-body-bg: #10161B;
        --bs-body-bg-rgb: 16, 22, 27;
        --bs-body-color: #E9ECEE;
        --bs-body-color-rgb: 233, 236, 238;
        --bs-border-color: #2B363F;
        --bs-border-color-translucent: rgba(255, 255, 255, 0.15);
        --bs-tertiary-bg: #212B33;
        --bs-secondary-bg: #212B33;
        --bs-secondary-color: #93A1AC;
        --bs-secondary-color-rgb: 147, 161, 172;
        --bs-tertiary-color: #6E7A85;
        --bs-tertiary-color-rgb: 110, 122, 133;
        --bs-emphasis-color: #E9ECEE;
        --bs-emphasis-color-rgb: 233, 236, 238;
        --bs-light: #212B33;
        --bs-light-rgb: 33, 43, 51;
        --bs-link-color: #D9A54B;
        --bs-link-color-rgb: 217, 165, 75;
        --bs-link-hover-color: #EBC077;

        --surface: #1A222A;
        --surface-alt: #212B33;
        --ink: #E9ECEE;
        --muted: #93A1AC;
        --border: #2B363F;
        --accent: #D9A54B;
        --accent-ink: #17212A;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 2px 10px rgba(0, 0, 0, 0.4);
    }
}

:root[data-theme="dark"] {
    --bs-primary: #D9A54B;
    --bs-primary-rgb: 217, 165, 75;
    --bs-secondary: #93A1AC;
    --bs-secondary-rgb: 147, 161, 172;
    --bs-success: #4FA98E;
    --bs-success-rgb: 79, 169, 142;
    --bs-warning: #E0A63D;
    --bs-warning-rgb: 224, 166, 61;
    --bs-danger: #E0685D;
    --bs-danger-rgb: 224, 104, 93;
    --bs-info: #6FA0C9;
    --bs-info-rgb: 111, 160, 201;
    --bs-body-bg: #10161B;
    --bs-body-bg-rgb: 16, 22, 27;
    --bs-body-color: #E9ECEE;
    --bs-body-color-rgb: 233, 236, 238;
    --bs-border-color: #2B363F;
    --bs-border-color-translucent: rgba(255, 255, 255, 0.15);
    --bs-tertiary-bg: #212B33;
    --bs-secondary-bg: #212B33;
    --bs-secondary-color: #93A1AC;
    --bs-secondary-color-rgb: 147, 161, 172;
    --bs-tertiary-color: #6E7A85;
    --bs-tertiary-color-rgb: 110, 122, 133;
    --bs-emphasis-color: #E9ECEE;
    --bs-emphasis-color-rgb: 233, 236, 238;
    --bs-light: #212B33;
    --bs-light-rgb: 33, 43, 51;
    --bs-link-color: #D9A54B;
    --bs-link-color-rgb: 217, 165, 75;
    --bs-link-hover-color: #EBC077;

    --surface: #1A222A;
    --surface-alt: #212B33;
    --ink: #E9ECEE;
    --muted: #93A1AC;
    --border: #2B363F;
    --accent: #D9A54B;
    --accent-ink: #17212A;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 2px 10px rgba(0, 0, 0, 0.4);
}

body {
    background-color: var(--bs-body-bg);
    color: var(--ink);
    font-family: var(--font-body);
}

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-display);
}

.btn-primary {
    color: var(--accent-ink);
}

.login-card {
    max-width: 380px;
    margin: 8rem auto 0;
    background: var(--surface);
    border-color: var(--border);
    box-shadow: var(--shadow);
}

.login-brand .brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 10px;
    background: var(--accent);
    color: var(--accent-ink);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
}

.login-brand .title {
    font-family: var(--font-display);
    font-weight: 700;
    margin-top: 0.5rem;
    color: var(--ink);
}

.login-theme-toggle {
    position: fixed;
    top: 1rem;
    right: 1rem;
}

.table-actions {
    white-space: nowrap;
}

/* App shell: left sidebar + main content column */
.app-shell {
    display: flex;
    min-height: 100vh;
    align-items: stretch;
}

.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: width 0.15s ease, transform 0.2s ease;
}

.sidebar.collapsed {
    width: var(--sidebar-width-collapsed);
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 1.25rem 1.25rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-brand .brand-mark {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: var(--accent);
    color: var(--accent-ink);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1.05rem;
}

.sidebar-brand .brand-text {
    line-height: 1.15;
}

.sidebar-brand .brand-text .title {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 0.98rem;
    color: var(--ink);
}

.sidebar-brand .brand-text .subtitle {
    font-size: 0.75rem;
    color: var(--muted);
}

.sidebar.collapsed .sidebar-brand .brand-text {
    display: none;
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem 0;
}

.sidebar-nav .nav-item-link {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    padding: 0.6rem 1.25rem;
    color: var(--muted);
    text-decoration: none;
    font-size: 0.92rem;
    border-left: 3px solid transparent;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-nav .nav-item-link i {
    font-size: 1.05rem;
    width: 1.2rem;
    text-align: center;
    flex-shrink: 0;
}

.sidebar-nav .nav-item-link:hover {
    background: var(--surface-alt);
    color: var(--ink);
}

.sidebar-nav .nav-item-link.active {
    background: rgba(var(--bs-primary-rgb), 0.12);
    color: var(--accent);
    border-left-color: var(--accent);
    font-weight: 600;
}

.sidebar.collapsed .sidebar-nav .nav-item-link span {
    display: none;
}

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 0.6rem;
}

.sidebar-footer .collapse-toggle {
    width: 100%;
    text-align: left;
    border: none;
    background: none;
    color: var(--muted);
    padding: 0.5rem 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar-footer .collapse-toggle:hover {
    background: var(--surface-alt);
    border-radius: 6px;
}

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

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    gap: 1rem;
    flex-wrap: wrap;
}

.topbar .page-title {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 1.2rem;
    margin: 0;
    color: var(--ink);
}

.topbar .page-subtitle {
    font-size: 0.85rem;
    color: var(--muted);
}

.page-body {
    padding: 1.5rem;
    flex: 1;
    max-width: 100%;
    overflow-x: hidden;
}

.theme-toggle-btn, .mobile-nav-toggle {
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.theme-toggle-btn:hover, .mobile-nav-toggle:hover {
    background: var(--surface-alt);
}

.mobile-nav-toggle {
    display: none;
}

.sidebar-backdrop {
    display: none;
}

/* Dashboard-style stat cards */
.stat-card, .chart-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1.1rem 1.25rem;
    height: 100%;
    box-shadow: var(--shadow);
}

.stat-card .stat-label {
    font-size: 0.82rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.stat-card .stat-value {
    font-family: var(--font-display);
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--ink);
    margin-top: 0.35rem;
    font-variant-numeric: tabular-nums;
}

.stat-card .stat-icon {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(var(--bs-primary-rgb), 0.14);
    color: var(--accent);
}

.chart-card .chart-card-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1rem;
    margin-bottom: 0.75rem;
}

.card {
    background-color: var(--surface);
    border-color: var(--border);
}

.card-header {
    font-family: var(--font-display);
    font-weight: 600;
    background-color: var(--surface-alt);
    border-bottom-color: var(--border);
}

.table {
    --bs-table-color: var(--ink);
    --bs-table-bg: transparent;
    --bs-table-border-color: var(--border);
    --bs-table-hover-bg: var(--surface-alt);
    --bs-table-hover-color: var(--ink);
}

.table td, .table th {
    vertical-align: middle;
}

.table thead th {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    border-bottom-width: 1px;
}

/* Standalone list-page tables (not nested in a .card) use .shadow-sm as their marker for
   "render like a raised surface" - give them an actual themed background to match, instead of
   the Bootstrap bg-white utility this used to lean on (which stayed white in dark mode). */
.table.shadow-sm {
    background-color: var(--surface);
    border: 1px solid var(--border);
}

.form-control, .form-select {
    background-color: var(--surface);
    border-color: var(--border);
    color: var(--ink);
}

.form-control:focus, .form-select:focus {
    background-color: var(--surface);
    color: var(--ink);
    border-color: var(--accent);
    box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.2);
}

.form-control::placeholder {
    color: var(--muted);
    opacity: 0.8;
}

.modal-content {
    background-color: var(--surface);
    color: var(--ink);
    border-color: var(--border);
}

.dropdown-menu {
    background-color: var(--surface);
    color: var(--ink);
    border-color: var(--border);
}

/* KPI calculation preview */
.calc-preview {
    background: var(--surface-alt);
    border: 1px dashed var(--border);
    border-radius: 8px;
    padding: 0.9rem 1rem;
}

.calc-preview table {
    font-size: 0.82rem;
}

.calc-preview .calc-formula {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-size: 0.82rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.5rem 0.7rem;
    margin: 0.35rem 0;
    overflow-x: auto;
    white-space: nowrap;
}

.calc-preview .calc-result {
    font-weight: 700;
    color: var(--accent);
}

/* Numeric-heavy table columns line up cleanly */
.table .text-numeric, .table td.numeric {
    font-variant-numeric: tabular-nums;
    font-family: var(--font-mono);
}

/* ---- Skeleton loading rows (ListToolkit.setLoading) ---- */
tr.list-skeleton-row td {
    padding-top: 0.85rem;
    padding-bottom: 0.85rem;
}

.skeleton-bar {
    height: 0.85rem;
    border-radius: 4px;
    background: linear-gradient(90deg, var(--surface-alt) 25%, var(--border) 50%, var(--surface-alt) 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
    .skeleton-bar {
        animation: none;
        background: var(--surface-alt);
    }
}

@keyframes skeleton-shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ---- Responsive: off-canvas sidebar drawer + stacked-card tables below 880px/640px ---- */

@media (max-width: 880px) {
    .mobile-nav-toggle {
        display: inline-flex;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 1040;
        width: min(var(--sidebar-width), 82vw);
        transform: translateX(-100%);
        box-shadow: var(--shadow);
    }

    .sidebar.collapsed {
        width: min(var(--sidebar-width), 82vw);
    }

    .sidebar.collapsed .sidebar-brand .brand-text,
    .sidebar.collapsed .sidebar-nav .nav-item-link span {
        display: block;
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    .sidebar-footer {
        display: none;
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1030;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
    }

    .sidebar-backdrop.visible {
        opacity: 1;
        pointer-events: auto;
    }

    .page-body {
        padding: 1rem;
    }
}

@media (max-width: 640px) {
    table.table-responsive-stack {
        border: none;
    }

    table.table-responsive-stack thead {
        display: none;
    }

    table.table-responsive-stack, table.table-responsive-stack tbody, table.table-responsive-stack tr {
        display: block;
        width: 100%;
    }

    table.table-responsive-stack tr {
        margin-bottom: 0.85rem;
        border: 1px solid var(--border);
        border-radius: 10px;
        padding: 0.35rem 0;
        background: var(--surface);
        box-shadow: var(--shadow);
    }

    table.table-responsive-stack td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        padding: 0.4rem 0.75rem;
        border: none;
        text-align: right;
    }

    table.table-responsive-stack td[data-label]::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--muted);
        text-align: left;
        margin-right: auto;
    }

    table.table-responsive-stack td:empty {
        display: none;
    }
}

/* ── Chat widget (floating footer widget, persists across page navigation) ───────── */
.chat-widget {
    position: fixed;
    right: 20px;
    bottom: 0;
    width: 320px;
    max-width: calc(100vw - 40px);
    background: var(--surface);
    border: 1px solid var(--border);
    border-bottom: none;
    border-radius: 10px 10px 0 0;
    box-shadow: var(--shadow);
    z-index: 1400;
    display: flex;
    flex-direction: column;
    max-height: 460px;
}

.chat-widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.85rem;
    background: var(--accent);
    color: var(--accent-ink);
    border-radius: 10px 10px 0 0;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
}

.chat-widget-header .btn-icon {
    background: none;
    border: none;
    color: var(--accent-ink);
    opacity: 0.85;
    padding: 0 0.25rem;
}

.chat-widget-header .btn-icon:hover { opacity: 1; }

.chat-widget-body {
    flex: 1;
    overflow-y: auto;
    padding: 0.65rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 220px;
    max-height: 300px;
}

.chat-widget.collapsed .chat-widget-body,
.chat-widget.collapsed .chat-widget-input-row,
.chat-widget.collapsed .chat-widget-load-more {
    display: none;
}

.chat-widget-load-more {
    text-align: center;
    font-size: 0.78rem;
    padding: 0.25rem 0 0.4rem;
}

.chat-msg {
    max-width: 82%;
    padding: 0.4rem 0.65rem;
    border-radius: 10px;
    font-size: 0.82rem;
    line-height: 1.35;
    overflow-wrap: anywhere;
}

.chat-msg-admin {
    align-self: flex-end;
    background: var(--accent);
    color: var(--accent-ink);
    border-bottom-right-radius: 2px;
}

.chat-msg-shopper {
    align-self: flex-start;
    background: var(--surface-alt);
    color: var(--ink);
    border-bottom-left-radius: 2px;
}

.chat-msg-sender {
    font-size: 0.68rem;
    opacity: 0.75;
    margin-bottom: 0.15rem;
    font-weight: 600;
}

.chat-msg-time {
    font-size: 0.65rem;
    opacity: 0.65;
    margin-top: 0.15rem;
}

.chat-msg-attachment img {
    max-width: 100%;
    border-radius: 8px;
    margin-top: 0.25rem;
    display: block;
}

.chat-widget-input-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.6rem;
    border-top: 1px solid var(--border);
}

.chat-widget-input-row input[type="text"] {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 0.35rem 0.75rem;
    font-size: 0.82rem;
    background: var(--surface);
    color: var(--ink);
}

.chat-widget-input-row .btn-icon {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 1.05rem;
    padding: 0.2rem 0.35rem;
}

.chat-widget-input-row .btn-send {
    background: var(--accent);
    color: var(--accent-ink);
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* ── Chat module page (shopper list + unread badge, notification composer) ───────── */
.chat-unread-dot {
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--bs-danger);
    margin-right: 6px;
}

.chat-shopper-row { cursor: pointer; }
