/* ============================================
   UNIFIED HEADER & SIDEBAR NAVIGATION
   Standard header for all TERP pages
   ============================================ */

/* Top Header Bar */
#unified-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: linear-gradient(135deg, #063970, #0A4A8A);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    z-index: 9999;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    -webkit-app-region: drag;
}

/* Push body content below fixed header */
body.has-unified-header {
    padding-top: 56px !important;
}

/* Left side: Hamburger + Brand */
.uh-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.uh-hamburger {
    background: none;
    border: none;
    color: white;
    font-size: 22px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    -webkit-app-region: no-drag;
}

.uh-hamburger:hover {
    background: rgba(255,255,255,0.15);
}

.uh-brand {
    color: white;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.uh-page-title {
    color: rgba(255,255,255,0.7);
    font-size: 14px;
    margin-left: 8px;
    font-weight: 400;
    display: none;
}

@media (min-width: 600px) {
    .uh-page-title {
        display: inline;
    }
}

/* Right side: Actions */
.uh-right {
    display: flex;
    align-items: center;
    gap: 4px;
}

.uh-btn {
    background: none;
    border: none;
    color: white;
    font-size: 14px;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    -webkit-app-region: no-drag;
}

.uh-btn:hover {
    background: rgba(255,255,255,0.15);
}

.uh-btn .btn-text {
    font-size: 13px;
}

.uh-logout {
    color: #fca5a5;
}

.uh-logout:hover {
    background: rgba(239,68,68,0.3);
    color: #fecaca;
}

/* Language Toggle */
.uh-lang {
    display: flex;
    background: rgba(255,255,255,0.1);
    border-radius: 20px;
    padding: 2px;
    gap: 2px;
}

.uh-lang-btn {
    background: none;
    border: none;
    color: rgba(255,255,255,0.6);
    font-size: 12px;
    font-weight: 600;
    padding: 5px 10px;
    border-radius: 18px;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-app-region: no-drag;
}

.uh-lang-btn:hover {
    color: white;
}

.uh-lang-btn.active {
    background: white;
    color: #063970;
}

/* ============================================
   SIDEBAR NAVIGATION
   ============================================ */

.uh-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}

.uh-overlay.open {
    opacity: 1;
    visibility: visible;
}

.uh-sidebar {
    position: fixed;
    top: 0;
    left: -300px;
    width: 300px;
    height: 100vh;
    background: white;
    z-index: 10001;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    box-shadow: 4px 0 24px rgba(0,0,0,0.15);
    overflow: hidden;
}

/* Wrapper for nav and footer to enable scrolling */
.uh-sidebar-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    -webkit-overflow-scrolling: touch;
}

.uh-sidebar.open {
    left: 0;
}

