:root {
  --bg: #f5f6f8;
  --panel-bg: #ffffff;
  --border: #d8dce2;
  --text: #1c2128;
  --muted: #6b7280;
  --accent: #2563eb;
  --rx: #059669;
  --tx: #2563eb;
  --warn-bg: #fff7e6;
  --warn-border: #d97706;
  --error-bg: #fdecec;
  --error-border: #dc2626;
  --mono: "Cascadia Mono", "Consolas", "SFMono-Regular", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --panel-bg: #171b21;
    --border: #2b313a;
    --text: #e6e8eb;
    --muted: #8b93a0;
    --accent: #60a5fa;
    --rx: #34d399;
    --tx: #60a5fa;
    --warn-bg: #2a2210;
    --warn-border: #d97706;
    --error-bg: #2a1414;
    --error-border: #ef4444;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-bg);
}

header h1 {
  font-size: 1.1rem;
  margin: 0;
}

.status {
  font-size: 0.8rem;
  color: var(--muted);
}
.status.ok { color: var(--rx); }
.status.down { color: var(--error-border); }

main {
  flex: 1;
  display: flex;
  min-height: 0;
}

#sidebar {
  width: 320px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--panel-bg);
  display: flex;
  flex-direction: column;
  min-height: 0; /* lets the inner scroller actually scroll instead of growing the column */
}

/* Everything above the docked Latest Command block scrolls on its own. */
#sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
}

#sidebar h2 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0 0 8px;
}

#sidebar section + section {
  margin-top: 24px;
}

.hint {
  font-size: 0.76rem;
  color: var(--muted);
  margin: 0 0 10px;
  line-height: 1.4;
}

#device-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#device-list li {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.82rem;
  cursor: pointer;
}

#device-list li:hover:not(.empty) {
  border-color: var(--accent);
}

#device-list li.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--panel-bg));
}

#device-list li.empty {
  color: var(--muted);
  border-style: dashed;
  cursor: default;
}

#device-list .imei {
  font-family: var(--mono);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

#device-list .meta {
  color: var(--muted);
  margin-top: 2px;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot.online { background: var(--rx); }
.dot.offline { background: var(--muted); }

.queued-badge {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--warn-border);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: 10px;
  padding: 1px 8px;
}

#send-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#send-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  color: var(--muted);
}

#send-form input,
#send-form select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

#send-form button {
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

#send-form button:hover { opacity: 0.9; }

#send-status {
  font-size: 0.8rem;
  min-height: 1.2em;
}
#send-status.ok { color: var(--rx); }
#send-status.err { color: var(--error-border); }

#log-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.log-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
}

.log-header h2 {
  font-size: 0.9rem;
  margin: 0;
  flex: 1;
}

.autoscroll {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 4px;
}

#clear-log {
  font-size: 0.8rem;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  color: var(--text);
  cursor: pointer;
}

.log-scope {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 20px;
  font-size: 0.78rem;
  color: var(--muted);
  background: color-mix(in srgb, var(--accent) 8%, var(--panel-bg));
  border-bottom: 1px solid var(--border);
}

.log-scope strong {
  font-family: var(--mono);
  color: var(--text);
}

.overview {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.overview-header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 10px;
}

.overview-header h2 {
  font-size: 0.85rem;
  margin: 0;
}

#overview-meta {
  font-size: 0.78rem;
  color: var(--muted);
  font-family: var(--mono);
}

/* Docked at the bottom of the sidebar, directly under the send form, so firing a command and
   seeing whether it landed happen in the same column. */
#latest-command {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  background: var(--bg); /* sits slightly apart from the --panel-bg sidebar above it */
}

#latest-command-body {
  max-height: 40vh; /* a long multi-config reply must not crowd the device list off-screen */
  overflow-y: auto;
}

.latest-command-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.latest-command-head h2 {
  margin: 0; /* the rest is inherited from the shared #sidebar h2 rule */
}

.cmd-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.74rem;
  font-family: var(--mono);
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.cmd-status.waiting { border-color: var(--warn-border); color: var(--warn-border); }
.cmd-status.ok { border-color: var(--rx); color: var(--rx); }
.cmd-status.failed { border-color: var(--error-border); background: var(--error-bg); color: var(--error-border); }

.cmd-status .pending-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn-border);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn-border) 25%, transparent);
  flex-shrink: 0;
}

