@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Manrope:wght@400;500;600;700;800&display=swap');

/* ---------- Design tokens (từ public/UI_GUIDE.md, X-OR CODE) ---------- */
:root {
  --primary: #000000;
  --on-primary: #ffffff;
  --background: #f9f9f9;
  --surface: #f9f9f9;
  --surface-container-lowest: #ffffff;
  --surface-container-low: #f3f3f4;
  --surface-container: #eeeeee;
  --surface-container-high: #e8e8e8;
  --surface-container-highest: #e2e2e2;
  --on-surface: #1a1c1c;
  --on-surface-variant: #474747;
  --outline: #777777;
  --outline-variant: #c6c6c6;
  --error: #ba1a1a;
  --success: #059669;
  --brand-blue: #0092d1;
}

/* Mặc định theo theme hệ điều hành, override được bằng nút toggle
   (data-theme="dark"/"light" trên <html>, lưu localStorage). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary: #ffffff;
    --on-primary: #000000;
    --background: #0d0e0e;
    --surface: #0d0e0e;
    --surface-container-lowest: #0a0b0b;
    --surface-container-low: #1a1c1c;
    --surface-container: #212323;
    --surface-container-high: #2b2d2d;
    --surface-container-highest: #333535;
    --on-surface: #e2e2e2;
    --on-surface-variant: #c6c6c6;
    --outline: #919191;
    --outline-variant: #474747;
    --error: #ffb4ab;
    --success: #34d399;
  }
}

/* Ép dark dù hệ điều hành đang light (bấm nút toggle) */
:root[data-theme="dark"] {
  --primary: #ffffff;
  --on-primary: #000000;
  --background: #0d0e0e;
  --surface: #0d0e0e;
  --surface-container-lowest: #0a0b0b;
  --surface-container-low: #1a1c1c;
  --surface-container: #212323;
  --surface-container-high: #2b2d2d;
  --surface-container-highest: #333535;
  --on-surface: #e2e2e2;
  --on-surface-variant: #c6c6c6;
  --outline: #919191;
  --outline-variant: #474747;
  --error: #ffb4ab;
  --success: #34d399;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--background);
  color: var(--on-surface);
  font-family: "Inter", sans-serif;
  font-size: 12px;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.3s, color 0.3s;
}

::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--outline) 20%, transparent); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: color-mix(in srgb, var(--outline) 40%, transparent); }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}
input:focus-visible { outline: none; border-color: var(--brand-blue); }

a { color: var(--brand-blue); }

/* ---------- Header ---------- */
header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--surface-container-lowest) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { height: 44px; width: auto; display: block; }
.brand-text { display: flex; flex-direction: column; gap: 2px; }
.brand-label {
  font-family: "Manrope", sans-serif;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--on-surface-variant);
}
.brand-slogan {
  font-size: 9px;
  font-weight: 500;
  color: var(--outline);
}

.header-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-low);
  color: var(--on-surface-variant);
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
}
.theme-toggle:hover { background: var(--surface-container-high); color: var(--on-surface); }

