:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --line: #e3e7ee;
  --line-strong: #cfd6e1;
  --text: #17202e;
  --muted: #637085;
  --accent: #1f6feb;
  --accent-soft: #e8f0fe;
  --good: #188a42;
  --warn: #b26a00;
  --bad: #c62828;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
button, input, select, textarea { font: inherit; color: inherit; }
code { background: #fff3; padding: 0 4px; border-radius: 4px; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.brand-mark {
  display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px;
  background: var(--accent); color: #fff; font-size: 12px; letter-spacing: .5px;
}
.range { display: flex; gap: 6px; align-items: center; }
#custom-range { display: inline-flex; gap: 6px; }
.spacer { flex: 1; }
.select {
  height: 34px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 6px; background: #fff; max-width: 100%;
}
.topbar form { margin: 0; }

.btn {
  height: 34px; padding: 0 14px; border-radius: 6px; border: 1px solid var(--line-strong);
  background: #fff; cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: #f0f3f7; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #1a5fd0; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: #eef1f5; }
.btn.small { height: 28px; padding: 0 10px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }

.banner { background: #fff7e0; color: #6b4b00; padding: 8px 16px; border-bottom: 1px solid #f0dc9c; font-size: 13px; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; padding: 14px 16px 4px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 20px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }

/* ---------- tabs + toolbar ---------- */
.tabs { display: flex; gap: 4px; padding: 10px 16px 0; overflow-x: auto; }
.tab {
  border: 1px solid var(--line); border-bottom: none; background: #eef1f5; color: var(--muted);
  padding: 9px 16px; border-radius: 8px 8px 0 0; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.tab.active { background: var(--panel); color: var(--text); }
.chip {
  display: inline-flex; align-items: center; gap: 4px; margin-left: 6px; padding: 1px 6px 1px 8px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent); font-size: 12px;
}
.chip b { cursor: pointer; font-weight: 700; padding: 0 2px; }
.toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin: 0 16px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-bottom: none;
}
.search { width: 240px; }
.check { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); cursor: pointer; }
.bulk { display: flex; gap: 6px; align-items: center; }
.bulk span { color: var(--muted); margin-right: 4px; }

