:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #16202a;
  --muted: #5d6b79;
  --line: #dfe5ea;
  --accent: #0b7a75;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --ok: #1a7f3c;
  --warn: #9a6700;
  --code-bg: #eef2f5;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1a2128;
    --text: #e6ecf1;
    --muted: #93a1ae;
    --line: #2b353e;
    --accent: #35b8b0;
    --accent-text: #08201f;
    --danger: #ff8a80;
    --ok: #5fd08a;
    --warn: #e3b341;
    --code-bg: #232c35;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); line-height: 1.5; font-size: 15px; }
[hidden] { display: none !important; }
h1, h2 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.1rem; }
p { margin: .4rem 0; }
code { background: var(--code-bg); padding: .08em .35em; border-radius: 4px; font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); min-height: 1.2em; white-space: pre-line; }
.grow { flex: 1; }

/* Bố cục */
.topbar { display: flex; justify-content: space-between; align-items: center; padding: .7rem 1rem; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: .5rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--ok); }
.dot.err { background: var(--danger); }
.tabs { display: flex; gap: .25rem; padding: .5rem 1rem 0; overflow-x: auto; background: var(--surface); border-bottom: 1px solid var(--line); }
.tabs button { background: none; border: 0; border-bottom: 3px solid transparent; padding: .6rem .8rem; color: var(--muted); font: inherit; cursor: pointer; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.content { max-width: 880px; margin: 0 auto; padding: 1rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.row { display: flex; align-items: center; gap: .6rem; margin-top: .7rem; }
.row.spread { justify-content: space-between; margin-top: 0; }
.row.wrap { flex-wrap: wrap; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 .45em; font-size: .75rem; margin-left: .2rem; }

/* Form */
label { display: block; margin: .7rem 0 0; font-weight: 600; font-size: .9rem; }
input, select, textarea { display: block; width: 100%; margin-top: .3rem; padding: .55rem .65rem; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 8px; font: inherit; }
textarea { min-height: 5.5rem; resize: vertical; font-family: inherit; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button { font: inherit; padding: .5rem .9rem; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.ghost { background: transparent; }
.hint { color: var(--muted); font-size: .88rem; padding-left: 1.1rem; margin-bottom: 0; }

/* Công tắc */
.switch { display: inline-flex; align-items: center; gap: .6rem; cursor: pointer; margin: 0; font-weight: 600; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; margin: 0; }
.track { width: 46px; height: 26px; border-radius: 999px; background: var(--line); position: relative; transition: background .15s; flex: none; }
.track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .track { opacity: .5; }

/* Trạng thái */
.state { display: flex; align-items: center; gap: .5rem; font-weight: 600; font-size: 1.05rem; }
.pill { font-size: .8rem; padding: .1rem .55rem; border-radius: 999px; border: 1px solid var(--line); font-weight: 600; }
.pill.ok { color: var(--ok); border-color: var(--ok); }
.pill.err { color: var(--danger); border-color: var(--danger); }
.pill.off { color: var(--muted); }

/* Danh sách lệnh */
.cmd { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem .9rem; margin: .8rem 0; background: var(--bg); }
.cmd.off { opacity: .7; }
.cmd-head { display: flex; align-items: center; gap: .6rem; }
.cmd-name { display: flex; align-items: center; flex: 1; gap: .2rem; }
.cmd-name span { font-weight: 700; color: var(--muted); }
.cmd-name input { margin: 0; font-family: ui-monospace, Menlo, Consolas, monospace; }
.cmd-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .7rem; }
.checks { display: flex; gap: 1.2rem; flex-wrap: wrap; margin-top: .6rem; }
.check { display: inline-flex; align-items: center; gap: .4rem; font-weight: 400; margin: 0; }
.check input { width: auto; margin: 0; }
details.vars { margin: .6rem 0; font-size: .9rem; }
details.vars summary { cursor: pointer; color: var(--accent); }
details.vars li { margin: .35rem 0; }

/* Danh sách chat */
.item { display: flex; align-items: center; justify-content: space-between; gap: .8rem; padding: .6rem 0; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item .meta { min-width: 0; }
.item .meta strong, .item .meta span { overflow-wrap: anywhere; }
.item .actions { display: flex; gap: .4rem; flex: none; }
.empty { color: var(--muted); padding: .4rem 0; }

/* Nhật ký */
.logs { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .82rem; max-height: 60vh; overflow: auto; }
.logline { padding: .15rem 0; border-top: 1px solid var(--line); overflow-wrap: anywhere; }
.logline:first-child { border-top: 0; }
.logline .lv-warn { color: var(--warn); }
.logline .lv-error { color: var(--danger); }

/* Thông báo nổi */
#toast { position: fixed; left: 50%; bottom: 1.2rem; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: 90vw; white-space: pre-line; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Đăng nhập */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 380px; }
.login-card button { width: 100%; margin-top: .6rem; }

@media (max-width: 640px) {
  .cmd-grid { grid-template-columns: 1fr; }
  .item { flex-direction: column; align-items: stretch; }
  .item .actions { justify-content: flex-end; }
}

/* Thiết bị và phân quyền */
.code { display: block; background: var(--code-bg); border-radius: 8px; padding: .5rem .7rem; margin: .35rem 0; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; overflow-x: auto; white-space: pre; user-select: all; }
.guide ol { padding-left: 1.2rem; }
.guide li { margin: .6rem 0; }
.guide .note { color: var(--muted); font-size: .88rem; }
.chat-wrap { border-top: 1px solid var(--line); }
.chat-wrap:first-child { border-top: 0; }
.chat-wrap .item { border-top: 0; }
.access { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .9rem; margin: 0 0 .7rem; }
.access .check { display: flex; margin: .35rem 0; }
.access .devlist { max-height: 14rem; overflow: auto; margin: .3rem 0 .6rem 1.4rem; }
.access .check small { color: var(--muted); }
.rename { display: flex; gap: .4rem; align-items: center; }
.rename input { margin: 0; }
