:root {
  color-scheme: dark;
  --bg: #0b1016;
  --bg-alt: #0d131b;
  --panel: #111927;
  --panel-2: #0f1623;
  --line: #243246;
  --line-strong: #324660;
  --text: #e8eef8;
  --muted: #95a3ba;
  --accent: #4f8cff;
  --accent-dark: #3f7ef1;
  --accent-soft: rgba(79, 140, 255, 0.14);
  --ok-bg: #10281f;
  --ok-line: #21563d;
  --ok-text: #b5efd1;
  --danger: #70303d;
  --danger-bg: #31161b;
  --danger-text: #ffc4cf;
  --warn-bg: #2a2312;
  --warn-line: #5d4c18;
  --warn-text: #f4d58d;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-alt) 100%);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  border: 1px solid transparent;
  border-radius: 12px;
  background: linear-gradient(180deg, #5d97ff 0%, #3f7ef1 100%);
  box-shadow: 0 10px 24px rgba(37, 101, 214, 0.28);
  color: #f7fbff;
  cursor: pointer;
  min-height: 36px;
  padding: 8px 14px;
  font-weight: 700;
}

button:hover {
  background: linear-gradient(180deg, #70a4ff 0%, #4f8cff 100%);
}

button.secondary {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.04);
  box-shadow: none;
  color: #dbe8ff;
}

button.secondary:hover {
  background: rgba(79, 140, 255, 0.14);
}

button.danger {
  border-color: var(--danger);
  background: linear-gradient(180deg, #8d3948 0%, #70303d 100%);
  color: var(--danger-text);
}

.link-button {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  color: #dbe8ff;
  padding: 8px 14px;
  font-weight: 700;
  text-decoration: none;
}

.link-button:hover {
  background: rgba(79, 140, 255, 0.14);
}

button:disabled {
  cursor: wait;
  opacity: 0.65;
  border-color: #283548;
  background: #202b3a;
  box-shadow: none;
  color: #7e8aa0;
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  padding: 8px 10px;
}

input::placeholder,
textarea::placeholder {
  color: #6f809a;
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.18);
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
}

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

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-weight: 600;
}

pre {
  min-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  color: #cdd7e6;
  padding: 12px;
}

.result-box {
  min-height: 96px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  padding: 12px;
}

.device-telemetry {
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(17, 25, 39, 0.96) 0%, rgba(13, 19, 29, 0.96) 100%);
  padding: 12px;
}

.device-config-events {
  display: grid;
  gap: 10px;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.03);
  padding: 12px;
}

.config-events-table {
  min-width: 860px;
}

.config-events-table code {
  color: #cfe0ff;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}

.result-box pre {
  min-height: 0;
  margin: 0;
}

.progress {
  overflow: hidden;
  height: 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
}

.progress span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #7fc7ff);
  transition: width 0.2s ease;
}

.flash-log {
  min-height: 180px;
  white-space: pre-wrap;
}

.telemetry-table {
  min-width: 760px;
}

.config-table {
  min-width: 640px;
}

.telemetry-table td,
.telemetry-table th,
.config-table td,
.config-table th {
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

.shell {
  min-height: 100vh;
}

.login {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.login .panel {
  width: min(420px, 100%);
}

.app {
  padding: 24px;
}

.topbar,
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.section-head.compact {
  margin-bottom: 0;
}

.topbar h1,
.section-head h2,
.panel h2,
.panel h3 {
  margin: 0;
  color: #f7fbff;
}

.topbar span {
  color: var(--muted);
}

.topbar button {
  align-self: center;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tabs button {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.04);
  box-shadow: none;
  color: #dbe8ff;
}

.tabs button.active {
  border-color: rgba(79, 140, 255, 0.28);
  background: var(--accent-soft);
  color: #f7fbff;
}

.tab {
  display: none;
}

.tab.active {
  display: block;
}

.grid {
  display: grid;
  gap: 16px;
}

.grid.two {
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.8fr);
}

.flash-grid {
  margin-top: 16px;
}

.panel {
  display: grid;
  gap: 12px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(20, 29, 42, 0.98) 0%, rgba(16, 24, 36, 0.98) 100%);
  box-shadow: var(--shadow);
  padding: 16px;
}

