/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary:    #0d6efd;
    --primary-dk: #0a58ca;
    --success:    #198754;
    --danger:     #dc3545;
    --warning:    #ffc107;
    --dark:       #1a1d23;
    --card-bg:    #22262f;
    --card-border:#2e3340;
    --text:       #e2e8f0;
    --text-muted: #8892a4;
    --bg:         #13161c;
    --relay-on:   #22c55e;
    --relay-off:  #475569;
    --radius:     12px;
    --shadow:     0 4px 24px rgba(0,0,0,0.4);
}

html { font-size: 16px; }
body {
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ===== NAVBAR ===== */
.navbar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0 2rem;
    height: 64px;
    background: var(--dark);
    border-bottom: 1px solid var(--card-border);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 12px rgba(0,0,0,0.5);
}

.nav-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
    white-space: nowrap;
}
.nav-icon { font-size: 1.5rem; }

.nav-menu {
    display: flex;
    list-style: none;
    gap: 0.25rem;
    margin-left: 1rem;
}
.nav-menu a {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 1rem;
    border-radius: 8px;
    color: var(--text-muted);
    text-decoration: none;
    font-weight: 500;
    transition: all 0.2s;
}
.nav-menu a:hover, .nav-menu a.active {
    background: rgba(13,110,253,0.15);
    color: var(--primary);
}

.mqtt-status {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}
.status-dot {
    width: 10px; height: 10px;
    border-radius: 50%;
    background: var(--relay-off);
    transition: background 0.3s;
    box-shadow: 0 0 0 0 transparent;
}
.status-dot.connected {
    background: var(--relay-on);
    box-shadow: 0 0 0 4px rgba(34,197,94,0.2);
    animation: pulse-green 2s infinite;
}
.status-dot.error { background: var(--danger); }

@keyframes pulse-green {
    0%   { box-shadow: 0 0 0 0 rgba(34,197,94,0.4); }
    70%  { box-shadow: 0 0 0 8px rgba(34,197,94,0); }
    100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); }
}

/* ===== MAIN ===== */
.main-content {
    flex: 1;
    padding: 2rem;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

/* ===== PAGE TITLE ===== */
.page-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 1.5rem;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ===== CARDS ===== */
.card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}
.card-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

/* ===== GRID LAYOUTS ===== */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.5rem; }

/* ===== GAUGE SECTION ===== */
.gauge-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}
.gauge-wrap canvas { max-width: 200px; }
.gauge-value {
    font-size: 2rem;
    font-weight: 800;
    color: var(--text);
    line-height: 1;
}
.gauge-unit {
    font-size: 0.9rem;
    color: var(--text-muted);
}
.gauge-label {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* ===== STAT CARDS ===== */
.stat-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--shadow);
}
.stat-icon {
    font-size: 2.2rem;
    width: 56px; height: 56px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 12px;
    background: rgba(13,110,253,0.12);
    flex-shrink: 0;
}
.stat-icon.temp  { background: rgba(239,68,68,0.12); }
.stat-icon.hum   { background: rgba(59,130,246,0.12); }
.stat-info { flex: 1; }
.stat-label { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.stat-val   { font-size: 1.8rem; font-weight: 800; color: var(--text); line-height: 1.1; }
.stat-unit  { font-size: 0.85rem; color: var(--text-muted); }

/* ===== RELAY CARDS ===== */
.relay-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}
.relay-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--shadow);
    transition: border-color 0.3s;
}
.relay-card.on { border-color: var(--relay-on); }

.relay-icon-wrap {
    position: relative;
    width: 80px; height: 80px;
    display: flex; align-items: center; justify-content: center;
}
.relay-bulb {
    font-size: 3rem;
    transition: filter 0.4s, transform 0.3s;
    filter: grayscale(1) brightness(0.5);
}
.relay-card.on .relay-bulb {
    filter: grayscale(0) brightness(1.2) drop-shadow(0 0 12px #fbbf24);
    transform: scale(1.1);
    animation: glow-pulse 2s ease-in-out infinite;
}
@keyframes glow-pulse {
    0%, 100% { filter: grayscale(0) brightness(1.2) drop-shadow(0 0 12px #fbbf24); }
    50%       { filter: grayscale(0) brightness(1.5) drop-shadow(0 0 20px #fbbf24); }
}

.relay-ring {
    position: absolute;
    inset: -8px;
    border-radius: 50%;
    border: 3px solid transparent;
    transition: border-color 0.4s, box-shadow 0.4s;
}
.relay-card.on .relay-ring {
    border-color: var(--relay-on);
    box-shadow: 0 0 16px rgba(34,197,94,0.4);
    animation: ring-spin 3s linear infinite;
}
@keyframes ring-spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

.relay-name {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
}
.relay-badge {
    font-size: 0.75rem;
    font-weight: 700;
    padding: 0.2rem 0.75rem;
    border-radius: 20px;
    background: var(--relay-off);
    color: #fff;
    transition: background 0.3s;
    letter-spacing: 0.05em;
}
.relay-card.on .relay-badge { background: var(--relay-on); }

/* Toggle Switch */
.toggle-switch {
    position: relative;
    width: 60px; height: 30px;
    cursor: pointer;
}
.toggle-switch input { display: none; }
.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--relay-off);
    border-radius: 30px;
    transition: background 0.3s;
}
.toggle-slider::before {
    content: '';
    position: absolute;
    width: 24px; height: 24px;
    left: 3px; top: 3px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.3s;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}
.toggle-switch input:checked + .toggle-slider { background: var(--relay-on); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(30px); }

/* ===== CHART ===== */
.chart-container {
    position: relative;
    height: 280px;
}

/* ===== LAST UPDATE ===== */
.last-update {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: right;
    margin-top: 0.5rem;
}

/* ===== SETTINGS FORM ===== */
.settings-section {
    max-width: 700px;
}
.form-group {
    margin-bottom: 1.25rem;
}
.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}
.form-control {
    width: 100%;
    padding: 0.6rem 0.9rem;
    background: var(--bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    color: var(--text);
    font-size: 0.95rem;
    transition: border-color 0.2s, box-shadow 0.2s;
    outline: none;
}
.form-control:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(13,110,253,0.2);
}
.form-hint {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 0.3rem;
}
.form-row { display: grid; grid-template-columns: 2fr 1fr; gap: 1rem; }

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.6rem 1.4rem;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dk); transform: translateY(-1px); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger  { background: var(--danger);  color: #fff; }

/* Alert */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    display: none;
}
.alert.show { display: block; }
.alert-success { background: rgba(25,135,84,0.2); border: 1px solid var(--success); color: #6ee7b7; }
.alert-danger   { background: rgba(220,53,69,0.2);  border: 1px solid var(--danger);  color: #fca5a5; }

/* ===== FOOTER ===== */
.footer {
    text-align: center;
    padding: 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    border-top: 1px solid var(--card-border);
    background: var(--dark);
}

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
    .grid-2, .grid-4 { grid-template-columns: 1fr 1fr; }
    .relay-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 600px) {
    .main-content { padding: 1rem; }
    .grid-2, .grid-4, .relay-grid { grid-template-columns: 1fr; }
    .navbar { padding: 0 1rem; }
    .nav-brand .nav-title { display: none; }
    .form-row { grid-template-columns: 1fr; }
}

/* ===== DIVIDER ===== */
.section-gap { margin-top: 1.5rem; }

/* ===== TOOLTIP ===== */
.tooltip-wrap { position: relative; display: inline-block; }
