/* Trang quản lý CODE4LIFE. Cùng bộ màu, chữ với web chính (DESIGN.md), bố cục kiểu bảng điều khiển: menu trái, nội dung trên nền tint. */
:root {
  --bg: #fff; --tint: #f5f8fd; --side: #fbfcfe; --line: #e3e9f3; --line-2: #cdd7e6;
  --ink: #0b1533; --ink-2: #48546f; --ink-3: #5d6884;
  --blue: #0258d1; --blue-700: #0246ab; --blue-50: #eef4ff; --blue-100: #d9e6ff;
  --green: #047857; --green-50: #e6f5ee; --red: #b42318; --red-50: #fdecea;
  --r-sm: 8px; --r: 12px; --r-lg: 16px;
  --shadow: 0 1px 2px rgba(11, 21, 51, 0.04), 0 8px 24px -16px rgba(11, 21, 51, 0.18);
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--tint); color: var(--ink); font: 400 15px/1.55 "Be Vietnam Pro", system-ui, sans-serif; -webkit-font-smoothing: antialiased; caret-color: var(--blue); }
h1, h2 { margin: 0; letter-spacing: -0.02em; line-height: 1.25; }
p { margin: 0; }
img { display: block; }
.i { flex: none; }
.muted { color: var(--ink-3); }
.num, td.num, .v { font-variant-numeric: tabular-nums; }
::selection { background: var(--blue-100); color: var(--ink); }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; border-radius: 6px; }
* { scrollbar-color: var(--line-2) transparent; scrollbar-width: thin; }
[hidden] { display: none !important; }

/* Màn hình nào đang hiện */
body[data-view="boot"] .login-screen, body[data-view="boot"] .shell,
body[data-view="login"] .shell, body[data-view="app"] .login-screen { display: none; }

