/* ==========================================================================
   DESIGN SYSTEM - CSS VARIABLES
   ========================================================================== */
:root {
    --font-primary: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    
    /* Transitions */
    --transition-fast: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    
    /* Box Shadows */
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 8px 20px rgba(0, 0, 0, 0.15);
    --shadow-lg: 0 12px 30px rgba(0, 0, 0, 0.25);
    
    /* Border Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-full: 9999px;
}

/* Dark Theme Colors (Default) */
.dark-theme {
    --bg-base: hsl(224, 71%, 4%);
    --bg-surface: hsl(222, 47%, 11%);
    --bg-surface-elevated: hsl(223, 47%, 16%);
    --bg-sidebar: hsl(222, 47%, 8%);
    
    --text-primary: hsl(210, 40%, 98%);
    --text-secondary: hsl(215, 20%, 75%);
    --text-muted: hsl(215, 16%, 50%);
    
    --primary: hsl(250, 84%, 63%);
    --primary-hover: hsl(250, 95%, 70%);
    --primary-glow: rgba(99, 102, 241, 0.25);
    --primary-gradient: linear-gradient(135deg, hsl(250, 84%, 63%) 0%, hsl(265, 84%, 63%) 100%);
    
    --success: hsl(142, 72%, 40%);
    --success-bg: rgba(16, 185, 129, 0.15);
    --success-border: rgba(16, 185, 129, 0.3);
    
    --warning: hsl(32, 95%, 48%);
    --warning-bg: rgba(245, 158, 11, 0.15);
    --warning-border: rgba(245, 158, 11, 0.3);
    
    --danger: hsl(346, 84%, 61%);
    --danger-bg: rgba(239, 68, 68, 0.15);
    --danger-border: rgba(239, 68, 68, 0.3);
    
    --border-color: rgba(255, 255, 255, 0.08);
    --glass-border: rgba(255, 255, 255, 0.05);
    --glass-bg: rgba(15, 23, 42, 0.6);
}

/* Light Theme Colors */
.light-theme {
    --bg-base: hsl(210, 40%, 96%);
    --bg-surface: hsl(0, 0%, 100%);
    --bg-surface-elevated: hsl(210, 40%, 98%);
    --bg-sidebar: hsl(210, 30%, 93%);
    
    --text-primary: hsl(222, 47%, 11%);
    --text-secondary: hsl(215, 25%, 35%);
    --text-muted: hsl(215, 16%, 55%);
    
    --primary: hsl(250, 84%, 55%);
    --primary-hover: hsl(250, 84%, 48%);
    --primary-glow: rgba(99, 102, 241, 0.15);
    --primary-gradient: linear-gradient(135deg, hsl(250, 84%, 55%) 0%, hsl(265, 84%, 55%) 100%);
    
    --success: hsl(142, 76%, 36%);
    --success-bg: rgba(16, 185, 129, 0.1);
    --success-border: rgba(16, 185, 129, 0.2);
    
    --warning: hsl(32, 95%, 44%);
    --warning-bg: rgba(245, 158, 11, 0.1);
    --warning-border: rgba(245, 158, 11, 0.2);
    
    --danger: hsl(346, 84%, 53%);
    --danger-bg: rgba(239, 68, 68, 0.1);
    --danger-border: rgba(239, 68, 68, 0.2);
    
    --border-color: rgba(0, 0, 0, 0.08);
    --glass-border: rgba(0, 0, 0, 0.04);
    --glass-bg: rgba(255, 255, 255, 0.7);
}

/* ==========================================================================
   RESET & GLOBAL
   ========================================================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-primary);
    background-color: var(--bg-base);
    color: var(--text-primary);
    transition: background-color var(--transition-normal), color var(--transition-normal);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: 600;
    letter-spacing: -0.02em;
}

a {
    color: inherit;
    text-decoration: none;
}

.hidden {
    display: none !important;
}

/* ==========================================================================
   BUTTONS & CONTROLS
   ========================================================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    font-family: var(--font-primary);
    font-size: 0.95rem;
    font-weight: 500;
    border-radius: var(--radius-md);
    border: none;
    cursor: pointer;
    transition: all var(--transition-fast);
    gap: 8px;
}

.btn-primary {
    background: var(--primary-gradient);
    color: white;
    box-shadow: 0 4px 14px var(--primary-glow);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--primary-glow);
    filter: brightness(1.1);
}

.btn-secondary {
    background-color: var(--bg-surface-elevated);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--border-color);
    transform: translateY(-1px);
}

.btn-danger {
    background-color: var(--danger-bg);
    color: var(--danger);
    border: 1px solid var(--danger-border);
}

.btn-danger:hover {
    background-color: var(--danger);
    color: white;
}

.btn-block {
    display: flex;
    width: 100%;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.85rem;
    border-radius: var(--radius-sm);
}

/* Form Controls */
.form-group {
    margin-bottom: 18px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-group label {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-input {
    width: 100%;
    padding: 10px 14px;
    font-family: var(--font-primary);
    font-size: 0.95rem;
    background-color: var(--bg-surface-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    transition: border-color var(--transition-fast);
    outline: none;
}

.form-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-glow);
}

.checkbox-group {
    flex-direction: row;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.checkbox-group input {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* ==========================================================================
   LOGIN SCREEN
   ========================================================================== */
.login-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--bg-base);
}

.login-card {
    width: 100%;
    max-width: 420px;
    padding: 40px;
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    text-align: center;
    animation: slideUp var(--transition-normal);
}

.logo-container {
    display: flex;
    justify-content: center;
    margin-bottom: 24px;
    color: var(--primary);
}

.logo-icon {
    width: 64px;
    height: 64px;
}

.login-card h1 {
    font-size: 2rem;
    margin-bottom: 10px;
}

.login-card p {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-bottom: 30px;
    line-height: 1.5;
}

/* ==========================================================================
   APP SHELL / LAYOUT
   ========================================================================== */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Sidebar styling */
.sidebar {
    width: 260px;
    background-color: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-brand {
    padding: 24px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border-color);
}

.brand-icon {
    width: 28px;
    height: 28px;
}

.brand-text {
    font-size: 1.15rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--text-primary) 30%, var(--text-secondary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.sidebar-nav {
    flex-grow: 1;
    padding: 24px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    color: var(--text-secondary);
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 0.95rem;
    transition: all var(--transition-fast);
}

.nav-item:hover {
    color: var(--text-primary);
    background-color: rgba(255, 255, 255, 0.04);
}

.nav-item.active {
    color: white;
    background: var(--primary-gradient);
    box-shadow: 0 4px 12px var(--primary-glow);
}

.nav-icon {
    width: 20px;
    height: 20px;
}

.sidebar-user {
    padding: 20px 16px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-full);
    background-color: var(--primary);
    color: white;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    box-shadow: var(--shadow-sm);
}

.user-info {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    min-width: 0;
}

.user-name {
    font-size: 0.9rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-role {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.btn-logout {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: color var(--transition-fast);
}

.btn-logout:hover {
    color: var(--danger);
}

/* Main Content Area */
.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background-color: var(--bg-base);
}

.main-header {
    height: 70px;
    padding: 0 32px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sun-icon, .moon-icon {
    width: 20px;
    height: 20px;
}

.content-scroll {
    flex-grow: 1;
    padding: 32px;
    overflow-y: auto;
    height: calc(100vh - 70px);
}

/* ==========================================================================
   TABS & PANELS
   ========================================================================== */
.tab-panel {
    display: none;
    animation: fadeIn var(--transition-normal);
}

.tab-panel.active {
    display: block;
}

/* ==========================================================================
   DASHBOARD / STATS
   ========================================================================== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 24px;
}

.stat-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    padding: 24px;
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    gap: 20px;
    box-shadow: var(--shadow-sm);
    transition: transform var(--transition-fast);
}

.stat-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.stat-icon {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon svg {
    width: 26px;
    height: 26px;
}

.bg-blue-trans { background-color: rgba(59, 130, 246, 0.1); }
.bg-green-trans { background-color: rgba(16, 185, 129, 0.1); }
.bg-red-trans { background-color: rgba(239, 68, 68, 0.1); }
.bg-purple-trans { background-color: rgba(139, 92, 246, 0.1); }

.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-purple { color: hsl(263, 90%, 68%); }

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

.stat-label {
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.stat-value {
    font-size: 1.8rem;
    font-weight: 700;
}

/* Alerts section */
.alerts-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-top: 16px;
}

.alert-item {
    background-color: var(--danger-bg);
    border: 1px solid var(--danger-border);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    animation: slideIn var(--transition-fast);
}

.alert-item-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.alert-title {
    font-weight: 600;
    font-size: 0.95rem;
}

.alert-subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.alert-empty-state {
    padding: 32px;
    text-align: center;
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

/* ==========================================================================
   DEVICES GRID & CARDS
   ========================================================================== */
.action-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.filter-controls {
    display: flex;
    gap: 12px;
}

.devices-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 24px;
}

.device-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    box-shadow: var(--shadow-sm);
    transition: all var(--transition-fast);
    position: relative;
    overflow: hidden;
}

.device-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
    border-color: var(--primary-glow);
}

.device-card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.device-title {
    font-weight: 600;
    font-size: 1.1rem;
    line-height: 1.3;
}

.device-meta {
    font-size: 0.78rem;
    color: var(--text-muted);
}

.device-badge {
    padding: 3px 8px;
    border-radius: var(--radius-full);
    font-size: 0.72rem;
    font-weight: 600;
    background-color: var(--bg-surface-elevated);
}

.device-badge.ccu3 {
    border: 1px solid var(--primary-glow);
    color: var(--primary-hover);
}

.device-badge.custom {
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
}

/* Level Indicator */
.device-level-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.device-level-text {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.85rem;
}

.level-percentage {
    font-weight: 700;
}

.level-bar-bg {
    width: 100%;
    height: 8px;
    background-color: var(--bg-surface-elevated);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.level-bar-fill {
    height: 100%;
    border-radius: var(--radius-full);
    transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Levels color fills */
.level-good {
    background-color: var(--success);
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.4);
}
.level-medium {
    background-color: var(--warning);
    box-shadow: 0 0 8px rgba(245, 158, 11, 0.4);
}
.level-low {
    background-color: var(--danger);
    box-shadow: 0 0 8px rgba(239, 68, 68, 0.4);
}

.device-card-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
}

/* ==========================================================================
   TABLES (Changes, Settings)
   ========================================================================== */
.table-container {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
    font-size: 0.92rem;
}

.data-table th, .data-table td {
    padding: 14px 20px;
    border-bottom: 1px solid var(--border-color);
}

.data-table th {
    background-color: var(--bg-sidebar);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.data-table tbody tr {
    transition: background-color var(--transition-fast);
}

.data-table tbody tr:hover {
    background-color: rgba(255, 255, 255, 0.02);
}

.data-table tbody tr:last-child td {
    border-bottom: none;
}

.btn-icon {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.btn-icon:hover {
    color: var(--text-primary);
    background-color: var(--bg-surface-elevated);
}

.btn-icon.delete:hover {
    color: var(--danger);
    background-color: var(--danger-bg);
}

/* ==========================================================================
   ANALYTICS & SETTINGS CHARTS
   ========================================================================== */
.analytics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 24px;
}

.chart-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 24px;
    box-shadow: var(--shadow-sm);
}

.chart-card h4 {
    margin-bottom: 20px;
    color: var(--text-secondary);
}

.chart-container {
    position: relative;
    height: 320px;
    width: 100%;
}

.settings-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
}

