:root {
    --primary: #FFBE0B;
    --primary-hover: #e0a800;
    --dark-bg: #1e190d;
    --dark: #2A2312;
    --text-main: #fcfcfc;
    --text-muted: #a39c89;
    
    --success: #34d399;
    --danger: #ef4444;
    --warning: #fbbf24;

    --panel-bg: rgba(42, 35, 18, 0.7);
    --border: rgba(255, 190, 11, 0.15);
    
    --font-family: 'Inter', sans-serif;
}

[hidden] { display: none !important; }

.setup-panel h1 { font-size: 1.8rem; margin-bottom: 12px; }
.setup-panel p { color: var(--text-muted); line-height: 1.6; font-size: 0.95rem; margin-bottom: 22px; }
.setup-panel form, #stockActionForm { display: grid; gap: 16px; }
.setup-panel label, #stockActionForm label { display: grid; gap: 7px; color: var(--text-muted); font-size: 0.9rem; }
.setup-panel input { width: 100%; padding: 11px 12px; }
.setup-panel a { color: var(--primary); display: inline-block; margin-top: 16px; font-size: 0.9rem; }
.setup-panel #setupMessage { margin: 16px 0 0; }

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-size: 11.5px; /* Scaled down base font size to fit more content */
    font-family: var(--font-family);
    background: linear-gradient(135deg, var(--dark-bg) 0%, var(--dark) 100%);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

/* Custom Premium Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb {
    background: rgba(255, 190, 11, 0.5); /* Primary color with transparency */
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--primary);
}

/* Glassmorphism Panel */
.panel {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px; /* Reduced from 16px */
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
    font-weight: 700;
    color: var(--text-main);
}
.text-primary { color: var(--primary); }
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 8px;
    border: none;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text-main);
}
.btn-primary {
    background: var(--primary);
    color: var(--dark);
}
.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
}
.btn-outline {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}
.btn-outline:hover {
    background: rgba(255, 190, 11, 0.1);
}
.icon-btn {
    background: transparent;
    border: none;
    color: var(--primary);
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.icon-btn:hover {
    background: rgba(255, 190, 11, 0.1);
}

/* Inputs */
input:not([type="checkbox"]), select {
    width: 100%;
    padding: 8px 12px;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-main);
    font-family: inherit;
    transition: border 0.2s;
}
input:not([type="checkbox"]):focus, select:focus {
    outline: none;
    border-color: var(--primary);
}

input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--primary);
    cursor: pointer;
    flex-shrink: 0;
}

input[type="date"] {
    color-scheme: dark;
    cursor: pointer;
}

select option {
    background: var(--dark);
    color: var(--text-main);
}

/* Custom Date Picker (Centered on Screen) */
.datepicker-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 300px;
    z-index: 99999;
    box-shadow: 0 16px 50px rgba(0, 0, 0, 0.9), 0 0 25px rgba(255, 190, 11, 0.2);
    padding: 20px;
    border-radius: 16px;
    background: #181818;
    border: 1px solid rgba(255, 190, 11, 0.35);
}
.dp-days-header {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 0.7rem;
    font-weight: bold;
    color: var(--primary);
    margin-bottom: 8px;
}
.dp-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
}
.dp-cell {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text-main);
}
.dp-cell:hover:not(.dp-empty) {
    background: rgba(255, 179, 71, 0.2);
}
.dp-cell.dp-selected {
    background: var(--primary);
    color: var(--dark);
    font-weight: bold;
}
.dp-empty {
    cursor: default;
}

/* Fix native password eye color for dark mode (Edge/Windows) */
input[type="password"]::-ms-reveal,
input[type="password"]::-ms-clear {
    filter: invert(1) brightness(2);
}

label {
    display: block;
    margin-bottom: 4px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* Loader */
.loader-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--dark-bg);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.8s ease, visibility 0.8s ease, transform 0.8s cubic-bezier(0.5, 0, 0.2, 1);
    transform: scale(1);
}
.loader-container.hidden {
    opacity: 0;
    visibility: hidden;
    transform: scale(5);
}
.loader-content {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}
.loader-icon {
    width: 140px;
    height: 140px;
    color: var(--primary);
    filter: drop-shadow(0 0 20px rgba(255, 190, 11, 0.6));
    animation: pulseGlow 1.5s infinite alternate ease-in-out;
}
@keyframes pulseGlow {
    0% { transform: scale(0.95); filter: drop-shadow(0 0 10px rgba(255, 190, 11, 0.4)); }
    100% { transform: scale(1.05); filter: drop-shadow(0 0 40px rgba(255, 190, 11, 0.9)); }
}

/* Background Particles */
#particles-canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    pointer-events: none;
}

