/* ========================================
   Smart Farm Dashboard — Stylesheet
   ======================================== */

:root {
  /* Colors */
  --bg: #f0f2f5;
  --bg-card: #ffffff;
  --bg-header: #1a1d23;
  --text-primary: #1a1d23;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --border-light: #f3f4f6;

  /* Accent */
  --accent: #2563eb;
  --accent-light: #dbeafe;
  --accent-hover: #1d4ed8;

  /* Status */
  --green: #10b981;
  --green-bg: #d1fae5;
  --green-text: #065f46;
  --red: #ef4444;
  --red-bg: #fee2e2;
  --red-text: #991b1b;
  --amber: #f59e0b;
  --amber-bg: #fef3c7;
  --amber-text: #92400e;
  --blue: #3b82f6;
  --blue-bg: #dbeafe;

  /* Sensor type colors */
  --color-temp: #ef4444;
  --color-humidity: #3b82f6;
  --color-light: #f59e0b;
  --color-power: #8b5cf6;
  --color-solar: #10b981;
  --color-converter: #6366f1;

  /* Layout */
  --max-width: 1280px;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.03);
  --transition: 0.2s ease;
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---- Header ---- */
.header {
  background: var(--bg-header);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.header-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 24px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo svg { color: var(--green); }

.logo h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--red);
}

.status-dot.online {
  background: var(--green);
  box-shadow: 0 0 6px rgba(16,185,129,0.6);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.last-update {
  font-size: 12px;
  color: rgba(255,255,255,0.5);
}

/* ---- Main ---- */
.main {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

/* ---- Section ---- */
.section-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 16px;
  letter-spacing: -0.3px;
}

/* ---- Card ---- */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  transition: box-shadow var(--transition);
}

.card:hover {
  box-shadow: var(--shadow-md);
}

/* ---- System Status Grid ---- */
.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
}

.status-card {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.status-card-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-card-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-card-icon.rpi { background: #f0fdf4; color: var(--green); }
.status-card-icon.devices { background: #eff6ff; color: var(--blue); }
.status-card-icon.alerts { background: #fffbeb; color: var(--amber); }

.status-card-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-card-title strong {
  font-size: 14px;
}

.status-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
}

.status-badge.online { background: var(--green-bg); color: var(--green-text); }
.status-badge.warning { background: var(--amber-bg); color: var(--amber-text); }
.status-badge.error { background: var(--red-bg); color: var(--red-text); }
.status-badge.offline { background: #f3f4f6; color: #6b7280; }

/* System metrics */
.system-metrics {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sys-metric {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}

.sys-label {
  font-size: 12px;
  color: var(--text-secondary);
  width: 60px;
  flex-shrink: 0;
}

.sys-value {
  font-size: 13px;
  font-weight: 600;
  flex: 1;
  text-align: right;
}

.sys-bar {
  width: 100%;
  height: 4px;
  background: var(--border-light);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 2px;
}

.sys-bar-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.5s ease;
}

.sys-bar-fill.temp { background: var(--green); }
.sys-bar-fill.temp.warn { background: var(--amber); }
.sys-bar-fill.temp.danger { background: var(--red); }
.sys-bar-fill.mem { background: var(--blue); }

/* Device list */
.device-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.device-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--border-light);
  font-size: 13px;
}

.device-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.device-dot.online { background: var(--green); }
.device-dot.offline { background: var(--red); animation: pulse 1.5s infinite; }

.device-name {
  font-weight: 500;
  flex: 1;
  min-width: 0;
}

.device-addr {
  font-size: 11px;
  color: var(--text-muted);
  font-family: 'Courier New', monospace;
}

.device-status {
  font-size: 11px;
  font-weight: 600;
}

.device-status.online { color: var(--green); }
.device-status.offline { color: var(--red); }

/* Alert list */
.alert-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.alert-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12px;
}

.alert-row svg { flex-shrink: 0; margin-top: 1px; }

.alert-row.warning { background: var(--amber-bg); color: var(--amber-text); }
.alert-row.error { background: var(--red-bg); color: var(--red-text); }
.alert-row.info { background: var(--blue-bg); color: var(--accent); }
.alert-row.success { background: var(--green-bg); color: var(--green-text); }

.alert-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.alert-msg {
  font-weight: 500;
  line-height: 1.3;
}

.alert-time {
  font-size: 11px;
  opacity: 0.7;
}

/* ---- Sensor Grid ---- */
.sensor-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.sensor-card {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
  overflow: hidden;
}

.sensor-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
}

