/* ===================== TABLET (max 1024px) ===================== */
@media (max-width: 1024px) {
    .stats-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .two-col {
        grid-template-columns: 1fr;
    }

    .date-picker-months {
        grid-template-columns: 1fr;
    }

    .date-picker-panel {
        flex-direction: column;
        max-width: 95vw;
    }

    .date-picker-presets {
        width: auto;
        flex-direction: row;
        flex-wrap: wrap;
        padding: 8px;
    }

    .date-picker-presets button {
        padding: 6px 12px;
    }

    .spx-metrics-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .spx-chart-row-2 {
        grid-template-columns: 1fr;
    }

    .dash-stats {
        grid-template-columns: repeat(2, 1fr);
    }

    .dash-chart-row {
        grid-template-columns: 1fr;
    }

    .dash-status-row {
        grid-template-columns: 1fr;
    }

    /* Sync cards */
    #page-sync>div:first-child {
        grid-template-columns: 1fr 1fr !important;
    }
}

/* ===================== MOBILE (max 768px) ===================== */
@media (max-width: 768px) {

    /* Sidebar as overlay */
    .sidebar {
        transform: translateX(-100%);
        width: 280px;
        z-index: 1000;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.3);
    }

    /* Sidebar backdrop */
    .sidebar.open::after {
        content: '';
        position: fixed;
        top: 0;
        right: -100vw;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, 0.4);
        z-index: -1;
    }

    .main-content {
        margin-left: 0;
        width: 100%;
    }

    .mobile-menu-btn {
        display: flex;
    }

    /* ── Topbar: stack vertically ── */
    .topbar {
        padding: 10px 12px;
        gap: 8px;
        flex-wrap: wrap;
    }

    .topbar-left {
        gap: 10px;
        flex: 1;
        min-width: 0;
    }

    .topbar-title {
        font-size: 15px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .topbar-subtitle {
        font-size: 10px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 200px;
    }

    .topbar-right {
        gap: 0;
        width: 100%;
        order: 3;
    }

    .topbar-right .btn {
        padding: 6px 10px;
        font-size: 11px;
    }

    .last-sync-info {
        display: none;
    }

    /* ── Header Monitoring Bar: horizontal scroll strip ── */
    .hmon-bar {
        width: 100%;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        border-radius: 8px;
        flex-shrink: 0;
    }

    .hmon-bar::-webkit-scrollbar {
        display: none;
    }

    .hmon-item {
        padding: 5px 8px;
        gap: 4px;
        flex-shrink: 0;
        min-width: 0;
    }

    .hmon-status-icon {
        width: 16px;
        height: 16px;
    }

    .hmon-status-icon i {
        font-size: 8px !important;
    }

    .hmon-title {
        font-size: 9px;
        gap: 3px;
    }

    .hmon-title i {
        font-size: 8px !important;
    }

    .hmon-desc {
        font-size: 8px;
        max-width: 120px;
    }

    .hmon-sync-btn {
        width: 16px;
        height: 16px;
    }

    .hmon-sync-btn svg {
        width: 10px;
        height: 10px;
    }

    /* ── Dashboard stat cards ── */
    .dash-stats {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .dash-stat-card {
        padding: 12px;
        border-radius: 12px;
    }

    .dsc-value {
        font-size: 16px;
    }

    .dsc-icon {
        width: 32px;
        height: 32px;
        font-size: 13px;
    }

    .dsc-sub {
        font-size: 9px;
    }

    .dsc-label {
        font-size: 9px;
    }

    /* ── Finance bar: wrap on mobile ── */
    .dash-finance-bar {
        padding: 12px 14px;
        justify-content: flex-start;
        gap: 10px;
        flex-wrap: wrap;
    }

    .dfb-divider {
        display: none;
    }

    .dfb-item {
        flex: 1;
        min-width: 120px;
    }

    .dfb-label {
        font-size: 9px;
    }

    .dfb-amount {
        font-size: 13px;
    }

    .dash-chart-row {
        grid-template-columns: 1fr;
    }

    .dash-status-row {
        grid-template-columns: 1fr;
    }

    .dsi-left {
        min-width: 80px;
    }

    /* ── Page content ── */
    .page-content {
        padding: 12px;
    }

    /* ── Stat cards (general) ── */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .stat-card {
        padding: 12px;
    }

    .stat-value {
        font-size: 18px;
    }

    .stat-icon-wrap {
        width: 32px;
        height: 32px;
        font-size: 13px;
    }

    /* Cards */
    .card-header {
        padding: 12px 14px;
        flex-direction: column;
        align-items: flex-start;
    }

    .table-filters {
        width: 100%;
        justify-content: flex-start;
    }

    .filter-input,
    .filter-select {
        font-size: 12px;
        padding: 8px 10px;
    }

    /* Tables — horizontal scroll */
    .table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .data-table {
        font-size: 12px;
        min-width: 600px;
    }

    .data-table thead th {
        padding: 8px 10px;
        font-size: 9px;
    }

    .data-table tbody td {
        padding: 8px 10px;
    }

    /* Table footer */
    .table-footer {
        flex-direction: column;
        gap: 8px;
        padding: 10px 14px;
    }

    /* SPX/EVP hero */
    .spx-hero {
        padding: 14px 16px;
        flex-direction: column;
        align-items: flex-start;
    }

    .spx-hero h2 {
        font-size: 16px;
    }

    .spx-hero-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .spx-hero-actions button {
        flex: 1;
        min-width: 0;
        justify-content: center;
    }

    /* SPX/EVP metric cards */
    .spx-metrics-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px;
    }

    .spx-metric-card {
        padding: 12px;
    }

    .spx-mc-value {
        font-size: 16px !important;
    }

    /* Charts */
    .spx-charts-wrapper {
        gap: 12px;
    }

    .spx-chart-card {
        border-radius: 10px;
    }

    .chart-container {
        height: 200px !important;
        padding: 0 12px 12px 12px !important;
    }

    /* SPX toolbar */
    .spx-table-toolbar {
        padding: 12px;
        flex-direction: column;
        align-items: stretch;
    }

    .spx-toolbar-search {
        width: 100%;
    }

    .spx-toolbar-filters {
        flex-wrap: wrap;
    }

    .spx-filter-btn {
        flex: 1;
        justify-content: center;
    }

    /* Status tabs */
    .spx-status-tabs {
        padding: 0 12px;
    }

    .spx-tab {
        padding: 10px 12px;
        font-size: 12px;
    }

    /* EVP filters */
    #evp-filters {
        flex-direction: column;
        align-items: stretch;
    }

    #evp-filters .filter-input {
        min-width: 0 !important;
        width: 100%;
    }

    /* Pagination */
    .pagination {
        flex-wrap: wrap;
        gap: 4px;
    }

    .pagination button {
        min-width: 32px;
        padding: 6px 8px;
        font-size: 11px;
    }

    /* CS Recap */
    .cs-grid {
        grid-template-columns: 1fr !important;
    }

    /* Input Order */
    #page-input>div {
        grid-template-columns: 1fr !important;
    }

    /* Draft */
    .draft-table {
        min-width: 800px;
    }

    /* Draft export bar: compact on mobile */
    .export-bar {
        flex-wrap: wrap;
        gap: 6px;
        padding: 8px 12px;
    }

    .selection-info {
        flex: 0 0 auto;
    }

    .selection-info .selection-info-text {
        display: none;
    }

    .export-bar .btn {
        font-size: 10px;
        padding: 5px 8px;
    }

    /* Draft tabs: horizontal scroll */
    .draft-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        flex-wrap: nowrap !important;
    }

    .draft-tabs::-webkit-scrollbar {
        display: none;
    }

    .draft-tab {
        flex-shrink: 0;
        font-size: 11px;
        padding: 8px 12px;
        white-space: nowrap;
    }

    /* Order cards: 2-column on mobile like dashboard */
    .order-stats-row {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .order-stat-card {
        padding: 10px 12px;
        border-radius: 10px;
    }

    .order-stat-card .os-value {
        font-size: 18px;
    }

    .order-stat-card .os-label {
        font-size: 9px;
    }

    .order-stat-card .os-sub {
        font-size: 8px;
    }

    .order-pills {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding-bottom: 4px;
    }

    .order-pills::-webkit-scrollbar {
        display: none;
    }

    .order-pill {
        flex-shrink: 0;
        padding: 4px 10px;
        font-size: 11px;
    }

    .order-search-bar {
        flex-direction: column;
    }

    .order-search-input {
        min-width: 0;
        width: 100%;
    }

    /* Sync cards */
    #page-sync>div:first-child {
        grid-template-columns: 1fr !important;
    }

    .sync-card {
        padding: 16px;
    }

    /* Credentials grid */
    #page-sync .card-body>div {
        grid-template-columns: 1fr !important;
    }

    /* Product modal */
    .modal-content,
    .modal {
        width: 95vw !important;
        max-width: 95vw !important;
        margin: 10px auto;
        max-height: 90vh;
        overflow-y: auto;
    }

    #product-modal .modal-body div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }

    /* Date picker */
    .date-picker-overlay {
        padding: 10px;
    }

    .date-picker-panel {
        max-height: 90vh;
        overflow-y: auto;
    }

    /* Retur charts */
    .spx-chart-full {
        margin-bottom: 12px;
    }
}

