/* ------------------------------------------------------------------
   luaclient hub panel — dark theme, no framework, no build step.
   Everything is plain CSS + custom properties. Responsive down to
   ~360px; the instance rail collapses into a drawer under 900px.
   ------------------------------------------------------------------ */

:root {
  color-scheme: dark;

  --bg:        #0d1117;
  --bg-2:      #131a23;
  --bg-3:      #18212c;
  --bg-hover:  #1e2936;
  --panel:     #111820;
  --line:      #26313d;
  --line-2:    #33414f;

  --fg:        #dbe3ec;
  --fg-dim:    #93a2b3;
  --fg-faint:  #66768a;

  --accent:      #4c9aff;
  --accent-dim:  #2f6fbd;
  --accent-ink:  #05121f;

  --ok:      #3fb950;
  --warn:    #d29922;
  --err:     #f0616d;
  --idle:    #6e7d8f;
  --purple:  #a371f7;
  --cyan:    #39c5cf;

  --radius: 7px;
  --radius-sm: 4px;
  --mono: ui-monospace, "Cascadia Mono", "JetBrains Mono", Consolas, "DejaVu Sans Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --rail-w: 232px;
  --shadow: 0 8px 28px rgba(0, 0, 0, .55);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--fg);
  font: 13.5px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* screen-reader-only, but focusable (skip link) */
.sr {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.sr:focus {
  position: fixed; top: 8px; left: 8px; z-index: 400;
  width: auto; height: auto; clip: auto; margin: 0;
  padding: 8px 14px; background: var(--accent); color: var(--accent-ink);
  border-radius: var(--radius-sm); font-weight: 600;
}

/* ---------- generic controls ---------- */

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg-3);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  color: var(--fg);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { background: var(--bg-hover); border-color: #46586b; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent-dim); border-color: var(--accent); color: #eaf3ff; }
.btn.primary:hover:not(:disabled) { background: var(--accent); color: var(--accent-ink); }
.btn.danger { border-color: #7d3038; color: #ffb7bd; }
.btn.danger:hover:not(:disabled) { background: #4a1c22; }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--fg-dim); }
.btn.ghost:hover:not(:disabled) { background: var(--bg-3); color: var(--fg); }

input[type=text], input[type=password], input[type=number],
input[type=search], input[type=datetime-local], select, textarea {
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  padding: 6px 9px;
  width: 100%;
  min-width: 0;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 2px rgba(76, 154, 255, .18);
}
textarea { resize: vertical; font-family: var(--mono); font-size: 12.5px; }
select { cursor: pointer; }

label.field { display: block; margin-bottom: 12px; }
label.field > span.lbl {
  display: block;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--fg-dim);
  margin-bottom: 4px;
}
label.check {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  user-select: none;
}
label.check input { width: auto; accent-color: var(--accent); cursor: pointer; }

.hint { color: var(--fg-faint); font-size: 12px; }
.mono { font-family: var(--mono); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; }
.spacer { flex: 1 1 auto; }

/* ---------- layout ---------- */

#app { height: 100%; display: flex; flex-direction: column; }

#banners { flex: 0 0 auto; }
.banner {
  background: #4a2c07;
  border-bottom: 1px solid #7a4a0c;
  color: #ffd79a;
  padding: 6px 14px;
  font-size: 12.5px;
  display: flex;
  gap: 10px;
  align-items: center;
}
.banner b { color: #ffcf7a; }
.banner.mock {
  background: #1c2f45;
  border-bottom-color: #2f5580;
  color: #a9cdf5;
}
.banner.mock b { color: #d6e9ff; }

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  height: 46px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#topbar .brand {
  font-weight: 700;
  letter-spacing: .02em;
  display: flex;
  align-items: center;
  gap: 8px;
}
#topbar .brand .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
#topbar nav { display: flex; gap: 2px; margin-left: 8px; flex-wrap: wrap; }
#topbar nav a {
  padding: 5px 11px;
  border-radius: var(--radius-sm);
  color: var(--fg-dim);
  font-weight: 500;
}
#topbar nav a:hover { background: var(--bg-3); color: var(--fg); text-decoration: none; }
#topbar nav a.active { background: var(--bg-hover); color: var(--fg); }

#conn-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--fg-dim);
  padding: 3px 9px; border: 1px solid var(--line-2); border-radius: 20px;
}
#conn-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--idle); }
#conn-pill.live .dot { background: var(--ok); box-shadow: 0 0 6px var(--ok); }
#conn-pill.retry .dot { background: var(--warn); }
#conn-pill.down .dot { background: var(--err); }

#body { flex: 1 1 auto; display: flex; min-height: 0; }

#rail {
  flex: 0 0 var(--rail-w);
  width: var(--rail-w);
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#rail .rail-head {
  padding: 9px 12px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--fg-faint);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 6px;
}
#rail-list { overflow-y: auto; flex: 1 1 auto; padding: 6px; }
.rail-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  margin-bottom: 2px;
  cursor: pointer;
}
.rail-item:hover { background: var(--bg-3); }
.rail-item.active { background: var(--bg-hover); border-color: var(--line-2); }
.rail-item .l1 { display: flex; align-items: center; gap: 6px; }
.rail-item .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail-item .l2 { font-size: 11.5px; color: var(--fg-faint); margin-top: 2px; display: flex; gap: 8px; }