.settings-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 24px;
    display: flex;
    flex-direction: column;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

@media (max-width: 992px) {
    .settings-grid {
        grid-template-columns: 1fr;
    }
}

/* ==========================================================================
   MODALS
   ========================================================================== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1010;
    background-color: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.modal.active {
    display: flex;
    opacity: 1;
}

.modal-content {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 500px;
    padding: 32px;
    box-shadow: var(--shadow-lg);
    position: relative;
    transform: scale(0.95);
    transition: transform var(--transition-fast);
    animation: slideUp var(--transition-fast);
}

.modal.active .modal-content {
    transform: scale(1);
}

.close-modal {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 24px;
    cursor: pointer;
    color: var(--text-muted);
    transition: color var(--transition-fast);
}

.close-modal:hover {
    color: var(--text-primary);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 24px;
}

/* ==========================================================================
   ANIMATIONS & UTILITIES
   ========================================================================== */
@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-10px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

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

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: .5; }
}

.animate-spin-slow {
    animation: spin 8s linear infinite;
}

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

.margin-top-sm { margin-top: 12px; }
.margin-top-md { margin-top: 20px; }
.margin-top-lg { margin-top: 32px; }

/* Read only restriction utility */
.rw-only {
    display: none !important;
}

body.user-rw .rw-only {
    display: inline-flex !important;
}
tr.rw-only, div.rw-only.stat-card {
    display: flex !important;
}
button.rw-only {
    display: inline-flex !important;
}

/* Responsive Overrides */
@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }
    .sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        height: auto;
    }
    .sidebar-nav {
        flex-direction: row;
        overflow-x: auto;
        padding: 10px 16px;
        gap: 6px;
    }
    .nav-item {
        padding: 8px 12px;
        font-size: 0.85rem;
        white-space: nowrap;
    }
    .sidebar-user {
        display: none; /* Hide profile in sidebar on small mobile, could move to header */
    }
    .content-scroll {
        padding: 16px;
        height: calc(100vh - 120px);
    }
}
