:root {
  --bg: #EEF2F1; --surface: #FFFFFF; --ink: #15302B; --muted: #5E716C; --line: #D5DEDB;
  --on: #F2B33D; --on-ink: #3A2600; --on-soft: #FCEFD2; --bad: #C2412D; --ok: #2F7D5B;
  --r-tile: 18px; --r-ctl: 12px;
  --font: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1A18; --surface: #172623; --ink: #E6EFEC; --muted: #8FA39E; --line: #2A3B37;
    --on: #F5BE55; --on-ink: #2B1C00; --on-soft: #3A3018; --bad: #E86A55; --ok: #5DBB8E;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 400 16px/1.5 var(--font); }
button, input, select { font: inherit; color: inherit; }
a { color: inherit; }
:focus-visible { outline: 3px solid var(--on); outline-offset: 2px; }
.wrap { max-width: 640px; margin: 0 auto; padding: max(16px, env(safe-area-inset-top)) 16px calc(32px + env(safe-area-inset-bottom)); }

/* ---- đầu trang ---- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: -0.01em; display: flex; align-items: center; gap: 10px; }
.brand img { width: 30px; height: 30px; }
.iconbtn { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; font-size: 14px; }

/* ---- chọn thiết bị ---- */
.devs { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px 20px; padding: 2px 16px 6px; scrollbar-width: none; }
.devs::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 7px 14px; cursor: pointer; font-size: 14px; display: flex; align-items: center; gap: 7px; white-space: nowrap; }
.chip[aria-current="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { background: var(--ok); }

/* ---- thiết bị ---- */
.dev-head h1 { font-size: 30px; line-height: 1.15; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
.status { color: var(--muted); font-size: 14px; margin: 6px 0 0; display: flex; align-items: center; gap: 8px; }
.status.off { color: var(--bad); }
.warn { background: var(--on-soft); border-radius: var(--r-ctl); padding: 12px 14px; font-size: 14px; margin-top: 14px; }

h2 { font-size: 15px; font-weight: 600; color: var(--muted); margin: 30px 0 10px; }

/* số đo cảm biến: dòng chữ số lớn, không đóng khung */
.readings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px 0; margin-top: 22px; }
.rd { padding: 0 14px; border-left: 1px solid var(--line); min-width: 0; }
.rd:nth-child(3n+1) { padding-left: 0; border-left: 0; }
.rd b { display: block; font-size: 24px; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.rd b small { font-size: 15px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.rd span { font-size: 13px; color: var(--muted); }
.rd.hot b { color: var(--bad); }
.rd.lit b { color: var(--ink); }
.rd.lit b::before { content: ""; display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--on); margin-right: 7px; vertical-align: 4px; box-shadow: 0 0 0 4px var(--on-soft); }

/* ---- điều hòa ---- */
.ac { background: var(--surface); border-radius: 24px; padding: 18px; border: 1px solid var(--line); }
.ac-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ac-temp { display: flex; align-items: center; gap: 6px; margin: 14px 0 16px; justify-content: center; }
.ac-temp output { font-size: 64px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.04em; min-width: 3.1ch; text-align: center; }
.ac-temp output sup { font-size: 26px; font-weight: 500; vertical-align: 28px; margin-left: 2px; color: var(--muted); }
.ac.off .ac-temp output { color: var(--muted); }
.step { width: 52px; height: 52px; border-radius: 50%; border: 1px solid var(--line); background: var(--bg); font-size: 26px; line-height: 1; cursor: pointer; }
.seg { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; background: var(--bg); padding: 4px; border-radius: var(--r-ctl); }
.seg button { border: 0; background: transparent; border-radius: 9px; padding: 8px 2px; font-size: 13px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface); box-shadow: 0 1px 2px rgba(21,48,43,.18); font-weight: 600; }
.ac-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; font-size: 14px; color: var(--muted); }
.ac-meta select { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 5px 8px; font-size: 14px; }

/* công tắc nguồn nhỏ (điều hòa, LED) */
.toggle { position: relative; width: 56px; height: 32px; border-radius: 999px; border: 0; background: var(--line); cursor: pointer; flex: none; transition: background .15s; }
.toggle::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s; }
.toggle[aria-checked="true"] { background: var(--on); }
.toggle[aria-checked="true"]::after { transform: translateX(24px); }

