:root {
  --bg: #f2f7f7;
  --panel: #ffffff;
  --panel-soft: #f8fbfb;
  --border: #d9e4e4;
  --text: #17333b;
  --muted: #5f7a80;
  --accent: #0f9f8f;
  --accent-2: #ff8c3a;
  --ok: #138f4f;
  --ng: #d83b45;
  --warn: #f0b429;
  --shadow: 0 12px 28px rgba(15, 55, 64, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Poppins", "Trebuchet MS", "Verdana", sans-serif;
  background: radial-gradient(circle at 0% 0%, #d7eeec 0, transparent 40%),
    radial-gradient(circle at 100% 100%, #ffe6d4 0, transparent 45%),
    var(--bg);
  color: var(--text);
}

.bg-shape {
  position: fixed;
  border-radius: 999px;
  filter: blur(70px);
  z-index: -1;
  opacity: 0.35;
}

.bg-shape-a {
  width: 340px;
  height: 340px;
  top: -120px;
  left: -80px;
  background: #2bc0b4;
}

.bg-shape-b {
  width: 320px;
  height: 320px;
  bottom: -120px;
  right: -60px;
  background: #ffb46b;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  padding: 20px 24px 14px;
}

.topbar h1 {
  margin: 0;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
}

.top-meta {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.meta-item {
  min-width: 132px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 9px 12px;
}

.meta-item span {
  display: block;
  font-size: 11px;
  color: var(--muted);
}

.meta-item strong {
  display: block;
  margin-top: 4px;
  font-size: 18px;
}

.layout {
  padding: 0 20px 20px;
}

.tabs {
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--border);
  border-radius: 16px;
  backdrop-filter: blur(3px);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.tab-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(248, 251, 251, 0.92);
}

.tab-button {
  border: 1px solid #cbdadb;
  background: #f4f9f9;
  color: #37585f;
  padding: 7px 13px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 10px;
  cursor: pointer;
}

.tab-button.active {
  color: white;
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent), #0a8b7d);
}

.tab-panels {
  padding: 14px;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.metric-card {
  background: linear-gradient(160deg, #ffffff, #f4fbfb);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.metric-card h4 {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.metric-card strong {
  display: block;
  margin-top: 8px;
  font-size: 24px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
}

.card h3 {
  margin: 0 0 10px;
  font-size: 17px;
}

.pool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(420px, 1fr));
  gap: 12px;
}

.pc-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
}

/* --- PC đang xem: chiếm TRỌN chiều ngang ---------------------------------------
   Bảng worker cần 1150px mới hiện đủ 14 cột (đo thực tế). Đó là con số TUYỆT ĐỐI,
   không phải tỉ lệ — nên chia 50% màn hình vẫn thiếu trên mọi laptop phổ thông
   (1920px cần 64%, 1440px cần 86%). Cho card active span hết lưới thì bảng luôn có
   đủ chỗ từ màn 1280px trở lên, và các PC còn lại vẫn gọn ở dạng card nhỏ. */
.pc-card.is-active {
  grid-column: 1 / -1;
  cursor: default;
  border-color: var(--accent-2);
  box-shadow: 0 0 0 1px var(--accent-2);
}

/* Card không active: gợi ý bấm được. */
.pc-card:not(.is-active):hover {
  border-color: var(--accent-2);
  background: #fdfdfb;
}

/* Nhãn nhỏ nhắc cách dùng, chỉ hiện ở card KHÔNG active. */
.pc-card .pc-activate-hint {
  display: none;
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
}

.pc-card:not(.is-active) .pc-activate-hint {
  display: inline;
}

/* Card active: bảng hết cuộn ngang nên bỏ min-width ép cuộn. */
.pc-card.is-active .table {
  min-width: 0;
}

/* Ở bề rộng lớn, lưới 3 cột làm mỗi ô metric rộng ~600px (thừa chỗ vô ích).
   Dùng flex-wrap + grow: ô tự xếp theo bề rộng thật, và hàng CUỐI tự giãn lấp đầy
   nên không bao giờ chừa mảng trống — đúng ở mọi số lượng ô. */
.pc-card.is-active .pc-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pc-card.is-active .pc-meta > .meta-mini {
  flex: 1 1 190px;
}

/* Nút cũng vậy: ở card rộng thì lưới 2 cột kéo mỗi nút ra ~900px. Cho về bề rộng
   tự nhiên, cụm icon dồn phải. `min-width` giữ nhãn "Kill Task (99+)" không làm
   nút đổi kích thước — vẫn đúng cam kết bố cục không nhảy. */
.pc-card.is-active .actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.pc-card.is-active .actions .btn:not(.icon-only) {
  flex: 0 0 auto;
  min-width: 150px;
}

.pc-card.is-active .actions-icons {
  margin-left: auto;
}

.pc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.pc-title {
  margin: 0;
  font-size: 18px;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 999px;
}

.status-chip.online {
  background: #e0f8ec;
  color: var(--ok);
}

.status-chip.offline {
  background: #fde7eb;
  color: var(--ng);
}

.pc-meta {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

/* Số ô metric thay đổi theo dữ liệu (vd ô Sessions chỉ hiện với client mới) nên hàng
   cuối hay bị lẻ 1-2 ô, chừa mảng trống lớn bên phải. Cho ô cuối TRÀN hết số cột còn
   lại để hàng cuối luôn đầy, ở mọi số lượng ô. */
.pc-meta > .meta-mini:last-child:nth-child(3n + 1) {
  grid-column: span 3;
}

.pc-meta > .meta-mini:last-child:nth-child(3n + 2) {
  grid-column: span 2;
}

.meta-mini {
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 8px;
}

.meta-mini span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.meta-mini strong {
  display: block;
  margin-top: 2px;
  font-size: 14px;
}

.meta-mini.mqtt-fail {
  border-color: #f6bcc3;
  background: #fff1f3;
  box-shadow: inset 0 0 0 1px #ffd9de;
}

.meta-mini.mqtt-fail strong {
  color: var(--ng);
  font-weight: 800;
}

/* PC offline: ô Up Time đổi thành Down Time, chữ đỏ để tăng khả năng nhận biết. */
.meta-mini.downtime {
  border-color: #f6bcc3;
  background: #fff1f3;
  box-shadow: inset 0 0 0 1px #ffd9de;
}

.meta-mini.downtime span {
  color: var(--ng);
  font-weight: 600;
}

.meta-mini.downtime strong {
  color: var(--ng);
  font-weight: 800;
}

.metric-progress-card strong {
  margin-top: 1px;
}

.metric-value-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-top: 1px;
}

.metric-value-row strong {
  margin: 0;
}

.metric-value-row em {
  margin: 0;
  font-size: 11px;
  font-style: normal;
  color: var(--muted);
  white-space: nowrap;
}

.metric-progress-track {
  margin-top: 6px;
  height: 8px;
  width: 100%;
  border-radius: 999px;
  background: #e3ecec;
  overflow: hidden;
}

.metric-progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  will-change: width;
}

