:root{
  --card-bg:#ffffff;
  --text:#0f172a;
  --muted:#64748b;
  --border:#e2e8f0;
  --shadow:0 10px 30px rgba(15,23,42,.06);
  --bg:#f3f4f6;
  --primary:#2563eb;
  --primary-soft:#eff6ff;
  --danger:#dc2626;
  --danger-soft:#fef2f2;
}

*{box-sizing:border-box}
html,body{height:100%}
body{
  background:#f6f8fc;
  color:var(--text);
  font-family:system-ui,Segoe UI,Roboto,Arial,sans-serif;
}
.container{max-width:1180px;margin:0 auto;padding:16px}
.hidden{display:none!important}

.card{
  background:var(--card-bg);
  border-radius:16px;
  box-shadow:var(--shadow);
  padding:16px;
  margin-bottom:12px;
  border:1px solid rgba(148,163,184,.35);
}
.row{display:flex;flex-wrap:wrap;gap:12px}

.btn-mini{
  display:inline-flex;align-items:center;gap:8px;
  border:1px solid var(--border);
  border-radius:999px;
  padding:0 14px;
  height:36px;
  background:#fff;
  color:var(--text);
  cursor:pointer;
  font-size:13px;
  transition:background .15s ease,transform .08s ease,box-shadow .15s ease,border-color .15s ease;
}
.btn-mini:hover{
  background:var(--primary-soft);
  border-color:rgba(37,99,235,.35);
  box-shadow:0 8px 22px rgba(37,99,235,.15);
  transform:translateY(-1px);
}
.btn-mini:active{transform:translateY(0)}