/* Sidebar Header */
.uh-sidebar-header {
    background: linear-gradient(135deg, #063970, #0A4A8A);
    padding: 20px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 80px;
}

.uh-sidebar-logo {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    object-fit: contain;
}

.uh-sidebar-brand {
    color: white;
    font-size: 16px;
    font-weight: 700;
    flex: 1;
}

.uh-sidebar-close {
    background: none;
    border: none;
    color: rgba(255,255,255,0.8);
    font-size: 22px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.uh-sidebar-close:hover {
    background: rgba(255,255,255,0.15);
    color: white;
}

/* Sidebar Navigation */
.uh-nav {
    padding: 8px 0;
}

.uh-nav-group {
    margin-bottom: 4px;
}

.uh-nav-group-header {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    cursor: pointer;
    transition: background 0.15s;
    user-select: none;
}

.uh-nav-group-header:hover {
    background: #f3f4f6;
}

.uh-nav-group-header i {
    width: 24px;
    text-align: center;
    color: #063970;
    font-size: 16px;
    margin-right: 12px;
}

.uh-nav-group-header span {
    flex: 1;
    font-size: 14px;
    font-weight: 600;
    color: #1f2937;
}

.uh-nav-group-header .uh-chevron {
    font-size: 12px;
    color: #9ca3af;
    transition: transform 0.2s;
    width: auto;
    margin-right: 0;
}

.uh-nav-group.expanded .uh-chevron {
    transform: rotate(90deg);
}

.uh-nav-group-items {
    display: none;
    padding: 2px 0;
}

.uh-nav-group.expanded .uh-nav-group-items {
    display: block;
}

.uh-nav-link {
    display: flex;
    align-items: center;
    padding: 9px 16px 9px 52px;
    color: #4b5563;
    text-decoration: none;
    font-size: 13px;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}

.uh-nav-link:hover {
    background: #f3f4f6;
    color: #063970;
}

.uh-nav-link.active {
    background: #eff6ff;
    color: #063970;
    font-weight: 600;
    border-left-color: #063970;
}

.uh-nav-link i {
    width: 20px;
    text-align: center;
    margin-right: 10px;
    font-size: 13px;
}

/* Direct nav links (Dashboard) */
.uh-nav-direct {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    color: #1f2937;
    text-decoration: none;
    font-size: 14px;
    font-weight: 600;
    transition: background 0.15s;
}

.uh-nav-direct:hover {
    background: #f3f4f6;
}

.uh-nav-direct.active {
    background: #eff6ff;
    color: #063970;
    border-left: 3px solid #063970;
}

.uh-nav-direct i {
    width: 24px;
    text-align: center;
    color: #063970;
    font-size: 16px;
    margin-right: 12px;
}

.uh-nav-divider {
    height: 1px;
    background: #e5e7eb;
    margin: 8px 16px;
}

/* Sidebar Footer */
.uh-sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid #e5e7eb;
    background: #f9fafb;
}

.uh-sidebar-footer .uh-user-info {
    font-size: 12px;
    color: #6b7280;
    margin-bottom: 8px;
}

.uh-sidebar-footer .uh-user-name {
    font-weight: 600;
    color: #1f2937;
    font-size: 14px;
}

.uh-sidebar-logout {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 12px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 8px;
    color: #dc2626;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.uh-sidebar-logout:hover {
    background: #fee2e2;
    border-color: #fca5a5;
}

/* ============================================
   MOBILE RESPONSIVE
   ============================================ */

/* Hide text labels on small screens */
@media (max-width: 600px) {
    .uh-btn .btn-text {
        display: none;
    }
    
    .uh-brand {
        font-size: 16px;
    }
    
    .uh-btn {
        padding: 8px;
    }
    
    .uh-lang-btn {
        padding: 4px 8px;
        font-size: 11px;
    }
}

@media (max-width: 400px) {
    #unified-header {
        padding: 0 8px;
    }

    .uh-right {
        gap: 2px;
    }

    .uh-lang {
        padding: 1px;
    }
}

/* ============================================
   GLOBAL PAGE MOBILE FIXES
   Override inline styles on page-level headers
   ============================================ */

/* Prevent horizontal overflow on body */
body.has-unified-header {
    overflow-x: hidden;
}

/* Container responsive */
@media (max-width: 768px) {
    body.has-unified-header .container {
        padding: 12px !important;
        max-width: 100vw !important;
        box-sizing: border-box !important;
    }

    /* Page-level header responsive - override inline flex styles */
    body.has-unified-header .header {
        flex-direction: column !important;
        gap: 12px !important;
        padding: 16px !important;
        text-align: left !important;
    }
    
    body.has-unified-header .header h1 {
        font-size: 20px !important;
    }
    
    body.has-unified-header .header p {
        font-size: 13px !important;
    }

    /* ── Table → Card transformation on mobile ── */
    body.has-unified-header table:not(.table-keep) {
        border-collapse: collapse;
        display: block !important;
        overflow-x: visible !important;
        white-space: normal !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    /* Hide column headers — labels appear via ::before */
    body.has-unified-header thead:not(.thead-keep) {
        display: none !important;
    }

    body.has-unified-header tbody:not(.tbody-keep) {
        display: block !important;
        width: 100% !important;
    }

    /* Each row = a card */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) {
        display: block !important;
        background: #fff;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        margin-bottom: 12px;
        padding: 4px 0;
        box-shadow: 0 1px 4px rgba(0,0,0,0.06);
        width: 100% !important;
        box-sizing: border-box;
    }

    /* Each cell = a label:value row */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td {
        display: flex !important;
        justify-content: space-between;
        align-items: flex-start;
        gap: 8px;
        padding: 7px 14px !important;
        font-size: 13px !important;
        border-bottom: 1px solid #f1f5f9;
        white-space: normal !important;
        word-break: break-word;
        min-height: 32px;
    }

    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td:last-child {
        border-bottom: none;
    }

    /* Column label shown before value */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label]::before {
        content: attr(data-label);
        font-weight: 600;
        color: #64748b;
        font-size: 11px;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        flex: 0 0 auto;
        min-width: 80px;
        padding-top: 1px;
    }

    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label] {
        justify-content: flex-start;
    }

    /* Value side fills remaining space */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label] > * {
        flex: 1;
    }

    /* Cells without label (e.g. select checkbox col) center themselves */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td:not([data-label]) {
        justify-content: center;
    }

    /* Reset page-level fixed column widths in card mode */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[class*="col-"] {
        width: auto !important;
        max-width: none !important;
        overflow: visible !important;
        text-overflow: unset !important;
    }

    /* Actions cell — 3-column grid of tappable buttons */
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label="Actions"] {
        display: grid !important;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 6px;
        padding: 10px 14px !important;
    }
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label="Actions"]::before {
        grid-column: 1 / -1;
        margin-bottom: 4px;
    }
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label="Actions"] button,
    body.has-unified-header tbody:not(.tbody-keep) tr:not(.pg-load-more-row) td[data-label="Actions"] a {
        width: 100% !important;
        height: 36px !important;
        font-size: 12px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 4px !important;
        padding: 0 6px !important;
        margin: 0 !important;
        border-radius: 6px !important;
        box-sizing: border-box;
    }

    /* Load-more row spans full width */
    body.has-unified-header tbody tr.pg-load-more-row {
        display: block !important;
        text-align: center;
        border-radius: 8px;
        border: 1px dashed #cbd5e1;
        padding: 10px;
        margin-bottom: 8px;
    }

    body.has-unified-header tbody tr.pg-load-more-row td {
        display: block !important;
        border: none !important;
    }

    /* Table containers don't need horizontal scroll anymore */
    body.has-unified-header .list-container,
    body.has-unified-header .table-container {
        overflow-x: visible !important;
    }

    /* Cards/page containers */
    body.has-unified-header .page {
        padding: 0 !important;
        margin: 12px auto !important;
    }
    
    body.has-unified-header .card {
        padding: 12px !important;
        border-radius: 8px !important;
    }

    /* Modals responsive */
    body.has-unified-header .modal > div {
        min-width: auto !important;
        max-width: 95vw !important;
        margin: 10px !important;
    }
}

