:root {
  /* Nền trang xám-xanh mát thay vì be trầm -- cảm giác "app hiện đại" hơn
     "bản in giấy". Sidebar/card dùng trắng thật (surface-1/2) để nổi bật
     khỏi nền, có shadow thay vì chỉ viền để tạo chiều sâu. */
  --surface-0: #eef1f6;
  --surface-1: #ffffff;
  --surface-2: #ffffff;
  --text-primary: #111827;
  --text-secondary: #667085;
  --text-muted: #98a2b3;
  --border: #e4e7ec;
  --border-strong: #d0d5dd;
  --accent: #2563eb;
  --bg-accent: #eff6ff;
  --text-accent: #1d4ed8;
  --border-accent: #93c5fd;
  --warning: #b45309;
  --bg-warning: #fffbeb;
  --text-warning: #92400e;
  --border-warning: #fbbf24;
  --success: #059669;
  --bg-success: #ecfdf5;
  --text-success: #047857;
  --danger: #dc2626;
  --bg-danger: #fef2f2;
  --text-danger: #b91c1c;
  --fill-primary: #2563eb;
  --fill-primary-hover: #1d4ed8;
  --on-primary: #ffffff;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.16);
  --mono: 'SFMono-Regular', Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

.mono { font-family: var(--mono); }
.hidden { display: none !important; }

/* --- Nút dùng chung --- */
.primary-btn {
  background: var(--fill-primary);
  color: var(--on-primary);
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  box-shadow: 0 1px 2px rgba(37, 99, 235, 0.3);
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}
.primary-btn:hover:not(:disabled) {
  background: var(--fill-primary-hover);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}
.primary-btn:active:not(:disabled) { transform: translateY(1px); }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

.ghost-btn {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  text-align: center;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.ghost-btn:hover:not(:disabled) { background: var(--surface-1); border-color: var(--text-muted); }
.ghost-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-block { width: 100%; margin-bottom: 10px; }

/* --- Overlay + popup card (bám sát mockup: nền tối mờ ~45%, khoá cuộn) --- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 100;
}

.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
}

.popup-card { padding: 28px; }

.popup-title { font-weight: 500; font-size: 18px; margin-bottom: 18px; }

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.popup-header .popup-title { margin-bottom: 0; }

.popup-close-x {
  background: none;
  border: none;
  font-size: 16px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.popup-close-x:hover { background: var(--surface-1); color: var(--text-primary); }

.popup-close-text {
  display: block;
  margin: 14px auto 0;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
}

/* --- Popup B: chọn hành động --- */
.popup-choose { width: min(440px, 92vw); }

.choice-option {
  width: 100%;
  display: flex;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 10px;
  background: var(--surface-2);
  font-size: 14px;
  color: var(--text-primary);
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}
.choice-option:hover { border-color: var(--accent); transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.choice-option-accent {
  border-color: var(--border-accent);
  background: var(--bg-accent);
  color: var(--text-accent);
  font-weight: 500;
}
.choice-option-accent:hover { border-color: var(--accent); }
.choice-icon { font-size: 15px; }

.popup-footnote {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.5;
  margin-top: 4px;
}

/* --- Popup C: tạo thiết bị --- */
.popup-create { width: min(540px, 92vw); }

.field-label {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.text-input {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  margin-bottom: 14px;
  font-family: var(--sans);
  background: var(--surface-2);
  color: var(--text-primary);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.text-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--bg-accent);
}

.key-block {
  border: 1.5px solid var(--border-warning);
  background: var(--bg-warning);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}
.key-block-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-warning);
  margin-bottom: 8px;
}
.key-block-warning {
  font-size: 12px;
  color: var(--text-warning);
  line-height: 1.5;
  margin-bottom: 10px;
}
.key-value {
  background: var(--surface-2);
  border: 1px solid var(--border-warning);
  border-radius: 8px;
  padding: 10px;
  font-size: 12px;
  word-break: break-all;
  margin-bottom: 10px;
}
.key-block-actions { display: flex; gap: 8px; }
.key-block-actions button { flex: 1; padding: 9px; font-size: 13px; }

.firmware-row { display: flex; gap: 8px; margin-bottom: 14px; align-items: center; }
.firmware-row select.text-input { flex: 1; width: auto; margin-bottom: 0; }
.firmware-row button { flex: none; }

.btn-flash { margin-top: 4px; }

/* --- Thanh tiến trình flash: xem trực quan, không cần đọc nhật ký --- */
.flash-progress-wrap { margin-top: 10px; }

.flash-progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.flash-progress-track {
  height: 8px;
  background: var(--surface-1);
  border: 0.5px solid var(--border-strong);
  border-radius: 20px;
  overflow: hidden;
}

.flash-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 20px;
  transition: width 0.2s ease-out, background 0.2s;
}
.flash-progress-fill.done { background: var(--success); }
.flash-progress-fill.error { background: var(--danger); }

