* { box-sizing: border-box; }
:root {
  --bg: #f4f6fb; --card: #fff; --line: #e3e7ef; --text: #1f2937; --muted: #6b7280;
  --pri: #2563eb; --pri-d: #1d4ed8; --danger: #dc2626; --ok: #16a34a; --side: #111827;
}
body { margin: 0; font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); display: flex; min-height: 100vh; font-size: 14px; }
a { color: var(--pri); text-decoration: none; }

/* Sidebar */
#sidebar { width: 250px; background: var(--side); color: #e5e7eb; padding: 16px; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; flex-shrink: 0; }
.brand { font-size: 18px; font-weight: 700; margin-bottom: 16px; cursor: pointer; }
.side-title { margin: 18px 0 6px; font-size: 11px; text-transform: uppercase; color: #9ca3af; letter-spacing: .05em; }
#catNav { flex: 1; overflow-y: auto; }
#catNav a, .side-link { display: flex; justify-content: space-between; padding: 8px 10px; border-radius: 6px; color: #e5e7eb; }
#catNav a:hover, .side-link:hover { background: #1f2937; }
#catNav a.active { background: var(--pri); color: #fff; }
#catNav .cnt { font-size: 11px; opacity: .7; }
.side-bottom { border-top: 1px solid #374151; padding-top: 10px; }

/* Main */
#main { flex: 1; padding: 24px 32px; min-width: 0; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.crumb { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.head-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px; cursor: pointer; transition: .15s; }
.tile:hover { border-color: var(--pri); box-shadow: 0 4px 14px rgba(37,99,235,.12); transform: translateY(-1px); }
.tile h3 { margin: 0 0 6px; font-size: 16px; }
.tile .meta { font-size: 12px; color: var(--muted); }
.empty { text-align: center; padding: 40px; color: var(--muted); }

/* Buttons & forms */
.btn { border: 1px solid var(--line); background: #fff; color: var(--text); padding: 7px 14px; border-radius: 7px; cursor: pointer;
  font-size: 13px; font-weight: 500; white-space: nowrap; }
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--pri); border-color: var(--pri); color: #fff; }
.btn.primary:hover { background: var(--pri-d); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.tg { background: #229ed9; border-color: #229ed9; color: #fff; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: wait; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
label { display: block; font-size: 12px; font-weight: 600; color: #374151; margin: 10px 0 4px; }
input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 7px; font-size: 14px; font-family: inherit; background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--pri); }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 140px; }
.inline-field { display: flex; flex-direction: column; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--line); margin-bottom: 18px; }
.tabs button { background: none; border: none; padding: 10px 16px; cursor: pointer; font-size: 14px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -2px; font-weight: 500; }
.tabs button.active { color: var(--pri); border-color: var(--pri); }

