:root {
  --bg: #0f1117;
  --panel: #181b24;
  --panel-2: #1f2330;
  --border: #2a2f3d;
  --text: #e6e8ee;
  --muted: #8b91a4;
  --accent: #7c8cff;
  --accent-2: #5563d6;
  --green: #4ade80;
  --red: #f87171;
  --sidebar-w: 64px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85em; }
.error { color: var(--red); min-height: 1.2em; }
code { font-family: "SFMono-Regular", Menlo, Consolas, monospace; }

button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.9em;
  background: var(--accent-2);
  color: white;
}
button.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
button.primary { background: var(--accent); }
button.small { padding: 4px 10px; font-size: 0.8em; }
button:hover { filter: brightness(1.1); }

input[type=text], input[type=password], input {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 0.9em;
}

/* ---------- Auth ---------- */
.auth-view { display: flex; align-items: center; justify-content: center; height: 100vh; }
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px;
  width: 360px;
  text-align: center;
}
.auth-card h1 { margin: 0 0 4px; }
.tabs { display: flex; gap: 8px; margin: 22px 0 14px; }
.tab { flex: 1; background: var(--panel-2); }
.tab.active { background: var(--accent-2); }
#auth-form { display: flex; flex-direction: column; gap: 12px; }

/* ---------- Dashboard ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 22px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.brand { font-weight: 700; }
.topbar-right { display: flex; gap: 14px; align-items: center; }

/* ---------- Sidebar + page body ---------- */
.dash-body {
  display: flex;
  height: calc(100vh - 53px);
}

.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 12px;
}
.sidebar-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
}
.sidebar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 100%;
  padding: 10px 4px;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.72em;
  transition: background 0.15s, color 0.15s;
  border-left: 3px solid transparent;
}
.sidebar-item:hover {
  background: var(--panel-2);
  color: var(--text);
}
.sidebar-item.active {
  color: var(--accent);
  background: var(--panel-2);
  border-left-color: var(--accent);
}
.sidebar-icon { font-size: 1.6em; line-height: 1; }
.sidebar-label { letter-spacing: 0.02em; }

.page-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---------- Config page ---------- */
.config-page {
  padding: 28px 32px;
  max-width: 900px;
}
.page-title {
  margin: 0 0 24px;
  font-size: 1.25em;
}
.config-cards {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.config-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.config-card label {
  font-size: 0.82em;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.token-line { display: flex; gap: 8px; align-items: center; }
.token-line code {
  background: var(--panel-2); padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--border); max-width: 520px; overflow-x: auto; white-space: nowrap; flex: 1;
}
.hint { background: var(--panel-2); padding: 7px 10px; border-radius: 8px; margin: 0; border: 1px solid var(--border); }

/* ---------- Nodes page ---------- */
.grid { display: grid; grid-template-columns: 280px 1fr; gap: 0; height: 100%; }
.nodes-panel { border-right: 1px solid var(--border); background: var(--panel); overflow-y: auto; }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; }
.panel-head h2 { margin: 0; font-size: 1em; }
.nodes-list { list-style: none; margin: 0; padding: 0; }
.node-item {
  padding: 12px 16px; border-top: 1px solid var(--border); cursor: pointer;
}
.node-item:hover { background: var(--panel-2); }
.node-item.active { background: var(--panel-2); border-left: 3px solid var(--accent); }
.node-name { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.online { background: var(--green); }
.node-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.tag { font-size: 0.72em; background: var(--panel); border: 1px solid var(--border); padding: 1px 7px; border-radius: 10px; color: var(--muted); }
.node-meta { font-size: 0.72em; color: var(--muted); margin-top: 4px; }

.work-panel { padding: 18px 22px; overflow-y: auto; }
.placeholder { color: var(--muted); margin-top: 40px; text-align: center; }
.work-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
.work-head h2 { margin: 0; }
.tag-editor { display: flex; gap: 8px; }

.browser { margin-top: 16px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.path-bar { display: flex; gap: 10px; align-items: center; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--border); }
.path-bar code { flex: 1; overflow-x: auto; white-space: nowrap; }
.upload-btn { display: inline-flex; align-items: center; }
.fs-list { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; }
.fs-item { display: flex; justify-content: space-between; padding: 8px 14px; border-top: 1px solid var(--border); cursor: pointer; }
.fs-item:hover { background: var(--panel-2); }
.fs-item .fs-name { display: flex; gap: 8px; align-items: center; }
.fs-item .fs-size { color: var(--muted); font-size: 0.8em; }

.term-wrap { margin-top: 18px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; scroll-margin-top: 20px; }
.term-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 14px; background: var(--panel); }
#terminal { height: 420px; padding: 6px; background: #000; }

/* ---------- View tabs ---------- */
.view-tabs { display: flex; gap: 8px; margin: 16px 0 4px; }
.vtab { background: var(--panel-2); }
.vtab.active { background: var(--accent-2); }