/* ===================== SMALL PHONE (max 480px) ===================== */
@media (max-width: 480px) {

    html {
        font-size: 13px;
    }

    .page-content {
        padding: 8px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 6px;
    }

    .stat-card {
        padding: 10px;
    }

    .stat-value {
        font-size: 16px;
    }

    .stat-label {
        font-size: 10px;
    }

    .topbar {
        padding: 8px 10px;
    }

    .topbar-title {
        font-size: 13px;
    }

    .topbar-subtitle {
        display: none;
    }

    .topbar-right .btn {
        padding: 5px 8px;
        font-size: 10px;
    }

    /* Sync button text hidden */
    .topbar-right .btn span {
        display: none;
    }

    /* ── Header Monitoring: ultra compact ── */
    .hmon-item {
        padding: 4px 6px;
        gap: 3px;
    }

    .hmon-desc {
        display: none;
    }

    .hmon-content {
        gap: 0;
    }

    .hmon-title {
        font-size: 8px;
    }

    .hmon-status-icon {
        width: 14px;
        height: 14px;
    }

    .hmon-sync-btn {
        width: 14px;
        height: 14px;
    }

    .hmon-sync-btn svg {
        width: 8px;
        height: 8px;
    }

    /* ── Dashboard stat cards: compact ── */
    .dash-stats {
        gap: 6px;
    }

    .dash-stat-card {
        padding: 10px;
    }

    .dsc-value {
        font-size: 14px;
    }

    .dsc-icon {
        width: 28px;
        height: 28px;
        font-size: 11px;
    }

    .dsc-sub {
        font-size: 8px;
    }

    .dsc-label {
        font-size: 8px;
    }

    /* ── Finance bar: tighter ── */
    .dfb-item {
        min-width: 100px;
    }

    .dfb-label {
        font-size: 8px;
    }

    .dfb-amount {
        font-size: 12px;
    }

    .dfb-sub {
        font-size: 8px;
    }

    /* ── Order stat cards ── */
    .order-stat-card {
        padding: 8px 10px;
    }

    .order-stat-card .os-value {
        font-size: 16px;
    }

    .order-stat-card .os-label {
        font-size: 8px;
    }

    .order-pill {
        padding: 3px 8px;
        font-size: 10px;
    }

    .spx-metrics-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px;
    }

    .spx-metric-card {
        padding: 10px;
    }

    .spx-mc-value {
        font-size: 14px !important;
    }

    .spx-mc-label {
        font-size: 8px !important;
    }

    .spx-hero {
        padding: 12px;
    }

    .spx-hero h2 {
        font-size: 14px;
    }

    .spx-hero-icon {
        width: 36px;
        height: 36px;
        font-size: 16px;
    }

    .card-title {
        font-size: 12px;
    }

    .chart-container {
        height: 160px !important;
    }

    /* Tables */
    .data-table {
        min-width: 500px;
    }

    .data-table thead th {
        font-size: 8px;
        padding: 6px 8px;
    }

    .data-table tbody td {
        font-size: 11px;
        padding: 6px 8px;
    }
}