.flash-log {
  background: #0d1117;
  border-radius: 8px;
  padding: 10px;
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.6;
  color: #9fe1cb;
  min-height: 60px;
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}
.flash-log .log-error { color: #f5a3a3; }
.flash-log .log-info { color: #85b7eb; }

.flash-note {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 6px;
}

/* --- Màn A: bản đồ --- */
.map-view {
  display: flex;
  height: 100vh;
  width: 100%;
}

/* --- Sidebar danh sách thiết bị (bên trái, giống ảnh mockup A) --- */
.device-sidebar {
  width: 190px;
  flex: none;
  background: var(--surface-0);
  border-right: 1px solid var(--border);
  box-shadow: 1px 0 0 rgba(16, 24, 40, 0.02);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.device-sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  flex: none;
}

.sidebar-add-btn {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}
.sidebar-add-btn:hover { color: var(--accent); border-color: var(--accent); }

.device-list { flex: 1; overflow-y: auto; padding: 4px 10px 10px; }

.device-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-left: 3px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.device-list-item:hover { background: var(--surface-2); }
.device-list-item.active {
  background: var(--surface-2);
  color: var(--text-accent);
  font-weight: 600;
  border-left-color: var(--accent);
  box-shadow: var(--shadow-sm);
}
.device-list-icon { flex: none; }
.device-list-name { overflow: hidden; text-overflow: ellipsis; }

.device-list-empty {
  padding: 10px;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* --- Vùng bản đồ (bên phải sidebar) -- context định vị absolute cho #map,
   .map-topbar, #map-empty-state --- */
.map-area {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 100%;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 0;
}

/* Nhãn chữ cho Hoàng Sa / Trường Sa trên bản đồ -- chỉ là text, không icon. */
.island-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.8);
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  pointer-events: none;
}

/* Neo bên PHẢI, không kéo dài hết chiều ngang -- nếu kéo tới left:12px sẽ đè
   lên nút zoom +/- mặc định của Leaflet (nằm ở góc trái trên). Nền mờ (không
   trắng đặc) để vẫn thấy được bản đồ phía sau. */
.map-topbar {
  position: absolute;
  top: 12px;
  right: 12px;
  left: auto;
  max-width: min(480px, calc(100% - 76px)); /* chừa chỗ cho nút zoom bên trái */
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.75);
  backdrop-filter: blur(12px) saturate(1.5);
  -webkit-backdrop-filter: blur(12px) saturate(1.5);
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: var(--shadow-lg);
}

.map-topbar-info { min-width: 0; }
.map-device-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.map-status {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.map-topbar-actions { display: flex; gap: 6px; flex: none; align-items: center; }
.ghost-btn-small { padding: 6px 10px; font-size: 12px; }

/* --- Menu "⋯" quản lý thiết bị (giống ảnh mockup F) --- */
.device-menu-wrap { position: relative; }

.device-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 240px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
  z-index: 20;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: none;
  border: none;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 12px;
  color: var(--text-primary);
  cursor: pointer;
  transition: background 0.15s ease;
}
.menu-item:hover { background: var(--surface-0); }
.menu-item-icon { flex: none; }
.menu-item-danger { color: var(--text-danger); }

.menu-divider {
  height: 0.5px;
  background: var(--border);
  margin: 4px 2px;
}

.lost-key-warning {
  border: 1px solid var(--border-warning);
  background: var(--bg-warning);
  border-radius: 10px;
  padding: 10px;
  margin: 6px 2px 2px;
}
.lost-key-warning-title {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-warning);
  margin-bottom: 4px;
}
.lost-key-warning-text {
  font-size: 11px;
  color: var(--text-warning);
  line-height: 1.5;
  margin-bottom: 8px;
}
.lost-key-create-btn {
  padding: 6px 10px;
  font-size: 11px;
  display: inline-block;
}

.map-empty-state {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 12px;
  color: var(--text-secondary);
  max-width: 90%;
  text-align: center;
  box-shadow: var(--shadow-md);
}

/* --- Popup E: nhập mã để xem --- */
.popup-view { width: min(440px, 92vw); }

.popup-subtitle {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.key-input {
  resize: vertical;
  min-height: 44px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 14px;
  cursor: pointer;
}

.toggle-switch { position: relative; flex: none; }
.toggle-switch input {
  position: absolute;
  opacity: 0;
  width: 32px;
  height: 17px;
  margin: 0;
  cursor: pointer;
}
.toggle-track {
  display: block;
  width: 32px;
  height: 17px;
  background: var(--fill-control, #d3d1c7);
  border-radius: 20px;
  position: relative;
  transition: background 0.15s;
}
.toggle-thumb {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 13px;
  height: 13px;
  background: #fff;
  border-radius: 50%;
  transition: left 0.15s;
}
.toggle-switch input:checked + .toggle-track { background: var(--accent); }
.toggle-switch input:checked + .toggle-track .toggle-thumb { left: 17px; }

.view-error {
  font-size: 11px;
  color: var(--text-danger);
  margin-bottom: 10px;
}
