/* ============================================
   KRM IoT Platform - Main Stylesheet
   ============================================ */

:root {
    --primary-color: #2196F3;
    --primary-dark: #1976D2;
    --secondary-color: #FF9800;
    --success-color: #4CAF50;
    --warning-color: #FFC107;
    --danger-color: #f44336;
    --text-primary: #333;
    --text-secondary: #666;
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --bg-dark: #1a1a2e;
    --bg-darker: #16213e;
    --border-color: #ddd;
    --shadow: 0 2px 10px rgba(0,0,0,0.1);
    --sidebar-width: 280px;
    --topbar-height: 60px;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    overflow-x: hidden;
}

/* ============================================
   LOGIN PAGE
   ============================================ */

.page {
    display: none;
    width: 100%;
    height: 100vh;
}

.page.active {
    display: block;
}

#login-page {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--bg-dark) 0%, var(--bg-darker) 100%);
    overflow: hidden;
}

.login-container {
    position: relative;
    z-index: 10;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    overflow: hidden;
    max-width: 450px;
    width: 90%;
    animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(50px); }
    to { opacity: 1; transform: translateY(0); }
}

.login-header {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    padding: 40px 30px;
    text-align: center;
    color: white;
}

.banner h1 {
    font-size: 2.5rem;
    font-weight: 700;
    letter-spacing: 2px;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}

.tagline {
    font-size: 1rem;
    opacity: 0.9;
    letter-spacing: 1px;
}

.login-form {
    padding: 40px 30px;
}

.login-form h2 {
    margin-bottom: 30px;
    color: var(--text-primary);
    font-size: 1.5rem;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: var(--text-secondary);
    font-weight: 500;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid var(--border-color);
    border-radius: 10px;
    font-size: 1rem;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.2);
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
    width: 100%;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 20px rgba(33, 150, 243, 0.4);
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background: var(--border-color);
}

.error-message {
    color: var(--danger-color);
    margin-top: 15px;
    text-align: center;
    min-height: 20px;
}

/* IoT Background Animation */
.iot-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: 1;
}

.bg-element {
    position: absolute;
    opacity: 0.1;
    animation: float 20s infinite ease-in-out;
}

@keyframes float {
    0%, 100% { transform: translate(0, 0) rotate(0deg); }
    25% { transform: translate(30px, -30px) rotate(90deg); }
    50% { transform: translate(-20px, 20px) rotate(180deg); }
    75% { transform: translate(20px, 30px) rotate(270deg); }
}

.bg-circle {
    border-radius: 50%;
    background: var(--primary-color);
}

.bg-square {
    background: var(--secondary-color);
}

.bg-triangle {
    width: 0 !important;
    height: 0 !important;
    background: transparent !important;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 86px solid var(--success-color);
}

.bg-hexagon {
    background: #9C27B0;
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}

.bg-dots {
    background-image: radial-gradient(circle, var(--primary-color) 2px, transparent 2px);
    background-size: 20px 20px;
}

.bg-wave {
    background: linear-gradient(45deg, transparent 48%, var(--primary-color) 49%, var(--primary-color) 51%, transparent 52%);
    background-size: 30px 30px;
}

.bg-1 { width: 100px; height: 100px; top: 10%; left: 10%; animation-delay: 0s; }
.bg-2 { width: 150px; height: 150px; top: 60%; left: 80%; animation-delay: -5s; }
.bg-3 { width: 80px; height: 80px; top: 30%; left: 70%; animation-delay: -10s; }
.bg-4 { width: 120px; height: 120px; top: 70%; left: 20%; animation-delay: -15s; }
.bg-5 { top: 20%; left: 40%; animation-delay: -3s; }
.bg-6 { width: 100px; height: 100px; top: 80%; left: 50%; animation-delay: -8s; }
.bg-7 { width: 200px; height: 200px; top: 5%; left: 60%; animation-delay: -12s; }
.bg-8 { width: 300px; height: 100px; bottom: 10%; right: 10%; animation-delay: -7s; }

/* ============================================
   DASHBOARD PAGE
   ============================================ */

#dashboard-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    height: var(--topbar-height);
    background: linear-gradient(135deg, var(--bg-dark), var(--bg-darker));
    color: white;
    box-shadow: var(--shadow);
    z-index: 100;
}

.top-bar-left {
    display: flex;
    align-items: center;
    gap: 15px;
}

#sidebar-toggle {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 5px 10px;
}

.logo {
    font-size: 1.3rem;
    font-weight: 700;
    letter-spacing: 1px;
}