.logout-btn {
  padding: 5px 12px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: transparent;
  color: var(--on-surface-variant);
  border-radius: 999px;
  cursor: pointer;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.logout-btn:hover { background: var(--surface-container-high); color: var(--error); }

.ai-toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  border-radius: 999px;
  background: var(--surface-container-low);
  color: var(--on-surface-variant);
  padding: 5px 8px 5px 12px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  line-height: 1.4;
  cursor: pointer;
}
.ai-toggle-btn:hover { background: var(--surface-container-high); color: var(--on-surface); }
.ai-toggle-btn.active { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.ai-toggle-btn.active .kbd-badge { background: color-mix(in srgb, var(--on-primary) 20%, transparent); color: var(--on-primary); }

/* Badge hiện tổ hợp phím tắt cạnh nút — dùng chung cho AI toggle + rail agent. */
.kbd-badge {
  display: inline-flex;
  align-items: center;
  font-size: 9px;
  font-weight: 800;
  text-transform: none;
  letter-spacing: 0;
  background: var(--surface-container-high);
  color: var(--outline);
  border-radius: 5px;
  padding: 2px 5px;
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
}

/* ---------- Layout ---------- */
.app-body { display: flex; align-items: stretch; width: 100%; }
/* Không giới hạn max-width — dùng hết chiều ngang màn hình lớn thay vì co cụm
   giữa trang; các grid bên trong (kpi-row auto-fit, dashboard-grid fr,
   status-grid fr) đã tự giãn theo container-query nên vẫn responsive đúng
   khi main hẹp lại (drawer AI mở, viewport nhỏ). */
main { width: 100%; margin: 24px 0; padding: 0 32px; flex: 1 1 auto; min-width: 0; container-type: inline-size; box-sizing: border-box; }
@media (max-width: 640px) {
  main { padding: 0 16px; }
}

.tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.tab-btn {
  position: relative; /* containing block cho overlay .shortcuts-visible [data-shortcut]::after */
  padding: 8px 16px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-lowest);
  color: var(--on-surface);
  border-radius: 8px;
  cursor: pointer;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  transition: all 0.2s;
}
.tab-btn.active { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.tab-btn:not(.active):hover { background: var(--surface-container); }

/* ---------- Giữ Cmd/Ctrl → overlay gợi ý phím tắt (kiểu Notion/Linear) ----------
   Badge đè lên góc trên-phải của element có `data-shortcut`, chỉ hiện khi
   body có class `shortcuts-visible` (bật/tắt bằng JS lúc giữ/thả Cmd/Ctrl,
   xem app.js `setShortcutsVisible`). Dùng chung 1 pattern cho tab (Shift+chữ)
   và agent trong rail (đúng số, không Shift) — đây là màn "tra cứu toàn bộ
   phím tắt", không phải chỉ đúng phím đang giữ. */
body.shortcuts-visible [data-shortcut]::after {
  content: attr(data-shortcut);
  position: absolute;
  top: -7px;
  right: -6px;
  z-index: 5;
  background: var(--primary);
  color: var(--on-primary);
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: none;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 999px;
  border: 1px solid var(--surface-container-lowest);
  pointer-events: none;
  animation: shortcut-hint-in 0.1s ease;
}
@keyframes shortcut-hint-in {
  from { opacity: 0; transform: scale(0.7); }
  to { opacity: 1; transform: scale(1); }
}

/* !important cố ý: .hidden phải thắng mọi class display khác (.form-grid,
   label...) trên cùng phần tử — nếu không, phần tử nào vừa .hidden vừa 1
   class display khác định nghĩa SAU trong file này sẽ vẫn hiện ra (bug thật
   gặp khi test: #ai-agent-form vừa .form-grid vừa .hidden vẫn hiện). */
.hidden { display: none !important; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }
.tab-panel .card + .card { margin-top: 16px; }
.tab-panel.card h2:first-child { margin-top: 0; }

/* ---------- Overview: KPI row ---------- */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi-card {
  position: relative;
  background: var(--surface-container-lowest);
  border: 1px solid color-mix(in srgb, var(--outline-variant) 10%, transparent);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kpi-value { font-family: "Manrope", sans-serif; font-size: 30px; font-weight: 800; color: var(--on-surface); line-height: 1.1; }
.kpi-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--on-surface-variant);
}
.kpi-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--outline-variant);
  flex-shrink: 0;
  transition: background-color 0.3s;
}
.kpi-card.status-ok .kpi-dot { background: var(--success); }
.kpi-card.status-warn .kpi-dot { background: #f5a623; }
.kpi-card.status-critical .kpi-dot { background: var(--error); }

.chart-wrap.gauge-wrap { height: 160px; display: flex; align-items: center; justify-content: center; }
.gauge-center {
  position: absolute;
  top: 58%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.gauge-value { font-family: "Manrope", sans-serif; font-size: 32px; font-weight: 800; color: var(--on-surface); }
.gauge-sub { font-size: 9px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em; color: var(--outline); }

/* ---------- Overview: bản đồ PoP (Leaflet + OpenStreetMap) ---------- */
.map-card { margin-bottom: 16px; }
.map-filter { display: flex; align-items: center; gap: 8px; font-size: 11px; font-weight: 700; color: var(--on-surface-variant); margin-bottom: 10px; }
.map-filter select { font-size: 12px; }
#pop-map { height: 420px; border-radius: 12px; overflow: hidden; }
/* Tile OpenStreetMap gốc luôn nền sáng — ở theme tối cần hạ sáng + đảo màu
   nhẹ để không chói lệch tông giữa các card tối xung quanh. Điều kiện áp
   dụng lặp lại đúng logic 2 tầng ở khối token màu phía trên (theo hệ điều
   hành, ghi đè được bằng nút toggle data-theme). */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) #pop-map .leaflet-tile-pane {
    filter: brightness(0.85) invert(1) hue-rotate(180deg) saturate(0.7);
  }
}
:root[data-theme="dark"] #pop-map .leaflet-tile-pane {
  filter: brightness(0.85) invert(1) hue-rotate(180deg) saturate(0.7);
}