/* ===== SPX TRACKING MODAL ===== */
.spx-track-popup {
    border-radius: 12px !important;
}

.spx-track-modal {
    text-align: left;
}

.spx-track-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.spx-track-badge {
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    padding: 4px 12px;
    border-radius: 16px;
}

.spx-track-resi {
    font-size: 13px;
    font-weight: 600;
    color: #475569;
    font-family: monospace;
}

.spx-track-info {
    background: #f8fafc;
    border-radius: 8px;
    padding: 10px 14px;
    margin-bottom: 16px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    font-size: 12px;
    color: #475569;
}

.spx-ti-label {
    color: #9ca3af;
    font-size: 10px;
    display: block;
}

.spx-tl-title {
    font-size: 13px;
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 10px;
}

.spx-timeline {
    position: relative;
    padding-left: 20px;
    max-height: 320px;
    overflow-y: auto;
}

.spx-tl-item {
    position: relative;
    padding-bottom: 16px;
    padding-left: 12px;
}

.spx-tl-item:not(:last-child)::before {
    content: '';
    position: absolute;
    left: -15px;
    top: 10px;
    bottom: 0;
    width: 2px;
    background: #e2e8f0;
}

.spx-tl-dot {
    position: absolute;
    left: -20px;
    top: 4px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 0 0 2px #e2e8f0;
}

.spx-tl-active .spx-tl-dot {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
    width: 12px;
    height: 12px;
    left: -21px;
    top: 3px;
}

.spx-tl-time {
    font-size: 10px;
    color: #94a3b8;
    font-weight: 600;
    margin-bottom: 2px;
}

.spx-tl-desc {
    font-size: 12px;
    color: #334155;
    line-height: 1.4;
}

.spx-tl-loc {
    font-size: 10px;
    color: #6366f1;
    margin-top: 3px;
}