/* ---------- table ---------- */
.table-wrap {
  margin: 0 16px 24px; background: var(--panel); border: 1px solid var(--line); overflow: auto;
  max-height: calc(100vh - 260px); min-height: 200px;
}
.grid { border-collapse: separate; border-spacing: 0; width: 100%; font-variant-numeric: tabular-nums; }
.grid th, .grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.grid th {
  position: sticky; top: 0; z-index: 2; background: #f8fafc; font-size: 12px; color: var(--muted); font-weight: 600;
  cursor: pointer; user-select: none; white-space: normal; min-width: 92px; vertical-align: bottom; line-height: 1.25;
}
.grid th.sorted { color: var(--text); }
.grid tfoot td { position: sticky; bottom: 0; background: #f8fafc; font-weight: 700; border-top: 1px solid var(--line-strong); }
.grid .left { text-align: left; }
.grid tbody tr:hover td { background: #f9fbff; }
.grid tr.selected td { background: var(--accent-soft); }
.grid .sticky1, .grid .sticky2, .grid .sticky3 { position: sticky; z-index: 1; background: inherit; }
.grid td.sticky1, .grid td.sticky2, .grid td.sticky3 { background: var(--panel); }
.grid tr.selected td.sticky1, .grid tr.selected td.sticky2, .grid tr.selected td.sticky3 { background: var(--accent-soft); }
.grid th.sticky1, .grid th.sticky2, .grid th.sticky3 { z-index: 3; background: #f8fafc; }
.grid tfoot td.sticky1, .grid tfoot td.sticky2, .grid tfoot td.sticky3 { z-index: 3; background: #f8fafc; }
.sticky1 { left: 0; width: 40px; }
.sticky2 { left: 40px; width: 56px; }
.sticky3 { left: 96px; border-right: 1px solid var(--line); }

.name-cell { display: flex; align-items: center; gap: 10px; min-width: 240px; max-width: 360px; }
.name-cell img { width: 38px; height: 38px; border-radius: 6px; object-fit: cover; flex: none; background: #eee; }
.name-cell .stack { min-width: 0; }
.name-link { color: var(--accent); font-weight: 600; cursor: pointer; overflow: hidden; text-overflow: ellipsis; display: block; }
.name-link:hover { text-decoration: underline; }
.sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.row-actions { display: inline-flex; gap: 2px; margin-top: 2px; }
.row-actions button { border: none; background: none; color: var(--muted); font-size: 12px; padding: 0 6px 0 0; cursor: pointer; }
.row-actions button:hover { color: var(--accent); }

.toggle { position: relative; width: 36px; height: 20px; display: inline-block; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle span { position: absolute; inset: 0; background: #c4ccd8; border-radius: 20px; transition: .15s; cursor: pointer; }
.toggle span::before {
  content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .15s;
  box-shadow: 0 1px 2px #0003;
}
.toggle input:checked + span { background: var(--accent); }
.toggle input:checked + span::before { transform: translateX(16px); }
.toggle input:disabled + span { opacity: .5; cursor: wait; }

.status { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa5b4; }
.dot.good { background: var(--good); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }

.budget { cursor: pointer; border-bottom: 1px dashed var(--line-strong); }
.budget:hover { color: var(--accent); border-color: var(--accent); }
.budget-input { width: 90px; height: 28px; text-align: right; }
.inline-name { width: 100%; height: 28px; }

.empty { padding: 40px; text-align: center; color: var(--muted); }
.skeleton td { color: transparent !important; }
.skeleton td::after { content: ""; display: block; height: 10px; border-radius: 4px; background: linear-gradient(90deg, #eef1f5, #f7f9fb, #eef1f5); }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: #0f172a66; display: grid; place-items: center; padding: 16px; }
.modal-card {
  width: min(760px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
  background: var(--panel); border-radius: 12px; box-shadow: 0 20px 60px #0004;
}
.modal-card.narrow { width: min(480px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 18px; overflow: auto; }
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.steps { display: flex; gap: 0; border-bottom: 1px solid var(--line); }
.steps span { flex: 1; text-align: center; padding: 9px 6px; color: var(--muted); font-weight: 600; font-size: 13px; border-bottom: 2px solid transparent; }
.steps span.on { color: var(--accent); border-color: var(--accent); }
.steps span.done { color: var(--good); }
.steps span.off { opacity: .4; }

.field { margin-bottom: 14px; }
.field > label, .field > .label { display: block; font-weight: 600; margin-bottom: 5px; }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 4px; }
.field input[type=text], .field input[type=number], .field input[type=url], .field input[type=datetime-local],
.field select, .field textarea {
  width: 100%; min-height: 36px; padding: 7px 10px; border: 1px solid var(--line-strong); border-radius: 6px; background: #fff;
}
.field textarea { min-height: 70px; resize: vertical; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.choice { border: 1px solid var(--line-strong); border-radius: 8px; padding: 10px 12px; cursor: pointer; }
.choice input { margin-right: 6px; }
.choice small { display: block; color: var(--muted); margin-top: 3px; font-size: 12px; }
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.multi-item { display: flex; gap: 6px; margin-bottom: 6px; }
.multi-item textarea, .multi-item input { flex: 1; }
.add-link { border: none; background: none; color: var(--accent); cursor: pointer; padding: 0; font-weight: 600; }
.remove-x { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.tag { background: var(--accent-soft); color: var(--accent); border-radius: 12px; padding: 3px 10px; font-size: 12px; }
.tag b { cursor: pointer; margin-left: 4px; }
.suggest { border: 1px solid var(--line); border-radius: 6px; margin-top: 4px; max-height: 180px; overflow: auto; }
.suggest div { padding: 6px 10px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.suggest div:hover { background: var(--accent-soft); }
.suggest small { color: var(--muted); }
.upload-box {
  border: 2px dashed var(--line-strong); border-radius: 10px; padding: 18px; text-align: center; color: var(--muted); cursor: pointer;
}
.upload-box:hover { border-color: var(--accent); color: var(--accent); }
.upload-box img, .upload-box video { max-width: 220px; max-height: 220px; border-radius: 8px; display: block; margin: 0 auto 8px; }
.enh-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 6px 14px; }
.check.enh { color: var(--text); font-size: 13px; align-items: flex-start; }
.check.enh input { margin-top: 2px; }
.summary-box { background: #f8fafc; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.warn-box { background: #fff7e0; border-color: #f0dc9c; color: #6b4b00; }
.error { color: var(--bad); margin: 0; font-size: 13px; }
.muted { color: var(--muted); }

.peek-img { width: 100%; border-radius: 8px; margin-bottom: 10px; }
.peek-text { white-space: pre-wrap; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: #17202e; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px #0003; max-width: 380px; font-size: 13px; }
.toast.bad { background: var(--bad); }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 26px; display: flex; flex-direction: column; gap: 10px; }
.login-card input { height: 40px; padding: 0 12px; border: 1px solid var(--line-strong); border-radius: 6px; }
.login-card label { font-weight: 600; }
.login-card .btn { height: 40px; margin-top: 6px; }

/* ---------- phones ---------- */
@media (max-width: 720px) {
  .topbar { padding: 8px 10px; gap: 6px; }
  .brand-name, .hide-sm { display: none; }
  #account { order: 10; flex: 1 1 100%; min-width: 0; }
  .kpis { padding: 10px 10px 0; grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .kpi { padding: 8px; }
  .kpi .value { font-size: 15px; }
  .tabs { padding: 10px 10px 0; }
  .toolbar, .table-wrap { margin-left: 10px; margin-right: 10px; }
  .search { width: 100%; }
  .table-wrap { max-height: none; }
  .name-cell { min-width: 170px; max-width: 220px; }
  .row2, .row3 { grid-template-columns: 1fr; }
}