/* ---------- Overview: dashboard grid ---------- */
.dashboard-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; align-items: start; }
.col-main, .col-side { display: flex; flex-direction: column; gap: 16px; }
.col-main .card, .col-side .card { margin-top: 0; } /* gap của flex đã lo khoảng cách, tránh cộng dồn với luật .card + .card */
@media (max-width: 900px) {
  .dashboard-grid { grid-template-columns: 1fr; }
}
@container (max-width: 900px) {
  .dashboard-grid { grid-template-columns: 1fr; }
}

/* 3 card "danh sách trạng thái" luôn cùng hàng, cùng chiều cao container —
   tách khỏi .dashboard-grid ở trên để không bị lệch theo số alert thật. */
.status-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 16px; }
@media (max-width: 900px) {
  .status-grid { grid-template-columns: 1fr; }
}
@container (max-width: 900px) {
  .status-grid { grid-template-columns: 1fr; }
}

.card-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 14px; gap: 8px; }
.card-source { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--outline); white-space: nowrap; }

.chart-wrap { position: relative; height: 240px; }
.chart-wrap-donut { height: 200px; }

/* ---------- Overview: compact status list (sidebar cards) ---------- */
.status-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.status-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 10%, transparent);
  font-size: 11px;
}
.status-list li:last-child { border-bottom: none; }
.status-list .item-name { font-weight: 600; color: var(--on-surface); }
.status-list .item-meta { font-size: 10px; color: var(--outline); font-weight: 500; }