.spx-tl-active .spx-tl-desc {
    font-weight: 600;
    color: #1e293b;
}

/* ===== NEW ORDER NOTIFICATION BANNER ===== */
.new-orders-banner {
    display: none;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 16px;
    background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
    border: 1px solid #93c5fd;
    border-radius: 8px;
    margin-bottom: 12px;
    cursor: pointer;
    transition: all 0.25s ease;
    font-size: 13px;
    color: #1d4ed8;
    animation: bannerSlideIn 0.35s ease;
}

.new-orders-banner:hover {
    background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.15);
}

.new-orders-banner i {
    font-size: 14px;
}

.new-orders-banner strong {
    font-weight: 700;
}

@keyframes bannerSlideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===== NEW ROW HIGHLIGHT (yellow fade to white) ===== */
@keyframes newRowFade {
    0% {
        background-color: #fef9c3;
    }

    100% {
        background-color: transparent;
    }
}

tr.new-row-highlight td {
    animation: newRowFade 3s ease forwards;
}

/* ===== CELL OVERFLOW POPUP (double-click expand) ===== */
.cell-overflow-popup {
    position: absolute;
    z-index: 100;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    padding: 8px 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    max-width: 400px;
    min-width: 120px;
    font-size: 13px;
    line-height: 1.5;
    word-break: break-word;
    white-space: normal;
    pointer-events: auto;
    animation: popupFadeIn 0.15s ease;
}

@keyframes popupFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Login styles moved to login.css */

/* ============================================
   SIDEBAR TENANT FOOTER
   ============================================ */
.sidebar-footer {
    border-top: 1px solid var(--gray-200);
    padding: 8px 12px;
    margin-top: auto;
    position: relative;
    flex-shrink: 0;
}

.sidebar {
    display: flex;
    flex-direction: column;
}

.tenant-info {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.tenant-info:hover {
    background: var(--gray-100);
}

.tenant-avatar {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--primary-500) 0%, var(--primary-700) 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 700;
    flex-shrink: 0;
}

.tenant-details {
    flex: 1;
    min-width: 0;
}

.tenant-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--gray-800);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tenant-role {
    font-size: 11px;
    color: var(--gray-500);
}

.tenant-chevron {
    font-size: 11px;
    color: var(--gray-400);
    transition: transform 0.2s;
}

.tenant-chevron.open {
    transform: rotate(180deg);
}

.tenant-switcher {
    position: absolute;
    bottom: 100%;
    left: 8px;
    right: 8px;
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 10px;
    padding: 6px;
    margin-bottom: 4px;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
    z-index: 100;
    max-height: 300px;
    overflow-y: auto;
}

.tenant-switch-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: var(--gray-700);
    transition: background 0.15s;
}

.tenant-switch-item:hover {
    background: var(--primary-50);
    color: var(--primary-700);
}

.tenant-switch-item.active {
    background: var(--primary-50);
    color: var(--primary-700);
    font-weight: 600;
}

.tenant-switch-item .switch-avatar {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    background: var(--gray-200);
    color: var(--gray-600);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
}

.tenant-switch-item.active .switch-avatar {
    background: linear-gradient(135deg, var(--primary-500) 0%, var(--primary-700) 100%);
    color: white;
}

.tenant-switch-logout {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: #ef4444;
    margin-top: 4px;
    border-top: 1px solid var(--gray-100);
    padding-top: 8px;
    transition: background 0.15s;
}

.tenant-switch-logout:hover {
    background: #fef2f2;
}

/* ============================================= */
/* SafeNumber Global Popup                       */
/* ============================================= */
.sn-global-popup {
    position: absolute;
    z-index: 9999;
    width: 210px;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: 12px;
    padding: 14px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12), 0 4px 12px rgba(0, 0, 0, 0.06);
    font-family: 'Inter', sans-serif;
    animation: snPopIn 0.2s ease-out;
}

@keyframes snPopIn {
    from {
        opacity: 0;
        transform: translateY(-4px) scale(0.96);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.sn-info-btn:hover {
    background: var(--primary-200) !important;
    transform: scale(1.1);
}

/* Input Order Mobile Layout */
@media (max-width: 768px) {
    .input-order-grid {
        grid-template-columns: 1fr !important;
    }

    .input-order-form {
        order: 1;
    }

    .input-order-preview {
        order: 2;
        position: static !important;
    }
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 38px;
    height: 20px;
    flex-shrink: 0;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: var(--gray-300);
    border-radius: 20px;
    transition: 0.3s;
}

.toggle-slider::before {
    content: "";
    position: absolute;
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: 0.3s;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.toggle-switch input:checked+.toggle-slider {
    background: var(--primary-500);
}

.toggle-switch input:checked+.toggle-slider::before {
    transform: translateX(18px);
}