@media (max-width: 480px) {
    body.has-unified-header .container {
        padding: 8px !important;
    }

    body.has-unified-header .header {
        padding: 12px !important;
        border-radius: 8px !important;
    }
    
    body.has-unified-header .header h1 {
        font-size: 18px !important;
    }

    /* Search inputs - override inline width */
    body.has-unified-header input[type="text"],
    body.has-unified-header #receivingSearch,
    body.has-unified-header #sortingSearch,
    body.has-unified-header #searchInput,
    body.has-unified-header #inventorySearch {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Search bar + button row stacks vertically */
    body.has-unified-header .table-container > div:first-child,
    body.has-unified-header .list-container > div:first-child {
        flex-direction: column !important;
        gap: 8px !important;
    }

    /* Action buttons wrap */
    body.has-unified-header .btn-action,
    body.has-unified-header .action-btn {
        padding: 4px 6px !important;
        font-size: 10px !important;
    }

    /* Toggle groups */
    body.has-unified-header .toggle-group {
        flex-wrap: wrap;
    }
    
    body.has-unified-header .toggle-btn {
        padding: 6px 12px !important;
        font-size: 12px !important;
    }

    /* Two-column packing layout stacks */
    body.has-unified-header #packingSessionContainer {
        grid-template-columns: 1fr !important;
    }
}

/* Tablet - wider sidebar */
@media (min-width: 768px) {
    .uh-sidebar {
        width: 320px;
        left: -320px;
    }
}

/* ============================================
   REMOVE OLD LANGUAGE SWITCHERS & SIDEBARS
   ============================================ */
body.has-unified-header > .language-switcher {
    display: none !important;
}

/* Adjust existing page headers that had language-switcher integration */
body.has-unified-header .header .language-switcher {
    display: none !important;
}

/* Hide old sidebar nav on pages that now use unified header */
body.has-unified-header > .sidebar,
body.has-unified-header .sidebar-nav {
    display: none !important;
}

/* When old sidebar is hidden, let main-content take full width */
body.has-unified-header .main-content {
    margin-left: 0 !important;
    width: 100% !important;
}

/* ============================================
   PWA - iOS Safe Areas
   ============================================ */
@supports (padding-top: env(safe-area-inset-top)) {
    #unified-header {
        padding-top: env(safe-area-inset-top);
        height: calc(56px + env(safe-area-inset-top));
    }
    
    body.has-unified-header {
        padding-top: calc(56px + env(safe-area-inset-top)) !important;
    }
}

/* ============================================
   PWA Install Prompt
   ============================================ */
.uh-install-btn {
    display: none;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 12px;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    color: #1d4ed8;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 8px;
}

