:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #1a1f29;
  --muted: #667085;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --user-bubble: #eef4ff;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);

  --c-function: #0e7490;  --c-function-bg: #e6f6f9;
  --c-task: #7c3aed;      --c-task-bg: #f3eeff;
  --c-basic: #15803d;     --c-basic-bg: #eaf7ee;
  --c-intermediate: #b45309; --c-intermediate-bg: #fdf3e3;
  --c-advanced: #be123c;  --c-advanced-bg: #fdecef;
  --c-kw: #475467;        --c-kw-bg: #f2f4f7;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #171b23;
    --surface-2: #1f2430;
    --border: #2a3140;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --accent: #8b87ff;
    --accent-soft: #24234a;
    --user-bubble: #1c2640;
    --danger: #f97066;
    --danger-soft: #3a1d1b;
    --shadow: none;
    --c-function: #67d4e8;  --c-function-bg: #10323a;
    --c-task: #c4a7ff;      --c-task-bg: #2a2045;
    --c-basic: #6ee7a0;     --c-basic-bg: #123322;
    --c-intermediate: #fbbf5b; --c-intermediate-bg: #3a2a10;
    --c-advanced: #fb7196;  --c-advanced-bg: #3d1622;
    --c-kw: #c4cad6;        --c-kw-bg: #262c38;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; font-size: 15px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.logo {
  display: inline-grid; place-items: center;
  width: 26px; height: 26px; border-radius: 7px;
  background: var(--accent); color: #fff; font-size: 14px;
}
.tabs { display: flex; gap: 4px; flex: 1; }
.tabs a {
  padding: 6px 12px; border-radius: 8px; color: var(--muted); font-weight: 550;
}
.tabs a:hover { background: var(--surface-2); text-decoration: none; }
.tabs a.active { background: var(--accent-soft); color: var(--accent); }

.container { max-width: 1280px; margin: 0 auto; padding: 24px; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; }

/* ---------- buttons, inputs ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 12px; border-radius: 8px; cursor: pointer; font: inherit;
}
.btn:hover { background: var(--surface-2); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn:disabled { opacity: .45; cursor: default; }
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border);
  padding: 7px 10px; border-radius: 8px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0; }
.filters input[type=search] { flex: 1 1 260px; }

/* ---------- cards, tiles ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px; box-shadow: var(--shadow);
}
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin: 16px 0 24px; }
.tile .label { color: var(--muted); font-size: 12px; font-weight: 550; text-transform: uppercase; letter-spacing: .04em; }
.tile .value { font-size: 26px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { color: var(--muted); font-size: 12px; }

.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; margin-bottom: 24px; }

.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 130px 1fr 36px; align-items: center; gap: 10px; font-size: 13px; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-track, .bar-fill { display: block; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 99px; }
.bar-row .num { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
.cell-title { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 12px; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.pager { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; color: var(--muted); }
.pager .btns { display: flex; gap: 8px; }

/* ---------- tag chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 550; white-space: nowrap;
  background: var(--c-kw-bg); color: var(--c-kw);
}
.chip .k { opacity: .7; font-weight: 500; }
.chip.function { background: var(--c-function-bg); color: var(--c-function); }
.chip.task { background: var(--c-task-bg); color: var(--c-task); }
.chip.basic { background: var(--c-basic-bg); color: var(--c-basic); }
.chip.intermediate { background: var(--c-intermediate-bg); color: var(--c-intermediate); }
.chip.advanced { background: var(--c-advanced-bg); color: var(--c-advanced); }
.chip.sensitive, .chip.error { background: var(--danger-soft); color: var(--danger); }
.chip.pending { background: var(--surface-2); color: var(--muted); font-style: italic; }

/* ---------- chat detail ---------- */
.back { display: inline-block; margin-bottom: 12px; }
.chat-head { display: grid; grid-template-columns: 1fr auto; gap: 16px; margin-bottom: 16px; }
.chat-head .use-case { color: var(--muted); margin: 4px 0 10px; }
.meta-list { display: grid; grid-template-columns: auto auto; gap: 2px 14px; font-size: 13px; }
.meta-list dt { color: var(--muted); }
.meta-list dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.thread { display: flex; flex-direction: column; gap: 12px; }
.turn { padding: 0; overflow: hidden; }
.turn-head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px;
  padding: 8px 14px; background: var(--surface-2); border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.turn-head .idx { font-weight: 650; color: var(--text); }
