:root {
  color-scheme: light;
  --page: #f4f6fa; --surface: #ffffff; --raised: #f7f8fb;
  --text: #191919; --muted: #666666; --grid: #e7e7e7; --axis: #c2c2c2;
  --border: rgba(11, 11, 11, .10); --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .04);
  --series-1: #257de0; --series-2: #f47230;
  --good: #0aac23; --warning: #fbb315; --serious: #f18a59; --critical: #dc3c3d;
  --accent: #257de0;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0b0b0b; --surface: #191919; --raised: #212121;
    --text: #efefef; --muted: #9c9c9c; --grid: #303030; --axis: #474747;
    --border: rgba(255, 255, 255, .10); --shadow: none;
    --series-1: #338ced; --series-2: #e76624; --accent: #338ced;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0b0b0b; --surface: #191919; --raised: #212121;
  --text: #efefef; --muted: #9c9c9c; --grid: #303030; --axis: #474747;
  --border: rgba(255, 255, 255, .10); --shadow: none;
  --series-1: #338ced; --series-2: #e76624; --accent: #338ced;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--text); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 15px; font-weight: 650; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.error { color: var(--critical); min-height: 21px; }
svg.i { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 9px; padding: 8px 14px; font-weight: 550; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn:hover { background: var(--raised); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: var(--critical); border-color: var(--critical); color: #fff; }
.btn.wide { width: 100%; padding: 12px; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 9px; width: 36px; height: 36px; display: grid; place-items: center; padding: 0; }

.logo { width: 52px; height: 52px; border-radius: 14px; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 13h4l2-7 5 13 2-6h5' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/70% no-repeat; }
.logo.small { width: 30px; height: 30px; border-radius: 8px; }

/* Đăng nhập */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 18px; padding: 28px 24px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
#pin { width: 100%; text-align: center; font-size: 28px; letter-spacing: 12px; padding: 8px; background: var(--raised); border: 1px solid var(--border); border-radius: 12px; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; margin-bottom: 4px; }
.keypad button { height: 52px; border-radius: 12px; border: 1px solid var(--border); background: var(--raised); font-size: 20px; font-weight: 550; touch-action: manipulation; }
.keypad button:active { background: var(--grid); }

/* Khung */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 0 8px 18px; }
#nav { display: flex; flex-direction: column; gap: 4px; }
#nav button { display: flex; align-items: center; gap: 10px; border: 0; background: none; border-radius: 9px; padding: 10px 12px; color: var(--muted); font-weight: 550; text-align: left; }
#nav button:hover { background: var(--raised); }
#nav button[aria-current="page"] { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.badge { margin-left: auto; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--critical); color: #fff; font-size: 12px; text-align: center; }
.main { padding: 20px 24px 32px; max-width: 1400px; width: 100%; }
.top { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.pill { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 5px 12px; font-weight: 550; font-size: 13px; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--axis); }
.dot.good { background: var(--good); } .dot.warning { background: var(--warning); } .dot.critical { background: var(--critical); } .dot.info { background: var(--series-1); }

/* Thẻ */
#view { display: flex; flex-direction: column; gap: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); }
.grid.tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.tile .label { color: var(--muted); font-size: 13px; display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.tile .value { font-size: 28px; font-weight: 650; line-height: 1.25; margin: 4px 0; }
.tile .sub { color: var(--muted); font-size: 12px; }
.meter { height: 6px; border-radius: 4px; background: var(--grid); overflow: hidden; margin: 6px 0; }
.meter i { display: block; height: 100%; border-radius: 4px; background: var(--series-1); }
.meter i.warning { background: var(--warning); } .meter i.critical { background: var(--critical); }
.tag { font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 12px; font-size: 13px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }

/* Biểu đồ */
.chart { position: relative; width: 100%; height: 170px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-line { stroke: var(--axis); stroke-width: 1; }
.chart .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 3px; border-radius: 2px; }
.tip { position: fixed; z-index: 50; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 9px; box-shadow: 0 6px 20px rgba(0, 0, 0, .18); padding: 7px 10px; font-size: 12px; white-space: nowrap; }
.tip .row-line { display: flex; align-items: center; gap: 6px; }

/* Danh sách */
.list { display: flex; flex-direction: column; }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-top: 1px solid var(--border); }
.row:first-child { border-top: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .name { font-weight: 600; overflow-wrap: anywhere; }
.row .meta { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.row .end { text-align: right; color: var(--muted); font-size: 12px; white-space: nowrap; }
.alert-bar { width: 4px; align-self: stretch; border-radius: 3px; flex: none; }
.alert-bar.critical { background: var(--critical); } .alert-bar.warning { background: var(--warning); } .alert-bar.info { background: var(--series-1); } .alert-bar.resolved { background: var(--good); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; color: var(--muted); font-weight: 550; padding: 6px 8px 6px 0; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 7px 8px 7px 0; border-bottom: 1px solid var(--border); overflow-wrap: anywhere; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border); border-radius: 7px; padding: 2px 8px; font-size: 12px; font-variant-numeric: tabular-nums; }
.empty-note { color: var(--muted); padding: 8px 0; }

dialog { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 16px; padding: 22px; width: min(92vw, 400px); }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog p { margin: 8px 0 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast { position: fixed; left: 50%; bottom: 84px; transform: translate(-50%, 8px); opacity: 0; pointer-events: none; transition: .2s; background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 10px; max-width: 90vw; z-index: 60; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1100px) { .grid.tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

@media (max-width: 760px) {
  .shell { display: block; }
  .side { position: fixed; inset: auto 0 0 0; height: auto; z-index: 20; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--border); }
  .brand { display: none; }
  #nav { flex-direction: row; }
  #nav button { flex: 1; flex-direction: column; gap: 2px; padding: 7px 2px; font-size: 11px; position: relative; }
  .badge { position: absolute; top: 2px; left: calc(50% + 6px); }
  .main { padding: 14px 12px 90px; }
  h1 { font-size: 18px; }
  .grid { gap: 12px; }
  .grid.tiles { gap: 10px; }
  .card { padding: 13px; }
  .tile .value { font-size: 23px; }
  .btn { min-height: 40px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