/* Login Page */
#page-login {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    position: relative;
    z-index: 1;
}
.login-box {
    width: 100%;
    max-width: 400px;
    text-align: center;
}
.login-box h2 {
    margin-bottom: 24px;
    font-size: 2rem;
}
.login-box .input-group {
    text-align: left;
    margin-bottom: 16px;
}
.login-box .btn {
    width: 100%;
    margin-top: 16px;
}
#loginError {
    color: var(--danger);
    font-size: 0.85rem;
    margin-bottom: 16px;
    display: none;
}
#loginError.active {
    display: block;
}

/* Dashboard Layout */
.dashboard-layout {
    display: none; /* hidden by default */
    grid-template-columns: 200px 1fr;
    height: 100vh;
    overflow: hidden;
}
.dashboard-layout.active {
    display: grid;
}

/* Sidebar */
.sidebar {
    background: var(--dark);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 12px;
    height: 100vh;
}
.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--primary);
}
.nav-menu {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 8px;
    transition: all 0.2s;
    font-weight: 500;
}
.nav-item:hover {
    color: var(--primary);
    background: rgba(255, 190, 11, 0.05);
}
.nav-item.active {
    color: var(--dark);
    background: var(--primary);
}
.nav-submenu {
    display: none;
    flex-direction: column;
    gap: 3px;
    padding-left: 14px;
    margin-top: 3px;
    margin-bottom: 4px;
    border-left: 2px solid rgba(255, 190, 11, 0.2);
    margin-left: 12px;
}
.nav-submenu.open {
    display: flex;
}
.nav-subitem {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 6px;
    font-size: 0.82rem;
    font-weight: 500;
    transition: all 0.2s;
}
.nav-subitem:hover {
    color: var(--primary);
    background: rgba(255, 190, 11, 0.08);
}
.nav-subitem.active {
    color: var(--primary);
    background: rgba(255, 190, 11, 0.16);
    font-weight: 600;
}
.user-sub-tab {
    transition: all 0.2s ease;
    cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    background: rgba(255, 255, 255, 0.04) !important;
    color: var(--text-muted) !important;
    padding: 8px 16px;
    font-size: 0.85rem;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 600;
    outline: none;
}
.user-sub-tab:hover {
    background: rgba(255, 190, 11, 0.12) !important;
    color: var(--primary) !important;
    border-color: rgba(255, 190, 11, 0.35) !important;
}
.user-sub-tab.active {
    background: var(--primary) !important;
    color: var(--dark) !important;
    font-weight: 700 !important;
    border-color: var(--primary) !important;
    box-shadow: 0 4px 14px rgba(255, 190, 11, 0.25);
}
.user-sub-tab span {
    background: rgba(255, 190, 11, 0.16) !important;
    color: var(--primary) !important;
    padding: 2px 7px;
    border-radius: 10px;
    font-size: 0.72rem;
    margin-left: 4px;
}
.user-sub-tab.active span {
    background: rgba(0, 0, 0, 0.25) !important;
    color: var(--dark) !important;
}
.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}
.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: var(--dark);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}
.user-info h4 {
    font-size: 0.9rem;
    margin-bottom: 2px;
}
.user-info p {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Main Content */
.main-content {
    padding: 12px;
    height: calc(100vh - 40px); /* 40px for margin */
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#viewMasterOrders, #viewUsers {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

#viewLocations, #viewVendors {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow-y: auto;
    padding-right: 4px;
    padding-bottom: 30px;
}

/* Top Header */
.dash-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    flex-shrink: 0;
}
.header-title h1 {
    font-size: 1.25rem;
    margin-bottom: 2px;
}
.header-title p {
    color: var(--text-muted);
    font-size: 0.8rem;
}

