:root {
  --bg: #0b0d10;
  --surface: #12151a;
  --surface2: #1a1f27;
  --border: #2a3140;
  --text: #e8ecf1;
  --muted: #8b95a8;
  --accent: #5b8def;
  --accent2: #3dd68c;
  --warn: #f0b429;
  --danger: #f07178;
  --floor: #1e2430;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app { min-height: 100vh; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.mascot {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  object-fit: cover;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
}

.subtitle {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.badge {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: #1a2e1f;
  color: var(--accent2);
  border: 1px solid #2a4a35;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
}

.btn:hover { border-color: var(--accent); }

.btn.primary {
  background: linear-gradient(135deg, #3d5afe, #5b8def);
  border-color: transparent;
  color: white;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: calc(100vh - 73px);
}

.agents-panel {
  border-right: 1px solid var(--border);
  background: var(--floor);
  padding: 1rem;
}

.agents-panel h2,
.panel-header h2 {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 600;
}

.panel-hint {
  margin: 0.25rem 0 1rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.agent-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.agent-card {
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: default;
  transition: background 0.15s;
}

.agent-card:hover,
.agent-card.active {
  background: var(--surface2);
  border-color: var(--border);
}

.agent-card.off-floor { opacity: 0.85; }

.agent-avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-size: 0.7rem;
  font-weight: 700;
  flex-shrink: 0;
}

.agent-card.floor .agent-avatar { background: #243049; color: #9ec5ff; }
.agent-card.off-floor .agent-avatar { background: #2d2438; color: #c9a0ff; }

.agent-name { font-size: 0.82rem; font-weight: 600; }
.agent-desc {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0.1rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.cursor-hint {
  margin-top: 1.25rem;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px dashed var(--border);
  font-size: 0.72rem;
  color: var(--muted);
}

.cursor-hint code {
  background: var(--surface2);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  color: var(--accent);
}

.main {
  padding: 1rem 1.25rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow: auto;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.meta {
  font-size: 0.75rem;
  color: var(--muted);
}

.feed-panel,
.market-panel,
.brief-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
}

.activity-feed {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 220px;
  overflow: auto;
}

.feed-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.65rem;
  align-items: start;
  padding: 0.6rem 0.7rem;
  border-radius: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
}

.feed-agent {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}

.feed-body {
  font-size: 0.8rem;
}

.feed-time {
  font-size: 0.68rem;
  color: var(--muted);
  white-space: nowrap;
}

.table-wrap { overflow: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

th, td {
  text-align: left;
  padding: 0.45rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

th { color: var(--muted); font-weight: 500; }

tr:hover td { background: var(--surface2); }

.pct-up { color: var(--accent2); }
.pct-down { color: var(--danger); }

.brief-content {
  max-height: 360px;
  overflow: auto;
  font-size: 0.82rem;
  color: #d5dbe6;
}

.brief-content h1,
.brief-content h2,
.brief-content h3 {
  margin: 1rem 0 0.5rem;
  font-size: 1rem;
}

.brief-content h1 { font-size: 1.1rem; }
.brief-content p { margin: 0.5rem 0; }
.brief-content ul { margin: 0.5rem 0; padding-left: 1.2rem; }
.brief-content table { margin: 0.5rem 0; }
.brief-content code {
  background: var(--surface2);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  font-size: 0.75rem;
}

.empty {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 1rem 0;
}

.user-label {
  font-size: 0.78rem;
  color: var(--muted);
}

.agent-detail {
  margin-top: 1rem;
  padding: 0.75rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.agent-detail.hidden { display: none; }

.agent-detail h3 {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
}

.agent-detail p {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.cursor-hint ol {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.72rem;
}

.cursor-hint li { margin-bottom: 0.25rem; }

.agent-card { cursor: pointer; }

/* Login page */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at top, #1a2332 0%, var(--bg) 55%);
}

.login-shell {
  width: min(420px, 92vw);
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.75rem;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.35);
}

.login-brand {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.25rem;
}

.login-brand h1 {
  margin: 0;
  font-size: 1.25rem;
}

.login-brand p {
  margin: 0.15rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.login-lead {
  font-size: 0.88rem;
  color: var(--muted);
  margin: 0 0 1.25rem;
}

.login-form label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.85rem;
}

.login-form input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 0.9rem;
}

.login-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-error {
  color: var(--danger);
  font-size: 0.8rem;
  margin: 0 0 0.75rem;
}

.btn.full { width: 100%; }

.login-foot {
  margin: 1.25rem 0 0;
  font-size: 0.72rem;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .agents-panel { border-right: none; border-bottom: 1px solid var(--border); }
  .topbar { flex-wrap: wrap; gap: 0.5rem; padding: 0.75rem 1rem; }
  .topbar-actions { flex-wrap: wrap; }
  .user-label { display: none; }
}

/* ---- Main tabs (Desk / Chat) ---- */
.main-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 20;
}

.main-tab {
  flex: 1;
  max-width: 8rem;
  padding: 0.75rem 1rem;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 2.75rem;
}

.main-tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

body.tab-chat-active .desk-only { display: none; }

.tab-panel.chat-panel.active {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - 120px);
  min-height: 0;
  overflow: hidden;
}

/* ---- Chat (X DM style, desk theme) ---- */
.chat-panel {
  --x-bg: var(--bg);
  --x-border: var(--border);
  --x-text: var(--text);
  --x-muted: var(--muted);
  --x-blue: var(--accent);
  --x-bubble-other: var(--surface2);
  --x-bubble-mine: #3d5afe;
  --x-hover: var(--surface2);
  --x-touch: 2.75rem;
}

.x-dm-layout {
  display: grid;
  grid-template-columns: min(320px, 36vw) 1fr;
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow: hidden;
}

.x-inbox {
  border-right: 1px solid var(--x-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
}

.x-inbox-head {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--x-border);
  flex-shrink: 0;
}

.x-inbox-head h1 {
  margin: 0;
  font-size: 1.1rem;
}

.x-inbox-list { flex: 1; overflow-y: auto; }

.x-inbox-item {
  width: 100%;
  display: flex;
  gap: 0.75rem;
  align-items: center;
  min-height: 4rem;
  padding: 0.75rem 1rem;
  border: none;
  border-bottom: 1px solid var(--x-border);
  background: transparent;
  color: var(--x-text);
  text-align: left;
  cursor: pointer;
  font: inherit;
}

.x-inbox-item.active { background: var(--x-hover); }

.x-avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent), #7856ff);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}

.x-inbox-meta { flex: 1; min-width: 0; }

.x-inbox-top {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.x-inbox-time { color: var(--x-muted); font-size: 0.75rem; }

.x-inbox-preview {
  color: var(--x-muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.x-persist-note {
  margin: 0;
  padding: 0.65rem 1rem calc(0.65rem + env(safe-area-inset-bottom, 0px));
  font-size: 0.72rem;
  color: var(--x-muted);
  border-top: 1px solid var(--x-border);
}

.x-thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--x-bg);
}

.x-thread-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--x-border);
  flex-shrink: 0;
  background: var(--surface);
}

.x-back-btn {
  display: none;
  min-width: var(--x-touch);
  min-height: var(--x-touch);
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--x-text);
  font-size: 1.25rem;
  cursor: pointer;
}