.btn-primary{
  background:linear-gradient(135deg,var(--primary),#1d4ed8);
  color:#fff;
  border-color:transparent;
}
.btn-primary:hover{box-shadow:0 12px 30px rgba(37,99,235,.35)}

.btn-danger{
  background:var(--danger-soft);
  border-color:rgba(220,38,38,.35);
  color:var(--danger);
}
.btn-danger:hover{
  background:var(--danger);
  color:#fff;
  border-color:transparent;
}

/* Header */
.header{
  position:sticky;top:0;z-index:10;
  background:#ffffffcc;
  backdrop-filter:blur(10px);
  border-bottom:1px solid rgba(148,163,184,.35);
}
.header-inner{display:flex;align-items:center;justify-content:space-between;gap:12px}
.hdr-title{display:flex;align-items:center;gap:10px}
.hdr-main{font-weight:900;font-size:16px;line-height:1.1}
.hdr-sub{font-size:12px;color:var(--muted);margin-top:2px}
.hdr-logo{
  width:34px;height:34px;border-radius:12px;
  background:linear-gradient(135deg,#22c55e,#16a34a);
  display:inline-flex;align-items:center;justify-content:center;
  color:#fff;font-size:13px;font-weight:900;
  box-shadow:0 8px 18px rgba(22,163,74,.25);
}
.hdr-actions{display:flex;gap:8px;align-items:center}

/* Settings icon button */
.btn-icon{
  width:40px;height:40px;
  border-radius:999px;
  border:1px solid var(--border);
  background:#fff;
  display:inline-flex;align-items:center;justify-content:center;
  cursor:pointer;
  color:#0f172a;
  transition:background .15s ease, box-shadow .15s ease, transform .08s ease;
}
.btn-icon svg{display:block}
.btn-icon:hover{
  background:var(--primary-soft);
  box-shadow:0 10px 24px rgba(37,99,235,.18);
  transform:translateY(-1px);
}
.btn-icon:active{transform:translateY(0)}

/* Tabs */
.tabs{display:flex;gap:8px;margin-bottom:12px;flex-wrap:wrap}
.tabs .tab-btn{border-radius:999px;background:#fff}
.tabs .tab-btn[aria-selected="true"]{
  background:linear-gradient(135deg,var(--primary),#1d4ed8);
  color:#fff;border-color:transparent;
  box-shadow:0 12px 30px rgba(37,99,235,.30);
}

/* View head */
.view-head{
  display:flex;
  gap:12px;
  align-items:flex-start;
  justify-content:space-between;
  margin-bottom:10px;
}
.view-title{font-weight:1000;font-size:16px}
.view-sub{font-size:12px;color:var(--muted);margin-top:3px}
.view-actions{
  display:flex;gap:8px;align-items:center;flex-wrap:wrap;justify-content:flex-end;
}
.search{
  height:36px;
  min-width:220px;
  border-radius:999px;
  border:1px solid var(--border);
  padding:0 12px;
  outline:none;
  background:#fff;
}

/* Filters */
.filters{
  display:flex;
  flex-wrap:wrap;
  align-items:center;
  gap:10px 12px;
  margin-bottom:10px;
}
.toolbar-inline{margin-left:auto;display:inline-flex;align-items:center;gap:8px}

/* Menu col / filter */
.col-menu{position:relative}
.col-menu-panel{
  position:absolute;
  left:0;
  top:calc(100% + 6px);
  background:#fff;
  border:1px solid var(--border);
  border-radius:12px;
  padding:8px;
  box-shadow:0 16px 36px rgba(15,23,42,.18);
  z-index:50;
  min-width:240px;
  max-height:50vh;
  overflow:auto;
}
.menu-toolbar{
  position:sticky;top:0;
  background:#fff;
  padding:6px 4px;
  display:flex;gap:6px;
  border-bottom:1px solid var(--border);
}
.menu-list{padding:6px 4px}
.ky-row,.col-row{display:flex;align-items:center;gap:8px;margin:4px 0;cursor:pointer;font-size:13px}

/* KPI */
.kpi-wrap{margin:10px 0 14px}
.kpi-grid{
  display:grid;
  grid-template-columns:repeat(4,minmax(0,1fr));
  gap:10px;
}
@media (max-width:1100px){ .kpi-grid{grid-template-columns:repeat(3,1fr);} }
@media (max-width:820px){ .kpi-grid{grid-template-columns:repeat(2,1fr);} }
@media (max-width:520px){ .kpi-grid{grid-template-columns:1fr;} }

.kpi-card{
  border:1px solid var(--border);
  border-radius:14px;
  background:#fff;
  padding:12px 12px;
  box-shadow:0 6px 18px rgba(15,23,42,.06);
  min-width:0;
}
.kpi-label{
  font-size:12px;
  color:var(--muted);
  font-weight:800;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.kpi-value{
  margin-top:6px;
  font-size:18px;
  font-weight:1000;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.kpi-empty{color:var(--muted);font-size:13px}

/* Charts */
.charts-grid{
  display:grid;
  grid-template-columns:repeat(auto-fit,minmax(320px,1fr));
  gap:12px;
  margin-bottom:14px;
}
.chart-card{
  background:#fff;
  border:1px solid var(--border);
  border-radius:16px;
  box-shadow:var(--shadow);
  overflow:hidden;
}
.chart-head{padding:12px 12px 8px}
.chart-title{font-weight:1000;font-size:14px}
.chart-body{padding:8px 12px 12px}
.chart-body canvas{width:100%!important;height:100%!important}

/* Table */
.table-card{
  border-radius:16px;
  background:#fff;
  box-shadow:var(--shadow);
  border:1px solid rgba(148,163,184,.35);
}
.table-scroll{max-height:65vh;overflow:auto}
.table{
  width:100%;
  border-collapse:collapse;
  table-layout:auto;
  font-size:13px;
}
.table th,.table td{
  min-width:140px;
  border-bottom:1px solid var(--border);
  padding:.6rem .5rem;
  text-align:left;
  background:#fff;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.table-scroll thead th{
  position:sticky;top:0;z-index:2;
  background:#fff;
  box-shadow:0 1px 0 rgba(0,0,0,.06);
}
.table tbody tr:nth-child(odd) td{background:rgba(148,163,184,.04)}
.table tbody tr:hover td{background:rgba(37,99,235,.06);cursor:pointer}
.table th{position:relative}
.col-resizer{
  position:absolute;top:0;right:-4px;height:100%;width:8px;
  cursor:col-resize;user-select:none;
}
.col-hidden{display:none}

/* Modal */
.gd-modal{
  position:fixed;inset:0;
  background:rgba(15,23,42,.45);
  display:flex;align-items:center;justify-content:center;
  z-index:50;
}
.modal-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:8px}
.modal-foot{margin-top:12px;display:flex;justify-content:flex-end;gap:8px}

/* Settings */
.cfg-card{
  width:min(980px,94vw);
  max-height:90vh;
  overflow:auto;
}
.cfg-scroll{
  max-height:40vh;
  overflow:auto;
  border:1px solid var(--border);
  border-radius:12px;
  padding:8px;
  background:#fff;
}
.cfg-grid{
  display:grid;
  grid-template-columns:repeat(auto-fill,minmax(220px,1fr));
  gap:8px;
}
.chip{
  display:flex;align-items:center;gap:10px;
  border:1px solid var(--border);
  border-radius:12px;
  padding:8px 10px;
  background:#fff;
}
.chip input[type="checkbox"]{margin:0}
.chip span{
  flex:1;
  min-width:0;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.chip-input{
  width:100%;
  height:36px;
  border:1px solid var(--border);
  border-radius:12px;
  padding:0 12px;
  outline:none;
  background:#fff;
  margin-top:6px;
}
.chip-input:focus{border-color:rgba(37,99,235,.5);box-shadow:0 0 0 2px rgba(37,99,235,.12)}

/* Settings - charts config */
.cfg-charts{display:flex;flex-direction:column;gap:10px}
.cfg-chart-block{
  border:1px solid rgba(148,163,184,.35);
  border-radius:14px;
  padding:10px;
  background:#fff;
}
.cfg-check{display:flex;align-items:center;gap:10px;font-weight:900}
.cfg-muted{margin-top:6px;color:var(--muted);font-size:12px}
.cfg-chart-fields{
  margin-top:10px;
  display:grid;
  grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
  gap:10px;
}
.cfg-fee-grid{
  margin-top:10px;
  display:grid;
  grid-template-columns:repeat(auto-fit,minmax(220px,1fr));
  gap:10px;
}
.cfg-field-label{font-size:12px;font-weight:800;color:var(--muted);margin-bottom:6px}
.cfg-field select{
  width:100%;
  height:34px;
  border:1px solid var(--border);
  border-radius:10px;
  padding:0 10px;
  background:#fff;
  outline:none;
}

/* ✅ Top N input */
.cfg-field .cfg-num{
  width:100%;
  height:34px;
  border:1px solid var(--border);
  border-radius:10px;
  padding:0 10px;
  background:#fff;
  outline:none;
}

/* Error toast */
.error-overlay{
  position:fixed;
  left:12px;bottom:12px;
  z-index:99999;
  max-width:60ch;
  background:#020617;
  color:#e5e7eb;
  padding:10px 12px;
  border-radius:12px;
  font:12px/1.4 system-ui,Segoe UI,Roboto;
  box-shadow:0 16px 40px rgba(15,23,42,.7);
  pointer-events:none;
}

/* Detail popup */
.detail-card{width:min(980px,94vw);max-height:90vh;overflow:auto}
.detail-body{padding-top:4px}
.detail-grid{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:12px;
}
@media (max-width:900px){ .detail-grid{grid-template-columns:1fr;} }
.detail-section{
  border:1px solid var(--border);
  border-radius:14px;
  padding:12px;
  background:#fff;
}
.section-title{font-weight:1000;margin-bottom:8px}
.kv{
  display:flex;justify-content:space-between;gap:10px;
  padding:6px 0;
  border-bottom:1px dashed rgba(148,163,184,.35);
}
.kv:last-child{border-bottom:none}
.kv .k{color:var(--muted);font-weight:800}
.kv .v{text-align:right;font-weight:900}
.sep{height:10px}

/* Report (PDF) - shared */
.report-root{
  position:fixed;
  left:-99999px;
  top:0;
  width:1000px;
  background:#fff;
}

/* =========================
   PDF PAGED (A4) - FIX CUT
   ========================= */
.report-a4{
  width:1000px;
  height:1414px;              /* 1000 * 297/210 */
  padding:42px 46px 34px;
  box-sizing:border-box;
  background:#fff;
  display:flex;
  flex-direction:column;
  gap:12px;
}

.report-header{
  display:flex;justify-content:space-between;align-items:flex-start;
  padding-bottom:12px;
  border-bottom:2px solid #0f172a;
}
.report-title{font-size:20px;font-weight:1000;letter-spacing:.3px}
.report-sub{margin-top:4px;color:#334155;font-size:12px}
.report-badge{
  font-weight:900;
  background:#0f172a;
  color:#fff;
  padding:8px 12px;
  border-radius:999px;
}

.report-body{
  flex:1;
  overflow:hidden;
  display:flex;
  flex-direction:column;
  gap:12px;
}

.report-footer{
  margin-top:auto;
  padding-top:8px;
  border-top:1px solid #e2e8f0;
  font-size:11px;
  color:#475569;
  display:flex;
  justify-content:space-between;
  gap:10px;
}

.report-block{ break-inside:avoid; }

.report-section{margin-top:14px}
.report-section-title{
  font-weight:1000;
  font-size:13px;
  margin-bottom:8px;
  color:#0f172a;
}
.report-filters{display:flex;flex-wrap:wrap;gap:8px}
.rf{
  border:1px solid #e2e8f0;
  border-radius:999px;
  padding:6px 10px;
  font-size:12px;
}
.muted{color:#64748b}

/* ===== PDF: CHARTS LAYOUT (nhỏ + 2 cột) ===== */
.report-charts-grid{
  display:grid;
  grid-template-columns:repeat(2, minmax(0, 1fr));
  gap:10px;
}
@media (max-width: 820px){
  .report-charts-grid{ grid-template-columns:1fr; }
}

.report-chart{
  border:1px solid #e2e8f0;
  border-radius:14px;
  padding:8px;
  background:#fff;
}
.report-chart-title{
  font-weight:900;
  font-size:12px;
  margin-bottom:6px;
}
.report-chart img{
  width:100%;
  height:auto;          /* ✅ cố định chiều cao -> chart nhỏ gọn */
  object-fit:contain;    /* ✅ giữ tỉ lệ, không bị méo */
  border-radius:10px;
  border:1px solid #eef2f7;
  background:#fff;
}

.report-table-wrap{border:1px solid #e2e8f0;border-radius:14px;overflow:hidden}
.report-table{
  width:100%;
  border-collapse:collapse;
  font-size:12px;
  table-layout:fixed;
}
.report-table th,.report-table td{
  border-bottom:1px solid #e2e8f0;
  padding:6px 8px;
  white-space:nowrap;
}
.report-table td{
  overflow:hidden;
  text-overflow:ellipsis;
}
.report-table th{background:#f8fafc;text-align:left;font-weight:900}
.report-table .num{ text-align:right; }
.report-table .apt{ font-weight:900; }

.kpi-section{
  margin-bottom:14px;
}

.kpi-section-title{
  font-size:13px;
  font-weight:900;
  color:#334155;
  margin:0 0 8px 2px;
}

.kpi-card--primary{
  border-color:rgba(37,99,235,.25);
  background:linear-gradient(180deg,#ffffff 0%, #f8fbff 100%);
}

.summary-layout-head{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  margin-bottom:10px;
}

.summary-layout-title{
  font-weight:900;
  font-size:13px;
}

.summary-group-card{
  border:1px solid var(--border);
  border-radius:14px;
  padding:12px;
  background:#fff;
  margin-bottom:10px;
}

.summary-group-top{
  display:flex;
  gap:10px;
  align-items:flex-end;
}
.kpi-subgroups{
  display:flex;
  flex-direction:column;
  gap:12px;
}

.kpi-subgroup{
  border:1px dashed rgba(148,163,184,.5);
  border-radius:14px;
  padding:12px;
  background:#fafcff;
}

.kpi-subgroup-title{
  font-size:12px;
  font-weight:900;
  color:#475569;
  margin:0 0 8px 2px;
}

.summary-subgroups{
  display:flex;
  flex-direction:column;
  gap:10px;
  margin-top:10px;
}

.summary-subgroup-card{
  border:1px dashed rgba(148,163,184,.45);
  border-radius:12px;
  padding:10px;
  background:#fbfdff;
}

.summary-subgroup-top{
  display:flex;
  gap:8px;
  align-items:flex-end;
}

.summary-col-list{
  display:flex;
  flex-direction:column;
  gap:8px;
  margin-top:8px;
}

.summary-col-row{
  display:flex;
  gap:8px;
  align-items:center;
}
.summary-col-row.is-selected{
  opacity:1;
}

.summary-col-row.is-unselected{
  opacity:.7;
}

.summary-col-row .btn-mini[disabled]{
  opacity:.35;
  cursor:not-allowed;
  pointer-events:none;
}
.report-charts-grid.is-single{
  grid-template-columns:1fr;
}

.report-charts-grid.is-multi{
  grid-template-columns:repeat(2, minmax(0, 1fr));
}

.report-chart--single img{
  height:320px;
}

.report-chart img{
  width:100%;
  height:220px;
  object-fit:contain;
  border-radius:10px;
  border:1px solid #eef2f7;
  background:#fff;
}