:root {
  --bg: #0b0d10;
  --surface: #12151b;
  --surface-2: #181c24;
  --border: #232934;
  --border-strong: #323a49;
  --text: #e6ebf2;
  --text-muted: #8b95a7;
  --text-dim: #5e6778;
  --emerald: #10b981;
  --emerald-bg: rgba(16, 185, 129, 0.12);
  --amber: #f59e0b;
  --amber-bg: rgba(245, 158, 11, 0.12);
  --red: #ef4444;
  --red-bg: rgba(239, 68, 68, 0.12);
  --blue: #3b82f6;
  --blue-bg: rgba(59, 130, 246, 0.14);
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, monospace;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.muted {
  color: var(--text-muted);
  margin: 0;
}

.hidden {
  display: none !important;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: #202632;
  border-color: #434d60;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-xs { padding: 4px 8px; font-size: 11.5px; }
.btn-full { width: 100%; padding: 10px 14px; font-size: 14px; }

.btn-primary {
  background: var(--text);
  color: #0b0d10;
  border-color: var(--text);
}
.btn-primary:hover:not(:disabled) {
  background: #ffffff;
  border-color: #ffffff;
}

.btn-emerald {
  background: var(--emerald-bg);
  color: var(--emerald);
  border-color: rgba(16, 185, 129, 0.35);
}
.btn-emerald:hover:not(:disabled) {
  background: rgba(16, 185, 129, 0.2);
  border-color: rgba(16, 185, 129, 0.55);
}

.btn-danger {
  background: var(--red-bg);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.35);
}
.btn-danger:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.55);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border-strong);
}

/* Auth View */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 26px;
}

.auth-header {
  margin-bottom: 20px;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  margin-bottom: 10px;
}

.brand-dot, .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  display: inline-block;
  flex-shrink: 0;
}

.status-dot.on, .brand-dot.on {
  background: var(--emerald);
}

.status-dot.off {
  background: var(--amber);
}

.auth-header h1 {
  margin: 0 0 6px;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.qr-setup-box {
  margin-bottom: 18px;
  padding: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.qr-frame {
  width: 184px;
  height: 184px;
  margin: 0 auto 12px;
  padding: 8px;
  background: #ffffff;
  border-radius: 6px;
}

.qr-frame svg {
  width: 100%;
  height: 100%;
  display: block;
}

.secret-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.secret-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.secret-value-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 8px;
}

.secret-value-wrap code {
  font-size: 11.5px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
}

.otp-inputs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 14px;
}

.otp-digit {
  width: 44px;
  height: 50px;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 600;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  outline: none;
  transition: border-color 0.12s ease;
}

.otp-digit:focus {
  border-color: var(--text);
}

.otp-sep {
  width: 8px;
  height: 2px;
  background: var(--border-strong);
  border-radius: 1px;
}