.x-thread-user {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.x-thread-sub {
  display: block;
  color: var(--x-muted);
  font-size: 0.8rem;
  font-weight: 400;
}

.x-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.x-empty {
  margin: auto;
  text-align: center;
  color: var(--x-muted);
}
.x-empty[hidden] { display: none !important; }
.x-empty .muted { font-size: 0.85rem; }

.x-msg-row {
  display: flex;
  flex-direction: column;
  max-width: min(42rem, 85%);
}
.x-msg-row.mine { align-self: flex-end; align-items: flex-end; }
.x-msg-row.theirs { align-self: flex-start; align-items: flex-start; }

.x-bubble {
  padding: 0.65rem 0.9rem;
  border-radius: 1.1rem;
  font-size: 0.95rem;
  line-height: 1.4;
  word-break: break-word;
}
.x-msg-row.mine .x-bubble {
  background: var(--x-bubble-mine);
  color: #fff;
  border-bottom-right-radius: 0.25rem;
}
.x-msg-row.theirs .x-bubble {
  background: var(--x-bubble-other);
  border-bottom-left-radius: 0.25rem;
}

.x-msg-meta {
  font-size: 0.7rem;
  color: var(--x-muted);
  margin-top: 0.15rem;
  padding: 0 0.25rem;
}

.x-compose {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem calc(0.6rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--x-border);
  background: var(--surface);
  flex-shrink: 0;
}

.x-compose #x-compose-input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--x-border);
  border-radius: 999px;
  background: var(--surface2);
  color: var(--x-text);
  padding: 0.7rem 1rem;
  font: inherit;
  font-size: 16px;
  min-height: var(--x-touch);
}

