:root {
  --bg: #0E1116; --panel: #161A22; --card: #1D222C; --card-hi: #252B37;
  --border: #2B3240; --text: #E7EAF0; --muted: #8B94A7; --faint: #5A6375;
  --accent: #3B82F6; --ctrl: #EF4444; --recv: #F59E0B; --rest: #22C55E; --danger: #E11D48;
  --a0: #60A5FA; --a1: #34D399; --a2: #F472B6; --a3: #FBBF24;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
h1, h2, h3, h4 { margin: 0; }
h1 { font-size: 20px; } h2 { font-size: 16px; } h3 { font-size: 14px; color: var(--text); }
h4 { font-size: 11px; font-weight: 600; letter-spacing: .04em; color: var(--muted); margin: 14px 0 6px; }
p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
kbd { font: 11px Menlo, monospace; padding: 1px 5px; border-radius: 4px; background: rgba(255,255,255,.12); margin-left: 4px; }
code { font-family: Menlo, monospace; }

.top { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between;
  margin: 16px; padding: 14px 18px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; }
.conn, .ctrl { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.label { color: var(--muted); font-size: 12px; }

.btn { appearance: none; border: 1px solid var(--border); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 9px; font-weight: 600; font-size: 13px; cursor: pointer; touch-action: manipulation; }
.btn:hover { background: var(--card-hi); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.btn.danger { background: var(--danger); border-color: var(--danger); }
.btn.photo.flash { background: var(--rest); border-color: var(--rest); }
.btn.held { background: var(--recv); border-color: var(--recv); color: #111; }

.pill { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill.off { background: #2a2f3a; color: var(--muted); }
.pill.on { background: rgba(34,197,94,.15); color: var(--rest); }
.pill.busy { background: rgba(245,158,11,.15); color: var(--recv); }
.owner { padding: 7px 16px; border-radius: 9px; font-weight: 800; letter-spacing: .03em; }
.owner.human { background: var(--rest); color: #06280f; }
.owner.pc { background: var(--ctrl); color: #fff; }

.grid { display: grid; gap: 16px; margin: 0 16px 16px; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 16px 18px; min-width: 0; }
.card-head { display: flex; gap: 12px; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; }
.logcard { grid-column: 1 / -1; }

.joys { display: flex; justify-content: space-around; gap: 16px; flex-wrap: wrap; margin-top: 12px; }
.joy { text-align: center; }
.joy canvas { width: min(280px, 40vw); height: auto; aspect-ratio: 1; touch-action: none; cursor: grab; display: block; margin: 8px auto 4px; }
.joyval { font: 12px Menlo, monospace; color: var(--muted); }
.hint { margin-top: 10px; }

.meter-list { display: grid; gap: 6px; }
.meter { display: grid; grid-template-columns: 58px 36px minmax(0, 1fr) 72px 64px; gap: 8px; align-items: center; }
.meter .name { font-weight: 700; font-size: 13px; }
.meter .pin { color: var(--faint); font-size: 11px; }
.meter .bar { position: relative; height: 12px; background: var(--card); border: 1px solid var(--border); border-radius: 3px; overflow: hidden; }
.meter .bar i { position: absolute; top: 0; bottom: 0; }
.meter .bar b { position: absolute; top: -2px; bottom: -2px; left: 50%; width: 1px; background: var(--muted); }
.meter .v { font: 700 13px Menlo, monospace; text-align: right; }
.meter .d { font: 11px Menlo, monospace; color: var(--faint); text-align: right; }
.meter .d.big { color: var(--text); }

.facts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-top: 14px; }
.facts div { background: var(--card); border-radius: 9px; padding: 8px 10px; }
.facts span { display: block; color: var(--muted); font-size: 11px; }
.facts b { font: 600 13px Menlo, monospace; }

.log { height: 160px; overflow-y: auto; font: 12px/1.5 Menlo, monospace; color: var(--muted); white-space: pre-wrap; }
.log .ok { color: var(--rest); } .log .warn { color: var(--recv); } .log .err { color: var(--ctrl); }

.recordbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 40px;
  margin: 12px 0 8px; padding: 6px 12px; background: var(--card); border-radius: 10px; color: var(--muted); font-size: 12px; }
.recordbar.learn { color: var(--recv); } .recordbar.rec { color: var(--ctrl); }
.cmdlist { display: grid; gap: 6px; max-height: 360px; overflow-y: auto; padding-right: 2px; }
.cmd { background: var(--card); border: 1px solid transparent; border-radius: 10px; padding: 8px 10px 8px 12px;
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; align-items: center; }
.cmd.playing { background: var(--card-hi); border-color: var(--rest); }
.cmd .nm { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cmd .id { color: var(--faint); font-size: 11px; text-align: right; }
.cmd .meta { color: var(--muted); font-size: 11px; }
.cmd .meta.empty { color: var(--faint); }
.cmd .btns { display: flex; gap: 4px; }
.cmd .btns button { appearance: none; width: 30px; height: 28px; border-radius: 7px; border: 0; cursor: pointer;
  background: var(--card-hi); font-size: 13px; color: var(--text); }
.cmd .btns button:hover { background: var(--border); }
.cmd .btns button:disabled { opacity: .3; cursor: not-allowed; background: var(--card-hi); }
.cmd .btns .r { color: var(--ctrl); } .cmd .btns .p { color: var(--rest); } .cmd .btns .x { color: var(--muted); }
.selbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; margin: 0 0 8px; padding: 6px 12px; min-height: 42px;
  background: var(--card); border: 1px solid transparent; border-radius: 10px; font-size: 12px; }
.selbar.has { border-color: var(--accent); background: rgba(59,130,246,.10); }
.selbar .selall { display: flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 600; }
.selbar span { flex: 1; color: var(--muted); }
.selbar.has span { color: var(--text); font-weight: 600; }
.selbar .btn { padding: 5px 10px; font-size: 12px; }
input.ck, .selall input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.cmd { grid-template-columns: 22px minmax(0, 1fr) auto; }
.cmd .ck { grid-row: 1 / 3; }
.cmd .ck:disabled { cursor: not-allowed; opacity: .4; }
.cmd.selected { border-color: var(--accent); background: rgba(59,130,246,.10); }
.empty-note { color: var(--faint); text-align: center; padding: 20px 0; font-size: 12px; }

.seqlist { margin-top: 12px; background: var(--card); border-radius: 10px; padding: 6px; min-height: 140px;
  max-height: 260px; overflow-y: auto; }
.step { padding: 6px 10px; border-radius: 7px; cursor: pointer; font-size: 13px; }
.step:hover { background: var(--card-hi); }
.step.sel { background: var(--accent); color: #fff; }
.step.delay { color: var(--muted); } .step.missing { color: var(--ctrl); }
.step.sel.delay, .step.sel.missing { color: #fff; }
.seqtools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.seqtools .btn { padding: 6px 10px; }
.seqtools select { max-width: 160px; }
.runrow { display: flex; gap: 6px; margin-top: 12px; }
.runrow .run { flex: 1; background: var(--rest); border-color: var(--rest); color: #06280f; }
.runrow .btn.active { background: var(--ctrl); border-color: var(--ctrl); }
.progress { height: 6px; background: var(--card); border-radius: 3px; margin-top: 10px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--rest); transition: width .2s; }

.banner { position: fixed; left: 16px; right: 16px; bottom: 16px; padding: 12px 16px; border-radius: 10px;
  background: #3b1d1d; border: 1px solid var(--ctrl); color: var(--text); }

@media (max-width: 900px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .joy canvas { width: min(240px, 42vw); }
}
@media (max-width: 520px) {
  .top, .grid { margin-left: 8px; margin-right: 8px; }
  .meter { grid-template-columns: 50px 30px minmax(0, 1fr) 62px 52px; gap: 6px; }
}

/* ---------------- Máy tính: vừa 1 màn hình, không cuộn trang ----------------
   Cột trái: Lệnh đã học · giữa: Tay điều khiển + Điện áp · phải: Chuỗi lệnh + Nhật ký.
   Danh sách dài cuộn bên trong từng khung. */
@media (min-width: 1100px) and (min-height: 540px) {
  html, body { height: 100%; overflow: hidden; }
  body { display: flex; flex-direction: column; }
  .top { margin: 10px 12px; padding: 8px 14px; flex: none; }
  .top h1 { font-size: 17px; }
  .btn { padding: 6px 12px; }
  .owner { padding: 5px 12px; }

  .grid { flex: 1; min-height: 0; margin: 0 12px 12px; gap: 12px;
    grid-template-columns: minmax(280px, 1fr) minmax(0, 1.6fr) minmax(280px, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "cmd sticks seq" "cmd meters log"; }
  .card { padding: 12px 14px; min-height: 0; }
  .sticks { grid-area: sticks; }
  .meters { grid-area: meters; overflow-y: auto; }
  .cmdcard { grid-area: cmd; display: flex; flex-direction: column; }
  .seqcard { grid-area: seq; display: flex; flex-direction: column; }
  .logcard { grid-area: log; grid-column: auto; display: flex; flex-direction: column; }

  /* tay điều khiển: cần ảo co theo chiều cao màn hình */
  .sticks .card-head { flex-wrap: nowrap; }
  .sticks .card-head > div:first-child { min-width: 0; }
  .sticks .actions { flex: none; }
  .joys { margin-top: 4px; gap: 8px; }
  .joy h3 { font-size: 13px; }
  .joy canvas { width: clamp(110px, calc(100vh - 610px), 240px); margin: 4px auto 2px; }
  .hint { margin-top: 4px; }

  /* điện áp gọn lại */
  .meters h2 { font-size: 15px; }
  .meters h4 { margin: 8px 0 4px; }
  .meter-list { gap: 3px; }
  .meter .name, .meter .v { font-size: 12px; }
  .facts { margin-top: 8px; gap: 6px; }
  .facts div { padding: 5px 8px; }

  /* lệnh: danh sách chiếm hết phần còn lại của cột */
  .cmdcard .card-head p { display: none; }
  .recordbar { margin: 8px 0 6px; min-height: 34px; }
  .selbar { min-height: 36px; padding: 4px 10px; margin-bottom: 6px; }
  .cmdlist { flex: 1; min-height: 0; max-height: none; align-content: start; }
  .cmd { padding: 6px 8px 6px 10px; }

  /* chuỗi lệnh */
  .seqlist { flex: 1; min-height: 60px; max-height: none; margin-top: 8px; }
  .seqtools { margin-top: 6px; gap: 4px; }
  .seqtools .btn { padding: 5px 8px; font-size: 12px; }
  .runrow { margin-top: 8px; }
  .progress { margin-top: 6px; }

  /* nhật ký */
  .log { flex: 1; min-height: 0; height: auto; margin-top: 4px; font-size: 11px; }
}
@media (min-width: 1100px) and (min-height: 540px) and (max-height: 700px) {
  .sticks .card-head p, .meters h2, .seqcard > p:first-of-type { display: none; }
  .seqtools .btn { padding: 4px 7px; }
  .runrow .btn { padding: 5px 10px; }
  .meter .bar { height: 10px; }
  .meter { line-height: 1.15; }
  .meters h4 { line-height: 1.2; }
  .facts span { display: inline; margin-right: 6px; }
  .facts b { font-size: 12px; }
  .meter-list { gap: 1px; }
  .meters h4 { margin: 5px 0 2px; }
  .facts { margin-top: 5px; }
  .facts div { padding: 3px 8px; }
}

/* Bay giả lập (web/sim trong iframe phủ toàn trang, giữ nguyên kết nối ESP32) */
.btn.sim { background: #0F766E; border-color: #14B8A6; font-weight: 600; }
.btn.sim:hover { background: #0D9488; }
.simlayer { position: fixed; inset: 0; z-index: 100; background: #000; }
.simlayer[hidden] { display: none; }
.simlayer iframe { width: 100%; height: 100%; border: 0; display: block; }
body.simopen { overflow: hidden; }

/* ---------------- Live stream ----------------
   Bật 📡 Live: video chiếm cột giữa; trái = Lệnh + Chuỗi lệnh; phải = Tay điều khiển + Điện áp + Nhật ký. */
.btn.live { background: #7C2D12; border-color: #EA580C; }
.btn.live:hover { background: #9A3412; }
.btn.live.on { background: #EA580C; border-color: #FB923C; }
.livecard { display: none; }
body.live .livecard { display: flex; flex-direction: column; grid-column: 1 / -1; order: -1; }
.live-head { display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: center; }
.live-head h2 { margin-right: 4px; }
.live-head .btn { padding: 5px 10px; }
.inp { appearance: none; border: 1px solid var(--border); background: var(--card); color: var(--text);
  padding: 6px 10px; border-radius: 9px; font: 13px Menlo, monospace; width: 150px; min-width: 0; flex: 1 1 120px; max-width: 320px; }
.inp:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.livebox { position: relative; margin-top: 8px; background: #000; border-radius: 10px; overflow: hidden;
  aspect-ratio: 16 / 9; display: flex; align-items: center; justify-content: center; }
.livebox video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.livebox:fullscreen { border-radius: 0; }
.livehelp { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; padding: 16px; text-align: center; color: var(--muted); font-size: 13px;
  background: radial-gradient(ellipse at center, #141a24 0%, #05070a 75%); }
.livehelp[hidden] { display: none; }
.livehelp b { color: var(--text); font-size: 14px; margin-bottom: 4px; }
.livehelp code { background: rgba(255,255,255,.08); padding: 1px 6px; border-radius: 5px; color: var(--text); }
.livehelp code.url { font-size: 15px; padding: 6px 12px; color: #FDBA74; user-select: all; }
.livehelp small { max-width: 560px; margin-top: 6px; font-size: 12px; line-height: 1.5; }
.liveosd { position: absolute; left: 10px; top: 10px; padding: 3px 8px; border-radius: 6px; font: 11px Menlo, monospace;
  background: rgba(0,0,0,.55); color: #e5e7eb; pointer-events: none; }
.liveosd[hidden] { display: none; }

@media (min-width: 1100px) and (min-height: 540px) {
  body.live .grid {
    grid-template-columns: minmax(270px, 320px) minmax(0, 1fr) minmax(290px, 340px);
    grid-template-rows: minmax(0, 1.05fr) minmax(0, .95fr) minmax(0, .9fr) minmax(0, .8fr);
    grid-template-areas: "cmd live sticks" "cmd live meters" "seq live meters" "seq live log"; }
  body.live .livecard { grid-area: live; grid-column: auto; order: 0; }
  body.live .livebox { flex: 1; min-height: 0; aspect-ratio: auto; }

  /* tay điều khiển gọn: cần ảo co theo khung */
  body.live .sticks { display: flex; flex-direction: column; }
  body.live .sticks .card-head p, body.live .sticks .actions kbd, body.live .hint { display: none; }
  body.live .sticks h2 { font-size: 14px; }
  body.live .sticks .btn { padding: 4px 8px; font-size: 12px; }
  body.live .joys { flex: 1; min-height: 0; flex-wrap: nowrap; margin-top: 4px; gap: 6px; }
  body.live .joy { display: flex; flex-direction: column; align-items: center; min-height: 0; min-width: 0; flex: 1; }
  body.live .joy h3 { font-size: 11px; color: var(--muted); }
  body.live .joy canvas { flex: 1 1 0; min-height: 0; height: 0; width: auto; max-width: 100%; margin: 2px auto; }
  body.live .joyval { font-size: 10px; }

  /* điện áp: 2 hàng thông tin */
  body.live .meters h2 { display: none; }
  body.live .meters h4 { margin: 4px 0 3px; }
  body.live .meter { grid-template-columns: 46px 30px minmax(0, 1fr) 58px 50px; gap: 5px; }
  body.live .meter .name, body.live .meter .v { font-size: 11px; }
  body.live .meter .pin { font-size: 10px; }
  body.live .facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  /* lệnh + chuỗi lệnh cột trái */
  body.live .cmdcard .card-head h2, body.live .seqcard h2 { font-size: 14px; }
  body.live .seqcard > p:first-of-type { display: none; }
  body.live .seqtools .btn { padding: 4px 7px; font-size: 11px; }
  body.live .runrow .btn { padding: 5px 10px; }
}
