:root {
  --bg: #f3f1ea;
  --surface: #fffefa;
  --surface-2: #f7f5ee;
  --line: #e4dfd2;
  --line-strong: #d3ccbb;
  --text: #2a2822;
  --label: #4a463d;
  --muted: #8a8476;
  --accent: #e8662e;
  --accent-dark: #c9541f;
  --accent-soft: #fdeadf;
  --stat-bg: #eef1ea;
  --stat-line: #dde3d8;
  --ok: #4f7f2a;
  --ok-soft: #ebf2db;
  --busy: #9a6a12;
  --busy-soft: #f8eed6;
  --warn: #b65a1c;
  --warn-soft: #fbe8da;
  --bad: #c03a3a;
  --bad-soft: #f9e3e1;
  --idle: #7a7263;
  --idle-soft: #eeeae0;
  --shadow: 0 6px 18px rgba(60, 52, 30, .06);
  --control-h: 34px;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html { touch-action: pan-x pan-y; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 "Be Vietnam Pro", system-ui, "Segoe UI", sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 11px; font-weight: 500; }

/* nút */
.btn {
  height: var(--control-h);
  border: 1px solid var(--line-strong);
  background: var(--surface-2);
  border-radius: 9px;
  padding: 0 14px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, box-shadow .15s, transform .08s;
}
.btn:hover { background: #efece3; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent-dark); color: #fff; box-shadow: 0 4px 12px rgba(232, 102, 46, .22); }
.btn-primary:hover { background: var(--accent-dark); }
.btn-danger-soft { color: var(--bad); }
.btn-danger-soft:hover { background: var(--bad-soft); border-color: #ecc5c1; }
.icon-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 9px;
  width: 30px; height: 30px;
  display: inline-grid; place-items: center;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.square { width: 32px; height: 32px; font-size: 18px; font-weight: 700; line-height: 1; }
.link-btn { border: 0; background: none; color: var(--accent-dark); font-weight: 700; cursor: pointer; padding: 0; font-size: 12px; }

/* form */
.field { display: grid; gap: 4px; align-content: start; }
.field > span { font-size: 11px; font-weight: 700; color: var(--label); }
.field input, .field select, .field textarea {
  width: 100%;
  height: var(--control-h);
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 12px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field select { padding-right: 6px; }
.field textarea { height: auto; min-height: 72px; padding: 8px 10px; resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 11.5px; }
.field input::placeholder { color: #b0a999; }
.field input.masked { -webkit-text-security: disc; }
.field input.masked::placeholder { -webkit-text-security: none; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.names-grid { display: grid; gap: 8px; }
.field.disabled, .check.disabled { opacity: .45; pointer-events: none; }
.form-error { color: var(--bad); min-height: 16px; margin: 0; font-size: 12px; font-weight: 600; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.grid-3 > *, .group-grid > * { min-width: 0; }
.check {
  display: flex; align-items: center; gap: 8px;
  height: var(--control-h);
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  transition: border-color .15s, background .15s;
}
.check:hover { border-color: var(--line-strong); }
.check:has(input:checked) { border-color: #efc1a6; background: #fff7f1; }
.check span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.check-hint { margin-left: auto; color: var(--muted); font-size: 11px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.check input { width: 14px; height: 14px; accent-color: var(--accent); margin: 0; flex: none; }
.check-inline { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 11px; font-weight: 600; color: var(--muted); }
.check-inline input { width: 13px; height: 13px; margin: 0; accent-color: var(--accent); }

/* đăng nhập */
.auth-view { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 24px 22px 20px;
}
.auth-brand { display: flex; gap: 12px; align-items: center; margin-bottom: 18px; }
.auth-brand h1 { margin: 2px 0 0; font-size: 19px; }
.auth-form { display: grid; gap: 10px; }
.auth-form .btn { margin-top: 4px; }
.auth-switch { margin-top: 8px; display: flex; gap: 6px; justify-content: center; font-size: 12px; color: var(--muted); }

/* khung app */
.app-shell { max-width: 1120px; margin: 0 auto; padding: 18px 16px 48px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-logo { width: 34px; height: 34px; image-rendering: pixelated; border-radius: 8px; }
.eyebrow { color: var(--accent-dark); font-size: 9px; font-weight: 800; letter-spacing: .18em; }
.wordmark h2 { margin: 1px 0 0; font-size: 16px; font-weight: 800; letter-spacing: .02em; line-height: 1.1; }
.wordmark .accent { color: var(--accent); }
.user-chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 4px 4px 4px 4px;
  min-width: 0;
}
.avatar { width: 28px; height: 28px; border-radius: 50%; background: #21493d; color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 12px; flex: none; }
.user-email { font-size: 12px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.btn-admin { height: 28px; padding: 0 10px; display: inline-flex; align-items: center; text-decoration: none; color: var(--accent-dark); border-color: #efc1a6; background: var(--accent-soft); }
.preview-bar { background: var(--accent-soft); color: var(--accent-dark); border-color: #efc1a6; display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.preview-bar a { color: var(--accent-dark); font-weight: 800; }

.main-nav {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 6px;
  background: #ebe8de;
  border: 1px solid var(--line);
  border-radius: 13px;
  padding: 5px;
  margin-bottom: 16px;
}
.nav-btn {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  height: 34px;
  border: 0;
  background: transparent;
  border-radius: 9px;
  font-size: 12px; font-weight: 700; color: #6a6457;
  cursor: pointer;
}
.nav-btn:hover { background: rgba(255, 255, 255, .55); }
.nav-btn svg { width: 14px; height: 14px; fill: currentColor; flex: none; }
.nav-btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-btn .lbl-short { display: none; }
.nav-btn.active { background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(232, 102, 46, .25); }

.offline-bar { background: var(--warn-soft); color: var(--warn); border: 1px solid #f0c9ad; padding: 8px 12px; border-radius: 10px; margin-bottom: 12px; font-size: 12px; font-weight: 600; }
.placeholder-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; }
.placeholder-card h3 { margin: 0 0 4px; font-size: 15px; }
.placeholder-card p { margin: 0; color: var(--muted); font-size: 12px; }

/* thẻ ô */
.slot-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 12px; align-items: start; }
.slot-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 13px;
  box-shadow: var(--shadow);
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px;
  cursor: pointer;
  transition: border-color .2s, box-shadow .2s;
}
.slot-card:hover { border-color: var(--line-strong); }
.slot-card.selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.slot-view { position: relative; aspect-ratio: 5 / 3; background: #0c0c0c; border-radius: 10px; overflow: hidden; }
.slot-view canvas { display: block; width: 100%; height: 100%; }
.slot-view-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 8px; text-align: center; color: #d8d3c4; font-size: 11px; font-weight: 700; background: rgba(0, 0, 0, .45); }
.slot-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.slot-head { display: flex; align-items: center; gap: 9px; }
.slot-badge {
  width: 32px; height: 32px; border-radius: 50%;
  border: 1.5px solid var(--line-strong);
  background: var(--surface-2);
  display: grid; place-items: center;
  font-weight: 800; font-size: 12px; flex: none;
}
.slot-card.running .slot-badge { border-color: var(--ok); color: var(--ok); background: var(--ok-soft); }
.slot-ident { min-width: 0; flex: 1; display: grid; gap: 1px; }
.slot-ident strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot-sub { color: var(--muted); font-size: 10.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  border-radius: 999px; padding: 4px 8px;
  font-size: 10px; font-weight: 800; white-space: nowrap;
  max-width: 120px; overflow: hidden; text-overflow: ellipsis; flex: none;
}
.pill i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; opacity: .85; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.ok i { box-shadow: 0 0 0 3px rgba(79, 127, 42, .15); }
.pill.busy { background: var(--busy-soft); color: var(--busy); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.idle { background: var(--idle-soft); color: var(--idle); }

.stats { background: var(--stat-bg); border: 1px solid var(--stat-line); border-radius: 10px; padding: 2px 10px; }
.stat-row { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; font-size: 11px; color: #5d665e; }
.stat-row + .stat-row { border-top: 1px solid var(--stat-line); }
.stat-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-row b { color: var(--text); font-weight: 700; }
.slot-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.slot-actions .btn { height: 30px; font-size: 11.5px; }

.slot-add {
  min-height: 228px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 14px;
  background: transparent;
  display: grid; place-items: center; align-content: center; gap: 8px;
  cursor: pointer; font-size: 12px; font-weight: 700; color: var(--label);
  transition: border-color .2s, background .2s;
}
.slot-add:hover { border-color: var(--accent); background: rgba(255, 255, 255, .4); }
.slot-add .plus { width: 40px; height: 40px; border-radius: 12px; border: 1.5px solid var(--accent); color: var(--accent); display: grid; place-items: center; font-size: 22px; background: var(--surface); }
.slot-add:disabled { opacity: .5; cursor: default; }
.empty-hint { color: var(--muted); font-size: 11px; font-weight: 500; }

/* drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(40, 34, 20, .16); z-index: 20; }
.drawer {
  position: fixed; top: 10px; right: 10px; bottom: 10px;
  width: min(532px, calc(100vw - 20px));
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 20px 50px rgba(40, 34, 20, .2);
  display: grid; grid-template-rows: auto 1fr auto;
  overflow: hidden;
  z-index: 30;
  transform: translateX(calc(100% + 20px));
  transition: transform .25s ease;
}
.drawer.open { transform: none; }
/* drawer J2ME nằm sẵn trong index.html kể cả khi tab tắt (ui.css chưa tải) */
.j-drawer { visibility: hidden; transition: transform .25s ease, visibility 0s linear .25s; }
.j-drawer.open { visibility: visible; transition: transform .25s ease; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); background: var(--surface); }
.segmented { flex: 1; display: grid; grid-template-columns: repeat(2, 1fr); padding: 3px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); max-width: 300px; margin: 0 auto; }
.seg-btn { border: 0; background: none; height: 28px; border-radius: 7px; font-weight: 700; font-size: 12px; cursor: pointer; color: #6a6457; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .08); }
.drawer-body { overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; padding: 12px 14px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; align-content: start; }
.drawer-body > div { display: grid; gap: 10px; min-width: 0; }
.drawer-foot { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 10px 14px max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--surface); }

.box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.box-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.box-head h4 { margin: 0; font-size: 13px; font-weight: 800; }
.chip-tabs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
.chip {
  height: 30px;
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 8px; padding: 0 6px;
  font-weight: 700; font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer; color: #6a6457;
}
.chip:hover { border-color: var(--line-strong); }
.chip.active { background: var(--accent); border-color: var(--accent-dark); color: #fff; }
.group-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; align-items: end; }
.group-grid .wide { grid-column: span 2; }
#settingsForm { display: grid; gap: 10px; }

.view-box { position: relative; width: 100%; max-width: 500px; margin: 0 auto; aspect-ratio: 1; background: #000; border-radius: var(--radius); overflow: hidden; }
.view-box canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.view-tools { display: flex; justify-content: center; gap: 8px; margin-top: 10px; }
.view-tools .btn { min-width: 112px; }

.nick-switch { position: relative; flex: none; }
.switch-btn { cursor: pointer; font-family: inherit; display: inline-flex; align-items: center; width: auto; min-width: 32px; padding: 0 9px; border-radius: 16px; }
.switch-btn::after { content: ""; display: inline-block; margin-left: 4px; border: 4px solid transparent; border-top-color: currentColor; border-bottom: 0; vertical-align: middle; }
.switch-btn:hover, .switch-btn[aria-expanded="true"] { border-color: var(--accent); color: var(--accent-dark); }
.nick-menu { position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; width: 280px; max-width: calc(100vw - 32px); max-height: 340px; overflow-y: auto; padding: 4px; background: var(--surface); border: 1px solid var(--line-strong); border-radius: 10px; box-shadow: 0 10px 28px rgba(40, 34, 20, .16); }
.nick-item { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 8px; width: 100%; padding: 7px 8px; border: 0; border-radius: 8px; background: none; text-align: left; cursor: pointer; font: inherit; color: var(--text); }
.nick-item:hover { background: var(--surface-2); }
.nick-item.active { background: var(--accent-soft); }
.nick-name { display: flex; flex-direction: column; min-width: 0; font-weight: 700; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nick-name small { font-weight: 500; font-size: 11px; color: var(--muted); }
.nick-status { font-size: 11px; color: var(--muted); white-space: nowrap; }
.view-state { position: absolute; inset: 0; margin: 0; display: grid; place-items: center; color: #d8d3c4; font-size: 12px; font-weight: 600; background: rgba(0, 0, 0, .55); }

.toast {
  position: fixed; left: 50%; bottom: 20px;
  transform: translate(-50%, 16px);
  background: #24312c; color: #fff;
  padding: 8px 14px; border-radius: 10px;
  font-weight: 600; font-size: 12px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--bad); }

@media (max-width: 640px) {
  .app-shell { padding: 12px 16px 36px; }
  .user-email { max-width: 150px; }
  .nav-btn { font-size: 11.5px; }
  .nav-btn svg { display: none; }
  .main-nav { gap: 3px; padding: 4px; }
  .nav-btn { padding: 0 2px; }
  .nav-btn .lbl-full { display: none; }
  .nav-btn .lbl-short { display: inline; }
  .slot-grid { grid-template-columns: 1fr; }
  .slot-add { min-height: 120px; }
  .drawer { top: 0; right: 0; bottom: 0; width: 100vw; border-radius: 0; border: 0; }
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .grid-3 > :last-child { grid-column: span 2; }
}
@media (max-width: 360px) {
  .group-grid { grid-template-columns: 1fr; }
  .group-grid .wide { grid-column: auto; }
}

/* ── bảng hỏi dùng chung (dialog.js) ─────── */
.btn-danger { background: var(--bad); border-color: #a53030; color: #fff; }
.btn-danger:hover { background: #a53030; }
.ui-dlg { width: min(420px, calc(100vw - 32px)); padding: 20px 20px 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); box-shadow: 0 18px 50px rgba(40, 32, 15, .22); font-family: inherit; }
.ui-dlg::backdrop { background: rgba(32, 28, 18, .42); backdrop-filter: blur(2px); }
.ui-dlg[open] { animation: ui-dlg-in .14s ease-out; }
@keyframes ui-dlg-in { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.ui-dlg-title { margin: 0 0 8px; font-size: 16px; font-weight: 800; }
.ui-dlg-text { margin: 0 0 16px; font-size: 13px; line-height: 1.55; color: var(--label); white-space: pre-line; }
.ui-dlg-note { margin: 10px 0 14px; font-size: 12px; color: var(--muted); }
.ui-dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.ui-dlg-actions .btn { min-width: 84px; }
.ui-dlg-secret { display: flex; gap: 8px; align-items: center; }
.ui-dlg-secret code { flex: 1; min-width: 0; padding: 10px 12px; border: 1px dashed var(--line-strong); border-radius: 9px; background: var(--surface-2); font: 700 16px ui-monospace, Consolas, monospace; letter-spacing: .04em; overflow-wrap: anywhere; user-select: all; }
.ui-dlg-input { width: 100%; height: var(--control-h); margin-bottom: 6px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 9px; background: var(--surface); font: inherit; font-size: 14px; }
.ui-dlg-input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ui-dlg-err { min-height: 16px; margin: 0 0 8px; font-size: 12px; color: var(--bad); }
.ui-dlg-list { display: grid; gap: 6px; max-height: min(55vh, 420px); overflow-y: auto; margin-bottom: 14px; }
.ui-dlg-opt { display: flex; gap: 10px; align-items: center; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; cursor: pointer; }
.ui-dlg-opt:has(input:checked) { border-color: #efc1a6; background: #fff7f1; }
.ui-dlg-opt.disabled { opacity: .5; cursor: not-allowed; }
.ui-dlg-opt input { accent-color: var(--accent); flex: none; }
.ui-dlg-opt-text { display: grid; gap: 1px; min-width: 0; }
.ui-dlg-opt-text b { font-size: 13px; overflow-wrap: anywhere; }
.ui-dlg-opt-text small { font-size: 11.5px; color: var(--muted); }
.combo { position: relative; }
.combo-list { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 20; max-height: 240px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); box-shadow: var(--shadow); }
.combo-item { padding: 7px 10px; font-size: 13px; cursor: pointer; }
.combo-item.active, .combo-item:hover { background: var(--accent-soft); }
.combo-empty { padding: 8px 10px; font-size: 12px; color: var(--muted); }
.dv-count { position: absolute; top: 6px; left: 6px; display: grid; gap: 1px; padding: 4px 7px; border-radius: 6px; background: rgba(0, 0, 0, .55); font-size: 11px; font-weight: 700; line-height: 1.35; pointer-events: none; }
.dv-count .dv-done { color: #7fd35a; }
.dv-count .dv-cancel { color: #ff7a6e; }