/* ---------- Skeleton loading ---------- */
.skeleton {
  height: 14px;
  border-radius: 6px;
  background: linear-gradient(
    90deg,
    var(--surface-container) 25%,
    var(--surface-container-high) 37%,
    var(--surface-container) 63%
  );
  background-size: 400% 100%;
  animation: skeleton-shimmer 1.4s ease infinite;
}
td .skeleton { width: 80%; }
li.skeleton { height: 14px; margin: 9px 0; list-style: none; }
@keyframes skeleton-shimmer {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

.card h2 { font-family: "Manrope", sans-serif; font-size: 13px; font-weight: 700; margin: 0; }

.empty { color: var(--outline); font-style: italic; font-weight: 500; }

/* ---------- Severity / status badges ---------- */
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.sev-disaster { background: color-mix(in srgb, var(--error) 18%, transparent); color: var(--error); }
.sev-high { background: color-mix(in srgb, #ff6b35 18%, transparent); color: #d9480f; }
.sev-average { background: color-mix(in srgb, #f5a623 18%, transparent); color: #a1650a; }
.sev-warning { background: color-mix(in srgb, #f5c518 18%, transparent); color: #8a6d00; }
.sev-info { background: var(--surface-container-high); color: var(--on-surface-variant); }
.sev-ok { background: color-mix(in srgb, var(--success) 18%, transparent); color: var(--success); }

.card {
  background: var(--surface-container-lowest);
  border: 1px solid color-mix(in srgb, var(--outline-variant) 10%, transparent);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
  animation: card-fade-in 0.3s ease both;
}
@keyframes card-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.kpi-card { animation: card-fade-in 0.3s ease both; }

/* ---------- Form ---------- */
form { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 8px; }
form h2 {
  flex-basis: 100%;
  font-family: "Manrope", sans-serif;
  font-size: 13px;
  font-weight: 700;
  margin: 0 0 4px;
  color: var(--on-surface);
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--on-surface-variant);
}
input {
  padding: 7px 10px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-low);
  color: var(--on-surface);
  border-radius: 8px;
  font-size: 12px;
  font-family: "Inter", sans-serif;
  font-weight: 500;
  min-width: 160px;
  text-transform: none;
  letter-spacing: normal;
}
input::placeholder { color: var(--outline); }

.hint { font-size: 11px; color: var(--outline); margin: 0 0 14px; }

/* ---------- Buttons (X-OR CODE component classes) ---------- */
.btn-primary {
  background: var(--primary);
  color: var(--on-primary);
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 9px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  transition: opacity 0.2s;
}
.btn-primary:hover { opacity: 0.9; }

.btn-ghost {
  background: var(--surface-container-highest);
  color: var(--on-surface);
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 6px 12px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  transition: background-color 0.2s;
}
.btn-ghost:hover { background: var(--surface-container-high); }

/* ---------- Feedback + table ---------- */
.message { min-height: 20px; margin: 8px 0 12px; font-size: 11px; font-weight: 500; }
.message.error { color: var(--error); }
.message.success { color: var(--success); }

table { width: 100%; border-collapse: collapse; font-size: 11px; }
th {
  text-align: left;
  padding: 8px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--on-surface-variant);
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 20%, transparent);
}
td {
  text-align: left;
  padding: 8px;
  font-weight: 500;
  color: var(--on-surface);
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 10%, transparent);
}

/* ---------- Tự động hoá ---------- */
.switch-label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: normal;
  font-size: 12px;
  font-weight: 600;
  color: var(--on-surface);
}
.switch { position: relative; display: inline-block; width: 34px; height: 20px; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch-track {
  position: absolute;
  inset: 0;
  background: var(--outline-variant);
  border-radius: 999px;
  transition: background-color 0.2s;
}
.switch-track::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--surface-container-lowest);
  transition: transform 0.2s;
}
.switch input:checked + .switch-track { background: var(--success); }
.switch input:checked + .switch-track::before { transform: translateX(14px); }

/* ---------- Cài đặt ---------- */
.settings-list { display: flex; flex-direction: column; }
.settings-list .switch-label {
  padding: 10px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 20%, transparent);
}
.settings-list .switch-label:last-child { border-bottom: none; }
.widget-hidden { display: none !important; }

/* ---------- Agent — danh sách dạng card + form slide-over ---------- */
.conn-platform {
  padding: 14px 0;
  border-top: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
}
.conn-platform:first-of-type { border-top: none; padding-top: 4px; }
.conn-platform-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.conn-platform code { word-break: break-all; }

.agent-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.agent-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 14px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  border-radius: 12px;
  background: var(--surface-container-low);
}
.agent-card-name { font-size: 12.5px; font-weight: 700; color: var(--on-surface); }
.agent-card-meta { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--outline); }
.agent-card-actions { display: flex; gap: 6px; margin-top: auto; }
.agent-card-actions .btn-ghost { padding: 4px 10px; font-size: 9px; }

.ai-agent-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.ai-agent-scrim.open { opacity: 1; pointer-events: auto; }

.ai-agent-form-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(420px, 92vw);
  background: var(--surface-container-lowest);
  border-left: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.25);
  z-index: 61;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.2s ease;
}
.ai-agent-form-panel.open { transform: translateX(0); }