.x-compose #x-compose-input:focus {
  outline: none;
  border-color: var(--x-blue);
}

.x-send-btn {
  min-width: var(--x-touch);
  min-height: var(--x-touch);
  border: none;
  border-radius: 999px;
  background: var(--x-blue);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .x-dm-layout {
    grid-template-columns: 1fr;
    position: relative;
  }

  .x-inbox { height: 100%; }

  .x-thread {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 10;
    height: 100%;
  }

  .x-dm-layout.thread-open .x-inbox { display: none; }
  .x-dm-layout.thread-open .x-thread { display: flex; }

  .x-back-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  body.chat-thread-open {
    overflow: hidden;
  }

  .tab-panel.chat-panel.active {
    height: calc(100dvh - 110px);
  }
}

@media (min-width: 769px) {
  .x-dm-layout .x-thread,
  .x-dm-layout .x-inbox { display: flex; flex-direction: column; }
  .x-back-btn { display: none !important; }
}

/* ---- Decision tree tab ---- */
.tree-panel.active {
  display: block;
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem;
  padding-bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
}

.tree-layout {
  display: grid;
  grid-template-columns: 1fr min(320px, 34vw);
  gap: 1.25rem;
  align-items: start;
}

.decision-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.15rem 1.25rem;
}

.tree-form-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.tree-form-head h2 {
  margin: 0;
  font-size: 1.05rem;
}

.verdict-badge {
  flex-shrink: 0;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0.4rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.verdict-pass { background: #14301f; color: var(--accent2); border-color: #2a4a35; }
.verdict-wait { background: #2a2410; color: var(--warn); border-color: #4a3a18; }
.verdict-reject { background: #301418; color: var(--danger); border-color: #4a2228; }

.tree-section {
  margin-top: 1.1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--border);
}

.tree-section h3 {
  margin: 0 0 0.45rem;
  font-size: 0.88rem;
  font-weight: 600;
}

.tree-note {
  margin: 0 0 0.6rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.check-row {
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  padding: 0.55rem 0.35rem;
  font-size: 0.9rem;
  cursor: pointer;
  min-height: 2.5rem;
}

.check-row input {
  width: 1.15rem;
  height: 1.15rem;
  margin-top: 0.15rem;
  flex-shrink: 0;
  accent-color: var(--accent);
}

.warn-check { color: #d4b483; }

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.decision-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.65rem;
}

.decision-form label.full { grid-column: 1 / -1; }

.decision-form label.check-row {
  flex-direction: row;
  color: var(--text);
  margin-bottom: 0;
  font-size: 0.9rem;
}

.decision-form input[type="text"],
.decision-form input[type="number"],
.decision-form select,
.decision-form textarea {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  min-height: 2.75rem;
}

.decision-form textarea {
  min-height: 4rem;
  resize: vertical;
}

.decision-form input:focus,
.decision-form select:focus,
.decision-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.rr-live {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.rr-live.ok { color: var(--accent2); }
.rr-live.bad { color: var(--danger); }

.tree-actions {
  margin-top: 1.25rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
}

.verdict-reasons {
  flex: 1 1 100%;
  margin: 0 0 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
}

.tree-history {
  background: var(--floor);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  position: sticky;
  top: 4.5rem;
  max-height: calc(100dvh - 6rem);
  overflow-y: auto;
}

.tree-history h2 {
  margin: 0;
  font-size: 0.9rem;
}

.decision-history {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
}

.decision-card {
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 0.55rem;
  cursor: pointer;
  background: var(--surface);
}

.decision-card:active { border-color: var(--accent); }

.decision-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.mini-badge {
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: var(--surface2);
}

.decision-card.verdict-pass .mini-badge { color: var(--accent2); }
.decision-card.verdict-wait .mini-badge { color: var(--warn); }
.decision-card.verdict-reject .mini-badge { color: var(--danger); }

.decision-card-meta {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.decision-card-thesis {
  font-size: 0.8rem;
  margin-top: 0.35rem;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.decision-history .empty {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0.5rem 0;
}

@media (max-width: 900px) {
  .tree-layout {
    grid-template-columns: 1fr;
  }
  .tree-history {
    position: static;
    max-height: none;
  }
  .field-grid {
    grid-template-columns: 1fr;
  }
  .main-tab { max-width: none; }
}