.top-bar-right {
    display: flex;
    align-items: center;
    gap: 20px;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 15px;
    background: rgba(255,255,255,0.1);
    border-radius: 20px;
    font-size: 0.85rem;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--warning-color);
    animation: pulse 2s infinite;
}

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

.status-indicator.connected .status-dot {
    background: var(--success-color);
}

.status-indicator.disconnected .status-dot {
    background: var(--danger-color);
    animation: none;
}

.last-update {
    font-size: 0.75rem;
    opacity: 0.8;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

#username-display {
    font-weight: 500;
}

/* ============================================
   MAIN CONTENT LAYOUT
   ============================================ */

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: var(--sidebar-width);
    background: white;
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    transition: transform 0.3s, width 0.3s;
    display: flex;
    flex-direction: column;
}

.sidebar.collapsed {
    width: 0;
    transform: translateX(-100%);
}

.sidebar-nav {
    padding: 20px 0;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 15px 25px;
    color: var(--text-primary);
    text-decoration: none;
    transition: all 0.3s;
    border-left: 3px solid transparent;
}

.nav-link:hover {
    background: var(--bg-secondary);
    border-left-color: var(--primary-color);
}

.nav-link.active {
    background: rgba(33, 150, 243, 0.1);
    border-left-color: var(--primary-color);
    color: var(--primary-color);
    font-weight: 600;
}

.nav-icon {
    font-size: 1.2rem;
}

.badge {
    margin-left: auto;
    background: var(--danger-color);
    color: white;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 0.75rem;
}

.badge.hidden {
    display: none;
}

.filter-section, .alarm-volume-section {
    padding: 20px;
    border-top: 1px solid var(--border-color);
}

.filter-section h3, .alarm-volume-section h3 {
    margin-bottom: 15px;
    font-size: 0.9rem;
    text-transform: uppercase;
    color: var(--text-secondary);
}

.filter-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 8px;
    border-radius: 5px;
    transition: background 0.3s;
}

.checkbox-label:hover {
    background: var(--bg-secondary);
}

.type-filters {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.volume-control {
    display: flex;
    align-items: center;
    gap: 10px;
}

.volume-control input[type="range"] {
    flex: 1;
}

/* ============================================
   CONTENT AREA
   ============================================ */

.content-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: var(--bg-secondary);
}

.view {
    display: none;
}

.view.active {
    display: block;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: 350px 1fr;
    gap: 20px;
    height: calc(100vh - var(--topbar-height) - 40px);
}

/* ============================================
   ALARM PANEL
   ============================================ */

