:root {
  --accent: #e8003d;
  --accent-soft: rgba(232, 0, 61, 0.1);
  --accent-hover: #c40034;
  --text: #111;
  --muted: #6b7280;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --surface: #fff;
  --surface-2: #f9fafb;
  --radius-lg: 20px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 32px rgba(17, 24, 39, 0.08);
  --online: #059669;
  --offline: #9ca3af;
  --provisioned: #2563eb;
  --updating: #d97706;
  --error: #dc2626;
  --page-bg:
    radial-gradient(70% 80% at 80% 10%, rgba(232, 0, 61, 0.12), transparent),
    radial-gradient(50% 90% at 0% 100%, rgba(232, 0, 61, 0.08), transparent),
    #f3f4f6;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--page-bg);
  min-height: 100vh;
  line-height: 1.45;
  font-size: 14px;
}

.page {
  max-width: 1320px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
}

.header h1 {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-stats {
  display: flex;
  gap: 12px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 16px;
  min-width: 88px;
  text-align: center;
  position: relative;
}

.stat-value {
  display: block;
  font-size: 22px;
  font-weight: 700;
  line-height: 1.1;
}

.stat-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-online { color: var(--online); }
.stat-selected { color: var(--accent); }

.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  margin-top: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card-narrow { max-width: 420px; }

.hidden { display: none !important; }

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

.hint {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
}

label {
  display: block;
  margin: 12px 0;
  font-size: 13px;
  font-weight: 500;
}

label span { display: block; margin-bottom: 6px; }

input, select, textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover { background: #f3f4f6; border-color: var(--border-strong); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { background: var(--error); color: #fff; border-color: var(--error); }
.btn.danger:hover { background: #b91c1c; border-color: #b91c1c; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.icon-btn {
  border: none;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.toolbar-left { display: flex; align-items: center; gap: 12px; }
.toolbar-meta { font-size: 12px; }

.section-head { margin-bottom: 12px; }
.section-head h2 { margin: 0 0 4px; font-size: 18px; }

.device-search {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.device-search input {
  max-width: 420px;
}

.bulk-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  background: var(--accent-soft);
  border: 1px solid rgba(232, 0, 61, 0.2);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  margin-bottom: 16px;
}

.bulk-count { font-size: 13px; }
.bulk-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.debug-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: #111827;
  color: #e5e7eb;
  padding: 16px;
  margin-bottom: 16px;
}

.debug-panel .hint,
.debug-panel .muted {
  color: #9ca3af;
}

.debug-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.debug-panel h3 {
  margin: 0 0 4px;
  font-size: 16px;
}

.debug-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.debug-panel .btn {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.28);
  color: #f9fafb;
}

.debug-panel .btn:hover {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.42);
}

.debug-panel .btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.debug-status {
  margin: 10px 0 12px;
  font-size: 12px;
}

.debug-events {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.debug-event {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.04);
  overflow: hidden;
}

.debug-event summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  list-style: none;
}

.debug-event summary::-webkit-details-marker {
  display: none;
}

.debug-event-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: rgba(5, 150, 105, 0.18);
  color: #86efac;
}

.debug-bad .debug-event-status {
  background: rgba(220, 38, 38, 0.18);
  color: #fca5a5;
}

.debug-event-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 8px;
  padding: 0 12px 12px;
}

.debug-event-grid div {
  min-width: 0;
}

.debug-event-grid span {
  display: block;
  color: #9ca3af;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 2px;
}

.debug-event-grid code {
  display: block;
  overflow-wrap: anywhere;
  color: #e5e7eb;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.debug-message {
  grid-column: 1 / -1;
}

.debug-event pre {
  margin: 0;
  padding: 12px;
  max-height: 360px;
  overflow: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.22);
  color: #d1d5db;
  font-size: 12px;
  line-height: 1.45;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.devices-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
}

.devices-table th,
.devices-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.devices-table th {
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
  position: relative;
}