/* seq=0x05 · 2026-08-04 13:05:54 · 862596089245157 */
.cmd-meta {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 6px;
  overflow-wrap: anywhere;
}

.cmd-block + .cmd-block {
  margin-top: 8px;
}

.cmd-block-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 2px;
}

.cmd-block-text {
  font-family: var(--mono);
  font-size: 0.76rem;
  line-height: 1.45;
  white-space: pre-wrap; /* renders the '\n'-joined reply fields one per line */
  overflow-wrap: anywhere;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
}

.cmd-empty {
  font-size: 0.76rem;
  color: var(--muted);
}

.alarm-panel {
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.alarm-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.alarm-panel-head h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0;
}

.alarm-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.alarm-grid:empty::after {
  content: "(no alarm types known)";
  color: var(--muted);
  font-size: 0.78rem;
}

.alarm-icon {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-bg);
  color: var(--muted);
  font-size: 0.76rem;
  cursor: default;
}

.alarm-icon .alarm-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}

.alarm-icon.active {
  border-color: var(--error-border);
  background: var(--error-bg);
  color: var(--error-border);
  font-weight: 600;
  cursor: pointer;
}

.alarm-icon.active .alarm-dot {
  background: var(--error-border);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--error-border) 25%, transparent);
}

.alarm-icon.active:hover {
  opacity: 0.85;
}

@keyframes alarm-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.alarm-icon.blink {
  animation: alarm-blink 0.35s ease-in-out 4;
}

.overview-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 10px;
}

.overview-empty {
  color: var(--muted);
  font-size: 0.82rem;
  grid-column: 1 / -1;
}

.overview-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--panel-bg);
}

.overview-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.overview-card-time {
  font-size: 0.72rem;
  color: var(--muted);
  font-family: var(--mono);
}

.overview-card-fields {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
}

.overview-card-fields li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.overview-card-fields .field-label {
  color: var(--muted);
  flex-shrink: 0;
}

.overview-card-fields .field-value {
  font-family: var(--mono);
  text-align: right;
  overflow-wrap: anywhere;
}

.overview-card-fields li.subitem {
  justify-content: flex-start;
  padding-left: 10px;
  border-left: 2px solid var(--border);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.78rem;
}

.filters {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-bg);
}

.filter-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 0.78rem;
}

.filter-label {
  color: var(--muted);
  width: 90px;
  flex-shrink: 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  user-select: none;
  background: var(--bg);
}

.chip input {
  margin: 0;
}

.chip:has(input:checked) {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

#filter-types {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.filter-row input[type="datetime-local"] {
  font: inherit;
  font-size: 0.76rem;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.link-btn {
  font-size: 0.76rem;
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

#log {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#log.filtered-empty::after {
  content: "no events match the current filters";
  color: var(--muted);
  font-size: 0.85rem;
}

.entry {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 6px;
  padding: 8px 12px;
  background: var(--panel-bg);
  font-size: 0.85rem;
  /* Off-screen entries skip layout/paint entirely - without this, every appended event re-lays-out
     the whole (up to MAX_LIVE_ROWS) log, which is what made a long-running dashboard tab drag. */
  content-visibility: auto;
  contain-intrinsic-size: auto 90px;
}

.entry.rx { border-left-color: var(--rx); }
.entry.tx { border-left-color: var(--tx); }
.entry.warn { background: var(--warn-bg); border-left-color: var(--warn-border); }
.entry.error { background: var(--error-bg); border-left-color: var(--error-border); }

.entry-head {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
  flex-wrap: wrap;
}

.entry-head .dir {
  font-weight: 700;
}
.entry.rx .dir { color: var(--rx); }
.entry.tx .dir { color: var(--tx); }

.entry-head .pending-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn-border);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn-border) 25%, transparent);
  flex-shrink: 0;
}

.entry-head .type-badge {
  font-family: system-ui, sans-serif;
  font-weight: 600;
  font-size: 0.72rem;
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
}

.entry-lines {
  font-family: var(--mono);
  white-space: pre-wrap;
  margin-top: 4px;
  line-height: 1.4;
}

.entry details {
  margin-top: 4px;
}

.entry summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.75rem;
}

.entry .raw-hex {
  font-family: var(--mono);
  font-size: 0.72rem;
  word-break: break-all;
  color: var(--muted);
  margin-top: 4px;
}