.alarm-panel {
    background: white;
    border-radius: 15px;
    box-shadow: var(--shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.alarm-panel h3 {
    padding: 20px;
    background: linear-gradient(135deg, var(--danger-color), #d32f2f);
    color: white;
    margin: 0;
}

.alarm-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.alarm-item {
    padding: 15px;
    border-radius: 10px;
    margin-bottom: 10px;
    background: var(--bg-secondary);
    border-left: 4px solid var(--warning-color);
    animation: alarmSlideIn 0.3s ease-out;
}

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

.alarm-item.priority-5 { border-left-color: #D32F2F; background: #ffebee; }
.alarm-item.priority-4 { border-left-color: #FF9800; background: #fff3e0; }
.alarm-item.priority-3 { border-left-color: #FFC107; background: #fffde7; }
.alarm-item.priority-2 { border-left-color: #8BC34A; background: #f1f8e9; }
.alarm-item.priority-1 { border-left-color: #4CAF50; background: #e8f5e9; }

.alarm-item.unacknowledged {
    animation: alarmHighlight 1s infinite alternate;
}

@keyframes alarmHighlight {
    from { box-shadow: 0 0 5px var(--danger-color); }
    to { box-shadow: 0 0 20px var(--danger-color); }
}

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

.alarm-device {
    font-weight: 600;
    color: var(--text-primary);
}

.alarm-priority {
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 0.75rem;
    font-weight: 600;
    color: white;
}

.priority-5 { background: #D32F2F; }
.priority-4 { background: #FF9800; }
.priority-3 { background: #FFC107; color: #333; }
.priority-2 { background: #8BC34A; }
.priority-1 { background: #4CAF50; }

.alarm-text {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.alarm-timestamp {
    font-size: 0.75rem;
    color: #999;
    margin-bottom: 10px;
}

.alarm-actions {
    display: flex;
    gap: 8px;
}

.alarm-actions .btn {
    padding: 6px 12px;
    font-size: 0.8rem;
    border-radius: 5px;
}

.btn-ack {
    background: var(--warning-color);
    color: #333;
}

.btn-clear {
    background: var(--success-color);
    color: white;
}

/* ============================================
   MAP
   ============================================ */

.map-container {
    border-radius: 15px;
    overflow: hidden;
    box-shadow: var(--shadow);
    position: relative;
}

#map {
    height: 100%;
    width: 100%;
}

/* ============================================
   DEVICE POPUP
   ============================================ */

.popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.popup.hidden {
    display: none;
}

.popup-content {
    background: white;
    border-radius: 15px;
    padding: 30px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
    animation: popupSlide 0.3s ease-out;
}

@keyframes popupSlide {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

.popup-close {
    position: absolute;
    top: 15px;
    right: 15px;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
}

.popup-info p {
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.popup-sensors, .popup-controls {
    margin-top: 20px;
    padding-top: 15px;
    border-top: 1px solid var(--border-color);
}

.sensor-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 10px;
}

.sensor-item {
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 8px;
    text-align: center;
}

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

.sensor-value {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--primary-color);
}

.control-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 8px;
    margin-bottom: 8px;
}

.toggle-switch {
    position: relative;
    width: 50px;
    height: 26px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--border-color);
    border-radius: 26px;
    transition: 0.3s;
}

.toggle-slider:before {
    content: "";
    position: absolute;
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: 0.3s;
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--success-color);
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(24px);
}

.control-feedback {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 5px;
}

/* ============================================
   ADMIN PANEL
   ============================================ */

.admin-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 10px;
}

.tab-btn {
    padding: 10px 20px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    color: var(--text-secondary);
    border-bottom: 2px solid transparent;
    transition: all 0.3s;
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.admin-content {
    background: white;
    border-radius: 15px;
    padding: 20px;
    box-shadow: var(--shadow);
}

.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th, .data-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.data-table th {
    background: var(--bg-secondary);
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-size: 0.8rem;
}

.data-table tr:hover {
    background: var(--bg-secondary);
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    margin-bottom: 15px;
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */

@media (max-width: 768px) {
    .dashboard-grid {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
    }
    
    .alarm-panel {
        max-height: 200px;
    }
    
    .sidebar {
        position: fixed;
        top: var(--topbar-height);
        left: 0;
        height: calc(100vh - var(--topbar-height));
        z-index: 99;
        transform: translateX(-100%);
    }
    
    .sidebar.mobile-open {
        transform: translateX(0);
    }
    
    .login-container {
        max-width: 350px;
    }
    
    .banner h1 {
        font-size: 2rem;
    }
    
    .top-bar {
        padding: 0 10px;
    }
    
    .last-update {
        display: none;
    }
}

@media (max-width: 480px) {
    .alarm-item {
        padding: 10px;
    }
    
    .alarm-actions {
        flex-direction: column;
    }
    
    .popup-content {
        padding: 20px;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

.hidden { display: none !important; }
.text-center { text-align: center; }
.mt-10 { margin-top: 10px; }
.mb-10 { margin-bottom: 10px; }

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}


/* ============================================
   KRM Devices Tab
   ============================================ */

#device-list {
    display: grid;
    grid-template-columns:
        repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
    padding: 8px 0;
}

.device-card {
    display: block;
    width: 100%;
    background: #ffffff;
    border: 1px solid #dddddd;
    border-left: 4px solid #2196f3;
    border-radius: 8px;
    padding: 16px;
    text-align: left;
    cursor: pointer;
    color: inherit;

    transition:
        box-shadow 0.15s ease,
        transform 0.15s ease;
}

.device-card:hover {
    transform: translateY(-2px);
    box-shadow:
        0 4px 12px rgba(0, 0, 0, 0.12);
}

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

.device-card-title {
    font-size: 1.05rem;
    font-weight: 700;
}

.device-card-status {
    padding: 4px 10px;
    border-radius: 12px;
    color: white;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
}

.device-card-details {
    display: grid;
    gap: 8px;
}

.device-card-detail {
    display: grid;
    grid-template-columns: 90px 1fr;
    gap: 12px;
}

.device-card-detail-label {
    color: #777777;
}

.device-card-detail-value {
    font-weight: 500;
    overflow-wrap: anywhere;
}

.device-list-message {
    grid-column: 1 / -1;
    padding: 30px;
    text-align: center;
    color: #777777;
}

.device-list-error {
    color: #d32f2f;
}