.ai-agent-form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  flex-shrink: 0;
}
.ai-agent-form-header h3 { margin: 0; font-family: "Manrope", sans-serif; font-size: 14px; font-weight: 800; color: var(--on-surface); }
.ai-agent-form-body { flex: 1 1 auto; overflow-y: auto; padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.ai-agent-form-footer {
  display: flex;
  gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  flex-shrink: 0;
}

.tool-group h4 {
  margin: 0 0 6px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--on-surface-variant);
}
.tool-group-hint { text-transform: none; font-weight: 600; color: var(--outline); letter-spacing: normal; }

.automation-status-row {
  display: flex;
  gap: 24px;
  margin: 4px 0 16px;
  padding: 12px 14px;
  background: var(--surface-container-low);
  border-radius: 10px;
}
.automation-status-row .item-meta { font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; color: var(--outline); }
.automation-status-row strong { font-size: 14px; color: var(--on-surface); }

.subhead {
  font-family: "Manrope", sans-serif;
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--on-surface-variant);
  margin: 4px 0 8px;
}

/* Tiêu đề nhóm card trên dashboard Tổng quan — mỗi card đã có <h2> riêng,
   đây là lớp phân loại cao hơn 1 bậc để dễ quét mắt khi trang dài. */
.dashboard-category {
  font-family: "Manrope", sans-serif;
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--outline);
  margin: 28px 0 12px;
  padding-top: 16px;
  border-top: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
}
.dashboard-category:first-child { margin-top: 0; padding-top: 0; border-top: none; }

.report-preview {
  max-height: 340px;
  overflow: auto;
  background: var(--surface-container-low);
  border: 1px solid color-mix(in srgb, var(--outline-variant) 30%, transparent);
  border-radius: 10px;
  padding: 12px 14px;
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 11px;
  line-height: 1.5;
  color: var(--on-surface);
  white-space: pre-wrap;
  word-break: break-word;
}

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); align-items: end; gap: 12px; }
.form-grid label input { width: 100%; min-width: 0; }

.ansible-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }

code {
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 11px;
  background: var(--surface-container-high);
  padding: 1px 5px;
  border-radius: 4px;
}

/* ---------- Trợ lý AI — Chat drawer (docked, kéo viền trái để resize) ---------- */
.ai-drawer {
  --ai-drawer-width: 440px;
  flex-shrink: 0;
  align-self: flex-start;
  position: relative; /* containing block cho .ai-drawer-resize-handle */
  width: 0;
  overflow: hidden;
  /* Ghim theo viewport (không cao bằng toàn bộ nội dung main) — nếu không,
     dashboard dài cuộn xuống sẽ kéo cả khung chat trôi mất khỏi màn hình.
     Trừ đúng --ai-header-h (JS đo thật, gán vào :root — xem aiSyncHeaderHeight
     trong app.js) ở CẢ top lẫn height, không phải top:0/height:100vh — nếu chỉ
     trừ ở top mà giữ height:100vh, đáy drawer sẽ tràn quá viewport đúng bằng
     chiều cao header lúc trang CHƯA cuộn (sticky chỉ thật sự dính top:0 sau
     khi cuộn qua khoảng đó) — bug thật đã gặp: composer bị cắt mất nửa dưới
     ngay khi vừa mở drawer, phải cuộn xuống mới thấy đủ. Dùng var(...,69px)
     làm fallback NGAY TẠI chỗ dùng — không khai báo lại property này trên
     .ai-drawer, vì 1 property tự khai báo trên chính element sẽ luôn thắng
     giá trị kế thừa từ :root, làm JS gán vào :root vô nghĩa. */
  top: var(--ai-header-h, 69px);
  height: calc(100vh - var(--ai-header-h, 69px));
  background: var(--surface-container-lowest);
  border-left: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  transition: width 0.15s ease;
}
.ai-drawer[data-open="true"] { position: sticky; width: var(--ai-drawer-width); }
/* Đang kéo resize: tắt transition để theo chuột tức thời, không giật/trễ. */
.ai-drawer.ai-drawer-resizing { transition: none; }

