/* Layout fixes and improvements */

/* Prevent horizontal overflow */
html {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

body {
    overflow-x: hidden !important;
    width: 100vw;
    max-width: 100%;
}

/* Fix main content width */
.main-content {
    max-width: calc(100vw - var(--sidebar-width)) !important;
    width: calc(100% - var(--sidebar-width)) !important;
    overflow-x: hidden !important;
    box-sizing: border-box !important;
}

/* Ensure all pages respect viewport */
.page {
    max-width: 100%;
    overflow-x: hidden;
    box-sizing: border-box;
}

/* Fix overview grid to fit viewport */
.overview-grid {
    max-width: 100%;
    box-sizing: border-box;
}

/* Fix KPI grid */
.kpi-grid {
    max-width: 100%;
    box-sizing: border-box;
}

/* Ensure cards don't overflow */
.card {
    max-width: 100%;
    box-sizing: border-box;
    overflow: hidden;
}

.card-body {
    max-width: 100%;
    overflow-x: auto;
}

/* Fix text wrapping */
.card-header h3,
.page-header h2,
.kpi-label,
.change-title,
.watchlist-program,
.change-description,
.watchlist-reason,
.mover-name,
.program-name,
.timeline-title {
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    max-width: 100%;
}

/* Prevent long text from overflowing */
.change-description,
.watchlist-reason,
.digest-item-content p,
.watch-content p {
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
}

/* Fix mover items to prevent overflow */
.mover-item {
    max-width: 100%;
    overflow: hidden;
}

.mover-info {
    min-width: 0;
    flex: 1;
    overflow: hidden;
}

.mover-name,
.mover-oem {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Ensure proper box-sizing inheritance */
*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Sidebar visibility fix */
.sidebar {
    visibility: visible !important;
    display: block !important;
}

.sidebar-nav {
    visibility: visible !important;
    display: block !important;
}

.nav-item {
    visibility: visible !important;
    display: flex !important;
    opacity: 1 !important;
}

/* Ensure sidebar text is visible */
.nav-item span {
    display: inline-block !important;
    visibility: visible !important;
    opacity: 1 !important;
    color: #64748b !important;
}

.nav-item.active span {
    color: #3b82f6 !important;
}

.nav-item:hover span {
    color: #1e293b !important;
}

/* Dark mode sidebar fix */
body.dark-mode .nav-item span {
    color: #cbd5e1 !important;
}

body.dark-mode .nav-item.active span {
    color: #3b82f6 !important;
}

body.dark-mode .nav-item:hover span {
    color: #f1f5f9 !important;
}

/* Prevent content overflow */
.overview-column {
    min-width: 0;
}

/* Better card spacing in grid */
.card {
    width: 100%;
}

/* Ensure charts don't overflow */
canvas {
    max-width: 100%;
    height: auto !important;
}

/* Better table responsiveness */
.table-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.data-table {
    min-width: 100%;
    table-layout: auto;
}

/* Improve mobile view */
@media (max-width: 768px) {
    .main-content {
        padding: var(--spacing-md);
    }
    
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--spacing-md);
    }
    
    .kpi-card {
        min-height: 120px;
    }
}

/* Prevent horizontal scroll on small screens */
@media (max-width: 640px) {
    .overview-grid {
        grid-template-columns: 1fr !important;
    }
    
    .kpi-grid {
        grid-template-columns: 1fr !important;
    }
}

/* Better chart containers */
.chart-card .card-body,
.chart-card-large .card-body {
    position: relative;
    min-height: 300px;
}

/* Fix any flex/grid conflicts */
.overview-column > * {
    flex-shrink: 0;
}

/* Improve spacing consistency */
.card + .card {
    margin-top: var(--spacing-lg);
}

/* Better mover item layout */
.mover-item {
    align-items: center;
}

.mini-chart {
    width: 80px;
    flex-shrink: 0;
}

/* Ensure proper text wrapping */
.card-header h3,
.page-header h2 {
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Fix any z-index stacking issues */
.top-nav {
    z-index: 1000;
}

.sidebar {
    z-index: 900;
}

.right-drawer {
    z-index: 1100;
    /* Ensure drawer is ALWAYS hidden unless explicitly opened */
    right: -480px !important;
    left: auto !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
    transition: right 0.3s ease-in-out, opacity 0.3s ease-in-out, visibility 0.3s ease-in-out !important;
}

.right-drawer.open {
    right: 0 !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
}

/* Prevent drawer from covering main content when closed */
.main-content {
    position: relative;
    z-index: 1;
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Better focus states for accessibility */
button:focus,
a:focus,
input:focus,
select:focus {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
}

/* Loading states */
.icon-btn .fa-spin {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}
