:root {
  --bg: #f5f7f9;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #64748b;
  --line: #dbe3ea;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --danger: #b42318;
  --warn: #a16207;
  --ok: #15803d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 232px;
  background: #111827;
  color: #fff;
  padding: 22px 16px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 28px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: linear-gradient(135deg, #14b8a6, #f59e0b);
}

.brand strong, .brand span { display: block; }
.brand span { color: #a8b3c4; font-size: 13px; margin-top: 3px; }

.nav {
  display: block;
  width: 100%;
  margin: 6px 0;
  padding: 11px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #dbe3ea;
  text-align: left;
  cursor: pointer;
  font-size: 15px;
}

.nav.active, .nav:hover { background: #263244; color: #fff; }

.main {
  margin-left: 232px;
  padding: 28px;
}

.view { display: none; }
.view.active { display: block; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 18px;
}

h1 { margin: 0; font-size: 26px; }
p { margin: 6px 0 0; color: var(--muted); }

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 14px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); color: var(--accent-strong); }

button:disabled {
  cursor: wait;
  opacity: .65;
}

.row-action {
  white-space: nowrap;
  padding: 7px 10px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 12px;
  margin: 18px 0;
}

.stats div, .filters, .table-wrap, .settings-grid, .log-list, .config-tabs, .module-tabs, .log-toolbar {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.stats div { padding: 16px; }
.stats span { display: block; font-size: 28px; font-weight: 700; }
.stats label { color: var(--muted); font-size: 13px; }

.filters {
  display: grid;
  grid-template-columns: repeat(5, minmax(150px, 1fr));
  gap: 12px;
  padding: 14px;
  margin-bottom: 14px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 10px;
  font: inherit;
  color: var(--text);
  background: #fff;
}

.enterprise-label {
  position: relative;
  min-width: 0;
}

.enterprise-combo {
  position: relative;
  min-width: 0;
}

.enterprise-control {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-width: 0;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
  background: #fff;
}

.enterprise-combo.open .enterprise-control {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, .12);
}

.enterprise-chips {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

.enterprise-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  max-width: 100%;
  flex: 1 1 auto;
  height: 30px;
  padding: 0 9px;
  border: 0;
  border-radius: 6px;
  background: #eef2f7;
  color: var(--text);
}

.enterprise-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.enterprise-chip b {
  color: var(--muted);
  font-weight: 500;
}

.enterprise-more {
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 9px;
  border-radius: 6px;
  background: #eef2f7;
  color: var(--muted);
  font-weight: 700;
}

#enterpriseSearch {
  min-width: 56px;
  height: 30px;
  flex: 1 1 72px;
  border: 0;
  border-radius: 0;
  padding: 0;
  outline: 0;
}

.enterprise-combo.has-selection #enterpriseSearch {
  min-width: 0;
  width: 12px;
  flex: 0 1 12px;
}

.enterprise-toggle {
  flex: 0 0 auto;
  border: 0;
  padding: 0 2px;
  color: var(--muted);
  background: transparent;
  font-size: 18px;
  line-height: 1;
}

.enterprise-menu {
  position: absolute;
  z-index: 15;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  display: none;
  max-height: 320px;
  overflow: auto;
  padding: 8px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 34px rgba(15, 23, 42, .12);
}

.enterprise-combo.open .enterprise-menu {
  display: block;
}

.enterprise-option {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 0;
  padding: 10px 14px;
  text-align: left;
  background: #fff;
  color: var(--text);
}

.enterprise-option:hover {
  background: #f1f5f9;
  color: var(--accent-strong);
}

.enterprise-empty {
  padding: 10px 14px;
  color: var(--muted);
}

.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 1160px; }
th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 600; background: #f8fafc; }
th[data-sort], th[data-alarm-sort] { cursor: pointer; user-select: none; }
th[data-sort].active-sort, th[data-alarm-sort].active-sort { color: var(--accent-strong); }
td { font-size: 14px; }

.mismatch-cell {
  max-width: 340px;
  color: var(--danger);
}

.empty-row {
  padding: 28px 12px;
  color: var(--muted);
  text-align: center;
}

