:root {
  --bg: #0f1117;
  --surface: #1a1d2e;
  --border: #2d3147;
  --text: #e2e8f0;
  --text-dim: #718096;
  --solar: #f6c90e;
  --battery: #48bb78;
  --grid: #63b3ed;
  --load: #d53f8c;
  --danger: #fc8181;
  --success: #68d391;
  --warning: #f6ad55;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  min-height: 100vh;
}

/* ── Login ─────────────────────────────────────────────── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 380px;
}

.login-card h1 { font-size: 22px; margin-bottom: 6px; }
.login-card .subtitle { color: var(--text-dim); margin-bottom: 28px; }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; color: var(--text-dim); font-size: 13px; }
.form-group input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
}
.form-group input:focus {
  outline: none;
  border-color: var(--grid);
}

.error-msg {
  color: var(--danger);
  font-size: 13px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: rgba(252, 129, 129, 0.1);
  border-radius: 6px;
}

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  padding: 8px 16px;
  transition: background 0.15s;
}
.btn:hover { background: var(--border); }
.btn-primary { background: #3b5bdb; border-color: #3b5bdb; color: #fff; }
.btn-primary:hover { background: #4c6ef5; }
.btn-danger { background: #c53030; border-color: #c53030; color: #fff; }
.btn-danger:hover { background: #e53e3e; }
.btn-sm { font-size: 12px; padding: 5px 10px; }
.btn-full { width: 100%; padding: 12px; }

/* ── Dashboard header ──────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

header h1 { font-size: 18px; font-weight: 600; }
.header-left { display: flex; align-items: center; gap: 20px; }
.header-right { display: flex; align-items: center; gap: 12px; }

/* ── Page navigation ───────────────────────────────────── */
.main-nav { display: flex; gap: 4px; }
.nav-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px;
  padding: 5px 10px;
  border-radius: 4px;
  border: 1px solid transparent;
}
.nav-link:hover { color: var(--text); background: var(--border); }
.nav-link.active {
  color: var(--text);
  background: var(--bg);
  border-color: var(--border);
}

/* Checkbox + text pair used for inline on/off switches (server log watch). */
.switch-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
  white-space: nowrap;
}
.switch-label input { cursor: pointer; }

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.status-dot.online { background: var(--battery); box-shadow: 0 0 6px var(--battery); }
.status-dot.offline { background: var(--text-dim); }

/* ── Device picker (custom searchable combobox) ────────── */
.device-picker { position: relative; }

.device-picker-trigger {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 180px;
  max-width: 280px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 5px 10px;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
}
.device-picker-trigger:hover { border-color: var(--grid); }
.device-picker-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.device-picker-caret {
  color: var(--text-dim);
  font-size: 10px;
  transition: transform 0.15s;
}
.device-picker.open .device-picker-caret { transform: rotate(180deg); }

.device-picker-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  width: 300px;
  max-width: 90vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

.device-picker-search-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-bottom: 1px solid var(--border);
}

.device-search {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
}
.device-search:focus { outline: none; border-color: var(--grid); }

.hide-offline-label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  cursor: pointer;
}

.device-picker-list {
  list-style: none;
  margin: 0;
  padding: 4px;
  max-height: 320px;
  overflow-y: auto;
}

.device-picker-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.device-picker-option:hover,
.device-picker-option.active { background: var(--border); }
.device-picker-option[aria-selected="true"] { background: rgba(99, 179, 237, 0.12); }

