/* =========================================================
   THEME VARIABLES
========================================================= */
:root {
  --bg-main: #0b0f19;
  --bg-card: #161c2d;
  --text-main: #f9fafb;
  --text-muted: #94a3b8;
  --border-color: rgba(255, 255, 255, 0.08);
  --shadow-card: 0 10px 30px rgba(0, 0, 0, 0.4);
  
  --primary: #3b82f6;
  --primary-glow: rgba(59, 130, 246, 0.2);
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --input-bg: rgba(0, 0, 0, 0.2);
}

[data-theme="light"] {
  --bg-main: #f3f4f6;
  --bg-card: #ffffff;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --border-color: rgba(0, 0, 0, 0.1);
  --shadow-card: 0 8px 24px rgba(0, 0, 0, 0.05);
  --input-bg: #f9fafb;
}

/* =========================================================
   GLOBAL RESET & LAYOUT
========================================================= */
* { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Inter', sans-serif; }
body { background: var(--bg-main); color: var(--text-main); transition: background 0.3s, color 0.3s; padding: 20px; line-height: 1.5; }

.app-layout { max-width: 1300px; margin: 0 auto; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 1.4rem; font-weight: 700; letter-spacing: -0.5px; }
.status-bar { display: flex; align-items: center; gap: 20px; }
.time-display { font-family: monospace; font-size: 0.9rem; color: var(--text-muted); }
.theme-toggle { cursor: pointer; color: var(--text-muted); transition: 0.2s; display: flex; align-items: center; }
.theme-toggle:hover { color: var(--primary); }

/* =========================================================
   BENTO GRID SYSTEM
========================================================= */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1.5fr; /* Automation box smaller than Relay box */
  gap: 20px;
}
.full-width { grid-column: 1 / -1; }

.bento-card {
  background: var(--bg-card);
  border-radius: 18px;
  padding: 24px;
  box-shadow: var(--shadow-card);
  border: 1px solid var(--border-color);
  transition: background 0.3s, border-color 0.3s;
}
.bento-card h2 { display: flex; align-items: center; gap: 10px; font-size: 0.9rem; margin-bottom: 20px; color: var(--text-muted); text-transform: uppercase; font-weight: 700; }
p.subtitle { font-size: 0.8rem; color: var(--text-muted); margin: -10px 0 15px 0; }

/* =========================================================
   AUTOMATION ZONES (Segmented Radio)
========================================================= */
.zone-grid { display: flex; flex-direction: column; gap: 12px; }

.zone-item { 
  display: flex; justify-content: space-between; align-items: center; 
  padding: 12px 16px; background: rgba(128,128,128,0.05); 
  border-radius: 12px; border: 1px solid transparent; cursor: pointer; transition: 0.2s;
}
.zone-item:hover { background: rgba(128,128,128,0.1); }
.zone-item.active-selection { border-color: var(--primary); background: var(--primary-glow); }
.zone-item label { font-size: 0.9rem; font-weight: 600; cursor: pointer; }

.mode-selector { display: flex; background: var(--input-bg); padding: 3px; border-radius: 8px; border: 1px solid var(--border-color); }
.mode-selector input { display: none; }
.mode-selector label { 
  padding: 6px 12px; font-size: 0.7rem; font-weight: 700; border-radius: 6px; 
  cursor: pointer; color: var(--text-muted); transition: 0.2s; margin: 0;
}
.mode-selector input:checked + label { background: var(--primary); color: white; box-shadow: 0 2px 4px rgba(0,0,0,0.2); }