.sensor-card[data-type="temp"]::before { background: var(--color-temp); }
.sensor-card[data-type="humidity"]::before { background: var(--color-humidity); }
.sensor-card[data-type="light"]::before { background: var(--color-light); }
.sensor-card[data-type="power"]::before { background: var(--color-power); }
.sensor-card[data-type="solar"]::before { background: var(--color-solar); }
.sensor-card[data-type="converter"]::before { background: var(--color-converter); }

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sensor-card[data-type="temp"] .card-icon { background: #fef2f2; color: var(--color-temp); }
.sensor-card[data-type="humidity"] .card-icon { background: #eff6ff; color: var(--color-humidity); }
.sensor-card[data-type="light"] .card-icon { background: #fffbeb; color: var(--color-light); }
.sensor-card[data-type="power"] .card-icon { background: #f5f3ff; color: var(--color-power); }
.sensor-card[data-type="solar"] .card-icon { background: #ecfdf5; color: var(--color-solar); }
.sensor-card[data-type="converter"] .card-icon { background: #eef2ff; color: var(--color-converter); }

.card-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.card-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.card-value {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -1px;
  line-height: 1.1;
}

.card-value small {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-left: 2px;
}

.card-footer {
  margin-top: auto;
}

.card-range, .card-sub {
  font-size: 12px;
}

.card-range {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 500;
}

.card-range.normal {
  background: var(--green-bg);
  color: var(--green-text);
}

.card-range.warning {
  background: var(--amber-bg);
  color: var(--amber-text);
}

.card-range.danger {
  background: var(--red-bg);
  color: var(--red-text);
}

.card-sub {
  color: var(--text-secondary);
}

.card-sub strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* ---- Relay Grid ---- */
.relay-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.relay-card {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.relay-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.relay-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.relay-name {
  font-size: 15px;
  font-weight: 700;
}

.relay-desc {
  font-size: 12px;
  color: var(--text-secondary);
}

.relay-status {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  white-space: nowrap;
}

.relay-status.on {
  background: var(--green-bg);
  color: var(--green-text);
}

.relay-status.off {
  background: var(--border-light);
  color: var(--text-secondary);
}

.relay-body {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.relay-indicator {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.relay-indicator.on {
  background: var(--green-bg);
  color: var(--green);
  box-shadow: 0 0 16px rgba(16,185,129,0.25);
}

.relay-indicator.off {
  background: var(--border-light);
  color: var(--text-muted);
}

/* Toggle Switch */
.toggle-switch {
  position: relative;
  width: 52px;
  height: 28px;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: #d1d5db;
  border-radius: 28px;
  transition: var(--transition);
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  bottom: 3px;
  background: white;
  border-radius: 50%;
  transition: var(--transition);
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--green);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(24px);
}

.relay-footer {
  margin-top: auto;
}

.relay-mode {
  font-size: 12px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 4px;
}

.relay-mode.auto {
  background: var(--blue-bg);
  color: var(--accent);
}

.relay-mode.manual {
  background: var(--border-light);
  color: var(--text-secondary);
}

/* ---- Mode switch (auto/manual segmented buttons) ---- */
.mode-switch {
  display: inline-flex;
  border: 1px solid var(--border-light);
  border-radius: 6px;
  overflow: hidden;
  font-size: 12px;
}

.mode-btn {
  background: transparent;
  border: none;
  padding: 4px 12px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.mode-btn:hover {
  background: var(--border-light);
}

.mode-btn.active {
  background: var(--accent);
  color: #fff;
}

.mode-btn.active:hover {
  background: var(--accent);
}

/* ---- Settings Card ---- */
.settings-card {
  padding: 28px;
}

.settings-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
  line-height: 1.6;
}

.settings-grid {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-bottom: 24px;
}

.setting-item {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  padding: 16px;
  background: var(--border-light);
  border-radius: var(--radius-sm);
}

.setting-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  color: var(--accent);
  flex-shrink: 0;
  box-shadow: var(--shadow);
}

.setting-icon.temp-icon { color: var(--color-temp); }
.setting-icon.humidity-icon { color: var(--color-humidity); }

.setting-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-label {
  font-size: 14px;
  font-weight: 600;
}

.setting-inputs {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.input-group input {
  width: 72px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 10px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  text-align: center;
  background: white;
  transition: border-color var(--transition);
}

.input-group input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.input-group span {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.input-separator {
  font-size: 16px;
  color: var(--text-muted);
  font-weight: 500;
}

.setting-current {
  font-size: 12px;
  color: var(--text-secondary);
}

.setting-current.met {
  color: var(--green);
  font-weight: 500;
}

.setting-current.unmet {
  color: var(--red);
  font-weight: 500;
}

.settings-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.summary-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
}

.summary-status.active {
  background: var(--green-bg);
  color: var(--green-text);
}

.summary-status.inactive {
  background: var(--border-light);
  color: var(--text-secondary);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  border: none;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: white;
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border-light);
}

/* ---- Chart Section ---- */
.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.chart-header .section-title {
  margin-bottom: 0;
}

.chart-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.chart-tabs {
  display: flex;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.chart-tab {
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  border-right: 1px solid var(--border);
}

.chart-tab:last-child { border-right: none; }

.chart-tab.active {
  background: var(--accent);
  color: white;
}

.chart-tab:hover:not(.active) {
  background: var(--border-light);
}

.chart-period {
  display: flex;
  gap: 4px;
}

.period-btn {
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  border: 1px solid var(--border);
  background: white;
  border-radius: 6px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.period-btn.active {
  background: var(--text-primary);
  color: white;
  border-color: var(--text-primary);
}

.period-btn:hover:not(.active) {
  background: var(--border-light);
}

.chart-card {
  padding: 20px;
  height: 360px;
}

.chart-card canvas {
  width: 100% !important;
  height: 100% !important;
}

/* ---- Export Section ---- */
.export-card {
  padding: 20px 24px;
}

.export-body {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.export-info {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-secondary);
}

.export-info div {
  display: flex;
  flex-direction: column;
}

.export-info strong {
  color: var(--text-primary);
  font-size: 14px;
}

.export-info span {
  font-size: 12px;
}

.export-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.export-controls select {
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: white;
  color: var(--text-primary);
  cursor: pointer;
}

/* ---- Footer ---- */
.footer {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 20px 24px 32px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
}

/* ============================================
   RESPONSIVE — Tablet (< 1024px)
   ============================================ */
@media (max-width: 1024px) {
  .status-grid {
    grid-template-columns: 1fr 1fr;
  }

  .alerts-card {
    grid-column: 1 / -1;
  }

  .sensor-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .relay-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ============================================
   RESPONSIVE — Mobile (< 768px)
   ============================================ */
@media (max-width: 768px) {
  .main {
    padding: 16px;
    gap: 24px;
  }

  .header-inner {
    padding: 0 16px;
  }

  .last-update {
    display: none;
  }

  .status-grid {
    grid-template-columns: 1fr;
  }

  .alerts-card {
    grid-column: auto;
  }

  .device-addr {
    display: none;
  }

  .sensor-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .sensor-card {
    padding: 16px;
  }

  .card-value {
    font-size: 24px;
  }

  .relay-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .relay-card {
    padding: 20px;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 12px;
  }

  .relay-header {
    flex: 1;
    min-width: 150px;
  }

  .relay-body {
    gap: 16px;
  }

  .relay-footer {
    width: 100%;
  }

  .settings-card {
    padding: 20px;
  }

  .setting-item {
    flex-direction: column;
    gap: 12px;
  }

  .settings-summary {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }

  .summary-status {
    justify-content: center;
  }

  .btn-primary {
    width: 100%;
    justify-content: center;
  }

  .chart-header {
    flex-direction: column;
  }

  .chart-controls {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .chart-tabs {
    width: 100%;
  }

  .chart-tab {
    flex: 1;
    text-align: center;
  }

  .chart-period {
    width: 100%;
  }

  .period-btn {
    flex: 1;
    text-align: center;
  }

  .chart-card {
    height: 280px;
    padding: 12px;
  }

  .export-body {
    flex-direction: column;
    align-items: stretch;
  }

  .export-controls {
    flex-direction: column;
  }

  .export-controls select,
  .export-controls .btn {
    width: 100%;
    justify-content: center;
  }
}

/* ============================================
   RESPONSIVE — Small phone (< 420px)
   ============================================ */
@media (max-width: 420px) {
  .sensor-grid {
    grid-template-columns: 1fr;
  }

  .logo h1 {
    font-size: 15px;
  }

  .card-value {
    font-size: 22px;
  }
}
