:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --panel-2: #f8f9fc;
  --border: #e3e7ef;
  --text: #1d2433;
  --muted: #6b7489;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --ok: #16a34a;
  --ok-soft: #e7f7ec;
  --warn: #d97706;
  --err: #dc2626;
  --err-soft: #fdecec;
  --log-bg: #0f172a;
  --log-text: #cbd5e1;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1320;
    --panel: #171c2c;
    --panel-2: #1c2235;
    --border: #2a3147;
    --text: #e6e9f2;
    --muted: #8d95ab;
    --accent: #6d9bff;
    --accent-soft: #1e2a4a;
    --ok: #4ade80;
    --ok-soft: #173024;
    --warn: #fbbf24;
    --err: #f87171;
    --err-soft: #3a1a1f;
    --log-bg: #0a0d17;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
    --bg: #0f1320;
    --panel: #171c2c;
    --panel-2: #1c2235;
    --border: #2a3147;
    --text: #e6e9f2;
    --muted: #8d95ab;
    --accent: #6d9bff;
    --accent-soft: #1e2a4a;
    --ok: #4ade80;
    --ok-soft: #173024;
    --warn: #fbbf24;
    --err: #f87171;
    --err-soft: #3a1a1f;
    --log-bg: #0a0d17;
    --shadow: none;
  }