/* =========================================================
   LIVE RELAYS (Detail View)
========================================================= */
.card-header-flex { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.card-header-flex h2 { margin-bottom: 0; }
.badge { padding: 4px 12px; border-radius: 20px; font-size: 0.7rem; font-weight: 800; text-transform: uppercase; }
.mode-auto { background: var(--primary-glow); color: var(--primary); }
.mode-manual { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.mode-off { background: rgba(128,128,128,0.15); color: var(--text-muted); }

.relay-list { display: flex; flex-direction: column; gap: 12px; }
.loading-text { font-size: 0.9rem; color: var(--text-muted); font-style: italic; }

.relay-detail-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px; background: rgba(128,128,128,0.03); border-radius: 14px; 
  border: 1px solid var(--border-color);
}
.relay-info { display: flex; align-items: center; gap: 15px; width: 35%; }
.relay-info span { font-size: 28px; }
.relay-info h4 { font-size: 0.95rem; margin-bottom: 2px; }
.relay-info p { font-size: 0.8rem; color: var(--text-muted); }

.relay-stats { width: 35%; display: flex; flex-direction: column; gap: 4px; }
.relay-stats p { margin: 0; color: var(--text-muted); font-size: 0.85rem; }

.manual-controls { display: flex; gap: 8px; justify-content: flex-end; width: 30%; }
.ctrl-btn { border: none; padding: 8px 16px; border-radius: 8px; font-weight: 800; font-size: 0.75rem; cursor: pointer; transition: 0.2s; }
.ctrl-btn.on { background: rgba(16, 185, 129, 0.15); color: var(--success); border: 1px solid var(--success); }
.ctrl-btn.off { background: rgba(239, 68, 68, 0.15); color: var(--danger); border: 1px solid var(--danger); }
.ctrl-btn.on:hover:not(:disabled) { background: var(--success); color: #fff; }
.ctrl-btn.off:hover:not(:disabled) { background: var(--danger); color: #fff; }
.ctrl-btn:disabled { opacity: 0.3; cursor: not-allowed; filter: grayscale(1); border-color: transparent; background: rgba(128,128,128,0.2); color: var(--text-muted); }

/* =========================================================
   SENSOR BOXES (Redesigned)
========================================================= */
.climate-data-layout {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

/* DHT Grid (Temperature & Humidity) */
.dht-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 15px;
}

.sensor-card-dht {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 16px 20px;
    background: rgba(128,128,128,0.03);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;
}

.sensor-card-dht:hover {
    transform: translateY(-2px);
    border-color: var(--primary);
    box-shadow: 0 4px 12px var(--primary-glow);
}

.sensor-indicator-vertical {
    width: 10px;
    height: 75px;
    background: var(--input-bg);
    border-radius: 10px;
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
}

.sensor-indicator-vertical .indicator-fill {
    position: absolute;
    bottom: 0;
    width: 100%;
    border-radius: 10px;
}

.sensor-content {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sensor-content .sensor-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--text-main);
}

.sensor-content .sensor-val {
    font-size: 1.8rem;
    font-weight: 800;
    color: var(--text-main);
    line-height: 1.1;
    margin: 4px 0;
}

.sensor-content .sensor-sub {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.sensor-content .sensor-peak {
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0.8;
}

/* Ammonia Sensors Grid */
.ammonia-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 15px;
}

.sensor-card-ammonia {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 20px;
    background: rgba(128,128,128,0.03);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;
}

.sensor-card-ammonia:hover {
    transform: translateY(-2px);
    border-color: var(--warning);
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.15);
}

.sensor-card-ammonia .sensor-title {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-muted);
}

.ammonia-bar-bg {
    width: 100%;
    height: 12px;
    background: var(--input-bg);
    border-radius: 10px;
    overflow: hidden;
}

.ammonia-bar-fill {
    height: 100%;
    border-radius: 10px;
}

.sensor-card-ammonia .sensor-val {
    font-size: 1.1rem;
    font-weight: 800;
    color: var(--text-main);
}

