/* ============================================================================
   panels.css — Thẻ KPI, thanh công cụ bảng, dòng bảng sensor, sức khoẻ tín hiệu.
   Phạm vi: các khối do js/panels.js sinh ra. Dùng token khai báo ở styles.css.
   ============================================================================ */

/* --------------------------------------------------------------------------
   0. Biến cục bộ + tiện ích dùng chung
   -------------------------------------------------------------------------- */
.kpis,
.kpi-grid,
.tp,
.health,
.sensor-table .srow {
  --kpi-h: 118px;
  --tint-wind: var(--accent);
  --tint-env: #0d9488;
  --tint-volt: #7c3aed;
  font-family: var(--font);
}

.pmut { color: var(--muted); font-weight: 400; }
.pdot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; flex: none;
}
.pi, .pi-sm, .pi-xs, .pi-lg { flex: none; display: block; }
.pi { width: 20px; height: 20px; }
.pi-sm { width: 14px; height: 14px; }
.pi-xs { width: 12px; height: 12px; }
.pi-lg { width: 28px; height: 28px; }

/* focus ro ràng cho mọi phần tử bấm được của panel */
.kpi:focus-visible,
.tp-chip:focus-visible,
.tp-reset:focus-visible,
.sensor-table .srow:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}

/* --------------------------------------------------------------------------
   1. Khối KPI
   -------------------------------------------------------------------------- */
.kpis { margin-top: 12px; }
.kpis__head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 10px; margin: 0 2px 8px;
}
.kpis__head h3 {
  margin: 0; font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .07em; color: var(--muted);
}
.kpis__meta { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}