.metric-progress-fill.meter-ok {
  background: linear-gradient(90deg, #1fb86d, #0f8a50);
}

.metric-progress-fill.meter-warn {
  background: linear-gradient(90deg, #ffad33, #ea7a1f);
}

.metric-progress-fill.meter-ng {
  background: linear-gradient(90deg, #ff5a5f, #cc2d3a);
}

.table-scroll {
  overflow: auto;
  max-width: 100%;
}

.table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

.table th,
.table td {
  border-bottom: 1px solid #ebf1f1;
  padding: 8px 7px;
  text-align: left;
  font-size: 12px;
  vertical-align: top;
}

.table thead th {
  position: sticky;
  top: 0;
  background: #f5faf9;
  z-index: 2;
}

/* Cột check chọn session để Kill Task: hẹp, không có nhãn chữ.
   Ô header = chọn/bỏ chọn tất cả dòng của PC đó.
   `vertical-align: top` cho khớp các cột còn lại: dòng cao (Last Error dài) thì ô
   check vẫn nằm ngang hàng chữ đầu tiên chứ không trôi xuống giữa dòng. */
.table th.col-select,
.table td.col-select {
  width: 30px;
  padding-left: 8px;
  padding-right: 4px;
  text-align: center;
  vertical-align: top;
  cursor: pointer;
}

.table .row-select-box {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--accent-2);
  cursor: pointer;
  vertical-align: top;
}

.table .row-select-box:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.level-ok {
  color: var(--ok);
  font-weight: 700;
}

.level-ng {
  color: var(--ng);
  font-weight: 700;
}

.level-info {
  color: #2f4f56;
  font-weight: 700;
}

.log-message-ng {
  color: var(--ng);
  font-weight: 600;
}

.log-table-wrap {
  max-height: calc(100vh - 280px);
}

.log-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  margin-bottom: 10px;
}

.log-filter-group {
  min-width: 140px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.log-filter-group label {
  font-size: 11px;
  color: var(--muted);
}

.log-filter-group select,
.log-filter-group input {
  height: 32px;
  border: 1px solid #cddcdd;
  border-radius: 8px;
  background: #f9fcfc;
  color: #18363d;
  padding: 6px 9px;
  font-size: 12px;
}

.log-filter-search {
  min-width: 220px;
  flex: 1;
}

.log-filter-actions {
  margin-left: auto;
  display: inline-flex;
  align-items: flex-end;
  gap: 8px;
}

.log-clear-btn {
  min-width: 94px;
}

.log-filter-count {
  font-size: 12px;
  color: var(--muted);
  padding-bottom: 6px;
}

/* Lưới 2 cột cố định:
     hàng 1: [Get Snapshot] [Kill Task (N)]
     hàng 2: [Restart Service] [🗑 ⏻ dồn phải]
   Bề rộng nút do CỘT quyết định nên nhãn dài ngắn thế nào cũng không đổi bố cục,
   và không còn hàng chỉ có 2 nút icon với mảng trống lớn bên phải. */
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
  align-items: center;
}

.btn {
  border: 1px solid #d3e0e0;
  background: #f5f9f9;
  color: #1d4148;
  border-radius: 10px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
}

.btn.primary {
  color: white;
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent-2), #f2781f);
}