.turn-body { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.bubble { padding: 10px 12px; border-radius: 10px; white-space: pre-wrap; word-break: break-word; }
.bubble.user { background: var(--user-bubble); }
.bubble.assistant { background: var(--surface-2); }
.bubble .who { display: block; font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 4px; }
.bubble.clamped { max-height: 180px; overflow: hidden; position: relative; }
.bubble.clamped::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 48px;
  background: linear-gradient(transparent, var(--surface-2));
}
.expand { align-self: flex-start; font-size: 12px; }
.turn-tags { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; padding: 10px 14px; border-top: 1px dashed var(--border); }
.turn-tags .topic { font-weight: 600; }

.empty { padding: 40px; text-align: center; color: var(--muted); }
.error-box { padding: 12px 14px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px 16px; padding: 10px 16px; }
  .container { padding: 16px; }
  .chat-head { grid-template-columns: 1fr; }
  .bar-row { grid-template-columns: 100px 1fr 30px; }
}

/* ---------- users page ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; align-items: end; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.notice { padding: 12px 14px; border-radius: 10px; margin-top: 12px; }
.notice.ok { background: var(--c-basic-bg); color: var(--c-basic); }
.notice.err { background: var(--danger-soft); color: var(--danger); }
.secret { display: inline-flex; align-items: center; gap: 8px; margin-top: 6px; }
.secret code {
  font: 600 14px Consolas, monospace; padding: 4px 10px; border-radius: 6px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
}
.budget-edit { display: flex; gap: 6px; align-items: center; }
.budget-edit input { width: 90px; }
.budget-edit select { width: 130px; }
.meter { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 6px; width: 160px; }
.meter > span { display: block; height: 100%; background: var(--c-basic); }
.meter.warn > span { background: var(--c-intermediate); }
.meter.over > span { background: var(--danger); }
.role { font-size: 11px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }

/* ---------- sign-in ---------- */
.session { display: flex; align-items: center; gap: 8px; }
.lang-select { width: auto; padding: 3px 6px; font-size: 12px; }
.login-card .lang-select { justify-self: start; }
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; box-sizing: border-box; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 16px;
}
.login-card h1 { font-size: 20px; margin: 0; }
.login-card form { display: grid; gap: 12px; }
.login-card .field span { font-size: 12px; font-weight: 600; color: var(--muted); }
.login-card input { width: 100%; box-sizing: border-box; }
.login-card .error { margin: 0; padding: 10px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: 13px; }
.login-btn { text-align: center; text-decoration: none; padding: 10px 14px; }

/* ---------- people ---------- */
.status { font-size: 11px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; }
.status.off { color: var(--danger); }
.status.admin { color: var(--accent); }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-actions .btn { padding: 4px 8px; font-size: 12px; }
tr.editing td { background: var(--surface-2); }
.edit-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; align-items: end; }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; }
tr.inactive td { opacity: .55; }