.auth-error {
  margin-bottom: 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--red-bg);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  font-size: 12.5px;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(11, 13, 16, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

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

.server-title {
  font-weight: 600;
  font-size: 14.5px;
  letter-spacing: -0.01em;
}

.pill {
  font-size: 11.5px;
  padding: 2px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
}

.pill.on {
  color: var(--emerald);
  border-color: rgba(16, 185, 129, 0.35);
  background: var(--emerald-bg);
}

.pill.off {
  color: var(--amber);
  border-color: rgba(245, 158, 11, 0.35);
  background: var(--amber-bg);
}

.sync-meta {
  font-size: 11.5px;
  color: var(--text-dim);
}

/* Toggle */
.toggle-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.toggle-wrap input {
  display: none;
}

.toggle-track {
  width: 28px;
  height: 16px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  position: relative;
  transition: background 0.15s ease;
}

.toggle-track::after {
  content: "";
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  position: absolute;
  top: 2px;
  left: 2px;
  transition: transform 0.15s ease, background 0.15s ease;
}

.toggle-wrap input:checked + .toggle-track {
  background: var(--emerald-bg);
  border-color: rgba(16, 185, 129, 0.45);
}

.toggle-wrap input:checked + .toggle-track::after {
  transform: translateX(12px);
  background: var(--emerald);
}

.toggle-label {
  font-size: 11.5px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* Container & Layout */
.container {
  max-width: 1320px;
  margin: 0 auto;
  padding: 18px 20px 36px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.control-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.control-title {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.btn-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* KPI Strip */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.kpi-value {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}

.kpi-sub {
  font-size: 11.5px;
  color: var(--text-dim);
}

/* Main Grid */
.main-grid {
  display: grid;
  grid-template-columns: 1.45fr 1fr;
  gap: 16px;
  align-items: start;
}

.col-main, .col-side {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.panel-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.panel-head h2 {
  margin: 0;
  font-size: 13.5px;
  font-weight: 600;
}

.panel-meta {
  font-size: 11.5px;
  color: var(--text-dim);
}

/* Sensor List */
.sensor-list {
  display: flex;
  flex-direction: column;
}

.sensor-row {
  display: grid;
  grid-template-columns: 165px 1fr 78px 105px;
  align-items: center;
  gap: 12px;
  padding: 9px 16px;
  border-bottom: 1px solid rgba(35, 41, 52, 0.55);
  font-size: 13px;
}

.sensor-row:last-child {
  border-bottom: none;
}

.sensor-name {
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sensor-bar-wrap {
  height: 5px;
  background: var(--bg);
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.sensor-bar {
  height: 100%;
  background: var(--emerald);
  transition: width 0.25s ease;
}

.sensor-bar.warn { background: var(--amber); }
.sensor-bar.crit { background: var(--red); }
.sensor-bar.na { width: 0% !important; }

.sensor-val {
  text-align: right;
  font-weight: 600;
}

.sensor-val.na {
  color: var(--text-dim);
  font-weight: 400;
}

.sensor-limits {
  text-align: right;
  font-size: 11px;
  color: var(--text-dim);
}

/* Fans Grid */
.fan-grid {
  padding: 12px 16px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.fan-tile {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}

.fan-name {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.fan-val {
  font-size: 14px;
  font-weight: 600;
}

.fan-val.ok { color: var(--emerald); }
.fan-val.na { color: var(--text-dim); font-weight: 400; }

/* KV List */
.kv-list {
  margin: 0;
  padding: 4px 16px;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(35, 41, 52, 0.55);
  font-size: 12.5px;
}

.kv-row:last-child {
  border-bottom: none;
}

.kv-row dt {
  color: var(--text-muted);
}

.kv-row dd {
  margin: 0;
  color: var(--text);
  text-align: right;
  word-break: break-all;
}

/* Health Grid */
.health-grid {
  padding: 10px 16px 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.health-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
}

.health-badge {
  font-size: 11px;
  font-family: var(--font-mono);
  padding: 1px 6px;
  border-radius: 4px;
}

.health-badge.ok { color: var(--emerald); background: var(--emerald-bg); }
.health-badge.bad { color: #f87171; background: var(--red-bg); }
.health-badge.na { color: var(--text-dim); background: var(--surface-2); }

/* SEL List */
.sel-list {
  max-height: 290px;
  overflow-y: auto;
}

.sel-row {
  padding: 8px 16px;
  border-bottom: 1px solid rgba(35, 41, 52, 0.55);
  font-size: 12px;
}

.sel-row:last-child {
  border-bottom: none;
}

.sel-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
}

.sel-sensor {
  font-weight: 500;
  color: var(--text);
}

.sel-time {
  font-size: 11px;
  color: var(--text-dim);
}

.sel-event {
  color: var(--text-muted);
  font-size: 11.5px;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 10, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 20px;
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

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

.modal-head h3 {
  margin: 0;
}

.modal-qr p {
  margin-bottom: 14px;
  font-size: 12.5px;
}

.modal-actions {
  margin-top: 18px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-actions-split {
  justify-content: space-between;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 60;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 7px;
  font-size: 13px;
  max-width: 360px;
}

.toast.error {
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .main-grid { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .fan-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sensor-row { grid-template-columns: 130px 1fr 64px; }
  .sensor-limits { display: none; }
  .health-grid { grid-template-columns: 1fr; }
}