/* Nút chữ lấp đầy ô lưới; nhãn đổi (Getting.../Kill Task (99+)) KHÔNG làm đổi bề rộng. */
.actions .btn:not(.icon-only) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  white-space: nowrap;
  text-align: center;
}

/* Cụm icon nằm ở ô phải hàng 2, dồn sát mép phải để cân với nút bên trái.
   Luôn đi cùng nhau, không bao giờ bị tách mỗi nút một dòng. */
.actions-icons {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

/* --- Trạng thái worker: tách "tiến trình sống" khỏi "browser sống" ------------- */
.worker-status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
}

.worker-status.st-alive {
  color: #12695a;
  background: #d8f3ec;
}

/* Worker còn sống nhưng KHÔNG còn browser -> cảnh báo nhẹ, không phải lỗi. */
.worker-status.st-idle {
  color: #8a5a00;
  background: #ffeccc;
}

.worker-status.st-stopped {
  color: #6f8388;
  background: #e8efef;
}

/* --- Engine điều khiển browser: PyAutoGUI (node C12) vs Playwright (node C10) --- */
.worker-engine {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
}

/* Xanh LÁ: input VẬT LÝ, thao tác nhập liệu bị tuần tự hoá.
   Badge đậm hơn nền dòng một bậc để vẫn nổi lên trên dòng đã tô cùng tông. */
.worker-engine.eng-pyautogui {
  color: #1d6b45;
  background: #c9ecd8;
}

/* Xanh DƯƠNG: CDP, nhập liệu song song được. */
.worker-engine.eng-playwright {
  color: #1b5e9c;
  background: #cbe4f8;
}

.worker-engine.eng-unknown {
  color: #8a9a9e;
  background: transparent;
  font-weight: 500;
}

/* Profile ID suy từ worker key (profile đã đóng) -> làm mờ để khỏi tưởng đang chạy. */
.derived-id {
  opacity: 0.55;
  font-style: italic;
}

.meta-mini strong.muted-value {
  opacity: 0.5;
}

