:root {
  --bg: #edf1f5;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --ink: #16283b;
  --muted: #5b6b7d;
  --line: #d3dbe4;
  --brand: #0f5ea8;
  --brand-ink: #ffffff;
  --ok: #1e7a55;
  --warn: #a86b0c;
  --danger: #b83227;
  --track: #e3e9ef;
  --focus: #0f5ea8;
  --radius-s: 4px;
  --radius-m: 8px;
  --shadow: 0 1px 2px rgba(22, 40, 59, 0.06), 0 8px 24px rgba(22, 40, 59, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1822;
    --surface: #16212e;
    --surface-2: #1b2837;
    --ink: #e4ebf3;
    --muted: #93a3b5;
    --line: #26364a;
    --brand: #5aa2e8;
    --brand-ink: #0b1520;
    --ok: #4cc38a;
    --warn: #e3a63b;
    --danger: #f06a5e;
    --track: #23324a;
    --focus: #8fc2f5;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.55 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}

a { color: var(--brand); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.75rem; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; font-weight: 600; }
h3 { font-size: 0.95rem; font-weight: 600; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.num { text-align: right; white-space: nowrap; }

/* ---------- Thanh trên cùng ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; text-decoration: none; color: var(--ink); }
.brand-mark { width: 22px; height: 22px; border-radius: 4px; background: var(--brand); position: relative; overflow: hidden; }
.brand-mark::after { content: ''; position: absolute; left: 0; bottom: 0; width: 100%; height: 38%; background: var(--brand-ink); opacity: 0.35; }
.topbar .spacer { flex: 1; }
.who { font-size: 0.85rem; color: var(--muted); }

main { max-width: 1180px; margin: 0 auto; padding: 28px 24px 64px; }

/* ---------- Nút & ô nhập ---------- */
button, .btn {
  font: inherit;
  font-weight: 500;
  font-size: 0.9rem;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-s);
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.55; cursor: progress; }
button.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
button.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
button.link { border: 0; background: none; padding: 4px 6px; color: var(--brand); }
button.link.danger { color: var(--danger); }

label.field { display: grid; gap: 4px; align-content: start; font-size: 0.85rem; color: var(--muted); }
input, select, textarea {
  font: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  padding: 8px 10px;
  width: 100%;
  min-width: 0;
}
input:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 22%, transparent); }
input[type='checkbox'] { width: auto; }
.hint { font-size: 0.8rem; color: var(--muted); }

/* ---------- Thước đo ngưỡng (điểm nhấn) ---------- */
.meter { --pct: 0; --c: var(--ok); position: relative; }
.meter-track {
  position: relative;
  height: 10px;
  background: var(--track);
  border-radius: 2px;
  overflow: visible;
}
.meter-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: calc(var(--pct) * 1%);
  background: var(--c);
  border-radius: 2px;
  transition: width 0.5s ease;
}
.meter-tick {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 2px;
  background: var(--ink);
  opacity: 0.35;
}
.meter-tick.t20 { left: 20%; }
.meter-tick.t10 { left: 10%; }
.meter.l1 { --c: var(--ok); }
.meter.l2 { --c: var(--warn); }
.meter.l3 { --c: var(--danger); }
.meter.none .meter-fill { background: repeating-linear-gradient(135deg, var(--line) 0 6px, transparent 6px 12px); width: 100%; }

.meter.big .meter-track { height: 22px; border-radius: 3px; }
.meter.big .meter-tick { top: -8px; bottom: -8px; }
.meter-scale { position: relative; height: 20px; font-size: 0.75rem; color: var(--muted); margin-top: 8px; }
.meter-scale span { position: absolute; transform: translateX(-50%); }
.meter-scale span:first-child { transform: none; }
.meter-scale span:last-child { transform: translateX(-100%); }

@media (prefers-reduced-motion: reduce) { .meter-fill { transition: none; } }

/* ---------- Trạng thái ---------- */
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 0.85rem; font-weight: 500; }
.status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status.l1 { color: var(--ok); }
.status.l2 { color: var(--warn); }
.status.l3 { color: var(--danger); }
.status.off, .status.none { color: var(--muted); }

/* ---------- Trang danh sách ---------- */
.list-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.list-head p { margin: 6px 0 0; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type='search'] { max-width: 260px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-s); overflow: hidden; }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed='true'] { background: var(--ink); color: var(--surface); }