.row-actions {
  min-width: 178px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.badge {
  display: inline-flex;
  align-items: center;
  height: 24px;
  border-radius: 999px;
  padding: 0 9px;
  font-size: 12px;
  font-weight: 700;
}

.badge.normal { color: var(--ok); background: #dcfce7; }
.badge.abnormal { color: var(--danger); background: #fee2e2; }
.badge.pending { color: var(--warn); background: #fef3c7; }
.badge.pending_query { color: var(--warn); background: #fef3c7; }
.badge.mismatch_pending_set { color: var(--danger); background: #fee2e2; }
.badge.set_success { color: var(--ok); background: #dcfce7; }
.badge.query_failed_retry, .badge.set_failed_retry, .badge.offline_pending {
  color: #9a3412;
  background: #ffedd5;
}

.module-tabs {
  display: flex;
  gap: 8px;
  padding: 8px;
  margin: 0 0 12px;
}

.module-tab.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.alarm-pane { display: none; }
.alarm-pane.active { display: block; }

.alarm-filters {
  grid-template-columns: repeat(4, minmax(150px, 1fr));
}

.log-filters {
  grid-template-columns: repeat(5, minmax(150px, 1fr));
}

.log-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 12px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 13px;
}

.terminal-log {
  height: min(62vh, 680px);
  min-height: 420px;
  overflow: auto;
  padding: 14px 16px;
  border: 1px solid #111827;
  border-radius: 8px;
  background: #05080d;
  color: #d1d5db;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.55;
}

.terminal-line {
  white-space: pre-wrap;
  word-break: break-word;
}

.terminal-line.warn { color: #fde68a; }
.terminal-line.error { color: #fecaca; }
.terminal-line.info { color: #d1fae5; }

.config-tabs {
  display: flex;
  gap: 8px;
  padding: 8px;
  margin-bottom: 12px;
}

.config-tab.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.settings-grid {
  display: block;
  padding: 18px;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin: 12px 0 0;
}

.pagination label {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  flex-direction: row;
  gap: 6px;
}

.pagination select {
  width: auto;
}

.settings-page {
  display: none;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 14px;
}

.settings-page.active { display: grid; }

.settings-page.module-page {
  grid-template-columns: 1fr;
}

.wide { grid-column: 1 / -1; }

.config-note {
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #f8fafc;
}

.config-note strong {
  font-size: 15px;
}

.config-note span {
  color: var(--muted);
  font-size: 13px;
}

.config-section {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.config-section h2 {
  margin: 0;
  font-size: 16px;
}

.section-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 14px;
}

.log-list { padding: 8px; }
.log-item { padding: 12px; border-bottom: 1px solid var(--line); }
.log-item:last-child { border-bottom: 0; }
.log-item strong { margin-right: 8px; }
.log-item small { color: var(--muted); }
.log-item pre { white-space: pre-wrap; margin: 8px 0 0; color: var(--muted); }

.toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  max-width: 420px;
  padding: 12px 14px;
  background: #111827;
  color: #fff;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: .18s ease;
}

.toast.show { opacity: 1; transform: translateY(0); }

.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: stretch;
  justify-content: flex-end;
  background: rgba(15, 23, 42, .36);
  z-index: 20;
}

.modal.open { display: flex; }

.modal-panel {
  width: min(920px, 100%);
  height: 100%;
  background: var(--bg);
  border-left: 1px solid var(--line);
  padding: 22px;
  overflow: auto;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.modal-head h2 {
  margin: 0;
  font-size: 22px;
}

@media (max-width: 900px) {
  .sidebar { position: static; width: auto; }
  .main { margin-left: 0; padding: 18px; }
  .topbar { display: block; }
  .modal-head { display: block; }
  .actions { justify-content: flex-start; margin-top: 14px; }
  .stats, .filters, .settings-page, .section-grid, .alarm-filters, .log-filters { grid-template-columns: 1fr; }
  .config-tabs, .module-tabs { overflow-x: auto; }
  .log-toolbar { display: block; }
  .terminal-log { min-height: 360px; }
}
