/* FKBuffer admin — compact chat + fixed n8n-style flow canvas */
:root {
  --bg: #0a0a0f;
  --surface: #12121a;
  --surface2: #181824;
  --border: #2a2a3a;
  --text: #e8e8f0;
  --muted: #8b8ba8;
  --accent: #7c6af7;
  --ok: #34d399;
  --warn: #fbbf24;
  --gen: #60a5fa;
  --val: #a78bfa;
  --mem: #34d399;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --font: "Inter", system-ui, sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { font-family: var(--font); background: var(--bg); color: var(--text); display: flex; flex-direction: column; }

.top {
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0.6rem;
  padding: 0.5rem 1rem; border-bottom: 1px solid var(--border); background: var(--surface);
}
.back { color: var(--muted); text-decoration: none; font-size: 0.75rem; }
.top h1 { font-size: 1rem; margin-top: 0.15rem; }
.sub { font-size: 0.78rem; color: var(--muted); }
.turn { font-size: 0.68rem; color: var(--accent); font-family: var(--mono); }
.top__controls { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: flex-end; }
.top__controls label { font-size: 0.68rem; color: var(--muted); display: flex; flex-direction: column; gap: 0.15rem; }
.top__controls input {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 0.3rem 0.5rem; border-radius: 5px; font-size: 0.72rem; min-width: 160px;
}
.btn { background: var(--accent); color: #fff; border: none; padding: 0.38rem 0.75rem; border-radius: 5px; cursor: pointer; font-size: 0.78rem; }
.btn--sm { font-size: 0.68rem; padding: 0.18rem 0.4rem; margin-left: 0.35rem; background: var(--border); }

.layout {
  flex: 1;
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(220px, 32vh);
  min-height: 0;
  gap: 0;
}
.top-band {
  display: grid;
  grid-template-columns: minmax(168px, 188px) 1fr;
  gap: 0.6rem;
  padding: 0.6rem;
  min-height: 0;
  overflow: hidden;
}
@media (max-width: 900px) { .top-band { grid-template-columns: 1fr; } }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.6rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel h2 { font-size: 0.78rem; font-weight: 600; margin-bottom: 0.35rem; flex: 0 0 auto; }
.panel-hint { font-size: 0.65rem; color: var(--muted); margin-bottom: 0.3rem; }

/* Memory panel — title, counts, tabs on one row (desktop) */
.storage-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.75rem;
  flex: 0 0 auto;
  margin-bottom: 0.35rem;
}
.storage-toolbar h2 { margin-bottom: 0; white-space: nowrap; }
.storage-toolbar .panel-hint {
  margin-bottom: 0;
  flex: 1 1 auto;
  min-width: 12rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@media (min-width: 901px) {
  .storage-toolbar { flex-wrap: nowrap; }
  .storage-toolbar .panel-hint { flex: 1 1 0; }
}
@media (max-width: 900px) {
  .storage-toolbar { flex-direction: column; align-items: flex-start; }
  .storage-toolbar .panel-hint { white-space: normal; min-width: 0; }
}
.layout > .panel--table {
  margin: 0 0.6rem 0.6rem;
  min-height: 0;
  overflow: hidden;
}
.panel--table .storage-body {
  flex: 1 1 0;
  min-height: 160px;
}

/* Compact Telegram */
.panel--chat { max-width: 240px; }
.chat-log {
  flex: 1;
  min-height: 0;
  max-height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.68rem;
  line-height: 1.35;
}
.bubble {
  padding: 0.28rem 0.45rem;
  border-radius: 6px;
  word-break: break-word;
  max-width: 100%;
}
.bubble--user { align-self: flex-end; background: #1e293b; border: 1px solid #334155; }
.bubble--agent { align-self: flex-start; background: var(--surface2); border: 1px solid var(--border); }
.bubble time { display: block; font-size: 0.58rem; color: var(--muted); font-family: var(--mono); margin-bottom: 0.1rem; }

/* Flow viz — 3-tier transaction layout (docs/admin/flow-ui.md) */
.panel--flow { min-width: 0; }
.panel--flow h2 { margin-bottom: 0.35rem; flex: 0 0 auto; }

.flow-viz {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1.05fr) minmax(0, 1.35fr) minmax(0, 0.85fr);
  gap: 0.45rem;
  overflow: hidden;
}

.flow-tier {
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(24, 24, 36, 0.95), rgba(10, 10, 15, 0.98));
  overflow: hidden;
}
.flow-tier__label {
  flex: 0 0 auto;
  font-size: 0.58rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  padding: 0.35rem 0.55rem 0.2rem;
  border-bottom: 1px solid rgba(42, 42, 58, 0.6);
}

/* Tier 1 — Agent rack (chief + helpers) */
.flow-tier--agent { border-color: #6366f1; box-shadow: inset 0 1px 0 rgba(99, 102, 241, 0.25); }
.agent-rack {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 0.8fr) minmax(0, 0.8fr);
  gap: 0.45rem;
  padding: 0.45rem 0.55rem 0.4rem;
}
.agent-card {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding: 0.4rem 0.45rem;
  border-radius: 8px;
  border: 1px solid rgba(99, 102, 241, 0.22);
  background: rgba(12, 12, 22, 0.55);
}
.agent-card--lead {
  display: grid;
  grid-template-columns: minmax(120px, 28%) 1fr;
  grid-template-rows: auto 1fr;
  gap: 0.35rem 0.5rem;
  border-color: rgba(99, 102, 241, 0.45);
}
.agent-card--helper {
  opacity: 0.72;
  border-style: dashed;
}
.agent-card--helper.is-suggested {
  opacity: 1;
  border-style: solid;
  border-color: rgba(167, 139, 250, 0.55);
  box-shadow: inset 0 0 0 1px rgba(167, 139, 250, 0.15);
}
.agent-card--helper.is-busy {
  opacity: 1;
  border-style: solid;
  border-color: var(--accent);
}
.agent-card--helper.is-done {
  opacity: 0.88;
  border-color: rgba(52, 211, 153, 0.45);
}
.agent-card__head { grid-column: 1 / -1; }
.agent-card__title-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.agent-card__title {
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #e8e8f0;
}
.agent-card--lead .agent-card__title {
  font-size: 0.95rem;
  background: linear-gradient(90deg, #e8e8f0, #a5b4fc);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.agent-card__badge {
  font-size: 0.55rem;
  font-weight: 600;
  padding: 0.12rem 0.4rem;
  border-radius: 999px;
  background: rgba(99, 102, 241, 0.18);
  border: 1px solid rgba(99, 102, 241, 0.45);
  color: #c7d2fe;
  font-family: var(--mono);
}
.agent-card__meta {
  font-size: 0.58rem;
  color: var(--muted);
  margin-top: 0.15rem;
  font-family: var(--mono);
  line-height: 1.35;
}
.agent-card__status {
  font-size: 0.56rem;
  color: #a5b4fc;
  font-family: var(--mono);
  margin: 0;
  min-height: 1.2em;
  line-height: 1.35;
}
.agent-card__spin {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(139, 139, 168, 0.45);
  border-top-color: var(--accent);
  border-radius: 50%;
  flex-shrink: 0;
  display: none;
}
.agent-card.is-busy .agent-card__spin { display: block; animation: spin 0.65s linear infinite; }
.agent-card__user { min-height: 0; display: flex; flex-direction: column; }
.agent-steps {
  list-style: none;
  margin: 0;
  padding: 0;
  min-height: 0;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.58rem;
  line-height: 1.45;
  border-left: 2px solid rgba(124, 106, 247, 0.35);
  padding-left: 0.45rem;
}
.agent-steps li {
  padding: 0.12rem 0;
  color: #b8b8d0;
  display: flex;
  gap: 0.35rem;
  align-items: baseline;
}
.agent-steps li time { color: var(--muted); flex-shrink: 0; font-size: 0.52rem; }
.agent-steps li.is-active { color: #e8e8f0; }
.agent-steps li.is-active::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  flex-shrink: 0;
  align-self: center;
  animation: pulse-dot 1s ease infinite;
}
.agent-steps li.is-done { opacity: 0.72; }
@keyframes pulse-dot { 50% { opacity: 0.45; transform: scale(0.85); } }

/* Shared pane */
.pane-label {
  display: block;
  font-size: 0.48rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.15rem;
}
.pane-body {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0.35rem 0.45rem;
  font-family: var(--mono);
  font-size: 0.52rem;
  line-height: 1.4;
  color: #c4c4dc;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid #32324a;
  border-radius: 5px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  scrollbar-width: thin;
  scrollbar-color: #3d3d52 transparent;
}
.pane-body--sm { max-height: 4.5rem; }
.pane-body--data { flex: 1; min-height: 3rem; max-height: 100%; }

/* Tier 2 — LLM rack */
.flow-tier--llm { border-color: rgba(96, 165, 250, 0.35); }
.llm-rack {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.4rem;
  padding: 0.35rem 0.45rem 0.45rem;
  overflow-x: auto;
}
@media (max-width: 1100px) {
  .llm-rack { grid-template-columns: repeat(3, minmax(160px, 1fr)); }
}
.llm-card {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid #3a3a52;
  border-radius: 7px;
  background: rgba(12, 12, 20, 0.85);
  transition: border-color 0.15s, box-shadow 0.15s, opacity 0.2s;
  opacity: 0.55;
}
.llm-card.is-used { opacity: 1; }
.llm-card.is-busy {
  border-color: var(--gen);
  box-shadow: 0 0 0 1px rgba(96, 165, 250, 0.35), 0 4px 20px rgba(0, 0, 0, 0.35);
  opacity: 1;
}
.llm-card.is-done { border-color: #5b6b8a; opacity: 1; }
.llm-card__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.2rem 0.35rem;
  padding: 0.35rem 0.4rem;
  border-bottom: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
  position: relative;
}
.llm-card__spin {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(139, 139, 168, 0.4);
  border-top-color: var(--gen);
  border-radius: 50%;
  display: none;
}
.llm-card.is-busy .llm-card__spin { display: block; animation: spin 0.65s linear infinite; }
.llm-card__name { font-size: 0.68rem; font-weight: 600; color: #e2e8f0; }
.llm-card__model {
  font-size: 0.5rem;
  font-family: var(--mono);
  color: #93c5fd;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.llm-card__host {
  font-size: 0.48rem;
  padding: 0.08rem 0.35rem;
  border-radius: 999px;
  font-family: var(--mono);
  background: rgba(52, 211, 153, 0.12);
  border: 1px solid rgba(52, 211, 153, 0.35);
  color: #6ee7b7;
}
.llm-card__host.host-cloud {
  background: rgba(167, 139, 250, 0.12);
  border-color: rgba(167, 139, 250, 0.35);
  color: #c4b5fd;
}
.llm-io {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 0.25rem;
  padding: 0.3rem;
}
.llm-io__pane {
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.llm-io__pane .pane-body { max-height: 100%; }

/* Tier 3 — Data rack */
.flow-tier--data { border-color: rgba(52, 211, 153, 0.3); }
.data-rack {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.4rem;
  padding: 0.35rem 0.45rem 0.45rem;
}
@media (max-width: 900px) {
  .data-rack { grid-template-columns: repeat(2, 1fr); }
  .agent-rack { grid-template-columns: 1fr; }
  .agent-card--lead { grid-template-columns: 1fr; }
}
.data-card {
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid #3a3a52;
  border-radius: 7px;
  background: rgba(12, 12, 20, 0.85);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.data-card.is-busy {
  border-color: var(--mem);
  box-shadow: 0 0 0 1px rgba(52, 211, 153, 0.3);
}
.data-card__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.4rem;
  padding: 0.3rem 0.4rem;
  border-bottom: 1px solid var(--border);
  position: relative;
}
.data-card__spin {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(139, 139, 168, 0.4);
  border-top-color: var(--mem);
  border-radius: 50%;
  display: none;
}
.data-card.is-busy .data-card__spin { display: block; animation: spin 0.65s linear infinite; }
.data-card__name { font-size: 0.65rem; font-weight: 600; }
.data-card__sub { font-size: 0.48rem; color: var(--muted); font-family: var(--mono); }
.data-feed {
  list-style: none;
  margin: 0;
  padding: 0.25rem 0.35rem;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 0.5rem;
  line-height: 1.35;
}
.data-feed li {
  padding: 0.2rem 0;
  border-bottom: 1px solid rgba(42, 42, 58, 0.5);
  color: #b0b0c8;
  word-break: break-word;
}
.data-feed li:last-child { border-bottom: none; }
.data-card .pane-body--data { margin: 0.25rem 0.35rem 0.35rem; flex: 1; }

@keyframes spin { to { transform: rotate(360deg); } }

.table-wrap { flex: 1; overflow: auto; min-height: 0; }
table { width: 100%; border-collapse: collapse; font-size: 0.68rem; }
th, td { text-align: left; padding: 0.3rem 0.4rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); position: sticky; top: 0; background: var(--surface); font-size: 0.65rem; }
td.sig { font-family: var(--mono); font-size: 0.58rem; color: #a5b4fc; max-width: 12rem; word-break: break-word; }
td.mono { font-family: var(--mono); font-size: 0.58rem; color: var(--muted); }


.storage-tabs { display: flex; flex-wrap: wrap; gap: 0.35rem; margin: 0; flex: 0 0 auto; }
.storage-toolbar .storage-tabs { margin-left: auto; }
@media (max-width: 900px) {
  .storage-toolbar .storage-tabs { margin-left: 0; }
}
.storage-tab {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--border, #333);
  background: transparent;
  color: inherit;
  border-radius: 4px;
  cursor: pointer;
}
.storage-tab.is-active { background: var(--accent, #3b82f6); border-color: var(--accent, #3b82f6); color: #fff; }

/* Fixed-height tab region — panels stack in the same box (no layout jump). */
.storage-body {
  position: relative;
  flex: 1 1 0;
  min-height: 160px;
  overflow: hidden;
}
.storage-panel {
  display: none;
  position: absolute;
  inset: 0;
  overflow: auto;
  min-height: 0;
}
.storage-panel.is-active { display: block; }
.storage-panel.table-wrap { flex: unset; }

.storage-subhead {
  font-size: 0.7rem;
  font-weight: 600;
  margin: 0.5rem 0 0.25rem;
  color: var(--muted, #94a3b8);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.storage-panel[data-panel="semantic"] .storage-subhead:first-child { margin-top: 0; }


/* ── Mobile: scrollable stacked layout (admin panel only) ── */
@media (max-width: 768px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body {
    display: block;
  }

  .top {
    position: sticky;
    top: 0;
    z-index: 20;
    flex-direction: column;
    align-items: stretch;
    padding: 0.55rem 0.75rem;
    gap: 0.5rem;
  }

  .top h1 {
    font-size: 0.95rem;
  }

  .top__controls {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
  }

  .top__controls label {
    width: 100%;
    font-size: 0.72rem;
  }

  .top__controls input {
    min-width: 0;
    width: 100%;
    font-size: 16px;
    padding: 0.45rem 0.55rem;
  }

  .btn {
    width: 100%;
    min-height: 44px;
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
  }

  .layout {
    display: flex;
    flex-direction: column;
    flex: none;
    min-height: auto;
    overflow: visible;
    gap: 0.5rem;
  }

  .top-band {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 0.5rem;
    min-height: auto;
    overflow: visible;
  }

  .panel {
    min-height: auto;
    overflow: visible;
  }

  .panel--chat {
    max-width: none;
    min-height: 10rem;
    max-height: 38vh;
  }

  .chat-log {
    max-height: 36vh;
    -webkit-overflow-scrolling: touch;
  }

  .panel--flow {
    min-height: auto;
  }

  .flow-viz {
    display: flex;
    flex-direction: column;
    flex: none;
    min-height: auto;
    overflow: visible;
    gap: 0.5rem;
  }

  .flow-tier {
    flex: none;
    min-height: auto;
    overflow: visible;
  }

  .agent-rack {
    grid-template-columns: 1fr;
    padding: 0.4rem;
    gap: 0.4rem;
    overflow: visible;
  }

  .agent-card--lead {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }

  .agent-card__title {
    font-size: 0.78rem;
  }

  .agent-card--lead .agent-card__title {
    font-size: 0.88rem;
  }

  .agent-steps {
    max-height: 10rem;
    -webkit-overflow-scrolling: touch;
  }

  .llm-rack {
    grid-template-columns: 1fr;
    overflow: visible;
    padding: 0.35rem;
    gap: 0.45rem;
  }

  .llm-card {
    min-height: 11rem;
    opacity: 1;
  }

  .llm-io {
    min-height: 8rem;
  }

  .llm-io__pane .pane-body {
    min-height: 3.5rem;
    max-height: 6rem;
  }

  .data-rack {
    grid-template-columns: 1fr;
    overflow: visible;
    padding: 0.35rem;
    gap: 0.45rem;
  }

  .data-card {
    min-height: 6rem;
  }

  .data-card .pane-body--data {
    min-height: 4rem;
    max-height: 8rem;
  }

  .data-feed {
    max-height: 8rem;
    -webkit-overflow-scrolling: touch;
  }

  .pane-body {
    font-size: 0.58rem;
  }

  .layout > .panel--table {
    margin: 0 0.5rem 1rem;
    min-height: auto;
    overflow: visible;
    flex: none;
  }

  .storage-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
  }

  .storage-toolbar h2 {
    white-space: normal;
    font-size: 0.82rem;
  }

  .storage-toolbar .panel-hint {
    white-space: normal;
    min-width: 0;
    font-size: 0.68rem;
  }

  .storage-toolbar .storage-tabs {
    margin-left: 0;
    width: 100%;
    gap: 0.3rem;
  }

  .storage-tab {
    flex: 1 1 calc(50% - 0.2rem);
    min-height: 40px;
    font-size: 0.72rem;
    padding: 0.35rem 0.4rem;
    text-align: center;
  }

  .storage-body {
    min-height: 240px;
    max-height: 55vh;
  }

  .table-wrap {
    -webkit-overflow-scrolling: touch;
  }

  table {
    font-size: 0.72rem;
  }

  th,
  td {
    padding: 0.4rem 0.45rem;
    white-space: nowrap;
  }

  td.sig,
  td.mono {
    max-width: 9rem;
    white-space: normal;
  }
}

@media (max-width: 480px) {
  .top {
    padding: 0.45rem 0.55rem;
  }

  .top-band,
  .layout > .panel--table {
    padding-left: 0.4rem;
    padding-right: 0.4rem;
  }

  .layout > .panel--table {
    margin-left: 0.4rem;
    margin-right: 0.4rem;
  }

  .agent-card__badge {
    font-size: 0.5rem;
  }

  .flow-tier__label {
    font-size: 0.55rem;
  }

  .storage-tab {
    flex: 1 1 100%;
  }
}
