:root {
  --bg: #f4f6fa; --panel: #ffffff; --panel2: #f0f3f8; --text: #1b2230; --muted: #667085;
  --line: #dde3ec; --accent: #2563eb; --accent-2: #dbe7ff; --danger: #d92d20; --ok: #12a150;
  --warn: #dc8a00; --sel: #cfe0ff; --shadow: 0 4px 18px rgba(16, 24, 40, .08); --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c; --panel: #171e29; --panel2: #1f2733; --text: #e6eaf0; --muted: #98a2b3;
    --line: #2b3544; --accent: #5b8cff; --accent-2: #22314f; --danger: #f97066; --ok: #32d583;
    --warn: #fdb022; --sel: #26406e; --shadow: 0 4px 18px rgba(0, 0, 0, .35); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 "Noto Sans Thai", "Sarabun", "Leelawadee UI", system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.ok { color: var(--ok); } .bad { color: var(--danger); } .warn { color: var(--warn); }

.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  padding: 7px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap; min-height: 36px;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn svg { width: 18px; height: 18px; flex: none; }
input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
  min-height: 40px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-weight: 600; margin: 12px 0 4px; }

/* ---------- auth pages ---------- */
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 26px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 22px; margin: 0 0 4px; }
.auth-card .btn.primary { width: 100%; justify-content: center; margin-top: 18px; min-height: 44px; }
.err { color: var(--danger); margin-top: 12px; min-height: 1.5em; }
.qr { display: block; margin: 12px auto; width: 220px; height: 220px; background: #fff; border-radius: 8px; }
.secret { word-break: break-all; background: var(--panel2); padding: 8px; border-radius: 6px; text-align: center; }
.code-input { letter-spacing: 6px; text-align: center; font-size: 22px !important; }

/* ---------- layout ---------- */
.app { display: flex; min-height: 100%; }
.nav { width: 220px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 12px 8px; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 700; font-size: 17px; padding: 6px 10px 14px; display: flex; gap: 8px; align-items: center; }
.brand svg { width: 22px; height: 22px; color: var(--accent); }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: var(--text);
  text-decoration: none; }
.nav a svg { width: 19px; height: 19px; flex: none; color: var(--muted); }
.nav a.active { background: var(--accent-2); color: var(--accent); font-weight: 600; }
.nav a.active svg { color: var(--accent); }
.nav a:hover:not(.active) { background: var(--panel2); }
.nav .spacer { flex: 1; }
.badge { margin-left: auto; background: var(--danger); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; }
.main { flex: 1; min-width: 0; padding: 20px 24px 40px; }
.topbar { display: none; }
h2.title { margin: 0 0 16px; font-size: 22px; }

.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); min-width: 0; }
.card h3 { margin: 0 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; display: flex;
  align-items: center; gap: 8px; }
.card .big { font-size: 28px; font-weight: 700; }
.card.wide { grid-column: 1 / -1; }
.bar { height: 8px; background: var(--panel2); border-radius: 6px; overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 6px; }
.bar > i.w { background: var(--warn); } .bar > i.d { background: var(--danger); }
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 5px 11px; cursor: pointer; }
.seg button.on { background: var(--accent); color: #fff; }
.chart { width: 100%; height: 220px; display: block; margin-top: 10px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }

table.t { width: 100%; border-collapse: collapse; }
table.t th, table.t td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.t th { color: var(--muted); font-weight: 600; font-size: 13px; }
.table-wrap { overflow-x: auto; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--danger); } .dot.warn { background: var(--warn); }

/* ---------- explorer ---------- */
.explorer { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); height: calc(100vh - 100px); min-height: 420px; }
.ex-toolbar { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); flex-wrap: wrap; align-items: center; }
.ex-toolbar .sep { width: 1px; height: 26px; background: var(--line); margin: 0 4px; }
.ex-toolbar .grow { flex: 1; }
.ex-toolbar input[type=search] { width: 220px; min-height: 36px; }
.ex-addr { display: flex; gap: 6px; padding: 6px 8px; border-bottom: 1px solid var(--line); align-items: center; }
.crumbs { display: flex; align-items: center; flex-wrap: wrap; gap: 2px; background: var(--panel2);
  border-radius: 8px; padding: 3px 6px; flex: 1; min-width: 0; }
.crumbs button { border: 0; background: transparent; padding: 3px 6px; border-radius: 5px; cursor: pointer; }
.crumbs button:hover { background: var(--accent-2); }
.crumbs .chev { color: var(--muted); }
.ex-body { display: flex; flex: 1; min-height: 0; }
.ex-tree { width: 230px; flex: none; border-right: 1px solid var(--line); overflow: auto; padding: 6px 4px; }
.tnode { display: flex; align-items: center; gap: 4px; padding: 3px 6px; border-radius: 6px; cursor: pointer;
  white-space: nowrap; user-select: none; }