.sensor-card-ammonia .sensor-peak {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* =========================================================
   SETTINGS FORM
========================================================= */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 30px; }
.settings-group { display: flex; flex-direction: column; gap: 15px; }
.settings-group h3 { font-size: 0.8rem; color: var(--primary); text-transform: uppercase; margin-bottom: 5px; }
.input-row { display: flex; gap: 15px; }
.input-group { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.input-group label { font-size: 0.75rem; color: var(--text-muted); font-weight: 600; }
.input-group input { 
    background: var(--input-bg); border: 1px solid var(--border-color); 
    padding: 10px; border-radius: 8px; color: var(--text-main); font-weight: 600; transition: 0.2s; outline: none;
}
.input-group input:focus { border-color: var(--primary); }
.settings-footer { margin-top: 25px; text-align: right; border-top: 1px solid var(--border-color); padding-top: 20px; }
.save-btn { background: var(--primary); border: none; padding: 12px 30px; border-radius: 10px; color: #fff; font-weight: 700; cursor: pointer; transition: 0.2s; }
.save-btn:hover { background: #2563eb; transform: translateY(-1px); }

/* =========================================================
   MODAL & CHART STYLES
========================================================= */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.6); backdrop-filter: blur(5px);
    display: flex; justify-content: center; align-items: center;
    z-index: 1000; transition: opacity 0.3s;
}
.modal-content {
    background: var(--bg-card); width: 90%; max-width: 800px;
    border-radius: 20px; padding: 24px; position: relative;
    box-shadow: 0 20px 50px rgba(0,0,0,0.5); border: 1px solid var(--border-color);
}
.modal-content h2 { margin-bottom: 10px; font-size: 1.2rem; }
.close-btn {
    position: absolute; top: 20px; right: 20px; background: none; border: none; 
    color: var(--text-muted); cursor: pointer; transition: 0.2s;
}
.close-btn:hover { color: var(--danger); transform: scale(1.1); }
.modal-charts-layout {
    display: flex;
    flex-direction: column;
    gap: 24px;
    margin-top: 20px;
    max-height: 65vh; /* Prevents the modal from exceeding screen height */
    overflow-y: auto; /* Adds a scrollbar if the graphs are tall */
    padding-right: 10px;
}

/* Custom scrollbar for a cleaner look */
.modal-charts-layout::-webkit-scrollbar { width: 6px; }
.modal-charts-layout::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 4px; }

.chart-container { 
    height: 300px; /* Reduced from 400px so both charts fit nicely */
    width: 100%; 
    position: relative; 
}

/* =========================================================
   ANIMATIONS & UTILS
========================================================= */
.spin { animation: spin 2s linear infinite; color: var(--success); }
@keyframes spin { 100% { transform: rotate(360deg); } }

.text-primary { color: var(--primary); }
.text-warning { color: var(--warning); }
.text-blue { color: #60a5fa; }
.text-green { color: var(--success); }
.text-red { color: var(--danger); }
.off-icon { color: var(--text-muted); opacity: 0.4; }

.hidden { display: none !important; }

/* Responsive adjustments */
@media (max-width: 900px) {
    .dashboard-grid { grid-template-columns: 1fr; }
    .relay-detail-item { flex-direction: column; align-items: flex-start; gap: 15px; }
    .relay-info, .relay-stats, .manual-controls { width: 100%; }
    .manual-controls { justify-content: flex-start; }
}

/* --- Fan Spinning & Pump Animations --- */
@keyframes spin { 100% { transform: rotate(360deg); } }
.spin { animation: spin 1.2s linear infinite; display: inline-block; }

@keyframes dropPulse { 
    0% { transform: scale(1); opacity: 1; } 
    50% { transform: scale(1.2); opacity: 0.7; } 
    100% { transform: scale(1); opacity: 1; } 
}
.drop-anim { animation: dropPulse 1.5s ease-in-out infinite; display: inline-block; }

/* --- Stats Range Buttons --- */
.range-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    padding: 4px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8rem;
    transition: all 0.2s;
}
.range-btn:hover { border-color: var(--primary); color: var(--text-main); }
.range-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.data-tabs {
    display: flex;
    gap: 5px;
    background: var(--input-bg);
    padding: 4px;
    border-radius: 8px;
}

.data-tab {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    transition: all 0.2s ease;
}

.data-tab:hover {
    color: var(--text-main);
}

.data-tab.active {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 2px 5px var(--primary-glow);
}