/* ---- nút bật/tắt: điểm nhấn của trang – bật thì sáng màu hổ phách như đèn báo trên công tắc ---- */
.switches { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.sw { position: relative; text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: var(--r-tile); padding: 14px 14px 13px; min-height: 104px; cursor: pointer; display: flex; flex-direction: column; justify-content: space-between; gap: 10px; transition: background .15s, border-color .15s; }
.sw .lamp { width: 26px; height: 6px; border-radius: 3px; background: var(--line); transition: background .15s, box-shadow .15s; }
.sw .nm { font-weight: 600; font-size: 16px; line-height: 1.25; overflow-wrap: anywhere; }
.sw .kd { font-size: 12px; color: var(--muted); }
.sw[aria-checked="true"] { background: var(--on-soft); border-color: var(--on); }
.sw[aria-checked="true"] .lamp { background: var(--on); box-shadow: 0 0 10px 1px var(--on); }
.sw[aria-checked="true"] .kd { color: var(--on-ink); }
@media (prefers-color-scheme: dark) { .sw[aria-checked="true"] .kd { color: var(--on); } }
.sw.wait .lamp { animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.sw:disabled, .toggle:disabled, .step:disabled, .seg button:disabled { cursor: not-allowed; opacity: .55; }

.links { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 34px; }

/* ---- nút, ô nhập ---- */
.btn { border: 1px solid var(--ink); background: var(--ink); color: var(--bg); border-radius: var(--r-ctl); padding: 11px 18px; font-weight: 600; cursor: pointer; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); font-weight: 500; }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--line); font-weight: 500; }
.btn.small { padding: 7px 12px; font-size: 14px; }
.btn:disabled { opacity: .6; cursor: wait; }
label.f { display: block; margin: 0 0 14px; font-size: 14px; color: var(--muted); }
label.f input, label.f select { display: block; width: 100%; margin-top: 6px; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--surface); color: var(--ink); font-size: 16px; }
.err { color: var(--bad); font-size: 14px; min-height: 1.5em; margin: 4px 0 10px; }
.hint { color: var(--muted); font-size: 14px; }
.mono { font-variant-numeric: tabular-nums; letter-spacing: .04em; }

/* ---- đăng nhập ---- */
.auth { max-width: 400px; margin: 0 auto; padding: 12vh 20px 40px; }
.auth h1 { font-size: 34px; letter-spacing: -0.03em; line-height: 1.1; margin: 22px 0 8px; }
.auth p.lead { color: var(--muted); margin: 0 0 28px; }
.auth .switch-mode { margin-top: 18px; font-size: 15px; color: var(--muted); }
.auth .switch-mode button { border: 0; background: none; color: var(--ink); font-weight: 600; text-decoration: underline; cursor: pointer; padding: 0; }

/* ---- trống ---- */
.empty { padding: 40px 0; }
.empty h1 { font-size: 26px; letter-spacing: -0.02em; margin: 0 0 8px; }
.empty p { color: var(--muted); margin: 0 0 22px; max-width: 46ch; }

/* ---- bảng trượt (dialog) ---- */
dialog { border: 0; padding: 0; background: var(--surface); color: var(--ink); width: min(560px, 100%); max-height: 88vh; border-radius: 24px 24px 0 0; margin: auto auto 0; }
@media (min-width: 640px) { dialog { border-radius: 24px; margin: auto; } }
dialog::backdrop { background: rgba(10, 22, 20, .45); }
dialog[open] { animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { dialog[open], .sw.wait .lamp { animation: none; } .toggle, .toggle::after, .sw, .sw .lamp { transition: none; } }
.sheet { padding: 22px 20px calc(24px + env(safe-area-inset-bottom)); }
.sheet h3 { font-size: 21px; margin: 0 0 16px; letter-spacing: -0.01em; }
.sheet h4 { font-size: 15px; margin: 24px 0 8px; }
.sheet .row { display: flex; gap: 10px; align-items: flex-end; }
.sheet .row label.f { flex: 1; margin: 0; }
.close { float: right; border: 0; background: var(--bg); width: 36px; height: 36px; border-radius: 50%; font-size: 20px; cursor: pointer; margin: -6px -6px 0 0; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
ul.plain li:last-child { border-bottom: 0; }
ul.plain small { color: var(--muted); display: block; }
.log li { display: block !important; }
.log time { color: var(--muted); font-size: 13px; display: block; font-variant-numeric: tabular-nums; }

/* ---- quản trị ---- */
.readings.stats { grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 10px 0 24px; }
.readings.stats .rd { border-left: 1px solid var(--line); padding-left: 14px; }
.readings.stats .rd:first-child { border-left: 0; padding-left: 0; }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th { text-align: left; color: var(--muted); font-weight: 500; padding: 6px 8px 6px 0; border-bottom: 1px solid var(--line); }
table.t td { padding: 9px 8px 9px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.tscroll { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
textarea { width: 100%; min-height: 90px; padding: 10px; border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--surface); color: var(--ink); font: 15px/1.4 var(--font); }

#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 999px; font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center; z-index: 10; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.wide { width: 100%; }
.mt0 { margin-top: 0; } .mt8 { margin-top: 8px; } .mt10 { margin-top: 10px; } .mt14 { margin-top: 14px; } .mt20 { margin-top: 20px; } .mt26 { margin-top: 26px; }
table.t th { white-space: nowrap; }
table.t td .btn { margin: 0 4px 4px 0; white-space: nowrap; }
a.iconbtn, a.btn { text-decoration: none; display: inline-block; }

.ac-opts { display: grid; gap: 4px; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.opt { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; font-size: 15px; }
.opt.wait .toggle { animation: pulse 0.9s ease-in-out infinite; }
.toggle.sm { transform: scale(.82); transform-origin: right center; }
details.sys { margin-top: 26px; border: 1px solid var(--line); border-radius: var(--r-tile); padding: 4px 14px; background: var(--surface); }
details.sys summary { cursor: pointer; font-weight: 600; padding: 10px 0; }
.sys-btns { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