/* Metrics Row */
.metrics-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    margin-bottom: 12px;
    flex-shrink: 0;
}
.metric-card {
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.metric-card h3 {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.metric-card .value {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--primary);
}

/* Dashboard Grid (Table + Sidebar) */
.dash-grid {
    display: grid;
    grid-template-columns: 1fr 240px; /* Reduced right sidebar from 260px */
    gap: 12px;
    flex: 1;
    min-height: 0; /* Important for scroll nested in flex/grid */
}

.dash-grid:has(.sidebar-right.hidden),
.dash-grid:has(.sidebar-right[style*="display: none"]),
.dash-grid:not(:has(.sidebar-right)) {
    grid-template-columns: 1fr;
}

/* Data Table */
.table-container {
    overflow-y: auto;
    height: 100%;
}
.table-container-compact {
    overflow-y: auto;
    height: auto !important;
    max-height: 280px;
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}
th, td {
    padding: 6px 8px; /* Compact padding */
    text-align: left;
    border-bottom: 1px solid var(--border);
}
th {
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.65rem;
    letter-spacing: 0.5px;
    position: sticky;
    top: 0;
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    z-index: 10;
}
tbody tr:hover {
    background: rgba(255, 190, 11, 0.03);
}

/* Status Badges */
.status {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}
.status.low { background: rgba(239, 68, 68, 0.2); color: var(--danger); }
.status.ok { background: rgba(52, 211, 153, 0.2); color: var(--success); }
.status.pending { background: rgba(251, 191, 36, 0.2); color: var(--warning); }

/* Right Sidebar Forms */
.sidebar-right {
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: 100%;
    overflow-y: auto;
}
.form-section {
    margin-bottom: 0; /* Gap handles spacing */
}
.form-section h3 {
    margin-bottom: 12px;
    font-size: 1rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 6px;
}
.form-section .input-group {
    margin-bottom: 8px;
}

/* Heatmap */
.heatmap-section {
    margin-top: 32px;
}
.heatmap-table th, .heatmap-table td {
    text-align: center;
}
.heatmap-cell {
    transition: background 0.3s;
}

/* Utility */
.hidden { display: none !important; }
.page:not(.active) { display: none !important; }

/* Responsive Media Queries */
@media (max-width: 1024px) {
    /* Tablet */
    .dashboard-layout {
        grid-template-columns: 180px 1fr;
    }
    .dash-grid {
        grid-template-columns: 1fr; /* Stack add/allocate below table */
        overflow-y: auto;
    }
    .table-container {
        height: auto;
        max-height: 400px; /* Give it a max height so the user can reach the forms */
        overflow-y: auto;
    }
    .sidebar-right {
        height: auto;
        overflow-y: visible;
        flex-direction: row;
        flex-wrap: wrap;
    }
    .form-section {
        flex: 1;
        min-width: 250px;
    }
}

@media (max-width: 768px) {
    /* Mobile */
    .dashboard-layout.active {
        display: flex;
        flex-direction: column;
        height: 100vh;
    }
    .sidebar {
        height: auto;
        flex-direction: row;
        padding: 12px;
        border-right: none;
        border-bottom: 1px solid var(--border);
        overflow-x: auto;
        align-items: center;
        flex-shrink: 0;
    }
    .sidebar-logo {
        margin-bottom: 0;
        margin-right: 20px;
        font-size: 1.1rem;
    }
    .nav-menu {
        flex-direction: row;
        margin-right: 20px;
        gap: 8px;
    }
    .nav-item {
        padding: 6px 10px;
        white-space: nowrap;
    }
    .user-profile {
        margin-top: 0;
        padding-top: 0;
        border-top: none;
        margin-left: auto;
    }
    .main-content {
        padding: 12px;
        height: auto;
        flex: 1;
    }
    .metrics-row {
        grid-template-columns: 1fr 1fr;
    }
    .header-title h1 {
        font-size: 1.2rem;
    }
    .dash-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }
    .sidebar-right {
        flex-direction: column;
    }
    .table-container {
        max-height: 300px;
    }
}

/* Fade In Page */
.fade-in-page {
    opacity: 0;
    transition: opacity 0.8s ease 0.3s;
}
.fade-in-page.show {
    opacity: 1;
}

/* Centered Custom UI Alert / Confirm / Prompt Modal */
#uiAlertModal {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    background: rgba(0, 0, 0, 0.8) !important;
    backdrop-filter: blur(8px) !important;
    -webkit-backdrop-filter: blur(8px) !important;
    z-index: 9999999 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    animation: fadeInModalBg 0.2s ease-out;
}

#uiAlertModal.hidden {
    display: none !important;
}

#uiAlertModal .panel {
    animation: centerModalScale 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeInModalBg {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes centerModalScale {
    from { transform: scale(0.92); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* Master Orders Outlet Filter Pills & Controls */
.outlet-pill {
    padding: 3px 10px;
    font-size: 0.74rem;
    font-weight: 500;
    border-radius: 16px;
    cursor: pointer;
    border: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-muted);
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    user-select: none;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}
.outlet-pill:hover {
    background: rgba(255, 190, 11, 0.12);
    color: var(--text-main);
    border-color: rgba(255, 190, 11, 0.35);
    transform: translateY(-1px);
}
.outlet-pill.active {
    background: var(--primary);
    color: #1a1712 !important;
    font-weight: 700;
    border-color: var(--primary);
    box-shadow: 0 0 12px rgba(255, 190, 11, 0.35);
}
#storeViewTrigger:hover {
    border-color: var(--primary);
    background: rgba(42, 35, 18, 1);
}
#btnResetOutletFilter:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-main);
}

body.database-saving::after {
    content: 'Saving…';
    position: fixed;
    inset: 0;
    z-index: 2147483647;
    background: rgba(0, 0, 0, 0.2);
    display: grid;
    place-items: center;
    color: #ffbe0b;
    font-weight: 700;
    cursor: wait;
}
