:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #efeeeb;
  --border: #dddcd7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --grid: #e6e5e1;
  --good: #008300;
  --warn: #b97800;
  --bad: #c43c3b;
  --idle: #9a9993;
  --user-bubble: #e6effa;
  --code-bg: #f0efec;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --series-1: #3987e5;
    --grid: #2a2a27;
    --good: #0ca30c;
    --warn: #d9a400;
    --bad: #e66767;
    --idle: #6c6b66;
    --user-bubble: #1f2c3d;
    --code-bg: #232321;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86; --accent: #3987e5;
  --series-1: #3987e5; --grid: #2a2a27; --good: #0ca30c; --warn: #d9a400;
  --bad: #e66767; --idle: #6c6b66; --user-bubble: #1f2c3d; --code-bg: #232321;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--text-3); }
.error { color: var(--bad); min-height: 1.2em; margin: 0; }
code, pre, .mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 12.5px; }
pre { white-space: pre-wrap; word-break: break-word; background: var(--code-bg); padding: 8px 10px; border-radius: 6px; margin: 6px 0; max-height: 420px; overflow: auto; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 20px; }
h3 { font-size: 15px; }

/* ---------- buttons & inputs ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font: inherit;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 3px 9px; font-size: 12.5px; }
.btn.link { border: none; background: none; color: var(--text-2); padding: 0; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%;
}
textarea { resize: vertical; min-height: 90px; }
label { display: flex; flex-direction: column; gap: 4px; color: var(--text-2); font-size: 13px; }

/* ---------- login ---------- */
.login { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.login-card p { margin: -8px 0 0; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }
.key-drop {
  display: flex; flex-direction: column; gap: 6px; align-items: center; text-align: center;
  border: 1.5px dashed var(--border); border-radius: 12px; padding: 18px 14px; cursor: pointer;
  background: var(--surface-2);
}
.key-drop:hover, .key-drop:focus-visible, .key-drop.over { border-color: var(--accent); outline: none; }
.key-drop strong { color: var(--accent); }
.key-drop .muted { font-size: 12.5px; }
.key-drop.busy { opacity: .6; pointer-events: none; }
.login-card .login-or { display: flex; align-items: center; gap: 10px; color: var(--text-3); font-size: 12.5px; margin: 0; }
.login-or::before, .login-or::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 270px 1fr; height: 100%; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px; font-weight: 600; font-size: 15px; }
.logo { width: 18px; height: 18px; border-radius: 50%; background: var(--accent); }
.ws-state { width: 8px; height: 8px; border-radius: 50%; background: var(--idle); margin-left: auto; }
.ws-state.on { background: var(--good); }
nav { display: flex; flex-direction: column; padding: 0 8px; }
.nav-item {
  text-align: left; border: none; background: none; color: var(--text); font: inherit;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
}
.nav-item:hover, .emp-item:hover { background: var(--surface-2); }
.nav-item.active, .emp-item.active { background: var(--surface-2); font-weight: 600; }
.side-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 18px 16px 6px; color: var(--text-3); font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
}
.emp-list { flex: 1; overflow: auto; padding: 0 8px; }
.emp-item {
  display: grid; grid-template-columns: 10px 1fr; gap: 2px 10px; align-items: center;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; border: none; background: none;
  width: 100%; text-align: left; color: var(--text); font: inherit;
}
.emp-item .sub { grid-column: 2; font-size: 12px; color: var(--text-3); }
.side-foot { display: flex; justify-content: space-between; padding: 12px 16px; border-top: 1px solid var(--border); font-size: 13px; }

.main { overflow: auto; padding: 24px 28px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .spacer { flex: 1; }
.meta { color: var(--text-3); font-size: 13px; }

/* ---------- status ---------- */
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.dot.offline { background: var(--idle); opacity: .55; }
.dot.idle { background: var(--warn); }
.dot.online { background: var(--good); }
.dot.working { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 30%, transparent); }
.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px;
  background: var(--surface-2); font-size: 12.5px; color: var(--text-2);
}

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tab {
  border: none; background: none; font: inherit; color: var(--text-2); padding: 9px 14px;
  border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- cards / tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.tile .label { color: var(--text-3); font-size: 12.5px; }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .hint { color: var(--text-3); font-size: 12px; margin-top: 2px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.card h3 { margin-bottom: 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.notice { background: var(--surface-2); border-radius: 10px; padding: 10px 14px; color: var(--text-2); font-size: 13px; margin-bottom: 16px; }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-3); font-weight: 500; font-size: 12.5px; }
td.num, th.num { text-align: right; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }

/* ---------- segmented ---------- */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: none; background: var(--surface); color: var(--text-2); font: inherit; padding: 5px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* ---------- chats ---------- */
.chats { display: grid; grid-template-columns: 320px 1fr; gap: 16px; height: calc(100vh - 210px); min-height: 400px; }
.sess-list { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.sess-search { padding: 10px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.sess-item { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.sess-item:hover { background: var(--surface-2); }
.sess-item.active { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.sess-item .t { font-weight: 500; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.sess-item .s { font-size: 12px; color: var(--text-3); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; display: flex; flex-direction: column; }
.conv-head { padding: 12px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.conv-body { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; }
.msg.user { align-self: flex-end; }
.msg .bubble { padding: 9px 13px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; }
.msg.user .bubble { background: var(--user-bubble); }
.msg.assistant .bubble { background: var(--surface-2); }
.msg .when { font-size: 11.5px; color: var(--text-3); margin: 2px 4px; }
.msg.user .when { text-align: right; }
.msg.side { opacity: .75; }
details.blk { border: 1px solid var(--border); border-radius: 8px; padding: 5px 10px; background: var(--surface); }
details.blk summary { cursor: pointer; color: var(--text-2); font-size: 13px; }
details.blk.err summary { color: var(--bad); }
.empty { color: var(--text-3); padding: 40px; text-align: center; }

/* ---------- remote control ---------- */
.run-form { display: grid; gap: 12px; }
.run-form .row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.run-out { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 8px; max-height: 70vh; overflow: auto; }
.run-line { font-size: 13.5px; }
.run-line .tag { font-size: 11.5px; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; }
.status-pill { padding: 2px 9px; border-radius: 99px; font-size: 12px; background: var(--surface-2); }
.status-pill.running, .status-pill.queued { color: var(--accent); }
.status-pill.done { color: var(--good); }
.status-pill.error, .status-pill.lost, .status-pill.rejected { color: var(--bad); }

/* ---------- chart ---------- */
.chart { width: 100%; height: 220px; display: block; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; }
.chart .bar { fill: var(--series-1); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { filter: brightness(1.15); }
.tooltip {
  position: fixed; pointer-events: none; z-index: 50; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font-size: 12.5px;
  box-shadow: 0 4px 16px rgb(0 0 0 / .12);
}
.bar-row { display: grid; grid-template-columns: 140px 1fr 60px; gap: 10px; align-items: center; margin: 5px 0; font-size: 13px; }
.bar-row .track { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; }

dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 22px; width: min(620px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / .35); }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.copy-box { display: flex; gap: 8px; align-items: stretch; }
.copy-box pre { flex: 1; margin: 0; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; height: auto; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .emp-list { max-height: 220px; }
  .main { padding: 16px; }
  .chats { grid-template-columns: 1fr; height: auto; }
  .sess-list { max-height: 260px; }
  .conv { min-height: 60vh; }
  .grid-2, .run-form .row { grid-template-columns: 1fr; }
}