/* Tables */
.tbl-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; background: #fff; }
table.data th, table.data td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; }
table.data th { background: #f1f5f9; font-weight: 600; font-size: 13px; white-space: nowrap; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:hover td { background: #f8fafc; }
table.data tr.total td { background: #eff6ff; font-weight: 700; }
table.data tr.avg td { background: #f0fdf4; font-weight: 600; }
table.data input { padding: 5px 7px; font-size: 13px; }

/* Report (dùng để in và chụp ảnh) */
#report { background: #fff; padding: 20px; border-radius: 8px; }
#report .r-title { text-align: center; font-size: 18px; font-weight: 700; margin: 0 0 4px; text-transform: uppercase; }
#report .r-sub { text-align: center; color: var(--muted); margin-bottom: 14px; font-size: 13px; }
#report .r-foot { margin-top: 10px; font-size: 11px; color: var(--muted); text-align: right; }

/* Column editor */
.col-item { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.col-item input { flex: 2; }
.col-item select { flex: 1; }
.col-item .unit { flex: 1; }

/* Modal & toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-start; justify-content: center;
  padding-top: 6vh; z-index: 50; overflow-y: auto; }
.modal.hidden { display: none; }
.modal-box { background: #fff; border-radius: 12px; width: min(620px, 94vw); padding: 20px; margin-bottom: 40px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.modal-head h3 { margin: 0; }
.x { border: none; background: none; font-size: 24px; cursor: pointer; color: var(--muted); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#toast { position: fixed; bottom: 20px; right: 20px; z-index: 99; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); animation: fade .2s; max-width: 380px; }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.badge { display: inline-block; background: #eef2ff; color: #4338ca; padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; }

@media (max-width: 800px) {
  body { flex-direction: column; }
  #sidebar { width: 100%; height: auto; position: static; }
  #main { padding: 16px; }
}

/* In ấn: chỉ in phần báo cáo */
@media print {
  body { background: #fff; display: block; }
  #sidebar, .no-print, .tabs, .crumb, .head-row, #toast, .modal { display: none !important; }
  #main { padding: 0; }
  .card { border: none; padding: 0; margin: 0; }
  #report { padding: 0; }
  table.data th { background: #e5e7eb !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  table.data tr.total td { background: #dbeafe !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ===== Trang Tổng hợp & Phân tích ===== */
.side-bottom .side-link:first-child { background: #1e3a8a; color: #fff; margin-bottom: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 5px; margin: 0; padding: 4px 10px; border: 1px solid #d1d5db; border-radius: 99px;
  font-weight: 400; font-size: 12px; cursor: pointer; background: #fff; user-select: none; }
.chip input { width: auto; margin: 0; }
.chip.on { background: #eff6ff; border-color: var(--pri); color: var(--pri-d); }
.src-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.src-cat { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fafbfd; }
.src-cat-head { font-size: 14px; margin: 0 0 4px; display: flex; gap: 6px; align-items: center; cursor: pointer; }
.src-tbl { font-weight: 400; margin: 2px 0 2px 20px; display: flex; gap: 6px; align-items: center; cursor: pointer; font-size: 13px; }
.src-cat input, .src-tbl input { width: auto; margin: 0; }
.step { border: 1px solid var(--line); border-left: 4px solid var(--pri); border-radius: 8px; padding: 10px 14px; margin-bottom: 10px; background: #fff; }
.step.off { opacity: .55; border-left-color: #9ca3af; }
.step.err { border-left-color: var(--danger); }
.step-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.step-no { background: var(--pri); color: #fff; border-radius: 99px; width: 22px; height: 22px; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; }
.step-out { color: var(--ok); font-size: 12px; font-weight: 600; }
.step-tools { margin-left: auto; display: flex; gap: 4px; }
.step-body label:first-child { margin-top: 6px; }
.step-err { color: var(--danger); font-size: 13px; font-weight: 600; margin-top: 6px; }
.step-err:empty { display: none; }
.cond { display: flex; gap: 8px; margin-bottom: 6px; align-items: stretch; flex-wrap: wrap; }
.cond > select, .cond > input { flex: 1; min-width: 130px; }
.cond > .btn { flex: 0 0 auto; }
.add-steps { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; padding-top: 10px; border-top: 1px dashed var(--line); }
.fhelp { margin-top: 8px; font-size: 12px; background: #f8fafc; border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; }
.fhelp summary { cursor: pointer; color: var(--pri); font-weight: 600; }
.fhelp code { background: #e0e7ff; padding: 1px 5px; border-radius: 4px; display: inline-block; margin: 1px 0; }
.fhelp code.ins { cursor: pointer; background: #dcfce7; }
.fhelp code.ins:hover { background: #bbf7d0; }
#srcInfo code { background: #f1f5f9; padding: 0 4px; border-radius: 3px; font-size: 11px; }

/* Đơn vị hiển thị */
.col-item .cscale { flex: 1.3; }
.col-item .cdec { flex: 0 0 70px; }
.col-item select:disabled, .col-item input:disabled { background: #f3f4f6; opacity: .6; }
.fmt-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 6px 14px; margin-top: 6px; }
.fmt-row { display: flex; gap: 6px; align-items: center; }
.fmt-row span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.fmt-row select { flex: 1.2; padding: 4px 6px; font-size: 12px; }
.fmt-row input { flex: 0 0 64px; padding: 4px 6px; font-size: 12px; }
table.units input { padding: 5px 7px; }
#sideStyle { background: #1f2937; color: #e5e7eb; border-color: #374151; font-size: 12px; padding: 6px 8px; margin-bottom: 10px; }
.col-head { margin-bottom: 2px; font-size: 11px; color: var(--muted); font-weight: 600; }
.col-head span { display: block; padding: 0 2px; }
.col-head .cname { flex: 2; } .col-head .ctype { flex: 1; } .col-head .unit { flex: 1; } .col-head .cscale { flex: 1.3; } .col-head .cdec { flex: 0 0 70px; }

/* Ô nhập số thuần */
input.num-in { text-align: right; font-variant-numeric: tabular-nums; }
input.bad { border-color: var(--danger) !important; background: #fef2f2; }
.num-preview { font-size: 11px; color: var(--ok); min-height: 14px; margin-top: 2px; text-align: right; }
.num-preview.bad, .uEx.bad { color: var(--danger); }
.uEx { font-weight: 600; color: var(--ok); white-space: nowrap; }
table.units .uF { font-family: ui-monospace, Menlo, monospace; }
/* ẩn mũi tên của mọi ô số còn lại (nếu có) */
input[type=number]::-webkit-outer-spin-button, input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; appearance: textfield; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { background: #e2e8f0; }
th.sortable .arrow { color: #94a3b8; font-size: 11px; }
th.sorted { color: var(--pri-d); }
th.sorted .arrow { color: var(--pri); }
table.qcfg { max-width: 640px; }
table.qcfg td { padding: 4px 8px; }
table.qcfg input:not([type=checkbox]) { padding: 5px 8px; font-size: 13px; }
table.qcfg tr.off td { opacity: .5; }
table.qcfg tr.is-row td:nth-child(2) { font-style: italic; }

/* Kéo thả cột */
.grip { color: #94a3b8; cursor: grab; margin-right: 4px; font-size: 12px; user-select: none; }
.grip.big { font-size: 18px; padding: 0 2px; align-self: center; }
th[draggable=true] { cursor: grab; }
th[draggable=true]:active, .drag-field:active { cursor: grabbing; }
.drag-field label[draggable] { cursor: grab; }
.dragging { opacity: .4; }
th.drop-before, .drag-field.drop-before { box-shadow: inset 3px 0 0 var(--pri); }
th.drop-after, .drag-field.drop-after { box-shadow: inset -3px 0 0 var(--pri); }
.col-item.dragging { outline: 2px dashed var(--pri); }

/* Căn giữa giá trị trong các bảng số liệu (không áp dụng cho bảng cấu hình) */
table.data:not(.qcfg):not(.units) th, table.data:not(.qcfg):not(.units) td,
table.data:not(.qcfg):not(.units) td.num, table.data:not(.qcfg):not(.units) th.num { text-align: center; }
table.data:not(.qcfg):not(.units) td:last-child.no-print { white-space: nowrap; }

/* ===== Đăng nhập & phân quyền ===== */
body:not(.can-edit) .need-edit { display: none !important; }
body:not(.is-admin) .need-admin { display: none !important; }
body.auth-screen #sidebar { display: none; }
body.auth-screen #main { padding: 0; }
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #60a5fa 100%); }
.auth-card { background: #fff; border-radius: 14px; padding: 28px 28px 22px; width: min(400px, 100%); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.auth-logo { font-size: 42px; text-align: center; }
.auth-msg { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; font-size: 13px; text-align: center; }
#userBox { border-top: 1px solid #374151; margin-top: 8px; padding-top: 10px; }
.user-chip { display: flex; gap: 10px; align-items: center; }
.avatar { width: 34px; height: 34px; border-radius: 99px; background: var(--pri); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
.u-name { font-weight: 600; color: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-role { font-size: 11px; color: #9ca3af; }
.side-btn { background: #1f2937; color: #e5e7eb; border-color: #374151; flex: 1; }
.side-btn:hover { background: #374151; }
.badge.role-admin { background: #fee2e2; color: #991b1b; }
.badge.role-editor { background: #dcfce7; color: #166534; }
.badge.role-viewer { background: #e0e7ff; color: #3730a3; }
table.users-tbl tr.off td { opacity: .55; }
/* Điện thoại */
@media (max-width: 800px) {
  #sidebar { padding: 10px 12px; }
  #catNav { max-height: 180px; }
  .row > * { min-width: 120px; }
  .tabs { overflow-x: auto; }
  .tabs button { padding: 8px 10px; white-space: nowrap; }
  h1 { font-size: 19px; }
}

/* ===== Đăng nhập & phân quyền ===== */
body:not(.can-edit) .need-edit { display: none !important; }
body:not(.is-admin) .need-admin { display: none !important; }
body.auth-screen #sidebar { display: none; }
body.auth-screen #main { padding: 0; }
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 50%, #60a5fa 100%); }
.auth-card { background: #fff; border-radius: 14px; padding: 28px 28px 22px; width: min(400px, 100%); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.auth-logo { font-size: 42px; text-align: center; }
.auth-msg { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; font-size: 13px; text-align: center; }
#userBox { border-top: 1px solid #374151; margin-top: 8px; padding-top: 10px; }
.user-chip { display: flex; gap: 10px; align-items: center; }
.avatar { width: 34px; height: 34px; border-radius: 99px; background: var(--pri); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
.u-name { font-weight: 600; color: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-role { font-size: 11px; color: #9ca3af; }
.side-btn { background: #1f2937; color: #e5e7eb; border-color: #374151; flex: 1; }
.side-btn:hover { background: #374151; }
.badge.role-admin { background: #fee2e2; color: #991b1b; }
.badge.role-editor { background: #dcfce7; color: #166534; }
.badge.role-viewer { background: #e0e7ff; color: #3730a3; }
table.users-tbl tr.off td { opacity: .55; }
/* Điện thoại */
@media (max-width: 800px) {
  #sidebar { padding: 10px 12px; }
  #catNav { max-height: 180px; }
  .row > * { min-width: 120px; }
  .tabs { overflow-x: auto; }
  .tabs button { padding: 8px 10px; white-space: nowrap; }
  h1 { font-size: 19px; }
}
