:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --text: #16181d;
  --muted: #646b78;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --ok: #15803d;
  --ok-soft: #e7f6ec;
  --fail: #c2410c;
  --fail-soft: #fdeee6;
  --danger: #b91c1c;
  --new: #fff7d6;
  --type-setdata: #7c3aed;
  --type-transfer: #0e7490;
  --type-dispersion: #b45309;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2b303a;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --accent: #6d9bff;
    --accent-soft: #1d2a45;
    --ok: #4ade80;
    --ok-soft: #13291c;
    --fail: #fb923c;
    --fail-soft: #33200f;
    --danger: #f87171;
    --new: #3a3215;
    --type-setdata: #b794ff;
    --type-transfer: #4cc9e0;
    --type-dispersion: #f2b35c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
/* Component display rules must not override the hidden attribute the script toggles. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

code, pre { font-family: var(--mono); font-size: 12.5px; }
h1, h2, h3 { margin: 0; }
.muted { color: var(--muted); }

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
  white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.ghost { background: transparent; }
button.ghost:hover { background: var(--surface-2); }
button.danger { color: var(--danger); }
button.icon { padding: 4px 9px; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 34px; height: 34px; padding: 6px; flex: none;
  border-radius: 9px; color: #fff; background: var(--accent);
}
.brand h1 { font-size: 17px; font-weight: 650; }
.subtitle { margin: 0; color: var(--muted); font-size: 12.5px; overflow-wrap: anywhere; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.live { display: inline-flex; align-items: center; gap: 8px; font-weight: 550; }
.live-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.live[aria-pressed="true"] .live-dot { background: var(--ok); animation: pulse 1.8s ease-out infinite; }
.live.error .live-dot { background: var(--danger); animation: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 55%, transparent); }
  100% { box-shadow: 0 0 0 9px transparent; }
}

/* Filters */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px 24px;
}
.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.segmented button { border: 0; padding: 5px 12px; border-radius: 7px; background: transparent; }
.segmented button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.search {
  flex: 1 1 260px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
}
.search svg { width: 16px; height: 16px; flex: none; }
.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  padding: 8px 0;
  font: inherit;
  color: var(--text);
  background: transparent;
}
.toolbar-end { display: flex; align-items: center; gap: 12px; margin-left: auto; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  padding: 0 24px 24px;
  align-items: start;
}
.layout.with-detail { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); }

.list-pane, .detail-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
}
.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.callbacks tbody tr { cursor: pointer; }
.callbacks tbody tr:hover { background: var(--surface-2); }
.callbacks tbody tr.selected { background: var(--accent-soft); }
.callbacks tbody tr.new { animation: arrive 4s ease-out; }
@keyframes arrive { from { background: var(--new); } to { background: transparent; } }
.callbacks td { white-space: nowrap; }
.callbacks .ident { font-family: var(--mono); font-size: 12.5px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.callbacks .msg { display: block; color: var(--muted); font-size: 12px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid currentColor;
}
.badge.type-SetData { color: var(--type-setdata); }
.badge.type-Transfer { color: var(--type-transfer); }
.badge.type-Dispersion { color: var(--type-dispersion); }
.badge.type-other { color: var(--muted); }

.result { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; padding: 1px 8px; border-radius: 6px; font-size: 12.5px; }
.result.ok { color: var(--ok); background: var(--ok-soft); }
.result.fail { color: var(--fail); background: var(--fail-soft); }
.result .code { font-family: var(--mono); font-weight: 500; opacity: .85; }

.empty { padding: 48px 24px; text-align: center; }
.empty-title { font-size: 16px; font-weight: 600; margin: 0 0 6px; }
.load-older { display: block; margin: 10px auto; }

/* Detail */
.detail-pane { padding: 18px; position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow: auto; }
.detail-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.detail-head h2 { font-size: 16px; }
.detail-pane h3 { font-size: 13px; margin: 18px 0 8px; }
.fields { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 16px; margin: 0; }
.fields dt { color: var(--muted); }
.fields dd { margin: 0; overflow-wrap: anywhere; }
.fields dd.mono { font-family: var(--mono); font-size: 12.5px; }
.items { border: 1px solid var(--border); border-radius: 8px; }
.items td { font-size: 13px; }
.items .mono { font-family: var(--mono); font-size: 12.5px; }
.kv { display: inline-block; margin: 0 4px 3px 0; padding: 0 6px; border-radius: 5px; background: var(--surface-2); font-family: var(--mono); font-size: 12px; }
.raw-head { display: flex; align-items: center; justify-content: space-between; margin-top: 18px; }
.raw-head h3 { margin: 0; }
.raw {
  margin: 8px 0 0;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: auto;
  max-height: 420px;
  white-space: pre;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
}

/* Login */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.login-card .brand { margin-bottom: 22px; }
.login-card label { display: block; font-weight: 550; margin-bottom: 6px; }
.login-card input {
  width: 100%;
  padding: 9px 11px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.login-card button { width: 100%; margin-top: 14px; padding: 9px; border: 0; background: var(--accent); color: #fff; font-weight: 600; }
.login-card button:hover { background: var(--accent); filter: brightness(1.08); }
.login-error { color: var(--danger); margin: 10px 0 0; }

@media (max-width: 1000px) {
  .layout.with-detail { grid-template-columns: minmax(0, 1fr); }
  .detail-pane { position: static; max-height: none; }
}

@media (max-width: 640px) {
  .topbar, .toolbar { padding-left: 16px; padding-right: 16px; }
  .layout { padding: 0 16px 16px; }
  .toolbar-end { margin-left: 0; width: 100%; justify-content: space-between; }
  .segmented { width: 100%; overflow-x: auto; }
  .subtitle { display: none; }
}