.devices-table tbody tr.device-row {
  cursor: pointer;
  transition: background 0.12s;
}

.devices-table tbody tr.device-row:hover { background: #fafafa; }
.devices-table tbody tr.device-row.expanded { background: var(--accent-soft); }
.devices-table tbody tr.device-row.selected { background: #fff5f7; }

.col-check { width: 44px; text-align: center; }
.col-check input { width: auto; margin: 0; cursor: pointer; }

.device-name {
  font-weight: 600;
  font-size: 14px;
  display: block;
}

.device-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.device-tag {
  display: inline-flex;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.badge-online { background: #d1fae5; color: var(--online); }
.badge-offline { background: #f3f4f6; color: var(--offline); }
.badge-provisioned { background: #dbeafe; color: var(--provisioned); }
.badge-updating { background: #fef3c7; color: var(--updating); }
.badge-error { background: #fee2e2; color: var(--error); }

.firmware-cell {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.firmware-target {
  color: var(--accent);
  font-weight: 600;
}

.cell-muted { color: var(--muted); font-size: 12px; }

.vpn-yes { color: var(--online); font-weight: 600; }
.vpn-no { color: var(--muted); }

.detail-row td {
  padding: 0;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.detail-panel {
  padding: 20px 24px 24px 56px;
  animation: slideDown 0.2s ease;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px 24px;
  margin-bottom: 20px;
}

.detail-item label {
  margin: 0;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

.detail-item .value {
  margin-top: 4px;
  font-size: 13px;
  word-break: break-all;
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.detail-actions input,
.detail-actions select { width: auto; min-width: 160px; flex: 0 1 200px; }

.detail-section h4 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
}

.fleet-domains-section {
  margin-bottom: 16px;
}

.fleet-domain-active {
  margin: 8px 0 10px;
  font-size: 12px;
}

.fleet-domain-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.fleet-domain-row {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.fleet-domain-row-active {
  border-color: rgba(5, 150, 105, 0.35);
  background: rgba(5, 150, 105, 0.06);
}

.wifi-diagnostics-section {
  margin-bottom: 16px;
}

.wifi-diagnostics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.wifi-diagnostics-body h5 {
  margin: 8px 0;
  font-size: 12px;
  color: var(--text);
}

.wifi-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.wifi-card span {
  color: var(--muted);
  font-size: 12px;
}

.wifi-card code {
  width: fit-content;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.wifi-password-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.wifi-password-row .wifi-pass-toggle {
  padding: 4px 10px;
  font-size: 11px;
}

.wifi-clients-table code {
  font-size: 12px;
}

.fleet-domain-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.fleet-domain-input {
  min-width: 260px;
  flex: 1 1 280px;
}

.command-form {
  display: grid;
  grid-template-columns: 220px 1fr auto;
  gap: 12px;
  align-items: start;
  margin-bottom: 12px;
}

.command-form-root {
  margin-bottom: 12px;
}

.cmd-form-left {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cmd-type-tip {
  margin: 0;
  font-size: 11px;
  line-height: 1.35;
}

.cmd-form-mid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.cmd-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cmd-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0;
}

.cmd-field span {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.cmd-field input,
.cmd-field select {
  width: 100%;
}

.cmd-no-fields {
  margin: 0;
  padding: 8px 0;
}

.toggle-group {
  display: inline-flex;
  gap: 4px;
}

.toggle-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}

.toggle-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.cmd-advanced textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.cmd-list-wrap {
  margin-top: 4px;
}

@media (max-width: 900px) {
  .command-form { grid-template-columns: 1fr; }
}

.cmd-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.cmd-table th,
.cmd-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.cmd-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
}

.progress {
  height: 6px;
  background: #e5e7eb;
  border-radius: 6px;
  overflow: hidden;
  min-width: 80px;
}

.progress span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 6px;
}

.cmd-row-succeeded .progress span {
  background: var(--online);
}

.cmd-row-failed .progress span {
  background: #dc2626;
}

.cmd-row-succeeded td:nth-child(2) {
  color: var(--online);
  font-weight: 600;
}

.cmd-row-failed td:nth-child(2) {
  color: #dc2626;
  font-weight: 600;
}

.ping-cell {
  font-variant-numeric: tabular-nums;
}

.empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}

.status { margin-top: 10px; font-size: 13px; }
.status.err { color: var(--error); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 1000;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 100%;
  max-width: 480px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.18);
}

.modal-wide { max-width: 760px; }
.modal-xwide { max-width: 1080px; }

.modal-danger .modal-header h3,
.modal-danger #modalHint {
  color: var(--error);
}

.danger-zone {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid #fecaca;
  background: #fef2f2;
  border-radius: var(--radius-md);
  padding: 16px 18px;
}

.danger-zone h4 {
  margin: 0 0 8px;
  color: var(--error);
}

.delete-warning {
  background: #fff1f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 14px;
  font-size: 13px;
  line-height: 1.45;
}

.delete-warning ul {
  margin: 8px 0 8px 18px;
  padding: 0;
}

.delete-warning p { margin: 0 0 8px; }
.delete-warning p:last-child { margin-bottom: 0; }

.firmware-upload-form {
  display: grid;
  gap: 10px;
}

.firmware-upload-form label {
  margin: 0;
}

.firmware-upload-form textarea {
  resize: vertical;
}

.firmware-releases-table-wrap {
  max-height: 420px;
  overflow: auto;
}

.manifest-viewer {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  max-height: 420px;
  overflow: auto;
  white-space: pre-wrap;
  font-size: 12px;
  line-height: 1.45;
}

.delete-device-name {
  margin-top: 10px !important;
  font-size: 12px;
}

.delete-confirm-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
}

.delete-confirm-label input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.04em;
}

.charts-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1100;
}

.charts-panel {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: min(1100px, 96vw);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.22);
  border: 1px solid var(--border);
}

.charts-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 24px 12px;
  border-bottom: 1px solid var(--border);
}

.charts-panel-header h3 { margin: 0 0 4px; font-size: 20px; }
.charts-panel-header .hint { margin: 0; }

.charts-panel-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.charts-period-label {
  margin: 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

.charts-period-label select {
  display: block;
  margin-top: 4px;
  min-width: 120px;
}

.charts-container {
  overflow-y: auto;
  padding: 16px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

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

.chart-card-title {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
}

.chart-card-canvas-wrap {
  position: relative;
  height: 280px;
}

.collapsible-section { margin-top: 4px; }

.collapsible-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 0;
}

.collapsible-head h4 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
}

.collapsible-body {
  margin-top: 12px;
  animation: slideDown 0.2s ease;
}

.collapsible-body.hidden { display: none !important; }

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

.root-password-wrap.hidden { display: none !important; }

.root-password-wrap input {
  min-width: 200px;
  max-width: 280px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

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

.modal-header h3 { margin: 0; font-size: 18px; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #111827;
  color: #fff;
  padding: 12px 18px;
  border-radius: var(--radius-md);
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 1100;
  max-width: 360px;
  animation: toastIn 0.25s ease;
}

.toast.err { background: var(--error); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

[data-tip] { position: relative; }

[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  font-size: 12px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  line-height: 1.4;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  white-space: normal;
  width: max-content;
  max-width: 260px;
  z-index: 50;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.devices-table th[data-tip]:hover::after {
  bottom: auto;
  top: calc(100% + 6px);
}

.log-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  background: #f3f4f6;
  color: var(--muted);
}

.log-raw { background: #dbeafe; color: #1d4ed8; }
.log-hour { background: #e0e7ff; color: #4338ca; }
.log-day { background: #f3f4f6; color: #6b7280; }
.log-ip-change { background: #fef3c7; color: #b45309; font-weight: 600; }

.log-row-ip-change { background: #fffbeb; }
.connect-log-table code { font-size: 12px; }

.detail-section + .detail-section { margin-top: 20px; }