.ai-drawer-resize-handle {
  display: none;
  position: absolute;
  /* Vùng bấm rộng 12px (không phải 6px) — 6px quá mảnh, người dùng thật trên
     chuột thật lệch 1-2px là trượt ra ngoài, kéo thành bôi đen chọn text của
     trang thay vì resize (bug thật, không phải giả định). */
  left: -6px;
  top: 0;
  bottom: 0;
  width: 12px;
  cursor: col-resize;
  z-index: 5;
  background: transparent;
}
.ai-drawer-resize-handle:hover, .ai-drawer.ai-drawer-resizing .ai-drawer-resize-handle {
  background: color-mix(in srgb, var(--primary) 40%, transparent);
}
.ai-drawer[data-open="true"] .ai-drawer-resize-handle { display: block; }

.ai-drawer-inner { display: flex; height: 100%; width: 100%; min-height: 400px; }

.ai-rail {
  display: none;
  flex-direction: column;
  width: 150px;
  flex-shrink: 0;
  gap: 2px;
  padding: 12px 8px;
  border-right: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  overflow-y: auto;
}
/* Rail agent hiện khi drawer đủ rộng, HOẶC khi bấm nút toggle (☰) ép hiện —
   không bắt buộc người dùng phải kéo resize chính xác mới thấy được danh
   sách multi-agent. */
.ai-drawer.ai-drawer-wide .ai-rail,
.ai-drawer.ai-drawer-rail-forced .ai-rail { display: flex; }
.ai-rail-item {
  position: relative; /* containing block cho overlay .shortcuts-visible [data-shortcut]::after */
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  padding: 8px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--on-surface-variant);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.ai-rail-item:hover { background: var(--surface-container-high); }
.ai-rail-item.active { background: var(--primary); color: var(--on-primary); }
.ai-rail-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  border-radius: 5px;
  background: color-mix(in srgb, var(--outline-variant) 30%, transparent);
  color: var(--on-surface-variant);
  font-size: 9px;
  font-weight: 800;
}
.ai-rail-item.active .ai-rail-index { background: color-mix(in srgb, var(--on-primary) 25%, transparent); color: var(--on-primary); }

.ai-shell {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  padding: 16px;
  gap: 4px;
}

.ai-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.ai-header-left { display: flex; align-items: center; gap: 10px; }
.ai-header-right { display: flex; align-items: center; gap: 6px; }
.ai-header h2 { font-family: "Manrope", sans-serif; font-size: 14px; font-weight: 800; margin: 0; color: var(--on-surface); }
.ai-header-sub { margin: 2px 0 0; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--outline); }

.ai-agent-select {
  padding: 5px 8px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-low);
  color: var(--on-surface);
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  min-width: 0;
}

.ai-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--outline);
  flex-shrink: 0;
}

.ai-disclaimer { margin: 12px 0 16px; line-height: 1.5; }

.ai-messages {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 2px 12px;
}

.ai-action-card {
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-low);
  border-radius: 14px;
  padding: 12px 14px;
}
.ai-action-desc { font-size: 12.5px; line-height: 1.5; color: var(--on-surface); margin-bottom: 10px; }
.ai-action-buttons { display: flex; gap: 8px; }

.ai-msg { display: flex; flex-direction: column; padding: 6px 4px; gap: 4px; }
.ai-msg-user { align-items: flex-end; }
.ai-msg-assistant { align-items: stretch; }
.ai-msg-system { align-items: center; }

.ai-bubble {
  max-width: 80%;
  padding: 9px 13px;
  border-radius: 14px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  background: var(--surface-container-low);
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--on-surface);
  white-space: pre-wrap;
}
.ai-msg-user .ai-bubble { border-color: var(--primary); }

.ai-msg-text {
  width: 100%;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--on-surface);
  white-space: pre-wrap;
}