.device-picker-option-body { flex: 1; min-width: 0; }
.device-picker-option-name {
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.device-picker-option-sub {
  font-size: 11px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.device-picker-option-fw {
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.device-picker-empty {
  padding: 14px 10px;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
}

.last-updated { color: var(--text-dim); font-size: 12px; }

.device-info-bar {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: var(--panel-bg, rgba(255,255,255,0.04));
  border-radius: 6px;
}
.device-info-label { font-size: 16px; font-weight: 600; }
.device-info-meta  { color: var(--text-dim); font-size: 12px; }

/* Firmware slots in the meta bar. Both slots are always listed; the one the
   device is actually booted from is lifted out of the dimmed meta colour and
   given a "live" badge, so which image is running is readable at a glance
   rather than inferred from a version suffix. */
.fw-slot { white-space: nowrap; }
.fw-slot-live {
  color: var(--text);
  font-weight: 600;
}
.fw-slot-badge {
  display: inline-block;
  margin-left: 5px;
  padding: 0 5px;
  border: 1px solid var(--success);
  border-radius: 8px;
  color: var(--success);
  font-size: 10px;
  font-weight: 600;
  line-height: 15px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  /* The badge carries the meaning, so don't rely on colour alone. */
  vertical-align: baseline;
}

.rtc-status-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-dim);
}
.rtc-status-bar strong { color: var(--text); }
.rtc-status-bar.warn { border-left-color: var(--warning); color: var(--text); }
.rtc-pill {
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 11px;
  cursor: help;
}
.rtc-pill.rtc-on          { color: var(--success); border-color: var(--success); }
.rtc-pill.rtc-unsupported { color: var(--warning); border-color: var(--warning); }
/* Writes/day inside an RTC pill, banded independently of the pill's RTC colour. */
.write-rate-high { color: var(--danger);  font-weight: 600; }
.write-rate-mid  { color: var(--warning); font-weight: 600; }
.write-rate-ok   { color: var(--success); }

.inverter-details-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.inverter-detail-chip {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 11px;
  color: var(--text-dim);
}
.inverter-detail-chip strong { color: var(--text); font-weight: 600; }

/* ── Shelly / Ogemray devices ──────────────────────────── */
.shelly-devices-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}
.shelly-devices-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 12px;
}
.shelly-devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.shelly-device-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.shelly-device-heading { font-weight: 600; font-size: 13px; }
.shelly-device-meta { font-size: 11px; color: var(--text-dim); }
.shelly-device-live { display: flex; align-items: center; font-size: 12px; color: var(--text-dim); }
.shelly-device-form { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.shelly-device-form select,
.shelly-device-form input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  padding: 5px 8px;
  font-size: 13px;
}
.shelly-device-form select:focus,
.shelly-device-form input:focus { outline: none; border-color: var(--grid); }

/* Per-inverter live solar/grid/battery/SOC, appended below the static spec
   line on each chip when the device publishes individualTelemetry. Reuses
   --solar/--grid/--battery and the .metric-direction/.soc-bar classes from
   the totals panels below — no new colours. */
.inverter-detail-chip .live-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}
.inverter-detail-chip .live-metric {
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 4px;
}
.inverter-detail-chip .live-metric.solar { color: var(--solar); }
.inverter-detail-chip .soc-bar-container.mini {
  width: 40px;
  height: 5px;
  margin: 0;
}

/* ── Main layout ───────────────────────────────────────── */
main { padding: 20px; max-width: 1200px; margin: 0 auto; }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-dim);
}

/* ── Panels ────────────────────────────────────────────── */
.panels-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}

@media (max-width: 900px) { .panels-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 500px) { .panels-grid { grid-template-columns: 1fr; } }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.panel-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 12px;
}

.panel-solar .panel-title { color: var(--solar); }
.panel-battery .panel-title { color: var(--battery); }
.panel-grid .panel-title { color: var(--grid); }
.panel-load .panel-title { color: var(--load); }

.panel-solar { border-top: 3px solid var(--solar); }
.panel-battery { border-top: 3px solid var(--battery); }
.panel-grid { border-top: 3px solid var(--grid); }
.panel-load { border-top: 3px solid var(--load); }