/* ---------- Chat ---------- */
.chat-bar { display: flex; gap: 12px; align-items: center; padding: 10px 0; }
.chat-log {
  border: 1px solid var(--border); border-radius: 12px; padding: 14px;
  height: 360px; overflow-y: auto; background: var(--panel); display: flex; flex-direction: column; gap: 10px;
}
.msg { max-width: 80%; padding: 9px 13px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; line-height: 1.45; }
.msg.user { align-self: flex-end; background: var(--accent-2); color: #fff; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--border); }
.msg.pending { opacity: 0.6; font-style: italic; }
.msg-role { font-size: 0.7em; color: var(--muted); margin-bottom: 3px; text-transform: uppercase; letter-spacing: 0.04em; }
.msg.user .msg-role { color: #dfe3ff; }
.msg.pending { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--border); opacity: 0.6; font-style: italic; padding: 9px 13px; border-radius: 12px; }
.event-line {
  align-self: flex-start; max-width: 85%; font-family: "SFMono-Regular", Menlo, monospace;
  font-size: 0.78em; color: var(--muted); white-space: pre-wrap; word-break: break-word;
  padding: 2px 6px; border-left: 2px solid var(--border); margin-left: 4px;
}
.chat-staged { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { background: var(--panel-2); border: 1px solid var(--border); border-radius: 14px; padding: 3px 10px; font-size: 0.8em; display: inline-flex; gap: 6px; align-items: center; }
.chip-x { cursor: pointer; color: var(--red); }
.attach-btn { display: inline-flex; align-items: center; font-size: 1.1em; }
.chat-input { display: flex; gap: 10px; margin-top: 12px; align-items: flex-end; }
.chat-input textarea {
  flex: 1; resize: vertical; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: 0.92em; font-family: inherit;
}

/* ---------- Sessions ---------- */
.sessions-list { list-style: none; margin: 8px 0 0; padding: 0; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.session-item { padding: 11px 14px; border-top: 1px solid var(--border); cursor: pointer; display: flex; justify-content: space-between; gap: 12px; }
.session-item:first-child { border-top: none; }
.session-item:hover { background: var(--panel-2); }
.session-item .s-title { font-weight: 500; }
.session-item .s-meta { color: var(--muted); font-size: 0.78em; white-space: nowrap; display: flex; gap: 10px; align-items: center; }
.session-item .s-del { color: var(--red); }
.session-detail-bar { display: flex; align-items: center; gap: 12px; margin: 12px 0; }

.toast {
  position: fixed; bottom: 20px; right: 20px; background: var(--panel-2);
  border: 1px solid var(--border); padding: 12px 18px; border-radius: 10px;
  opacity: 0; transition: opacity 0.2s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ========== Mobile back button (hidden on desktop) ========== */
.mobile-back { display: none; }

/* ========== Mobile responsive ========== */
@media (max-width: 768px) {
  /* Sidebar → horizontal bottom bar */
  .dash-body {
    flex-direction: column-reverse;
    height: calc(100vh - 48px);
  }
  .topbar { padding: 10px 14px; }
  .brand { font-size: 0.9em; }

  .sidebar {
    width: 100%;
    min-width: unset;
    height: 52px;
    flex-direction: row;
    border-right: none;
    border-top: 1px solid var(--border);
    padding: 0;
  }
  .sidebar-menu {
    flex-direction: row;
    gap: 0;
    height: 100%;
  }
  .sidebar-menu li { flex: 1; }
  .sidebar-item {
    flex-direction: row;
    gap: 6px;
    padding: 0;
    height: 100%;
    justify-content: center;
    border-left: none;
    border-top: 3px solid transparent;
  }
  .sidebar-item.active {
    border-left-color: transparent;
    border-top-color: var(--accent);
  }
  .sidebar-icon { font-size: 1.2em; }
  .sidebar-label { font-size: 0.8em; }

  .page-content {
    overflow-y: auto;
    height: calc(100vh - 48px - 52px);
  }

  /* Config page */
  .config-page {
    padding: 16px;
    max-width: 100%;
  }
  .config-card { padding: 14px; }
  .token-line { flex-wrap: wrap; }
  .token-line code { max-width: 100%; flex: 1 1 100%; font-size: 0.78em; }

  /* Nodes page: stack vertically */
  .grid {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100%;
  }

  /* Node list - collapsible on mobile */
  .nodes-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: none;
    overflow-y: visible;
  }
  .nodes-panel.mobile-collapsed .nodes-list { display: none; }
  .nodes-panel.mobile-collapsed { border-bottom: 1px solid var(--border); }

  .node-item { padding: 10px 14px; }

  /* Work panel */
  .work-panel { padding: 12px 14px; }
  .work-head { flex-direction: column; align-items: flex-start; gap: 8px; }
  .work-head h2 { font-size: 1em; }
  .tag-editor { width: 100%; }
  .tag-editor input { flex: 1; }

  /* Mobile back button to return to node list */
  .mobile-back {
    display: inline-flex;
    margin-bottom: 8px;
  }

  /* View tabs - scrollable */
  .view-tabs { overflow-x: auto; flex-wrap: nowrap; }
  .vtab { white-space: nowrap; flex-shrink: 0; }

  /* File browser */
  .browser { margin-top: 10px; }
  .path-bar {
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 10px;
  }
  .path-bar code { font-size: 0.78em; order: 10; flex: 1 1 100%; }
  .fs-list { max-height: 200px; }
  .fs-item { padding: 10px 12px; }

  /* Terminal */
  .term-wrap { margin-top: 12px; }
  #terminal { height: 280px; }

  /* Chat */
  .chat-log { height: 300px; padding: 10px; }
  .msg { max-width: 90%; font-size: 0.9em; }
  .chat-input { gap: 6px; }
  .chat-input textarea { font-size: 0.88em; padding: 8px 10px; }

  /* Sessions */
  .session-item { flex-direction: column; gap: 4px; padding: 10px 12px; }
  .session-detail-bar { flex-wrap: wrap; gap: 8px; }
  #session-transcript { height: 300px; }

  /* Placeholder */
  .placeholder { margin-top: 20px; font-size: 0.9em; padding: 0 10px; }
}

/* Extra small screens */
@media (max-width: 400px) {
  .topbar { padding: 8px 10px; }
  .auth-card { width: 95vw; padding: 24px 20px; }
  #terminal { height: 220px; }
  .chat-log { height: 240px; }
  .config-page { padding: 12px; }
}