.stack {
  display: grid;
  gap: 12px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check input {
  width: auto;
}

.actions > input,
.actions > select {
  width: auto;
  min-width: 160px;
}

.muted {
  color: var(--muted);
}

.status {
  display: inline-flex;
  min-width: 64px;
  justify-content: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 700;
}

.status.online {
  border: 1px solid var(--ok-line);
  background: var(--ok-bg);
  color: var(--ok-text);
}

.status.offline {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
}

.status.registered {
  margin-left: 6px;
  border: 1px solid var(--ok-line);
  background: var(--ok-bg);
  color: var(--ok-text);
}

.status.unregistered {
  margin-left: 6px;
  border: 1px solid rgba(255, 190, 92, 0.45);
  background: rgba(255, 190, 92, 0.12);
  color: #ffd18a;
}

.mode-pill {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  border: 1px solid rgba(255, 190, 92, 0.45);
  border-radius: 999px;
  padding: 0 12px;
  color: #ffd18a;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.mode-pill.enabled {
  border-color: var(--ok-line);
  color: var(--ok-text);
}

.error {
  min-height: 20px;
  margin: 0;
  color: var(--danger-text);
}

tr.clickable {
  cursor: pointer;
}

tr.clickable:hover {
  background: rgba(79, 140, 255, 0.08);
}

tr.selected {
  background: rgba(79, 140, 255, 0.14);
}

.device-list-panel {
  overflow: auto;
  padding: 0;
}

.manual-device-form {
  display: grid;
  grid-template-columns: 1fr 1.4fr auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 14px;
}

.manual-device-form h3,
.manual-device-form p {
  grid-column: 1 / -1;
  margin: 0;
}

.devices-table th,
.devices-table td {
  padding: 12px 16px;
}

.sort-header {
  display: block;
  width: 100%;
  min-height: 0;
  padding: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  text-align: left;
  box-shadow: none;
}

.sort-header:hover,
.sort-header.active {
  background: transparent;
  color: var(--accent);
  box-shadow: none;
}

.device-row.selected td {
  border-bottom-color: transparent;
}

.device-details-row > td {
  padding: 0 16px 16px;
  background: rgba(13, 19, 29, 0.76);
}

.device-details {
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(20, 29, 42, 0.98) 0%, rgba(16, 24, 36, 0.98) 100%);
  padding: 14px;
}

.board-edit-form {
  display: grid;
  grid-template-columns: minmax(180px, 0.7fr) minmax(240px, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.board-edit-form button {
  white-space: nowrap;
}

.device-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.device-summary > div {
  display: grid;
  gap: 3px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  padding: 10px;
}

.device-summary strong {
  overflow-wrap: anywhere;
}

.device-message {
  min-height: 24px;
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(2, 6, 12, 0.72);
  backdrop-filter: blur(10px);
  padding: 24px;
}

.modal {
  display: grid;
  gap: 16px;
  width: min(1180px, 100%);
  max-height: min(880px, calc(100vh - 48px));
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(20, 29, 42, 0.99) 0%, rgba(13, 20, 31, 0.99) 100%);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.52);
  padding: 18px;
}

.modal > .section-head {
  align-items: flex-start;
  flex-direction: row;
}

.modal > .section-head button {
  align-self: flex-start;
}

.narrow-modal {
  width: min(680px, 100%);
}

.config-form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.config-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(260px, 0.8fr);
  gap: 14px;
}

.config-layout > section {
  display: grid;
  align-content: start;
  gap: 10px;
  min-width: 0;
}

.config-bms-list {
  display: grid;
  gap: 10px;
}

