:root {
  --bg: #f4f6fb;
  --bg-card: #ffffff;
  --bg-header: #101c33;
  --text: #1b2437;
  --text-dim: #6b768c;
  --border: #e2e8f2;
  --accent: #2563eb;
  --sidebar-bg: #16233f;
  --sidebar-text: #cdd7ec;
  --sidebar-active: #2563eb;
  --burn-red: #e5484d;
  --burn-orange: #f5a524;
  --burn-green: #17a34a;
  --shadow: 0 2px 10px rgba(16, 28, 51, .08);
}

[data-theme="dark"] {
  --bg: #0d1424;
  --bg-card: #162033;
  --bg-header: #0a1120;
  --text: #e8edf7;
  --text-dim: #93a0bb;
  --border: #26334d;
  --accent: #5b8cff;
  --sidebar-bg: #0a1120;
  --sidebar-text: #aeb9d2;
  --shadow: 0 2px 10px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}
.hidden { display: none !important; }

/* ---------- Header ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg-header); color: #fff;
  padding: 10px 20px; position: sticky; top: 0; z-index: 50;
}
.brand { font-size: 1.35rem; font-weight: 800; letter-spacing: .5px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.commander { display: flex; align-items: center; gap: 8px; margin-right: 6px; }
.commander-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: linear-gradient(135deg, #f5a524, #e5484d);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: .85rem; color: #fff;
}
.commander-name { font-weight: 700; font-size: .95rem; }
.icon-btn {
  background: rgba(255,255,255,.12); color: #fff; border: none;
  width: 36px; height: 36px; border-radius: 10px; font-size: 1.1rem;
  cursor: pointer; transition: background .2s;
}
.icon-btn:hover { background: rgba(255,255,255,.25); }

/* ---------- Mode switch ---------- */
.mode-switch { display: flex; justify-content: center; gap: 10px; padding: 14px 0 4px; }
.mode-btn {
  padding: 9px 34px; font-size: 1rem; font-weight: 700;
  border: 2px solid var(--accent); border-radius: 999px;
  background: transparent; color: var(--accent); cursor: pointer;
}
.mode-btn.active { background: var(--accent); color: #fff; }

/* ---------- Dashboard ---------- */
#dashboardView { max-width: 1200px; margin: 0 auto; padding: 14px 16px 40px; }
.meters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 18px; }
.meter-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 16px; padding: 18px; box-shadow: var(--shadow); text-align: center;
}
.meter-title { font-weight: 800; font-size: 1.05rem; margin-bottom: 6px; }
.meter-sub { color: var(--text-dim); font-size: .85rem; margin-top: 4px; }
.ring { width: 130px; height: 130px; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--border); stroke-width: 12; }
.ring-fg {
  fill: none; stroke-width: 12; stroke-linecap: round;
  stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .8s ease;
}
.ring-blue { stroke: var(--accent); }
.ring-green { stroke: var(--burn-green); }
.ring-num {
  transform: rotate(90deg); transform-origin: center;
  font-size: 30px; font-weight: 800; fill: var(--text); text-anchor: middle;
  dominant-baseline: central;
}
.social-list { display: flex; flex-direction: column; gap: 12px; padding: 8px 4px; text-align: left; }
.social-row { display: flex; align-items: center; gap: 10px; }
.social-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.social-name { font-weight: 700; width: 84px; }
.social-val { margin-left: auto; font-weight: 800; color: var(--accent); }
.social-sub { font-size: .75rem; color: var(--text-dim); width: 100%; }