.kpi {
  position: relative;
  display: flex; flex-direction: column; gap: 4px;
  min-height: var(--kpi-h);
  padding: 11px 12px 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: border-color .15s ease, box-shadow .15s ease, transform .15s ease;
}
.kpi:hover { border-color: var(--line-2, #cbd5e1); }
.kpi[data-accent="wind"] { border-left-color: var(--tint-wind); }
.kpi[data-accent="env"] { border-left-color: var(--tint-env); }
.kpi[data-accent="volt"] { border-left-color: var(--tint-volt); }
.kpi[data-accent="warn"] { border-left-color: var(--warn); }
.kpi[data-accent="muted"] { border-left-color: var(--line-2, #cbd5e1); }

.kpi__top { display: flex; align-items: center; justify-content: space-between; gap: 6px; min-height: 22px; }
.kpi__ico {
  display: inline-grid; place-items: center;
  width: 26px; height: 26px; border-radius: 8px;
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line);
}
.kpi[data-accent="wind"] .kpi__ico { color: var(--tint-wind); background: rgba(37, 99, 235, .08); border-color: rgba(37, 99, 235, .16); }
.kpi[data-accent="env"] .kpi__ico { color: var(--tint-env); background: rgba(13, 148, 136, .08); border-color: rgba(13, 148, 136, .16); }
.kpi[data-accent="volt"] .kpi__ico { color: var(--tint-volt); background: rgba(124, 58, 237, .08); border-color: rgba(124, 58, 237, .16); }
.kpi[data-accent="warn"] .kpi__ico { color: var(--warn); background: rgba(245, 158, 11, .10); border-color: rgba(245, 158, 11, .22); }
.kpi__menu {
  font-size: 10px; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 100%;
  font-variant-numeric: tabular-nums;
}

.kpi__label {
  font-size: 10.5px; font-weight: 600; letter-spacing: .06em;
  text-transform: uppercase; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.kpi__valwrap {
  display: flex; align-items: baseline; gap: 5px; min-width: 0;
  margin-top: auto;
}
.kpi__val {
  font-size: 26px; line-height: 1.05; font-weight: 600;
  color: var(--text); letter-spacing: -.015em;
  font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.kpi__unit {
  font-size: 11.5px; color: var(--muted); font-weight: 500; white-space: nowrap;
}

/* Mũi tên hướng gió: chỉ HƯỚNG ĐI TỚI (deg + 180), xoay bằng transform */
.kpi__arrowwrap { display: inline-grid; place-items: center; width: 20px; height: 20px; }
.parrow {
  display: inline-block; transform-origin: 50% 50%;
  font-size: 17px; line-height: 1; color: var(--tint-wind);
  transition: transform .35s ease;
}

.kpi__foot {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px; margin-top: 4px; min-height: 18px;
}
.kpi__sub {
  font-size: 10.5px; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums;
}

.kpi-pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 6px; border-radius: 999px;
  font-size: 9.5px; font-weight: 700; letter-spacing: .04em;
  white-space: nowrap; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--muted);
}
.kpi-pill.is-ok { color: #047857; background: #ecfdf5; border-color: #a7f3d0; }
.kpi-pill.is-warn { color: #b45309; background: #fffbeb; border-color: #fde68a; }
.kpi-pill.is-danger { color: #b91c1c; background: #fef2f2; border-color: #fecaca; }
.kpi-pill.is-muted { color: var(--muted); }
.kpi-pill.is-ok .pdot { animation: pd-pulse 2.4s ease-in-out infinite; }
@keyframes pd-pulse {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(16, 185, 129, .5); }
  50% { opacity: .65; box-shadow: 0 0 0 3px rgba(16, 185, 129, 0); }
}

/* --------------------------------------------------------------------------
   2. Skeleton (class .sk) — giữ nguyên kích thước thẻ để layout không nhảy
   -------------------------------------------------------------------------- */
.kpi.sk { pointer-events: none; box-shadow: none; border-left-color: var(--line); }
.sk-box {
  display: block; border-radius: 5px;
  background: linear-gradient(100deg, #f1f5f9 30%, #e8eef6 50%, #f1f5f9 70%);
  background-size: 220% 100%;
  animation: sk-shimmer 1.3s infinite linear;
}
.sk-ico { width: 26px; height: 26px; border-radius: 8px; }
.sk-tag { width: 46px; height: 10px; }
.sk-line { height: 10px; margin-top: 2px; }
.sk-big { height: 26px; margin-top: 8px; border-radius: 6px; }
@keyframes sk-shimmer { from { background-position: 140% 0; } to { background-position: -40% 0; } }

/* --------------------------------------------------------------------------
   3. Thanh công cụ bảng: tìm kiếm + chip lọc + đếm
   -------------------------------------------------------------------------- */
.tp {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.tp-searchwrap {
  position: relative; display: flex; align-items: center; gap: 6px;
  flex: 1 1 210px; min-width: 160px; max-width: 320px;
  padding: 0 10px; height: 32px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px;
  color: var(--muted);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.tp-searchwrap:focus-within {
  border-color: var(--accent); color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}
.tp-searchico { width: 16px; height: 16px; flex: none; }
.tp-search {
  flex: 1 1 auto; min-width: 0; height: 100%;
  border: 0; outline: 0; background: transparent;
  font-family: var(--font); font-size: 12.5px; color: var(--text);
}
.tp-search::placeholder { color: var(--muted); opacity: .85; }
.tp-search::-webkit-search-cancel-button { cursor: pointer; }

.tp-chips { display: flex; flex-wrap: wrap; gap: 4px; flex: 0 1 auto; }
.tp-chip {
  appearance: none; cursor: pointer;
  padding: 4px 10px; height: 26px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel); color: var(--text-2, #334155);
  font-family: var(--font); font-size: 11.5px; line-height: 1; white-space: nowrap;
  transition: background .12s ease, color .12s ease, border-color .12s ease;
}
.tp-chip:hover { background: var(--panel-2); border-color: var(--line-2, #cbd5e1); }
.tp-chip.is-active,
.tp-chip[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent); color: #fff;
  box-shadow: 0 1px 2px rgba(37, 99, 235, .35);
}
.tp-chip.is-active:hover { background: #1d4ed8; }

.tp-meta { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.tp-count {
  font-size: 11.5px; color: var(--muted); white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.tp-count b { color: var(--text); font-weight: 600; }
.tp-reset {
  appearance: none; cursor: pointer;
  display: inline-flex; align-items: center; gap: 4px;
  height: 26px; padding: 0 9px;
  border: 1px solid #fecaca; border-radius: 999px;
  background: #fef2f2; color: #b91c1c;
  font-family: var(--font); font-size: 11.5px; font-weight: 500; white-space: nowrap;
}
.tp-reset:hover { background: #fee2e2; }

/* --------------------------------------------------------------------------
   4. Dòng bảng sensor
   -------------------------------------------------------------------------- */
.sensor-table .srow {
  cursor: pointer;
  transition: background .12s ease;
}
.sensor-table .srow:hover { background: var(--panel-2); }
.sensor-table .srow.is-active,
.sensor-table .srow[aria-selected="true"] {
  background: rgba(37, 99, 235, .06);
  box-shadow: inset 3px 0 0 var(--accent);
}
.sensor-table .srow.is-active td:first-child { font-weight: 600; }

/* Dòng không có tín hiệu: mờ đi */
.sensor-table .srow.is-silent { color: var(--muted); opacity: .72; }
.sensor-table .srow.is-silent:hover { opacity: .95; }
.sensor-table .srow.is-silent .sname { font-style: italic; }

.sensor-table .cell-sensor { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.sname { font-weight: 500; color: var(--text); }
.srow.is-silent .sname { color: var(--muted); }
.hchip {
  display: inline-block; margin-left: 6px; padding: 0 6px;
  border-radius: 999px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--muted);
  font-size: 10px; font-style: normal; font-weight: 500;
  line-height: 16px; vertical-align: 1px;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}

.sensor-table .cell-eval { display: table-cell; }
.evi {
  display: inline-flex; align-items: center; gap: 5px;
  color: var(--text-2, #334155); font-weight: 500;
}
.evi .pi-sm { color: var(--muted); }
.srow.is-silent .evi .pi-sm { opacity: .6; }
.evt {
  display: block; margin-left: 19px;
  font-size: 10.5px; color: var(--muted); opacity: .85; line-height: 1.15;
}

.sensor-table .srow .num {
  text-align: right;
  font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1;
}
.cell-avg { color: var(--text); font-weight: 600; }
.cell-max, .cell-min { color: var(--text-2, #334155); }
.cell-unit { color: var(--muted); font-size: 11px; }
.srow.is-dir .cell-avg, .srow.is-dir .cell-max, .srow.is-dir .cell-min { color: var(--text); }

/* Count: thanh tiến trình nhỏ + số */
.sensor-table .cell-count { min-width: 92px; }
.cbar {
  display: inline-block; position: relative;
  width: 46px; height: 5px; margin-right: 7px;
  border-radius: 999px; background: #e8eef6;
  vertical-align: middle; overflow: hidden;
}
.cbar > i {
  display: block; height: 100%; width: 0;
  border-radius: inherit;
  background: var(--accent-2, #0ea5e9);
  transition: width .3s ease;
}
.srow.is-silent .cbar { background: #eef2f7; }
.srow.is-silent .cbar > i { background: #cbd5e1; }
.cnum {
  font-size: 11.5px; font-weight: 500; color: var(--text-2, #334155);
  font-variant-numeric: tabular-nums;
}

/* Trạng thái: chấm xanh/xám */
.sstat {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; white-space: nowrap;
}
.sdot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--line-2, #cbd5e1); }
.sstat.is-ok { color: #047857; }
.sstat.is-ok .sdot { background: var(--ok); box-shadow: 0 0 0 3px rgba(16, 185, 129, .14); }
.sstat.is-mute { color: var(--muted); }
.sstat.is-mute .sdot { background: #94a3b8; }

/* Bảng trống */
.sensor-table .srow-empty td { padding: 0; border-bottom: 0; white-space: normal; }
.srow-empty__box {
  display: grid; justify-items: center; gap: 2px;
  padding: 26px 16px; color: var(--muted); text-align: center;
}
.srow-empty__box .pi-lg { color: var(--line-2, #cbd5e1); margin-bottom: 4px; }
.srow-empty__box p { margin: 0; font-size: 12.5px; color: var(--text-2, #334155); }
.srow-empty__box p.muted { font-size: 11.5px; }

/* --------------------------------------------------------------------------
   5. Sức khoẻ tín hiệu + hạ tầng
   -------------------------------------------------------------------------- */
.health { padding: 12px 14px 14px; }
.health__head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  flex-wrap: wrap;
}
.health__sum { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.health__num {
  font-size: 22px; font-weight: 600; color: var(--text);
  font-variant-numeric: tabular-nums; letter-spacing: -.01em;
}
.health__num .pmut { margin: 0 1px; font-weight: 400; }
.health__cap {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--muted);
}
.health__pct {
  padding: 0 6px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line);
  font-size: 10.5px; font-weight: 600; color: var(--text-2, #334155);
  font-variant-numeric: tabular-nums;
}

.health__bar {
  position: relative; height: 6px; margin: 9px 0 10px;
  border-radius: 999px; background: #e8eef6; overflow: hidden;
}
.health__bar > i {
  display: block; height: 100%; width: 0;
  border-radius: inherit; background: var(--ok);
  transition: width .35s ease;
}
.health__bar.is-ok > i { background: var(--ok); }
.health__bar.is-warn > i { background: var(--warn); }
.health__bar.is-danger > i { background: var(--danger); }

/* Danh sách sensor im lặng */
.silent { margin: 0 0 10px; font-size: 11.5px; }
.silent > summary {
  display: flex; align-items: center; gap: 6px;
  cursor: pointer; list-style: none;
  padding: 5px 8px; border-radius: 8px;
  background: #fffbeb; border: 1px solid #fde68a; color: #b45309;
}
.silent > summary::-webkit-details-marker { display: none; }
.silent > summary::after {
  content: ""; margin-left: auto; width: 6px; height: 6px;
  border-right: 1.6px solid currentColor; border-bottom: 1.6px solid currentColor;
  transform: rotate(45deg) translateY(-2px); transition: transform .2s ease;
}
.silent[open] > summary::after { transform: rotate(-135deg) translateY(-1px); }
.silent > summary b { font-weight: 600; }
.silent > summary small { font-weight: 400; margin-left: 2px; }
.silent__list {
  display: flex; flex-wrap: wrap; gap: 4px;
  margin: 6px 0 0; padding: 0; list-style: none;
}
.silent__item {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--text-2, #334155); max-width: 100%;
}
.silent__item .sdot { background: #94a3b8; }
.silent__name {
  max-width: 22ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 500;
}
.silent__ev { color: var(--muted); font-size: 10.5px; white-space: nowrap; }
.silent__more {
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 999px;
  border: 1px dashed var(--line-2, #cbd5e1); color: var(--muted); font-size: 10.5px;
}

/* Lưới hạ tầng */
.infra {
  display: grid; gap: 6px;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
}
.infra__tile {
  display: flex; align-items: center; gap: 8px; min-width: 0;
  padding: 7px 9px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: var(--radius-sm, 8px);
}
.infra__ico { display: grid; place-items: center; color: var(--muted); flex: none; }
.infra__ico .pi { width: 16px; height: 16px; }
.infra__txt { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.infra__val {
  font-size: 12.5px; font-weight: 600; color: var(--text);
  font-variant-numeric: tabular-nums; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.infra__label {
  font-size: 10px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .04em; white-space: nowrap;
}
.infra__tile.is-warn {
  background: #fffbeb; border-color: #fde68a;
}
.infra__tile.is-warn .infra__ico,
.infra__tile.is-warn .infra__val { color: #b45309; }
.infra__tile.is-danger { background: #fef2f2; border-color: #fecaca; }
.infra__tile.is-danger .infra__ico,
.infra__tile.is-danger .infra__val { color: #b91c1c; }

.health.is-empty { padding: 14px; }
.health.is-empty p { margin: 4px 0 0; font-size: 12px; }

/* --------------------------------------------------------------------------
   6. Responsive
   -------------------------------------------------------------------------- */
@media (max-width: 1200px) {
  /* Ẩn cột Đơn vị: đơn vị đã nằm trong cột Đại lượng/giá trị */
  .sensor-table .cell-unit { display: none; }
}

@media (max-width: 900px) {
  .kpi-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
  .kpi { min-height: 106px; padding: 10px 11px 9px; }
  .kpi__val { font-size: 23px; }

  .tp { gap: 6px; padding: 9px 10px; }
  .tp-searchwrap { flex: 1 1 100%; max-width: none; }
  .tp-meta { margin-left: 0; }
  .tp-reset { margin-left: auto; }

  /* Bảng -> dạng card: mỗi dòng là một thẻ */
  .sensor-table { display: block; width: 100%; }
  .sensor-table thead { display: none; }
  .sensor-table tbody { display: grid; gap: 8px; padding: 10px; }
  .sensor-table .srow {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px 10px;
    padding: 9px 11px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
  }
  .sensor-table .srow.is-active {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, .16);
  }
  .sensor-table .srow.is-silent { background: var(--panel-2); }
  .sensor-table .srow td { display: block; padding: 2px 0; border: 0; white-space: normal; }
  .sensor-table .srow td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 9.5px; font-weight: 600; letter-spacing: .05em;
    text-transform: uppercase; color: var(--muted); margin-bottom: 1px;
  }
  .sensor-table .srow .cell-sensor { grid-column: 1 / -1; }
  .sensor-table .srow .cell-sensor::before { display: none; }
  .sensor-table .srow .cell-eval { grid-column: 1 / -1; margin-top: -2px; }
  .sensor-table .srow .cell-eval::before { display: none; }
  .evt { margin-left: 19px; }
  .sensor-table .srow .num { text-align: left; }
  /* Ẩn cột Min (và Đơn vị) trên card cho gọn — 3 giá trị Avg/Max/Count vẫn đủ */
  .sensor-table .srow .cell-min,
  .sensor-table .srow .cell-unit { display: none; }
  .sensor-table .srow .cell-status { grid-column: 1 / -1; margin-top: 2px; }
  .sensor-table .srow .cell-status::before { display: none; }

  .sensor-table .srow-empty { display: block; }
  .sensor-table .srow-empty td { display: block; }
  .sensor-table .srow-empty td::before { display: none; content: none; }
  .sensor-table .srow-empty td { grid-column: 1 / -1; }

  .health { padding: 11px 12px 12px; }
  .health__num { font-size: 20px; }
  .infra { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
}

@media (max-width: 440px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi__val { font-size: 21px; }
  .kpi__foot .kpi-pill { display: none; } /* giữ dòng phụ, nhãn trạng thái trạm vẫn ở khối sức khoẻ */
  .sensor-table .srow { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .silent__item { padding: 2px 6px; }
  .silent__name { max-width: 14ch; }
}

/* In ấn: cho phép xuống phân trang hợp lý */
@media print {
  .kpi, .health, .sensor-table .srow { break-inside: avoid; box-shadow: none; }
  .tp-chips, .tp-reset { display: none; }
  .tp-searchwrap { border: 0; background: transparent; }
}

/* Tôn trọng thiết lập giảm chuyển động */
@media (prefers-reduced-motion: reduce) {
  .kpi, .sk-box, .parrow, .cbar > i, .health__bar > i, .kpi-pill.is-ok .pdot { animation: none !important; transition: none !important; }
}
