:root {
  --bg: #0e1117;
  --panel: #151a24;
  --panel-2: #1a212e;
  --panel-3: #212a3a;
  --line: rgba(255, 255, 255, 0.07);
  --line-strong: rgba(255, 255, 255, 0.14);
  --text: #e7eaf2;
  --muted: #8a93a6;
  --faint: #5b6577;
  --accent: #36d6c3;
  --accent-dim: rgba(54, 214, 195, 0.14);

  --active: #3ecf8e;
  --inactive: #6b7486;
  --locked: #b78bff;
  --error: #ff6b5b;
  --warn: #f5b14c;

  --radius: 10px;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

.hidden { display: none !important; }

/* ── buttons ─────────────────────────────────────────── */
.btn {
  border: 1px solid var(--line-strong);
  background: var(--panel-2);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  transition: border-color .15s, background .15s, transform .05s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: #062420; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); border-color: var(--line); }
.btn.danger:hover { border-color: var(--error); color: var(--error); }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

input, select, textarea {
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  color: var(--text);
  padding: 9px 11px;
  border-radius: 8px;
  font-size: 13px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label.field { display: block; margin-bottom: 12px; }
label.field span { display: block; color: var(--muted); font-size: 12px; margin-bottom: 5px; }

/* ── login ───────────────────────────────────────────── */
#login {
  height: 100%;
  display: grid;
  place-items: center;
  background:
    radial-gradient(1200px 600px at 70% -10%, rgba(54,214,195,.08), transparent),
    var(--bg);
}
.login-card {
  width: 340px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 32px 28px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.brand .dot { width: 11px; height: 11px; border-radius: 3px; background: var(--accent); box-shadow: 0 0 14px var(--accent); }
.brand b { font-size: 17px; letter-spacing: .5px; }
.brand b span { color: var(--accent); }
.login-card p.sub { color: var(--muted); font-size: 12px; margin: 0 0 22px; }
.login-card .err { color: var(--error); font-size: 12px; min-height: 16px; margin-top: 4px; }

/* ── app shell ───────────────────────────────────────── */
#app { display: grid; grid-template-rows: 52px 1fr; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.topbar .brand b { font-size: 15px; }
.topbar .spacer { flex: 1; }
.who { display: flex; align-items: center; gap: 10px; }
.who .name { font-size: 13px; }
.role-pill {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px;
  padding: 3px 8px; border-radius: 20px; font-weight: 600;
  border: 1px solid var(--line-strong); color: var(--muted);
}
.role-admin { color: var(--accent); border-color: var(--accent); }
.role-teamlead { color: var(--warn); border-color: rgba(245,177,76,.5); }
.role-farmer { color: var(--active); border-color: rgba(62,207,142,.4); }
.role-buyer { color: var(--locked); border-color: rgba(183,139,255,.4); }

.layout { display: grid; grid-template-columns: 256px 1fr; min-height: 0; }
.sidebar {
  border-right: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
  padding: 12px;
}
.nav { display: flex; flex-direction: column; gap: 2px; margin-bottom: 16px; }
.nav button {
  display: flex; align-items: center; gap: 9px;
  background: transparent; border: none; color: var(--muted);
  padding: 9px 10px; border-radius: 8px; text-align: left; font-size: 13.5px; width: 100%;
}
.nav button:hover { background: var(--panel-2); color: var(--text); }
.nav button.active { background: var(--accent-dim); color: var(--accent); }
.nav .ico { width: 16px; text-align: center; opacity: .9; }

.side-h { font-size: 11px; text-transform: uppercase; letter-spacing: .7px; color: var(--faint); padding: 6px 10px; margin-top: 4px; }
.group-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 2px;
}
.group-item:hover { background: var(--panel-2); }
.group-item.active { background: var(--panel-2); outline: 1px solid var(--line-strong); }
.group-item .gi-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.group-item .gi-name { font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-item .gi-count { font-size: 11px; color: var(--faint); font-family: var(--mono); }
.bar { display: flex; height: 4px; border-radius: 3px; overflow: hidden; margin-top: 7px; background: #0c0f15; }
.bar i { display: block; height: 100%; }
.bar i.active { background: var(--active); }
.bar i.inactive { background: var(--inactive); }
.bar i.locked { background: var(--locked); }
.bar i.error { background: var(--error); }

/* ── main ────────────────────────────────────────────── */
.main { overflow-y: auto; padding: 18px 22px; min-height: 0; }
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar h1 { font-size: 18px; margin: 0; font-weight: 600; }
.toolbar .crumb { color: var(--faint); font-weight: 400; }
.toolbar .spacer { flex: 1; }
.toolbar input.search { width: 220px; }
.toolbar select { width: auto; }

.legend { display: flex; gap: 14px; margin-bottom: 12px; color: var(--muted); font-size: 11.5px; }
.legend span { display: flex; align-items: center; gap: 6px; }

/* ── table ───────────────────────────────────────────── */
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--faint); font-weight: 600; padding: 8px 12px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--bg);
}
tbody td { padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:hover { background: var(--panel); }
.p-title { font-weight: 500; }
.p-uuid { font-family: var(--mono); font-size: 11px; color: var(--faint); }
.tagchip {
  display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 20px;
  background: var(--panel-3); color: var(--muted); margin-right: 4px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.st { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; }
.st.active { color: var(--active); } .st.active .dot { background: var(--active); box-shadow: 0 0 8px var(--active); }
.st.inactive { color: var(--muted); } .st.inactive .dot { background: var(--inactive); }
.st.locked { color: var(--locked); } .st.locked .dot { background: var(--locked); }
.st.error { color: var(--error); } .st.error .dot { background: var(--error); }
.row-actions { display: flex; gap: 5px; justify-content: flex-end; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.empty b { color: var(--text); display: block; margin-bottom: 6px; font-size: 15px; }

/* ── modal / drawer ──────────────────────────────────── */
.scrim { position: fixed; inset: 0; background: rgba(4,6,10,.6); display: grid; place-items: center; z-index: 50; }
.modal {
  width: 420px; max-width: calc(100vw - 40px); background: var(--panel);
  border: 1px solid var(--line-strong); border-radius: 14px; padding: 22px;
}
.modal h3 { margin: 0 0 4px; font-size: 16px; }
.modal p.hint { color: var(--muted); font-size: 12px; margin: 0 0 18px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal .err { color: var(--error); font-size: 12px; min-height: 16px; }

.drawer-scrim { position: fixed; inset: 0; background: rgba(4,6,10,.5); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: 420px; max-width: 92vw;
  background: var(--panel); border-left: 1px solid var(--line-strong); z-index: 41;
  display: flex; flex-direction: column;
}
.drawer header { padding: 18px 20px; border-bottom: 1px solid var(--line); }
.drawer header .dt { font-size: 16px; font-weight: 600; }
.drawer header .du { font-family: var(--mono); font-size: 11px; color: var(--faint); margin-top: 4px; }
.drawer .body { padding: 18px 20px; overflow-y: auto; }
.drawer .acts { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: 6px 12px; font-size: 12.5px; margin-bottom: 20px; }
.kv dt { color: var(--faint); }

/* history feed */
.feed-h { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--faint); margin-bottom: 10px; }
.event { display: grid; grid-template-columns: 10px 1fr; gap: 10px; padding-bottom: 14px; position: relative; }
.event::before { content: ""; position: absolute; left: 4px; top: 14px; bottom: -2px; width: 1px; background: var(--line); }
.event:last-child::before { display: none; }
.event .marker { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-top: 5px; }
.event.err .marker { background: var(--error); }
.event .ev-act { font-size: 12.5px; font-weight: 500; }
.event .ev-meta { font-size: 11px; color: var(--faint); margin-top: 2px; }
.event .ev-detail { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-top: 4px; word-break: break-word; }

/* toast */
#toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--panel-3); border: 1px solid var(--line-strong); padding: 10px 16px; border-radius: 10px; font-size: 13px; }
.toast.err { border-color: var(--error); color: #ffd9d3; }
.toast.ok { border-color: var(--active); }

.notice { background: var(--panel); border: 1px solid var(--warn); color: var(--warn);
  padding: 10px 14px; border-radius: 10px; font-size: 12.5px; margin-bottom: 16px; }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

/* ── selection, bulk bar, CRM statuses, sortable headers ─────────────── */
input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }

.bulkbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--panel-2); border: 1px solid var(--accent);
  border-radius: 10px; padding: 9px 14px; margin-bottom: 12px;
}
.bulkbar b { font-size: 13px; color: var(--accent); margin-right: 4px; }
.bulkbar select { width: auto; padding: 6px 9px; font-size: 12.5px; }

.crm-chip { display: inline-flex; align-items: center; gap: 7px; }
.crm-dot { width: 9px; height: 9px; border-radius: 50%; border: 1px solid; flex: 0 0 auto; }
.crm-select { width: auto; min-width: 110px; padding: 5px 8px; font-size: 12.5px; background: var(--panel-2); }

.status-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.status-row input[type="text"], .status-row input:not([type="color"]) { padding: 7px 9px; font-size: 13px; }

.spinner { width: 12px; height: 12px; border: 2px solid rgba(6,36,32,.35); border-top-color: #062420; border-radius: 50%; display: inline-block; animation: spin .6s linear infinite; margin-right: 7px; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.save-flag { font-size: 11.5px; min-width: 86px; }

.range-tabs { display: inline-flex; gap: 6px; }
.stat-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 22px; max-width: 640px; }
.stat-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.stat-card .stat-n { font-size: 26px; font-weight: 600; font-family: var(--mono); }
.stat-card .stat-l { font-size: 12px; color: var(--muted); margin-top: 4px; }
.report-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; max-width: 720px; }
.report-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; }
.report-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.report-row:last-child { border-bottom: none; }
.rr-name { display: inline-flex; align-items: center; gap: 8px; font-size: 13.5px; }
.rr-count { font-family: var(--mono); font-weight: 600; color: var(--accent); }
@media (max-width: 760px) { .stat-cards { grid-template-columns: repeat(2,1fr); } .report-cols { grid-template-columns: 1fr; } }

.custom-range { display: flex; align-items: center; gap: 8px; margin-bottom: 18px; flex-wrap: wrap; font-size: 12.5px; }
.custom-range input { padding: 6px 9px; font-size: 12.5px; }

.gm-label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.gm-list { max-height: 180px; overflow-y: auto; border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 10px; display: flex; flex-direction: column; gap: 6px; }
.gm-item { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }

.gi-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.gi-right { display: inline-flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.gi-del { background: transparent; border: none; color: var(--faint); font-size: 12px; padding: 0 2px; line-height: 1; opacity: 0; transition: opacity .12s, color .12s; }
.group-item:hover .gi-del { opacity: 1; }
.gi-del:hover { color: var(--error); }

.proxy-cell { background: transparent; border: 1px dashed transparent; color: var(--text); font-family: var(--mono); font-size: 12px; padding: 3px 6px; border-radius: 6px; cursor: pointer; text-align: left; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proxy-cell:hover { border-color: var(--line-strong); background: var(--panel-2); }