#main { flex: 1 1 auto; overflow-y: auto; min-width: 0; padding: 16px; }

.view-head {
  display: flex; align-items: center; gap: 10px;
  flex-wrap: wrap; margin-bottom: 14px;
}
.view-head h1 { font-size: 19px; margin: 0; font-weight: 650; }
.view-head h2 { font-size: 15px; margin: 0; font-weight: 600; color: var(--fg-dim); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}
.card > h3 {
  margin: 0 0 10px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--fg-dim);
  font-weight: 600;
}
.card.warn { border-color: #6a4f13; background: #1a1710; }
.grid { display: grid; gap: 14px; }
.grid.c2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.c3 { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }

/* ---------- pills / bars ---------- */

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 1px 8px; border-radius: 20px;
  font-size: 11.5px; font-weight: 600; letter-spacing: .02em;
  border: 1px solid var(--line-2); color: var(--fg-dim);
  background: var(--bg-3);
  white-space: nowrap;
}
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill.online     { color: #7ee094; border-color: #205c2c; background: #102616; }
.pill.connecting,
.pill.starting   { color: #f0c674; border-color: #6a4f13; background: #29200a; }
.pill.stopping   { color: #f0c674; border-color: #6a4f13; background: #29200a; }
/* the supervisor's richer lifecycle: `running` = the worker is up and the control
   link is established but the character is not in the game yet; `backoff` = it
   exited and the supervisor is waiting before the next restart. */
.pill.running    { color: #9ecbff; border-color: #1f4a72; background: #0e1c2a; }
.pill.backoff    { color: #f0c674; border-color: #6a4f13; background: #29200a; }
.pill.stopped    { color: var(--fg-faint); }
.pill.error      { color: #ff9aa2; border-color: #7d3038; background: #2a1114; }
.pill.on   { color: #7ee094; border-color: #205c2c; background: #102616; }
.pill.off  { color: var(--fg-faint); }

.bar {
  position: relative;
  height: 9px;
  min-width: 64px;
  background: #0a0f15;
  border: 1px solid var(--line);
  border-radius: 20px;
  overflow: hidden;
}
.bar > i { display: block; height: 100%; border-radius: 20px; transition: width .35s ease; }
.bar.hp > i   { background: linear-gradient(90deg, #a4343f, #e05561); }
.bar.mana > i { background: linear-gradient(90deg, #2f5fa8, #5a9bf0); }
.bar.exp > i  { background: linear-gradient(90deg, #6f47b8, #a371f7); }
.bar.sup > i  { background: linear-gradient(90deg, #2f7f52, #4fbf7f); }
.bar.sup.low > i { background: linear-gradient(90deg, #a4343f, #e05561); }
.barwrap { display: flex; flex-direction: column; gap: 3px; min-width: 90px; }
.barwrap .cap { font-size: 10.5px; color: var(--fg-faint); font-family: var(--mono); }

/* ---------- tables ---------- */

.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
table.grid-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  background: var(--panel);
}
table.grid-table th {
  text-align: left;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--fg-faint);
  font-weight: 600;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-2);
  position: sticky; top: 0; z-index: 1;
  white-space: nowrap;
}
table.grid-table td {
  padding: 7px 10px;
  border-bottom: 1px solid #1b232d;
  vertical-align: middle;
}
table.grid-table tbody tr:hover { background: var(--bg-2); }
table.grid-table tbody tr.sel { background: #16283c; }
table.grid-table tbody tr.errrow { background: #23121466; }
table.grid-table td.num { font-family: var(--mono); text-align: right; }

/* ---------- Debug tab (R3): event-kind badges ---------- */
.tag.evk-info  { color: var(--fg-dim); }
.tag.evk-warn  { color: var(--warn); border-color: #6a4f13; background: #23190a; }
.tag.evk-error { color: var(--err);  border-color: #7d3038; background: #2a1114; }
.linkish {
  background: none; border: none; padding: 0;
  color: var(--accent); cursor: pointer; font: inherit; font-weight: 600;
}
.linkish:hover { text-decoration: underline; }

/* ---------- tabs ---------- */

.tabs {
  display: flex; gap: 2px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 14px;
  overflow-x: auto;
}
.tabs button {
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: 7px 13px; color: var(--fg-dim); cursor: pointer; white-space: nowrap;
}
.tabs button:hover { color: var(--fg); }
.tabs button[aria-selected=true] { color: var(--fg); border-bottom-color: var(--accent); }

/* ---------- stats ---------- */

.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .k {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--fg-faint);
}
.stat .v { font-family: var(--mono); font-size: 17px; font-weight: 600; }
.stat .v.pos { color: var(--ok); }
.stat .v.neg { color: var(--err); }
.statgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; }

.kv { display: grid; grid-template-columns: minmax(90px, auto) 1fr; gap: 4px 12px; font-size: 12.5px; }
.kv dt { color: var(--fg-faint); }
.kv dd { margin: 0; font-family: var(--mono); overflow-wrap: anywhere; }

canvas.chart { width: 100%; height: 92px; display: block; }
.chartbox .chead {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 11px; color: var(--fg-faint); margin-bottom: 2px;
}
.chartbox .chead b { color: var(--fg); font-family: var(--mono); font-size: 13px; }

/* ---------- console / chat ---------- */

.logbox {
  background: #080c11;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.45;
  height: 46vh;
  min-height: 220px;
  overflow-y: auto;
  padding: 8px 10px;
}
.logbox .ln { display: flex; gap: 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
.logbox .t { color: #4b5a6d; flex: 0 0 auto; }
.logbox .lv {
  flex: 0 0 auto; width: 54px; text-transform: uppercase;
  font-size: 10.5px; padding-top: 1px; white-space: nowrap; overflow: hidden;
}
.logbox .lv.debug { color: #6f7f92; }
.logbox .lv.info  { color: #5aa0e6; }
.logbox .lv.warn  { color: var(--warn); }
.logbox .lv.error { color: var(--err); }
.logbox .msg { flex: 1 1 auto; }
.logbox .msg.echo { color: var(--purple); }
.logbox .msg.ret  { color: var(--cyan); }
.logbox .who { color: #d8c07a; }

/* ---------- toasts ---------- */

#toasts {
  position: fixed; right: 14px; bottom: 14px; z-index: 300;
  display: flex; flex-direction: column; gap: 8px;
  max-width: min(420px, calc(100vw - 28px));
}
.toast {
  background: var(--bg-3);
  border: 1px solid var(--line-2);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 9px 12px;
  display: flex; gap: 10px; align-items: flex-start;
  animation: slide-in .16s ease-out;
}
.toast.err  { border-left-color: var(--err); }
.toast.ok   { border-left-color: var(--ok); }
.toast.warn { border-left-color: var(--warn); }
.toast .tx { flex: 1 1 auto; overflow-wrap: anywhere; }
.toast .tt { font-weight: 650; margin-bottom: 1px; }
.toast .td { color: var(--fg-dim); font-size: 12px; }
@keyframes slide-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- modal ---------- */

#modal-back {
  position: fixed; inset: 0; z-index: 250;
  background: rgba(3, 6, 10, .68);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
#modal-back[hidden] { display: none; }
.modal {
  background: var(--panel);
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: min(560px, 100%);
  max-height: calc(100vh - 40px);
  display: flex; flex-direction: column;
}
.modal h2 { margin: 0; padding: 13px 16px; font-size: 15px; border-bottom: 1px solid var(--line); }
.modal .mbody { padding: 16px; overflow-y: auto; }
.modal .mfoot {
  padding: 11px 16px; border-top: 1px solid var(--line);
  display: flex; gap: 8px; justify-content: flex-end;
}

/* ---------- login ---------- */

#login-wrap {
  flex: 1 1 auto;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  overflow-y: auto;
}
#login-wrap .box {
  width: min(370px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
}
#login-wrap h1 { margin: 0 0 4px; font-size: 20px; }
#login-wrap .sub { color: var(--fg-faint); font-size: 12.5px; margin-bottom: 18px; }
#login-err {
  background: #2a1114; border: 1px solid #7d3038; color: #ff9aa2;
  border-radius: var(--radius-sm); padding: 7px 10px; margin-bottom: 12px; font-size: 12.5px;
}

/* ---------- misc ---------- */

.empty {
  padding: 30px; text-align: center; color: var(--fg-faint);
  border: 1px dashed var(--line-2); border-radius: var(--radius);
}
.warnbox {
  background: #2b2109; border: 1px solid #6a4f13; color: #f0d295;
  border-radius: var(--radius-sm); padding: 9px 12px; font-size: 12.5px; margin-bottom: 12px;
}
.tag {
  display: inline-block; padding: 0 6px; border-radius: 3px;
  background: var(--bg-3); border: 1px solid var(--line-2);
  font-size: 11px; font-family: var(--mono); color: var(--fg-dim);
  margin: 0 3px 3px 0;
}
.tag.role-admin { color: #d3a7ff; border-color: #57407a; background: #1f1630; }
.tag.default { color: var(--warn); border-color: #6a4f13; background: #23190a; }

/* ---------- bot config editors (CONFIGAPI.md) ---------- */

.cfglist { display: flex; flex-direction: column; gap: 10px; }
.cfgrow {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px; background: var(--bg-2);
}
.cfgrow .field { margin-bottom: 8px; }
.cfgrow .field > span.lbl { font-size: 10px; }
.cfghead { font-weight: 600; }
.cfggroup {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--fg-faint); margin: 8px 0 4px;
}
.cfggroup:first-child { margin-top: 0; }
.cfgcard-foot { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }

#rail-toggle { display: none; }

@media (max-width: 900px) {
  #rail {
    position: fixed; z-index: 200; top: 0; bottom: 0; left: 0;
    box-shadow: var(--shadow);
    transform: translateX(-100%);
    transition: transform .16s ease;
  }
  body.rail-open #rail { transform: none; }
  #rail-toggle { display: inline-flex; }
  #main { padding: 12px; }
  #topbar nav a { padding: 5px 8px; }
}
@media (max-width: 620px) {
  #topbar { height: auto; flex-wrap: wrap; padding: 6px 10px; gap: 8px; }
  .stat .v { font-size: 15px; }
}