.uh-install-btn:hover {
    background: #dbeafe;
}

.uh-install-btn.show {
    display: flex;
}

/* Standalone mode adjustments */
@media (display-mode: standalone) {
    .uh-install-btn {
        display: none !important;
    }
}

/* ========== PORTAL STYLES ========== */

/* Portal badge in sidebar footer */
.uh-portal-badge {
    display: inline-block;
    padding: 4px 10px;
    background: linear-gradient(135deg, #3b82f6, #1d4ed8);
    color: white;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}

/* Client selector dropdown */
.uh-client-selector {
    padding: 12px 20px;
    background: linear-gradient(135deg, #f8fafc, #f1f5f9);
    border-bottom: 1px solid #e2e8f0;
}

.uh-client-selector label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    color: #64748b;
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.uh-client-selector label i {
    margin-right: 6px;
    color: #3b82f6;
}

.uh-client-selector select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background: white;
    font-size: 14px;
    font-weight: 500;
    color: #1e293b;
    cursor: pointer;
    transition: all 0.2s;
}

.uh-client-selector select:hover {
    border-color: #3b82f6;
}

.uh-client-selector select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* Access denied toast */
.uh-access-denied-toast {
    position: fixed;
    top: 70px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    background: linear-gradient(135deg, #fef2f2, #fee2e2);
    border: 1px solid #fecaca;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(239, 68, 68, 0.2);
    z-index: 10001;
    animation: slideDown 0.3s ease-out;
}

.uh-access-denied-toast i.fa-exclamation-triangle {
    color: #dc2626;
    font-size: 18px;
}

.uh-access-denied-toast span {
    color: #991b1b;
    font-weight: 500;
    font-size: 14px;
}

.uh-access-denied-toast button {
    background: none;
    border: none;
    color: #dc2626;
    cursor: pointer;
    padding: 4px;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.uh-access-denied-toast button:hover {
    opacity: 1;
}

.uh-access-denied-toast.fade-out {
    animation: fadeOut 0.3s ease-out forwards;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes fadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

/* Portal-specific sidebar styling */
.uh-sidebar.portal-mode .uh-nav-group-header,
.uh-sidebar.portal-mode .uh-nav-direct {
    font-size: 15px;
}

/* Mobile adjustments for portal elements */
@media (max-width: 480px) {
    .uh-client-selector {
        padding: 10px 16px;
    }
    
    .uh-client-selector select {
        font-size: 13px;
        padding: 8px 10px;
    }
    
    .uh-access-denied-toast {
        left: 10px;
        right: 10px;
        transform: none;
        width: auto;
    }
}

/* ── Global mobile overrides for common page patterns ── */
@media (max-width: 768px) {
    /* Page headers */
    body.has-unified-header .page-header {
        flex-direction: column !important;
        gap: 10px !important;
        padding: 14px 16px !important;
        align-items: flex-start !important;
    }
    body.has-unified-header .page-header-right,
    body.has-unified-header .ip-header .btn-row {
        width: 100% !important;
        flex-wrap: wrap !important;
    }

    /* Toolbars with search and filters */
    body.has-unified-header .toolbar {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        padding: 10px 12px !important;
    }
    body.has-unified-header .toolbar-r {
        margin-left: 0 !important;
        flex-direction: column !important;
        align-items: stretch !important;
    }
    body.has-unified-header .flt,
    body.has-unified-header .srch {
        width: 100% !important;
        min-width: 0 !important;
    }

    /* Summary cards */
    body.has-unified-header .summary-bar {
        gap: 8px !important;
    }
    body.has-unified-header .sc {
        min-width: calc(50% - 8px) !important;
        padding: 10px 14px !important;
    }
    body.has-unified-header .sc-val {
        font-size: 20px !important;
    }

    /* Horizontal-scroll table wrappers */
    body.has-unified-header .tbl-wrap {
        overflow-x: visible !important;
    }

    /* Modals */
    body.has-unified-header .modal-overlay .modal {
        max-width: 95vw !important;
        padding: 16px !important;
        margin: 10px auto !important;
    }
    body.has-unified-header .form-row {
        grid-template-columns: 1fr !important;
    }

    /* Pagination footer */
    body.has-unified-header .footer {
        flex-wrap: wrap !important;
        gap: 4px !important;
        padding: 10px 12px !important;
    }

    /* Common grid layouts */
    body.has-unified-header .row2,
    body.has-unified-header .row2-w,
    body.has-unified-header .row3 {
        grid-template-columns: 1fr !important;
    }
}