:root { color-scheme: light; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { color-scheme: dark; } }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 14px/1.5 "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
}
a { color: var(--accent); }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { font-weight: 700; font-size: 15px; padding: 0 10px 4px; }
.brand-sub { color: var(--muted); font-size: 12px; padding: 0 10px 12px; }
.company-switch { display: block; margin: 0 6px 16px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel-2); }
.company-switch span { display: block; font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.company-switch select { width: 100%; font-weight: 600; font-size: 13px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; margin-bottom: 2px;
}
.nav a:hover { background: var(--panel-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .sep { height: 1px; background: var(--border); margin: 10px 6px; }
.nav .ico { width: 20px; text-align: center; }
/* Kéo thả đổi thứ tự chức năng (riêng từng tài khoản) */
.nav a[draggable="true"] { cursor: grab; }
.nav a.dragging { opacity: .45; outline: 1px dashed var(--accent); }
.nav-reset { background: none; border: 0; padding: 0; font: inherit; font-size: 12px; color: var(--accent); cursor: pointer; }
.nav-reset:hover { text-decoration: underline; }
.user-box { margin: 14px 6px 0; padding: 10px; border-top: 1px solid var(--border); font-size: 12.5px; line-height: 1.7; }
.user-box .muted { color: var(--muted); }
.theme-switch { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin: 10px 6px 0; padding: 10px 10px 0;
  border-top: 1px solid var(--border); }
.theme-switch span { grid-column: 1 / -1; font-size: 11.5px; color: var(--muted); margin-bottom: 2px; }
.theme-switch button { min-width: 0; padding: 5px 2px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2);
  color: var(--text); font: inherit; font-size: 11.5px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.theme-switch button:hover { border-color: var(--accent); }
.theme-switch button.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.field input[readonly] { background: var(--bg); color: var(--muted); cursor: default; }
.provider-box {margin: 10px 6px 0; padding: 10px 10px 4px; border-top: 1px solid var(--border); font-size: 11.5px;
  line-height: 1.6; color: var(--muted); overflow-wrap: anywhere; }
.provider-box a { color: inherit; }
.provider-foot { margin-top: 16px; text-align: center; font-size: 12px; line-height: 1.7; color: var(--muted); }
.provider-foot a { color: inherit; }
/* phần chỉ quản trị thấy (trang Cài đặt) */
body:not(.is-admin) [data-admin] { display: none !important; }

/* ---------- Main ---------- */
main { flex: 1; padding: 26px 32px 40px; max-width: 1180px; min-width: 0; }
h1 { font-size: 22px; margin: 0 0 4px; }
.subtitle { color: var(--muted); margin: 0 0 22px; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  margin-bottom: 16px;
}
.card h2 { font-size: 15px; margin: 0 0 14px; display: flex; align-items: center; gap: 8px; }
.card h2 .hint { font-weight: 400; color: var(--muted); font-size: 12.5px; }
.card h3 { font-size: 13.5px; margin: 20px 0 10px; padding-top: 14px; border-top: 1px dashed var(--border); display: flex; align-items: center; gap: 8px; }
.card h3 .hint { font-weight: 400; color: var(--muted); font-size: 12.5px; }

.grid { display: grid; gap: 12px 16px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
input[type=text], input[type=password], input[type=date], input[type=number], textarea, select {
  font: inherit; color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.pw { position: relative; }
.pw input { padding-right: 36px; }
.pw button {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  border: 0; background: transparent; cursor: pointer; color: var(--muted); padding: 4px 6px;
}
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; }

/* Công tắc bật/tắt */
.auto-grid { display: grid; gap: 12px 24px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.switch-row { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; }
.switch-row b { display: block; font-size: 14px; }
.switch-row small { display: block; color: var(--muted); font-size: 12.5px; margin-top: 2px; line-height: 1.45; }
.switch { position: relative; flex: none; width: 42px; height: 24px; margin-top: 1px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--border); transition: background .15s; pointer-events: none; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform .15s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.success { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--err); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
[hidden] { display: none !important; }
.btn.active, .btn.active:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ---------- Log & results ---------- */
.log {
  background: var(--log-bg); color: var(--log-text);
  font: 12.5px/1.55 Consolas, "Cascadia Mono", monospace;
  border-radius: 8px; padding: 12px 14px;
  height: 340px; overflow: auto; white-space: pre-wrap; margin: 0;
}
.status-pill {
  display: inline-block; font-size: 12px; font-weight: 600;
  padding: 2px 10px; border-radius: 99px; background: var(--panel-2); color: var(--muted);
}
.status-pill.running { background: var(--accent-soft); color: var(--accent); }
.status-pill.done { background: var(--ok-soft); color: var(--ok); }
.status-pill.error, .status-pill.stopped { background: var(--err-soft); color: var(--err); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; background: var(--panel-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 5px; background: var(--accent-soft); color: var(--accent); }
.empty { color: var(--muted); text-align: center; padding: 18px; }

/* ---------- Dashboard tiles ---------- */
.tiles { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.tile {
  display: block; text-decoration: none; color: var(--text);
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow); transition: border-color .15s, transform .15s;
}
.tile:hover { border-color: var(--accent); transform: translateY(-2px); }
.tile .big { font-size: 26px; }
.tile h3 { margin: 8px 0 4px; font-size: 15px; }
.tile p { margin: 0; color: var(--muted); font-size: 13px; }
.tile .last { margin-top: 12px; font-size: 12px; color: var(--muted); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 99;
  display: flex; flex-direction: column; gap: 8px;
}
#toast div {
  background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12); max-width: 380px;
}
#toast div.ok { border-left-color: var(--ok); }
#toast div.err { border-left-color: var(--err); }

/* ---------- MST ---------- */
.kv { display: grid; grid-template-columns: 170px 1fr auto; gap: 0; }
.kv > div { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.kv .k { font-weight: 600; }
.kv .v { white-space: pre-wrap; word-break: break-word; }
.kv .v.good { color: var(--ok); font-weight: 600; }
.kv .v.hl { color: var(--warn); }

@media (max-width: 760px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; border-right: 0; border-bottom: 1px solid var(--border); }
  .nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .nav .sep { display: none; }
  main { padding: 18px 16px; }
  .kv { grid-template-columns: 110px 1fr auto; }
}

/* Menu chỉ dành cho một số vai trò (vd Sao lưu Google Drive: Chủ doanh nghiệp + quản trị) */
.nav a[data-roles] { display: none; }
body[data-role="owner"] .nav a[data-roles~="owner"], body[data-role="admin"] .nav a[data-roles~="admin"] { display: flex; }
