/* Design System & Variables */
:root {
    --bg-dark: #0f1115;
    --bg-card: #181b21;
    --bg-card-hover: #1e222b;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --border: rgba(255, 255, 255, 0.08);
    --error: #ef4444;
    --glass-bg: rgba(15, 17, 21, 0.7);
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.3);
    --radius-md: 8px;
    --radius-lg: 12px;
}

body.light-mode {
    --bg-dark: #f1f5f9;
    --bg-card: #ffffff;
    --bg-card-hover: #f8fafc;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --border: rgba(0, 0, 0, 0.1);
    --glass-bg: rgba(255, 255, 255, 0.7);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
}

/* Utilities */
.hidden {
    display: none !important;
}

/* Overlay & Auth */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    transition: opacity 0.3s ease;
}

.auth-card {
    background: var(--bg-card);
    padding: 2.5rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    width: 100%;
    max-width: 400px;
    text-align: center;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.auth-icon {
    width: 48px;
    height: 48px;
    color: var(--accent);
    margin-bottom: 1rem;
}

.auth-card h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.auth-card p {
    color: var(--text-secondary);
    font-size: 0.875rem;
    margin-bottom: 1.5rem;
}

form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

input {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-family: inherit;
    transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

button {
    background: var(--accent);
    color: white;
    border: none;
    padding: 0.75rem 1rem;
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 1rem;
    cursor: pointer;
    font-family: inherit;
    transition: background-color 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

button:hover {
    background: var(--accent-hover);
}

button:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
}

.btn-outline:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
}

.error-msg {
    color: var(--error);
    font-size: 0.875rem;
    text-align: left;
}

/* App Layout */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

h1 {
    font-size: 1.75rem;
    font-weight: 700;
    letter-spacing: -0.025em;
}

.badge {
    background: rgba(16, 185, 129, 0.1);
    color: #34d399;
    padding: 0.25rem 0.6rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border: 1px solid rgba(52, 211, 153, 0.2);
}

/* Stats Banner */
.stats-banner {
    display: flex;
    gap: 2rem;
    margin-bottom: 2rem;
    background: var(--bg-card);
    padding: 1.5rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
}

.stat-item {
    display: flex;
    flex-direction: column;
}

.stat-label {
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 600;
}

/* Live Dashboard */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.status-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 1rem;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.status-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
    border-color: rgba(255, 255, 255, 0.15);
}

.status-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.status-title {
    font-weight: 600;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.status-indicator {
    width: 14px;
    height: 14px;
    border-radius: 50%;
}

.status-indicator.healthy {
    background: #10b981;
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.5);
}

.status-indicator.warning {
    background: #f59e0b;
    box-shadow: 0 0 8px rgba(245, 158, 11, 0.5);
}

.status-indicator.offline {
    background: #ef4444;
    box-shadow: 0 0 8px rgba(239, 68, 68, 0.5);
}

.status-metrics {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.metric {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.metric-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
}

.metric-value {
    font-size: 1rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.metric-icon {
    color: var(--text-secondary);
}

/* Analytics Dashboard */
.analytics-dashboard {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.chart-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    height: 300px;
    /* Fixed height for Chart.js */
    position: relative;
    box-shadow: var(--shadow-sm);
}

@media (max-width: 768px) {
    .analytics-dashboard {
        grid-template-columns: 1fr;
    }
}

/* Toolbar */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 1rem 1.5rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    gap: 1rem;
    flex-wrap: wrap;
}

.toolbar-left,
.toolbar-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

#bulk-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border-left: 1px solid var(--border);
    padding-left: 1rem;
}

#selected-count {
    font-size: 0.875rem;
    color: var(--accent);
    font-weight: 500;
}

.sort-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.date-filters {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border-right: 1px solid var(--border);
    padding-right: 1rem;
}

.sort-dropdown {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-md);
    font-family: inherit;
    cursor: pointer;
}

.sort-dropdown option {
    background: var(--bg-card);
    color: var(--text-primary);
}

/* Gallery Masonry Grid */
.gallery {
    column-count: 3;
    column-gap: 1.5rem;
    display: block;
    width: 100%;
}

@media (max-width: 1100px) {
    .gallery {
        column-count: 2;
    }
}

@media (max-width: 700px) {
    .gallery {
        column-count: 1;
    }
}

.image-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
    position: relative;
    break-inside: avoid;
    margin-bottom: 1.5rem;
    display: inline-block;
    width: 100%;
}

.image-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
    border-color: rgba(255, 255, 255, 0.15);
}

.image-wrapper {
    position: relative;
    width: 100%;
    padding-top: 75%;
    /* 4:3 Aspect Ratio */
    background: rgba(0, 0, 0, 0.3);
}