.panel-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow); overflow: hidden;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; }
.panel-head h2 { margin: 0; font-size: 1.15rem; }
.updated-at { font-size: .8rem; color: var(--text-dim); }
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.data-table th, .data-table td { padding: 10px 12px; text-align: left; border-top: 1px solid var(--border); white-space: nowrap; }
.data-table thead th { border-top: none; background: rgba(127,140,160,.08); font-size: .8rem; text-transform: uppercase; letter-spacing: .4px; color: var(--text-dim); }
.burn { font-weight: 800; padding: 3px 10px; border-radius: 999px; color: #fff; display: inline-block; min-width: 52px; text-align: center; }
.burn-red { background: var(--burn-red); }
.burn-orange { background: var(--burn-orange); }
.burn-green { background: var(--burn-green); }
.agent-cell { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.mini-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-size: .7rem; font-weight: 800;
}
.avatar-img { object-fit: cover; padding: 0; }
img.commander-avatar { object-fit: cover; display: block; padding: 0; }
.notice {
  padding: 26px; text-align: center; color: var(--text-dim); font-size: .95rem; line-height: 1.6;
}

/* ---------- Functions layout ---------- */
#functionsView { display: flex; min-height: calc(100vh - 130px); }
.sidebar {
  width: 230px; flex-shrink: 0; background: var(--sidebar-bg);
  padding: 14px 10px; display: flex; flex-direction: column; gap: 6px;
}
.side-btn {
  background: transparent; border: none; color: var(--sidebar-text);
  text-align: left; padding: 12px 14px; border-radius: 10px;
  font-size: .95rem; font-weight: 600; cursor: pointer;
}
.side-btn:hover { background: rgba(255,255,255,.08); }
.side-btn.active { background: var(--sidebar-active); color: #fff; }
.panel { flex: 1; padding: 18px; min-width: 0; }
.panel iframe { width: 100%; height: calc(100vh - 190px); border: 1px solid var(--border); border-radius: 12px; background: #fff; }

/* ---------- Kanban ---------- */
.kanban-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-wrap: wrap; gap: 10px; }
.kanban-head h2 { margin: 0; }
.btn { padding: 9px 18px; border-radius: 10px; font-weight: 700; cursor: pointer; border: none; font-size: .9rem; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
.kanban { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.kcol {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 14px;
  padding: 12px; min-height: 320px; box-shadow: var(--shadow);
}
.kcol.dragover { outline: 2px dashed var(--accent); outline-offset: -4px; }
.kcol h3 { margin: 4px 4px 12px; font-size: .95rem; display: flex; justify-content: space-between; align-items: center; }
.kcount { background: rgba(127,140,160,.18); border-radius: 999px; padding: 2px 10px; font-size: .8rem; }
.kcard {
  background: var(--bg); border: 1px solid var(--border); border-left: 5px solid var(--agent-color, var(--accent));
  border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; cursor: grab;
  box-shadow: var(--shadow);
}
.kcard:active { cursor: grabbing; }
.kcard.dragging { opacity: .45; }
.kcard-title { font-weight: 600; font-size: .9rem; margin-bottom: 8px; }
.kcard-agent { display: flex; align-items: center; gap: 7px; font-size: .78rem; color: var(--text-dim); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box {
  background: var(--bg-card); border-radius: 16px; padding: 24px;
  width: min(420px, 92vw); box-shadow: var(--shadow);
}
.modal-box h3 { margin: 0 0 16px; }
.modal-box label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: 14px; }
.modal-box select, .modal-box input {
  width: 100%; margin-top: 6px; padding: 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: .95rem;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* ---------- Chart sức khỏe ---------- */
.chart-wrap { position: relative; padding: 6px 14px 0; }
.chart-wrap canvas { width: 100% !important; }
.chart-legend {
  display: flex; flex-wrap: wrap; gap: 8px 22px;
  padding: 10px 18px 16px; font-size: .82rem; color: var(--text-dim);
}
.lg-dot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---------- Org Chart native ---------- */
.org-wrap { max-width: 1100px; margin: 0 auto; padding: 8px 4px 24px; }
.org-level { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; }
.org-row { gap: 12px; }
.org-vline { width: 2px; height: 20px; background: var(--border); margin: 2px auto; }
.org-tag {
  text-align: center; color: var(--text-dim); font-size: .8rem;
  letter-spacing: 3px; text-transform: uppercase; margin: 8px 0 10px;
}
.org-commander {
  display: flex; align-items: center; gap: 16px;
  border: 2px solid #d4af37; border-radius: 16px; padding: 16px 30px;
  background: var(--bg-card); box-shadow: var(--shadow);
}
.org-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-top: 4px solid var(--agent-color, var(--accent));
  border-radius: 14px; padding: 14px 16px; width: 210px;
  box-shadow: var(--shadow);
}
.org-top { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.org-avatar { width: 44px; height: 44px; font-size: .9rem; border-radius: 50%; object-fit: cover; }
.org-name { font-weight: 800; font-size: 1rem; }
.org-role { font-size: .78rem; color: #b8860b; }
.org-role.gold { color: #d4af37; }
.org-stats { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.org-quota { font-weight: 800; font-size: .9rem; }
.link-actions { white-space: nowrap; }
.btn-sm { padding: 5px 12px; font-size: .8rem; text-decoration: none; display: inline-block; margin-right: 6px; }
a.btn-sm { color: var(--text); }
.kcard-meta { display: flex; justify-content: space-between; gap: 8px; font-size: .72rem; color: var(--text-dim); margin-bottom: 8px; }
.kfolder { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 70%; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .meters { grid-template-columns: 1fr; }
  #functionsView { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; overflow-x: auto; }
  .side-btn { white-space: nowrap; }
  .kanban { grid-template-columns: 1fr; }
  .commander-name { display: none; }
}