/* Nội dung Markdown render từ assistant (marked.parse) — .ai-msg-text đặt
   white-space:pre-wrap cho text thô, ở đây trả về normal vì đã có thẻ HTML
   thật lo xuống dòng/đoạn, không cần giữ newline nguồn nữa. */
.ai-markdown { white-space: normal; }
.ai-markdown p { margin: 0 0 8px; }
.ai-markdown p:last-child { margin-bottom: 0; }
.ai-markdown ul, .ai-markdown ol { margin: 0 0 8px; padding-left: 20px; }
.ai-markdown li { margin-bottom: 2px; }
.ai-markdown strong { font-weight: 700; color: var(--on-surface); }
.ai-markdown code {
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 11px;
  background: var(--surface-container-high);
  padding: 1px 5px;
  border-radius: 4px;
}
.ai-markdown pre {
  background: var(--surface-container-high);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 0 0 8px;
}
.ai-markdown pre code { background: none; padding: 0; }
.ai-markdown table { border-collapse: collapse; margin: 0 0 8px; width: 100%; }
.ai-markdown th, .ai-markdown td {
  border: 1px solid color-mix(in srgb, var(--outline-variant) 30%, transparent);
  padding: 4px 8px;
  text-align: left;
  font-size: 11.5px;
}
.ai-markdown a { color: var(--brand-blue); }
.ai-markdown blockquote {
  margin: 0 0 8px;
  padding-left: 10px;
  border-left: 2px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  color: var(--outline);
}

.ai-msg-system .ai-bubble {
  background: transparent;
  border-style: dashed;
  color: var(--outline);
  font-size: 11px;
  font-style: italic;
}

.ai-msg-footer { font-size: 10px; color: var(--outline); font-weight: 500; }

.ai-typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 9px 13px;
}
.ai-typing span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--outline);
  animation: ai-typing-bounce 1.1s ease-in-out infinite;
}
.ai-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes ai-typing-bounce {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.ai-composer { margin-top: 4px; }

.ai-input-box {
  border-radius: 16px;
  border: 1px solid color-mix(in srgb, var(--outline-variant) 35%, transparent);
  background: color-mix(in srgb, var(--on-surface) 3%, var(--surface-container-lowest));
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
.ai-input-box:hover { border-color: color-mix(in srgb, var(--outline-variant) 55%, transparent); }
.ai-input-box:focus-within {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--on-surface) 5%, var(--surface-container-lowest));
}

.ai-textarea {
  display: block;
  width: 100%;
  min-width: 0;
  margin: 0;
  padding: 12px 14px 6px;
  border: none;
  outline: none;
  background: transparent;
  box-sizing: border-box;
  resize: none;
  font-family: "Inter", sans-serif;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--on-surface);
  max-height: 160px;
  overflow-y: auto;
}
.ai-textarea::placeholder { color: var(--outline); }

.ai-toolbar { display: flex; align-items: center; gap: 2px; padding: 4px 8px 8px; }
.ai-toolbar-spacer { flex: 1; }

.ai-icon-btn {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--on-surface-variant);
  font-size: 13px;
  cursor: pointer;
  transition: background-color 0.15s ease;
}
.ai-icon-btn:hover { background: var(--surface-container-high); }

.ai-kbd-hint {
  font-size: 9px;
  font-weight: 800;
  color: var(--outline);
  background: var(--surface-container-high);
  border: 1px solid color-mix(in srgb, var(--outline-variant) 40%, transparent);
  border-radius: 6px;
  padding: 3px 7px;
  white-space: nowrap;
  margin-right: 8px;
}

.ai-send-btn {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: var(--surface-container-high);
  color: var(--outline);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, opacity 0.15s ease;
}
.ai-send-btn:not(:disabled) { background: var(--primary); color: var(--on-primary); }
.ai-send-btn:not(:disabled):hover { opacity: 0.9; }
.ai-send-btn:disabled { cursor: default; }