/* Image Actions Overlay */
.actions-menu {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    display: flex;
    gap: 0.5rem;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.2s ease-in-out;
}

.select-checkbox {
    position: absolute;
    top: 0.75rem;
    left: 0.75rem;
    width: 20px;
    height: 20px;
    z-index: 15;
    cursor: pointer;
    accent-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.5);
    border-radius: 4px;
}

/* Show actions on desktop hover or always on touch devices */
.image-card:hover .actions-menu,
.image-card:focus-within .actions-menu {
    opacity: 1;
}

@media (hover: none) {
    .actions-menu {
        opacity: 1;
        /* Always visible on mobile devices */
    }
}

.action-btn {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 32px;
    height: 32px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
    padding: 0;
}

.action-btn:hover {
    background: var(--accent);
    transform: scale(1.05);
}

.action-btn.delete-btn:hover {
    background: var(--error);
}

.image-card img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.5s ease;
}

.image-card:hover .image-wrapper img {
    transform: scale(1.05);
}

.ai-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    margin-bottom: 0.5rem;
}

.badge-tag {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    color: white;
    font-size: 0.7rem;
    padding: 0.15rem 0.5rem;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    font-weight: 500;
}

body.light-mode .badge-tag {
    background: rgba(255, 255, 255, 0.85);
    color: var(--text-primary);
    border-color: rgba(0, 0, 0, 0.1);
}

/* Fallback for browsers that don't support AVIF */
.img-fallback-msg {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.card-metadata {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding-top: 0.5rem;
    border-top: 1px solid rgba(255, 255, 255, 0.05);
}

body.light-mode .card-metadata {
    border-top-color: rgba(0, 0, 0, 0.05);
}

.meta-row {
    display: flex;
    align-items: center;
    width: 100%;
    overflow: hidden;
}

.meta-split {
    justify-content: space-between;
}

.card-filename {
    font-size: 0.75rem;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-right: 0.5rem;
}

.mule-id {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.mule-id::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #34d399;
    border-radius: 50%;
}

.timestamp {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.file-size {
    font-size: 0.75rem;
    color: var(--text-secondary);
    background: var(--bg-body);
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
}

/* Loading States */
.center-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4rem 1rem;
    color: var(--text-secondary);
    gap: 1rem;
}

.empty-icon {
    color: rgba(255, 255, 255, 0.1);
    margin-bottom: 1rem;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.loading-pulse {
    animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

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

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

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

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: .5;
    }
}

@media (max-width: 768px) {

    /* Maximize horizontal screen real estate on phones */
    .container {
        padding: 0.75rem;
    }

    /* Stack header and allow buttons to wrap */
    .app-header {
        flex-direction: column;
        align-items: stretch;
        gap: 1.5rem;
        padding-bottom: 1rem;
    }

    .header-left {
        justify-content: space-between;
    }

    .header-right {
        flex-wrap: wrap;
        justify-content: space-evenly;
        gap: 0.75rem;
    }

    .header-right button {
        flex: 1 1 calc(33% - 0.75rem);
        padding: 0.75rem 0.5rem;
        font-size: 0.875rem;
    }

    /* Stack the stats banner */
    .stats-banner {
        flex-direction: column;
        gap: 1rem;
        padding: 1rem;
    }

    /* Entire toolbar converts to a vertical stack for deep scrolling */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 1.5rem;
        padding: 1.25rem 1rem;
    }

    .toolbar-left,
    .toolbar-right,
    .date-filters {
        flex-direction: column;
        align-items: stretch;
        width: 100%;
        gap: 1rem;
    }

    /* Ensure dropdowns and inputs are easy to tap and read */
    .sort-dropdown,
    input[type="date"] {
        width: 100%;
        height: 48px;
        /* Material Design recommended touch target height */
        font-size: 1rem;
    }

    .date-filters {
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding-right: 0;
        padding-bottom: 1.5rem;
    }

    .date-filters span {
        text-align: center;
        margin: -0.5rem 0;
    }

    #bulk-actions {
        border-left: none;
        border-top: 1px solid var(--border);
        padding-left: 0;
        padding-top: 1rem;
        flex-direction: column;
        align-items: stretch;
        width: 100%;
    }

    /* Fallback to single column for images on very small screens, 
       otherwise rely on minmax(300px) which usually becomes 1 col on phones anyway */
    .gallery {
        gap: 1rem;
    }

    /* Dramatically increase touch target size for Android standard */
    .action-btn {
        width: 48px;
        height: 48px;
    }

    /* Ensure action buttons are always fully visible and easy to tap on mobile */
    .actions-menu {
        opacity: 1;
        gap: 0.75rem;
        top: 0.75rem;
        right: 0.75rem;
    }

    .select-checkbox {
        width: 32px;
        height: 32px;
        top: 1rem;
        left: 1rem;
    }
}