.tnode:hover { background: var(--panel2); }
.tnode.on { background: var(--sel); }
.tnode .tw { width: 16px; height: 16px; flex: none; color: var(--muted); display: grid; place-items: center; }
.tnode svg { width: 16px; height: 16px; flex: none; }
.tnode.drop { outline: 2px dashed var(--accent); }
.tchildren { margin-left: 14px; }
.ex-pane { flex: 1; overflow: auto; position: relative; padding: 8px; user-select: none; }
.ex-pane.dragover { outline: 3px dashed var(--accent); outline-offset: -6px; background: var(--accent-2); }
.ex-status { display: flex; gap: 14px; padding: 5px 10px; border-top: 1px solid var(--line); font-size: 13px;
  color: var(--muted); flex-wrap: wrap; }
.icons { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 4px; }
.icons .it { display: flex; flex-direction: column; align-items: center; padding: 8px 4px; border-radius: 8px;
  border: 1px solid transparent; cursor: default; text-align: center; min-width: 0; }
.icons .it svg { width: 48px; height: 48px; }
.icons .it .nm { font-size: 13px; margin-top: 4px; word-break: break-word; max-width: 100%;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.it:hover { background: var(--panel2); }
.it.sel { background: var(--sel); border-color: var(--accent); }
.it.cut { opacity: .5; }
.it.drop { outline: 2px dashed var(--accent); }
.list { width: 100%; border-collapse: collapse; }
.list th { position: sticky; top: -8px; background: var(--panel); text-align: left; font-weight: 600; color: var(--muted);
  font-size: 13px; padding: 6px 8px; border-bottom: 1px solid var(--line); cursor: pointer; }
.list td { padding: 5px 8px; white-space: nowrap; }
.list td.n { display: flex; align-items: center; gap: 8px; white-space: normal; word-break: break-all; }
.list td svg { width: 20px; height: 20px; flex: none; }
.list tr.it { cursor: default; }
.empty { color: var(--muted); text-align: center; padding: 50px 10px; }
.lockico { width: 12px !important; height: 12px !important; color: var(--muted); }

.ctx { position: fixed; z-index: 50; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25); padding: 5px; min-width: 210px; }
.ctx button { display: flex; width: 100%; gap: 10px; align-items: center; border: 0; background: transparent;
  padding: 8px 10px; border-radius: 6px; cursor: pointer; text-align: left; }
.ctx button:hover:not(:disabled) { background: var(--accent-2); }
.ctx button:disabled { opacity: .4; cursor: default; }
.ctx button svg { width: 17px; height: 17px; }
.ctx button kbd { margin-left: auto; color: var(--muted); font-size: 12px; font-family: inherit; }
.ctx hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.ctx button.danger { color: var(--danger); }

/* ---------- modal / toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 15, 25, .5); z-index: 60; display: grid;
  place-items: center; padding: 16px; }
.modal { background: var(--panel); border-radius: 14px; padding: 20px; width: 100%; max-width: 440px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3); max-height: 92vh; overflow: auto; }
.modal.wide { max-width: 980px; }
.modal h3 { margin: 0 0 10px; }
.modal .acts { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.modal ul { max-height: 160px; overflow: auto; padding-left: 20px; margin: 6px 0; }
.preview-box { display: flex; justify-content: center; align-items: center; min-height: 200px; }
.preview-box img, .preview-box video { max-width: 100%; max-height: 75vh; }
.preview-box iframe { width: 100%; height: 75vh; border: 0; background: #fff; border-radius: 6px; }
.preview-box pre { width: 100%; max-height: 75vh; overflow: auto; background: var(--panel2); padding: 12px;
  border-radius: 8px; white-space: pre-wrap; word-break: break-word; margin: 0; }
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow);
  max-width: 360px; }
.toast.bad { background: var(--danger); color: #fff; }
.progress { position: fixed; left: 16px; bottom: 16px; z-index: 80; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 14px; width: 320px; box-shadow: var(--shadow); }
.placeholder { text-align: center; padding: 60px 20px; color: var(--muted); }
.placeholder svg { width: 64px; height: 64px; color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--panel2); }
.pill.ok { background: rgba(18, 161, 80, .15); } .pill.bad { background: rgba(217, 45, 32, .15); }
.pill.warn { background: rgba(220, 138, 0, .15); }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .app { flex-direction: column; }
  .topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel);
    border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 40; }
  .topbar .brand { padding: 0; }
  .nav { position: fixed; z-index: 45; left: 0; top: 0; height: 100%; transform: translateX(-100%);
    transition: transform .2s; box-shadow: var(--shadow); width: 250px; }
  .nav.open { transform: none; }
  .main { padding: 14px 12px 40px; }
  .ex-tree { position: absolute; z-index: 20; background: var(--panel); height: 100%; box-shadow: var(--shadow);
    width: 240px; }
  .ex-body { position: relative; }
  .ex-tree.mhide { display: none; }
  .ex-toolbar input[type=search] { width: 100%; order: 10; }
  .ex-toolbar .grow { display: none; }
  .ex-toolbar .lbl { display: none; }
  .explorer { height: calc(100vh - 140px); }
  .icons { grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); }
  .list .c-date { display: none; }
}
@media (min-width: 821px) { .only-mobile { display: none !important; } }