.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 0.8rem; font-weight: 500; color: var(--muted); background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
tbody tr.row { cursor: pointer; }
tbody tr.row:hover { background: var(--surface-2); }
td .name { font-weight: 600; }
td .id { font-size: 0.8rem; color: var(--muted); }
td.meter-cell { min-width: 220px; }
td.meter-cell .meter-line { display: flex; justify-content: space-between; font-size: 0.8rem; margin-top: 6px; color: var(--muted); }

.empty { padding: 48px 24px; text-align: center; }
.empty p { max-width: 46ch; margin: 8px auto 20px; }

/* ---------- Trang chi tiết ---------- */
.back { display: inline-block; margin-bottom: 16px; font-size: 0.9rem; text-decoration: none; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.detail-head .sub { margin-top: 6px; color: var(--muted); font-size: 0.9rem; }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.gauge {
  margin: 24px 0 28px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow);
}
.gauge-top { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.gauge-remaining { font-size: 2.4rem; font-weight: 600; letter-spacing: -0.02em; line-height: 1.1; }
.gauge-remaining.l2 { color: var(--warn); }
.gauge-remaining.l3 { color: var(--danger); }
.gauge-caption { color: var(--muted); font-size: 0.9rem; margin-top: 4px; }
.gauge-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 16px 24px; margin-top: 24px; padding-top: 18px; border-top: 1px solid var(--line); }
.gauge-facts dt { font-size: 0.8rem; color: var(--muted); }
.gauge-facts dd { margin: 2px 0 0; font-weight: 600; }

.notice { padding: 10px 14px; border-radius: var(--radius-s); font-size: 0.9rem; margin-top: 16px; border-left: 3px solid var(--warn); background: color-mix(in srgb, var(--warn) 10%, var(--surface)); }
.notice.error { border-left-color: var(--danger); background: color-mix(in srgb, var(--danger) 9%, var(--surface)); }

.cols { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 24px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 20px; margin-bottom: 24px; }
.panel > header { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.panel table th, .panel table td { padding: 9px 8px; }
.panel table th:first-child, .panel table td:first-child { padding-left: 0; }
.panel table th:last-child, .panel table td:last-child { padding-right: 0; }
.panel table th { background: none; }

.inline-form { display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 10px; align-items: end; margin-bottom: 8px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 0.85rem; color: var(--muted); margin: 6px 0 16px; }

.iam { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.iam:last-of-type { border-bottom: 0; }
.iam .label { font-weight: 600; }
.iam .key { font-size: 0.85rem; color: var(--muted); }
.iam .actions { grid-row: 1 / span 2; grid-column: 2; display: flex; gap: 2px; align-items: center; }
.iam .result { grid-column: 1 / -1; font-size: 0.85rem; }
.iam .result.ok { color: var(--ok); }
.iam .result.err { color: var(--danger); }

.settings { display: grid; gap: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: start; }
.danger-zone { border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }

.tag { font-size: 0.75rem; padding: 1px 6px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.pos { color: var(--ok); }
.neg { color: var(--danger); }

/* ---------- Dialog ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius-m);
  padding: 0;
  width: min(640px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 20, 32, 0.45); }
dialog form { padding: 24px; display: grid; gap: 14px; }
dialog h2 { margin-bottom: 4px; }
dialog .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.iam-rows { display: grid; gap: 10px; }
.iam-row { display: grid; grid-template-columns: 1fr 1.3fr 1.6fr auto; gap: 8px; align-items: end; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius-s); padding: 12px 14px 14px; margin: 0; }
legend { font-size: 0.85rem; color: var(--muted); padding: 0 6px; }

/* ---------- Thông báo nhanh ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: var(--radius-s);
  font-size: 0.9rem;
  max-width: calc(100vw - 32px);
  z-index: 50;
}
.toast.err { background: var(--danger); color: #fff; }

.loading { padding: 64px 0; text-align: center; color: var(--muted); }
.fatal { max-width: 560px; margin: 64px auto; }

@media (max-width: 860px) {
  .cols { grid-template-columns: 1fr; }
  main { padding: 20px 16px 48px; }
  .topbar { padding: 10px 16px; }
  .inline-form { grid-template-columns: 1fr 1fr; }
  .inline-form button { grid-column: 1 / -1; }
  .iam-row { grid-template-columns: 1fr 1fr; }
  .iam-row .wide { grid-column: 1 / -1; }
  .row2 { grid-template-columns: 1fr; }
  .gauge-remaining { font-size: 2rem; }
  .hide-sm { display: none; }
  .meter-scale span:first-child { display: none; }
}