@media (max-width: 640px) {
  .ai-kbd-hint { display: none; }
}

/* Màn hẹp: drawer bóp dashboard xuống mức không dùng được nếu vẫn reflow —
   chuyển sang overlay tạm thời (nhấc lên, có bóng/scrim) thay vì đẩy nội
   dung, giống ngoại lệ mà X-OR STACK AI 4L áp dụng cho layout hẹp. */
@media (max-width: 900px) {
  .ai-drawer[data-open="true"] {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: min(var(--ai-drawer-width), 100vw);
    z-index: 40;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.25);
  }
}

/* ---------- Đăng nhập ---------- */
#app-shell.hidden { display: none; }

.login-screen {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--background);
  background-image: radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--primary) 6%, transparent), transparent 60%);
}
.login-screen.active { display: flex; }

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface-container-lowest);
  border: 1px solid color-mix(in srgb, var(--outline-variant) 15%, transparent);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 40px 120px rgba(0, 0, 0, 0.18);
  animation: card-fade-in 0.3s ease both;
}

.login-brand { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 24px; }
.login-brand img { height: 48px; width: auto; }
.login-brand-label {
  font-family: "Manrope", sans-serif;
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--on-surface-variant);
}
.login-title { font-family: "Manrope", sans-serif; font-size: 16px; font-weight: 800; color: var(--on-surface); margin: 0 0 4px; text-align: center; }
.login-sub { font-size: 11px; color: var(--outline); margin: 0 0 24px; text-align: center; }

#login-form { flex-direction: column; align-items: stretch; gap: 16px; margin-bottom: 0; }
#login-form label { gap: 6px; }
#login-form input { width: 100%; box-sizing: border-box; min-width: 0; }
.login-submit { width: 100%; padding: 11px 16px; }

.login-status { min-height: 16px; margin-top: 14px; font-size: 11px; font-weight: 600; text-align: center; color: var(--outline); }
.login-status.error { color: var(--error); }
tr:hover td { background: var(--surface-container-low); }

/* ---------- Quang (dark fibre) ---------- */
#tab-optical table td { vertical-align: top; }
#tab-optical .card-footer-row { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
#tab-optical .card-footer-row #optical-eval-btn { margin-left: auto; }
#optical-detail-body canvas { max-width: 100%; }

.optical-toolbar { justify-content: flex-start; margin: 0 0 8px; }
.optical-panel-actions { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--outline-variant); }
.optical-fieldset { border: 1px solid var(--outline-variant); border-radius: 8px; padding: 8px 12px 12px; margin: 0 0 12px; display: grid; gap: 8px; }
.optical-fieldset legend { font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em; padding: 0 6px; }
.optical-fieldset label { display: grid; gap: 4px; font-size: 11px; }
.optical-fieldset label.optical-check { display: flex; align-items: center; gap: 8px; }
.optical-danger { color: var(--error); }
.optical-subhead { display: block; margin: 14px 0 4px; font-size: 11px; }
.optical-layers { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin: 12px 0; }
.optical-layer { border: 1px solid var(--outline-variant); border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; }
.optical-layer-head { display: flex; align-items: baseline; gap: 6px; font-size: 12px; }
.optical-layer-val { margin-left: auto; font-weight: 700; }
.optical-bar { height: 6px; border-radius: 3px; background: var(--surface-container-high); overflow: hidden; }
.optical-bar > div { height: 100%; background: var(--success); }
.optical-layer-warn .optical-bar > div { background: #f5a623; }
.optical-layer-crit .optical-bar > div { background: var(--error); }
.optical-layer-none .optical-bar > div { background: var(--outline-variant); }
#optical-import-result details { margin-top: 8px; font-size: 11px; }
#optical-form { flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: 0; margin-bottom: 0; overflow-y: auto; }
.optical-fieldset { min-width: 0; box-sizing: border-box; }
.optical-fieldset input:not([type=checkbox]), .optical-fieldset select { width: 100%; box-sizing: border-box; }