/* ---------- Nút ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 0 20px; border-radius: var(--r-sm); border: 1px solid var(--line-2); background: #fff; color: var(--ink); font: inherit; font-weight: 700; cursor: pointer; transition: background 0.15s, border-color 0.15s, transform 0.15s var(--ease); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #fff; box-shadow: 0 6px 14px -8px rgba(2, 88, 209, 0.7); }
.btn.primary:hover { background: var(--blue-700); border-color: var(--blue-700); }
.btn:disabled { cursor: progress; opacity: 0.85; }
.btn[data-busy]::before { content: ""; width: 16px; height: 16px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.4); border-top-color: #fff; animation: spin 0.7s linear infinite; }
.btn[data-busy] span::after { content: "…"; }
.icon-btn { display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: var(--r-sm); border: 1px solid var(--line); background: #fff; color: var(--ink-2); cursor: pointer; transition: background 0.15s, color 0.15s, border-color 0.15s; }
.icon-btn:hover { color: var(--ink); border-color: var(--line-2); background: var(--tint); }
.icon-btn[data-busy] .i { animation: spin 0.8s linear infinite; }
@keyframes spin { to { rotate: 360deg; } }

/* ---------- Đăng nhập ---------- */
.login-screen { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login { width: min(100%, 400px); background: #fff; border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow); padding: 32px 28px 28px; display: grid; gap: 10px; }
.login-logo { width: 150px; height: auto; margin-bottom: 14px; }
.login h1 { font-size: 22px; font-weight: 800; }
.login .muted { font-size: 14.5px; margin-bottom: 10px; }
.login label { font-size: 14px; font-weight: 600; }
.field { position: relative; }
.field input { width: 100%; height: 46px; padding: 0 48px 0 14px; border: 1px solid var(--line-2); border-radius: var(--r-sm); font: inherit; font-size: 16px; color: var(--ink); background: #fff; transition: border-color 0.15s, box-shadow 0.15s; }
.field input:hover { border-color: #b4c1d6; }
.field input:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-100); }
.field-btn { position: absolute; right: 4px; top: 4px; width: 38px; height: 38px; display: grid; place-items: center; border: 0; border-radius: 6px; background: none; color: var(--ink-3); cursor: pointer; }
.field-btn:hover { color: var(--ink); background: var(--tint); }
.form-error { color: var(--red); font-size: 14px; font-weight: 500; }
.form-error:empty { display: none; }
.login .btn { margin-top: 6px; }

/* ---------- Khung ---------- */
.shell { display: grid; grid-template-columns: 252px minmax(0, 1fr); min-height: 100dvh; }
.mobile-bar { display: none; }
.side { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 6px; padding: 20px 14px 16px; background: var(--side); border-right: 1px solid var(--line); overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 20px; }
.brand img { width: 118px; height: auto; mix-blend-mode: multiply; }
.brand span { font-size: 12.5px; font-weight: 700; color: var(--ink-3); padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; background: #fff; }
.nav, .links { display: grid; gap: 2px; }
.nav a, .links a, .logout { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 10px; border-radius: var(--r-sm); color: var(--ink-2); font-weight: 600; font-size: 14.5px; text-decoration: none; transition: background 0.15s, color 0.15s; }
.nav a:hover, .links a:hover, .logout:hover { background: #eef2f8; color: var(--ink); }
.nav a[aria-current] { background: var(--blue-50); color: var(--blue); }
.links { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.links a { font-weight: 500; }
.links a .i:last-child { margin-left: auto; color: var(--ink-3); opacity: 0.7; }
.logout { margin-top: auto; border: 0; background: none; font: inherit; font-weight: 600; font-size: 14.5px; cursor: pointer; width: 100%; text-align: left; }

.main { min-width: 0; padding: 28px clamp(16px, 3vw, 40px) 56px; }
.topbar { max-width: 1180px; margin: 0 auto 24px; display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 24px; }
.topbar h1 { font-size: 26px; font-weight: 800; letter-spacing: -0.025em; }
.topbar .muted { font-size: 14.5px; margin-top: 4px; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.status { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 14px; border-radius: 999px; background: #fff; border: 1px solid var(--line); font-size: 13.5px; font-weight: 500; color: var(--ink-2); white-space: nowrap; }
.status i { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); }
.status[data-state="ok"] i { background: #10a36a; }
.status[data-tick] i { animation: pulse 1s var(--ease); } /* nhấp một lần mỗi khi vừa có số mới (app.js) */
.status[data-state="error"] { color: var(--red); border-color: #f3c5c0; background: var(--red-50); }
.status[data-state="error"] i { background: var(--red); }
@keyframes pulse { from { box-shadow: 0 0 0 0 rgba(16, 163, 106, 0.5); } to { box-shadow: 0 0 0 7px rgba(16, 163, 106, 0); } }
.view { max-width: 1180px; margin: 0 auto; display: grid; gap: 20px; }

/* ---------- Chỉ số ---------- */
.kpis { margin: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); background: #fff; border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow); }
.kpi { padding: 20px 24px 22px; min-width: 0; }
.kpi + .kpi { border-left: 1px solid var(--line); }
.kpi dt { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; color: var(--ink-2); }
.kpi dd { margin: 10px 0 0; display: grid; gap: 4px; }
.kpi .v { font-size: 34px; font-weight: 800; letter-spacing: -0.03em; line-height: 1.1; min-height: 1.1em; }
.kpi .sub { font-size: 13.5px; color: var(--ink-3); min-height: 1.4em; }
.live { width: 8px; height: 8px; border-radius: 50%; background: #10a36a; }

/* Khung chờ trước lần tải đầu */
.view:not([data-ready]) .v, .view:not([data-ready]) .sub:empty, .view:not([data-ready]) .panel-head .muted { color: transparent; border-radius: 6px; background: linear-gradient(90deg, #eef2f8 25%, #f6f8fc 50%, #eef2f8 75%) 0 0 / 200% 100%; animation: shimmer 1.4s linear infinite; }
.view:not([data-ready]) .v { width: 64%; }
.view:not([data-ready]) .sub:empty, .view:not([data-ready]) .panel-head .muted { width: 80%; min-height: 1em; }
.view:not([data-ready]) .chart { opacity: 0.4; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Khối nội dung ---------- */
.panel { background: #fff; border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow); padding: 20px 24px 24px; min-width: 0; }
.panel-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px 20px; margin-bottom: 18px; }
.panel-head h2 { font-size: 17px; font-weight: 700; }
.panel-head .muted { font-size: 14px; margin-top: 4px; }

.seg { display: inline-flex; padding: 3px; background: var(--tint); border: 1px solid var(--line); border-radius: 10px; }
.seg button { min-height: 34px; padding: 0 14px; border: 0; border-radius: 7px; background: none; font: inherit; font-size: 13.5px; font-weight: 600; color: var(--ink-2); cursor: pointer; transition: background 0.15s, color 0.15s, box-shadow 0.15s; }
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(11, 21, 51, 0.08), 0 0 0 1px var(--line); }

/* ---------- Biểu đồ ---------- */
.chart { display: grid; grid-template-columns: 44px minmax(0, 1fr); grid-template-rows: 260px auto; transition: opacity 0.2s; }
.yaxis { position: relative; }
.yaxis span { position: absolute; right: 10px; translate: 0 50%; font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.plot { position: relative; touch-action: pan-y; cursor: crosshair; border-radius: 4px; }
.plot:focus-visible { outline-offset: 4px; }
.grid i { position: absolute; left: 0; right: 0; height: 0; border-top: 1px solid #edf1f7; }
.grid i:first-child { border-top-color: var(--line-2); }
.plot svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.plot .area { fill: rgba(2, 88, 209, 0.08); }
.plot .line { fill: none; stroke: var(--blue); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.cross { position: absolute; top: 0; bottom: 0; width: 0; border-left: 1px dashed #9fb0ca; pointer-events: none; }
.cross i { position: absolute; left: 0; width: 10px; height: 10px; translate: -50% 50%; border-radius: 50%; background: var(--blue); box-shadow: 0 0 0 2px #fff, 0 1px 4px rgba(11, 21, 51, 0.3); }
.tip { position: absolute; top: 6px; translate: -50% 0; display: grid; gap: 1px; padding: 7px 11px; background: var(--ink); color: #fff; border-radius: 8px; font-size: 12.5px; white-space: nowrap; pointer-events: none; box-shadow: 0 8px 20px -8px rgba(11, 21, 51, 0.5); }
.tip[data-edge="start"] { translate: 8px 0; }
.tip[data-edge="end"] { translate: calc(-100% - 8px) 0; }
.tip-d { color: #c3cde6; }
.tip-v { font-size: 14px; font-variant-numeric: tabular-nums; }
.empty { position: absolute; left: 50%; top: 50%; translate: -50% -50%; width: max-content; max-width: 90%; padding: 10px 16px; background: #fff; border: 1px solid var(--line); border-radius: var(--r-sm); text-align: center; color: var(--ink-2); font-size: 14.5px; }
.xaxis { grid-column: 2; position: relative; height: 28px; }
.xaxis span { position: absolute; top: 8px; translate: -50% 0; font-size: 12px; color: var(--ink-3); white-space: nowrap; font-variant-numeric: tabular-nums; }
.xaxis span[data-edge="start"] { translate: 0 0; }
.xaxis span[data-edge="end"] { translate: -100% 0; }

/* ---------- Bảng ---------- */
.table-wrap { max-height: 420px; overflow: auto; margin: 0 -24px -24px; border-top: 1px solid var(--line); border-radius: 0 0 var(--r-lg) var(--r-lg); }
table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
th { position: sticky; top: 0; background: #fafbfd; text-align: left; font-size: 13px; font-weight: 600; color: var(--ink-3); padding: 10px 24px; border-bottom: 1px solid var(--line); }
td { padding: 11px 24px; border-bottom: 1px solid #eef2f7; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fafbfd; }
.r { text-align: right; }
.day .d { white-space: nowrap; }
.day .wd { margin-left: 10px; color: var(--ink-3); font-size: 13.5px; }
.delta { color: var(--ink-3); }
.delta .i { vertical-align: -2px; }
.delta[data-dir="up"] { color: var(--green); }
.delta[data-dir="down"] { color: var(--red); }

/* ---------- Màn hẹp ---------- */
@media (max-width: 1080px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi:nth-child(3) { border-left: 0; }
  .kpi:nth-child(n + 3) { border-top: 1px solid var(--line); }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .mobile-bar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; height: 60px; padding: 0 16px; background: rgba(255, 255, 255, 0.96); border-bottom: 1px solid var(--line); }
  .mobile-bar img { width: 116px; height: auto; mix-blend-mode: multiply; }
  .side { display: none; position: fixed; z-index: 19; top: 60px; left: 0; right: 0; height: auto; max-height: calc(100dvh - 60px); padding: 12px 12px 16px; border-right: 0; border-bottom: 1px solid var(--line); box-shadow: 0 18px 30px -18px rgba(11, 21, 51, 0.35); }
  .side.open { display: flex; }
  .brand { display: none; }
  .logout { margin-top: 10px; border-top: 1px solid var(--line); border-radius: 0; padding-top: 10px; min-height: 48px; }
  .nav a, .links a { min-height: 46px; }
  .main { padding-top: 20px; }
  .topbar { align-items: flex-start; }
  .topbar h1 { font-size: 22px; }
}
@media (max-width: 560px) {
  .kpi { padding: 16px; }
  .kpi .v { font-size: 28px; }
  .panel { padding: 16px; }
  .table-wrap { margin: 0 -16px -16px; }
  th, td { padding-inline: 16px; }
  .chart { grid-template-columns: 36px minmax(0, 1fr); grid-template-rows: 200px auto; }
  .top-actions { width: 100%; }
  .status { flex: 1; }
  .seg { width: 100%; }
  .seg button { flex: 1; }
  .day .wd { display: block; margin: 1px 0 0; font-size: 12.5px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