.metric { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.metric:last-child { margin-bottom: 0; }
.metric-label { color: var(--text-dim); font-size: 12px; min-width: 70px; }
.metric-value { font-size: 15px; font-weight: 600; }
.metric-direction { font-size: 12px; color: var(--text-dim); }
.metric-direction.charging, .metric-direction.exporting { color: var(--battery); }
.metric-direction.discharging, .metric-direction.importing { color: var(--danger); }

/* Solar breakdown (per-inverter MPPT + generation-role Shelly rows) */
.solar-breakdown {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.solar-breakdown-row { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; }
.solar-breakdown-label { color: var(--text-dim); }
.solar-breakdown-value { font-weight: 600; }

/* SoC bar */
.soc-bar-container {
  height: 6px;
  background: var(--bg);
  border-radius: 3px;
  margin: 6px 0 10px;
  overflow: hidden;
}
.soc-bar {
  height: 100%;
  border-radius: 3px;
  background: var(--battery);
  transition: width 0.5s ease;
}
.soc-bar.low { background: var(--danger); }
.soc-bar.mid { background: var(--warning); }

/* ── Settings panel ────────────────────────────────────── */
.settings-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.settings-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.setting { display: flex; flex-direction: column; gap: 4px; }
.setting-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.setting-value { font-size: 14px; font-weight: 500; }

/* ── Controls ──────────────────────────────────────────── */
.controls-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.controls-panel > summary {
  padding: 14px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  user-select: none;
}

.controls-panel > summary:hover { color: var(--text); }
.controls-panel[open] > summary { border-bottom: 1px solid var(--border); }

.controls-grid {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

.control-section { border-top: 1px solid var(--border); }
.control-section:first-of-type { border-top: none; }
.section-heading {
  margin: 0;
  padding: 14px 16px 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.control-section.control-section-danger .section-heading { color: var(--danger); }
.control-section.control-section-danger { background: rgba(252, 129, 129, 0.04); }

.control-group h3 { font-size: 13px; color: var(--text-dim); margin-bottom: 8px; }
.control-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.control-form input[type="time"],
.control-form input[type="number"],
.control-form input[type="text"],
.control-form input[type="url"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  padding: 5px 8px;
  font-size: 13px;
}
.control-form input:focus { outline: none; border-color: var(--grid); }

/* ── OTA firmware form ──
   Stacked form (full-width URL field); spans the whole firmware panel row. */
.firmware-grid { padding: 16px; }
.ota-form { display: flex; flex-direction: column; gap: 12px; max-width: 640px; }
.ota-form label { display: flex; flex-direction: column; gap: 4px; }
.ota-form input[type="text"],
.ota-form input[type="url"],
.ota-form select,
#reboot-target {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
#reboot-target { margin-bottom: 8px; }
.ota-form input:focus,
.ota-form select:focus,
#reboot-target:focus { outline: none; border-color: var(--grid); }
.ota-form select[disabled] { opacity: 0.5; }
.ota-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ota-actions .hint { margin-top: 0; flex: 1; min-width: 240px; }
#ota-submit[disabled] { opacity: 0.5; cursor: wait; }

.control-group-danger { border-top: 1px solid var(--border); padding-top: 16px; }
.hint { font-size: 11px; color: var(--text-dim); margin-top: 6px; }

.reg-output {
  margin-top: 8px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.4;
  max-height: 360px;
  overflow: auto;
  white-space: pre;
  color: var(--text);
}
/* Opt-in wrap for free-text output (device logs) — the register-dump / scan
   layouts that also use .reg-output rely on fixed-width columns and stay
   unwrapped by default. */
.reg-output.wrap {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.logs-wrap-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  margin-top: 8px;
}

.command-status {
  margin: 0 16px 16px;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 13px;
}
.command-status.success { background: rgba(104, 211, 145, 0.15); color: var(--success); }
.command-status.error { background: rgba(252, 129, 129, 0.15); color: var(--danger); }

/* ── History charts ─────────────────────────────────────── */
.charts-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-top: 16px;
}

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

.charts-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.range-btns { display: flex; gap: 6px; }

.range-btn { color: var(--text-dim); }
.range-btn.active { background: #3b5bdb; border-color: #3b5bdb; color: #fff; }

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

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

.chart-container {
  position: relative;
  height: 220px;
}

/* ── Modal popup ─────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.6);
}
.modal-overlay[hidden] { display: none; }

.modal-box {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 640px;
  max-height: 85vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 15px; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.modal-close:hover { color: var(--text); }

.modal-body { padding: 16px; overflow: auto; }
/* In a modal the output uses the full available height (the modal body scrolls). */
.modal-body .reg-output { margin-top: 0; max-height: none; }

/* ── Network scan results table ──────────────────────────── */
.scan-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.scan-table th,
.scan-table td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.scan-table th {
  color: var(--text-dim);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.scan-table td {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.scan-table tr.scan-inverter td { color: var(--success); }
.scan-table tr.scan-self td { color: var(--text-dim); }

/* Single-host probe grid: colour each connect outcome. */
.scan-table td.probe-ok      { color: var(--success); }
.scan-table td.probe-slow    { color: var(--warning); }
.scan-table td.probe-refused { color: var(--danger); }
.scan-table td.probe-fail    { color: var(--text-dim); }