.config-bms-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(100px, 0.7fr) minmax(150px, 1fr) 90px 110px auto;
  gap: 8px;
  align-items: end;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.03);
  padding: 10px;
}

.scan-list {
  display: grid;
  gap: 8px;
}

.scan-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  text-align: left;
  box-shadow: none;
}

.scan-item:hover {
  background: rgba(79, 140, 255, 0.14);
  color: var(--text);
}

.modal-actions {
  justify-content: flex-end;
}

.config-source-list {
  display: grid;
  gap: 10px;
}

.config-source {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.03);
  padding: 12px;
  color: var(--text);
}

.config-source.selected {
  border-color: var(--accent);
  background: rgba(79, 140, 255, 0.14);
}

.config-source.empty {
  opacity: 0.62;
}

.config-source input {
  width: auto;
  margin-top: 4px;
}

.config-source span {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.config-source small {
  width: fit-content;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
  padding: 2px 8px;
  font-weight: 700;
}

.config-source.selected small {
  border: 1px solid rgba(79, 140, 255, 0.28);
  background: var(--accent-soft);
  color: #dbe8ff;
}

.config-source em {
  color: var(--muted);
  font-style: normal;
}

.config-source code {
  overflow-wrap: anywhere;
  color: var(--muted);
}

.config-preview {
  display: grid;
  gap: 12px;
}

@media (max-width: 900px) {
  body {
    font-size: 13px;
  }

  .app {
    padding: 12px;
  }

  .grid.two {
    grid-template-columns: 1fr;
  }

  .device-summary,
  .board-edit-form,
  .config-form-grid,
  .config-layout {
    grid-template-columns: 1fr;
  }

  .config-bms-row {
    grid-template-columns: 1fr;
  }

  .scan-item {
    grid-template-columns: 1fr;
  }

  .topbar,
  .section-head {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar {
    gap: 10px;
  }

  .topbar button,
  .section-head button,
  .actions > button,
  .actions > input,
  .actions > select {
    width: 100%;
  }

  .tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .tabs button,
  button,
  input,
  textarea,
  select {
    min-height: 44px;
  }

  .panel {
    border-radius: 12px;
    padding: 12px;
  }

  .device-list-panel {
    overflow: visible;
    border: 0;
    background: transparent;
    box-shadow: none;
    padding: 0;
  }

  .devices-table,
  .devices-table thead,
  .devices-table tbody,
  .devices-table tr,
  .devices-table th,
  .devices-table td,
  .firmware-table,
  .firmware-table thead,
  #firmware-body,
  #firmware-body tr,
  #firmware-body td,
  #merged-firmware-body,
  #merged-firmware-body tr,
  #merged-firmware-body td,
  .telemetry-table,
  .telemetry-table thead,
  .telemetry-table tbody,
  .telemetry-table tr,
  .telemetry-table th,
  .telemetry-table td,
  .config-table,
  .config-table thead,
  .config-table tbody,
  .config-table tr,
  .config-table th,
  .config-table td {
    display: block;
  }

  .devices-table thead,
  .firmware-table thead,
  .telemetry-table thead,
  .config-table thead {
    display: none;
  }

  .devices-table {
    border-collapse: separate;
    border-spacing: 0 10px;
  }

  .devices-table tr.device-row,
  .telemetry-table tr,
  .config-table tr,
  #firmware-body tr,
  #merged-firmware-body tr {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: linear-gradient(180deg, rgba(20, 29, 42, 0.98) 0%, rgba(16, 24, 36, 0.98) 100%);
    padding: 10px;
  }

  .devices-table tr.device-row.selected {
    border-color: var(--accent);
    background: rgba(79, 140, 255, 0.14);
  }

  .devices-table td,
  .telemetry-table td,
  .config-table td,
  #firmware-body td,
  #merged-firmware-body td {
    display: grid;
    grid-template-columns: minmax(94px, 0.42fr) minmax(0, 1fr);
    gap: 10px;
    align-items: center;
    border-bottom: 0;
    padding: 6px 0;
    overflow-wrap: anywhere;
    white-space: normal;
  }

  .devices-table td::before,
  .telemetry-table td::before,
  .config-table td::before,
  #firmware-body td::before,
  #merged-firmware-body td::before {
    color: var(--muted);
    font-weight: 700;
  }

  .devices-table .device-row td:nth-child(1)::before { content: "State"; }
  .devices-table .device-row td:nth-child(2)::before { content: "MAC"; }
  .devices-table .device-row td:nth-child(3)::before { content: "Version"; }
  .devices-table .device-row td:nth-child(4)::before { content: "Board"; }
  .devices-table .device-row td:nth-child(5)::before { content: "Last check"; }

  .telemetry-table td:nth-child(1)::before { content: "BMS"; }
  .telemetry-table td:nth-child(2)::before { content: "Type"; }
  .telemetry-table td:nth-child(3)::before { content: "MAC"; }
  .telemetry-table td:nth-child(4)::before { content: "BLE"; }
  .telemetry-table td:nth-child(5)::before { content: "Data"; }
  .telemetry-table td:nth-child(6)::before { content: "Voltage"; }
  .telemetry-table td:nth-child(7)::before { content: "Current"; }
  .telemetry-table td:nth-child(8)::before { content: "SOC"; }
  .telemetry-table td:nth-child(9)::before { content: "Temp"; }

  .config-table td:nth-child(1)::before { content: "#"; }
  .config-table td:nth-child(2)::before { content: "Name"; }
  .config-table td:nth-child(3)::before { content: "Type"; }
  .config-table td:nth-child(4)::before { content: "MAC"; }
  .config-table td:nth-child(5)::before { content: "Instance"; }
  .config-table td:nth-child(6)::before { content: "Capacity Ah"; }

  #firmware-body td:nth-child(1)::before { content: "ID"; }
  #firmware-body td:nth-child(2)::before { content: "Version"; }
  #firmware-body td:nth-child(3)::before { content: "File"; }
  #firmware-body td:nth-child(4)::before { content: "Size"; }
  #merged-firmware-body td:nth-child(1)::before { content: "ID"; }
  #merged-firmware-body td:nth-child(2)::before { content: "Version"; }
  #merged-firmware-body td:nth-child(3)::before { content: "File"; }
  #merged-firmware-body td:nth-child(4)::before { content: "Size"; }

  .device-details-row > td {
    display: block;
    padding: 0 0 10px;
    background: transparent;
  }

  .device-details-row > td::before {
    content: "";
  }

  .device-details {
    border-radius: 12px;
    padding: 12px;
  }

  .device-summary {
    gap: 8px;
  }

  .status {
    justify-self: start;
  }

  .device-telemetry,
  .result-box {
    overflow-x: visible;
  }

  .telemetry-table,
  .config-table {
    min-width: 0;
  }

  .telemetry-table tbody,
  .config-table tbody,
  #firmware-body,
  #merged-firmware-body {
    display: grid;
    gap: 10px;
  }

  .modal-backdrop {
    align-items: stretch;
    justify-items: stretch;
    padding: 8px;
  }

  .modal,
  .narrow-modal {
    width: 100%;
    max-height: calc(100vh - 16px);
    border-radius: 14px;
    padding: 12px;
  }

  .modal > .section-head {
    flex-direction: column;
  }

  .modal-actions {
    justify-content: stretch;
  }

  .config-source {
    grid-template-columns: 1fr;
  }

  .config-source input {
    margin: 0;
  }
}

@media (max-width: 520px) {
  .app {
    padding: 8px;
  }

  .topbar h1,
  .section-head h2,
  .panel h2,
  .modal h2 {
    font-size: 20px;
  }

  .devices-table td,
  .telemetry-table td,
  .config-table td,
  #firmware-body td,
  #merged-firmware-body td {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .check {
    min-height: 44px;
  }

  .board-edit-form button {
    white-space: normal;
  }
}
