:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #d9dde3;
  --border-strong: #b9c0ca;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #1f6feb;
  --accent-soft: rgba(31, 111, 235, 0.12);
  --accent-text: #ffffff;
  --danger: #d1242f;
  --ok: #1a7f37;
  --warn: #9a6700;
  --head-bg: #f0f2f5;
  --head-text: #4b5563;
  --cell-bg: #ffffff;
  --sel-bg: rgba(31, 111, 235, 0.10);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "Courier New", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --panel: #171a21; --panel-2: #1e222b; --border: #2a303b; --border-strong: #3a4250;
    --text: #e6e8ec; --muted: #9aa3b2; --accent: #4c8dff; --accent-soft: rgba(76, 141, 255, 0.18);
    --head-bg: #1e222b; --head-text: #aab3c2; --cell-bg: #12151b; --sel-bg: rgba(76, 141, 255, 0.18);
    --danger: #ff6b6b; --ok: #3fb950; --warn: #d29922; --shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --panel: #171a21; --panel-2: #1e222b; --border: #2a303b; --border-strong: #3a4250;
  --text: #e6e8ec; --muted: #9aa3b2; --accent: #4c8dff; --accent-soft: rgba(76, 141, 255, 0.18);
  --head-bg: #1e222b; --head-text: #aab3c2; --cell-bg: #12151b; --sel-bg: rgba(76, 141, 255, 0.18);
  --danger: #ff6b6b; --ok: #3fb950; --warn: #d29922; --shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); font-size: 13px; color: var(--text); background: var(--bg); overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