.btn.danger {
  color: #ffffff;
  border-color: transparent;
  background: linear-gradient(135deg, #ff5a5f, #c92a36);
}

.btn:disabled {
  color: #6f8388;
  border-color: #d3e0e0;
  background: #e8efef;
  cursor: not-allowed;
}

.btn.danger:disabled {
  color: #6f8388;
  border-color: #d3e0e0;
  background: #e8efef;
}

.btn.icon-only {
  width: 42px;
  min-width: 42px;
  height: 38px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn.icon-only img {
  width: 22px;
  height: 22px;
  object-fit: contain;
  display: block;
}

.btn.icon-only svg {
  width: 24px;
  height: 24px;
  display: block;
}

.restart-pc-btn {
  background: #ffffff;
  border: 2px solid var(--ng);
  color: var(--ng);
  box-shadow: 0 0 0 1px rgba(216, 59, 69, 0.08);
}

.restart-pc-btn:hover {
  background: #fff3f4;
}

.restart-pc-btn:disabled {
  border-color: #e9a4a9;
  background: #ffffff;
  color: #e9a4a9;
}

/* Nút Clear PC (dọn máy offline) — cùng phong cách với nút Restart PC nhưng màu cam. */
.clear-pc-btn {
  margin-left: auto;
  background: #ffffff;
  border: 2px solid var(--accent-2);
  color: var(--accent-2);
  box-shadow: 0 0 0 1px rgba(255, 140, 58, 0.08);
}

.clear-pc-btn:hover {
  background: #fff5ec;
}

.clear-pc-btn:disabled {
  border-color: #f4c69e;
  background: #ffffff;
  color: #f4c69e;
}

.volume-card {
  min-height: 68px;
}

.disk-lines {
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.disk-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
}

.disk-line span {
  color: inherit;
}

.disk-line.tone-ok {
  color: var(--ok);
}

.disk-line.tone-warn {
  color: #d97706;
}

.disk-line.tone-ng {
  color: var(--ng);
}

.disk-line.tone-muted {
  color: var(--muted);
  font-weight: 500;
}

.table tbody tr.worker-row {
  cursor: pointer;
}

/* --- Tô nền cả DÒNG theo engine: liếc một cái là biết, khỏi đọc cột Engine -------
   Xanh LÁ nhạt  = PyAutoGUI (node C12) — chuột/phím vật lý.
   Xanh DƯƠNG nhạt = Playwright (node C10) — điều khiển qua CDP.
   Không tô khi chưa xác định được engine.
   ĐẶT TRƯỚC :hover và .row-selected (cùng độ đặc hiệu, thắng nhau bằng thứ tự) để hai
   trạng thái tương tác đó vẫn đè lên được — nếu không, chọn dòng sẽ không thấy gì. */
.table tbody tr.worker-row.eng-row-pyautogui {
  background: #e7f7ed;
}

.table tbody tr.worker-row.eng-row-playwright {
  background: #e6f1fb;
}

.table tbody tr.worker-row:hover {
  background: #f2f8f8;
}

.table tbody tr.worker-row.row-selected {
  background: #ffe9dd;
}

.screenshot-wrap {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #fbfdfd;
}

.screenshot-open {
  display: block;
  width: 100%;
  border: 0;
  margin: 0;
  padding: 0;
  cursor: zoom-in;
  background: transparent;
}

.screenshot-wrap img {
  width: 100%;
  max-height: 230px;
  object-fit: cover;
  display: block;
  cursor: zoom-in;
}

.image-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1200;
}

.image-modal.active {
  display: flex;
}

.image-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 20, 22, 0.78);
  backdrop-filter: blur(2px);
}

/* Popup xem ảnh: tận dụng tối đa màn hình (bỏ giới hạn 1800x1100 cũ). */
.image-modal-dialog {
  position: relative;
  z-index: 2;
  width: 97vw;
  height: 96vh;
  border-radius: 12px;
  border: 1px solid #bfd0d0;
  background: #0d1718;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.35);
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.image-modal-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-bottom: 1px solid #24383a;
  background: rgba(16, 28, 30, 0.96);
}

.image-modal-zoom-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.image-modal-zoom-label {
  min-width: 58px;
  text-align: center;
  color: #cfe9e9;
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.image-modal-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 32px;
  padding: 0 7px;
  border-radius: 8px;
  border: 1px solid #33565a;
  background: rgba(28, 46, 49, 0.9);
  color: #d4efef;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.image-modal-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}

.image-modal-btn.wide {
  font-size: 12px;
  padding: 0 12px;
}

.image-modal-btn:hover {
  background: rgba(42, 68, 72, 0.95);
}

.image-modal-btn.close {
  margin-left: 4px;
  font-size: 22px;
}

.image-modal-status {
  margin-left: auto;
  margin-right: 6px;
  color: #ffcf8a;
  font-size: 12px;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.image-modal-status.active {
  opacity: 1;
}

.image-modal-stage {
  flex: 1 1 auto;
  display: flex;
  overflow: auto;
  padding: 6px;
  background: #0d1718;
}

.image-modal-stage.pannable {
  cursor: grab;
}

.image-modal-stage.panning {
  cursor: grabbing;
}

/* `margin:auto` căn giữa cả 2 chiều mà KHÔNG cắt mất mép ảnh khi phóng to vượt khung
   (khác với justify/align-content: center vốn bị cắt khi overflow). */
.image-modal-stage img {
  margin: auto;
  display: block;
  /* flex:0 0 auto để ảnh KHÔNG bị flex container co lại khi phóng to vượt khung;
     nhờ vậy zoom > 100% mới thật sự phóng to (kết hợp overflow:auto của stage để cuộn/pan). */
  flex: 0 0 auto;
  image-rendering: auto;
  user-select: none;
  -webkit-user-drag: none;
}

.image-modal-stage img.fit {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.empty-state {
  padding: 24px 14px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed #cad9d9;
  border-radius: 12px;
  background: #f9fcfc;
}

@media (max-width: 980px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .pool-grid {
    grid-template-columns: 1fr;
  }

  .pc-meta {
    grid-template-columns: 1fr 1fr;
  }

  .log-filter-count {
    width: 100%;
    margin-left: 0;
    padding-bottom: 0;
  }
}
