/* Mobile-first approach */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Light mode defaults */
html {
    overflow-y: scroll !important; /* Force scrollbar on html element */
    height: 100%;
}

body {
    background-color: #ffffff;
    color: #1a1a1a;
    transition: all 0.3s ease;
    -webkit-text-size-adjust: 100%;
    width: 100%;
    overflow-x: hidden;
    min-height: 100vh;
    /* Force scrollbar to always be present to prevent layout shift */
    overflow-y: scroll !important;
}

/* Responsive container */
.uk-container {
    padding: 0.5rem;
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    overflow-x: hidden;
}

/* Ensure content stays within viewport */
.homepage-grid,
#charts-container,
.uk-card {
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    padding: 0.5rem;
    box-sizing: border-box;
}

/* Responsive grid */
@media (max-width: 768px) {
    .grid {
        grid-template-columns: 1fr !important;
    }

    .uk-card {
        margin-bottom: 0.5rem;
    }

    .uk-button {
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
    }

    input[type="search"],
    input[type="number"] {
        width: 100% !important;
        margin-bottom: 0.5rem;
    }
}

/* Responsive controls */
@media (max-width: 640px) {
    .controls-container {
        flex-direction: column;
    }

    .controls-container > div {
        width: 100%;
        margin-bottom: 0.5rem;
    }

    .uk-dropdown {
        width: 100%;
        max-width: none;
    }
}

/* Mobile modal optimizations */
@media (max-width: 768px) {
    .uk-modal-full .uk-modal-dialog {
        padding: 1rem;
    }

    .uk-modal-title {
        font-size: 1.25rem;
        line-height: 1.3;
        margin-bottom: 1rem;
    }

    /* Ensure charts are touch-friendly on mobile */
    .plotly .modebar {
        position: absolute;
        top: 10px;
        right: 10px;
    }

    .plotly .modebar-btn {
        width: 32px;
        height: 32px;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    .chart-expand-btn {
        opacity: 1 !important;
        transform: none !important;
    }

    .chart-expand-btn:active {
        transform: scale(0.95);
        background: rgba(59, 130, 246, 0.1);
    }
}

.loading-indicator {
    display: none;
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 1000;
}

.loading-indicator .htmx-indicator {
    padding: 0.5rem 1rem;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.loading-indicator .htmx-indicator.htmx-request {
    display: inline-block;
}

.top-nav {
    border-bottom: 1px solid #e5e7eb;
    background: #f8fafc;
}

.top-nav li {
    margin: 0;
}

.top-nav li a {
    font-weight: 500;
    color: #4b5563;
    transition: all 0.2s;
}

.top-nav li a:hover {
    color: #1e40af;
    background: #f1f5f9;
}

.top-nav li.uk-active a {
    color: #1e40af;
    border-bottom: 2px solid #1e40af;
}

/* Dark mode styles */
body.dark-mode {
    background-color: #1a1a1a;
    color: #e5e7eb;
}

body.dark-mode .uk-card {
    background-color: #262626;
    border-color: #404040;
    color: #e5e7eb;
}

body.dark-mode .uk-card-header {
    border-color: #404040;
}

body.dark-mode .top-nav {
    background: #262626;
    border-color: #404040;
}

body.dark-mode .top-nav li a {
    color: #e5e7eb;
}

body.dark-mode .top-nav li a:hover {
    color: #60a5fa;
    background: #333333;
}

body.dark-mode .top-nav li.uk-active a {
    color: #60a5fa;
    border-color: #60a5fa;
}

body.dark-mode .uk-input,
body.dark-mode .uk-select,
body.dark-mode .uk-textarea,
body.dark-mode .uk-button-secondary {
    background-color: #333333;
    border-color: #404040;
    color: #e5e7eb;
}

body.dark-mode .uk-dropdown {
    background-color: #262626;
    border-color: #404040;
    color: #e5e7eb;
}

body.dark-mode .uk-dropdown li a {
    color: #e5e7eb;
}

body.dark-mode .uk-dropdown li a:hover {
    background-color: #333333;
}

.uk-dropdown {
    min-width: 200px;
    max-width: fit-content;
    width: auto;
    margin-top: 5px;
}

body.dark-mode .loading-indicator .htmx-indicator {
    background: #262626;
    color: #e5e7eb;
    box-shadow: 0 2px 4px rgba(255,255,255,0.1);
}

/* Ensure text colors are readable in dark mode */
body.dark-mode h1,
body.dark-mode h2,
body.dark-mode h3,
body.dark-mode h4,
body.dark-mode h5,
body.dark-mode h6,
body.dark-mode p {
    color: #e5e7eb;
}

body.dark-mode .text-muted-foreground,
body.dark-mode .uk-text-muted {
    color: #9ca3af;
}

/* Update dark mode button styles */
body.dark-mode .uk-button-default,
body.dark-mode .uk-button-secondary {
    background-color: #1f1f1f;
    color: #e5e7eb;
    border-color: #2d2d2d;
}

body.dark-mode .uk-button-default:hover,
body.dark-mode .uk-button-secondary:hover {
    background-color: #2d2d2d;
    color: #ffffff;
    border-color: #404040;
}

body.dark-mode .uk-button-primary {
    background-color: #0f2d66;
    color: #ffffff;
    border: none;
}

body.dark-mode .uk-button-primary:hover {
    background-color: #1a3f80;
}

/* Switch styles */
.uk-toggle-switch {
    appearance: none;
    position: relative;
    width: 32px;
    height: 18px;
    border-radius: 9px;
    background-color: #e5e7eb;
    cursor: pointer;
    transition: all 0.3s ease;
}

.uk-toggle-switch:checked {
    background-color: #1e40af;
}

.uk-toggle-switch::before {
    content: '';
    position: absolute;
    left: 2px;
    top: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background-color: white;
    transition: transform 0.3s ease;
}

.uk-toggle-switch:checked::before {
    transform: translateX(14px);
}

/* Dark mode switch styles */
body.dark-mode .uk-toggle-switch {
    background-color: #4b5563;
}

body.dark-mode .uk-toggle-switch:checked {
    background-color: #60a5fa;
}

body.dark-mode .uk-toggle-switch::before {
    background-color: #e5e7eb;
}

/* Homepage and charts grid layout */
.homepage-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 0.75rem;
    padding: 0.75rem;
    width: 100%;
    box-sizing: border-box;
    margin: 0 auto;
}

/* Ensure consistent chart sizing */
.homepage-grid .uk-card,
#charts-container .uk-card {
    width: 100%;
    height: 100%;
    margin-bottom: 0;
    overflow: hidden;
}

/* Ensure cards have consistent width on homepage */
.homepage-grid .uk-card {
    width: 100%;
    height: 100%;
    margin-bottom: 0;
}

/* Chart placeholder styling to reduce layout shift */
#charts-container .uk-card {
    min-height: 400px; /* Minimum height for chart cards */
}

/* Loading placeholder styling */
.uk-card:has([hx-trigger="load"]) {
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Keep chart columns readable on phones, including after settings changes. */
@media (max-width: 640px) {
    #charts-container { grid-template-columns: minmax(0, 1fr); }
    .homepage-grid { grid-template-columns: minmax(0, 1fr); }
}