/* ---------- groups ---------- */
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.stack { display: grid; gap: 10px; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.chip.group { background: var(--surface-2); color: var(--text); text-decoration: none; margin-top: 4px; }
.chip.group:hover { background: var(--border); }
.group-picks { grid-column: 1 / -1; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }

/* ---------- loading ---------- */
.loading-bar { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 20; overflow: hidden; background: transparent; }
.loading-bar[hidden] { display: none; }
.loading-bar::before {
  content: ""; position: absolute; top: 0; bottom: 0; width: 35%;
  background: var(--accent); border-radius: 99px; animation: loading-slide 1.1s ease-in-out infinite;
}
@keyframes loading-slide { from { left: -35%; } to { left: 100%; } }
.spinner-wrap { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 64px 0; color: var(--muted); }
.spinner {
  width: 20px; height: 20px; border-radius: 50%;
  border: 2.5px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#view.busy { opacity: .6; transition: opacity .15s .1s; pointer-events: none; }
@media (prefers-reduced-motion: reduce) {
  .loading-bar::before, .spinner { animation-duration: 3s; }
}
.budget-origin { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font: inherit; text-decoration: underline; }

/* ---------- person details ---------- */
.person-head { display: grid; grid-template-columns: auto 1fr auto; gap: 16px; align-items: center; margin-bottom: 16px; }
.person-head h1 { margin: 0; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 650;
}
.avatar.sm { width: 28px; height: 28px; font-size: 12px; }
.avatar.lg { width: 56px; height: 56px; font-size: 24px; }
.person-actions { justify-content: flex-end; }
.person-link { color: inherit; text-decoration: none; display: block; }
.person-link:hover { color: var(--accent); text-decoration: underline; }
.meta-list.left dd { text-align: left; }
.meter.wide { width: 100%; }
@media (max-width: 720px) {
  .person-head { grid-template-columns: auto 1fr; }
  .person-actions { grid-column: 1 / -1; justify-content: flex-start; }
}

/* ---------- friendlier layout ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.page-head h1 { margin-bottom: 4px; }
.page-head p { margin: 0; }
.page-head .btn { flex: none; }
.add-box { margin-bottom: 16px; border-color: var(--accent); }
.add-head { display: flex; align-items: center; justify-content: space-between; }
.add-head h2 { margin: 0 0 8px; }
.icon-btn { background: none; border: 0; color: var(--muted); font-size: 16px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar input[type=search] { flex: 1 1 240px; min-width: 200px; }
.segmented { display: inline-flex; flex-wrap: wrap; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button {
  background: var(--surface); color: var(--muted); border: 0; border-right: 1px solid var(--border);
  padding: 7px 12px; font: inherit; font-size: 13px; cursor: pointer;
}
.segmented button:last-child { border-right: 0; }
.segmented button:hover { color: var(--text); }
.segmented button.on { background: var(--accent); color: #fff; }
.who { display: flex; align-items: center; gap: 10px; }
.badges { display: flex; flex-wrap: wrap; gap: 4px; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--c-basic-bg); color: var(--c-basic); }
.badge.admin { background: var(--c-function-bg); color: var(--c-function); }
.badge.off { background: var(--danger-soft); color: var(--danger); }
.badge.pending { background: var(--c-intermediate-bg); color: var(--c-intermediate); }
td.go { color: var(--muted); font-size: 20px; width: 24px; text-align: center; vertical-align: middle; }
tr.clickable:hover td.go { color: var(--accent); }
td.right { text-align: right; }
.budget-col { min-width: 170px; }
.text-danger { color: var(--danger); font-weight: 600; }
.tiny { font-size: 11px; }
.num-inline { font-weight: 600; }
.btn.subtle { border-color: transparent; color: var(--muted); }
.btn.subtle:hover { color: var(--danger); border-color: var(--danger); }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; }
.card-head h2 { margin: 0; }
.inline-forms { display: flex; flex-wrap: wrap; gap: 10px; }
form.inline { display: flex; gap: 6px; align-items: center; }
form.inline select { max-width: 240px; }
.input-prefix {
  display: inline-flex; align-items: center; gap: 4px; padding-left: 10px; color: var(--muted);
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}
.input-prefix input { border: 0; background: transparent; width: 90px; padding-left: 2px; }
.input-prefix input:focus { outline: none; }
details.card > summary { cursor: pointer; list-style: none; }
details.card > summary::before { content: "▸ "; color: var(--muted); }
details.card[open] > summary::before { content: "▾ "; }
a.tile.link { color: inherit; text-decoration: none; transition: border-color .15s; }
a.tile.link:hover { border-color: var(--accent); }
.attention { margin-bottom: 16px; border-left: 4px solid var(--c-intermediate); }
.attention h2 { margin-bottom: 6px; }
.attention ul { margin: 0; padding-left: 18px; display: grid; gap: 6px; font-size: 14px; }
.attention li.warn strong { color: var(--danger); }

#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 50; display: grid; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px;
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--c-basic);
  animation: toast-in .2s ease-out; transition: opacity .4s, transform .4s;
}
.toast.err { border-left-color: var(--danger); }
.toast.hide { opacity: 0; transform: translateY(8px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

@media (max-width: 720px) {
  .page-head { flex-direction: column; }
  table.people th:nth-child(2), table.people td:nth-child(2),
  table.people th:nth-child(4), table.people td:nth-child(4) { display: none; }
}

/* ---------- reports ---------- */
.adoption { display: flex; align-items: center; gap: 8px; }
.adoption .meter { width: 80px; margin: 0; }
.bar-track.inline { display: inline-block; width: 90px; vertical-align: middle; margin-right: 6px; }

/* ---------- health ---------- */
.health-pill {
  font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 99px; text-decoration: none;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.health-pill.ok { background: var(--c-basic-bg); color: var(--c-basic); }
.health-pill.warn { background: var(--c-intermediate-bg); color: var(--c-intermediate); }
.health-pill.down { background: var(--danger-soft); color: var(--danger); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--c-basic); }
.dot.warn { background: var(--c-intermediate); }
.dot.down { background: var(--danger); }
.health-banner { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.health-banner.down { border-left: 4px solid var(--danger); }
.health-banner.warn { border-left: 4px solid var(--c-intermediate); }
.health-banner.ok { border-left: 4px solid var(--c-basic); }
.health-head { display: flex; align-items: center; gap: 8px; }
.health-head h2 { margin: 0; }
.health-summary { margin: 8px 0 4px; }
.health-card.down { border-color: var(--danger); }

/* ---------- Tenfold Panel ---------- */
body.locked .tabs, body.locked #refresh { visibility: hidden; }
.login-card .field { display: grid; gap: 4px; }