code { font-family: var(--mono); font-size: 12px; background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
a { color: var(--accent); }

/* ---------- màn hình toàn trang (đăng nhập, thiết lập) ---------- */
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; overflow: auto; background: var(--bg); }
.card { width: 100%; max-width: 460px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.card.center { text-align: center; }
.card h1 { margin: 6px 0 14px; font-size: 22px; }
.card p { line-height: 1.5; }
.logo { font-size: 40px; color: var(--accent); line-height: 1; }
.logo-img { width: 64px; height: 64px; display: block; margin: 0 auto 4px; }
.card.center .logo-img, .card .logo-img { margin-left: auto; margin-right: auto; }
.brand-icon { width: 20px; height: 20px; vertical-align: -5px; }
.badge.ro { color: var(--warn); border-color: var(--warn); }
.tab .eye { font-size: 11px; opacity: 0.8; margin-left: 4px; }
.toolbar button:disabled { opacity: 0.4; cursor: default; }
.users-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.users-table th, .users-table td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.users-table th { color: var(--muted); font-weight: 600; font-size: 11.5px; }
.users-table .acts { display: flex; gap: 2px; flex-wrap: wrap; }
.users-table .on { color: var(--ok); }
.perm-table { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-top: 6px; }
.perm-table td { padding: 4px 6px; border-bottom: 1px solid var(--border); }
.perm-table select { padding: 4px 6px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--panel); }
.audit { max-height: 220px; overflow: auto; font-size: 12px; border: 1px solid var(--border); border-radius: 8px; }
.audit div { padding: 4px 8px; border-bottom: 1px solid var(--border); display: flex; gap: 10px; }
.audit .bad { color: var(--danger); }
.audit .t { color: var(--muted); white-space: nowrap; }
.progress { height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; border: 1px solid var(--border); margin: 6px 0; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
.muted { color: var(--muted); }
label { display: block; margin: 12px 0; font-weight: 500; }
label input, .field input, .field select, .field textarea { display: block; width: 100%; margin-top: 6px; padding: 9px 11px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--panel); outline: none; font-weight: 400; }
label input:focus, .field input:focus, .field select:focus, .field textarea:focus, #search:focus, #formula-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.error { color: var(--danger); min-height: 18px; margin: 4px 0 8px; font-size: 12.5px; }
.btn { padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--panel-2); cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn:disabled { opacity: 0.6; cursor: default; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.qr-box { text-align: center; margin: 10px 0; }
.qr-box img { width: 220px; height: 220px; background: #fff; border-radius: 8px; padding: 6px; border: 1px solid var(--border); }
.secret { margin-top: 8px; font-size: 12px; color: var(--muted); word-break: break-all; }

/* ---------- khung ứng dụng ---------- */
#screen-app { position: fixed; inset: 0; display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { font-weight: 700; color: var(--accent); margin-right: 8px; white-space: nowrap; }
.toolbar { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.icon-btn, .tb { border: 1px solid transparent; background: transparent; border-radius: 7px; padding: 5px 8px; cursor: pointer; color: var(--text); white-space: nowrap; }
.icon-btn { font-size: 15px; line-height: 1; min-width: 30px; }
.icon-btn:hover, .tb:hover { background: var(--panel-2); border-color: var(--border); }
.tb.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.icon-btn:disabled { opacity: 0.4; cursor: default; background: transparent; border-color: transparent; }
.search-wrap { display: flex; align-items: center; gap: 2px; margin-left: 8px; position: relative; }
#search { width: 240px; max-width: 40vw; padding: 6px 28px 6px 10px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--panel); outline: none; }
#search-clear { position: absolute; right: 2px; padding: 3px 6px; }
#topbar .right { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.badge { font-size: 11px; padding: 3px 8px; border-radius: 99px; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge.remote { color: var(--warn); border-color: var(--warn); }
.badge.local { color: var(--ok); border-color: var(--ok); }

#formula-bar { display: flex; align-items: stretch; border-bottom: 1px solid var(--border); background: var(--panel); }
#cell-name { width: 80px; display: flex; align-items: center; justify-content: center; border-right: 1px solid var(--border); font-weight: 600; color: var(--muted); font-size: 12px; }
#formula-input { flex: 1; border: none; padding: 7px 10px; background: var(--panel); outline: none; font-family: var(--mono); font-size: 12.5px; }
#formula-input:focus { box-shadow: inset 0 -2px 0 var(--accent); }

#main { flex: 1; display: flex; min-height: 0; }

/* ---------- thanh công cụ trái (dock) ---------- */
#dock { width: 76px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: stretch; padding: 6px 4px; gap: 4px; overflow-y: auto; overflow-x: hidden; flex-shrink: 0; }
#dock-tools { display: flex; flex-direction: column; gap: 4px; }
.dock-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; padding: 7px 2px 5px; border-radius: 9px; border: 1px solid transparent; background: transparent; cursor: pointer; width: 100%; color: var(--text); }
.dock-btn:hover { background: var(--panel-2); border-color: var(--border); }
.dock-btn img { width: 30px; height: 30px; object-fit: contain; image-rendering: auto; }
.dock-btn .emoji { font-size: 24px; line-height: 30px; height: 30px; }
.dock-btn .lbl { font-size: 10.5px; line-height: 1.15; max-width: 66px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.dock-btn.add { border: 1px dashed var(--border-strong); margin-top: 4px; }
.dock-btn.running { opacity: 0.6; }

/* ---------- lưới ---------- */
#grid-area { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#grid-wrap { flex: 1; overflow: auto; position: relative; background: var(--bg); outline: none; }
#grid-inner { position: relative; }
#grid-head { position: sticky; top: 0; z-index: 3; background: var(--head-bg); }
#grid-body { position: absolute; left: 0; }
.hrow, .row { display: flex; align-items: stretch; }
.hrow { border-bottom: 1px solid var(--border-strong); }
.row.frozen { border-bottom: 2px solid var(--border-strong); background: var(--cell-bg); }
.rn, .ch { flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 11.5px; color: var(--head-text); background: var(--head-bg); user-select: none; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.rn { position: sticky; left: 0; z-index: 2; }
.rn.corner { z-index: 4; }
.ch { position: relative; cursor: pointer; font-weight: 600; }
.rn.hl, .ch.hl { background: var(--accent-soft); color: var(--accent); }
.rsz { position: absolute; right: -3px; top: 0; width: 7px; height: 100%; cursor: col-resize; z-index: 1; }
.rsz:hover { background: var(--accent); opacity: 0.5; }
.sp { flex-shrink: 0; }
.cell { flex-shrink: 0; padding: 0 6px; display: flex; align-items: center; overflow: hidden; white-space: pre; text-overflow: ellipsis; background: var(--cell-bg); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); font-size: 12.5px; cursor: cell; user-select: none; }
.cell.num { justify-content: flex-end; font-variant-numeric: tabular-nums; }
.cell.err { color: var(--danger); font-weight: 600; justify-content: center; }
.cell.sel { background: var(--sel-bg); }
.cell.act { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--cell-bg); }
.cell.sel.act { background: var(--cell-bg); }
.cell.hit { box-shadow: inset 0 0 0 100px rgba(255, 200, 0, 0.18); }
#cell-editor { position: absolute; z-index: 6; margin: 0; padding: 4px 6px; border: 2px solid var(--accent); border-radius: 0; background: var(--cell-bg); color: var(--text); font-size: 12.5px; resize: none; overflow: hidden; line-height: 18px; min-height: 28px; box-shadow: 0 4px 14px rgba(0,0,0,0.2); outline: none; white-space: pre; }
/* chế độ "chờ": ô nhập trong suốt nằm trên ô đang chọn để nhận phím gõ (kể cả bộ gõ tiếng Việt) */
#cell-editor.idle { opacity: 0; pointer-events: none; box-shadow: none; height: 28px !important; }
body.resizing, body.resizing * { cursor: col-resize !important; user-select: none; }

/* ---------- panel kết quả công cụ ---------- */
#tool-output { height: 220px; border-top: 1px solid var(--border-strong); background: var(--panel); display: flex; flex-direction: column; }
.to-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px; border-bottom: 1px solid var(--border); font-weight: 600; }
.to-actions { display: flex; gap: 4px; }
#to-body { flex: 1; margin: 0; padding: 8px 10px; overflow: auto; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; }

/* ---------- thanh dưới: tab trang + trạng thái ---------- */
#bottombar { display: flex; align-items: center; gap: 4px; background: var(--panel); border-top: 1px solid var(--border); padding: 0 6px; min-height: 34px; }
#sheet-tabs { display: flex; gap: 2px; overflow-x: auto; max-width: 55vw; }
.tab { padding: 6px 14px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; color: var(--muted); user-select: none; border-radius: 6px 6px 0 0; }
.tab:hover { background: var(--panel-2); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
#statusbar { margin-left: auto; display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 12px; white-space: nowrap; }
#statusbar .grow { flex: 1; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); display: inline-block; }
.dot.on { background: var(--ok); }

/* ---------- menu chuột phải ---------- */
#ctx-menu { position: fixed; z-index: 100; background: var(--panel); border: 1px solid var(--border-strong); border-radius: 8px; box-shadow: var(--shadow); padding: 4px; min-width: 200px; }
#ctx-menu .mi { padding: 7px 12px; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 16px; }
#ctx-menu .mi:hover { background: var(--accent-soft); }
#ctx-menu .mi.disabled { opacity: 0.45; pointer-events: none; }
#ctx-menu .mi.danger { color: var(--danger); }
#ctx-menu .mi .kbd { color: var(--muted); font-size: 11px; }
#ctx-menu .msep { height: 1px; background: var(--border); margin: 4px 6px; }

/* ---------- modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; width: 100%; max-width: 520px; max-height: 92vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal.wide { max-width: 760px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 14px 18px; overflow: auto; line-height: 1.5; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.field { margin: 10px 0; }
.field > span { display: block; font-weight: 500; margin-bottom: 2px; }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 3px; }
.field.inline { display: flex; align-items: center; gap: 10px; }
.field.inline input[type="checkbox"] { width: auto; margin: 0; }
.field-row { display: flex; gap: 8px; align-items: flex-end; }
.field-row .field { flex: 1; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); padding: 0 18px; }
.tabs button { background: none; border: none; padding: 10px 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.box { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin: 10px 0; background: var(--panel-2); }
.box h3 { margin: 0 0 8px; font-size: 13.5px; }
.box.warn { border-color: var(--warn); }
.box.ok { border-color: var(--ok); }
.kv { display: flex; align-items: center; gap: 8px; margin: 6px 0; flex-wrap: wrap; }
.kv code { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 5px 8px; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.switch input { width: 36px; height: 20px; accent-color: var(--accent); margin: 0; }
.tool-list { display: flex; flex-direction: column; gap: 6px; }
.tool-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.tool-item img, .tool-item .emoji { width: 26px; height: 26px; font-size: 20px; text-align: center; line-height: 26px; }
.tool-item .ti-name { font-weight: 600; }
.tool-item .ti-path { color: var(--muted); font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-item .ti-main { flex: 1; min-width: 0; }
.tool-item .ti-actions { display: flex; gap: 2px; }
.qr-inline { display: block; width: 180px; height: 180px; background: #fff; padding: 5px; border-radius: 8px; margin: 8px auto; }
pre.log { max-height: 140px; overflow: auto; font-size: 11px; background: var(--panel); padding: 8px; border-radius: 6px; border: 1px solid var(--border); }

/* ---------- bảng kiểm tra UID Facebook (góc dưới phải) ---------- */
#uidpanel { position: fixed; right: 14px; bottom: 44px; width: 380px; max-width: calc(100vw - 20px); max-height: calc(100vh - 120px); background: var(--panel); border: 1px solid var(--border-strong); border-radius: 12px; box-shadow: var(--shadow); z-index: 40; display: flex; flex-direction: column; overflow: hidden; }
#uidpanel.min .up-body { display: none; }
.up-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 6px 6px 12px; border-bottom: 1px solid var(--border); font-weight: 600; user-select: none; }
#uidpanel.min .up-head { border-bottom: none; }
.up-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px; overflow: auto; }
.up-body textarea { width: 100%; min-height: 72px; resize: vertical; padding: 6px 8px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--panel); color: var(--text); font-family: var(--mono); font-size: 12px; outline: none; }
.up-body textarea:focus { border-color: var(--accent); }
.up-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.up-row .grow { flex: 1; }
.up-results { display: flex; gap: 8px; }
.up-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.up-col-head { display: flex; justify-content: space-between; align-items: center; font-size: 12px; }
.up-col textarea { min-height: 96px; }
#btn-uid { font-size: 11px; font-weight: 700; letter-spacing: 0.5px; }
#btn-uid.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ---------- nhãn phiên bản (góc dưới trái các màn hình toàn trang) ---------- */
#version-tag { position: fixed; left: 10px; bottom: 8px; font-size: 11px; color: var(--muted); z-index: 5; pointer-events: none; }

/* ---------- toast ---------- */
#toast-root { position: fixed; bottom: 48px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); max-width: 80vw; animation: toast-in 0.15s ease-out; }
.toast.err { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- điện thoại ---------- */
@media (max-width: 720px) {
  #topbar { gap: 4px; padding: 6px; }
  .brand span { display: none; }
  .toolbar .tb { padding: 5px 6px; font-size: 12px; }
  #search { width: 140px; }
  #cell-name { width: 56px; }
  #dock { width: 60px; }
  .dock-btn .lbl { display: none; }
  #statusbar { gap: 8px; }
  #st-sum { display: none; }
  .modal { max-height: 96vh; }
}
